-
Notifications
You must be signed in to change notification settings - Fork 0
/
beacondump.c
354 lines (319 loc) · 8.15 KB
/
beacondump.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <pcap/pcap.h>
#include "ieee802_11_radio.h"
typedef struct {
int freq;
int channel;
} one_freq2channel_t;
one_freq2channel_t freq2channel[] = {
{ 5160, 32 },
{ 5170, 34 },
{ 5180, 36 },
{ 5190, 38 },
{ 5200, 40 },
{ 5210, 42 },
{ 5220, 44 },
{ 5230, 46 },
{ 5240, 48 },
{ 5250, 50 },
{ 5260, 52 },
{ 5270, 54 },
{ 5280, 56 },
{ 5290, 58 },
{ 5300, 60 },
{ 5310, 62 },
{ 5320, 64 },
{ 5340, 68 },
{ 5480, 96 },
{ 5500, 100 },
{ 5510, 102 },
{ 5520, 104 },
{ 5530, 106 },
{ 5540, 108 },
{ 5550, 110 },
{ 5560, 112 },
{ 5570, 114 },
{ 5580, 116 },
{ 5590, 118 },
{ 5600, 120 },
{ 5610, 122 },
{ 5620, 124 },
{ 5630, 126 },
{ 5640, 128 },
{ 5660, 132 },
{ 5670, 134 },
{ 5680, 136 },
{ 5690, 138 },
{ 5700, 140 },
{ 5710, 142 },
{ 5720, 144 },
{ 5745, 149 },
{ 5755, 151 },
{ 5765, 153 },
{ 5775, 155 },
{ 5785, 157 },
{ 5795, 159 },
{ 5805, 161 },
{ 5815, 163 },
{ 5825, 165 },
{ 5835, 167 },
{ 5845, 169 },
{ 5855, 171 },
{ 5865, 173 },
{ 5875, 175 },
{ 5885, 177 },
{ 5900, 180 },
{ 5910, 182 },
{ 5920, 184 },
{ 5935, 187 },
{ 5940, 188 },
{ 5945, 189 },
{ 5960, 192 },
{ 5980, 196 },
{ 0, 0 },
};
int ieee80211_field_size[32] = {
sizeof(uint64_t), // IEEE80211_RADIOTAP_TSFT
sizeof(uint8_t), // IEEE80211_RADIOTAP_FLAGS
sizeof(uint8_t), // IEEE80211_RADIOTAP_RATE
2*sizeof(uint16_t), // IEEE80211_RADIOTAP_CHANNEL
sizeof(uint16_t), // IEEE80211_RADIOTAP_FHSS
sizeof(int8_t), // IEEE80211_RADIOTAP_DBM_ANTSIGNAL
sizeof(int8_t), // IEEE80211_RADIOTAP_DBM_ANTNOISE
sizeof(uint16_t), // IEEE80211_RADIOTAP_LOCK_QUALITY
sizeof(uint16_t), // IEEE80211_RADIOTAP_TX_ATTENUATION
sizeof(uint16_t), // IEEE80211_RADIOTAP_DB_TX_ATTENUATION
sizeof(int8_t), // IEEE80211_RADIOTAP_DBM_TX_POWER
sizeof(uint8_t), // IEEE80211_RADIOTAP_ANTENNA
sizeof(uint8_t), // IEEE80211_RADIOTAP_DB_ANTSIGNAL
sizeof(uint8_t), // IEEE80211_RADIOTAP_DB_ANTNOISE
};
int convert_freq2_channel(int freq) {
int i = 0;
while (freq2channel[i].channel) {
if (freq == freq2channel[i].freq) {
return freq2channel[i].channel;
}
i++;
}
return freq;
}
void *get80211field(const u_char *pkt,
enum ieee80211_radiotap_type field)
{
struct ieee80211_radiotap_header* hdr = (struct ieee80211_radiotap_header*)pkt;
int i;
int offs = sizeof(struct ieee80211_radiotap_header);
char *pc = (char *)hdr;
for(i = 0; i < field; i++) {
if(hdr->it_present & (1 << i)) {
offs += ieee80211_field_size[i];
}
}
if((ieee80211_field_size[field] > 1) && (offs %2)) {
offs++;
}
pc = pc + offs;
return (void *)pc;
}
void *get80211payload(const u_char *pkt) {
struct ieee80211_radiotap_header* hdr = (struct ieee80211_radiotap_header*)pkt;
char *pc = (char *)hdr;
return pc + hdr->it_len;
}
void hex(void *ptr, int len) {
int i = 0;
uint8_t *pc = ptr;
int val = -1;
char str[20];
memset(str, 0, sizeof(str));
while(len > 0) {
do {
if(0 == i%16) {
printf("%04x ", i);
}
val = len > 0 ? *pc : -1;
if (val >= 0) {
printf("%02x ", val );
} else {
printf(" ");
}
str[i%16] = (val > 0x40 && val < 0x7f) ? val : '.';
pc++;
i++;
len--;
if(0 == i%16) {
printf(" %s\n", str);
}
} while (i%16);
}
}
char **EXCLUDE_SSID = NULL;
int EXCLUDE_SSID_COUNT = 0;
char *BLACK = "\x1b[30m";
char *GREEN = "\x1b[32m";
char *YELLOW= "\x1b[33m";
char *RED = "\x1b[31m";
char *BRIGHT_RED = "\x1b[31;1m";
char *RESET= "\x1b[0m";
void got_packet(u_char *args, const struct pcap_pkthdr *header,
const u_char *packet) {
uint16_t *pfreq;
uint64_t *pts;
uint64_t curr_ts;
uint8_t *prate;
uint8_t *pflags;
int8_t *psignal, *pnoise;
struct timeval tv;
uint8_t *pc = get80211payload(packet);
uint8_t *bss = pc + 16;
uint16_t *pseq = (uint16_t *)(bss+6);
uint8_t *ptag = bss+20;
char bssid[256];
int i;
gettimeofday(&tv,NULL);
curr_ts = 1000000 * tv.tv_sec + tv.tv_usec;
if(*pc != 0x80) {
// Not a beacon
return;
}
pfreq = get80211field(packet, IEEE80211_RADIOTAP_CHANNEL);
prate = get80211field(packet, IEEE80211_RADIOTAP_RATE);
pflags = get80211field(packet, IEEE80211_RADIOTAP_FLAGS);
pts = get80211field(packet, IEEE80211_RADIOTAP_TSFT);
psignal = get80211field(packet, IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
pnoise = get80211field(packet, IEEE80211_RADIOTAP_DBM_ANTNOISE);
if (*pflags & 0x40) {
// Bad FCS
return;
}
if(0 == *ptag) {
int i;
memset(bssid, 0, sizeof(bssid));
memcpy(bssid, ptag+2, *(ptag+1));
for(i=0; i<*(ptag+1); i++) {
if(((uint8_t)bssid[i] >= 0x80) || (bssid[i] < 0x20)) {
bssid[i] = '.';
}
}
bssid[64] = 0;
} else {
memset(bssid, 0, sizeof(bssid));
}
for(i=0; i<EXCLUDE_SSID_COUNT; i++) {
if(0 == strncmp(EXCLUDE_SSID[i], bssid, sizeof(bssid))) {
// we do not want to print this
return;
}
}
/*
printf("%20lld len %-5d freq %4d rate %-5d s/n: %d %d seq %03x BSS %02x%02x%02x%02x%02x%02x | %s\n",
curr_ts, header->len, *pfreq, 500*(*prate), *psignal, *pnoise, *pseq >> 4,
bss[0],bss[1],bss[2],bss[3],bss[4],bss[5], bssid);
*/
char *color_start = "";
char *color_end = RESET;
if (*psignal > -40) {
color_start = BRIGHT_RED;
} else if (*psignal > -50) {
color_start = YELLOW;
}
printf("%20lld channel %4d %ssignal%s: %d noise: %d BSS %02x%02x%02x%02x%02x%02x | %s\n",
curr_ts, convert_freq2_channel(*pfreq), color_start, color_end, *psignal, *pnoise,
bss[0],bss[1],bss[2],bss[3],bss[4],bss[5], bssid);
fflush(stdout);
// hex((void *)packet, header->len);
}
void capture(char *dev) {
pcap_t *pcap;
char errbuf[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr header; /* The header that pcap gives us */
const u_char *packet; /* The actual packet */
pcap_if_t *devlist = 0;
int v_set_monitor_mode = 1;
int attempt_count = 3;
if(NULL == dev) {
if (pcap_findalldevs(&devlist, errbuf)) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
exit(1);
}
dev = devlist->name;
}
for(;;) {
printf("Attempting to open '%s', set_monitor_mode: %d...\n", dev, v_set_monitor_mode);
pcap = pcap_create(dev, errbuf);
if (!pcap) {
perror("pcap_set_create");
}
int res = pcap_set_rfmon(pcap, v_set_monitor_mode);
if (res) {
perror("pcap_set_rfmon");
}
res = pcap_set_promisc(pcap, 1);
if (res) {
perror("pcap_set_promisc");
}
res = pcap_set_buffer_size(pcap, 1 * 1024 * 1024);
if (res) {
perror("pcap_set_buffer_size");
}
res = pcap_set_timeout(pcap, 1);
if (res) {
perror("pcap_set_timeout");
}
res = pcap_set_snaplen(pcap, 16384);
if (res) {
pcap_perror(pcap, "pcap_set_snaplen");
}
res = pcap_activate(pcap);
if (res) {
pcap_perror(pcap, "pcap_activate");
switch(res) {
case PCAP_ERROR_RFMON_NOTSUP:
printf("Trying without setting monitor mode...\n");
v_set_monitor_mode = 0;
break;
default:
printf("Unhandled error: %x\n", res);
exit(1);
}
} else {
printf("Open successful.\n");
break;
}
if (0 == --attempt_count) {
printf("Tried too many times. Giving up.");
}
}
if(DLT_IEEE802_11_RADIO == pcap_datalink(pcap)) {
pcap_loop(pcap, 0, got_packet, 0);
} else {
fprintf(stderr, "Could not initialize a IEEE802_11_RADIO packet capture for interface %s\n", dev);
perror("wifi");
}
}
void list_devices() {
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *devlist = 0;
if (0 == pcap_findalldevs(&devlist, errbuf)) {
while(devlist) {
printf("%020s: %s\n", devlist->name,
devlist->description ? devlist->description : "[no description]");
devlist = devlist->next;
}
}
}
int main(int argc, char *argv[]) {
if(argc > 1) {
if (argc > 2) {
EXCLUDE_SSID = argv + 2;
EXCLUDE_SSID_COUNT = argc - 2;
}
capture(argv[1]);
} else {
list_devices();
capture(NULL);
}
}