initial commit
[wisuck.git] / wisuck.c
1 //Russ Handorf wrote most of this in a fit of rage. 
2 //Mike, all the love in the world for putting up with me and my bad coding.
3 //Demo code pulled from LORCON man pages and examples (thanks Brad Antoniewicz!)
4 //Run this at your own peril. It's bad code. Honest.
5
6 #include <stdio.h>
7 #include <getopt.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/time.h>
12 #include <time.h>
13 #include <lorcon2/lorcon.h>
14 #include <lorcon2/lorcon_packasm.h>
15 #include <inttypes.h>
16 #include <math.h>
17
18 void help() {
19   printf("There's really only one option right now:\n");
20   printf("\t-i <int> \tInterface\n");
21 }
22
23 void random_ssid(char *ssid) {
24   int i, len;
25   srand(time(NULL) + getpid());  //replace me im shit: fopen /dev/urandom in rb mode then fread(in_mac, 6, 1, fileptr)
26   len=rand() % 31;
27   static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
28
29   for (i = 0; i < len; i++) {
30     ssid[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
31   }
32   ssid[len] = 0;
33 }
34
35 void random_mac(uint8_t mac[6]){
36   int i;
37   srand(time(NULL) + getpid());  //replace me im shit: fopen /dev/urandom in rb mode then fread(in_mac, 6, 1, fileptr) 
38
39   for (i = 0; i < 6; i++) {
40     mac[i] = rand() % 256;
41   }
42 }
43
44 int main(int argc, char *argv[]) {
45   char *interface = NULL;
46   char ssid[32];
47   int c;
48   lorcon_driver_t *drvlist, *driver;
49   lorcon_t *context;
50   lcpa_metapack_t *metapack;
51   lorcon_packet_t *txpack;
52   uint8_t src_mac[6], dst_mac[6];
53
54   struct timeval time2; 
55   uint64_t timestamp;
56   int interval = 10;
57   int capabilities = 0x0421;
58
59   int channel=1;
60
61   long ms; // Milliseconds
62   time_t s;  // Seconds
63   struct timespec spec;
64
65   int randompacket=rand()%3;
66   char *packettype="UNK";
67
68   while ((c = getopt(argc, argv, "i:h")) != EOF) {
69     switch (c) {
70       case 'i': 
71         interface = strdup(optarg);
72         break;
73       case 'h':
74         help();
75         return(0);
76         break;
77     }
78   }
79
80   if ( interface == NULL ) { 
81     printf ("ERROR: Interface not set.\n");
82     help();
83     return(-1);
84   }
85
86   if ( (driver = lorcon_auto_driver(interface)) == NULL) {
87     printf("[!] Could not determine the driver for %s\n",interface);
88     return -1;
89   } else {
90     printf("[+]\t Driver: %s\n",driver->name);
91   }
92
93
94   if ((context = lorcon_create(interface, driver)) == NULL) {
95     printf("[!]\t Failed to create context");
96     return -1; 
97   }
98
99   if (lorcon_open_injmon(context) < 0) {
100     printf("[!]\t Could not create Monitor Mode interface!\n");
101     return -1;
102   } else {
103     printf("[+]\t Monitor Mode VAP: %s\n",lorcon_get_vap(context));
104     lorcon_free_driver_list(driver);
105   }
106
107   int tmpchan=0;
108   int finalchannels[255];
109   int channels24[15]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
110   int channels52[10]={0,36,40,44,48,52,56,60,64};
111   int channels58[17]={0,100,104,108,112,116,120,124,128,132,136,140,149,153,157,161,165};
112   uint8_t rates[] = "\x8c\x12\x98\x24\xb0\x48\x60\x6c";
113   int chancount=0;
114   int tmpcode=0;
115
116
117   //technically, I dont need the loops below anymore, and can just go from 1 to the max since it all goes in the array anyways
118   //he said after recovering from mucinex dm
119   int chanmax=sizeof(channels24) / sizeof(int);
120   for (tmpchan=1; tmpchan<chanmax; tmpchan++) {
121     //printf("Setting channel %d\n", channels24[tmpchan]);
122     tmpcode = lorcon_set_channel(context,channels24[tmpchan]);
123     if (tmpcode == 0) {
124       channels24[0]=1;
125       finalchannels[chancount]=channels24[tmpchan];
126       chancount++;
127     }
128   }
129   chanmax=sizeof(channels52) / sizeof(int);
130   for (tmpchan=1; tmpchan<chanmax; tmpchan++) {
131     tmpcode = lorcon_set_channel(context,channels52[tmpchan]);
132     if (tmpcode == 0) {
133       channels52[0]=1;
134       finalchannels[chancount]=channels52[tmpchan];
135       chancount++;
136     }
137   }
138   chanmax=sizeof(channels58) / sizeof(int);
139   for (tmpchan=1; tmpchan<chanmax; tmpchan++) {
140     tmpcode = lorcon_set_channel(context,channels58[tmpchan]);
141     if (tmpcode == 0) {
142       channels58[0]=1;
143       finalchannels[chancount]=channels58[tmpchan];
144       chancount++;
145     }
146   }
147
148   if (channels24[0]==1) {
149     printf("[+]\t Enabling 2.4\n");
150   } else {
151     printf("[+]\t DISABLING 2.4\n");
152   }
153
154   if (channels52[0]==1) {
155     printf("[+]\t Enabling 5.2\n");
156   } else {
157     printf("[+]\t DISABLING 5.2\n");
158   }
159
160   if (channels58[0]==1) {
161     printf("[+]\t Enabling 5.8\n");
162   } else {
163     printf("[+]\t DISABLING 5.8\n");
164   }
165
166   printf("[+]\t Enabling a total of %d channels\n", chancount);
167   int count=0;
168
169   srand(time(NULL) + getpid());
170
171   FILE * logs;
172   logs = fopen ("eventlogs.txt","w");
173   fprintf (logs,"TIME, TYPE, SRC MAC, DST MAC, CHANNEL, SSID\n");
174
175   while(1) {
176     // Create timestamp
177     clock_gettime(CLOCK_REALTIME, &spec);
178     s  = spec.tv_sec;
179     ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds
180     if (ms > 999) {
181       s++;
182       ms = 0;
183     }
184     gettimeofday(&time2, NULL);
185     timestamp = time2.tv_sec * 1000000 + time2.tv_usec;
186
187     // Initialize the LORCON metapack       
188     metapack = lcpa_init();
189
190     // Create a Beacon frame from 00:DE:AD:BE:EF:00
191     random_mac(src_mac);
192     random_mac(dst_mac);
193     random_ssid(ssid);
194
195     channel = finalchannels[rand() % (chancount)];
196     lorcon_set_channel(context,channel);
197
198     randompacket=rand()%3;
199     switch(randompacket) {
200       case 0:
201         packettype="BEACON";
202         lcpf_beacon(metapack, src_mac, dst_mac, 0x00, 0x00, 0x00, 0x00, timestamp, interval, capabilities);
203         break;
204       case 1:
205         packettype="PROBE REQUEST";
206         lcpf_probereq(metapack, src_mac, 0x00, 0x00, 0x00, 0x00);
207         break;
208       case 2:
209         packettype="PROBE RESPONSE";
210         lcpf_proberesp(metapack, dst_mac, src_mac, src_mac, 0x00, 0x00, 0x00, 0x00, timestamp, interval, capabilities);
211         break;
212     }
213
214     // Append IE Tag 0 for SSID
215     lcpf_add_ie(metapack, 0, strlen(ssid),ssid);
216
217     // Most of the following IE tags are not needed, but added here as examples
218
219     // Append IE Tag 1 for rates
220     lcpf_add_ie(metapack, 1, sizeof(rates)-1, rates);
221
222     // Append IE Tag 3 for Channel 
223     lcpf_add_ie(metapack, 3, 1, &channel);
224
225     // Append IE Tags 42/47 for ERP Info 
226     lcpf_add_ie(metapack, 42, 1, "\x05");
227     lcpf_add_ie(metapack, 47, 1, "\x05");
228         
229     // Convert the LORCON metapack to a LORCON packet for sending
230     txpack = (lorcon_packet_t *) lorcon_packet_from_lcpa(context, metapack);
231
232     if ( lorcon_inject(context,txpack) < 0 )
233       return -1;
234
235     usleep(interval * 1000);
236
237     printf("\033[K\r");
238     fprintf(logs,"%"PRIdMAX".%03ld, %s, %02x:%02x:%02x:%02x:%02x:%02x, %02x:%02x:%02x:%02x:%02x:%02x, %d, %s\n", (intmax_t)s, ms, packettype, src_mac[0], src_mac[1], src_mac[2], src_mac[3], src_mac[4], src_mac[5], dst_mac[0], dst_mac[1], dst_mac[2], dst_mac[3], dst_mac[4], dst_mac[5], channel, ssid);
239     printf("[+] Src Mac: %02x:%02x:%02x:%02x:%02x:%02x  Dst Mac: %02x:%02x:%02x:%02x:%02x:%02x  C: %d  Sent %d frames, Hit CTRL + C to stop...", src_mac[0], src_mac[1], src_mac[2], src_mac[3], src_mac[4], src_mac[5], dst_mac[0], dst_mac[1], dst_mac[2], dst_mac[3], dst_mac[4], dst_mac[5], channel, count);
240     fflush(stdout);
241     count++;
242
243     // Free the metapack
244     lcpa_free(metapack);
245
246   }
247
248   fclose(logs);
249   lorcon_close(context);
250   lorcon_free(context);
251   return(0);
252 }