diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 446 |
1 files changed, 446 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..b74907d --- /dev/null +++ b/src/main.c | |||
@@ -0,0 +1,446 @@ | |||
1 | #include <errno.h> | ||
2 | #include <fcntl.h> | ||
3 | #include <libwifi.h> | ||
4 | #include <linux/if_packet.h> | ||
5 | #include <net/if.h> | ||
6 | #include <netinet/ether.h> | ||
7 | #include <netinet/ip.h> | ||
8 | #include <pcap/pcap.h> | ||
9 | #include <poll.h> | ||
10 | #include <pthread.h> | ||
11 | #include <stdio.h> | ||
12 | #include <sys/ioctl.h> | ||
13 | #include <unistd.h> | ||
14 | |||
15 | #include "addr_list.h" | ||
16 | #include "beacon_data.h" | ||
17 | |||
18 | static unsigned char kBroadcastAddress[] = "\xFF\xFF\xFF\xFF\xFF\xFF"; | ||
19 | |||
20 | static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
21 | |||
22 | static pthread_mutex_t seq_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
23 | static int seq_number = 0; | ||
24 | |||
25 | int get_interface_mac_address(int sock, const char *interface, unsigned char *output) | ||
26 | { | ||
27 | struct ifreq ifr = {0}; | ||
28 | strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1); | ||
29 | |||
30 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) | ||
31 | { | ||
32 | return -1; | ||
33 | } | ||
34 | |||
35 | memcpy(output, ifr.ifr_hwaddr.sa_data, 6); | ||
36 | return 0; | ||
37 | } | ||
38 | |||
39 | int get_interface_index(int sock, const char *interface, int *index) | ||
40 | { | ||
41 | struct ifreq ifr = {0}; | ||
42 | strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1); | ||
43 | |||
44 | if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) | ||
45 | { | ||
46 | return -1; | ||
47 | } | ||
48 | |||
49 | *index = ifr.ifr_ifindex; | ||
50 | return 0; | ||
51 | } | ||
52 | |||
53 | int get_next_seq_number() { | ||
54 | pthread_mutex_lock(&seq_mutex); | ||
55 | int next = seq_number++; | ||
56 | pthread_mutex_unlock(&seq_mutex); | ||
57 | |||
58 | return next; | ||
59 | } | ||
60 | |||
61 | int make_beacon_frame(int index, const unsigned char tx_addr[6], int seq_number, unsigned char **buf) | ||
62 | { | ||
63 | struct libwifi_beacon beacon = {0}; | ||
64 | |||
65 | if (libwifi_create_beacon(&beacon, kBroadcastAddress, tx_addr, tx_addr, NULL, 13)) | ||
66 | { | ||
67 | printf("Could not create beacon frame.\n"); | ||
68 | return -3; | ||
69 | } | ||
70 | |||
71 | beacon.frame_header.seq_control.sequence_number = seq_number; | ||
72 | beacon.fixed_parameters.capabilities_information = (1 << CAPABILITIES_ESS) | (1 << CAPABILITIES_SHORT_PREAMBLE) | (1 << CAPABILITIES_SHORT_SLOT); | ||
73 | beacon.fixed_parameters.beacon_interval = 200; | ||
74 | |||
75 | static const unsigned char supported_rates[] = {0x82, 0x84, 0x0b, 0x16, 0x24, 0x30, 0x48, 0x6c}; | ||
76 | if (libwifi_quick_add_tag(&beacon.tags, TAG_SUPP_RATES, supported_rates, 8)) | ||
77 | { | ||
78 | printf("Could not add supported rates tag.\n"); | ||
79 | return -7; | ||
80 | } | ||
81 | |||
82 | static const unsigned char tim_bitmap[] = {0x01, 0x02, 0x00, 0x00}; | ||
83 | if (libwifi_quick_add_tag(&beacon.tags, TAG_TIM, tim_bitmap, 4)) { | ||
84 | printf("Could not add TIM tag.\n"); | ||
85 | return -7; | ||
86 | } | ||
87 | |||
88 | static const unsigned char nothing = 0; | ||
89 | libwifi_quick_add_tag(&beacon.tags, TAG_ERP, ¬hing, 1); | ||
90 | libwifi_quick_add_tag(&beacon.tags, 47, ¬hing, 1); | ||
91 | |||
92 | static const unsigned char extended_supported_rates[] = {0x0c, 0x12, 0x18, 0x60}; | ||
93 | if (libwifi_quick_add_tag(&beacon.tags, TAG_EXTENDED_SUPPORTED_RATES, extended_supported_rates, 4)) | ||
94 | { | ||
95 | printf("Could not add extended supported rates tag.\n"); | ||
96 | return -7; | ||
97 | } | ||
98 | |||
99 | unsigned char payload_data[BEACON_PAYLOAD_LENGTH + 8]; | ||
100 | payload_data[0] = 0x00; | ||
101 | payload_data[1] = 0x09; | ||
102 | payload_data[2] = 0xBF; | ||
103 | payload_data[3] = 0x00; | ||
104 | payload_data[4] = 0xFF; | ||
105 | payload_data[5] = 0xFF; | ||
106 | payload_data[6] = 0x00; | ||
107 | payload_data[7] = 0x00; | ||
108 | memcpy(payload_data + 8, kBeaconPayloads[index], BEACON_PAYLOAD_LENGTH); | ||
109 | |||
110 | if (libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, payload_data, BEACON_PAYLOAD_LENGTH + 8)) | ||
111 | { | ||
112 | printf("Could not add beacon data tag.\n"); | ||
113 | return -6; | ||
114 | } | ||
115 | |||
116 | size_t beacon_size = libwifi_get_beacon_length(&beacon); | ||
117 | unsigned char *beacon_output = (unsigned char *)malloc(beacon_size); | ||
118 | if (beacon_output == NULL) | ||
119 | { | ||
120 | printf("Could not allocate beacon output.\n"); | ||
121 | return -4; | ||
122 | } | ||
123 | |||
124 | if (libwifi_dump_beacon(&beacon, beacon_output, beacon_size) < 0) | ||
125 | { | ||
126 | printf("Could not dump beacon.\n"); | ||
127 | return -5; | ||
128 | } | ||
129 | |||
130 | libwifi_free_beacon(&beacon); | ||
131 | |||
132 | *buf = beacon_output; | ||
133 | |||
134 | return beacon_size; | ||
135 | } | ||
136 | |||
137 | int prepend_radiotap(const unsigned char *input, int input_size, unsigned char **output) | ||
138 | { | ||
139 | static const unsigned char radiotap[] = "\x00\x00\x08\x00\x00\x00\x00\x00"; | ||
140 | static const int radiotap_size = 8; | ||
141 | |||
142 | int output_size = input_size + radiotap_size; | ||
143 | |||
144 | unsigned char *buf = (unsigned char *)malloc(output_size); | ||
145 | if (buf == NULL) | ||
146 | { | ||
147 | return -1; | ||
148 | } | ||
149 | |||
150 | memcpy(buf, radiotap, radiotap_size); | ||
151 | memcpy(buf + radiotap_size, input, input_size); | ||
152 | |||
153 | *output = buf; | ||
154 | |||
155 | return output_size; | ||
156 | } | ||
157 | |||
158 | int send_packet(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size) | ||
159 | { | ||
160 | struct sockaddr_ll socket_address; | ||
161 | socket_address.sll_ifindex = device_index; | ||
162 | socket_address.sll_halen = ETH_ALEN; | ||
163 | memcpy(socket_address.sll_addr, dst_addr, 6); | ||
164 | |||
165 | pthread_mutex_lock(&socket_mutex); | ||
166 | |||
167 | int ret = 0; | ||
168 | if (sendto(sock, packet, packet_size, 0, (struct sockaddr *)&socket_address, sizeof(struct sockaddr_ll)) < 0) | ||
169 | { | ||
170 | printf("Could not send packet: %d\n", errno); | ||
171 | ret = errno; | ||
172 | } | ||
173 | |||
174 | pthread_mutex_unlock(&socket_mutex); | ||
175 | |||
176 | return ret; | ||
177 | } | ||
178 | |||
179 | int send_packet_with_radiotap(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size) | ||
180 | { | ||
181 | struct libwifi_radiotap_info radiotap_info = {0}; | ||
182 | radiotap_info.present = (1 << IEEE80211_RADIOTAP_FLAGS) | (1 << IEEE80211_RADIOTAP_RATE); | ||
183 | radiotap_info.flags = IEEE80211_RADIOTAP_F_FCS | IEEE80211_RADIOTAP_F_SHORTPRE; | ||
184 | radiotap_info.rate_raw = 4; | ||
185 | |||
186 | unsigned char radiotap_buffer[256]; | ||
187 | int radiotap_size = libwifi_create_radiotap(&radiotap_info, radiotap_buffer); | ||
188 | |||
189 | uint32_t fcs = libwifi_calculate_fcs(packet, packet_size); | ||
190 | |||
191 | int buffer_size = radiotap_size + packet_size + sizeof(uint32_t); | ||
192 | unsigned char *buffer = malloc(buffer_size); | ||
193 | if (buffer == NULL) | ||
194 | { | ||
195 | printf("Could not prepend radiotap.\n"); | ||
196 | return -2; | ||
197 | } | ||
198 | |||
199 | memcpy(buffer, radiotap_buffer, radiotap_size); | ||
200 | memcpy(buffer + radiotap_size, packet, packet_size); | ||
201 | memcpy(buffer + radiotap_size + packet_size, (unsigned char*)&fcs, sizeof(uint32_t)); | ||
202 | |||
203 | int ret = send_packet(sock, device_index, dst_addr, buffer, buffer_size); | ||
204 | free(buffer); | ||
205 | |||
206 | return ret; | ||
207 | } | ||
208 | |||
209 | int send_ack(int sock, int device_index, const unsigned char dst_addr[6]) | ||
210 | { | ||
211 | struct libwifi_cts ack_frame = {0}; | ||
212 | if (libwifi_create_cts(&ack_frame, dst_addr, 0)) { | ||
213 | printf("Could not create ack packet.\n"); | ||
214 | } | ||
215 | |||
216 | ack_frame.frame_header.frame_control.subtype = SUBTYPE_ACK; | ||
217 | |||
218 | int ret = send_packet_with_radiotap(sock, device_index, dst_addr, (unsigned char*)&ack_frame, sizeof(struct libwifi_cts)); | ||
219 | |||
220 | return ret; | ||
221 | } | ||
222 | |||
223 | // output buffer size must be at least 18 bytes | ||
224 | void format_mac_address(const unsigned char addr[6], char *output) | ||
225 | { | ||
226 | sprintf(output, "%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); | ||
227 | } | ||
228 | |||
229 | struct thread_info | ||
230 | { | ||
231 | pthread_t thread_id; | ||
232 | int sock; | ||
233 | unsigned char tx_addr[6]; | ||
234 | int device_index; | ||
235 | |||
236 | struct addr_list authenticated; | ||
237 | }; | ||
238 | |||
239 | void handle_incoming_packet(u_char *userdata, const struct pcap_pkthdr *h, const u_char *bytes) | ||
240 | { | ||
241 | struct thread_info *tinfo = (struct thread_info *)userdata; | ||
242 | |||
243 | struct libwifi_frame frame = {0}; | ||
244 | if (libwifi_get_wifi_frame(&frame, bytes, h->caplen, true)) | ||
245 | { | ||
246 | printf("Could not parse frame.\n"); | ||
247 | return; | ||
248 | } | ||
249 | |||
250 | if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_AUTH) | ||
251 | { | ||
252 | if (strncmp(frame.header.data.addr1, tinfo->tx_addr, 6)) { | ||
253 | // This is not for me. Misdelivery. | ||
254 | return; | ||
255 | } | ||
256 | |||
257 | unsigned char in_addr[18]; | ||
258 | format_mac_address(frame.header.data.addr2, in_addr); | ||
259 | printf("Authentication request from %s\n", in_addr); | ||
260 | |||
261 | if (addr_list_contains(&tinfo->authenticated, frame.header.data.addr2)) | ||
262 | { | ||
263 | // Already authenticated. | ||
264 | return; | ||
265 | } | ||
266 | |||
267 | struct libwifi_auth_fixed_parameters *afp = (struct libwifi_auth_fixed_parameters *)frame.body; | ||
268 | if (afp->algorithm_number != AUTH_OPEN || afp->status_code != STATUS_SUCCESS || afp->transaction_sequence != 1) | ||
269 | { | ||
270 | // Incorrect auth details. | ||
271 | return; | ||
272 | } | ||
273 | |||
274 | //usleep(frame.header.ctrl.duration / 2 * 100); | ||
275 | |||
276 | send_ack(tinfo->sock, tinfo->device_index, frame.header.data.addr2); | ||
277 | |||
278 | addr_list_add(&tinfo->authenticated, frame.header.data.addr2); | ||
279 | |||
280 | struct libwifi_auth auth_resp = {0}; | ||
281 | if (libwifi_create_auth(&auth_resp, frame.header.data.addr2, frame.header.data.addr1, frame.header.data.addr1, AUTH_OPEN, 2, STATUS_SUCCESS)) | ||
282 | { | ||
283 | printf("Could not create auth response.\n"); | ||
284 | return; | ||
285 | } | ||
286 | |||
287 | auth_resp.frame_header.duration = frame.header.ctrl.duration; | ||
288 | auth_resp.frame_header.seq_control.sequence_number = get_next_seq_number(); | ||
289 | |||
290 | //static const unsigned char broadcom_tag_value[] = {0x00, 0x10, 0x18, 0x02, 0x00, 0xf0, 0x00, 0x00, 0x00}; | ||
291 | //libwifi_quick_add_tag(&auth_resp.tags, TAG_VENDOR_SPECIFIC, broadcom_tag_value, 9); | ||
292 | |||
293 | size_t auth_resp_size = libwifi_get_auth_length(&auth_resp); | ||
294 | unsigned char *auth_resp_output = (unsigned char *)malloc(auth_resp_size); | ||
295 | if (auth_resp_output == NULL) | ||
296 | { | ||
297 | printf("Could not allocate auth response output.\n"); | ||
298 | return; | ||
299 | } | ||
300 | |||
301 | if (libwifi_dump_auth(&auth_resp, auth_resp_output, auth_resp_size) < 0) | ||
302 | { | ||
303 | printf("Could not dump auth response.\n"); | ||
304 | return; | ||
305 | } | ||
306 | |||
307 | libwifi_free_auth(&auth_resp); | ||
308 | |||
309 | //usleep(frame.header.ctrl.duration * 100); | ||
310 | |||
311 | if (send_packet_with_radiotap(tinfo->sock, tinfo->device_index, frame.header.data.addr2, auth_resp_output, auth_resp_size)) | ||
312 | { | ||
313 | printf("Could not send auth response.\n"); | ||
314 | return; | ||
315 | } | ||
316 | |||
317 | usleep(5000); | ||
318 | |||
319 | if (send_packet_with_radiotap(tinfo->sock, tinfo->device_index, frame.header.data.addr2, auth_resp_output, auth_resp_size)) | ||
320 | { | ||
321 | printf("Could not send auth response.\n"); | ||
322 | return; | ||
323 | } | ||
324 | |||
325 | printf("Successfully authenticated %s!\n", in_addr); | ||
326 | } | ||
327 | } | ||
328 | |||
329 | void *polling_thread(void *arg) | ||
330 | { | ||
331 | struct thread_info *tinfo = (struct thread_info *)arg; | ||
332 | |||
333 | /*for (;;) { | ||
334 | struct pollfd pfd; | ||
335 | pfd.fd = tinfo->sock; | ||
336 | pfd.events = POLLIN; | ||
337 | |||
338 | if (poll(&pfd, 1, -1) < 0) { | ||
339 | printf("Error polling.\n"); | ||
340 | } else { | ||
341 | if (pfd.revents & POLLIN) { | ||
342 | unsigned char message[1024]; | ||
343 | int msglen = read(tinfo->sock, message, 1024); | ||
344 | if (msglen < 0) { | ||
345 | printf("Could not read from socket.\n"); | ||
346 | } else { | ||
347 | struct libwifi_frame frame = {0}; | ||
348 | if (libwifi_get_wifi_frame(&frame, message, msglen, true)) { | ||
349 | printf("Could not parse frame.\n"); | ||
350 | } else { | ||
351 | printf("Frame type: %d:%d\n", frame.frame_control.type, frame.frame_control.subtype); | ||
352 | } | ||
353 | } | ||
354 | } | ||
355 | } | ||
356 | }*/ | ||
357 | |||
358 | char errbuf[PCAP_ERRBUF_SIZE]; | ||
359 | pcap_t *sniffer = pcap_create("mon0", errbuf); | ||
360 | if (sniffer == NULL) | ||
361 | { | ||
362 | printf("Could not open sniffer: %s\n", errbuf); | ||
363 | return NULL; | ||
364 | } | ||
365 | |||
366 | pcap_set_promisc(sniffer, 1); | ||
367 | pcap_set_immediate_mode(sniffer, 1); | ||
368 | pcap_activate(sniffer); | ||
369 | pcap_loop(sniffer, -1, handle_incoming_packet, (u_char *)tinfo); | ||
370 | |||
371 | return NULL; | ||
372 | } | ||
373 | |||
374 | int main(int argc, char **argv) | ||
375 | { | ||
376 | static const char *interface_name = "mon0"; | ||
377 | |||
378 | int sock = socket(AF_PACKET, SOCK_RAW, 0); | ||
379 | if (sock == -1) | ||
380 | { | ||
381 | printf("Could not open socket.\n"); | ||
382 | return 1; | ||
383 | } | ||
384 | |||
385 | unsigned char tx_addr[6]; | ||
386 | if (get_interface_mac_address(sock, interface_name, tx_addr)) | ||
387 | { | ||
388 | printf("Could not get hardware address.\n"); | ||
389 | return 2; | ||
390 | } | ||
391 | |||
392 | int device_index = 0; | ||
393 | if (get_interface_index(sock, interface_name, &device_index)) | ||
394 | { | ||
395 | printf("Could not get device index.\n"); | ||
396 | return 2; | ||
397 | } | ||
398 | |||
399 | struct thread_info *tinfo = (struct thread_info *)malloc(sizeof(struct thread_info *)); | ||
400 | tinfo->sock = sock; | ||
401 | memcpy(tinfo->tx_addr, tx_addr, 6); | ||
402 | tinfo->device_index = device_index; | ||
403 | addr_list_init(&tinfo->authenticated); | ||
404 | |||
405 | pthread_attr_t thread_attr; | ||
406 | if (pthread_attr_init(&thread_attr)) | ||
407 | { | ||
408 | printf("Could not initialize thread attr.\n"); | ||
409 | return 6; | ||
410 | } | ||
411 | |||
412 | pthread_create(&tinfo->thread_id, &thread_attr, &polling_thread, tinfo); | ||
413 | pthread_attr_destroy(&thread_attr); | ||
414 | |||
415 | int beacon_index = 0; | ||
416 | for (;;) | ||
417 | { | ||
418 | unsigned char *beacon_output; | ||
419 | int beacon_size = make_beacon_frame(beacon_index, tx_addr, get_next_seq_number(), &beacon_output); | ||
420 | if (beacon_size < 0) | ||
421 | { | ||
422 | return 3; | ||
423 | } | ||
424 | |||
425 | if (send_packet_with_radiotap(sock, device_index, kBroadcastAddress, beacon_output, beacon_size)) | ||
426 | { | ||
427 | return 4; | ||
428 | } | ||
429 | |||
430 | free(beacon_output); | ||
431 | |||
432 | beacon_index = (beacon_index + 1) % 10; | ||
433 | |||
434 | usleep(1024 * 200); | ||
435 | } | ||
436 | |||
437 | if (pthread_join(tinfo->thread_id, NULL)) | ||
438 | { | ||
439 | printf("Could not join thread I guess.\n"); | ||
440 | return 6; | ||
441 | } | ||
442 | |||
443 | free(tinfo); | ||
444 | |||
445 | return 0; | ||
446 | } | ||