forked from 0x90/wifi-arsenal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathairtap.c
276 lines (225 loc) · 7.43 KB
/
airtap.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
/*
* AirTap: Capture IEEE 802.11 packets
*
* Dino Dai Zovi <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pcap.h>
#ifdef __FreeBSD__
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_radiotap.h>
#endif /* __FreeBSD__ */
#ifdef __Linux__
#include <linux/802_11.h>
#endif /* __Linux__ */
#include "airtap.h"
#define TO_MS 10
int init_pcap(char*);
/*
* Datalink decoder table
*/
typedef void (*datalink_decoder)(const u_char*, at_frame_info_t*);
#ifdef __FreeBSD__
static void decode_ieee802_11_radio(const u_char*, at_frame_info_t*);
#endif /* __FreeBSD__ */
static void decode_prism_header(const u_char*, at_frame_info_t*);
static void decode_ieee802_11(const u_char*, at_frame_info_t*);
struct {
int datalink;
datalink_decoder decoder;
} datalink_decoders[] = {
/* Ordered by perference in case interface supports multiple */
#ifdef __FreeBSD__
{DLT_IEEE802_11_RADIO, decode_ieee802_11_radio},
#endif /* __FreeBSD__ */
{DLT_PRISM_HEADER, decode_prism_header},
{DLT_IEEE802_11, decode_ieee802_11},
//{DLT_AIRONET_HEADER, decode_aironet_header},
//{DLT_IEEE802_11_RADIO_AVS, decode_ieee802_11_radio_avs},
{-1, NULL}
};
static int does_file_exist(char* path)
{
struct stat sb;
return (stat(path, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG) ? 1 : 0;
}
static void handle_packet(u_char* u, const struct pcap_pkthdr* pkthdr,
const u_char* pkt)
{
datalink_decoder decoder = (datalink_decoder)u;
(*decoder)(pkt, NULL);
}
struct at_hook {
struct at_hook* next;
unsigned char type;
unsigned char subtype;
unsigned char dir;
at_hook_t hook;
};
/* dir, type, subtype */
struct at_hook* hooks[4][16] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
void airtap_add_hook(unsigned int type, unsigned int subtype,
unsigned int dir, at_hook_t hook)
{
struct at_hook** h;
/*
* Handle wild cards
*/
for (h = &(hooks[type >> 2][subtype >> 4]); *h != NULL; h = &(*h)->next);
if ((*h = malloc(sizeof(struct at_hook))) == NULL) {
perror("airtap_add_hook: malloc");
exit(EXIT_FAILURE);
}
(*h)->next = NULL;
(*h)->type = type;
(*h)->subtype = subtype;
(*h)->dir = dir;
(*h)->hook = hook;
}
static void call_hooks(unsigned int type,
unsigned int subtype,
unsigned int dir,
const u_char* frame,
const at_frame_info_t* frame_info)
{
unsigned int t = type >> 2, s = subtype >> 4;
struct at_hook* h;
assert(t < 3 && s <= 15);
/* Specific type/subtype handler */
for (h = hooks[t][s]; h; h = h->next)
if (h->dir == AT_DIR_ALL || h->dir == dir)
(*h->hook)(frame, frame_info);
/* All subtypes handler */
for (h = hooks[3][t]; h; h = h->next)
if (h->dir == AT_DIR_ALL || h->dir == dir)
(*h->hook)(frame, frame_info);
/* All types handler */
for (h = hooks[3][4]; h; h = h->next)
if (h->dir == AT_DIR_ALL || h->dir == dir)
(*h->hook)(frame, frame_info);
}
static pcap_t* pcap = NULL;
static datalink_decoder decode_datalink = NULL;
int airtap_open(char* file_or_int)
{
char pcap_errbuf[PCAP_ERRBUF_SIZE];
int* datalinks, n_datalinks, i, j;
/*
* Read packets from pcap interface or file
*/
if (does_file_exist(file_or_int)) {
if (!(pcap = pcap_open_offline(file_or_int, pcap_errbuf))) {
fprintf(stderr, "init_pcap: pcap_open_offline: %s\n", pcap_errbuf);
exit(EXIT_FAILURE);
}
}
else {
if (!(pcap = pcap_open_live(file_or_int,
2500, /* snaplen: whole packet */
1, /* promisc: yes */
TO_MS,
pcap_errbuf))) {
fprintf(stderr, "init_pcap: pcap_open_live: %s\n", pcap_errbuf);
exit(EXIT_FAILURE);
}
}
/*
* Select best available datalink
*/
if ((n_datalinks = pcap_list_datalinks(pcap, &datalinks)) < 0) {
pcap_perror(pcap, "init_pcap: pcap_list_datalinks");
exit(EXIT_FAILURE);
}
for (i = 0; !decode_datalink && datalink_decoders[i].datalink > 0; i++) {
for (j = 0; j < n_datalinks; j++) {
if (datalink_decoders[i].datalink == datalinks[j]) {
if (pcap_set_datalink(pcap,
datalink_decoders[i].datalink) < 0) {
pcap_perror(pcap, "init_pcap: pcap_set_datalink");
exit(EXIT_FAILURE);
}
decode_datalink = datalink_decoders[i].decoder;
}
}
}
if (!decode_datalink) {
fprintf(stderr, "init_pcap: no suitable datalink decoder found\n");
exit(EXIT_FAILURE);
}
return 0;
}
int airtap_loop()
{
/*
* Read packets forever
*/
return pcap_loop(pcap, -1, handle_packet, (u_char*)decode_datalink);
}
#ifdef __FreeBSD__
/*********************************************************************
* BSD Radiotap Datalink support *
*********************************************************************/
/* Copied from FreeBSD header in notoriously bad style */
struct wi_rx_radiotap_header {
struct ieee80211_radiotap_header wr_ihdr;
u_int8_t wr_flags;
u_int8_t wr_rate;
u_int16_t wr_chan_freq;
u_int16_t wr_chan_flags;
u_int8_t wr_antsignal;
u_int8_t wr_antnoise;
};
void
decode_ieee802_11_radio(const u_char* pkt, at_frame_info_t* frame_info)
{
/*
* XXX: We should really not hardcode static radiotap header types
*/
struct wi_rx_radiotap_header* rt_hdr =
(struct wi_rx_radiotap_header*)pkt;
struct ieee80211_frame* frame =
(struct ieee80211_frame*)
(pkt + sizeof(struct wi_rx_radiotap_header));
decode_ieee802_11((const u_char*)frame);
}
#endif /* __FreeBSD__ */
void
decode_prism_header(const u_char* pkt, at_frame_info_t* frame_info)
{
struct at_prism_header* p2_hdr =
(struct at_prism_header*)pkt;
struct at_wifi_frame* frame =
(struct at_wifi_frame*)
(pkt + sizeof(struct at_prism_header));
/*
* XXX: Extract signal and noise and place in AirTap header
*/
if (frame_info == NULL) {
frame_info = (at_frame_info_t*)malloc(sizeof(at_frame_info_t));
}
frame_info->channel = p2_hdr->channel.data;
frame_info->signal = p2_hdr->signal.data;
frame_info->noise = p2_hdr->noise.data;
decode_ieee802_11((const u_char*)frame, frame_info);
}
void
decode_ieee802_11(const u_char* pkt, at_frame_info_t* frame_info)
{
struct at_wifi_frame* frame =
(struct at_wifi_frame*)pkt;
u_int8_t
type = frame->frame_control & AT_TYPE_ALL,
subtype = frame->frame_control & AT_SUBTYPE_ALL,
direction = (frame->frame_control & (AT_DIR_ALL << 8)) >> 8;
assert((frame->frame_control & 0x3) == 0);
call_hooks(type, subtype, direction, (const u_char*)frame, frame_info);
}