esp8266混杂模式下的接收数据回调函数 promisc_cb 解析数据 串口输出。
#include <ESP8266WiFi.h>
#include "./sniffer.h"
#define disable 0
#define CHANNEL 1
#define BAUD_RATE 115200
#define CHANNEL_HOPPING true //if true it will scan on all channels
#define MAX_CHANNEL 11 //(only necessary if channelHopping is true)
#define HOP_INTERVAL 214 //in ms (only necessary if channelHopping is true)
// Channel to perform deauth
int ch = CHANNEL;
unsigned long lastChannelChange = 0;
uint8_t channel = 1;
// Access point MAC to deauth
uint8_t ap[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
// Client MAC to deauth
uint8_t client[6] = {0xc4, 0x6a, 0xb7, 0x9f, 0xcc, 0x34};
// Sequence number of a packet from AP to client
uint16_t seq_n = 0;
// Packet buffer
uint8_t packet_buffer[64];
uint8_t temp_mac[6] = {0xc4, 0x6a, 0xb7, 0x9f, 0xcc, 0x34};
extern "C" {
#include "user_interface.h"
}
void promisc_cb(uint8_t *buf, uint16_t len)
{
if (len == 12){
struct RxControl *sniffer = (struct RxControl*) buf;
} else if (len == 128) {
struct sniffer_buf2 *sniffer = (struct sniffer_buf2*) buf;
} else {
struct sniffer_buf *sniffer = (struct sniffer_buf*) buf;
int i=0;
// Check MACs
// 如果MAC地址和上一次一样就返回
if(0==memcmp(temp_mac, &sniffer->buf[4], 6)){
return;
}
// 缓存上次的MAC,避免重复打印
for (i=0; i<6; i++){
temp_mac[i] = sniffer->buf[i+4];
}
#if SNIFFER_TEST
Serial.printf("-> %3d: %d", wifi_get_channel(), len);
printmac(sniffer->buf, 4);
printmac(sniffer->buf, 10);
Serial.printf("\n");
#endif
// 判断client
for (i=0; i<6; i++) if (sniffer->buf[i+4] != client[i]) return;
printmac(sniffer->buf, 4);
Serial.printf("\r\n");
Serial.printf("\trssi:%d\r\n", sniffer->rx_ctrl.rssi);
Serial.printf("\tchannel:%d\r\n", sniffer->rx_ctrl.channel);
Serial.printf("\trate:%d\r\n", sniffer->rx_ctrl.rate);
Serial.printf("\tsig_mode:%d\r\n",sniffer->rx_ctrl.sig_mode);
// 判断AP
for (i=0; i<6; i++) if (sniffer->buf[i+10] != ap[i]) return;
printmac(sniffer->buf, 10);
//os_timer_disarm(&channelHop_timer);
// Update sequence number
#if DEAUTH_ENABLE
seq_n = sniffer->buf[23] * 0xFF + sniffer->buf[22];
#endif
}
}
void setup() {
Serial.begin(BAUD_RATE);
delay(2000);
Serial.println();
Serial.println("<<START>>");
wifi_set_opmode(STATION_MODE);
wifi_promiscuous_enable(0);
WiFi.disconnect();
wifi_set_promiscuous_rx_cb(promisc_cb); // Set up promiscuous callback
wifi_set_channel(channel);
wifi_promiscuous_enable(1);
}
void loop() {
/* Channel Hopping */
if(CHANNEL_HOPPING){
unsigned long currentTime = millis();
if(currentTime - lastChannelChange >= HOP_INTERVAL){
lastChannelChange = currentTime;
ch++; //increase channel
if(ch > MAX_CHANNEL) ch = 1;
wifi_set_channel(ch); //switch to new channel
}
}
// channel = random(1,12);
// wifi_set_channel(channel);
}