10 read_timeout = 100 # in milliseconds
11 pc = pcapy.open_live(iface, max_bytes, promiscuous, read_timeout)
13 # Removed leading IEEE80211_RADIOTAP_
14 ratiotap_header_bits = (
15 ( "TSFT" , 0 , "q" , False ) ,
16 ( "FLAGS" , 1 , "B" , True ) ,
17 ( "RATE" , 2 , "B" , True ) ,
18 ( "CHANNEL" , 3 , "hh" , False ) ,
19 ( "FHSS" , 4 , "h" , False ) ,
20 ( "DBM_ANTSIGNAL" , 5 , "b" , True ) ,
21 ( "DBM_ANTNOISE" , 6 , "b" , True ) ,
22 ( "LOCK_QUALITY" , 7 , "h" , False ) ,
23 ( "TX_ATTENUATION" , 8 , "h" , False ) ,
24 ( "DB_TX_ATTENUATION" , 9 , "h" , False ) ,
25 ( "DBM_TX_POWER" , 10 , "b" , True ) ,
26 ( "ANTENNA" , 11 , "B" , True ) ,
27 ( "DB_ANTSIGNAL" , 12 , "B" , True ) ,
28 ( "DB_ANTNOISE" , 13 , "B" , True ) ,
29 ( "RX_FLAGS" , 14 , "h" , False ) ,
30 ( "TX_FLAGS" , 15 , "h" , False ) ,
31 ( "RTS_RETRIES" , 16 , "B" , True ) ,
32 ( "DATA_RETRIES" , 17 , "B" , True ) ,
33 ( "EXT" , 31 , "" , False )
36 # Removed leading IEEE80211_CHAN_
38 ( "INDOOR" , 0x0004 ) , # This channel can be used indoor
39 ( "OUTDOOR" , 0x0008 ) , # This channel can be used outdoor
40 ( "TURBO" , 0x0010 ) , # Turbo channel
41 ( "CCK" , 0x0020 ) , # CCK channel
42 ( "OFDM" , 0x0040 ) , # OFDM channel
43 ( "2GHZ" , 0x0080 ) , # 2 GHz spectrum channel.
44 ( "5GHZ" , 0x0100 ) , # 5 GHz spectrum channel
45 ( "PASSIVE" , 0x0200 ) , # Only passive scan allowed
46 ( "DYN" , 0x0400 ) , # Dynamic CCK-OFDM channel
47 ( "GFSK" , 0x0800 ) , # GFSK channel (FHSS PHY)
48 ( "RADAR" , 0x1000 ) , # Radar found on channel
49 ( "STURBO" , 0x2000 ) , # 11a static turbo channel only
50 ( "HALF" , 0x4000 ) , # Half rate channel
51 ( "QUARTER" , 0x8000 ) , # Quarter rate channel
53 # IEEE80211_CHAN_A = IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_OFDM
54 # IEEE80211_CHAN_B = IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_CCK
55 # IEEE80211_CHAN_G = IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_DYN
56 # IEEE80211_CHAN_TA = IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_OFDM | IEEE80211_CHAN_TURBO
57 # IEEE80211_CHAN_TG = IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_DYN | IEEE80211_CHAN_TURBO
59 # Removed leading IEEE80211_RADIOTAP_F_
61 ( "CFP" , 0x01 ) , # sent/received during CFP
62 ( "SHORTPRE" , 0x02 ) , # sent/received with short preamble
63 ( "WEP" , 0x04 ) , # sent/received with WEP encryption
64 ( "FRAG" , 0x08 ) , # sent/received with fragmentation
65 ( "FCS" , 0x10 ) , # frame includes FCS
66 ( "DATAPAD" , 0x20 ) # frame has padding between 802.11 header and payload (to 32-bit boundary)
69 # Removed leading IEEE80211_RADIOTAP_F_RX_
71 ( "BADFCS" , 0x0001 ) # frame failed crc check
74 # Removed leading IEEE80211_RADIOTAP_F_TX_
76 ( "FAIL" , 0x0001 ) , # failed due to excessive retries
77 ( "CTS" , 0x0002 ) , # used cts 'protection'
78 ( "RTS" , 0x0004 ) # used rts/cts handshake
82 # without IEEE80211_FC0_TYPE_
88 # without IEEE80211_FC0_SUBTYPE_
89 management_subtypes = (
90 ( "ASSOC_REQ" , 0x00 ) ,
91 ( "ASSOC_RESP" , 0x10 ) ,
92 ( "REASSOC_REQ" , 0x20 ) ,
93 ( "REASSOC_RESP" , 0x30 ) ,
94 ( "PROBE_REQ" , 0x40 ) ,
95 ( "PROBE_RESP" , 0x50 ) ,
98 ( "DISASSOC" , 0xA0 ) ,
100 ( "DEAUTH" , 0xC0 ) ,
103 # without IEEE80211_FC0_SUBTYPE_
105 ( "PS_POLL" , 0xa0 ) ,
109 ( "CF_END" , 0xe0 ) ,
110 ( "CF_END_ACK" , 0xf0 )
112 # without IEEE80211_FC0_SUBTYPE_
115 ( "CF_ACK" , 0x10 ) ,
116 ( "CF_POLL" , 0x20 ) ,
117 ( "CF_ACPL" , 0x30 ) ,
120 ( "CFPOLL" , 0x60 ) ,
121 ( "CF_ACK_CF_ACK" , 0x70 ) ,
123 ( "QOS_NULL" , 0xc0 )
132 def dealWithPacket ( hdr , data ) :
134 if hdr.getlen() != hdr.getcaplen() :
135 print "Error in header : %d vs. %d" % ( hdr.getlen() , hdr.getcaplen() )
137 if len(data) != hdr.getlen() :
138 print "Data lenght does not match"
141 it_version , it_len , it_present = struct.unpack("<Bxhl",data[:8])
143 print "Bad version (%s), it is probably not radiotap header" % it_version
146 print "Bad length on radiotap header"
149 radiotap = data[:it_len]
150 payload = data[it_len:]
152 format , padstr = "<" , ""
154 for name,bit,fmt,pad in ratiotap_header_bits :
155 # What about 'it_present & ( 0x1 << bit )' ??
156 if it_present & pow(2,bit) == pow(2,bit) :
158 print "Unknown bit %s (%d) set" % ( name , bit )
160 fields.append( name )
162 fields.append( "CHANNEL_BITMAP" )
166 values = struct.unpack(format+padstr,radiotap[8:])
169 for name,value in radiotap_flags :
170 if values[1] & value == value :
174 for name,value in channel_flags :
175 if values[4] & value == value :
176 channel.append( name )
179 pcktlen = len(payload)
181 frame_ctl , frame_subtype , duration_id = struct.unpack("BBh",payload[:4])
185 for name,value in frame_type :
186 if frame_ctl & 0x0c == value :
190 print "Unknown frame type %s" % frame_ctl & 0x0c
194 for name,value in management_subtypes :
195 if frame_ctl & 0xf0 == value :
199 print "Unknown MGT subtype %s" % frame_ctl & 0xf0
203 for name,value in control_subtypes :
204 if frame_ctl & 0xf0 == value :
208 print "Unknown CTL subtype %s" % frame_ctl & 0xf0
211 elif type == "DATA" :
212 subtype = "UNDEFINED"
215 mac_str = "BBBBBB" # is leading '<' required
216 mac_fmt = "%02X:%02X:%02X:%02X:%02X:%02X"
220 maclist.append( mac_fmt % struct.unpack( mac_str , payload[pointer:pointer+6] ) )
223 # FIXME : only CTL/ACK have single address ???
224 # FIXME : Is this the same than pcktlen == 2
228 sequence = struct.unpack("BB",payload[pointer:pointer+2])
234 # print "Radiotap flags : %s" % " ".join(flags)
235 # print "Channel %d (%s)" % ( values[3] , " ".join(channel) )
236 ## print "Control info",frame_ctl,frame_subtype,duration_id,"seq",sequence
237 # print type,":",maclist,"->",pcktlen
240 global tstamp,max_time,discovered
242 if not mac in discovered :
244 discovered.append( mac )
245 if time.time()-tstamp > max_time :
246 fd = open( "discovered.list" , "a" )
247 for mac in discovered :
248 fd.write( "%s\n" % mac )
250 raise Exception( "Neighborhoud scan completed" )
252 print "%4s %13s %4d %4d from %4d" % (type,subtype,values[3],pcktlen,len(payload))," ; %4d %4d "%sequence,":"+" %s"*len(maclist) % tuple(maclist)
254 packet_limit = -1 # infinite
255 pc.loop( packet_limit , dealWithPacket )