Debian Codegraph @

scripts/airdrop-ng/airdrop/libDumpParse.py

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