Using LORCON now for channel hopping and interface setup. Client is pretty much all...
[soho-sigint.git] / client-wifi / client-c / soho-sigint-wifi.c
index c577e72..a49e42d 100644 (file)
@@ -1,17 +1,10 @@
 #include <pcap.h>
+#include <pcap.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/socket.h>
-//#include <netinet/in.h>
-//#include <arpa/inet.h>
-//#include <netinet/if_ether.h> 
-//#include <net/ethernet.h>
-//#include <netinet/ether.h> 
-//#include <netinet/ip.h> 
-//#include <linux/if.h>
-//#include <linux/wireless.h>
 #include <ctype.h>
 #include <unistd.h>
 #include "radiotap.h"
 #include <curl/curl.h>
 #include <json-c/json.h>
 #include <stdbool.h>
+#include <lorcon2/lorcon.h>
 
 const struct pcap_pkthdr* callback_header;
 
 time_t start = 0;
-char *ssid_buf[34][2] = { NULL, NULL };
-char *probe_resp_buf[34][3] = { NULL, NULL };
-char *probe_buf[34][2] = { NULL, NULL };
+int element = -1;
+int changechan=1;
+char *ssid_buf[50][2] = { NULL, NULL };
+char *probe_resp_buf[50][3] = { NULL, NULL };
+char *probe_buf[50][2] = { NULL, NULL };
+
+lorcon_t *context;
+int lchannel = 1;
 
 //char post_url[255] = "http://intranet.spangdorfia.com/butler/sohoinput.php";
 char *post_url = NULL;
