-
Notifications
You must be signed in to change notification settings - Fork 6
/
wifiscan.c
262 lines (215 loc) · 7.21 KB
/
wifiscan.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
// wifiscan by Rodizio. Based on wifibroadcast rx by Befinitiv. Licensed under GPL2
// scans for wifi packets to determince channel usage for auto CTS protection
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "lib.h"
#include "wifibroadcast.h"
#include "radiotap.h"
#include <time.h>
#include <sys/resource.h>
// this is where we store a summary of the information from the radiotap header
typedef struct {
int m_nChannel;
int m_nChannelFlags;
int m_nRate;
int m_nAntenna;
int m_nRadiotapFlags;
} __attribute__((packed)) PENUMBRA_RADIOTAP_DATA;
int flagHelp = 0;
wifibroadcast_rx_status_t *rx_status = NULL;
void
usage(void)
{
printf(
"wifiscan by Rodizio. Based on wifibroadcast rx by Befinitiv. Licensed under GPL2\n"
"\n"
"Usage: wifiscan <interfaces>\n\n"
"Example:\n"
" wifiscan wlan0 (check for wifi traffic on interface wlan0)\n"
"\n");
exit(1);
}
typedef struct {
pcap_t *ppcap;
int selectable_fd;
int n80211HeaderLength;
} monitor_interface_t;
void open_and_configure_interface(const char *name, monitor_interface_t *interface) {
struct bpf_program bpfprogram;
char szProgram[512];
char szErrbuf[PCAP_ERRBUF_SIZE];
// open the interface in pcap
szErrbuf[0] = '\0';
interface->ppcap = pcap_open_live(name, 44, 0, -1, szErrbuf);
if (interface->ppcap == NULL) {
fprintf(stderr, "Unable to open %s: %s\n", name, szErrbuf);
exit(1);
}
if(pcap_setnonblock(interface->ppcap, 1, szErrbuf) < 0) {
fprintf(stderr, "Error setting %s to nonblocking mode: %s\n", name, szErrbuf);
}
int nLinkEncap = pcap_datalink(interface->ppcap);
if (nLinkEncap == DLT_IEEE802_11_RADIO) {
interface->n80211HeaderLength = 0x18; // 24 bytes
// sprintf(szProgram, "ether[0x00:1] == 0x08 || ether[0x00:1] == 0xc4 || ether[0x00:1] == 0xb4 || ether[0x00:1] == 0xd4 || ether[0x00:1] == 0x94 || ether[0x00:1] == 0x84"); // match on 1st byte (data, cts, rts, ack, block ack)
sprintf(szProgram, "ether[0x00:1] == 0x08 || ether[0x00:1] == 0xc4 || ether[0x00:1] == 0xb4 || ether[0x00:1] == 0xd4 || ether[0x00:1] == 0x94 || ether[0x00:1] == 0x84 || ether[0x00:1] == 0x80"); // match on 1st byte (data, cts, rts, ack, block ack, beacon)
} else {
fprintf(stderr, "ERROR: unknown encapsulation on %s! check if monitor mode is supported and enabled\n", name);
exit(1);
}
if (pcap_compile(interface->ppcap, &bpfprogram, szProgram, 1, 0) == -1) {
puts(szProgram);
puts(pcap_geterr(interface->ppcap));
fprintf(stderr,"error in pcap_compile");
exit(1);
} else {
if (pcap_setfilter(interface->ppcap, &bpfprogram) == -1) {
fprintf(stderr, "%s\n", szProgram);
fprintf(stderr, "%s\n", pcap_geterr(interface->ppcap));
} else {
}
pcap_freecode(&bpfprogram);
}
interface->selectable_fd = pcap_get_selectable_fd(interface->ppcap);
}
void process_packet(monitor_interface_t *interface, int adapter_no) {
struct pcap_pkthdr * ppcapPacketHeader = NULL;
struct ieee80211_radiotap_iterator rti;
PENUMBRA_RADIOTAP_DATA prd;
u8 payloadBuffer[100];
u8 *pu8Payload = payloadBuffer;
int bytes;
int n;
int retval;
int u16HeaderLen;
// receive
retval = pcap_next_ex(interface->ppcap, &ppcapPacketHeader, (const u_char**)&pu8Payload);
if (retval < 0) {
if (strcmp("The interface went down",pcap_geterr(interface->ppcap)) == 0) {
fprintf(stderr, "rx: The interface went down\n");
exit(9);
} else {
fprintf(stderr, "rx: %s\n", pcap_geterr(interface->ppcap));
exit(2);
}
}
if (retval != 1) return;
// fetch radiotap header length from radiotap header (seems to be 36 for Atheros and 18 for Ralink)
u16HeaderLen = (pu8Payload[2] + (pu8Payload[3] << 8));
// fprintf(stderr, "u16headerlen: %d\n", u16HeaderLen);
bytes = ppcapPacketHeader->len - (u16HeaderLen + interface->n80211HeaderLength);
// fprintf(stderr, "bytes: %d\n", bytes);
if (bytes < 0) return;
if (ieee80211_radiotap_iterator_init(&rti, (struct ieee80211_radiotap_header *)pu8Payload, ppcapPacketHeader->len) < 0) {
fprintf(stderr,"error radiotap_iterator_init");
exit(1);
}
//fprintf(stderr,"ppcapPacketHeader->len:%d\n",ppcapPacketHeader->len);
while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0) {
switch (rti.this_arg_index) {
/* case IEEE80211_RADIOTAP_CHANNEL:
prd.m_nChannel =
le16_to_cpu(*((u16 *)rti.this_arg));
prd.m_nChannelFlags =
le16_to_cpu(*((u16 *)(rti.this_arg + 2)));
break;
case IEEE80211_RADIOTAP_ANTENNA:
prd.m_nAntenna = (*rti.this_arg) + 1;
break;
*/
case IEEE80211_RADIOTAP_FLAGS:
prd.m_nRadiotapFlags = *rti.this_arg;
break;
// case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
// rx_status->adapter[adapter_no].current_signal_dbm = (int8_t)(*rti.this_arg);
// break;
}
}
return;
}
long long current_timestamp() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
return milliseconds;
}
int main(int argc, char *argv[])
{
monitor_interface_t interfaces[8];
int num_interfaces = 0;
int i;
int packetcounter = 0;
long long start_time = 0;
long long now = 0;
while (1) {
int nOptionIndex;
static const struct option optiona[] = {
{ "help", no_argument, &flagHelp, 1 },
{ 0, 0, 0, 0 }
};
int c = getopt_long(argc, argv, "h:",
optiona, &nOptionIndex);
if (c == -1)
break;
switch (c) {
case 0: // long option
break;
case 'h': // help
usage();
default:
fprintf(stderr, "unknown switch %c\n", c);
usage();
}
}
if (optind >= argc)
usage();
int x = optind;
while(x < argc && num_interfaces < 8) {
open_and_configure_interface(argv[x], interfaces + num_interfaces);
++num_interfaces;
++x;
usleep(10000); // wait a bit between configuring interfaces to reduce Atheros and Pi USB flakiness
}
start_time = current_timestamp();
now = current_timestamp();
for(;;) {
fd_set readset;
struct timeval to;
to.tv_sec = 0;
to.tv_usec = 1e5;
FD_ZERO(&readset);
for(i=0; i<num_interfaces; ++i)
FD_SET(interfaces[i].selectable_fd, &readset);
int n = select(30, &readset, NULL, NULL, &to);
for(i=0; i<num_interfaces; ++i) {
if(n == 0) {
// printf("n == 0\n");
//break;
}
if(FD_ISSET(interfaces[i].selectable_fd, &readset)) {
packetcounter++;
// printf("packetcount: %d\n", packetcounter);
process_packet(interfaces + i, i);
}
}
now = current_timestamp();
if (now - start_time > 2000) { // scan for 2 seconds
packetcounter = packetcounter / 2; // divide by 2 to get packets per second
printf("%d\n", packetcounter);
exit(0);
}
}
return (0);
}