forked from cheeky4n6monkey/iOS_sysdiagnose_forensic_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysdiagnose-wifi-net.py
378 lines (327 loc) · 21.1 KB
/
sysdiagnose-wifi-net.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#! /usr/bin/env python
# For Python3
# Script to print the Network names (and log to TSV) from WiFi/wifi*.log
# Author: [email protected]
import sys
from optparse import OptionParser
import tarfile
import re
import shlex
version_string = "sysdiagnose-wifi-net.py v2019-05-10 Version 1.0"
if sys.version_info[0] < 3:
print("Must be using Python 3! Exiting ...")
exit(-1)
#no arguments given by user, print help and exit
if len(sys.argv) == 1:
parser.print_help()
exit(-1)
print("Running " + version_string + "\n")
usage = "\n%prog -i inputfile\n"
parser = OptionParser(usage=usage)
parser.add_option("-i", dest="inputfile",
action="store", type="string",
help="WiFi/wifi*.log To Be Searched")
(options, args) = parser.parse_args()
linecount = 0
ignorecount = 0
filterednetworkscount = 0
channelsfoundcount = 0
backgroundscancount = 0
MRUnetworkscount = 0
ppmattachedcount = 0
alreadyattachedcount = 0
filterednetworks = []
channelsfound = []
backgroundscan = []
MRUnetworks = []
ppmattached = []
alreadyattached = []
#print(options.inputfile[0:-4]) # = wifi-buf-04-26-2019__16:00:52.768.log
if options.inputfile.endswith('.tgz'): # eg wifi-buf-04-26-2019__16:00:52.768.log.tgz
# untar file first to current directory
# tarfile.open Throws InvalidHeader error!
with tarfile.open(options.inputfile, 'r:gz') as t:
t.extractall('./')
# does not get here due to exception ... todo
# readfile = ass-ume extracted name is same as input name minus the .tgz
with open(options.inputfile[0:-4], 'r') as fp:
data = fp.readlines()
# TODO: Implement processing when given zip file
else: # ass-ume input file has been untarred already and is plaintext
with open(options.inputfile, 'r') as fp:
data = fp.readlines()
for line in data:
linecount += 1
if 'AJScan: Filtered networks -' in line:
#print("\n" + line)
txts = line.split()
#print(txts, str(linecount))
if '<NOTICE>:' in line: # current log format has extra <NOTICE>: text after date/time eg "wifi-mm-dd-yyyy__hh[separator]mm[separator]ss.sss.log"
if len(txts) > 7: # 7th field is first network name (after "AJScan: Filtered networks -")
filterednetworkscount += 1
date = txts[0]
time = txts[1]
networks = txts[7:-1] # skip last item "..."
#print("$$$ Filtered Networks found = " + ' '.join(networks) + ", line = " + str(linecount))
for net in networks: # each network gets own entry
filterednetworks.append((date, time, net, linecount))
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - Filtered Networks = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
else: #archived log format eg WiFiManager/wifi-buf-mm-dd-yyyy__hh-mm-ss.sss.log
if len(txts) > 6: # 7th field is first network name (after "AJScan: Filtered networks -")
filterednetworkscount += 1
date = txts[0]
time = txts[1]
networks = txts[6:-1] # skip last item "..."
#print("$$$ Filtered Networks found = " + ' '.join(networks) + ", line = " + str(linecount))
for net in networks: # each network gets own entry
filterednetworks.append((date, time, net, linecount))
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - Filtered Networks = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
if 'Scanning 2Ghz Channels found:' in line:
#print("\n" + line)
txts = line.split()
#print(txts, str(linecount))
if '<NOTICE>:' in line: # current log format has extra <NOTICE>: text after date/time eg "wifi-mm-dd-yyyy__hh[separator]mm[separator]ss.sss.log"
# Can be empty ...
if len(txts) > 7: # there is something after the "Scanning 2Ghz Channels found:"
channelsfoundcount += 1
date = txts[0]
time = txts[1]
networks = ' '.join(txts[7:])
networks2 = networks.split(', ') # parse comma seperated string list
#print("@@@ Scanning Channels found: " + ' '.join(networks2) + ", line = " + str(linecount))
for net in networks2: # each network gets own entry
channelsfound.append((date, time, net, linecount))
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - Scanning Channels = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
else: #archived log format eg WiFiManager/wifi-buf-mm-dd-yyyy__hh-mm-ss.sss.log
# Can be empty ...
if len(txts) > 6: # there is something after the "Scanning 2Ghz Channels found:"
channelsfoundcount += 1
date = txts[0]
time = txts[1]
networks = ' '.join(txts[6:])
networks2 = networks.split(', ') # parse comma seperated string list
#print("@@@ Scanning Channels found: " + ' '.join(networks2) + ", line = " + str(linecount))
for net in networks2: # each network gets own entry
channelsfound.append((date, time, net, linecount))
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - Scanning Channels = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
if 'Preparing background scan request for ' in line:
#print("\n" + line)
txts = line.split()
#print(txts, str(linecount))
if '<NOTICE>:' in line: # current log format has extra <NOTICE>: text after date/time eg "wifi-mm-dd-yyyy__hh[separator]mm[separator]ss.sss.log"
if len(txts) > 7: # 7th field should contain network name
backgroundscancount += 1
date = txts[0]
time = txts[1]
result = re.search('Preparing background scan request for (.*) on channels:', line)
#print(result)
if (result is None): # search for 2nd variant
result = re.search('Preparing background scan request for (.*) Background Scan Caching is Enabled', line)
if (result is not None):
networks = result.group(1) # group all network names together via regex
networks2 = shlex.split(networks) # split which is "" friendly. It won't break up quoted phrases that have spaces
#print("!!! Background Scan networks found " + ' '.join(networks2) + ", line = " + str(linecount))
for net in networks2: # each network gets own entry
backgroundscan.append((date, time, net, linecount))
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - background scan request = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
else: #archived log format eg WiFiManager/wifi-buf-mm-dd-yyyy__hh-mm-ss.sss.log
if len(txts) > 6: # 7th field should contain network name
backgroundscancount += 1
date = txts[0]
time = txts[1]
result = re.search('Preparing background scan request for (.*) on channels:', line)
#print(result)
if (result is None): # search for 2nd variant
result = re.search('Preparing background scan request for (.*) Background Scan Caching is Enabled', line)
if (result is not None):
networks = result.group(1) # group all network names together via regex
networks2 = shlex.split(networks) # split which is "" friendly. It won't break up quoted phrases that have spaces
#print("!!! Background Scan networks found " + ' '.join(networks2) + ", line = " + str(linecount))
for net in networks2: # each network gets own entry
backgroundscan.append((date, time, net, linecount))
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - background scan request = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
if 'Scanning for MRU Networks:' in line:
#print("\n" + line)
txts = line.split()
#print(txts, str(linecount))
if '<NOTICE>:' in line: # current log format has extra <NOTICE>: text after date/time eg "wifi-mm-dd-yyyy__hh[separator]mm[separator]ss.sss.log"
if len(txts) > 7: # 7th field should contain network name
MRUnetworkscount += 1
date = txts[0]
time = txts[1]
# time can be 3/18/2019 6:40:17.341 Scanning for MRU Networks:
# or 1/07/2019 19:12:36.354 Scanning for MRU Networks:
tlength = len(time) # should be 11 or 12
mru = line.split(' ')# double spaces between networks eg "BLAH" on channels: 1 "BLAH2" on channels: 2
#print(mru)
#print("mru len = " + str(len(mru)))
if (tlength == 11):
# mru = [' 1/19/2019', '8:46:40.818 Scanning for MRU Networks:', '"Marriott_GUEST" on channels: 136 48 \n']
if len(mru) > 2: # has to be something after "Scanning for MRU Networks:"
#print("### MRU networks found: " + ', '.join(mru[2:]) + ", line = " + str(linecount))
for m in mru[2:]:
MRUnetworks.append((date, time, m.rstrip(), linecount))
if (tlength == 12):
# mru = [' 1/07/2019 19:11:57.026 Scanning for MRU Networks:', '"AndroidAP" on channels: 1', '"Vodafone" on channels: 36', '"*WIFI-AIRPORT" on channels: 60 40 \n']
if len(mru) > 1: # has to be something after "Scanning for MRU Networks:"
#print("### MRU networks found: " + ', '.join(mru[1:]) + ", line = " + str(linecount))
for m in mru[1:]:
MRUnetworks.append((date, time, m.rstrip(), linecount))
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - MRU networks = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
else: #archived log format eg WiFiManager/wifi-buf-mm-dd-yyyy__hh-mm-ss.sss.log
if len(txts) > 6: # 7th field should contain network name
MRUnetworkscount += 1
date = txts[0]
time = txts[1]
# time can be 3/18/2019 6:40:17.341 Scanning for MRU Networks:
# or 1/07/2019 19:12:36.354 Scanning for MRU Networks:
tlength = len(time) # should be 11 or 12
mru = line.split(' ')# double spaces between networks eg "BLAH" on channels: 1 "BLAH2" on channels: 2
#print(mru)
#print("mru len = " + str(len(mru)))
if (tlength == 11):
# mru = [' 1/19/2019', '8:46:40.818 Scanning for MRU Networks:', '"Marriott_GUEST" on channels: 136 48 \n']
if len(mru) > 2: # has to be something after "Scanning for MRU Networks:"
#print("### MRU networks found: " + ', '.join(mru[2:]) + ", line = " + str(linecount))
for m in mru[2:]:
MRUnetworks.append((date, time, m.rstrip(), linecount))
if (tlength == 12):
# mru = [' 1/07/2019 19:11:57.026 Scanning for MRU Networks:', '"AndroidAP" on channels: 1', '"Vodafone" on channels: 36', '"*WIFI-AIRPORT" on channels: 60 40 \n']
if len(mru) > 1: # has to be something after "Scanning for MRU Networks:"
#print("### MRU networks found: " + ', '.join(mru[1:]) + ", line = " + str(linecount))
for m in mru[1:]:
MRUnetworks.append((date, time, m.rstrip(), linecount))
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - MRU networks = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
if '__WiFiDeviceManagerReleasePpmResource: PPM attached - still connected to ' in line:
#print("\n" + line)
txts = line.split()
#print(txts, str(linecount))
if '<NOTICE>:' in line: # current log format has extra <NOTICE>: text after date/time eg "wifi-mm-dd-yyyy__hh[separator]mm[separator]ss.sss.log"
if len(txts) > 10:
ppmattachedcount += 1
date = txts[0]
time = txts[1]
net = txts[10:] # Assuming only one entry
#print("$$$ PPM attached networks found = " + ' '.join(net).rstrip('.') + ", line = " + str(linecount))
ppmattached.append((date, time, ' '.join(net).rstrip('.'), linecount)) # net has . at end
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - PPM attached networks = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
else: #archived log format eg WiFiManager/wifi-buf-mm-dd-yyyy__hh-mm-ss.sss.log
if len(txts) > 9:
ppmattachedcount += 1
date = txts[0]
time = txts[1]
net = txts[9:] # Assuming only one entry
#print("$$$ PPM attached networks found = " + ' '.join(net).rstrip('.') + ", line = " + str(linecount))
ppmattached.append((date, time, ' '.join(net).rstrip('.'), linecount)) # net has . at end
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - PPM attached networks = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
if '__WiFiDeviceManagerAutoAssociate: Already connected to ' in line:
#print("\n" + line)
txts = line.split()
#print(txts, str(linecount))
if '<NOTICE>:' in line: # current log format has extra <NOTICE>: text after date/time eg "wifi-mm-dd-yyyy__hh[separator]mm[separator]ss.sss.log"
if len(txts) > 7:
alreadyattachedcount += 1
date = txts[0]
time = txts[1]
net = txts[7:] # Assuming only one entry
#print("$$$ Already attached networks found = " + ' '.join(net).rstrip('.') + ", line = " + str(linecount))
alreadyattached.append((date, time, ' '.join(net).rstrip('.'), linecount)) # net has . at end
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - Already attached networks = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
else: #archived log format eg WiFiManager/wifi-buf-mm-dd-yyyy__hh-mm-ss.sss.log
if len(txts) > 6:
alreadyattachedcount += 1
date = txts[0]
time = txts[1]
net = txts[6:] # Assuming only one entry
#print("$$$ Already attached networks found = " + ' '.join(net).rstrip('.') + ", line = " + str(linecount))
alreadyattached.append((date, time, ' '.join(net).rstrip('.'), linecount)) # net has . at end
else:
#wrong number of fields (eg missing date) ... skip hit completely
print("\nERROR - Already attached networks = Wrong Number of fields in file (line = " + str(linecount) + ") - Ignoring ...\n")
ignorecount += 1
print("\n\n=====================================================")
print("\nFound " + str(filterednetworkscount) + " filtered network messages in " + options.inputfile)
print("\nFound " + str(channelsfoundcount) + " found channel messages in " + options.inputfile)
print("\nFound " + str(backgroundscancount) + " background scan channel messages in " + options.inputfile)
print("\nFound " + str(MRUnetworkscount) + " MRU network messages in " + options.inputfile)
print("\nFound " + str(ppmattachedcount) + " PPM attached still connected messages in " + options.inputfile)
print("\nFound " + str(alreadyattachedcount) + " already attached still connected messages in " + options.inputfile)
print("\n=====================================================\n")
if filterednetworkscount > 0:
f_filtered = open("wifi-buf-net_filtered.TSV", "w")
f_filtered.write("DATE\tTIME\tTYPE\tNETWORK\tLINE\tLOGFILE\n")
for date, time, net, linecount in filterednetworks:
f_filtered.write(date + "\t" + time + "\tFilteredNetworks\t" + net +"\t" + str(linecount) + "\t" + options.inputfile + "\n")
f_filtered.close()
print("Logged "+ str(len(filterednetworks)) + " FilteredNetworks to wifi-buf-net_filtered.TSV output file\n")
if channelsfoundcount > 0:
f_channel = open("wifi-buf-net_channels.TSV", "w")
f_channel.write("DATE\tTIME\tTYPE\tNETWORK\tLINE\tLOGFILE\n")
for date, time, net, linecount in channelsfound:
f_channel.write(date + "\t" + time + "\tFoundChannels\t" + net +"\t" + str(linecount) + "\t" + options.inputfile + "\n")
f_channel.close()
print("Logged "+ str(len(channelsfound)) + " FoundChannels to wifi-buf-net_channels.TSV output file\n")
if backgroundscancount > 0:
f_bgscan = open("wifi-buf-net_bgscan.TSV", "w")
f_bgscan.write("DATE\tTIME\tTYPE\tNETWORK\tLINE\tLOGFILE\n")
for date, time, net, linecount in backgroundscan:
f_bgscan.write(date + "\t" + time + "\tBGScans\t" + net +"\t" + str(linecount) + "\t" + options.inputfile + "\n")
f_bgscan.close()
print("Logged "+ str(len(backgroundscan)) + " BGScans to wifi-buf-net_bgscan.TSV output file\n")
if MRUnetworkscount > 0:
f_mru = open("wifi-buf-net_mru.TSV", "w")
f_mru.write("DATE\tTIME\tTYPE\tNETWORK\tLINE\tLOGFILE\n")
for date, time, m, linecount in MRUnetworks:
f_mru.write(date + "\t" + time + "\tMRUs\t" + m +"\t" + str(linecount) + "\t" + options.inputfile + "\n")
f_mru.close()
print("Logged "+ str(len(MRUnetworks)) + " MRUs to wifi-buf-net_mru.TSV output file\n")
if ppmattachedcount > 0:
f_mru = open("wifi-buf-net_ppmattached.TSV", "w")
f_mru.write("DATE\tTIME\tTYPE\tNETWORK\tLINE\tLOGFILE\n")
for date, time, m, linecount in ppmattached:
f_mru.write(date + "\t" + time + "\tPPMAttached\t" + m +"\t" + str(linecount) + "\t" + options.inputfile + "\n")
f_mru.close()
print("Logged "+ str(len(ppmattached)) + " PPMAttached to wifi-buf-net_ppmattached.TSV output file\n")
if alreadyattachedcount > 0:
f_mru = open("wifi-buf-net_alreadyattached.TSV", "w")
f_mru.write("DATE\tTIME\tTYPE\tNETWORK\tLINE\tLOGFILE\n")
for date, time, m, linecount in alreadyattached:
f_mru.write(date + "\t" + time + "\tAlreadyAttached\t" + m +"\t" + str(linecount) + "\t" + options.inputfile + "\n")
f_mru.close()
print("Logged "+ str(len(alreadyattached)) + " AlreadyAttached to wifi-buf-net_alreadyattached.TSV output file\n")
print("=====================================================\n")
print("Ignored " + str(ignorecount) + " malformed entries")
print("Exiting ...\n")