-
Notifications
You must be signed in to change notification settings - Fork 5
/
ESP32_BeaconSniffer.ino
310 lines (263 loc) · 8.31 KB
/
ESP32_BeaconSniffer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// ESP32 WiFi beacon sniffer
//
// by Michele <[email protected]> Pinassi
// https://github.com/michelep/ESP32_BeaconSniffer
//
// Sighly based on https://github.com/ESP-EOS/ESP32-WiFi-Sniffer, code written to work on ESP32 TTGO with OLED display SSD1306 I2C
//
// Build with TTGO-LoRa32-OLED V1 Arduino template
//
// https://www.espressif.com/en/products/hardware/esp32-devkitc/resources
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_wifi_types.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "logo.h"
// OLED LCD display
// https://github.com/igrr/esp8266-oled-ssd1306
#include <Wire.h>
#include "SSD1306.h"
#define I2C_SCL 4
#define I2C_SDA 5
#define DISPLAY_MAX_W 128
#define DISPLAY_MAX_H 64
SSD1306 display(0x3c, I2C_SDA, I2C_SCL);
//
#define WIFI_CHANNEL_SWITCH_INTERVAL (500)
#define WIFI_CHANNEL_MAX (13)
uint8_t level = 0, channel = 1;
static wifi_country_t wifi_country = {.cc="IT", .schan = 1, .nchan = 13}; // Most recent esp32 library struct
typedef struct {
unsigned protocol:2;
unsigned type:2;
unsigned subtype:4;
unsigned to_ds:1;
unsigned from_ds:1;
unsigned more_frag:1;
unsigned retry:1;
unsigned pwr_mgmt:1;
unsigned more_data:1;
unsigned wep:1;
unsigned strict:1;
} wifi_header_frame_control_t;
// https://carvesystems.com/news/writing-a-simple-esp8266-based-sniffer/
typedef struct {
wifi_header_frame_control_t frame_ctrl;
unsigned duration_id:16;
uint8_t addr1[6]; /* receiver MAC address */
uint8_t addr2[6]; /* sender MAC address */
uint8_t addr3[6]; /* BSSID filtering address */
unsigned sequence_ctrl:16;
uint8_t addr4[6]; /* optional */
} wifi_ieee80211_mac_hdr_t;
typedef struct {
wifi_ieee80211_mac_hdr_t hdr;
uint8_t payload[0]; /* network data ended with 4 bytes csum (CRC32) */
} wifi_ieee80211_packet_t;
typedef struct
{
unsigned interval:16;
unsigned capability:16;
unsigned tag_number:8;
unsigned tag_length:8;
char ssid[0];
uint8_t rates[1];
} wifi_beacon_hdr;
typedef struct {
uint8_t mac[6];
} __attribute__((packed)) mac_addr;
typedef enum
{
ASSOCIATION_REQ,
ASSOCIATION_RES,
REASSOCIATION_REQ,
REASSOCIATION_RES,
PROBE_REQ,
PROBE_RES,
NU1, /* ......................*/
NU2, /* 0110, 0111 not used */
BEACON,
ATIM,
DISASSOCIATION,
AUTHENTICATION,
DEAUTHENTICATION,
ACTION,
ACTION_NACK,
} wifi_mgmt_subtypes_t;
static esp_err_t event_handler(void *ctx, system_event_t *event);
static void wifi_sniffer_init(void);
static void wifi_sniffer_set_channel(uint8_t channel);
static const char *wifi_sniffer_packet_type2str(wifi_promiscuous_pkt_type_t type);
static void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type);
esp_err_t event_handler(void *ctx, system_event_t *event)
{
return ESP_OK;
}
void wifi_sniffer_init(void)
{
nvs_flash_init();
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_country(&wifi_country) ); /* set country for channel range [1, 13] */
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
ESP_ERROR_CHECK( esp_wifi_start() );
esp_wifi_set_promiscuous(true);
esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler);
}
void wifi_sniffer_set_channel(uint8_t channel)
{
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
}
const char * wifi_sniffer_packet_type2str(wifi_promiscuous_pkt_type_t type)
{
switch(type) {
case WIFI_PKT_MGMT: return "MGMT";
case WIFI_PKT_DATA: return "DATA";
default:
case WIFI_PKT_MISC: return "MISC";
}
}
void wifi_sniffer_packet_handler(void* buff, wifi_promiscuous_pkt_type_t type) {
if (type != WIFI_PKT_MGMT)
return;
// https://blog.podkalicki.com/wp-content/uploads/2017/01/esp32_promiscuous_pkt_structure.jpeg
const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff;
const wifi_ieee80211_packet_t *ipkt = (wifi_ieee80211_packet_t *)ppkt->payload;
const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr;
// From https://github.com/SHA2017-badge/bpp/blob/master/esp32-recv/main/bpp_sniffer.c
// https://github.com/n0w/esp8266-simple-sniffer/blob/master/src/main.cpp
char ssid[32] = {0};
const wifi_header_frame_control_t *fctl = (wifi_header_frame_control_t *)&hdr->frame_ctrl;
// Details about beacon frames: https://mrncciew.com/2014/10/08/802-11-mgmt-beacon-frame/
if(fctl->subtype == BEACON) { //beacon
wifi_beacon_hdr *beacon=(wifi_beacon_hdr*)ipkt->payload;
if(beacon->tag_length >= 32) {
strncpy(ssid, beacon->ssid, 31);
} else {
strncpy(ssid, beacon->ssid, beacon->tag_length);
}
Serial.printf("Beacon %s\n",ssid);
addBeacon(ssid, ppkt->rx_ctrl.channel, ppkt->rx_ctrl.rssi);
}
printf("PACKET TYPE=%s, CHAN=%02d, RSSI=%02d,"
" ADDR1=%02x:%02x:%02x:%02x:%02x:%02x,"
" ADDR2=%02x:%02x:%02x:%02x:%02x:%02x,"
" ADDR3=%02x:%02x:%02x:%02x:%02x:%02x\n",
wifi_sniffer_packet_type2str(type),
ppkt->rx_ctrl.channel,
ppkt->rx_ctrl.rssi,
// ADDR1
hdr->addr1[0],hdr->addr1[1],hdr->addr1[2],
hdr->addr1[3],hdr->addr1[4],hdr->addr1[5],
// ADDR2
hdr->addr2[0],hdr->addr2[1],hdr->addr2[2],
hdr->addr2[3],hdr->addr2[4],hdr->addr2[5],
// ADDR3
hdr->addr3[0],hdr->addr3[1],hdr->addr3[2],
hdr->addr3[3],hdr->addr3[4],hdr->addr3[5]
);
}
// ************************************
// DEBUG()
//
// ************************************
#define __DEBUG__
void DEBUG(String message) {
#ifdef __DEBUG__
Serial.println(message);
#endif
}
#include <LinkedList.h>
class WiFiBeacon {
public:
char name[32];
int rssi;
uint8_t channel;
char mac[6];
uint8_t lastseen;
};
LinkedList<WiFiBeacon*> myBeacons = LinkedList<WiFiBeacon*>();
void addBeacon(char ssid[],uint8_t channel, int rssi) {
WiFiBeacon *beacon;
for(int i = 0; i < myBeacons.size(); i++) {
beacon = myBeacons.get(i);
if(strncmp(beacon->name,ssid,32)==0) {
// update beacon data and return
beacon->rssi = rssi;
beacon->channel = channel;
beacon->lastseen = 0;
Serial.printf("Update beacon %s\n",ssid);
return;
}
}
// add new beacon
beacon = new WiFiBeacon();
strncpy(beacon->name,ssid,32);
beacon->rssi = rssi;
beacon->lastseen = 0;
myBeacons.add(beacon);
Serial.printf("Add new beacon %s on channel %d\n",ssid,channel);
}
hw_timer_t *timer = NULL;
bool timerChannel=false;
void IRAM_ATTR onTimer(){
timerChannel=true;
}
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(115200);
delay(10);
// Initialize OLED display
display.init();
display.drawXbm(0, 0, logo_width, logo_height, logo_bits);
display.display();
delay(5000);
wifi_sniffer_init();
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
}
// the loop function runs over and over again forever
void loop() {
if(timerChannel) {
WiFiBeacon *beacon;
// Age for all beacons detected...
for(int i = 0; i < myBeacons.size(); i++) {
beacon = myBeacons.get(i);
beacon->lastseen++;
if(beacon->lastseen > 60) {
// older that 60 secs? remove it!
Serial.printf("Remove lost beacon %s\n",beacon->name);
myBeacons.remove(i);
}
}
// Set channel
wifi_sniffer_set_channel(channel);
channel = (channel % WIFI_CHANNEL_MAX) + 1;
// Update display
display.clear();
display.setColor(WHITE);
display.drawHorizontalLine(0, 0, round((DISPLAY_MAX_W / WIFI_CHANNEL_MAX)*channel));
display.drawHorizontalLine(0, 1, round((DISPLAY_MAX_W / WIFI_CHANNEL_MAX)*channel));
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 2, String(channel));
display.drawHorizontalLine(0, 14, DISPLAY_MAX_W);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 16, "Total APs: "+String(myBeacons.size()));
// Display the 4 nearest APs..
for(int i=0; i<4; i++) {
beacon = myBeacons.get(i);
display.drawString(0, 26+(10*i), String(beacon->name));
}
display.display();
timerChannel=false;
}
}