10 from ieee80211 import *
15 logfile = open( "wifilogger.log" , "a" )
19 read_timeout = 100 # in milliseconds
20 pc = pcapy.open_live(iface, max_bytes, promiscuous, read_timeout)
23 def parse_radiotap( radiotap , it_present ) :
26 rfmt , padstr = "<" , ""
27 for name,bit,fmt,pad in ratiotap_header_bits :
28 if it_present & ( 0x1 << bit ) :
30 print "ERROR : unknown bit %s (%d) set" % ( name , bit )
34 fields.append( "CHANNEL_BITMAP" )
38 values = struct.unpack(rfmt+padstr,radiotap)
41 for i in range(len(fields)) :
42 radio_hdr[fields[i]] = values[i]
45 for name,value in radiotap_flags :
46 if radio_hdr['FLAGS'] & value == value :
48 if radio_hdr['FLAGS'] != 16 and radio_hdr['FLAGS'] != 18 :
51 print 'WARNING : Unexpected flags : (%s) %s' % ( radio_hdr['FLAGS'] , " , ".join( flags ) )
52 radio_hdr['_flags'] = flags
55 for name,value in channel_flags :
56 if radio_hdr['CHANNEL_BITMAP'] & value == value :
57 channel.append( name )
58 if radio_hdr['CHANNEL_BITMAP'] != 160 and radio_hdr['CHANNEL_BITMAP'] != 192 :
61 print 'WARNING : Unexpected channel flags : (%s) %s' % ( radio_hdr['CHANNEL_BITMAP'] , " , ".join( channel ) )
62 radio_hdr['_channel_bitmap'] = channel
67 class CaptureEnd ( Exception ) : pass
70 def dealWithPacket ( hdr , data ) :
72 if hdr.getlen() != hdr.getcaplen() :
73 print "ERROR : bad sizes in header : %d vs. %d" % ( hdr.getlen() , hdr.getcaplen() )
75 if len(data) != hdr.getlen() :
76 print "ERROR : Data lenght does not match"
79 it_version , it_len , it_present = struct.unpack("<Bxhl",data[:8])
81 print "ERROR : Bad version (%s), it is probably not radiotap header" % it_version
84 print "ERROR : Bad length on radiotap header"
87 print "ERROR : Strange length on radiotap header"
90 radio_hdr = parse_radiotap( data[8:it_len] , it_present )
95 payload = data[it_len:]
97 pcktlen = len(payload)
99 frame_ctl , frame_ctl2 , duration_id = struct.unpack("BBh",payload[:4])
104 for name,value in frame_types :
105 if frame_ctl & 0x0c == value :
109 print "ERROR : unknown frame type %s" % ( frame_ctl & 0x0c , )
112 if frame_type == "MGT" :
113 for name,value in management_subtypes :
114 if frame_ctl & 0xf0 == value :
118 print "ERROR : unknown MGT subtype %s" % ( frame_ctl & 0xf0 , )
121 elif frame_type == "CTL" :
122 for name,value in control_subtypes :
123 if frame_ctl & 0xf0 == value :
127 print "ERROR : unknown CTL subtype %s" % ( frame_ctl & 0xf0 , )
130 elif frame_type == "DATA" :
132 for name,value in data_subtypes :
133 if frame_ctl & 0xf0 == value :
134 _subtype.append( name )
135 frame_subtype = "-".join( _subtype )
138 print "Handling of frame type %s not implemented" % frame_type
142 for name,value in directions : # Only for DATA frames ???
143 if frame_ctl2 & 0x03 == value :
147 print "ERROR : unknown direction %s" % ( frame_ctl2 & 0x03 , )
151 mac_str = "BBBBBB" # is leading '<' required
152 mac_fmt = "%02X:%02X:%02X:%02X:%02X:%02X"
156 maclist.append( mac_fmt % struct.unpack( mac_str , payload[pointer:pointer+6] ) )
163 if frame_type != "CTL" :
164 sequence = struct.unpack("BB",payload[pointer:pointer+2])
168 sequence = ( -1 , -1 )
171 if frame_type == "DATA" :
172 maclist.append( mac_fmt % struct.unpack( mac_str , payload[pointer:pointer+6] ) )
177 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) )
178 logfile.write( " = %s %s " % ( radio_hdr['DBM_ANTSIGNAL'] , radio_hdr['DBM_ANTNOISE'] ) )
179 logfile.write( " ; %4d %4d " % sequence )
180 logfile.write( " - %4d :" % duration_id )
181 logfile.write( " %s"*len(maclist) % tuple(maclist) )
182 logfile.write( "\n" )
184 curtime = time.time()
185 if curtime - tstamp > max_time :
186 raise CaptureEnd( "Neighborhoud scan completed" )
189 packet_limit = -1 # infinite
191 pc.loop( packet_limit , dealWithPacket )
192 except CaptureEnd , ex :
193 print "FINISED : %s" % ex
194 except Exception , ex :
195 print "ERROR : %s" % ex