-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbasic_ap.c
More file actions
411 lines (287 loc) · 9.4 KB
/
basic_ap.c
File metadata and controls
411 lines (287 loc) · 9.4 KB
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
407
408
409
410
/*
THIS IS INCOMPLETE CODE!
basic_ap.c
by brad.antoniewicz@foundstone.com
Simple access point using LORCON - Right
now its just doing 802.11 session establishment
THIS IS INCOMPLETE CODE!
*/
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/time.h> // Needed for timestamp
#include <lorcon2/lorcon.h> // For LORCON
#include <lorcon2/lorcon_packasm.h> // For metapack packet assembly
/*
Various Constants
*/
#define MAC_LEN 6
#define SRC_MAC_OFFSET 9
#define DST_MAC_OFFSET 3
#define BSSID_OFFSET 15
#define SSID_LEN_OFFSET 37
#define SSID_OFFSET 38
#define PSSID_OFFSET 26
#define PSRC_OFFSET 10
#define TYPE_OFFSET 0
/*
This struct holds the AP information
*/
typedef struct {
char *ssid;
uint8_t ssid_len;
uint8_t channel;
uint8_t *src_mac;
uint8_t *dst_mac;
uint8_t *bssid;
int beacon_interval;
int capabilities;
uint8_t *rates;
struct timeval time;
uint64_t timestamp;
int got_probe_req;
int got_assoc_req;
int got_auth_req;
} ap_info;
ap_info ap_info_t;
void usage(char *argv[]) {
printf("\t-s <SSID>\tSSID to flood\n");
printf("\t-i <int> \tInterface\n");
printf("\t-c <channel>\tChannel\n");
//printf("\t-b <bssid> \tBSSID (XX:XX:XX:XX:XX:XX)\n");
printf("\nExample:\n");
printf("\t%s -s brad -i wlan0 -c 1\n\n",argv[0]);
}
void *beacon(void *context) {
lcpa_metapack_t *metapack; // metapack for LORCON packet assembly
lorcon_packet_t *txpack; // The raw packet to be sent
//Set up AP
ap_info_t.beacon_interval = 100;
ap_info_t.capabilities = 0x0421;
ap_info_t.rates = "\x8c\x12\x98\x24\xb0\x48\x60\x6c"; // 6,9,12,18,24,36,48,54
/*
The following is the packet creation and sending code
*/
// Keep sending frames until interrupted
while(1) {
// Create timestamp
gettimeofday(&ap_info_t.time, NULL);
ap_info_t.timestamp = ap_info_t.time.tv_sec * 1000000 + ap_info_t.time.tv_usec;
// Initialize the LORCON metapack
metapack = lcpa_init();
// Create a Beacon frame from 00:DE:AD:BE:EF:00
lcpf_beacon(metapack, ap_info_t.src_mac, ap_info_t.bssid, 0x00, 0x00, 0x00, 0x00, ap_info_t.timestamp, ap_info_t.beacon_interval, ap_info_t.capabilities);
// Append IE Tag 0 for SSID
lcpf_add_ie(metapack, 0, ap_info_t.ssid_len, ap_info_t.ssid);
// Most of the following IE tags are not needed, but added here as examples
// Append IE Tag 1 for rates
lcpf_add_ie(metapack, 1, sizeof(ap_info_t.rates)-1, ap_info_t.rates);
// Append IE Tag 3 for Channel
lcpf_add_ie(metapack, 3, 1, &ap_info_t.channel);
// Append IE Tags 42/47 for ERP Info
lcpf_add_ie(metapack, 42, 1, "\x05");
lcpf_add_ie(metapack, 47, 1, "\x05");
// Convert the LORCON metapack to a LORCON packet for sending
txpack = (lorcon_packet_t *) lorcon_packet_from_lcpa(context, metapack);
// Send and exit if error
if ( lorcon_inject(context,txpack) < 0 )
exit(1);
// Wait interval before next beacon
usleep(ap_info_t.beacon_interval * 1000);
// Free the metapack
lcpa_free(metapack);
}
}
/*
This function inspects packets, looking for a deauthentication frame
*/
int find_deauth(lorcon_packet_t *packet) {
return 0;
}
/*
This function sends a probe response
*/
void send_probe_resp(lorcon_t *context) {
lcpa_metapack_t *metapack; // metapack for LORCON packet assembly
lorcon_packet_t *txpack; // The raw packet to be sent
/*
The following is the packet creation and sending code
*/
// Initialize the LORCON metapack
metapack = lcpa_init();
// Create
lcpf_proberesp(metapack, ap_info_t.dst_mac, ap_info_t.src_mac, ap_info_t.bssid, 0x00, 0x00, 0x00, 0x00, ap_info_t.timestamp, ap_info_t.beacon_interval, ap_info_t.capabilities);
// Append IE Tag 0 for SSID
lcpf_add_ie(metapack, 0, ap_info_t.ssid_len, ap_info_t.ssid);
// Send and exit if error
if ( lorcon_inject(context,txpack) < 0 )
exit(1);
// Free the metapack
lcpa_free(metapack);
}
/*
This function inspects packets, looking for a probe request
*/
int find_probe_req(lorcon_packet_t *packet) {
int ret, i;
char ssid[256];
char mac[MAC_LEN];
// Looking for any probe requests with a valid SSID length
if ((packet->packet_header[0] == 0x40) && (packet->packet_header[PSSID_OFFSET-1] < 255) && (packet->packet_header[PSSID_OFFSET-1] != 0)) {
for(i=0;i<packet->packet_header[PSSID_OFFSET-1];i++)
ssid[i] = packet->packet_header[PSSID_OFFSET + i];
ssid[packet->packet_header[PSSID_OFFSET-1]] = '\0';
ret = strncmp(ssid,ap_info_t.ssid, packet->packet_header[PSSID_OFFSET-1]);
if ( ret == 0 ) {
// TODO: Set MAC in ap_info_t
ap_info_t.got_probe_req = 1;
return 1; // Found probe request for my SSID
}
}
return 0; // Didn't find a probe request for my SSID
}
/*
This function waits for a probe request then responds
with a probe response
*/
void probe_state(lorcon_t *context, lorcon_packet_t *packet, u_char *user) {
if (find_probe_req(packet) && ap_info_t.got_probe_req == 1) {
printf("[+] Got probe request\n");
}
if (find_deauth(packet) && ap_info_t.got_probe_req == 1) {
printf("[!] Got deauthentication frame!\n");
ap_info_t.got_probe_req = 0;
lorcon_breakloop(context);
}
}
/*
This function sends a association response
*/
void send_assoc_resp(lorcon_t *context) {
}
/*
This function waits for a association request then responds
with a association response
*/
void assoc_state(lorcon_t *context, lorcon_packet_t *packet, u_char *user) {
if ( !ap_info_t.got_probe_req ) {
lorcon_loop(context, 0, probe_state, NULL);
}
}
/*
This function sends a authentication response
*/
void send_auth_resp(lorcon_t *context) {
}
/*
This function waits for a authentication request then responds
with a association response
*/
void auth_state(lorcon_t *context, lorcon_packet_t *packet, u_char *user) {
if ( !ap_info_t.got_probe_req ) {
lorcon_loop(context, 0, probe_state, NULL);
}
if ( !ap_info_t.got_assoc_req ) {
lorcon_loop(context, 0, assoc_state, NULL);
}
}
int main(int argc, char *argv[]) {
char *interface = NULL;
int c, ret;
pthread_t beacon_thread;
lorcon_driver_t *drvlist, *driver; // Needed to set up interface/context
lorcon_t *context; // LORCON context
// BSSID and source MAC address
ap_info_t.src_mac = "\x00\xDE\xAD\xBE\xEF\x00";
ap_info_t.bssid = "\x00\xDE\xAD\xBE\xEF\x00";
ap_info_t.ssid = NULL;
ap_info_t.got_probe_req = 0;
ap_info_t.got_assoc_req = 0;
ap_info_t.got_auth_req = 0;
printf ("%s - Simple 802.11 Access Point\n", argv[0]);
printf ("-----------------------------------------------------\n\n");
/*
This handles all of the command line arguments
*/
while ((c = getopt(argc, argv, "i:s:hc:b:")) != EOF) {
switch (c) {
case 'i':
interface = strdup(optarg);
break;
case 's':
if ( strlen(strdup(optarg)) < 255 ) {
ap_info_t.ssid = strdup(optarg);
ap_info_t.ssid_len = strlen(ap_info_t.ssid);
} else {
printf("ERROR: SSID Length too long! Should not exceed 255 characters\n");
return -1;
}
break;
case 'c':
ap_info_t.channel = atoi(optarg);
break;
case 'h':
usage(argv);
break;
default:
usage(argv);
break;
}
}
if ( interface == NULL || ap_info_t.ssid == NULL ) {
printf ("ERROR: Interface, channel, or SSID not set (see -h for more info)\n");
return -1;
}
printf("[+] Using interface %s\n",interface);
/*
The following is all of the standard interface, driver, and context setup
*/
// Automatically determine the driver of the interface
if ( (driver = lorcon_auto_driver(interface)) == NULL) {
printf("[!] Could not determine the driver for %s\n",interface);
return -1;
} else {
printf("[+]\t Driver: %s\n",driver->name);
}
// Create LORCON context
if ((context = lorcon_create(interface, driver)) == NULL) {
printf("[!]\t Failed to create context");
return -1;
}
// Create Monitor Mode Interface
if (lorcon_open_injmon(context) < 0) {
printf("[!]\t Could not create Monitor Mode interface!\n");
return -1;
} else {
printf("[+]\t Monitor Mode VAP: %s\n",lorcon_get_vap(context));
lorcon_free_driver_list(driver);
}
// Set the channel we'll be injecting on
lorcon_set_channel(context, ap_info_t.channel);
printf("[+]\t Using channel: %d\n\n",ap_info_t.channel);
/*
Bulk of program
*/
// Start thread for beacons
printf("[+] Spawning Beacon Thread for SSID: %s ....",ap_info_t.ssid);
ret = pthread_create( &beacon_thread, NULL, beacon, (void*) context );
if ( ret == 0 ) {
printf("STARTED\n");
} else {
printf("FAILED (%d) \n", ret);
exit(1);
}
//sleep(1000);
printf("[+] Waiting for a probe request\n");
lorcon_loop(context, 0, probe_state, NULL);
/*
The following is all of the standard cleanup stuff
*/
// Close the interface
lorcon_close(context);
// Free the LORCON Context
lorcon_free(context);
return 0;
}