| 1 |
#!/usr/bin/env python |
| 2 |
#airodump parsing lib |
| 3 |
#returns in an array of client and Ap information |
| 4 |
#part of the airdrop-ng project |
| 5 |
from sys import exit as Exit |
| 6 |
|
| 7 |
class airDumpParse: |
| 8 |
def parser(self,file): |
| 9 |
""" |
| 10 |
One Function to call to parse a file and return the information |
| 11 |
""" |
| 12 |
self.capr = None |
| 13 |
self.NAP = None |
| 14 |
self.NA = None |
| 15 |
self.apDict = None |
| 16 |
self.clientDict = None |
| 17 |
fileOpenResults = self.airDumpOpen(file) |
| 18 |
self.airDumpParse(fileOpenResults) |
| 19 |
self.clientApChannelRelationship() |
| 20 |
return {'NA':self.NA,'capr':self.capr,'apDict':self.apDict, |
| 21 |
'clientDict':self.clientDict,'NAP':self.NAP} |
| 22 |
|
| 23 |
def airDumpOpen(self,file): |
| 24 |
""" |
| 25 |
Takes one argument (the input file) and opens it for reading |
| 26 |
Returns a list full of data |
| 27 |
""" |
| 28 |
try: |
| 29 |
openedFile = open(file, "r") |
| 30 |
except IOError: |
| 31 |
print("Error Airodump File",file,"does not exist") |
| 32 |
Exit(1) |
| 33 |
data = openedFile |
| 34 |
cleanedData = [line.rstrip() for line in data] |
| 35 |
openedFile.close() |
| 36 |
return cleanedData |
| 37 |
|
| 38 |
def airDumpParse(self,cleanedDump): |
| 39 |
""" |
| 40 |
Function takes parsed dump file list and does some more cleaning. |
| 41 |
Returns a list of 2 dictionaries (Clients and APs) |
| 42 |
""" |
| 43 |
try: #some very basic error handling to make sure they are loading up the correct file |
| 44 |
try: |
| 45 |
apStart = cleanedDump.index('BSSID, First time seen, Last time seen, Channel, Speed, Privacy, Power, # beacons, # data, LAN IP, ESSID') |
| 46 |
except Exception: |
| 47 |
apStart = cleanedDump.index('BSSID, First time seen, Last time seen, channel, Speed, Privacy, Cipher, Authentication, Power, # beacons, # IV, LAN IP, ID-length, ESSID, Key') |
| 48 |
del cleanedDump[apStart] #remove the first line of text with the headings |
| 49 |
try: |
| 50 |
stationStart = cleanedDump.index('Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs') |
| 51 |
except Exception: |
| 52 |
stationStart = cleanedDump.index('Station MAC, First time seen, Last time seen, Power, # packets, BSSID, ESSID') |
| 53 |
except Exception: |
| 54 |
print("Invalid input file. Please make sure you are loading an airodump txt file and not a pcap") |
| 55 |
Exit(1) |
| 56 |
|
| 57 |
del cleanedDump[stationStart] #Remove the heading line |
| 58 |
clientList = cleanedDump[stationStart:] #Splits all client data into its own list |
| 59 |
del cleanedDump[stationStart:] #The remaining list is all of the AP information |
| 60 |
self.apDict = self.apTag(cleanedDump) |
| 61 |
self.clientDict = self.clientTag(clientList) |
| 62 |
return |
| 63 |
|
| 64 |
def apTag(self,devices): |
| 65 |
""" |
| 66 |
Create a ap dictionary with tags of the data type on an incoming list |
| 67 |
""" |
| 68 |
dict = {} |
| 69 |
for entry in devices: |
| 70 |
ap = {} |
| 71 |
string_list = entry.split(',') |
| 72 |
#sorry for the clusterfuck but I swear it all makes sense, this is building a dic from our list so we don't have to do position calls later |
| 73 |
len(string_list) |
| 74 |
if len(string_list) == 15: |
| 75 |
ap = {"bssid":string_list[0].replace(' ',''), |
| 76 |
"fts":string_list[1], |
| 77 |
"lts":string_list[2], |
| 78 |
"channel":string_list[3].replace(' ',''), |
| 79 |
"speed":string_list[4], |
| 80 |
"privacy":string_list[5].replace(' ',''), |
| 81 |
"cipher":string_list[6], |
| 82 |
"auth":string_list[7], |
| 83 |
"power":string_list[8], |
| 84 |
"beacons":string_list[9], |
| 85 |
"iv":string_list[10], |
| 86 |
"ip":string_list[11], |
| 87 |
"id":string_list[12], |
| 88 |
"essid":string_list[13][1:], |
| 89 |
"key":string_list[14]} |
| 90 |
elif len(string_list) == 11: |
| 91 |
ap = {"bssid":string_list[0].replace(' ',''), |
| 92 |
"fts":string_list[1], |
| 93 |
"lts":string_list[2], |
| 94 |
"channel":string_list[3].replace(' ',''), |
| 95 |
"speed":string_list[4], |
| 96 |
"privacy":string_list[5].replace(' ',''), |
| 97 |
"power":string_list[6], |
| 98 |
"beacons":string_list[7], |
| 99 |
"data":string_list[8], |
| 100 |
"ip":string_list[9], |
| 101 |
"essid":string_list[10][1:]} |
| 102 |
if len(ap) != 0: |
| 103 |
dict[string_list[0]] = ap |
| 104 |
return dict |
| 105 |
|
| 106 |
def clientTag(self,devices): |
| 107 |
""" |
| 108 |
Create a client dictionary with tags of the data type on an incoming list |
| 109 |
""" |
| 110 |
dict = {} |
| 111 |
for entry in devices: |
| 112 |
client = {} |
| 113 |
string_list = entry.split(',') |
| 114 |
if len(string_list) >= 7: |
| 115 |
client = {"station":string_list[0].replace(' ',''), |
| 116 |
"fts":string_list[1], |
| 117 |
"lts":string_list[2], |
| 118 |
"power":string_list[3], |
| 119 |
"packets":string_list[4], |
| 120 |
"bssid":string_list[5].replace(' ',''), |
| 121 |
"probe":string_list[6:][0:]} |
| 122 |
if len(client) != 0: |
| 123 |
dict[string_list[0]] = client |
| 124 |
return dict |
| 125 |
|
| 126 |
def clientApChannelRelationship(self): |
| 127 |
""" |
| 128 |
parse the dic for the relationships of client to ap |
| 129 |
in the process also populate list of |
| 130 |
""" |
| 131 |
clients = self.clientDict |
| 132 |
AP = self.apDict |
| 133 |
NA = [] #create a var to keep the not associated clients mac's |
| 134 |
NAP = [] #create a var to keep track of associated clients mac's to AP's we can't see |
| 135 |
apCount = {} #count number of Aps dict is faster the list stored as BSSID:number of essids |
| 136 |
apClient = {} #dict that stores bssid and clients as a nested list |
| 137 |
for key in (clients): |
| 138 |
mac = clients[key] #mac is the MAC address of the client |
| 139 |
if mac["bssid"] != ' (notassociated) ': #one line of our dictionary of clients |
| 140 |
if mac["bssid"] in AP: # if it is check to see it's an AP we can see and have info on |
| 141 |
if mac["bssid"] in apClient: |
| 142 |
apClient[mac["bssid"]].extend([key]) #if key exists append new client |
| 143 |
else: |
| 144 |
apClient[mac["bssid"]] = [key] #create new key and append the client |
| 145 |
else: NAP.append(key) # stores the clients that are talking to an access point we can't see |
| 146 |
else: NA.append(key) #stores the lines of the not associated AP's in a list |
| 147 |
self.NAP = NAP |
| 148 |
self.NA = NA |
| 149 |
self.capr = apClient |
| 150 |
return |