-
Notifications
You must be signed in to change notification settings - Fork 0
/
iwscan.py
executable file
·109 lines (93 loc) · 3.58 KB
/
iwscan.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
#!/usr/bin/env python
#
# Usage example: sudo ~/iwscan.py wlan1
import subprocess
import re
import sys
class line_matcher:
def __init__(self, regexp, handler):
self.regexp = re.compile(regexp)
self.handler = handler
def handle_new_network(line, result, networks):
global wpa_block
global wps_block
wpa_block = False
wps_block = False
networks.append({})
networks[-1]['bssid'] = result.group(1)
def handle_param(line, result, networks):
global wpa_block
global wps_block
wpa_block = False
wps_block = False
networks[-1][result.group(1)] = result.group(2)
def handle_wpx_block(line, result, networks):
global wpa_block
global wps_block
global offset
wpa_block = False
wps_block = False
offset = len(result.group(1))
if result.group(2) == 'WPA':
wpa_block = True
networks[-1]['wpa'] = {}
networks[-1]['wpa']['version'] = result.group(3)
if result.group(2) == 'WPS':
wps_block = True
networks[-1]['wps'] = {}
networks[-1]['wps']['version'] = result.group(3)
def handle_wpx_param(line, result, networks):
global offset
global wpa_block
global wps_block
if len(result.group(1)) > offset:
if wpa_block:
networks[-1]['wpa'][result.group(2)] = result.group(3)
if wps_block:
networks[-1]['wps'][result.group(2)] = result.group(3)
else:
wpa_block = False
wps_block = False
def get_wpx_param(network, x, param):
if param in network[x]:
return network[x][param]
else:
return 'N/A'
if __name__ == '__main__':
proc = subprocess.Popen(['/usr/sbin/iw', sys.argv[1], 'scan'], stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
lines = stdout.split('\n')
networks = []
matchers = []
offset = 0
wpa_block = False
wps_block = False
matchers.append(line_matcher(r'^BSS (([a-zA-Z0-9]{2}:){5}[a-zA-Z0-9]{2}).*', handle_new_network))
matchers.append(line_matcher(r'\s+(SSID): (.*)', handle_param))
matchers.append(line_matcher(r'\s+(signal): -(\d+)\.\d+ dBm', handle_param))
matchers.append(line_matcher(r'\s+DS Parameter set: (channel) (\d+)', handle_param))
matchers.append(line_matcher(r'(\s+)(WP[AS]):\s+\* Version: (.*)', handle_wpx_block))
matchers.append(line_matcher(r'(\s+)\* ([^\:]+)\: (.*)', handle_wpx_param))
for line in lines:
for m in matchers:
result = m.regexp.match(line)
if result:
m.handler(line, result, networks)
break
for n in networks:
if 'wps' in n:
wps_configured = False
if get_wpx_param(n, 'wps', 'Wi-Fi Protected Setup State') == '2 (Configured)':
wps_configured = True
wps_lock_state = 'No'
if get_wpx_param(n, 'wps', 'AP setup locked') == '0x01':
wps_lock_state = 'Yes'
wps_response = 'Registrar or Enrollee'
if get_wpx_param(n, 'wps', 'Response Type') == '3 (AP)':
wps_response = 'AP'
wps_manufacturer = get_wpx_param(n, 'wps', 'Manufacturer')
wps_model = get_wpx_param(n, 'wps', 'Model')
wps_model_number = get_wpx_param(n, 'wps', 'Model Number')
wps_device_name = get_wpx_param(n, 'wps', 'Device name')
if ('wpa' in n) and wps_configured and (wps_lock_state == 'No') and (wps_response == 'AP'):
print n['SSID'], '; -', n['signal'], 'dBm; chan:', n['channel'], ';', wps_manufacturer, ';', wps_device_name, ';', wps_model, ';', wps_model_number