First implementation for detected wifi cards database
[wifihood] / wifisniffer / wifilogger.py
index 34c6c2f..4ee5f86 100755 (executable)
@@ -3,11 +3,16 @@
 import pcapy
 import struct
 
+import pyiw
+import threading
+
 iface = 'wlan0'
+wlan = pyiw.WirelessInterface(iface)
 
 import time
 
 from ieee80211 import *
+from wificards import *
 
 max_time = 15 * 60
 tstamp = time.time()
@@ -20,6 +25,17 @@ read_timeout = 100 # in milliseconds
 pc = pcapy.open_live(iface, max_bytes, promiscuous, read_timeout)
 
 
+fd = open( "discovered.list" )
+for line in fd.readlines() :
+    items = line[:-1].split()
+    mac = items.pop(0)
+    discovered[ mac ] = card( mac )
+    discovered[mac].from_string( items )
+    if discovered[mac].tipo not in ( 'AP' , 'STA' , 'CELL' ) :
+        print "Unknwon type '%s' for %s" % ( discovered[mac].tipo , mac )
+fd.close()
+
+
 channel_hop = [ 30.0 , False ]
 
 def channel_change ( ) :
@@ -137,8 +153,13 @@ def dealWithPacket ( hdr , data ) :
                 frame_subtype = name
                 break
         else :
-            print "ERROR : unknown CTL subtype %s" % ( frame_ctl & 0xf0 , )
-            return
+            if frame_ctl & 0xf0 == 144 :
+                frame_subtype = "CF_END_ACK"
+            elif frame_ctl & 0xf0 == 128 :
+                frame_subtype = "UNKNOWN_1"
+            else :
+                print "ERROR : unknown CTL subtype %s" % ( frame_ctl & 0xf0 , )
+                return
 
     elif frame_type == "DATA" :
         _subtype = []
@@ -181,12 +202,40 @@ def dealWithPacket ( hdr , data ) :
         sequence = ( -1 , -1 )
 
 
-    if frame_type == "DATA" :
+    if frame_type == "DATA" and pcktlen > 6 :
         maclist.append( mac_fmt % struct.unpack( mac_str , payload[pointer:pointer+6] ) )
         pointer += 6
         pcktlen -= 6
 
 
+    if frame_type == "MGT" : # addresses : dest orig BSSID
+        if len(maclist) != 3 :
+            print "ERROR : insuficientes macs (%d) en un MGT",len(maclist),pcktlen," %s"*len(maclist) % tuple(maclist)
+        else :
+            if frame_subtype == "BEACON" :
+                if maclist[0] == "FF:FF:FF:FF:FF:FF" :
+                    add_full_card ( maclist[1] , 'AP' , frame_subtype , radio_hdr )
+                    discovered[ maclist[1] ].add_rssi( radio_hdr )
+                    if maclist[1] != maclist[2] :
+                        add_full_card ( maclist[2] , 'CELL' , frame_subtype , radio_hdr )
+                        if maclist[1] not in discovered[ maclist[2] ].sta :
+                            discovered[ maclist[2] ].add_sta( maclist[1] )
+                else :
+                    print "ERROR : non broadcast BEACON : %s %s %s" % tuple(maclist)
+            elif frame_subtype == "PROBE_REQ" : # Pueden ser al broadcast o una "reasociacion ??
+                if maclist[0] == maclist[2] :
+                    add_full_card ( maclist[1] , 'STA' , frame_subtype , radio_hdr )
+                    discovered[ maclist[1] ].add_rssi( radio_hdr )
+                    if maclist[0] != "FF:FF:FF:FF:FF:FF" :
+                        add_full_card ( maclist[0] , 'AP' , frame_subtype , radio_hdr )
+                        if maclist[1] not in discovered[ maclist[0] ].sta :
+                            discovered[ maclist[0] ].add_sta( maclist[1] )
+                else :
+                    print "ERROR : broken PROBE_REQ : %s %s %s" % tuple(maclist)
+            else :
+                print "WARNING : unhandled MGT subtype %s" % frame_subtype
+
+
     logfile.write( "%4s %13s %6s %4d [ %2d %2d ] read %4d missing %4d" % (frame_type,frame_subtype,direction,radio_hdr['CHANNEL'],radio_hdr['FLAGS'],radio_hdr['CHANNEL_BITMAP'],pointer,pcktlen) )
     logfile.write( " = %s %s " % ( radio_hdr['DBM_ANTSIGNAL'] , radio_hdr['DBM_ANTNOISE'] ) )
     logfile.write( " ; %4d %4d " % sequence )
@@ -211,7 +260,13 @@ try :
 except CaptureEnd , ex :
     print "FINISED : %s" % ex
 except Exception , ex :
+    channel_hop[0] = 0
     print "ERROR : %s" % ex
 
 logfile.close()
 
+fd = open( "discovered.list" , "w" )
+for mac in discovered.keys() :
+    fd.write( "%s\n" % discovered[mac] )
+fd.close()
+