/
wifi-sniff.c
591 lines (533 loc) · 13.2 KB
/
wifi-sniff.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if.h>
#define BUFSIZE 2048
#define PARAMS "bcdhst"
#define USAGE "Usage: %s [-" PARAMS "] [interface] [maclist...]\n"
#define HELP USAGE "\nSimple wifi interface monitoring\n\n" \
"[maclist...] which mac addresses to monitor, empty = all\n" \
"[interface] which interface to monitor\n" \
" -b don't output beacon frames\n" \
" -c enables colors\n" \
" -d include differential timestamps (inaccurate)\n" \
" -h display this help and exit\n" \
" -m don't output management frames\n" \
" -s simple names\n" \
" -t include timestamps (inaccurate)\n" \
""
#define IEEE80211_FTYPE_MGMT 0x0000
#define IEEE80211_FTYPE_CTL 0x0004
#define IEEE80211_FTYPE_DATA 0x0008
#define IEEE80211_STYPE_CTL_EXT 0x006
#define IEEE80211_STYPE_BACK_REQ 0x008
#define IEEE80211_STYPE_BACK 0x009
#define IEEE80211_STYPE_PSPOLL 0x00A
#define IEEE80211_STYPE_RTS 0x00B
#define IEEE80211_STYPE_CTS 0x00C
#define IEEE80211_STYPE_ACK 0x00D
#define IEEE80211_STYPE_CFEND 0x00E
#define IEEE80211_STYPE_CFENDACK 0x00F
#define IEEE80211_STYPE_ASSOC_REQ 0x000
#define IEEE80211_STYPE_ASSOC_RESP 0x001
#define IEEE80211_STYPE_REASSOC_REQ 0x002
#define IEEE80211_STYPE_REASSOC_RESP 0x003
#define IEEE80211_STYPE_PROBE_REQ 0x004
#define IEEE80211_STYPE_PROBE_RESP 0x005
#define IEEE80211_STYPE_BEACON 0x008
#define IEEE80211_STYPE_ATIM 0x009
#define IEEE80211_STYPE_DISASSOC 0x00A
#define IEEE80211_STYPE_AUTH 0x00B
#define IEEE80211_STYPE_DEAUTH 0x00C
#define IEEE80211_STYPE_ACTION 0x00D
#define CNORMAL "\033[0m"
#define CRED "\033[31m"
#define CGREEN "\033[32m"
#define CYELLOW "\033[33m"
#define CBLUE "\033[34m"
#define CMAGENTA "\033[35m"
#define CCYAN "\033[36m"
#define CWHITE "\033[37m"
//char * colors[] = {CRED, CGREEN, CYELLOW, CBLUE, CMAGENTA, CCYAN, CWHITE};
typedef int bool;
#define true 1
#define false 0
int sock;
bool opt_timestamp = false;
bool opt_diffstamp = false;
bool opt_simpleaddr = false;
bool opt_nobeacon = false;
bool opt_nomgmt = false;
bool opt_color = false;
int ccolor = 0;
clock_t lastts = 0;
int maclist_count;
unsigned char * maclist = NULL; //contains mac list
bool isinmaclist(unsigned char * addr)
{
if(maclist == NULL) return true;
for(int i = 0; i < maclist_count; i++)
{
if(memcmp(maclist + i*6, addr, 6) == 0) return true;
}
return false;
}
struct station
{
int color;
unsigned char addr[6];
int txcount;
int rxcount;
char *ssid;
struct station * next;
};
struct station * sta_head = NULL;
void sta_add(struct station ** head, int color, unsigned char * addr)
{
if (*head == NULL)
{
*head = (struct station*)malloc(sizeof(struct station));
memset(*head, 0, sizeof(struct station));
(*head)->color = color;
(*head)->next = NULL;
memcpy((*head)->addr, addr, 6);
} else sta_add(&((*head)->next), color, addr);
}
struct station* sta_find(struct station * head, unsigned char addr[6])
{
if(head == NULL) return NULL;
else if (head->addr != NULL && memcmp(head->addr, addr, 6) == 0) return head;
else return sta_find(head->next, addr);
}
struct wframe
{
bool nowifi;
int ts, diffts;
int type;
int stype;
uint16_t nav; //nav in usec
unsigned char addr1[6];
unsigned char addr2[6];
unsigned char addr3[6];
unsigned char* rxaddr;
struct station * rxsta;
unsigned char* txaddr;
struct station * txsta;
bool retry;
bool powermgmt;
};
bool isbeacon(struct wframe* f)
{
return f->type == IEEE80211_FTYPE_MGMT &&
f->stype == IEEE80211_STYPE_BEACON;
}
static bool keepRunning = true;
void print_stalist(struct station*);
void intHandler(int dummy)
{
if(keepRunning == false)
{
printf("Station List: \n");
print_stalist(sta_head->next);
exit(0);
}
keepRunning = false;
}
struct timespec tsdiff(struct timespec start, struct timespec end)
{
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
const char * subtype_name(int type, int stype)
{
if (type == IEEE80211_FTYPE_CTL) {
if (stype == IEEE80211_STYPE_RTS)
return "RTS";
if (stype == IEEE80211_STYPE_CTS)
return "CTS";
if (stype == IEEE80211_STYPE_ACK)
return "ACK";
else
return "Unknown Control";
}
else if (type == IEEE80211_FTYPE_MGMT) {
if (stype == IEEE80211_STYPE_BEACON)
return "Beacon";
if (stype == IEEE80211_STYPE_PROBE_REQ)
return "Probe Req";
if (stype == IEEE80211_STYPE_PROBE_RESP)
return "Probe Resp";
if (stype == IEEE80211_STYPE_ASSOC_REQ)
return "Assoc Req";
if (stype == IEEE80211_STYPE_ASSOC_RESP)
return "Assoc Resp";
if (stype == IEEE80211_STYPE_AUTH)
return "Auth";
if (stype == IEEE80211_STYPE_DEAUTH)
return "Deauth";
else
return "Unknown Management";
}
else
return "Data";
}
int sock_bind(const char * ifname) {
struct sockaddr_ll sll;
struct ifreq ifr; bzero(&sll , sizeof(sll));
bzero(&ifr , sizeof(ifr));
strncpy((char *)ifr.ifr_name , ifname, IFNAMSIZ);
//copy device name to ifr
if((ioctl(sock, SIOCGIFINDEX, &ifr)) == -1)
{
perror("Unable to find interface index");
exit(-1);
}
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifr.ifr_ifindex;
sll.sll_protocol = htons(ETH_P_ALL);
if((bind(sock , (struct sockaddr *)&sll , sizeof(sll))) ==-1)
{
perror("bind: ");
exit(-1);
}
return 0;
}
int sock_open()
{
sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if(sock == -1) {
if(errno == EPERM)
printf("You require root priviliges to monitor network data. ");
else
printf("Socket creation failed: %d", errno);
return errno;
}
return 0;
}
void sock_close()
{
close(sock);
}
void print_addr(unsigned char* addr)
{
for(int i = 0; i < 6; i++)
i == 5 ? printf("%02x", addr[i]) : printf("%02x:", addr[i]);
}
struct wframe * buffertowframe(char * buffer, int size)
{
//Let's process the packet.
//First remove radiotap.
struct wframe *frame;
frame = (struct wframe *) malloc(sizeof(struct wframe));
memset(frame, 0, sizeof(struct wframe));
int pos = 0;
clock_t ts = clock();
frame->ts = ts;
frame->diffts = ts - lastts;
lastts = ts;
uint8_t radiotap_version = buffer[pos++];
uint8_t radiotap_pad = buffer[pos++];
uint16_t radiotap_length = buffer[pos];
if (radiotap_version != 0 || radiotap_pad != 0 || radiotap_length > 1000) {
frame->nowifi = true;
return frame;
}
//Skip Radiotap
pos += radiotap_length - 4 + 2;
//Decode packet type
uint16_t fc = buffer[pos]; //frame control info
pos += 2;
if((fc & 0x04) != 0) frame->type += 0x2;
if((fc & 0x08) != 0) frame->type += 0x1;
if((fc & 0x10) != 0) frame->stype += 0x1;
if((fc & 0x20) != 0) frame->stype += 0x2;
if((fc & 0x40) != 0) frame->stype += 0x4;
if((fc & 0x80) != 0) frame->stype += 0x8;
frame->retry = (fc & 0x400);
frame->powermgmt = (fc & 0x800);
frame->nav = buffer[pos];
pos += 2;
memcpy(&frame->addr1, buffer+pos, 6); //This address is always there
pos += 6;
if (frame->type == IEEE80211_FTYPE_CTL)
if (frame->type == IEEE80211_STYPE_CTS ||
frame->type == IEEE80211_STYPE_ACK)
goto FCS;
memcpy(&frame->addr2, buffer+pos, 6);
pos += 6;
if (frame->type == IEEE80211_FTYPE_CTL)
goto FCS;
memcpy(&frame->addr3, buffer+pos, 6);
pos += 6;
FCS:
pos += 2; //TODO: Parse sequence number (fcs)
if(opt_nobeacon && frame->type == IEEE80211_FTYPE_MGMT && frame->stype == IEEE80211_STYPE_BEACON)
return NULL;
if(opt_nomgmt && frame->type == IEEE80211_FTYPE_MGMT)
return NULL;
if (frame->type == IEEE80211_FTYPE_CTL)
{
switch (frame->stype)
{
case IEEE80211_STYPE_RTS:
frame->rxaddr = frame->addr1;
frame->txaddr = frame->addr2;
break;
case IEEE80211_STYPE_CTS:
frame->rxaddr = frame->addr1;
break;
case IEEE80211_STYPE_ACK:
frame->rxaddr = frame->addr1;
break;
}
}
else
{
frame->rxaddr = frame->addr1;
frame->txaddr = frame->addr2;
}
if(frame->txaddr != NULL)
{
struct station * sta = sta_find(sta_head, frame->txaddr);
if (sta != NULL)
frame->txsta = sta;
else
{
sta_add(&sta_head, ccolor++, frame->txaddr);
frame->txsta = sta_find(sta_head, frame->txaddr);
}
frame->txsta->txcount++;
}
if(frame->rxaddr != NULL)
{
struct station * sta = sta_find(sta_head, frame->rxaddr);
if (sta != NULL)
frame->rxsta = sta;
else
{
sta_add(&sta_head, ccolor++, frame->rxaddr);
frame->rxsta = sta_find(sta_head, frame->rxaddr);
}
frame->rxsta->rxcount++;
}
if(frame->type == IEEE80211_FTYPE_MGMT)
{
if(frame->stype == IEEE80211_STYPE_BEACON)
{
if(frame->txsta->ssid == NULL)
{
int bpos = pos+12;
if(buffer[bpos] == 0)
{
int len = buffer[bpos+1];
frame->txsta->ssid = (char*) malloc(len+1);
memcpy(frame->txsta->ssid, buffer + bpos + 2, len);
frame->txsta->ssid[len] = '\0';
}
}
}
}
return frame;
}
void print_nowifi(struct wframe *frame)
{
printf("Non-wifi packet");
if(opt_timestamp)
printf(" (t %d)", frame->ts);
if(opt_diffstamp)
printf(" (d %d)", frame->diffts);
printf("\n");
}
void print_colormark(int color)
{
if (opt_color)
{
switch (color % 6)
{
case 0: printf(CRED); break;
case 1: printf(CGREEN); break;
case 2: printf(CYELLOW); break;
case 3: printf(CBLUE); break;
case 4: printf(CMAGENTA); break;
case 5: printf(CCYAN); break;
}
}
}
void reset_colormark()
{
if (opt_color)
printf(CWHITE);
}
void print_node(struct station *sta)
{
if (sta->color != 0)
print_colormark(sta->color);
if (opt_simpleaddr)
{
if (sta->color == 0)
printf("Broadcast");
else if (sta->ssid != NULL)
printf("%8.8s", sta->ssid);
else
printf("Node %c ", sta->color+64);
}
else
{
if (sta->color == 0)
printf("Broadcast");
else
print_addr(sta->addr);
}
reset_colormark();
}
void print_wifi(struct wframe *frame)
{
if(!isinmaclist(frame->txaddr) && !isinmaclist(frame->rxaddr))
return;
if(frame->txsta != NULL)
{
print_node(frame->txsta);
printf(" sent ");
}
else printf("someone sent ");
printf("%-12s ", subtype_name(frame->type, frame->stype));
if(frame->rxsta != NULL)
{
printf("-> ");
print_node(frame->rxsta);
}
else printf("-> someone ");
reset_colormark();
if (opt_timestamp)
printf(" (t %d)", frame->ts);
if (opt_diffstamp)
printf(" (d %d)", frame->diffts);
printf("\n");
fflush(stdout);
}
void analyze(char* buffer, int size)
{
struct wframe *frame = buffertowframe(buffer, size);
if(frame == NULL)
return;
if(frame->nowifi)
print_nowifi(frame);
else
print_wifi(frame);
free(frame);
}
void print_stalist(struct station * head)
{
if(head == NULL) return;
if(isinmaclist(head->addr))
{
bool old = opt_simpleaddr;
opt_simpleaddr = true;
print_node(head);
printf(" = ");
opt_simpleaddr = false;
print_node(head);
if(head->ssid)
printf(" [%s]", head->ssid);
printf(" (tx: %d, rx: %d)", head->txcount, head->rxcount);
printf("\n");
opt_simpleaddr = old;
}
print_stalist(head->next);
}
int main(int argc, char *argv[])
{
char buffer[BUFSIZE];
struct sockaddr saddr;
int opt;
struct timespec starttime, endtime;
clock_gettime(CLOCK_MONOTONIC, &starttime);
unsigned char bcast[] = "\xFF\xFF\xFF\xFF\xFF\xFF";
sta_add(&sta_head, ccolor++, bcast);
signal(SIGINT, intHandler);
while ((opt = getopt(argc, argv, PARAMS)) != -1)
{
switch (opt)
{
case 'h':
fprintf(stdout, HELP, argv[0]);
exit(EXIT_SUCCESS);
case 'b':
opt_nobeacon = true;
break;
case 'c':
opt_color = true;
break;
case 'd':
opt_diffstamp = true;
break;
case 'm':
opt_nomgmt = true;
break;
case 's':
opt_simpleaddr = true;
break;
case 't':
opt_timestamp = true;
break;
default:
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
}
if (optind >= argc)
{
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
char * iface = argv[optind];
int macs = argc - optind - 1;
if(macs > 0)
{
maclist = (unsigned char*)malloc(6*macs);
unsigned char * pos = maclist;
for(int i = optind + 1; i < argc; i++)
{
unsigned int iMac[6];
unsigned char mac[6];
sscanf(argv[i], "%x:%x:%x:%x:%x:%x", &iMac[0], &iMac[1], &iMac[2], &iMac[3], &iMac[4], &iMac[5]);
for(int j=0;j<6;j++)
mac[j] = (unsigned char)iMac[j];
memcpy(pos, mac, 6);
pos += 6;
}
}
maclist_count = macs;
if (sock_open()) return 0;
if (sock_bind(argv[optind])) return 0;
while(keepRunning) {
socklen_t saddr_size = sizeof saddr;
int size = recvfrom(sock, buffer, BUFSIZE, 0, &saddr, &saddr_size);
analyze(buffer, size);
}
clock_gettime(CLOCK_MONOTONIC, &endtime);
printf(CNORMAL);
sock_close();
printf("Station List: \n");
print_stalist(sta_head->next);
struct timespec ts = tsdiff(starttime, endtime);
printf("Total Running Time: %ld.%lds\n", ts.tv_sec, ts.tv_nsec / 1000);
return 0;
}