forked from yadox666/WiFi_CTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiFiCTF.py
executable file
·727 lines (661 loc) · 28.2 KB
/
WiFiCTF.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__version__ = '0.2'
import fcntl
import sys, os, time
import base64
from datetime import datetime
from random import randint
from platform import system
from threading import Thread, Lock
import logging, time, socket
from subprocess import Popen, PIPE
from gpiozero import LED,Button,Buzzer,OutputDevice
from signal import SIGINT,signal
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
# user defined variables
verbose = 1 ## verbosity level (0-3)
# interface = 'wlan1' ## Wi-Fi interface/s ['wlan1','wlan2']
interface = open('/tmp/wifi').readlines()[0].rstrip('\n') ## Wi-Fi interface/s ['wlan1','wlan2']
channel = 3 ## Channel to use by default
gpioled = 6 ## GPIO port number for Status led
gpiosend = 5 ## GPIO port number when sending packets
gpioflash = 24 ## GPIO port number for white led as camera flash
gpioarm = 8 ## GPIO port number to force exploding the bomb
gpiodisarm = 7 ## GPIO port number to disarm the bomb by button
gpioexplode = 9 ## GPIO port to connect explode mechanism for cartridge
gpioexplode2 = 10 ## GPIO port to connect explode mechanism for second cartridge
gpioext = 11 ## GPIO port number to external peripheral
gpiobuzzer = 18 ## GPIO port number for a buzzer
countdownhdmi=0 ## If 1 it will display a countdown full screen in HDMI connector
countdownlcd=1 ## If 1 it will display a countdown full screen
savecap=1 ## will save captured packets from attack
# system variables
ctftime=3600 ## Default CTF duration in seconds
apsecurity = 'OPEN' ## open, wep, wpa, wpa2
ssid = 'CTF!!!' ## default SSID to use
count = 100 ## Default number of packets to send
disarmpayload='DisarmITn0w!!!' ## Disarm the bomb sending various Probe Request packets with this SSID
src = RandMAC() ## source ip from packets
dst = 'ff:ff:ff:ff:ff:ff' ## Destination address for beacons and probes
broadcast = 'ff:ff:ff:ff:ff:ff' ## Destination address for beacons and probes
sc = randint(1, 4096)
payload = ''
frequency = ''
gpiobase = '/sys/class/gpio'
lock = Lock()
DN = open(os.devnull, 'w')
gpioarmnc = 1 ## Arm mechanism is normally open (0) or normally closed (1)
gpiodisarmnc = 1 ## Disarm mechanism Normally open (0) or normally closed (1)
payload_ie = 221 ## 802.11 Element ID to include payload
payload_preffix = 'CTF' ## When using element ID 221 the first 3 bytes are for the manuf OUI
closing = 0
imagedir='./images'
camera_on=0
intfmon=''
winner=''
ctfstart=int(time.time())
lcd = None
capfile='./ctf_sentpkts.cap' ## directory and file name to save captured packets
# 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:']
def stoptimers(reason=0):
global lcd
if countdownhdmi:
logging.debug("Stopping HDMI countdown timer!")
hdmi.terminate()
time.sleep(0.2)
hdmi = None
if countdownlcd:
logging.debug("Stopping LCD countdown timer!")
lcd.terminate()
time.sleep(0.3)
lcd = None
if reason == 1:
lcd = subprocess.Popen(['./lcdprint', 'run !!!!', '4'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
elif reason == 2:
lcd = subprocess.Popen(['./lcdprint', 'dISARmEd', '4'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
elif reason == 3:
lcd = subprocess.Popen(['./lcdprint', winner[-8], '4'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def PacketHandler(pkt):
global winner
sta = pkt.addr2.upper()
ssid = pkt.info
if ssid == disarmpayload:
ret = sled.is_lit
sled.blink(on_time=1,off_time=0.4,n=1)
logging.debug("Received Probe Request packet from station %s with payload: %s. Disarming bomb!" %(sta,ssid))
winner=sta
timer.disarm_bomb()
if ret: sled.on()
class Sniffer(Thread): # Scapy sniffer thread
def __init__(self):
Thread.__init__(self)
Thread.daemon = True
def run(self):
try:
if verbose: logging.debug("Start sniffing data with interface %s" % intfmon)
sniff(iface=intfmon, prn=PacketHandler, lfilter=lambda p:(Dot11ProbeReq in p), store=0)
except Exception as e:
logging.error("Cannot start sniffer thread with interface %s! %s" %(intfmon,e.message))
class CheckTimer(Thread):
def __init__(self):
Thread.__init__(self)
Thread.daemon = True
self.statusled = LED(gpioled)
self.arm = Button(gpioarm)
self.disarm = Button(gpiodisarm)
self.buzzer= Buzzer(gpiobuzzer)
def run(self):
if gpioarmnc:
self.arm.when_released = self.arm_bomb
else:
self.arm.when_pressed = self.arm_bomb
if gpiodisarmnc:
self.disarm.when_released = self.disarm_bomb
else:
self.disarm.when_pressed = self.disarm_bomb
self.timercheck()
def timercheck(self):
ledexec=1
ledinterval=15 ## percentage of ctf accelerate led light freq
ledfreq=1 ## time to flash led on
i=0
while not closing:
elapsedtime = int(time.time())-ctfstart
percent=float(elapsedtime)/ctftime*100
if timeleft() < 1:
logging.debug("CTF finished! Time left: %s" %timeformat(timeleft()))
self.arm_bomb()
closeall(0,0)
if percent >= ledinterval*i:
if ledexec:
ledfreq = 1-(float(percent)/100)+0.1
self.statusled.off()
time.sleep(0.4)
self.statusled.blink(ledfreq,ledfreq-0.1)
if i == 100/ledinterval: ## Last round
self.buzzer.source = self.statusled.values
i += 1
ledexec=1
time.sleep(0.4)
def disarm_bomb(self):
self.statusled.on()
self.buzzer.on()
stoptimers(2)
takephoto()
time.sleep(3)
self.statusled.off()
self.buzzer.off()
takephoto()
logging.debug("Disarm button pressed! Disarming bomb now!")
self.statusled.off()
stoptimers(3)
closeall(0,0)
def arm_bomb(self):
explode = OutputDevice(gpioexplode, active_high=False)
explode2 = OutputDevice(gpioexplode2, active_high=False)
takephoto()
logging.debug("ARM button pressed! Arming bomb now!")
self.statusled.blink(on_time=0.05, off_time=0.05)
self.buzzer.source = self.statusled.values
stoptimers(1)
time.sleep(2.5)
explode.on()
time.sleep(0.8)
explode2.on()
takephoto()
time.sleep(2)
takephoto()
explode.off()
time.sleep(1)
explode2.off()
takephoto()
closeall(0,0)
def closeall(signal,frame):
global closing
closing = 1
print ''
logging.debug('Ending execution!')
sled.off()
exit()
def takephoto():
if camera_on:
flash.on()
date = datetime.now().isoformat()
camera.capture('%s/%s.jpg' %(imagedir,date))
time.sleep(0.2)
flash.off()
def oscheck():
osversion = system()
if verbose:
logging.debug("Operating System: %s" % osversion)
if osversion != 'Linux':
logging.debug("This script only works on Linux OS! Exitting!")
exit(1)
def initmon(intfparent):
global intfmon, ignore
logging.debug("Wi-Fi nterface to use: %s" %intfparent)
if not os.path.isdir("/sys/class/net/" + interface):
logging.debug("WiFi parent interface %s does not exist! Cannot continue!" % interface)
exit(1)
else:
intfmon = 'mon' + intfparent[-1]
if os.path.isdir("/sys/class/net/" + intfmon):
logging.debug("WiFi interface %s exists! Deleting it!" % (intfmon))
try:
# create monitor interface using iw
os.system("iw dev %s del" % intfmon)
time.sleep(0.5)
except OSError as oserr:
logging.debug("Could not delete monitor interface %s. %s" % (intfmon, oserr.message))
os.kill(os.getpid(), SIGINT)
sys.exit(1)
try:
# create monitor interface using iw
os.system("ifconfig %s down" % interface)
time.sleep(0.3)
os.system("iwconfig %s mode monitor" % interface)
time.sleep(0.3)
os.system("iw dev %s interface add %s type monitor" % (interface, intfmon))
time.sleep(0.3)
os.system("ifconfig %s up" % intfmon)
if verbose:
logging.debug("Creating monitor VAP %s for parent %s..." % (intfmon, interface))
except OSError as oserr:
logging.debug("Could not create monitor %s. %s" % (intfmon, oserr.message))
os.kill(os.getpid(), SIGINT)
sys.exit(1)
# Get actual MAC addresses
macaddr1 = GetMAC(interface).upper()
ignore.append(macaddr1)
if verbose: logging.debug("Actual %s MAC Address: %s" % (interface, macaddr1))
macaddr = GetMAC(intfmon).upper()
if macaddr1 != macaddr:
ignore.append(macaddr);
if verbose:
logging.debug("Actual %s MAC Address: %s" % (intfmon, macaddr))
def GetMAC(iface):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', iface[:15]))
mac = ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]
return mac
def calc_freq(channel):
global frequency
if channel in range(1, 14):
if channel == 14:
frequency = "2484"
else:
frequency = str(2407 + (channel * 5))
return frequency
else:
return "n/a"
class Dot11EltRates(Packet):
name = "802.11 Rates Information Element"
# Our Test STA supports the rates 6, 9, 12, 18, 24, 36, 48 and 54 Mbps
supported_rates = [0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c]
fields_desc = [ByteField("ID", 1), ByteField("len", len(supported_rates))]
for index, rate in enumerate(supported_rates):
fields_desc.append(ByteField("supported_rate{0}".format(index + 1), rate))
def next_sc():
# type: () -> object
global sc
sc = (sc + 1) % 4096
temp = sc
return temp * 16 # Fragment number -> right 4 bits
def get_radiotap_header():
global channel
radiotap_packet = RadioTap(len=18, present='Flags+Rate+Channel+dBm_AntSignal+Antenna',
notdecoded='\x00\x6c' + frequency + '\xc0\x00\xc0\x01\x00\x00')
return radiotap_packet
class Scapy80211():
def __init__(self, intfparent='wlan1', intfmon='mon1'):
self.intfparent = intfparent
self.intfmon = intfmon
conf.iface = self.intfmon
self.savecap = savecap
def beacon(self, src, count, ssid, apsecurity, payload):
if apsecurity.upper() == 'WEP':
beacon = Dot11Beacon(cap='ESS+privacy')
rsn = ''
elif apsecurity.upper() == 'WPA':
beacon = Dot11Beacon(cap='ESS+privacy')
rsn = Dot11Elt(ID='RSNinfo', info=(
'\x01\x00' # RSN Version 1
'\x00\x0f\xac\x02' # Group Cipher Suite : 00-0f-ac TKIP
'\x02\x00' # 2 Pairwise Cipher Suites (next two lines)
'\x00\x0f\xac\x04' # AES Cipher
'\x00\x0f\xac\x02' # TKIP Cipher
'\x01\x00' # 1 Authentication Key Managment Suite (line below)
'\x00\x0f\xac\x02' # Pre-Shared Key
'\x00\x00')) # RSN Capabilities (no extra capabilities)
elif apsecurity.upper == 'WPA2':
beacon = Dot11Beacon(cap='ESS+privacy')
rsn = Dot11Elt(ID='RSNinfo', info=(
'\x01\x00' # RSN Version 1
'\x00\x0f\xac\x02' # Group Cipher Suite : 00-0f-ac TKIP
'\x02\x00' # 2 Pairwise Cipher Suites (next two lines)
'\x00\x0f\xac\x04' # AES Cipher
'\x00\x0f\xac\x02' # TKIP Cipher
'\x01\x00' # 1 Authentication Key Managment Suite (line below)
'\x00\x0f\xac\x02' # Pre-Shared Key
'\x00\x00')) # RSN Capabilities (no extra capabilities)
else:
rsn = ''
beacon = Dot11Beacon(cap='ESS')
essid = Dot11Elt(ID='SSID', info=ssid, len=len(ssid))
dsset = Dot11Elt(ID='DSset', info='\x01')
tim = Dot11Elt(ID='TIM', info='\x00\x01\x00\x00')
eltpayload = Dot11Elt(ID=payload_ie,info=payload_preffix+payload) ## vendor WPS
pkt = RadioTap() / Dot11(type=0, subtype=8, addr1=broadcast.upper(), addr2=src, addr3=src, SC=next_sc()) / beacon / essid / rsn / Dot11EltRates() / eltpayload / dsset / tim
if verbose >= 3: pkt.show()
if verbose >= 2: print '[*] 802.11 Beacon: SSID=[%s], count=%d' % (ssid, count)
try:
sled.on()
sendp(pkt, iface=self.intfmon, count=count, inter=0.100, verbose=0)
if self.savecap:
try:
writer = PcapWriter(capfile, append=True)
writer.write(pkt)
writer.close()
except:
self.savecap=0
sled.off()
except Exception as e:
logging.error('Cannot send packets. %s' %e.message)
def probereq(self, src, count, ssid, dst, payload):
param = Dot11ProbeReq()
essid = Dot11Elt(ID='SSID', info=ssid, len=len(ssid))
dsset = Dot11Elt(ID='DSset', info=chr(channel))
eltpayload = Dot11Elt(ID=payload_ie,len=len(payload),info=payload_preffix+payload) ## vendorWPS
pkt = RadioTap() / Dot11(type=0, subtype=4, addr1=dst, addr2=src, addr3=dst, SC=next_sc()) / param / essid / Dot11EltRates() / eltpayload / dsset
if verbose >= 3: pkt.show()
if verbose >= 2: print '[*] 802.11 Probe Request: SSID=[%s], count=%d' % (ssid, count)
try:
sled.on()
sendp(pkt, count=count, inter=0.1, verbose=0)
if self.savecap:
try:
writer = PcapWriter(capfile, append=True)
writer.write(pkt)
writer.close()
except:
self.savecap=0
sled.off()
except Exception as e:
logging.error('Cannot send packets. %s' %e.message)
def proberesp(self, src, count, ssid, dst, payload):
param = Dot11ProbeResp(beacon_interval=0x0064, cap=0x2104)
essid = Dot11Elt(ID='SSID', info=ssid, len=len(ssid))
dsset = Dot11Elt(ID='DSset', info=chr(channel))
eltpayload = Dot11Elt(ID=payload_ie,len=len(payload),info=payload_preffix+payload) ## vendorWPS
pkt = RadioTap() / Dot11(subtype=5, addr1=src, addr2=dst, addr3=dst, SC=next_sc()) / param / essid / Dot11EltRates() / eltpayload / dsset
# If we are an RSN network, add RSN data to response
if security[0:3] == 'WPA':
probe_response_packet[Dot11ProbeResp].cap = 0x3101
rsn_info = Dot11Elt(ID='RSNinfo', info=RSN)
probe_response_packet = probe_response_packet / rsn_info
try:
sled.on()
sendp(pkt, count=count, inter=0.1, verbose=0)
if self.savecap:
try:
writer = PcapWriter(capfile, append=True)
writer.write(pkt)
writer.close()
except:
self.savecap=0
sled.off()
except Exception as e:
logging.error('Cannot send packets. %s' %e.message)
def authreq(self, src, dst, count, ssid, apsecurity, payload):
# authentication with open system
param = Dot11Auth(algo=0, seqnum=1, status=0)
essid = Dot11Elt(ID='SSID', info=ssid, len=len(ssid))
eltpayload = Dot11Elt(ID=payload_ie,len=len(payload),info=payload_preffix+payload) ## vendorWPS
pkt = RadioTap() / Dot11(subtype=0xb, addr1=dst, addr2=src, addr3=dst, SC=next_sc()) / param / essid / eltpayload
sled.on()
res = srp(pkt, iface=self.intfmon, count=count, timeout=2)
if self.savecap:
try:
writer = PcapWriter(capfile, append=True)
writer.write(pkt)
writer.close()
except:
self.savecap=0
sled.off()
if res:
res.summary()
logging.debug("Got answer from " + res.addr2)
else:
logging.debug("Got no answer from " + dst)
def assocreq(self, src, dst, count, ssid, apsecurity, payload):
# association request
param = Dot11AssoReq()
essid = Dot11Elt(ID='SSID', info=ssid, len=len(ssid))
eltpayload = Dot11Elt(ID=payload_ie,len=len(payload),info=payload_preffix+payload) ## vendorWPS
pkt = RadioTap() / Dot11(type=0, subtype=0, addr1=dst, addr2=src, addr3=dst,
SC=next_sc()) / param / essid / Dot11EltRates() / eltpayload
sled.on()
res = srp(pkt, iface=self.intfmon, count=count, timeout=2)
if self.savecap:
try:
writer = PcapWriter(capfile, append=True)
writer.write(pkt)
writer.close()
except:
self.savecap=0
sled.off()
if res:
logging.debug("Got answer from " + res.addr2)
else:
logging.debug("Got no answer from " + ssid)
def deauth(self, src, dst, count, ssid, payload):
# Deauthentication request
eltpayload = Dot11Elt(ID=payload_ie,len=len(payload),info=payload_preffix+payload) ## vendorWPS
param = Dot11Deauth() / eltpayload
pkt = RadioTap() / Dot11(addr1=src, addr2=dst, addr3=dst, SC=next_sc()) / param ## AP to STA deauth
pkt2 = RadioTap() / Dot11(addr1=dst, addr2=src, addr3=dst, SC=next_sc()) / param ## STA to AP deauth
res = srp1(pkt, retry=5, iface=self.intfmon, timeout=2)
if res:
logging.debug("Got answer from " + res.addr2)
else:
logging.debug("Got no answer from Station: " + str(src))
sled.on()
res = srp(pkt2, count=count, iface=self.intfmon, timeout=2)
if self.savecap:
try:
writer = PcapWriter(capfile, append=True)
writer.write(pkt)
writer.close()
except:
self.savecap=0
sled.off()
if res:
logging.debug("Got answer from " + res.addr2)
else:
logging.debug("Got no answer from AP: " + dst)
# Dot11AssoResp : 802.11 Association Response
# Dot11Disas : 802.11 Disassociation
# Dot11ReassoReq : 802.11 Reassociation Request
# Dot11ReassoResp : 802.11 Reassociation Response
# Data packets...
def setchannel(self, chan):
global channel,frequency
channel = int(chan)
frequency = calc_freq(channel)
try:
proc = Popen(['iw', 'dev', self.intfmon, 'set', 'channel', str(channel)], stdout=DN, stderr=PIPE)
if verbose >= 2: logging.debug("Setting %s to channel: %s (%s MHz)" % (self.intfmon, channel, frequency))
except OSError as e:
logging.debug('Could not execute iw!')
os.kill(os.getpid(), SIGINT)
return False
for line in proc.communicate()[1].split('\n'):
if len(line) > 2: # iw dev shouldnt display output unless there's an error
print line
logging.debug("Error setting channel %s for %s" % (channel, self.intfmon))
return False
def executecommand(command, value):
global ctftime, ctfstart, count, src, dst, ssid, channel, apsecurity, payload, closing, disarmpayload, lcd
try:
if closing:
return
if command == "slp":
logging.debug('[%s] Sleeping %s seconds...' %(timeformat(timeleft()),value))
for i in range(int(value)):time.sleep(1)
sys.stdout.write("\033[F") # Cursor up one line
if command == "wai":
wait=ctfstart+(int(value)*60)-int(time.time())
if wait < 0:
wait=1
logging.debug('[%s] Sleeping %d seconds until %s minutes from CTF Start...' %(timeformat(timeleft()),wait,value))
for i in range(wait): time.sleep(1)
sys.stdout.write("\033[F") # Cursor up one line
elif command == "ctf":
ctftime = int(value)
if verbose >= 1: logging.debug("Fixing CTF duration to: %s" %(timeformat(ctftime)))
# Start timers
logging.debug("Starting CTF execution at: %s" % datetime.now())
ctfstart=int(time.time())
# Start countdown screen and LCD
if countdownhdmi:
logging.debug("Starting HDMI countdown timer: %s" %timeformat(timeleft()))
if hdmi:
subprocess.Pkill(hdmi.pid)
time.sleep(1)
hdmi = subprocess.Popen(['python', './countdown.py', str(ctftime)], shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
if countdownlcd:
logging.debug("Starting LCD countdown timer: %s" %timeformat(timeleft()))
if lcd:
subprocess.Pkill(hdmi.pid)
time.sleep(1)
lcd = subprocess.Popen(['./countdown', str(ctftime)], shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
elif command == "cnt":
count = int(value)
if verbose >= 1: logging.debug('[%s] Changing number of packets to send to:%s' %(timeformat(timeleft()),str(count)))
elif command == "sec":
apsecurity = value
if verbose >= 1: logging.debug('[%s] Changing ap security to:%s' %(timeformat(timeleft()),apsecurity))
elif command == "snf":
disarmpayload = value
if verbose >= 1: logging.debug('[%s] Changing ProbeReq disarm payload to:%s' %(timeformat(timeleft()),disarmpayload))
elif command == "pay":
if value[:3] == 'b64':
payload_plain = value[4:]
payload = base64.b64encode(bytes(payload_plain))
if verbose >= 1: logging.debug('[%s] Changing payload to %s base64(%s)' %(timeformat(timeleft()),payload,payload_plain))
else:
payload = value[4:]
if verbose >= 1: logging.debug('[%s] Changing plain payload to:%s' %(timeformat(timeleft()),payload))
elif command == "gpo":
gpioval = value.split('-')[0]
gpiotime = value.split('-')[1]
if verbose >= 1: logging.debug('[%s] Setting external GPIO to: %s - %s' % (timeformat(timeleft()),gpioval, gpiotime))
gpioext.on()
for i in range(3): time.sleep(1)
gpioext.off()
elif command == "chn":
channel = int(value)
if verbose >= 1: logging.debug('[%s] Setting %s to channel: %s (%s MHz)' % (timeformat(timeleft()),intfmon, str(channel), frequency))
sdot11.setchannel(channel)
elif command == "src":
src = value.upper()
if verbose >= 1: logging.debug('[%s] Setting source MAC to: %s' %(timeformat(timeleft()),str(src).upper()))
elif command == "dst":
dst = value.upper()
if verbose >= 1: logging.debug('[%s] Settingg dst MAC to:%s' %(timeformat(timeleft()),dst))
elif command == "bcn":
ssid = value
logging.debug('[%s] Sending %d beacons with SSID:%s (%s) CHAN:%d (%s MHz) BSSID:%s' %(timeformat(timeleft()), count, ssid, apsecurity, channel, frequency, str(src).upper()))
sdot11.beacon(src, count, ssid, apsecurity,payload)
elif command == "prb":
ssid = value
logging.debug('[%s] Sending %d probe requests to SSID:%s CHAN:%d (%s MHz) from MAC:%s' %(timeformat(timeleft()), count, ssid, channel, frequency, dst.upper()))
sdot11.probereq(src, count, ssid, dst, payload)
elif command == "prr":
ssid = value
logging.debug('[%s] Sending %d probe responses to MAC:%s CHAN:%d (%s MHz) from BSSID:%s' %(timeformat(timeleft()), count, dst.upper(), channel, frequency, str(src).upper()))
sdot11.proberesp(src, count, ssid, dst, payload)
elif command == "ass":
ssid = value
logging.debug('[%s] Sending %d association requests to BSSID:%s (%s) CHAN:%d (%s MHz) from MAC:%s' %(timeformat(timeleft()), count, dst.upper(), apsecurity, channel, frequency, str(src).upper()))
sdot11.assocreq(src, dst, count, ssid, apsecurity, payload)
elif command == "aut":
ssid = value
logging.debug('[%s] Sending %d authentication requests to BSSID:%s (%s) CHAN:%d (%s MHz) from MAC:%s' %(timeformat(timeleft()), count, dst.upper(), apsecurity, channel, frequency, str(src).upper()))
sdot11.authreq(src, dst, count, ssid, apsecurity, payload)
elif command == "dea":
ssid = value
logging.debug('[%s] Sending %d deauth requests to BSSID:%s (%s) CHAN:%d (%s MHz) from MAC:%s' %(timeformat(timeleft()), count, dst.upper(), apsecurity, channel, frequency, str(src).upper()))
sdot11.deauth(src, dst, count, ssid, payload)
elif command == "ext":
logging.debug("[%s] Requested to end execution at:%s" %(timeformat(timeleft()),datetime.now()))
closeall(0,0)
else:
if verbose >= 2: logging.error('[%s] Wrong command: %s(%s)' %(timeformat(timeleft()),command, value))
return
except Exception as e:
logging.error('Cannot parse command. %s' %e.message)
def timeleft():
elapsedtime = int(time.time())-ctfstart
timeleft = ctftime - elapsedtime
return timeleft
def timeformat(seconds):
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
return "%d:%02d:%02d" % (h, m, s)
# main routine
if __name__ == "__main__":
# Print init banner
print "\n=========================================================="
print " ▌ ▌▗ ▛▀▘▗ ▞▀▖ ▐ ▛▀▘ "
print " ▌▖▌▄ ▙▄ ▄ ▌ ▜▀ ▙▄ "
print " ▙▚▌▐ ▄ ▌ ▐ ▌ ▖apture ▐ ▖he ▌ lag"
print " ▘ ▘▀ ▘ ▀ ▝▀ ▀ ▘ "
print " Wi-Fi (802.11) CTF GENERATOR SCRIPTING LANGUAGE"
print "==========================================================\n"
# Check if OS is linux:
oscheck()
# Check for root privileges
if os.geteuid() != 0:
logging.debug("You need to be root to run this script!")
exit()
else:
if verbose: logging.debug("You are running this script as root!")
# Check if monitor device exists
initmon(interface)
# Interrupt handler to exit
signal(SIGINT, closeall)
# Start injection class
sdot11 = Scapy80211(interface,intfmon)
sdot11.setchannel(channel)
# Start sniffer
Sniffer().start()
# Start camera
if camera_on:
try:
from picamera import PiCamera
camera = PiCamera()
except Exception as e:
logging.error("Pi Camera error! %s" %e.message)
camera_on=0
if not os.path.exists(imagedir):
try:
os.makedirs(imagedir)
except OSError as e:
logging.error("Cannot create directory: images")
imagedir = "./"
# Start GPIOs
sled = LED(gpiosend)
flash = LED(gpioflash)
gpioext = LED(gpioext)
timer = CheckTimer()
timer.start()
# parse file: ctf.conf
fpattern=''
try:
ctfconf = open('ctf.conf', 'r')
for line in ctfconf.readlines():
if not line.strip() or line[0:1] == "#":
continue
else:
fpattern = fpattern + line.replace("\n", ",").strip()
except IOError:
logging.error('Cannot open ctf.conf file, exiting!')
exit()
lpattern = fpattern.split(',')
index1 = 0
firstpass=1
lpattern1 = []
try:
while index1 < len(lpattern) and not closing:
command = lpattern[index1][0:3]
value = lpattern[index1][3:].translate(None, '()')
if command == "rpt" or index1 == len(lpattern) - 1:
if firstpass:
firstpass=0
else:
if verbose >= 1: logging.debug('repeating loop %s times...' % value)
indice = int(value)
loop = True
counter = 1
while loop and not closing:
index = 0
while index < len(lpattern1):
command = lpattern1[index][0:3]
value = lpattern1[index][3:].translate(None, '()')
executecommand(command, value)
index += 1
if counter == indice:
lpattern1 = []
loop = False
if indice == 0: loop = True
counter += 1
else:
lpattern1.append(command + '(' + value + ')')
index1 += 1
except KeyboardInterrupt:
closeall(0,0)