-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwatchtower.py
More file actions
342 lines (273 loc) · 11.4 KB
/
watchtower.py
File metadata and controls
342 lines (273 loc) · 11.4 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
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
#!/usr/bin/env python
import sys, os, signal
from multiprocessing import Process
import json
import requests
from scapy.all import *
import argparse
import csv
parser = argparse.ArgumentParser(description="WatchTower - Detect Rogue APs", prog='watchtower')
parser.add_argument('--tune', required=False, dest='tune', action='store_true',
help='Collect samples of average signal strength from each AP')
parser.add_argument('adapter', help='Name of wireless adapter in promiscuous mode')
parser.set_defaults(tune=False)
args = parser.parse_args()
#####################
#
# Global Variables
#
#####################
interface = '' # monitor interface
aps = set() # dictionary to store unique APs
clients = set()
apSignals = {} # For --tune mode
deauthTimes = {}
deauthAlertTimes = {}
deauthAlertTimeout = 5 # How long (in seconds) minimum to wait between detected deauths to call it a new attack
macVendors = {}
def checkAP(ap_mac, ap_channel, ap_enc, ap_cipher, ap_auth, ap_strength):
if config['checks']['checkMAC']:
if ap_mac.upper() not in config['macs']:
return False
if config['checks']['checkChannel']:
if ap_channel != config['channel']:
print("Bad channel: ", ap_channel, " ", config['channel'])
return False
if config['checks']['checkEncryption']:
if ap_enc != config['encryption']:
print("Bad encryption: ", ap_enc, " ", config['encryption'])
return False
if config['checks']['checkCipher']:
if ap_cipher != config['cipher']:
print("Bad cipher: ", ap_cipher, " ", config['cipher'])
return False
if config['checks']['checkAuthentication']:
if ap_auth != config['authentication']:
print("Bad auth: ", ap_auth, " - ", config['authentication'])
return False
if config['checks']['checkStrength']:
upper = config['signalStrength'] + config['strengthVariance']
lower = config['signalStrength'] - config['strengthVariance']
if ap_strength < lower or ap_strength > upper:
return False
return True
def noise_filter(addr1, addr2):
# Broadcast, broadcast, IPv6mcast, spanning tree, spanning tree, multicast, broadcast
ignore = ['ff:ff:ff:ff:ff:ff', '00:00:00:00:00:00', '33:33:00:', '33:33:ff:', '01:80:c2:00:00:00', '01:00:5e:'] # possibly add our detecting MAC address
for i in ignore:
if i in addr1 or i in addr2:
return True
def getWPA2info(pkt):
# pkt = p.getlayer(Dot11Elt, ID=48)
# print(pkt.ID, len(pkt.info), pkt.info)
# Array slices don't include end index so add 1
# 00-0F-AC-01 WEP40
# 00-0F-AC-05 WEP104
# 00-0F-AC-04 CCMP
# 00-0F-AC-02 TKIP
# OUI = [2:4]
# groupCipherOUI = pkt.info[2:5]
groupCipherOUI = pkt.group_cipher_suite.oui
# Group Cipher Type = [5]
# 1 = WEP40, 2 = TKIP, 4 = CCMP, 5 = WEP104
# groupCipherType = pkt.info[5]
groupCipherType = pkt.group_cipher_suite.cipher
if groupCipherType == 1:
cipher = "WEP40"
elif groupCipherType == 2:
cipher = "TKIP"
elif groupCipherType == 4:
cipher = "CCMP"
elif groupCipherType == 5:
cipher = "WEP104"
else:
cipher = "???"
# Pairwise Cipher Count = [6:7]
# pairwiseCipherCount = pkt.info[6]
# PairwiseKey Cipher List (array?) = [8:11]
# pairwiseCipherOUI =
# AuthKey Mngmnt Count = [12:13]
# authKeyMgmtCount = pkt.info[12]
authKeyMgmtCount = pkt.nb_akm_suites
# AuthKey Mngmnt Suite List = [14:17]
# 00-0f-ac-02 PSK
# 00-0f-ac-01 802.1x (EAP)
# authKeyMgmtSuite = pkt.info[14:18]
if pkt.akm_suites[0].suite == 2:
auth = "PSK"
authKeyMgmtSuite = b'\x00\x0f\xac\x02'
elif pkt.akm_suites[0].suite == 1:
auth = "EAP"
authKeyMgmtSuite = b'\x00\x0f\xac\x01'
elif pkt.akm_suites[0].suite == 0:
auth = "RESERVED"
authKeyMgmtSuite = "???"
else:
auth = "???"
authKeyMgmtSuite = "???"
# DEBUG
if cipher == '???' or auth == '???':
print("Unknown cipher or auth.")
return {
"groupCipherOUI": groupCipherOUI,
"groupCipherType": groupCipherType,
"cipher": cipher,
"authKeyMgmtCount": authKeyMgmtCount,
"authKeyMgmtSuite": authKeyMgmtSuite,
"auth": auth
}
def sendSlackNotification(message):
response = requests.post(
config['slackWebhook'], json={"text":message},
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
def sniffAP(pkt):
# Look for clients of our network
if pkt.haslayer(Dot11) and pkt.type == 2:
if noise_filter(pkt.addr1, pkt.addr2):
return
if pkt.addr1.upper() in config['macs'] and pkt.addr2.upper() not in clients:
clients.add(pkt.addr2.upper())
elif pkt.addr2.upper() in config['macs'] and pkt.addr1.upper() not in clients:
clients.add(pkt.addr1.upper())
# Watch for deauth-ing of our clients
elif pkt.haslayer(Dot11Deauth):
sourceMAC = str(pkt[Dot11].addr2).upper()
if sourceMAC not in clients: # We only care about our AP and clients
return
if sourceMAC in deauthTimes:
timeFromLastDeauth = time.time() - deauthTimes[sourceMAC]
if timeFromLastDeauth < 2:
if sourceMAC not in deauthAlertTimes or time.time() - deauthAlertTimes[sourceMAC] > deauthAlertTimeout:
print("Deauth detected! Targeted client: " + sourceMAC)
if config['sendSlackNotify']:
sendSlackNotification(":rotating_light: Deauth attack detected! :rotating_light: Targeted client: " + sourceMAC)
deauthAlertTimes[sourceMAC] = time.time()
deauthTimes[sourceMAC] = time.time()
# process unique sniffed Beacons and ProbeResponses.
if pkt.haslayer(Dot11Beacon) or pkt.haslayer(Dot11ProbeResp):
if pkt.haslayer(Dot11FCS):
bssid = str(pkt[Dot11FCS].addr3).upper()
else:
bssid = str(pkt[Dot11].addr3).upper()
ssid = pkt[Dot11Elt].info.decode('UTF-8')
channel = int(ord(pkt[Dot11Elt:3].info))
capability = pkt.sprintf("{Dot11Beacon:%Dot11Beacon.cap%}\
{Dot11ProbeResp:%Dot11ProbeResp.cap%}")
strength = pkt[RadioTap].dBm_AntSignal
# Check for encrypted networks
if re.search("privacy", capability):
priv = 'Y'
else:
priv = 'N'
# Determine encryption type
if pkt.getlayer(Dot11Elt, ID=48) is not None:
enc = "WPA2"
elif pkt.getlayer(Dot11Elt, ID=221) is not None and pkt.getlayer(Dot11Elt, ID=221).info.startswith(
b'\x00P\xf2\x01\x01\x00'):
enc = "WPA"
else:
if priv == 'Y':
enc = "WEP"
else:
enc = "OPEN"
if ssid == config['ssid']:
if enc == "WPA2":
apInfo = getWPA2info(pkt.getlayer(Dot11Elt, ID=48))
else:
apInfo = {}
currentAP = "{:>2d} {:s} {:s} {:s} {:s} {:s} {:s}".format(
int(channel), priv, enc, apInfo["cipher"], apInfo["auth"], bssid, ssid)
if currentAP not in aps: # This is an AP we haven't seen before
aps.add(currentAP)
currentAP = "{:>2d} {:s} {:s} {:s} {:s} {:s} {:s} {:s}".format(
int(channel), priv, enc, apInfo["cipher"], apInfo["auth"], str(strength), bssid, ssid)
if checkAP(bssid, channel, enc, apInfo["cipher"], apInfo["auth"], strength):
print("[Good AP] ", currentAP)
else:
print("[Bad AP] ", currentAP)
vendor = macVendors[bssid[0:8].replace(':', '')]
print("[Bad AP] Manufacturer: ", vendor)
if config['sendSlackNotify']:
sendSlackNotification(":rotating_light: Rogue AP detected! :rotating_light: \n *Channel*: " + str(int(channel)) +
"\n *Privacy*: " + priv +
"\n *Encryption*: " + enc +
"\n *Cipher*: " + apInfo['cipher'] +
"\n *Authentication*: " + apInfo["auth"] +
"\n *MAC*: " + bssid +
"\n *SSID*: " + ssid +
"\n *Vendor*: " + vendor)
def tune(pkt):
bssid, ssid, channel, strength = '', '', '', ''
if pkt.haslayer(Dot11Beacon) or pkt.haslayer(Dot11ProbeResp):
if pkt.haslayer(Dot11FCS):
bssid = str(pkt[Dot11FCS].addr3).upper()
else:
bssid = str(pkt[Dot11].addr3).upper()
ssid = pkt[Dot11Elt].info.decode('UTF-8')
channel = int(ord(pkt[Dot11Elt:3].info))
strength = pkt[RadioTap].dBm_AntSignal
if bssid not in config['macs']:
return
if strength != '':
# print(type(strength), strength)
# Check if AP already has signal measurement
if bssid in apSignals:
# https://math.stackexchange.com/questions/106313/regular-average-calculated-accumulatively
apSignals[bssid]['count'] += 1
old_avg = apSignals[bssid]['avgStrength']
new_avg = ( (old_avg * (apSignals[bssid]['count']-1)) + strength ) / apSignals[bssid]['count']
new_avg = int(round(new_avg))
apSignals[bssid]['avgStrength'] = new_avg
if old_avg != new_avg:
print('Avg for', bssid, 'changed to: ', str(new_avg))
else:
apSignals[bssid] = {}
apSignals[bssid]['count'] = 0
apSignals[bssid]['avgStrength'] = 0
# Channel hopper
def channel_hopper():
while True:
try:
channel = random.randrange(1, 13)
os.system("iw dev %s set channel %d" % (interface, channel))
time.sleep(1)
except KeyboardInterrupt:
break
# Capture interrupt signal and cleanup before exiting
def signal_handler(signal, frame):
# p.terminate()
# p.join()
sys.exit(0)
if __name__ == "__main__":
with open('config.json') as f:
config = json.load(f)
with open('oui.csv') as csvfile:
macreader = csv.reader(csvfile, delimiter=',', quotechar='"')
i = 0
for row in macreader:
macVendors[row[1]] = row[2]
interface = args.adapter
if config['checks']['checkChannel']:
# Start the channel hopper
print("[*] Starting channel hopper process...")
p = Process(target=channel_hopper)
p.start()
else:
# Change adapter channel to expected channel
print("[*] Locking adapter to channel", str(config['channel']))
os.system("iw dev %s set channel %d" % (args.adapter, config['channel']))
# Capture CTRL-C
signal.signal(signal.SIGINT, signal_handler)
# Start the sniffer
if args.tune:
print("[*] Starting tuning sniff...\n")
sniff(iface=args.adapter, prn=tune, store=0)
else:
print("[*] Starting regular sniff...\n")
sniff(iface=args.adapter, prn=sniffAP, store=0)