summary refs log tree commit diff stats
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp165
1 files changed, 139 insertions, 26 deletions
diff --git a/src/main.cpp b/src/main.cpp index aad5545..d34874c 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -1,6 +1,9 @@
1#include <errno.h>
1#include <fcntl.h> 2#include <fcntl.h>
2#include <libwifi.h> 3#include <libwifi.h>
4#include <linux/if_packet.h>
3#include <net/if.h> 5#include <net/if.h>
6#include <netinet/ether.h>
4#include <netinet/ip.h> 7#include <netinet/ip.h>
5#include <pcap/pcap.h> 8#include <pcap/pcap.h>
6#include <stdio.h> 9#include <stdio.h>
@@ -9,10 +12,12 @@
9 12
10#include "beacon_data.h" 13#include "beacon_data.h"
11 14
15static unsigned char kBroadcastAddress[] = "\xFF\xFF\xFF\xFF\xFF\xFF";
16
12int get_interface_mac_address(int sock, const char *interface, unsigned char *output) 17int get_interface_mac_address(int sock, const char *interface, unsigned char *output)
13{ 18{
14 struct ifreq ifr = {0}; 19 struct ifreq ifr = {0};
15 strcpy(ifr.ifr_name, interface); 20 strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1);
16 21
17 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) 22 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
18 { 23 {
@@ -23,32 +28,43 @@ int get_interface_mac_address(int sock, const char *interface, unsigned char *ou
23 return 0; 28 return 0;
24} 29}
25 30
26int main(int argc, char **argv) 31int get_interface_index(int sock, const char *interface, int *index)
27{ 32{
28 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); 33 struct ifreq ifr = {0};
29 if (sock == -1) 34 strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1);
30 {
31 printf("Could not open socket.\n");
32 return 1;
33 }
34 35
35 unsigned char tx_addr[6]; 36 if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0)
36 if (get_interface_mac_address(sock, "wlo1", tx_addr))
37 { 37 {
38 printf("Could not get hardware address.\n"); 38 return -1;
39 return 2;
40 } 39 }
41 40
42 static unsigned char bcast[] = "\xFF\xFF\xFF\xFF\xFF\xFF"; 41 *index = ifr.ifr_ifindex;
42 return 0;
43}
44
45int make_beacon_frame(int index, const unsigned char tx_addr[6], int seq_number, unsigned char **buf)
46{
43 struct libwifi_beacon beacon = {0}; 47 struct libwifi_beacon beacon = {0};
44 48
45 if (libwifi_create_beacon(&beacon, bcast, tx_addr, tx_addr, "ballze", 7)) 49 if (libwifi_create_beacon(&beacon, kBroadcastAddress, tx_addr, tx_addr, NULL, 7))
46 { 50 {
47 printf("Could not create beacon frame.\n"); 51 printf("Could not create beacon frame.\n");
48 return 3; 52 return -3;
53 }
54
55 beacon.frame_header.seq_control.sequence_number = seq_number;
56
57 static const unsigned char supported_rates[] = {0x82, 0x84, 0x0b, 0x16, 0x24, 0x30, 0x48, 0x6c};
58 if (libwifi_quick_add_tag(&beacon.tags, TAG_SUPP_RATES, supported_rates, 8)) {
59 printf("Could not add supported rates tag.\n");
60 return -7;
49 } 61 }
50 62
51 libwifi_remove_tag(&beacon.tags, TAG_SSID); 63 static const unsigned char extended_supported_rates[] = {0x0c, 0x12, 0x18, 0x60};
64 if (libwifi_quick_add_tag(&beacon.tags, TAG_EXTENDED_SUPPORTED_RATES, extended_supported_rates, 4)) {
65 printf("Could not add extended supported rates tag.\n");
66 return -7;
67 }
52 68
53 unsigned char payload_data[BEACON_PAYLOAD_LENGTH + 8]; 69 unsigned char payload_data[BEACON_PAYLOAD_LENGTH + 8];
54 payload_data[0] = 0x00; 70 payload_data[0] = 0x00;
@@ -59,12 +75,12 @@ int main(int argc, char **argv)
59 payload_data[5] = 0xFF; 75 payload_data[5] = 0xFF;
60 payload_data[6] = 0x00; 76 payload_data[6] = 0x00;
61 payload_data[7] = 0x00; 77 payload_data[7] = 0x00;
62 memcpy(payload_data + 8, kBeaconPayloads[0], BEACON_PAYLOAD_LENGTH); 78 memcpy(payload_data + 8, kBeaconPayloads[index], BEACON_PAYLOAD_LENGTH);
63 79
64 if (libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, payload_data, BEACON_PAYLOAD_LENGTH + 8)) 80 if (libwifi_quick_add_tag(&beacon.tags, TAG_VENDOR_SPECIFIC, payload_data, BEACON_PAYLOAD_LENGTH + 8))
65 { 81 {
66 printf("Could not add beacon data tag.\n"); 82 printf("Could not add beacon data tag.\n");
67 return 6; 83 return -6;
68 } 84 }
69 85
70 size_t beacon_size = libwifi_get_beacon_length(&beacon); 86 size_t beacon_size = libwifi_get_beacon_length(&beacon);
@@ -72,26 +88,123 @@ int main(int argc, char **argv)
72 if (beacon_output == NULL) 88 if (beacon_output == NULL)
73 { 89 {
74 printf("Could not allocate beacon output.\n"); 90 printf("Could not allocate beacon output.\n");
75 return 4; 91 return -4;
76 } 92 }
77 93
78 if (libwifi_dump_beacon(&beacon, beacon_output, beacon_size) < 0) 94 if (libwifi_dump_beacon(&beacon, beacon_output, beacon_size) < 0)
79 { 95 {
80 printf("Could not dump beacon.\n"); 96 printf("Could not dump beacon.\n");
81 return 5; 97 return -5;
82 } 98 }
83 99
84 libwifi_free_beacon(&beacon); 100 libwifi_free_beacon(&beacon);
85 101
86 int fd = open("output.dat", O_WRONLY | O_CREAT, 00600); 102 *buf = beacon_output;
103
104 return beacon_size;
105}
106
107int prepend_radiotap(const unsigned char *input, int input_size, unsigned char **output)
108{
109 static const unsigned char radiotap[] = "\x00\x00\x08\x00\x00\x00\x00\x00";
110 static const int radiotap_size = 8;
111
112 int output_size = input_size + radiotap_size;
113
114 unsigned char *buf = (unsigned char *)malloc(output_size);
115 if (buf == NULL)
116 {
117 return -1;
118 }
119
120 memcpy(buf, radiotap, radiotap_size);
121 memcpy(buf + radiotap_size, input, input_size);
87 122
88 static unsigned char radiotap[] = "\x00\x00\x08\x00\x00\x00\x00\x00"; 123 *output = buf;
89 write(fd, radiotap, 8); 124
125 return output_size;
126}
90 127
91 write(fd, beacon_output, beacon_size); 128int send_packet(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size)
92 close(fd); 129{
130 struct sockaddr_ll socket_address;
131 socket_address.sll_ifindex = device_index;
132 socket_address.sll_halen = ETH_ALEN;
133 memcpy(socket_address.sll_addr, dst_addr, 6);
134
135 if (sendto(sock, packet, packet_size, 0, (struct sockaddr *)&socket_address, sizeof(struct sockaddr_ll)) < 0)
136 {
137 printf("Could not send packet.\n");
138 return errno;
139 }
93 140
94 free(beacon_output); 141 return 0;
142}
143
144int send_packet_with_radiotap(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size)
145{
146 unsigned char *buffer;
147 int buffer_size = prepend_radiotap(packet, packet_size, &buffer);
148 if (buffer_size < 0)
149 {
150 printf("Could not prepend radiotap.\n");
151 return -2;
152 }
153
154 int ret = send_packet(sock, device_index, dst_addr, buffer, buffer_size);
155 free(buffer);
156
157 return ret;
158}
159
160int main(int argc, char **argv)
161{
162 static const char *interface_name = "mon0";
163
164 int sock = socket(AF_PACKET, SOCK_RAW, 0);
165 if (sock == -1)
166 {
167 printf("Could not open socket.\n");
168 return 1;
169 }
170
171 unsigned char tx_addr[6];
172 if (get_interface_mac_address(sock, interface_name, tx_addr))
173 {
174 printf("Could not get hardware address.\n");
175 return 2;
176 }
177
178 int device_index = 0;
179 if (get_interface_index(sock, interface_name, &device_index))
180 {
181 printf("Could not get device index.\n");
182 return 2;
183 }
184
185 int beacon_index = 0;
186 int seq_number = 0;
187 for (;;)
188 {
189 unsigned char *beacon_output;
190 int beacon_size = make_beacon_frame(beacon_index, tx_addr, seq_number, &beacon_output);
191 if (beacon_size < 0)
192 {
193 return 3;
194 }
195
196 if (send_packet_with_radiotap(sock, device_index, kBroadcastAddress, beacon_output, beacon_size))
197 {
198 return 4;
199 }
200
201 free(beacon_output);
202
203 beacon_index = (beacon_index + 1) % 10;
204 seq_number++;
205
206 usleep(1024 * 100);
207 }
95 208
96 return 0; 209 return 0;
97} 210}