@@ -55,8 +54,27 @@ typedef struct {
 
 struct json_object *obj1, *obj2, *array, *tmp1, *tmp2;
 
+/*
+int change_chan(context) {
+  if (changechan==1) {
+    int gchannel=0;
+    changechan=0;
+    gchannel=lorcon_get_channel(context);
+    printf("got channel %d", gchannel);
+    lchannel=gchannel+1;
+    if (lchannel == 14) {
+      lchannel=1;
+    }
+    printf("Setting channel %d\n", lchannel);
+    lorcon_set_channel(context, lchannel);
+    changechan=1;
+  }
+}
+*/
+
 void send_data(json_object *array) {
 
+  //printf("curl start\n");
   CURL *curl;
   CURLcode res;
 
@@ -110,7 +128,7 @@ void send_data(json_object *array) {
   }
 
   curl_global_cleanup();
-
+  //printf("curl end\n");
 }
 
 
@@ -124,9 +142,9 @@ void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* pac
   char *ssid;
   char tmp_channel[2];
   char tmp_rssi[1];
-
-
+  
   int diff;
+
   if (start == 0) {
     start = time(0);
   }
@@ -162,7 +180,7 @@ void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* pac
     if (iter.this_arg_index == IEEE80211_RADIOTAP_FLAGS) {
       flags = (*(u_int8_t *)iter.this_arg);
       if (flags & IEEE80211_RADIOTAP_F_BADFCS) {
-        //printf("bad fcs\n");
+        printf("bad fcs\n");
         fcsfail=1;
         break;
       }
@@ -180,9 +198,11 @@ void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* pac
  
   if (pkthdr->len >= 24) {
     u_int8_t hlen;
-    hlen = packet[2]+(packet[3]<<8); //Usually 18 or 13 in some cases
+    //hlen = packet[2]+(packet[3]<<8); //Usually 18 or 13 in some cases
+    hlen=36;
     switch (packet[hlen]) {
       case 0x40: //probe request
+       //printf("probe request\n");
         ssid_len=packet[61];
         memset(ssid, 0, sizeof(ssid));
         if (ssid_len>0) {
@@ -193,13 +213,17 @@ void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* pac
         memset(client_mac, 0, sizeof(client_mac));
         sprintf(client_mac, "%02x:%02x:%02x:%02x:%02x:%02x", packet[46], packet[47],packet[48],packet[49],packet[50],packet[51]);
 
-        for (i=0; i<34; i++) {
+        for (i=0; i<50; i++) {
           if (probe_buf[i][0] != NULL) {
             if ((strcoll(probe_buf[i][0], ssid) == 0 && strcoll(probe_buf[i][1], client_mac) == 0)) {
               break;
             }
           }
           if (probe_buf[i][0] == NULL) {
+              if (element < i) {
+                element=i;
+              }
+             //printf("probe element: %d\n", element);
               probe_buf[i][0] = strdup(ssid);
               probe_buf[i][1] = strdup(client_mac);
               obj2 = json_object_new_object();
@@ -220,21 +244,26 @@ void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* pac
 
         break;
       case 0x50: //probe response
+       //printf("probe response\n");
         ssid_len=packet[73];
         for (i=0;i<ssid_len;++i){
           sprintf(ssid+i, "%c", packet[hlen+38+i]);
         }
-
-        sprintf(client_mac, "%02x:%02x:%02x:%02x:%02x:%02x", packet[39], packet[40],packet[41],packet[42],packet[43],packet[44]);
+        //was 39-44
+        sprintf(client_mac, "%02x:%02x:%02x:%02x:%02x:%02x", packet[40], packet[41],packet[42],packet[43],packet[44],packet[45]);
         sprintf(bssid_mac, "%02x:%02x:%02x:%02x:%02x:%02x", packet[52], packet[53],packet[54],packet[55],packet[56],packet[57]);
 
-        for (i=0; i<34; i++) {
+        for (i=0; i<50; i++) {
           if (probe_resp_buf[i][0] != NULL) {
             if ((strcoll(probe_resp_buf[i][0], ssid) == 0 && strcoll(probe_resp_buf[i][1], client_mac) == 0)) {
               break;
             }
           }
           if (probe_resp_buf[i][0] == NULL) {
+              if (element < i) {
+                element=i;
+              }
+             //printf("probe response element: %d\n", element);
               probe_resp_buf[i][0] = strdup(ssid);
               probe_resp_buf[i][1] = strdup(client_mac);
               probe_resp_buf[i][2] = strdup(bssid_mac);
@@ -257,6 +286,7 @@ void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* pac
         }
         break;
       case 0x80: //beacon
+       //printf("beacon\n");
         ssid_len=packet[73];
         if (ssid_len>0) {
           for (i=0;i<ssid_len;++i){
@@ -272,13 +302,17 @@ void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* pac
         //printf("debug ssid: %s  fieldlen: %d strlen: %d\n", ssid, ssid_len, strlen(ssid));
         sprintf(client_mac, "%02x:%02x:%02x:%02x:%02x:%02x", packet[52], packet[53],packet[54],packet[55],packet[56],packet[57]);
 
-        for (i=0; i<34; i++) {
+        for (i=0; i<50; i++) {
           if (ssid_buf[i][0] != NULL) {
             if ((strcoll(ssid_buf[i][0], ssid) == 0 && strcoll(ssid_buf[i][1], client_mac) == 0)) {
               break;
             }
           }
           if (ssid_buf[i][0] == NULL) {
+              if (element < i) {
+                element=i;
+              }
+             //printf("ssid element: %d\n", element);
               ssid_buf[i][0] = strdup(ssid);
               ssid_buf[i][1] = strdup(client_mac);
               obj2 = json_object_new_object();
@@ -297,30 +331,51 @@ void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* pac
             }
         }
         break;
+      //default:
+      //  printf("Got something different: %02x\n", packet[hlen]);
+      //  printf("hlen: %d\n", hlen);
+      //  break;
     }
   };
 
   diff = (t0 - start);
 
-  if (diff >= 5) {
+  if ((diff >= 5) || (element == 25)) {
     //printf("time to barf!\n");
+    //printf("size: %d\n", element);
+    //change_chan(context);
+    int gchannel=0;
+    gchannel=lorcon_get_channel(context);
+    //printf("got channel %d", gchannel);
+    lchannel=gchannel+1;
+    if (lchannel == 14) {
+      lchannel=1;
+    }
+    //printf("Setting channel %d\n", lchannel);
+    lorcon_set_channel(context, lchannel);
+
+    lorcon_set_channel(context, lchannel);
     //for (i=0; i<34; i++) {
     //  printf("barf beacons: buffer %d, ssid: %s \t\t mac: %s  channel: %s rssi: %s\n", i, ssid_buf[i][0], ssid_buf[i][1], ssid_buf[i][2], ssid_buf[i][3]);
     //}
     //for (i=0; i<34; i++) {
     //  printf("barf probe reponses: buffer %d, ssid: %s \t mac: %s mac: %s  channel: %s rssi: %s\n", i, probe_resp_buf[i][0], probe_resp_buf[i][1], probe_resp_buf[i][2], probe_resp_buf[i][3], probe_resp_buf[i][4]);
     //}
-    for (i=0; i<34; i++) {
+    //for (i=0; i<34; i++) {
       //printf("barf probes: buffer %d, ssid: %s \t mac: %s channel: %s rssi: %s\n", i, probe_buf[i][0], probe_buf[i][1], probe_buf[i][2], probe_buf[i][3]);
-    }
+    //}
 
-    send_data(array);
-    //printf ("The json object created: %s\n",json_object_to_json_string(array));
-    json_object_put(array);
+    if (element >= 0) {
+      //printf("barfing\n");
+      send_data(array);
+      //printf ("The json object created: %s\n",json_object_to_json_string(array));
+      json_object_put(array);
 
-    memset(ssid_buf, 0, sizeof(ssid_buf));
-    memset(probe_resp_buf, 0, sizeof(probe_resp_buf));
-    memset(probe_buf, 0, sizeof(probe_buf));
+      memset(ssid_buf, 0, sizeof(ssid_buf));
+      memset(probe_resp_buf, 0, sizeof(probe_resp_buf));
+      memset(probe_buf, 0, sizeof(probe_buf));
+      element=-1;
+    }
     start = time(0);
   }
   //printf("rate: %d  channel: %04x  rssi: %d\n", rate, channel, rssi);
@@ -330,6 +385,9 @@ int main(int argc,char **argv)
 {
   int c;
   char *dev = NULL; 
+  char *interface = NULL;
+  lorcon_driver_t *drvlist, *driver;
+  //lorcon_t *context;
   char errbuf[PCAP_ERRBUF_SIZE];
   pcap_t* pcap;
   struct bpf_program fp;      /* hold compiled program     */
@@ -345,7 +403,7 @@ int main(int argc,char **argv)
         totpacket = optarg;
         break;
       case 'i':
-        dev = optarg;
+        interface = optarg;
         break;
       case 'p':
         post_url = optarg;
@@ -359,10 +417,10 @@ int main(int argc,char **argv)
 
   if (totpacket == NULL) {
     totpacket="-1";
-    printf("-a not specified... setting to 10.\n");
+    printf("-a not specified... looping forever.\n");
   }
 
-  if (dev == NULL) {
+  if (interface == NULL) {
     printf("You forgot -i\n");
     exit(0);
   }
@@ -372,13 +430,40 @@ int main(int argc,char **argv)
     exit(0);
   }
 
-  printf ("%s %s\n", totpacket, dev);
+  //printf ("%s %s\n", totpacket, dev);
 
-  if(dev == NULL) {
-    printf("%s\n",errbuf); 
-    exit(1); 
+  //if(interface == NULL) {
+  //  printf("%s\n",errbuf); 
+  //  exit(1); 
+  //}
+
+  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_monitor(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, lchannel);
+
+  dev=lorcon_get_vap(context);
+
   /* ask pcap for the network address and mask of the device */
   pcap_lookupnet(dev,&netp,&maskp,errbuf);