-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
1057 lines (850 loc) · 32.8 KB
/
main.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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# License : GPLv2.0
# copyright (c) 2021 Dave Bailey
# Author: Dave Bailey (dbisu, @daveisu)
#
# Fork: Andreas Brett @andreasbrett
# - major refactoring
# - extended duckyscript language
# - dynamic locale switching
# - mouse and media key injection
# - externalized configuration to customize ducky easily
# - spawn new or connect to existing AP
# - responsive web ui
# - detailed debugging through serial monitor
# - display support
import binascii
import gc
import json
import os
import random
import re
import time
import ampule
import socketpool
import supervisor
import storage
import usb_hid
import wifi
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS as KeyboardLayout
from adafruit_hid.keycode import Keycode
from adafruit_hid.mouse import Mouse
from board import *
from digitalio import DigitalInOut, Direction, Pull
# import configuration
try:
from fd.config import config
except ImportError:
print(
"Using default configuration (config_default.py). To use your own settings copy as config.py and modify to taste."
)
from fd.config_default import config
from fd.consumerControlCommands import consumerControlCommands
from fd.duckyCommands import duckyCommands
from fd.htmlHeaders import headersAuth, headersCss, headersHtml, headersJs, headersJson
from fd.mouseButtons import mouseButtons
from fd.unquote import unquote
# -----------------------------------------------------------------------------------------------------
# Ducky Script Processing / HID Injection
# -----------------------------------------------------------------------------------------------------
def myprint(msg, store=True):
global duckyScriptResult
print(msg)
if store:
duckyScriptResult = duckyScriptResult + msg + "\r\n"
# check if provided pin is grounded
def isPinGrounded(pin):
checkPin = DigitalInOut(pin)
checkPin.switch_to_input(pull=Pull.UP)
return not checkPin.value
# sleep a short amount of time (and add to global delay counter)
def delay(seconds):
global delayCounter
time.sleep(seconds)
delayCounter += seconds
# split string into list tokens and make 1st token UPPERCASE
def splitToTokens(line):
tokens = line.split(" ")
tokens[0] = tokens[0].upper()
return tokens
# join list of tokens to a string
def joinTokens(tokens, start=0):
return " ".join(tokens[start:])
# pick a random delay time
# - returns current timestamp and determined delay time
def pickRandomDelay():
return time.monotonic(), (
random.randint(
config["mouseJiggler"]["delayMinimum"],
config["mouseJiggler"]["delayMaximum"],
)
)
# blink onboard LED
def blinkLED(duration=0.2, repeats=1):
for i in range(repeats):
led.value = True
delay(duration)
led.value = False
delay(duration)
# infinitely jiggle mouse ever so often
def mouseJigglerLoop():
# blink LED (startup indicator)
if config["mouseJiggler"]["LED"]["startupIndicator"]["enabled"]:
blinkLED(
config["mouseJiggler"]["LED"]["startupIndicator"]["duration"] / 1000,
config["mouseJiggler"]["LED"]["startupIndicator"]["repetitions"],
)
myprint("")
myprint("Running mouse jiggler")
myprint("--------------------------------")
myprint(" > movement =", config["mouseJiggler"]["movement"], "pixels")
myprint(" > delay min =", config["mouseJiggler"]["delayMinimum"], "seconds")
myprint(" > delay max =", config["mouseJiggler"]["delayMaximum"], "seconds")
myprint("--------------------------------")
timestamp, delay = pickRandomDelay()
myprint("waiting", delay, "seconds...")
while True:
if (time.monotonic() - timestamp) > delay:
# move mouse up+left then back down+right
myprint("jiggling mouse", config["mouseJiggler"]["movement"], "pixels")
mouse.move(
x=config["mouseJiggler"]["movement"] * -1,
y=config["mouseJiggler"]["movement"] * -1,
)
mouse.move(
x=config["mouseJiggler"]["movement"],
y=config["mouseJiggler"]["movement"],
)
# blink LED
if config["mouseJiggler"]["LED"]["enabled"]:
blinkLED(config["mouseJiggler"]["LED"]["duration"] / 1000)
# determine delay
timestamp, delay = pickRandomDelay()
myprint("waiting", delay, "seconds...")
# dynamically load the provided keyboard locale
def loadLocale(locale):
global KeyboardLayout, Keycode, kbd_layout
if locale.upper() == "US":
moduleKeyboardLayout = __import__(
"adafruit_hid.keyboard_layout_us", globals(), locals(), ["KeyboardLayoutUS"]
)
moduleKeycode = __import__(
"adafruit_hid.keycode", globals(), locals(), ["Keycode"]
)
KeyboardLayout = moduleKeyboardLayout.KeyboardLayoutUS
Keycode = moduleKeycode.Keycode
else:
moduleKeyboardLayout = __import__(
"keyboard_layout_win_" + locale.lower(),
globals(),
locals(),
["KeyboardLayout"],
)
moduleKeycode = __import__(
"keycode_win_" + locale.lower(), globals(), locals(), ["Keycode"]
)
KeyboardLayout = moduleKeyboardLayout.KeyboardLayout
Keycode = moduleKeycode.Keycode
kbd_layout = KeyboardLayout(kbd)
# convert to keycodes
def convertLineToKeycodes(line):
keycodes = []
# loop on each key - the filter removes empty values
for key in filter(None, line.split(" ")):
key = key.upper()
# find the keycode for the command in the list
command_keycode = duckyCommands.get(key, None)
if command_keycode is not None:
# if it exists in the list, use it
keycodes.append(command_keycode)
elif hasattr(Keycode, key):
# if it's in the Keycode module, use it (allows any valid keycode)
keycodes.append(getattr(Keycode, key))
else:
# if it's not a known key name, show the error for diagnosis
myprint(f"Unknown key: <{key}>")
myprint(f"{line} (keycodes = {keycodes})")
return keycodes
# build OR-ed list of mouse buttons to click/press/release
def convertLineToMouseButtons(line):
buttons = 0
for button in filter(None, line.split(" ")):
buttons |= mouseButtons.get(button, None)
return buttons
# perform a keyboard action (press keys provided in keycode list)
def performKeyboardAction(keycodes):
for keycode in keycodes:
kbd.press(keycode)
kbd.release_all()
# perform a mouse action (move, scroll, click, press, release)
def performMouseAction(line):
# split line into tokens (0 = command, 1-x = parameters)
tokens = splitToTokens(line)
# move mouse pointer
if tokens[0] == "MOVE":
moveX = int(tokens[1])
moveY = int(tokens[2])
myprint(f"MOUSE {line} (x={moveX}, y={moveY})")
mouse.move(x=moveX, y=moveY)
# scroll mouse wheel
elif tokens[0] == "WHEEL":
amount = int(joinTokens(tokens, 1))
myprint(f"MOUSE {line}")
mouse.move(wheel=amount)
# click and release one or more mouse buttons
elif tokens[0] == "CLICK":
mouse_buttons = convertLineToMouseButtons(joinTokens(tokens, 1))
myprint(f"MOUSE {line} (buttons = {mouse_buttons})")
mouse.click(mouse_buttons)
# press (and don't release) one or more mouse buttons
elif tokens[0] == "PRESS":
mouse_buttons = convertLineToMouseButtons(joinTokens(tokens, 1))
myprint(f"MOUSE {line} (buttons = {mouse_buttons})")
mouse.press(mouse_buttons)
# release one or more mouse buttons
elif tokens[0] == "RELEASE":
mouse_buttons = convertLineToMouseButtons(joinTokens(tokens, 1))
myprint(f"MOUSE {line} (buttons = {mouse_buttons})")
mouse.release(mouse_buttons)
# release all mouse buttons
elif tokens[0] == "RELEASEALL":
myprint(f"MOUSE RELEASEALL")
mouse.release_all()
# perform a consumer control action
def performConsumerControlAction(line):
# split line into tokens (0 = command, 1-x = parameters)
tokens = splitToTokens(line)
# send cc action
if tokens[0] == "SEND":
consumer_code = consumerControlCommands.get(joinTokens(tokens, 1))
myprint(f"CC {line} (code = {consumer_code})")
cc.send(consumer_code)
# press (and don't release) one or more mouse buttons
elif tokens[0] == "PRESS":
consumer_code = consumerControlCommands.get(joinTokens(tokens, 1))
myprint(f"CC {line} (code = {consumer_code})")
cc.press(consumer_code)
# release currently pressed cc (only one can be pressed at a time)
elif tokens[0] == "RELEASE":
myprint(f"CC RELEASE")
cc.release()
# type out provided string
# - randomly moves mouse in psychoMouse mode
# - stops time to type out the provided string
def typeString(s):
prefix = s[0:32] + ("..." if len(s) > 32 else "")
myprint(f"STRING {prefix}")
stopwatch = time.monotonic()
if psychoMouse:
rand = []
for i in range(0, config["psychoMouse"]["randomMovements"]):
rand.append(
random.randint(
-1 * config["psychoMouse"]["range"], config["psychoMouse"]["range"]
)
)
i = pos = 0
while pos < len(s):
kbd_layout.write(s[pos : (pos + config["psychoMouse"]["characters"])])
mouse.move(
x=rand[i % config["psychoMouse"]["randomMovements"]],
y=rand[(i + 1) % config["psychoMouse"]["randomMovements"]],
)
i += 1
pos += config["psychoMouse"]["characters"]
else:
kbd_layout.write(s)
stopwatch = time.monotonic() - stopwatch
if stopwatch > 1:
myprint(f" -> {len(s)} characters in {round(stopwatch, 2)} seconds")
else:
myprint(f" -> {len(s)} characters in {round(1000 * stopwatch, 2)} milliseconds")
# wait for presence of give Wifi Access Point
def waitForWifiAP(ssid, bssid=None, minimumRssi=None):
myprint("")
myprint(f'Waiting for Wifi AP "{ssid}"...')
myprint("--------------------------------------------------")
stopwatch = time.monotonic()
ssid = ssid.lower()
if bssid:
bssid = binascii.unhexlify(bssid.replace(":", "").replace("-", ""))
while True:
for ap in wifi.radio.start_scanning_networks():
if ap.ssid.lower() == ssid:
bSsid = True
else:
bSsid = False
if not bssid:
bBssid = True
elif ap.bssid == bssid:
bBssid = True
else:
bBssid = False
if not minimumRssi:
bRssi = True
elif ap.rssi > minimumRssi:
bRssi = True
else:
bRssi = False
if bSsid and bBssid and bRssi:
bssid = binascii.hexlify(ap.bssid).decode()
bssid = (
bssid[0:2]
+ ":"
+ bssid[2:4]
+ ":"
+ bssid[4:6]
+ ":"
+ bssid[6:8]
+ ":"
+ bssid[8:10]
+ ":"
+ bssid[10:12]
)
stopwatch = time.monotonic() - stopwatch
myprint(
f" --> Access Point present after {round(stopwatch, 2)} seconds"
)
myprint(f" * SSID: {ap.ssid}")
myprint(f" * BSSID: {bssid}")
myprint(f" * RSSI: {ap.rssi}")
wifi.radio.stop_scanning_networks()
return True
wifi.radio.stop_scanning_networks()
delay(2)
# process a line of duckyscript
def processLine(line):
global defaultDelay, psychoMouse, config
# split line into tokens (0 = command, 1-x = parameters)
tokens = splitToTokens(line)
# comment => ignore them
if tokens[0] == "REM":
pass
# wait X milliseconds
elif tokens[0] == "DELAY":
delay(float(tokens[1]) / 1000)
# output a string directly
elif tokens[0] == "STRING":
typeString(joinTokens(tokens, 1))
# output a string with carriage return directly
elif tokens[0] == "STRINGLN":
typeString(joinTokens(tokens, 1))
performKeyboardAction(convertLineToKeycodes("ENTER"))
# print out statement
elif tokens[0] == "PRINT":
myprint(f"[SCRIPT]: {joinTokens(tokens, 1)}")
# set default delay
elif tokens[0] == "DEFAULTDELAY" or tokens[0] == "DEFAULT_DELAY":
defaultDelay = int(tokens[1]) * 10
# control the LED
elif tokens[0] == "LED":
# toggle if there are not parameters
if len(tokens) == 1:
led.value = not led.value
# enable or disable LED otherwise
else:
led.value = True if tokens[1].upper() == "ON" else False
# control the LED
elif tokens[0] == "BLINK_LED":
if len(tokens) == 1:
blinkLED()
elif len(tokens) == 2:
blinkLED(duration=(float(tokens[1]) / 1000))
elif len(tokens) == 3:
blinkLED(duration=(float(tokens[1]) / 1000), repeats=float(tokens[2]))
# import another duckyscript payload
elif tokens[0] == "IMPORT":
processDuckyScript(tokens[1], initialCall=False)
# switch locale
elif tokens[0] == "LOCALE":
loadLocale(tokens[1])
# control mouse
elif tokens[0] == "MOUSE":
performMouseAction(joinTokens(tokens, 1))
# consumer control command
elif tokens[0] == "CC":
performConsumerControlAction(joinTokens(tokens, 1))
# control psychoMouse mode
elif tokens[0] == "PSYCHOMOUSE":
# enable psychoMouse mode
psychoMouse = True
if len(tokens) > 1:
# disable psychoMouse mode
if tokens[1] == "OFF":
psychoMouse = False
# set psychoMouse settings
else:
config["psychoMouse"]["characters"] = int(tokens[1])
if len(tokens) > 2:
config["psychoMouse"]["range"] = int(tokens[2])
elif tokens[0] == "WAITFORWIFI":
waitForWifiAP(joinTokens(tokens, 1))
elif tokens[0] == "WAITFORLED":
ledCodes = {
"CAPS_LOCK": Keyboard.LED_CAPS_LOCK,
"COMPOSE": Keyboard.LED_COMPOSE,
"NUM_LOCK": Keyboard.LED_NUM_LOCK,
"SCROLL_LOCK": Keyboard.LED_SCROLL_LOCK,
}
ledCode = ledCodes[tokens[1].upper()]
ledState = (tokens[2].upper() == "ON")
myprint(f"Waiting for {tokens[1].upper()} LED to be {tokens[2].lower()}...")
while True:
if kbd.led_on(ledCode) == ledState:
break
delay(0.1)
# no recognized special command => just run the converted keycodes
else:
performKeyboardAction(convertLineToKeycodes(line))
# process a duckyscript file or file content
def processDuckyScript(duckyScriptPath, duckyScript=None, initialCall=True):
global delayCounter
if initialCall:
delayCounter = 0
if duckyScriptPath:
displayTextLine(f"{duckyScriptPath.replace("fd/payloads/", "")}", clear=True)
elif duckyScript:
displayTextLine("<fileless payload>", clear=True)
displayTextLine("... running ...", 2)
if not initialCall:
myprint("")
stopwatch = time.monotonic()
if duckyScriptPath:
myprint(f"Running {duckyScriptPath}")
myprint("--------------------------------")
f = open(duckyScriptPath, "r", encoding="utf-8")
duckyScript = f.readlines()
f.close()
elif duckyScript:
myprint(f"Running fileless duckyscript")
myprint("--------------------------------")
duckyScriptPath = "<fileless duckyscript>"
duckyScript = duckyScript.split("\n")
previousLine = ""
for line in duckyScript:
# split line into tokens (0 = command, 1-x = parameters)
line = line.rstrip()
tokens = splitToTokens(line)
if tokens[0] == "REPEAT":
# repeat the last command
for i in range(int(tokens[1])):
processLine(previousLine)
delay(float(defaultDelay) / 1000)
else:
processLine(line)
previousLine = line
delay(float(defaultDelay) / 1000)
myprint("--------------------------------")
stopwatch = time.monotonic() - stopwatch
if stopwatch > 1:
myprint(
f" -> Finished {duckyScriptPath}. Processed {len(duckyScript)} lines in {round(stopwatch, 2)} seconds."
)
else:
myprint(
f" -> Finished {duckyScriptPath}. Processed {len(duckyScript)} lines in {round(1000 * stopwatch, 2)} milliseconds."
)
if initialCall:
displayTextLine(f"finished in {round(stopwatch, 2)}s", 2)
myprint(
f" -> All delays (commands and default delay) summed up to {delayCounter} seconds."
)
myprint("")
# -----------------------------------------------------------------------------------------------------
# Web Server
# -----------------------------------------------------------------------------------------------------
def encodeForTransport(s):
s = s.encode("utf-8")
s = binascii.b2a_base64(s)
return s
def decodeFromTransport(s):
s = unquote(s)
s = binascii.a2b_base64(s)
s = s.decode("utf-8")
return s
def debugRequest(request):
if config["webserver"]["debugRequests"]["enabled"]:
print("[REQUEST]", request.path)
if config["webserver"]["debugRequests"]["showHeaders"]:
print(" - headers:")
for key, value in request.headers.items():
print(f" - {key} = {value}")
if config["webserver"]["debugRequests"]["showParameters"]:
print(" - params:")
for key, value in request.params.items():
print(f" - {key} = {value}")
if config["webserver"]["debugRequests"]["showBody"]:
print(" - body: ", request.body)
def getPostParams(request):
params = {}
pairs = request.body.split("&")
for pair in pairs:
tmp = pair.split("=")
if len(tmp) == 2:
params[tmp[0]] = tmp[1]
return params
def readFile(filename):
try:
f = open(filename, "r")
d = f.read()
f.close()
return d
except Exception:
print("Error trying to read file: ", filename)
return None
def writeFile(filename, content):
try:
f = open(filename, "w", encoding="utf-8")
for line in content:
f.write(line)
f.close()
return True
except Exception:
print("Error trying to write file: ", filename)
return False
def htmlHeader():
return readFile("fd/web/header.html")
def htmlFooter():
return readFile("fd/web/footer.html")
def checkAuthorization(request):
# don't ask for credentials when disabled
if config["webserver"]["credentials"] is None:
return False
# 'authorization' header present?
if "authorization" in request.headers.keys():
# derive credentials from authorization header
credentialsBase64 = request.headers["authorization"].replace("Basic ", "")
credentials = binascii.a2b_base64(credentialsBase64).decode().split(":")
# compare username and password
if credentials[0] == config["webserver"]["credentials"]["username"] and credentials[1] == config["webserver"]["credentials"]["password"]:
return False
return (401, headersAuth, "")
@ampule.route("/")
def light_set(request):
debugRequest(request)
if requiresAuthorization := checkAuthorization(request):
return requiresAuthorization
html = htmlHeader()
html += readFile("fd/web/content.html")
html += htmlFooter()
return (200, headersHtml, html)
@ampule.route("/script.js")
def light_set(request):
debugRequest(request)
if requiresAuthorization := checkAuthorization(request):
return requiresAuthorization
content = readFile("fd/web/script.js")
return (200, headersJs, content)
@ampule.route("/style.css")
def light_set(request):
debugRequest(request)
if requiresAuthorization := checkAuthorization(request):
return requiresAuthorization
content = readFile("fd/web/style.css")
return (200, headersCss, content)
@ampule.route("/api/statistics")
def light_set(request):
debugRequest(request)
if requiresAuthorization := checkAuthorization(request):
return requiresAuthorization
# board
board_name = os.uname().machine
chip_name = os.uname().nodename
circuit_python_version = os.uname().release
circuit_python_version_full = os.uname().version
# storage
fs_stat = os.statvfs("/")
storage_total = fs_stat[0] * fs_stat[2] / 1024
storage_available = fs_stat[0] * fs_stat[3] / 1024
storage_used = storage_total - storage_available
# memory
mem_available = gc.mem_free() / 1024
mem_used = gc.mem_alloc() / 1024
mem_total = mem_available + mem_used
result = {
"board": {
"name": board_name,
"chip": chip_name,
"circuitpython_version": circuit_python_version,
"circuitpython_version_full": circuit_python_version_full,
},
"storage": {
"absolute": {
"unit_of_measurement": "KB",
"available": round(storage_available, 2),
"used": round(storage_used, 2),
"total": round(storage_total, 2),
},
"relative": {
"unit_of_measurement": "%",
"available": round(100 * storage_available / storage_total, 2),
"used": round(100 * storage_used / storage_total, 2),
},
},
"memory": {
"absolute": {
"unit_of_measurement": "KB",
"available": round(mem_available, 2),
"used": round(mem_used, 2),
"total": round(mem_total, 2),
},
"relative": {
"unit_of_measurement": "%",
"available": round(100 * mem_available / mem_total, 2),
"used": round(100 * mem_used / mem_total, 2),
},
},
"usb_connected": supervisor.runtime.usb_connected
}
return (200, headersJson, json.dumps(result))
@ampule.route("/api/fetchPayloads")
def light_set(request):
debugRequest(request)
if requiresAuthorization := checkAuthorization(request):
return requiresAuthorization
payloads = []
files = os.listdir("fd/payloads")
payloads = [f for f in files if f.endswith(".dd")]
return (200, headersJson, json.dumps(payloads))
@ampule.route("/api/loadPayload")
def light_set(request):
debugRequest(request)
if requiresAuthorization := checkAuthorization(request):
return requiresAuthorization
if "file" in request.params.keys():
file = request.params["file"]
payload = readFile(f"fd/payloads/{file}")
if payload:
return (200, headersJson, json.dumps({"payload": encodeForTransport(payload)}))
return (404, headersJson, json.dumps({"error": "file not found"}))
@ampule.route("/api/runPayload", method="POST")
def light_set(request):
global duckyScriptResult
debugRequest(request)
if requiresAuthorization := checkAuthorization(request):
return requiresAuthorization
params_post = getPostParams(request)
duckyScriptResult = ""
processDuckyScript(
duckyScriptPath=None, duckyScript=decodeFromTransport(params_post["payload"])
)
result = {
"result": encodeForTransport(duckyScriptResult),
"notification": "Script ran successfully. See script output below.",
}
return (200, headersJson, json.dumps(result))
@ampule.route("/api/savePayload", method="POST")
def light_set(request):
debugRequest(request)
if requiresAuthorization := checkAuthorization(request):
return requiresAuthorization
params_post = getPostParams(request)
filename = f"fd/payloads/{decodeFromTransport(params_post["filename"])}"
content = decodeFromTransport(params_post["payload"])
try:
# remount with write permissions
storage.remount("/", readonly=False)
except Exception:
result = {
"result": "error",
"notification": "USB drive is mounted on the host. Configure FeatherS2 Ducky to boot into stealth-mode.",
}
return (200, headersJson, json.dumps(result))
# write payload to file
if writeFile(filename, content):
result = {
"result": "success",
"notification": "Successfully written file.",
}
else:
result = {
"result": "error",
"notification": "Error writing file.",
}
# remount with read-only permissions
storage.remount("/", readonly=True)
return (200, headersJson, json.dumps(result))
@ampule.route("/api/deletePayload", method="POST")
def light_set(request):
return (200, headersJson, "")
@ampule.route("/api/checkUsbHid")
def light_set(request):
if supervisor.runtime.usb_connected:
kbd = Keyboard(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
cc = ConsumerControl(usb_hid.devices)
loadLocale(config["locale"])
return (200, headersJson, json.dumps({ "result": supervisor.runtime.usb_connected }))
# spawn or connect to Wifi Access Point
def runWebserver():
print("")
if config["webserver"]["hostname"]:
wifi.radio.enabled = False
wifi.radio.hostname = config["webserver"]["hostname"]
wifi.radio.enabled = True
if config["wifi"]["mode"] == "connect":
displayTextLine(f"Connecting to Wifi\n{config['wifi']['connectAP']['ssid']}", clear=True)
displayTextLine(f"...", 2)
print(f"Connecting to Wifi AP \"{config['wifi']['connectAP']['ssid']}\"")
if config["wifi"]["connectAP"]["macAddress"]:
macAddress = binascii.unhexlify(
config["wifi"]["connectAP"]["macAddress"]
.replace(":", "")
.replace("-", "")
)
wifi.radio.mac_address = macAddress
if config["wifi"]["connectAP"]["bssid"]:
bssid = binascii.unhexlify(
config["wifi"]["connectAP"]["bssid"].replace(":", "").replace("-", "")
)
wifi.radio.connect(
ssid=config["wifi"]["connectAP"]["ssid"],
password=config["wifi"]["connectAP"]["password"],
bssid=bssid,
)
else:
wifi.radio.connect(
ssid=config["wifi"]["connectAP"]["ssid"],
password=config["wifi"]["connectAP"]["password"],
)
print(" - Hostname: ", wifi.radio.hostname)
print(" - IP Address: ", wifi.radio.ipv4_address)
print(" - Subnet: ", wifi.radio.ipv4_subnet)
print(" - MAC Address:", ":".join("%02X" % _ for _ in wifi.radio.mac_address))
print(" - Gateway: ", wifi.radio.ipv4_gateway)
print(" - DNS: ", wifi.radio.ipv4_dns)
ipAddress = wifi.radio.ipv4_address
displayTextLine(f"{config['wifi']['connectAP']['ssid']}")
else:
displayTextLine(f"Spawning Wifi\n{config['wifi']['spawnAP']['ssid']}", clear=True)
displayTextLine(f"...", 2)
print(f"Spawning Wifi AP \"{config['wifi']['spawnAP']['ssid']}\"")
if config["wifi"]["spawnAP"]["macAddress"]:
macAddress = binascii.unhexlify(
config["wifi"]["spawnAP"]["macAddress"]
.replace(":", "")
.replace("-", "")
)
wifi.radio.mac_address_ap = macAddress
wifi.radio.start_ap(
ssid=config["wifi"]["spawnAP"]["ssid"],
password=config["wifi"]["spawnAP"]["password"],
channel=config["wifi"]["spawnAP"]["channel"],
)
print(" - Wifi Secret:", config["wifi"]["spawnAP"]["password"])
print(" - Hostname: ", wifi.radio.hostname)
print(" - IP Address: ", wifi.radio.ipv4_address_ap)
print(
" - MAC Address:", ":".join("%02X" % _ for _ in wifi.radio.mac_address_ap)
)
print(" - Subnet: ", wifi.radio.ipv4_subnet_ap)
print(" - Gateway: ", wifi.radio.ipv4_gateway_ap)
ipAddress = wifi.radio.ipv4_address_ap
displayTextLine(f"{config['wifi']['spawnAP']['ssid']}")
# run HTTP server and listen
print("")
if config["webserver"]["port"] == 80:
if config["wifi"]["mode"] == "connect":
displayTextLine(f"http://{wifi.radio.hostname}", 2)
else:
displayTextLine(f"http://{ipAddress}", 2)
print("Serving via HTTP at:")
print(f" - http://{wifi.radio.hostname}")
print(f" - http://{ipAddress}")
else:
if config["wifi"]["mode"] == "connect":
displayTextLine(f"http://{wifi.radio.hostname}:{config['webserver']['port']}", 2)
else:
displayTextLine(f"http://{ipAddress}:{config['webserver']['port']}", 2)
print("Serving via HTTP at:")
print(f" - http://{wifi.radio.hostname}:{config['webserver']['port']}")
print(f" - http://{ipAddress}:{config['webserver']['port']}")
pool = socketpool.SocketPool(wifi.radio)
socket = pool.socket()
socket.bind(["0.0.0.0", config["webserver"]["port"]])
socket.listen(1)
if config["webserver"]["credentials"] is None:
print(" - no credentials required")
else:
print(f" - Username: {config['webserver']['credentials']['username']}")
print(f" - Password: {config['webserver']['credentials']['password']}")
# webserver listen loop
while True:
ampule.listen(socket)
# -----------------------------------------------------------------------------------------------------
# RUNTIME
# -----------------------------------------------------------------------------------------------------
def importDisplay():
if config["display"]["enabled"]:
from fd.display import displayTextLine
return displayTextLine
else:
def _func(text, lineNumber=1, clear=False):
pass
return _func
# disable CircuitPython auto-restart feature
if not config["usbConnection"]["autoRestartOnTouch"]:
supervisor.disable_autoreload()
# dynamically import display lib (if enabled)
displayTextLine = importDisplay()
# set up LED
displayTextLine("Setting up LED...", clear=True)
led = DigitalInOut(LED)
led.direction = Direction.OUTPUT
# default settings
defaultDelay = config["defaultDelay"]
psychoMouse = False
delayCounter = 0
duckyScriptResult = ""