-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMACSniff32.ino
135 lines (125 loc) · 3.91 KB
/
MACSniff32.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
#include "esp_wifi.h"
//#define LED 2
bool debugMode = false;
String macList[100][3]; //macList stores MAC, timer & channel for up to 100 MACs
String macList2[10][2] = {
{"MyMAC1","E8:DF:70:C3:5C:4B"},
{"MyMAC2","0C:60:76:9C:3F:C2"}
};
int maxMacs = sizeof macList / sizeof macList[0];
int maxMacs2 = sizeof macList2 / sizeof macList2[0];
int knownMacs = 0;
int channel = 1;
int timer = 60; // Set to 0 or less for infinite duration of entries
const wifi_promiscuous_filter_t filt={
.filter_mask=WIFI_PROMIS_FILTER_MASK_MGMT|WIFI_PROMIS_FILTER_MASK_DATA
};
typedef struct {
uint8_t mac[6];
} __attribute__((packed)) MacAddr;
typedef struct {
int16_t fctl;
int16_t duration;
MacAddr da;
MacAddr sa;
MacAddr bssid;
int16_t seqctl;
unsigned char payload[];
} __attribute__((packed)) WifiMgmtHdr;
void sniffer(void* buf, wifi_promiscuous_pkt_type_t type) {
int channel1 = channel;
wifi_promiscuous_pkt_t *p = (wifi_promiscuous_pkt_t*)buf;
int len = p->rx_ctrl.sig_len;
WifiMgmtHdr *wh = (WifiMgmtHdr*)p->payload;
len -= sizeof(WifiMgmtHdr);
if (len < 0) return;
String packet;
String mac;
String info;
int fctl = ntohs(wh->fctl);
for(int i=0;i<=20;i++){ // i <= len
String hpay=String(p->payload[i],HEX);
if(hpay.length()==1)hpay="0"+hpay;
packet += hpay;
}
for(int i=10;i<=15;i++){ // extract MAC address
String hpay=String(p->payload[i],HEX);
if(hpay.length()==1)hpay="0"+hpay;
mac += hpay;
if(i<15)mac+=":";
}
mac.toUpperCase();
info="MAC = " + mac + " channel=" + channel1 + " in " + packet+"(...)";
int added = 0;
for(int i=0;i<=maxMacs;i++){ // check if MAC address is known
if(mac == macList[i][0]){ // if the MAC address is known, reset the time remaining
macList[i][1] = String(timer);
added = 1;
}
}
if(added == 0){ // Add new entry to the array if added==0
macList[knownMacs][0] = mac;
macList[knownMacs][1] = String(timer);
macList[knownMacs][2] = String(channel);
if (debugMode == true)
Serial.println(info);
else
Serial.printf("\r\n%d MACs detected.\r\n",knownMacs);
knownMacs ++;
if(knownMacs > maxMacs){
Serial.println("Warning: MAC overflow");
knownMacs = 0;
}
}
}
void updateTimer(){ // update time remaining for each known device
for(int i=0;i<maxMacs;i++){
if(!(macList[i][0] == "")){
int newTime = (macList[i][1].toInt());
newTime --;
if(newTime <= 0){
macList[i][1] = String(timer);
}else{
macList[i][1] = String(newTime);
}
}
}
}
void showMyMACs(){ // show the MACs that are on both macList and macList2.
String res = "";
int counter=0;
for(int i=0;i<maxMacs;i++){
if(!(macList[i][0] == "")){
for(int j=0;j<maxMacs2;j++){
if(macList[i][0] == macList2[j][1]){
counter += 1;
res += (String(counter) + ". MAC=" + macList[i][0] + " ALIAS=" + macList2[j][0] + " Channel=" + macList[i][2] + " Timer=" + macList[i][1] + "\r\n");
Serial.print("\r\n"+(String(counter) + ". MAC=" + macList[i][0] + " ALIAS=" + macList2[j][0] + " Channel=" + macList[i][2] + " Timer=" + macList[i][1] + "\r\n"));
//digitalWrite(LED, HIGH);
}
}
}
}
}
void setup() {
Serial.begin(115200);
Serial.printf("\n\nSDK version:%s\n\r", system_get_sdk_version());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
esp_wifi_set_storage(WIFI_STORAGE_RAM);
esp_wifi_set_mode(WIFI_MODE_NULL);
esp_wifi_start();
esp_wifi_set_promiscuous(true);
esp_wifi_set_promiscuous_filter(&filt);
esp_wifi_set_promiscuous_rx_cb(&sniffer);
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
//pinMode(LED, OUTPUT);
}
void loop() {
if(channel > 14) channel = 1;
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
delay(1000);
if (timer>0) updateTimer();
if (debugMode == false) showMyMACs();
channel++;
}