-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect.py
More file actions
executable file
·275 lines (236 loc) · 8.14 KB
/
collect.py
File metadata and controls
executable file
·275 lines (236 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python
import subprocess
import re
import os
import sys
import signal
import tempfile
from time import sleep
from datetime import datetime
# variables
folder = "/usr/share/essid-collect/"
collecting_file = "collecting"
essids_file = "essids.csv"
essids = {}
# essids["testWlan"]=["01-01-2017","ap","client","macs"]
def signal_handler(signal, frame):
print('\n stopping')
stop()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
def main():
print("in the works!!")
monInterface = startmon()
if monInterface==None:
stop()
print("monInterface: ."+monInterface+".")
ret = makeDir()
if ret == None:
stop()
startdump(monInterface)
collecting()
def stop():
print("stop")
stopcollecting()
stopdump()
stopmon()
sys.exit(1)
def startmon():
print("startmon")
#search device
devs = subprocess.check_output(['ifconfig']).decode("utf-8")
devs = devs.splitlines()
monInterface = []
for line in devs:
if line.find("mon")>=0 or line.find("wlp")==0 or line.find("wlan")==0:
if line.find("PROMISC")>=0:
monInterface.append(re.search(".+?(:|\s)",line).group()[:-1])
print("1 potential monitor interface: ."+re.search(".+?(:|\s)",line).group()[:-1]+".")
if len(monInterface)>0:
for i in monInterface:
if i.find("mon")>=0:
return i
return monInterface[0]
# start monitor interface
wlanInterface = ""
for dev in getDevices():
if dev.find("wlp")>=0 or dev.find("wlan")>=0:
wlanInterface = dev
break
if wlanInterface == "":
print("No wlan interface found")
return None
needsRoot()
# p = subprocess.Popen(['ifconfig',wlanInterface,'promisc'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p = subprocess.Popen(['airmon-ng','start',wlanInterface], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()
p.wait()
if p.returncode==1:
print("ifconfig promisc error")
return None
devs = subprocess.check_output(['ifconfig']).decode("utf-8")
devs = devs.splitlines()
monInterface = []
for line in devs:
if line.find("mon")>=0 or line.find("wlp")==0 or line.find("wlan")==0:
if line.find("PROMISC")>=0 or True:
monInterface.append(re.search(".+?(:|\s)",line).group()[:-1])
print("2 potential monitor interface: ."+re.search(".+?(:|\s)",line).group()[:-1]+".")
if len(monInterface)>0:
for i in monInterface:
if i.find("mon")>=0:
return i
return monInterface[0]
else:
return None
def makeDir():
needsRoot()
p = subprocess.Popen(['mkdir','-p',folder], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()
if p.returncode==1:
print("couldn't create folder "+folder)
return None
p = subprocess.Popen(['rm','-rf',folder+"*"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()
if p.returncode==1:
print("couldn't delete files from "+folder)
return None
return 0
def startdump(monInterface):
print("startdump")
purgeFiles(folder, collecting_file+".*\.csv")
p = subprocess.Popen(['airodump-ng','-w',os.path.join(folder,collecting_file),'--output-format','csv',monInterface], stdout=tempfile.TemporaryFile(), stderr=subprocess.STDOUT)
return p
def collecting():
print("collecting\n")
while (True):
sleep(5)
# print("analyze")
for f in os.listdir(folder):
if f.find(collecting_file)<0:
continue
fi = open(os.path.join(folder,f),"r")
r = fi.read().splitlines()
fi.close()
mode = "before"
for line in r:
if (line.find("BSSID")==0):
mode = "AP"
continue
if (line.find("Station")==0):
mode = "client"
continue
if (len(line)<3):
continue
line = line.split(",")
if (mode=="AP" and len(line)>13):
addnew(line[13].strip(), True, line[0].strip(), line[2].strip())
if (mode=="client" and len(line)>6):
for i in line[6:]:
addnew(i.strip(), False, "", line[2].strip())
def addnew(entry, ap, mac, last):
entry = str(entry).strip()
if len(entry)<1:
return
if not (entry in essids):
print(entry)
essids[entry] = {}
essids[entry]['mac'] = set()
essids[entry]['last-time'] = str(last)
if ap:
essids[entry]['mac'].add(mac)
essids[entry]['ap'] = "x"
else:
essids[entry]['client'] = "x"
def stopcollecting():
print("stop collecting")
unionessids()
writeessidsfile()
def unionessids():
print("unionessids")
for f in os.listdir(folder):
if f.find(essids_file)<0:
continue
fi = open(os.path.join(folder, f),"r")
r = fi.read().splitlines()
fi.close()
# essids["testWlan"]=["01-01-2017","ap","client","macs"]
for line in r:
line = line.split(",")
if not line[0] in essids:
essids[line[0]] = {}
essids[line[0]]['mac'] = set()
essids[line[0]]['last-time'] = str(line[1])
if line[2].find("x")>=0:
essids[line[0]]['ap']="x"
if line[3].find("x")>=0:
essids[line[0]]['client']="x"
for i in line[4:]:
if len(i)>=1:
essids[line[0]]['mac'].add(i)
def writeessidsfile():
print("writeessidsfile")
fi = open(os.path.join(folder,essids_file),"w")
for k,v in essids.items():
fi.write(k+",")
for e in ["last-time","ap","client","mac"]:
if e in v:
if type(v[e]) is set:
for el in v[e]:
fi.write(el+",")
else:
fi.write(v[e]+",")
else:
fi.write(",")
fi.write("\n")
fi.close()
def stopdump():
print("stopdump")
p = subprocess.Popen(['killall','airodump-ng'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
purgeFiles(folder, collecting_file+".*\.csv")
def getMonInterface():
print("getMonInterface")
devs = subprocess.check_output(['ifconfig']).decode("utf-8")
devs = devs.splitlines()
monInterface = []
def stopmon():
print("stopmon")
# stop monitor interface
devs = subprocess.check_output(['ifconfig']).decode("utf-8")
devs = devs.splitlines()
monInterface = []
for line in devs:
if line.find("mon")>=0 or line.find("wlp")==0 or line.find("wlan")==0:
if line.find("PROMISC")>=0 or True:
monInterface.append(re.search(".+?(:|\s)",line).group()[:-1])
print("3 potential monitor interface: ."+re.search(".+?(:|\s)",line).group()[:-1]+".")
monI = ""
if len(monInterface)>0:
for i in monInterface:
if i.find("mon")>=0:
monI = i
monI = monInterface[0]
else:
monI = ""
needsRoot()
# p = subprocess.Popen(['ifconfig',wlanInterface,'-promisc'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p = subprocess.Popen(['airmon-ng','stop',monI], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()
if p.returncode==1:
print("ifconfig -promisc error")
return None
return monI
def getDevices():
devs = subprocess.check_output(["ls","/sys/class/net/"]).decode("utf-8")
devs = devs.splitlines()
return devs
def needsRoot():
if os.geteuid() !=0:
click.echo("root privileges needed, run with sudo essid-collect ...")
sys.exit(1)
def purgeFiles(dir, pattern):
for f in os.listdir(dir):
if re.search(pattern, f):
os.remove(os.path.join(dir,f))
if __name__ == '__main__':
main()