10 wlan = pyiw.WirelessInterface(iface)
14 from ieee80211 import *
19 logfile = open( "wifilogger.log" , "a" )
23 read_timeout = 100 # in milliseconds
24 pc = pcapy.open_live(iface, max_bytes, promiscuous, read_timeout)
27 channel_hop = [ 30.0 , False ]
29 def channel_change ( ) :
31 wlan["channel"] = ( wlan["channel"] ) % 12 + 1
32 except pyiw.error, error:
33 print "PYIW error : %s" % error
36 channel_hop[1] = threading.Timer( channel_hop[0] , channel_change )
37 channel_hop[1].start()
40 def parse_radiotap( radiotap , it_present ) :
43 rfmt , padstr = "<" , ""
44 for name,bit,fmt,pad in ratiotap_header_bits :
45 if it_present & ( 0x1 << bit ) :
47 print "ERROR : unknown bit %s (%d) set" % ( name , bit )
51 fields.append( "CHANNEL_BITMAP" )
55 values = struct.unpack(rfmt+padstr,radiotap)
58 for i in range(len(fields)) :
59 radio_hdr[fields[i]] = values[i]
62 for name,value in radiotap_flags :
63 if radio_hdr['FLAGS'] & value == value :
65 if radio_hdr['FLAGS'] != 16 and radio_hdr['FLAGS'] != 18 :
68 print 'WARNING : Unexpected flags : (%s) %s' % ( radio_hdr['FLAGS'] , " , ".join( flags ) )
69 radio_hdr['_flags'] = flags
72 for name,value in channel_flags :
73 if radio_hdr['CHANNEL_BITMAP'] & value == value :
74 channel.append( name )
75 if radio_hdr['CHANNEL_BITMAP'] != 160 and radio_hdr['CHANNEL_BITMAP'] != 192 :
78 print 'WARNING : Unexpected channel flags : (%s) %s' % ( radio_hdr['CHANNEL_BITMAP'] , " , ".join( channel ) )
79 radio_hdr['_channel_bitmap'] = channel
84 class CaptureEnd ( Exception ) : pass
87 def dealWithPacket ( hdr , data ) :
89 if hdr.getlen() != hdr.getcaplen() :
90 print "ERROR : bad sizes in header : %d vs. %d" % ( hdr.getlen() , hdr.getcaplen() )
92 if len(data) != hdr.getlen() :
93 print "ERROR : Data lenght does not match"
96 it_version , it_len , it_present = struct.unpack("<Bxhl",data[:8])
98 print "ERROR : Bad version (%s), it is probably not radiotap header" % it_version
101 print "ERROR : Bad length on radiotap header"
104 print "ERROR : Strange length on radiotap header"
107 radio_hdr = parse_radiotap( data[8:it_len] , it_present )
112 payload = data[it_len:]
114 pcktlen = len(payload)
116 frame_ctl , frame_ctl2 , duration_id = struct.unpack("BBh",payload[:4])
121 for name,value in frame_types :
122 if frame_ctl & 0x0c == value :
126 print "ERROR : unknown frame type %s" % ( frame_ctl & 0x0c , )
129 if frame_type == "MGT" :
130 for name,value in management_subtypes :
131 if frame_ctl & 0xf0 == value :
135 print "ERROR : unknown MGT subtype %s" % ( frame_ctl & 0xf0 , )
138 elif frame_type == "CTL" :
139 for name,value in control_subtypes :
140 if frame_ctl & 0xf0 == value :
144 if frame_ctl & 0xf0 == 144 :
145 frame_subtype = "CF_END_ACK"
146 elif frame_ctl & 0xf0 == 128 :
147 frame_subtype = "UNKNOWN_1"
149 print "ERROR : unknown CTL subtype %s" % ( frame_ctl & 0xf0 , )
152 elif frame_type == "DATA" :
154 for name,value in data_subtypes :
155 if frame_ctl & 0xf0 == value :
156 _subtype.append( name )
157 frame_subtype = "-".join( _subtype )
160 print "Handling of frame type %s not implemented" % frame_type
164 for name,value in directions : # Only for DATA frames ???
165 if frame_ctl2 & 0x03 == value :
169 print "ERROR : unknown direction %s" % ( frame_ctl2 & 0x03 , )
173 mac_str = "BBBBBB" # is leading '<' required
174 mac_fmt = "%02X:%02X:%02X:%02X:%02X:%02X"
178 maclist.append( mac_fmt % struct.unpack( mac_str , payload[pointer:pointer+6] ) )
185 if frame_type != "CTL" :
186 sequence = struct.unpack("BB",payload[pointer:pointer+2])
190 sequence = ( -1 , -1 )
193 if frame_type == "DATA" and pcktlen > 6 :
194 maclist.append( mac_fmt % struct.unpack( mac_str , payload[pointer:pointer+6] ) )
199 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) )
200 logfile.write( " = %s %s " % ( radio_hdr['DBM_ANTSIGNAL'] , radio_hdr['DBM_ANTNOISE'] ) )
201 logfile.write( " ; %4d %4d " % sequence )
202 logfile.write( " - %4d :" % duration_id )
203 logfile.write( " %s"*len(maclist) % tuple(maclist) )
204 logfile.write( "\n" )
206 curtime = time.time()
207 if curtime - tstamp > max_time :
209 raise CaptureEnd( "Neighborhoud scan completed" )
213 channel_hop[1] = threading.Timer( channel_hop[0] , channel_change )
214 channel_hop[1].start()
217 packet_limit = -1 # infinite
219 pc.loop( packet_limit , dealWithPacket )
220 except CaptureEnd , ex :
221 print "FINISED : %s" % ex
222 except Exception , ex :
224 print "ERROR : %s" % ex