forked from rskanth/WifiScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_scan.m
406 lines (328 loc) · 9.63 KB
/
wifi_scan.m
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
//
// wifi_scan.c
// WifiScanner
//
// Created by Sreekanth Rupavatharam on 11/1/13.
// Copyright (c) 2013 Sreekanth Rupavatharam. All rights reserved.
//
#import "AppDelegate.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "pcap/pcap.h"
#define __packed __attribute__((__packed__))
#include "ieee80211.h"
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
#include "ieee80211_radiotap.h"
#include "mac_vendor.h"
static int set_monitor_mode(pcap_t *p);
static void process_packet(u_char *arg, const struct pcap_pkthdr* pkthdr, const u_char * packet);
void setup_timer(int interval);
void house_keep_timer(int val);
int interval = 10;
void flush_mac_cache();
void
house_keep_timer(int val)
{
struct itimerval timeout;
int static count;
timeout.it_interval.tv_sec = 0;
timeout.it_interval.tv_usec = 0;
timeout.it_value.tv_sec = interval;
timeout.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &timeout, 0);
signal(SIGALRM, house_keep_timer);
printf("timer: %d\n", count++);
flush_mac_cache();
}
void
setup_timer(int interval)
{
struct itimerval timeout;
timeout.it_interval.tv_sec = 0;
timeout.it_interval.tv_usec = 0;
timeout.it_value.tv_sec = interval;
timeout.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &timeout, 0);
signal(SIGALRM, house_keep_timer);
}
pcap_t *p;
int setup_scan(char *interface, char *err)
{
int ret = 0;
char errbuf[PCAP_ERRBUF_SIZE];
printf("Start scanning on interface %s\n", interface);
if(p == NULL)
p = pcap_create(interface, errbuf);
ret = set_monitor_mode(p);
if(ret) {
//printf("Set monitor mode failed %d\n", ret);
sprintf(err, "%s", pcap_geterr(p));
pcap_close(p);
p = NULL;
//pcap_perror(p, "Error string");
return ret;
}
return ret;
}
int
start_scan(char *interface, void* self)
{
if(p == NULL)
return -1;
// ret = setup_scan(interface);
// if(ret) return ret;
setup_timer(interval);
if(pcap_loop(p, -1, process_packet, self) == -1) {
fprintf(stderr, "ERROR: %s\n", pcap_geterr(p));
return -1;
}
return 0;
}
#define le32toh(x) (x)
int get_rth_param(struct ieee80211_radiotap_header *rth, int param, void *out)
{
int i, present;
unsigned char *body;
if(param < IEEE80211_RADIOTAP_TSFT ||
param > IEEE80211_RADIOTAP_DATA_RETRIES)
return EINVAL;
if(out == NULL) return EINVAL;
body = (unsigned char *)(rth+1);
present = le32toh(rth->it_present);
if(!(present & (1 << param)))
return ENOENT;
for(i = 0; i < param;i++) {
if(!(present & (1 << i)))
continue;
body += ieee80211_radiotap_sizes[i];
}
bcopy(body, out, ieee80211_radiotap_sizes[i]);
return 0;
}
char *get_vendor(unsigned char * mac)
{
char str[9];
sprintf(str, "%02x-%02x-%02x", mac[0], mac[1], mac[2]);
int i;
int max = sizeof(mac_db) / sizeof(struct mac_info);
/* need to put the table in a hash or tree */
for(i = max-1;i > 0;i--) {
if(!strcmp(mac_db[i].mac_prefix, str))
break;
}
if(i > 0) {
// printf("Index: %d %s\n", i, mac_db[i].vendor);
return mac_db[i].vendor;
}
return "Unknown";
}
int get_signal_strength(struct ieee80211_radiotap_header *rth)
{
char sig = 0;
char noise = 0;
int ret;
ret = get_rth_param(rth, IEEE80211_RADIOTAP_DBM_ANTSIGNAL,
(void *)&sig);
if(ret) {
// printf("get signal strength failed: %s\n", strerror(ret));
}
ret = get_rth_param(rth, IEEE80211_RADIOTAP_DBM_ANTNOISE,
(void *)&noise);
if(ret) {
// printf("get signal strength failed: %s\n", strerror(ret));
}
// return (sig-noise);
if(noise != 0)
return sig * 100 /noise;
else
return sig;
}
#define MAX_SSID_LEN 32
char g_ssid[MAX_SSID_LEN+1];
#define IEEE80211_TSTAMP_LEN 8
#define IEEE80211_INTVL_LEN 2
#define IEEE80211_CAP_LEN 2
#define FIXED_HEADER_LEN (IEEE80211_TSTAMP_LEN + IEEE80211_INTVL_LEN + \
IEEE80211_CAP_LEN)
char *get_ssid(struct ieee80211_frame *wh, int total, int offset)
{
char *tags = ((char*)(wh+1)) + offset;
unsigned int count = sizeof(*wh) + offset;
unsigned int len = tags[1];
if(len > total)
printf("len: %d\n", len);
if(count > total) {
printf("total: %d\n", total);
return NULL;
}
/*
while (tags[0] != 0 && count < total ) {
len = tags[1];
if(len < 0 && len > total) {
printf("len: %d\n", len);
return NULL;
}
tags += len + 2;
count += len + 2;
}
*/
bzero(g_ssid, MAX_SSID_LEN);
if(tags[0] == 0 && (len > 0 && len < MAX_SSID_LEN) ) {
strncpy(g_ssid, (tags+2), len);
g_ssid[len] = '\0';
// printf("SSID: %s (%d)\n", g_ssid, len);
return g_ssid;
}
else {
return NULL;
}
}
#define MAX_CACHE_SIZE 100
unsigned char *mac_cache[MAX_CACHE_SIZE][6];
int cache_index = 0;
void
flush_mac_cache()
{
printf("Flushing the cache\n");
bzero(mac_cache, 100 * 6);/* XXX */
cache_index = 0;
}
int
find_mac_in_cache(unsigned char *mac, int size)
{
int i;
for(i=0;i<cache_index;i++) {
if(!bcmp(mac, mac_cache[i], 6)) {
// printf("found mac %02x:%02x:%02x:%02x:%02x:%02x in cache\n", mac[0], mac[1], mac[2],
// mac[3], mac[4], mac[5]);
return 1;
}
}
return 0;
}
int
add_mac_in_cache(unsigned char *mac, int size)
{
if(cache_index == MAX_CACHE_SIZE-1) {
printf("cache full\n");
flush_mac_cache();
}
//printf("adding to cache: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2],
// mac[3], mac[4], mac[5]);
bcopy(mac, mac_cache[cache_index], 6);
cache_index++;
return 0;
}
static
int ok_to_display(int subtype, unsigned char *mac, id param)
{
int found;
found = find_mac_in_cache(mac, 6);
if(found) return 0; // Display the mac only once every few seconds
add_mac_in_cache(mac, 6);
return 1;
}
#define print_mac(x) printf("%02x:%02x:%02x:%02x:%02x:%02x\n",(x)[0],(x)[1], \
(x)[2], (x)[3],(x)[4],(x)[5])
static void
process_packet(u_char *arg, const struct pcap_pkthdr* pkthdr,
const u_char * packet)
{
int static count = 0;
int type, subtype;
struct ieee80211_frame *wh;
struct ieee80211_radiotap_header *rth;
id param = (__bridge id)((void*)arg);
char buf[100];
int signal;
rth = (struct ieee80211_radiotap_header*) packet;
wh = (struct ieee80211_frame*)(packet + rth->it_len);
type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
if(type != IEEE80211_FC0_TYPE_MGT) return;
subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
if(subtype != IEEE80211_FC0_SUBTYPE_BEACON && subtype != IEEE80211_FC0_SUBTYPE_PROBE_REQ)
return;
count++;
if(!ok_to_display(subtype, wh->i_addr2, param)) {
return;
}
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
wh->i_addr2[0], wh->i_addr2[1],
wh->i_addr2[2], wh->i_addr2[3],
wh->i_addr2[4], wh->i_addr2[5]);
NSString *srcmac = [NSString stringWithUTF8String: buf];
int offset = (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ)? 0: FIXED_HEADER_LEN;
char *ssid = get_ssid(wh, pkthdr->len, offset);
NSString *ApMac;
if(ssid != NULL) {
ApMac = [[NSString alloc ] initWithUTF8String:ssid];
if(ApMac == nil) {
printf("SSID is NULL\n");
return;
}
}
else {
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
wh->i_addr3[0], wh->i_addr3[1],
wh->i_addr3[2], wh->i_addr3[3],
wh->i_addr3[4], wh->i_addr3[5]);
ApMac = [[NSString alloc ] initWithUTF8String: buf];
}
if ([ srcmac isEqualToString:ApMac ] || [ ApMac isEqualToString:@"ff:ff:ff:ff:ff:ff" ])
ApMac = @"---";
signal = get_signal_strength(rth);
sprintf(buf, "%d", signal);
NSString *sig = [NSString stringWithUTF8String: buf];
char *vendor_string = get_vendor(wh->i_addr2);
NSString *vendor;
if(vendor_string != NULL)
vendor =[[NSString alloc] initWithUTF8String:vendor_string];
else
vendor = @"Unknown";
[param capture_callback:srcmac apmac:ApMac signal:sig vendor: vendor type: subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ ? 1:2];
}
void
stop_scan()
{
struct itimerval timeout;
printf("Stopping the scan\n");
pcap_breakloop(p);
pcap_close(p);
/* Stop the timer */
timeout.it_interval.tv_sec = 0;
timeout.it_interval.tv_usec = 0;
timeout.it_value.tv_sec = 0;
timeout.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &timeout, 0);
p = NULL;
return;
}
static int set_monitor_mode(pcap_t *p)
{
int ret;
/* Set the monitor mode */
/*
ret = pcap_can_set_rfmon(p);
if(ret != 1) {
printf("Monitor mode cannot be set: %s \n", pcap_geterr(p));
return 1;
}
*/
ret = pcap_set_rfmon(p, 1);
if(ret != 0) {
//printf("Unable to set monitor mode: %s\n", pcap_geterr(p));
return errno;
}
pcap_set_snaplen(p, 2048);
pcap_set_promisc(p, 0);
pcap_set_timeout(p, 512);
ret = pcap_activate(p);
if(ret != 0) {
printf("pcap_activate failed: %d\n", ret);
return ret;
}
return 0;
}