Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
derv82
GitHub Repository: derv82/wifite2
Path: blob/master/wifite/model/target.py
412 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from ..util.color import Color
5
6
import re
7
8
9
class WPSState:
10
NONE, UNLOCKED, LOCKED, UNKNOWN = range(0, 4)
11
12
13
class Target(object):
14
'''
15
Holds details for a 'Target' aka Access Point (e.g. router).
16
'''
17
18
def __init__(self, fields):
19
'''
20
Initializes & stores target info based on fields.
21
Args:
22
Fields - List of strings
23
INDEX KEY EXAMPLE
24
0 BSSID (00:1D:D5:9B:11:00)
25
1 First time seen (2015-05-27 19:28:43)
26
2 Last time seen (2015-05-27 19:28:46)
27
3 channel (6)
28
4 Speed (54)
29
5 Privacy (WPA2)
30
6 Cipher (CCMP TKIP)
31
7 Authentication (PSK)
32
8 Power (-62)
33
9 beacons (2)
34
10 # IV (0)
35
11 LAN IP (0. 0. 0. 0)
36
12 ID-length (9)
37
13 ESSID (HOME-ABCD)
38
14 Key ()
39
'''
40
self.bssid = fields[0].strip()
41
self.channel = fields[3].strip()
42
43
self.encryption = fields[5].strip()
44
if 'WPA' in self.encryption:
45
self.encryption = 'WPA'
46
elif 'WEP' in self.encryption:
47
self.encryption = 'WEP'
48
if len(self.encryption) > 4:
49
self.encryption = self.encryption[0:4].strip()
50
51
self.power = int(fields[8].strip())
52
if self.power < 0:
53
self.power += 100
54
55
self.beacons = int(fields[9].strip())
56
self.ivs = int(fields[10].strip())
57
58
self.essid_known = True
59
self.essid_len = int(fields[12].strip())
60
self.essid = fields[13]
61
if self.essid == '\\x00' * self.essid_len or \
62
self.essid == 'x00' * self.essid_len or \
63
self.essid.strip() == '':
64
# Don't display '\x00...' for hidden ESSIDs
65
self.essid = None # '(%s)' % self.bssid
66
self.essid_known = False
67
68
self.wps = WPSState.UNKNOWN
69
70
self.decloaked = False # If ESSID was hidden but we decloaked it.
71
72
self.clients = []
73
74
self.validate()
75
76
def validate(self):
77
''' Checks that the target is valid. '''
78
if self.channel == '-1':
79
raise Exception('Ignoring target with Negative-One (-1) channel')
80
81
# Filter broadcast/multicast BSSIDs, see https://github.com/derv82/wifite2/issues/32
82
bssid_broadcast = re.compile(r'^(ff:ff:ff:ff:ff:ff|00:00:00:00:00:00)$', re.IGNORECASE)
83
if bssid_broadcast.match(self.bssid):
84
raise Exception('Ignoring target with Broadcast BSSID (%s)' % self.bssid)
85
86
bssid_multicast = re.compile(r'^(01:00:5e|01:80:c2|33:33)', re.IGNORECASE)
87
if bssid_multicast.match(self.bssid):
88
raise Exception('Ignoring target with Multicast BSSID (%s)' % self.bssid)
89
90
def to_str(self, show_bssid=False):
91
'''
92
*Colored* string representation of this Target.
93
Specifically formatted for the 'scanning' table view.
94
'''
95
96
max_essid_len = 24
97
essid = self.essid if self.essid_known else '(%s)' % self.bssid
98
# Trim ESSID (router name) if needed
99
if len(essid) > max_essid_len:
100
essid = essid[0:max_essid_len-3] + '...'
101
else:
102
essid = essid.rjust(max_essid_len)
103
104
if self.essid_known:
105
# Known ESSID
106
essid = Color.s('{C}%s' % essid)
107
else:
108
# Unknown ESSID
109
essid = Color.s('{O}%s' % essid)
110
111
# Add a '*' if we decloaked the ESSID
112
decloaked_char = '*' if self.decloaked else ' '
113
essid += Color.s('{P}%s' % decloaked_char)
114
115
if show_bssid:
116
bssid = Color.s('{O}%s ' % self.bssid)
117
else:
118
bssid = ''
119
120
channel_color = '{G}'
121
if int(self.channel) > 14:
122
channel_color = '{C}'
123
channel = Color.s('%s%s' % (channel_color, str(self.channel).rjust(3)))
124
125
encryption = self.encryption.rjust(4)
126
if 'WEP' in encryption:
127
encryption = Color.s('{G}%s' % encryption)
128
elif 'WPA' in encryption:
129
encryption = Color.s('{O}%s' % encryption)
130
131
power = '%sdb' % str(self.power).rjust(3)
132
if self.power > 50:
133
color ='G'
134
elif self.power > 35:
135
color = 'O'
136
else:
137
color = 'R'
138
power = Color.s('{%s}%s' % (color, power))
139
140
if self.wps == WPSState.UNLOCKED:
141
wps = Color.s('{G} yes')
142
elif self.wps == WPSState.NONE:
143
wps = Color.s('{O} no')
144
elif self.wps == WPSState.LOCKED:
145
wps = Color.s('{R}lock')
146
elif self.wps == WPSState.UNKNOWN:
147
wps = Color.s('{O} n/a')
148
149
clients = ' '
150
if len(self.clients) > 0:
151
clients = Color.s('{G} ' + str(len(self.clients)))
152
153
result = '%s %s%s %s %s %s %s' % (
154
essid, bssid, channel, encryption, power, wps, clients)
155
result += Color.s('{W}')
156
return result
157
158
159
if __name__ == '__main__':
160
fields = 'AA:BB:CC:DD:EE:FF,2015-05-27 19:28:44,2015-05-27 19:28:46,1,54,WPA2,CCMP TKIP,PSK,-58,2,0,0.0.0.0,9,HOME-ABCD,'.split(',')
161
t = Target(fields)
162
t.clients.append('asdf')
163
t.clients.append('asdf')
164
print(t.to_str())
165
166
167