-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathPacketMonitor32.ino
316 lines (256 loc) · 8.02 KB
/
PacketMonitor32.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
311
312
313
314
315
316
/* uncomment if the default 4 bit mode doesn't work */
/* ------------------------------------------------ */
// #define BOARD_HAS_1BIT_SDMMC true // forces 1bit mode for SD MMC
/* ------------------------------------------------ */
#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 <stdio.h>
#include <string>
#include <cstddef>
#include <Wire.h>
#include <Preferences.h>
using namespace std;
/* ===== compile settings ===== */
#define MAX_CH 14 // 1 - 14 channels (1-11 for US, 1-13 for EU and 1-14 for Japan)
#define SNAP_LEN 2324 // max len of each recieved packet
#define BUTTON_PIN 5 // button to change the channel
#define USE_DISPLAY // comment out if you don't want to use the OLED display
#define FLIP_DISPLAY // comment out if you don't like to flip it
#define SDA_PIN 26
#define SCL_PIN 27
#define MAX_X 128
#define MAX_Y 51
#if CONFIG_FREERTOS_UNICORE
#define RUNNING_CORE 0
#else
#define RUNNING_CORE 1
#endif
#ifdef USE_DISPLAY
#include "SH1106.h"
#endif
#include "FS.h"
#include "SD_MMC.h"
#include "Buffer.h"
esp_err_t event_handler(void* ctx, system_event_t* event) {
return ESP_OK;
}
/* ===== run-time variables ===== */
Buffer sdBuffer;
#ifdef USE_DISPLAY
SH1106 display(0x3c, SDA_PIN, SCL_PIN);
#endif
Preferences preferences;
bool useSD = false;
bool buttonPressed = false;
bool buttonEnabled = true;
uint32_t lastDrawTime;
uint32_t lastButtonTime;
uint32_t tmpPacketCounter;
uint32_t pkts[MAX_X]; // here the packets per second will be saved
uint32_t deauths = 0; // deauth frames per second
unsigned int ch = 1; // current 802.11 channel
int rssiSum;
/* ===== functions ===== */
double getMultiplicator() {
uint32_t maxVal = 1;
for (int i = 0; i < MAX_X; i++) {
if (pkts[i] > maxVal) maxVal = pkts[i];
}
if (maxVal > MAX_Y) return (double)MAX_Y / (double)maxVal;
else return 1;
}
void setChannel(int newChannel) {
ch = newChannel;
if (ch > MAX_CH || ch < 1) ch = 1;
preferences.begin("packetmonitor32", false);
preferences.putUInt("channel", ch);
preferences.end();
esp_wifi_set_promiscuous(false);
esp_wifi_set_channel(ch, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous_rx_cb(&wifi_promiscuous);
esp_wifi_set_promiscuous(true);
}
bool setupSD() {
if (!SD_MMC.begin()) {
Serial.println("Card Mount Failed");
return false;
}
uint8_t cardType = SD_MMC.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD_MMC card attached");
return false;
}
Serial.print("SD_MMC Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);
return true;
}
void wifi_promiscuous(void* buf, wifi_promiscuous_pkt_type_t type) {
wifi_promiscuous_pkt_t* pkt = (wifi_promiscuous_pkt_t*)buf;
wifi_pkt_rx_ctrl_t ctrl = (wifi_pkt_rx_ctrl_t)pkt->rx_ctrl;
if (type == WIFI_PKT_MGMT && (pkt->payload[0] == 0xA0 || pkt->payload[0] == 0xC0 )) deauths++;
if (type == WIFI_PKT_MISC) return; // wrong packet type
if (ctrl.sig_len > SNAP_LEN) return; // packet too long
uint32_t packetLength = ctrl.sig_len;
if (type == WIFI_PKT_MGMT) packetLength -= 4; // fix for known bug in the IDF https://github.com/espressif/esp-idf/issues/886
//Serial.print(".");
tmpPacketCounter++;
rssiSum += ctrl.rssi;
if (useSD) sdBuffer.addPacket(pkt->payload, packetLength);
}
void draw() {
#ifdef USE_DISPLAY
double multiplicator = getMultiplicator();
int len;
int rssi;
if (pkts[MAX_X - 1] > 0) rssi = rssiSum / (int)pkts[MAX_X - 1];
else rssi = rssiSum;
display.clear();
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString( 10, 0, (String)ch);
display.drawString( 14, 0, ("|"));
display.drawString( 30, 0, (String)rssi);
display.drawString( 34, 0, ("|"));
display.drawString( 82, 0, (String)tmpPacketCounter);
display.drawString( 87, 0, ("["));
display.drawString(106, 0, (String)deauths);
display.drawString(110, 0, ("]"));
display.drawString(114, 0, ("|"));
display.drawString(128, 0, (useSD ? "SD" : ""));
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString( 36, 0, ("Pkts:"));
display.drawLine(0, 63 - MAX_Y, MAX_X, 63 - MAX_Y);
for (int i = 0; i < MAX_X; i++) {
len = pkts[i] * multiplicator;
display.drawLine(i, 63, i, 63 - (len > MAX_Y ? MAX_Y : len));
if (i < MAX_X - 1) pkts[i] = pkts[i + 1];
}
display.display();
#endif
}
/* ===== main program ===== */
void setup() {
// Serial
Serial.begin(115200);
// Settings
preferences.begin("packetmonitor32", false);
ch = preferences.getUInt("channel", 1);
preferences.end();
// System & WiFi
nvs_flash_init();
tcpip_adapter_init();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
//ESP_ERROR_CHECK(esp_wifi_set_country(WIFI_COUNTRY_EU));
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_channel(ch, WIFI_SECOND_CHAN_NONE);
// SD card
sdBuffer = Buffer();
if (setupSD())
sdBuffer.open(&SD_MMC);
// I/O
pinMode(BUTTON_PIN, INPUT_PULLUP);
// display
#ifdef USE_DISPLAY
display.init();
#ifdef FLIP_DISPLAY
display.flipScreenVertically();
#endif
/* show start screen */
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(6, 6, "PacketMonitor32");
display.setFont(ArialMT_Plain_10);
display.drawString(24, 34, "Made with <3 by");
display.drawString(29, 44, "@Spacehuhn");
display.display();
delay(1000);
#endif
// second core
xTaskCreatePinnedToCore(
coreTask, /* Function to implement the task */
"coreTask", /* Name of the task */
2500, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
NULL, /* Task handle. */
RUNNING_CORE); /* Core where the task should run */
// start Wifi sniffer
esp_wifi_set_promiscuous_rx_cb(&wifi_promiscuous);
esp_wifi_set_promiscuous(true);
}
void loop() {
vTaskDelay(portMAX_DELAY);
}
void coreTask( void * p ) {
uint32_t currentTime;
while (true) {
currentTime = millis();
/* bit of spaghetti code, have to clean this up later :D */
// check button
if (digitalRead(BUTTON_PIN) == LOW) {
if (buttonEnabled) {
if (!buttonPressed) {
buttonPressed = true;
lastButtonTime = currentTime;
} else if (currentTime - lastButtonTime >= 2000) {
if (useSD) {
useSD = false;
sdBuffer.close(&SD_MMC);
draw();
} else {
if (setupSD())
sdBuffer.open(&SD_MMC);
draw();
}
buttonPressed = false;
buttonEnabled = false;
}
}
} else {
if (buttonPressed) {
setChannel(ch + 1);
draw();
}
buttonPressed = false;
buttonEnabled = true;
}
// save buffer to SD
if (useSD)
sdBuffer.save(&SD_MMC);
// draw Display
if ( currentTime - lastDrawTime > 1000 ) {
lastDrawTime = currentTime;
// Serial.printf("\nFree RAM %u %u\n", heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT), heap_caps_get_minimum_free_size(MALLOC_CAP_32BIT));// for debug purposes
pkts[MAX_X - 1] = tmpPacketCounter;
draw();
Serial.println((String)pkts[MAX_X - 1]);
tmpPacketCounter = 0;
deauths = 0;
rssiSum = 0;
}
// Serial input
if (Serial.available()) {
ch = Serial.readString().toInt();
if (ch < 1 || ch > 14) ch = 1;
setChannel(ch);
}
}
}