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 channel_hop = [ 30.0 , False ]
25 def channel_change ( ) :
27 wlan["channel"] = ( wlan["channel"] ) % 12 + 1
28 except pyiw.error, error:
29 print "PYIW error : %s" % error
32 channel_hop[1] = threading.Timer( channel_hop[0] , channel_change )
33 channel_hop[1].start()
36 def parse_radiotap( radiotap , it_present ) :
39 rfmt , padstr = "<" , ""
40 for name,bit,fmt,pad in ratiotap_header_bits :
41 if it_present & ( 0x1 << bit ) :
43 print "ERROR : unknown bit %s (%d) set" % ( name , bit )
47 fields.append( "CHANNEL_BITMAP" )
51 values = struct.unpack(rfmt+padstr,radiotap)
54 for i in range(len(fields)) :
55 radio_hdr[fields[i]] = values[i]
58 for name,value in radiotap_flags :
59 if radio_hdr['FLAGS'] & value == value :
61 if radio_hdr['FLAGS'] != 16 and radio_hdr['FLAGS'] != 18 :
64 print 'WARNING : Unexpected flags : (%s) %s' % ( radio_hdr['FLAGS'] , " , ".join( flags ) )
65 radio_hdr['_flags'] = flags
68 for name,value in channel_flags :
69 if radio_hdr['CHANNEL_BITMAP'] & value == value :
70 channel.append( name )
71 if radio_hdr['CHANNEL_BITMAP'] != 160 and radio_hdr['CHANNEL_BITMAP'] != 192 :
74 print 'WARNING : Unexpected channel flags : (%s) %s' % ( radio_hdr['CHANNEL_BITMAP'] , " , ".join( channel ) )
75 radio_hdr['_channel_bitmap'] = channel
80 class CaptureEnd ( Exception ) : pass
83 def dealWithPacket ( hdr , data ) :
85 if hdr.getlen() != hdr.getcaplen() :
86 print "ERROR : bad sizes in header : %d vs. %d" % ( hdr.getlen() , hdr.getcaplen() )
88 if len(data) != hdr.getlen() :
89 print "ERROR : Data lenght does not match"
92 it_version , it_len , it_present = struct.unpack("<Bxhl",data[:8])
94 print "ERROR : Bad version (%s), it is probably not radiotap header" % it_version
97 print "ERROR : Bad length on radiotap header"
100 print "ERROR : Strange length on radiotap header"
103 radio_hdr = parse_radiotap( data[8:it_len] , it_present )
108 payload = data[it_len:]
110 pcktlen = len(payload)
112 frame_ctl , frame_ctl2 , duration_id = struct.unpack("BBh",payload[:4])
117 for name,value in frame_types :
118 if frame_ctl & 0x0c == value :
122 print "ERROR : unknown frame type %s" % ( frame_ctl & 0x0c , )
125 if frame_type == "MGT" :
126 for name,value in management_subtypes :
127 if frame_ctl & 0xf0 == value :
131 print "ERROR : unknown MGT subtype %s" % ( frame_ctl & 0xf0 , )
134 elif frame_type == "CTL" :
135 for name,value in control_subtypes :
136 if frame_ctl & 0xf0 == value :
140 print "ERROR : unknown CTL subtype %s" % ( frame_ctl & 0xf0 , )
143 elif frame_type == "DATA" :
145 for name,value in data_subtypes :
146 if frame_ctl & 0xf0 == value :
147 _subtype.append( name )
148 frame_subtype = "-".join( _subtype )
151 print "Handling of frame type %s not implemented" % frame_type
155 for name,value in directions : # Only for DATA frames ???
156 if frame_ctl2 & 0x03 == value :
160 print "ERROR : unknown direction %s" % ( frame_ctl2 & 0x03 , )
164 mac_str = "BBBBBB" # is leading '<' required
165 mac_fmt = "%02X:%02X:%02X:%02X:%02X:%02X"
169 maclist.append( mac_fmt % struct.unpack( mac_str , payload[pointer:pointer+6] ) )
176 if frame_type != "CTL" :
177 sequence = struct.unpack("BB",payload[pointer:pointer+2])
181 sequence = ( -1 , -1 )
184 if frame_type == "DATA" :
185 maclist.append( mac_fmt % struct.unpack( mac_str , payload[pointer:pointer+6] ) )
190 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) )
191 logfile.write( " = %s %s " % ( radio_hdr['DBM_ANTSIGNAL'] , radio_hdr['DBM_ANTNOISE'] ) )
192 logfile.write( " ; %4d %4d " % sequence )
193 logfile.write( " - %4d :" % duration_id )
194 logfile.write( " %s"*len(maclist) % tuple(maclist) )
195 logfile.write( "\n" )
197 curtime = time.time()
198 if curtime - tstamp > max_time :
200 raise CaptureEnd( "Neighborhoud scan completed" )
204 channel_hop[1] = threading.Timer( channel_hop[0] , channel_change )
205 channel_hop[1].start()
208 packet_limit = -1 # infinite
210 pc.loop( packet_limit , dealWithPacket )
211 except CaptureEnd , ex :
212 print "FINISED : %s" % ex
213 except Exception , ex :
214 print "ERROR : %s" % ex