I think this question can be answered by most python users and is quite general in terms of knowledge but for me I can't figure it out.
This is the code:
if ssid in net and p.addr2 not in clients:
count +=1
get_oui(p.addr2)
net.append(ssid)
checkmac(p.addr2)
mps+=1
print str(count)+'>',p.addr2+' ('+G+macf+W+') <--Probing--> '+O+ssid+W+' < '+Y+'MPS'+W
if args.log:
wr_log(p.addr2,ssid,macf)
elif ssid not in net and p.addr2 in clients:
count +=1
net.append(ssid)
get_oui(p.addr2)
clients.append(p.addr2)
mpm+=1
print str(count)+'>',p.addr2+' ('+G+macf+W+') <--Probing--> '+O+ssid+W+' < '+Y+'MPM'+W
if args.log:
wr_log(p.addr2,ssid,macf)
elif ssid not in net and p.addr2 not in clients:
count +=1
net.append(ssid)
get_oui(p.addr2)
checkmac(p.addr2)
print str(count)+'>',p.addr2+' ('+G+macf+W+') <--Probing--> '+O+ssid+W
if args.log:
wr_log(p.addr2,ssid,macf)
Now heres what you guys need to know: this script is monitoring and analyzing wireless packets one by one from the air and from each packet I extract the ssid, mac address and manufacturer data. Clients are prone to sending loads of duplicate packets with same data and amongst them will be unique packets with unique data.
Current SSID is stored ssid and current MAC is stored in p.addr2. Previous ssid and p.addr2 values are stored in the lists - 'net' and 'clients', respectively.
For most of the packets my code survives and is valid but for one special condition I am lost. Consider these hypothetical values of ssid and mac addresses:
SSID MAC
S1 A
S2 A
S1 B
S2 B
For the first scenario, the third condition holds true For the second scenario, the second condition holds true For the third scenrario, the first condition holds true For the fourth condition, none of the conditions hold true, am i right? Because when the lists are checked with the "not in" and "in" operands it finds both the client and the ssid are already there and hence drops the packet whereas this is a valid condition and means that basically 2 clients are looking for the same same ssid and I would want it to be printed. But if I do this:
elif ssid in net and p.addr2 in clients:
get_oui(p.addr2)
checkmac(p.addr2)
print str(count)+'>',p.addr2+' ('+G+macf+W+') <--Probing--> '+O+ssid+W
Duplicate packets start printing out because each client sends multiple packets with same data whereas this is situation can arise. How do I implement a condition so that I can validate such situations where multiple clients in the clients[] are looking for multiple ssid's in net[]???
According to cmidi's suggestion: I tried to use a dictionary and tried to access it this way, its still giving me duplicates!
if count > 0:
for k,v in obs.items():
if k and v != p.addr2 and ssid:
count +=1
get_oui(p.addr2)
net.append(ssid)
checkmac(p.addr2)
obs[p.addr2] = ssid
mps+=1
print str(count)+'>',p.addr2+' ('+G+macf+W+') <--Probing--> '+O+ssid+W+' < '+Y+'MPS'+W
if args.log:
wr_log(p.addr2,ssid,macf)
else:
count +=1
get_oui(p.addr2)
net.append(ssid)
checkmac(p.addr2)
obs[p.addr2] = ssid
print str(count)+'>',p.addr2+' ('+G+macf+W+') <--Probing--> '+O+ssid+W+' < '+Y+'MPS'+W
if args.log:
wr_log(p.addr2,ssid,macf)
Whats going on here?
@lmz Ok so according to request this is how everything works or rather should work:
For each packet I scan, I obtain my SSID, MAC and Manufacturer values. I want to save this data, only the SSID and MAC, together because as a pair they will be unique after discarding the other duplicate packets. For the first packet, I print the values without any check and we have our first set of values, then from the second packet onwards the check for duplicates and certain conditions become active. Ideally to lessen code as much as possible, here there should be a direct check for the SSID:MAC pairs captured since the first packet in the list, dict, ordereddict etc. Here more than being ordered, they code needs to be able to scan through all previous pairs removing each pair that in the list, dict that does not match the incoming SSID:MAC. In case a duplicate is not found, the new SSID:MAC pair is added to the list, dict etc. otherwise if it finds a duplicate, the loop breaks and we move onto the third packet and so on.