mirror of
https://github.com/dbisu/pico-ducky.git
synced 2026-01-21 17:26:00 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca88c6c159 | ||
|
|
ab753989d5 | ||
|
|
522e640c26 | ||
|
|
e61d232b46 | ||
|
|
6f9cbc3d80 | ||
|
|
fda4617e33 | ||
|
|
90a76bcada | ||
|
|
8b9822151f | ||
|
|
2dea346723 |
1
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
1
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
@@ -243,8 +243,8 @@ keycode_win_de.mpy
|
|||||||
|
|
||||||
### Installation Tool
|
### Installation Tool
|
||||||
|
|
||||||
[raspberrydeveloper](https://github.com/raspberrydeveloper) Created a tool to convert a blank RPi Pico to a ducky.
|
[ryo-yamada](https://github.com/ryo-yamada) Created a tool to convert a blank RPi Pico to a ducky.
|
||||||
You can find the tool [here](https://github.com/raspberrydeveloper/pyducky)
|
You can find the tool [here](https://github.com/ryo-yamada/PicoDuckyBuilder)
|
||||||
|
|
||||||
### Docs
|
### Docs
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ languages = [ "MAC_FR",
|
|||||||
|
|
||||||
supported_boards = ["raspberry_pi_pico",
|
supported_boards = ["raspberry_pi_pico",
|
||||||
"raspberry_pi_pico_w",
|
"raspberry_pi_pico_w",
|
||||||
"raspberry_pi_pico2"]
|
"raspberry_pi_pico2",
|
||||||
|
"raspberry_pi_pico2_w"]
|
||||||
|
|
||||||
files_to_bundle = ["boot.py",
|
files_to_bundle = ["boot.py",
|
||||||
"code.py",
|
"code.py",
|
||||||
@@ -113,7 +114,7 @@ def main(argv):
|
|||||||
replacements = {
|
replacements = {
|
||||||
"#from keyboard_layout_win_LANG": "from keyboard_layout_"+dest_dir.lower(),
|
"#from keyboard_layout_win_LANG": "from keyboard_layout_"+dest_dir.lower(),
|
||||||
"#from keycode_win_LANG": "from keycode_"+dest_dir.lower(),
|
"#from keycode_win_LANG": "from keycode_"+dest_dir.lower(),
|
||||||
"from adafruit_hid.keyboard": "#from adafruit_hid.keyboard",
|
"from adafruit_hid.keyboard_": "#from adafruit_hid.keyboard_",
|
||||||
"from adafruit_hid.keycode": "#from adafruit_hid.keycode"
|
"from adafruit_hid.keycode": "#from adafruit_hid.keycode"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
388
duckyinpython.py
388
duckyinpython.py
@@ -1,9 +1,14 @@
|
|||||||
# License : GPLv2.0
|
# License : GPLv2.0
|
||||||
# copyright (c) 2023 Dave Bailey
|
# copyright (c) 2023 Dave Bailey
|
||||||
# Author: Dave Bailey (dbisu, @daveisu)
|
# Author: Dave Bailey (dbisu, @daveisu)
|
||||||
|
#
|
||||||
|
# TODO: ADD support for the following:
|
||||||
|
# Add jitter
|
||||||
|
# Add LED functionality
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
import random
|
||||||
import digitalio
|
import digitalio
|
||||||
from digitalio import DigitalInOut, Pull
|
from digitalio import DigitalInOut, Pull
|
||||||
from adafruit_debouncer import Debouncer
|
from adafruit_debouncer import Debouncer
|
||||||
@@ -13,6 +18,8 @@ import pwmio
|
|||||||
import asyncio
|
import asyncio
|
||||||
import usb_hid
|
import usb_hid
|
||||||
from adafruit_hid.keyboard import Keyboard
|
from adafruit_hid.keyboard import Keyboard
|
||||||
|
from adafruit_hid.consumer_control import ConsumerControl
|
||||||
|
from adafruit_hid.consumer_control_code import ConsumerControlCode
|
||||||
|
|
||||||
# comment out these lines for non_US keyboards
|
# comment out these lines for non_US keyboards
|
||||||
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS as KeyboardLayout
|
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS as KeyboardLayout
|
||||||
@@ -20,13 +27,22 @@ from adafruit_hid.keycode import Keycode
|
|||||||
|
|
||||||
# uncomment these lines for non_US keyboards
|
# uncomment these lines for non_US keyboards
|
||||||
# replace LANG with appropriate language
|
# replace LANG with appropriate language
|
||||||
#from keyboard_layout_win_LANG import KeyboardLayout
|
#from keyboard_layout_win_LANG import KeyboardLayout as KeyboardLayout
|
||||||
#from keycode_win_LANG import Keycode
|
#from keycode_win_LANG import Keycode
|
||||||
|
|
||||||
duckyCommands = {
|
def _capsOn():
|
||||||
'WINDOWS': Keycode.WINDOWS, 'GUI': Keycode.GUI,
|
return kbd.led_on(Keyboard.LED_CAPS_LOCK)
|
||||||
'APP': Keycode.APPLICATION, 'MENU': Keycode.APPLICATION, 'SHIFT': Keycode.SHIFT,
|
|
||||||
'ALT': Keycode.ALT, 'CONTROL': Keycode.CONTROL, 'CTRL': Keycode.CONTROL,
|
def _numOn():
|
||||||
|
return kbd.led_on(Keyboard.LED_NUM_LOCK)
|
||||||
|
|
||||||
|
def _scrollOn():
|
||||||
|
return kbd.led_on(Keyboard.LED_SCROLL_LOCK)
|
||||||
|
|
||||||
|
duckyKeys = {
|
||||||
|
'WINDOWS': Keycode.GUI, 'RWINDOWS': Keycode.RIGHT_GUI, 'GUI': Keycode.GUI, 'RGUI': Keycode.RIGHT_GUI, 'COMMAND': Keycode.GUI, 'RCOMMAND': Keycode.RIGHT_GUI,
|
||||||
|
'APP': Keycode.APPLICATION, 'MENU': Keycode.APPLICATION, 'SHIFT': Keycode.SHIFT, 'RSHIFT': Keycode.RIGHT_SHIFT,
|
||||||
|
'ALT': Keycode.ALT, 'RALT': Keycode.RIGHT_ALT, 'OPTION': Keycode.ALT, 'ROPTION': Keycode.RIGHT_ALT, 'CONTROL': Keycode.CONTROL, 'CTRL': Keycode.CONTROL, 'RCTRL': Keycode.RIGHT_CONTROL,
|
||||||
'DOWNARROW': Keycode.DOWN_ARROW, 'DOWN': Keycode.DOWN_ARROW, 'LEFTARROW': Keycode.LEFT_ARROW,
|
'DOWNARROW': Keycode.DOWN_ARROW, 'DOWN': Keycode.DOWN_ARROW, 'LEFTARROW': Keycode.LEFT_ARROW,
|
||||||
'LEFT': Keycode.LEFT_ARROW, 'RIGHTARROW': Keycode.RIGHT_ARROW, 'RIGHT': Keycode.RIGHT_ARROW,
|
'LEFT': Keycode.LEFT_ARROW, 'RIGHTARROW': Keycode.RIGHT_ARROW, 'RIGHT': Keycode.RIGHT_ARROW,
|
||||||
'UPARROW': Keycode.UP_ARROW, 'UP': Keycode.UP_ARROW, 'BREAK': Keycode.PAUSE,
|
'UPARROW': Keycode.UP_ARROW, 'UP': Keycode.UP_ARROW, 'BREAK': Keycode.PAUSE,
|
||||||
@@ -44,54 +60,243 @@ duckyCommands = {
|
|||||||
'Z': Keycode.Z, 'F1': Keycode.F1, 'F2': Keycode.F2, 'F3': Keycode.F3,
|
'Z': Keycode.Z, 'F1': Keycode.F1, 'F2': Keycode.F2, 'F3': Keycode.F3,
|
||||||
'F4': Keycode.F4, 'F5': Keycode.F5, 'F6': Keycode.F6, 'F7': Keycode.F7,
|
'F4': Keycode.F4, 'F5': Keycode.F5, 'F6': Keycode.F6, 'F7': Keycode.F7,
|
||||||
'F8': Keycode.F8, 'F9': Keycode.F9, 'F10': Keycode.F10, 'F11': Keycode.F11,
|
'F8': Keycode.F8, 'F9': Keycode.F9, 'F10': Keycode.F10, 'F11': Keycode.F11,
|
||||||
'F12': Keycode.F12,
|
'F12': Keycode.F12, 'F13': Keycode.F13, 'F14': Keycode.F14, 'F15': Keycode.F15,
|
||||||
|
'F16': Keycode.F16, 'F17': Keycode.F17, 'F18': Keycode.F18, 'F19': Keycode.F19,
|
||||||
|
'F20': Keycode.F20, 'F21': Keycode.F21, 'F22': Keycode.F22, 'F23': Keycode.F23,
|
||||||
|
'F24': Keycode.F24
|
||||||
|
}
|
||||||
|
duckyConsumerKeys = {
|
||||||
|
'MK_VOLUP': ConsumerControlCode.VOLUME_INCREMENT, 'MK_VOLDOWN': ConsumerControlCode.VOLUME_DECREMENT, 'MK_MUTE': ConsumerControlCode.MUTE,
|
||||||
|
'MK_NEXT': ConsumerControlCode.SCAN_NEXT_TRACK, 'MK_PREV': ConsumerControlCode.SCAN_PREVIOUS_TRACK,
|
||||||
|
'MK_PP': ConsumerControlCode.PLAY_PAUSE, 'MK_STOP': ConsumerControlCode.STOP
|
||||||
}
|
}
|
||||||
|
|
||||||
variables = {}
|
variables = {"$_RANDOM_MIN": 0, "$_RANDOM_MAX": 65535}
|
||||||
|
internalVariables = {"$_CAPSLOCK_ON": _capsOn, "$_NUMLOCK_ON": _numOn, "$_SCROLLLOCK_ON": _scrollOn}
|
||||||
|
defines = {}
|
||||||
functions = {}
|
functions = {}
|
||||||
|
|
||||||
|
letters = "abcdefghijklmnopqrstuvwxyz"
|
||||||
|
numbers = "0123456789"
|
||||||
|
specialChars = "!@#$%^&*()"
|
||||||
|
|
||||||
|
class IF:
|
||||||
|
def __init__(self, condition, codeIter):
|
||||||
|
self.condition = condition
|
||||||
|
self.codeIter = list(codeIter)
|
||||||
|
self.lastIfResult = None
|
||||||
|
|
||||||
|
def _exitIf(self):
|
||||||
|
_depth = 0
|
||||||
|
for line in self.codeIter:
|
||||||
|
line = self.codeIter.pop(0)
|
||||||
|
line = line.strip()
|
||||||
|
if line.upper().startswith("END_IF"):
|
||||||
|
_depth -= 1
|
||||||
|
elif line.upper().startswith("IF"):
|
||||||
|
_depth += 1
|
||||||
|
if _depth < 0:
|
||||||
|
print("No else, exiting" + str(list(self.codeIter)))
|
||||||
|
break
|
||||||
|
return(self.codeIter)
|
||||||
|
|
||||||
|
def runIf(self):
|
||||||
|
if isinstance(self.condition, str):
|
||||||
|
self.lastIfResult = evaluateExpression(self.condition)
|
||||||
|
elif isinstance(self.condition, bool):
|
||||||
|
self.lastIfResult = self.condition
|
||||||
|
else:
|
||||||
|
raise ValueError("Invalid condition type")
|
||||||
|
|
||||||
|
# print(f"condition {self.condition} result is {self.lastIfResult} since \"$VAR\" is {variables["$VAR"]}, code is {self.codeIter}")
|
||||||
|
depth = 0
|
||||||
|
for line in self.codeIter:
|
||||||
|
line = self.codeIter.pop(0)
|
||||||
|
line = line.strip()
|
||||||
|
if line == "":
|
||||||
|
continue
|
||||||
|
# print(line)
|
||||||
|
|
||||||
|
if line.startswith("IF"):
|
||||||
|
depth += 1
|
||||||
|
elif line.startswith("END_IF"):
|
||||||
|
if depth == 0:
|
||||||
|
return(self.codeIter, -1)
|
||||||
|
depth -=1
|
||||||
|
|
||||||
|
elif line.startswith("ELSE") and depth == 0:
|
||||||
|
# print(f"ELSE LINE {line}, lastIfResult: {self.lastIfResult}")
|
||||||
|
if self.lastIfResult is False:
|
||||||
|
line = line[4:].strip() # Remove 'ELSE' and strip whitespace
|
||||||
|
if line.startswith("IF"):
|
||||||
|
nestedCondition = _getIfCondition(line)
|
||||||
|
# print(f"nested IF {nestedCondition}")
|
||||||
|
self.codeIter, self.lastIfResult = IF(nestedCondition, self.codeIter).runIf()
|
||||||
|
if self.lastIfResult == -1 or self.lastIfResult == True:
|
||||||
|
# print(f"self.lastIfResult {self.lastIfResult}")
|
||||||
|
return(self.codeIter, True)
|
||||||
|
else:
|
||||||
|
return IF(True, self.codeIter).runIf() #< Regular ELSE block
|
||||||
|
else:
|
||||||
|
self._exitIf()
|
||||||
|
break
|
||||||
|
|
||||||
|
# Process regular lines
|
||||||
|
elif self.lastIfResult:
|
||||||
|
# print(f"running line {line}")
|
||||||
|
self.codeIter = list(parseLine(line, self.codeIter))
|
||||||
|
# print("end of if")
|
||||||
|
return(self.codeIter, self.lastIfResult)
|
||||||
|
|
||||||
|
def _getIfCondition(line):
|
||||||
|
return str(line)[2:-4].strip()
|
||||||
|
|
||||||
|
def _isCodeBlock(line):
|
||||||
|
line = line.upper().strip()
|
||||||
|
if line.startswith("IF") or line.startswith("WHILE"):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _getCodeBlock(linesIter):
|
||||||
|
"""Returns the code block starting at the given line."""
|
||||||
|
code = []
|
||||||
|
depth = 1
|
||||||
|
for line in linesIter:
|
||||||
|
line = line.strip()
|
||||||
|
if line.upper().startswith("END_"):
|
||||||
|
depth -= 1
|
||||||
|
elif _isCodeBlock(line):
|
||||||
|
depth += 1
|
||||||
|
if depth <= 0:
|
||||||
|
break
|
||||||
|
code.append(line)
|
||||||
|
return code
|
||||||
|
|
||||||
|
def evaluateExpression(expression):
|
||||||
|
"""Evaluates an expression with variables and returns the result."""
|
||||||
|
# Replace variables (e.g., $FOO) in the expression with their values
|
||||||
|
expression = re.sub(r"\$(\w+)", lambda m: str(variables.get(f"${m.group(1)}", 0)), expression)
|
||||||
|
|
||||||
|
expression = expression.replace("^", "**") #< Replace ^ with ** for exponentiation
|
||||||
|
expression = expression.replace("&&", "and")
|
||||||
|
expression = expression.replace("||", "or")
|
||||||
|
|
||||||
|
return eval(expression, {}, variables)
|
||||||
|
|
||||||
|
def deepcopy(List):
|
||||||
|
return(List[:])
|
||||||
|
|
||||||
def convertLine(line):
|
def convertLine(line):
|
||||||
newline = []
|
commands = []
|
||||||
# print(line)
|
# print(line)
|
||||||
# loop on each key - the filter removes empty values
|
# loop on each key - the filter removes empty values
|
||||||
for key in filter(None, line.split(" ")):
|
for key in filter(None, line.split(" ")):
|
||||||
key = key.upper()
|
key = key.upper()
|
||||||
# find the keycode for the command in the list
|
# find the keycode for the command in the list
|
||||||
command_keycode = duckyCommands.get(key, None)
|
command_keycode = duckyKeys.get(key, None)
|
||||||
|
command_consumer_keycode = duckyConsumerKeys.get(key, None)
|
||||||
if command_keycode is not None:
|
if command_keycode is not None:
|
||||||
# if it exists in the list, use it
|
# if it exists in the list, use it
|
||||||
newline.append(command_keycode)
|
commands.append(command_keycode)
|
||||||
|
elif command_consumer_keycode is not None:
|
||||||
|
# if it exists in the list, use it
|
||||||
|
commands.append(1000+command_consumer_keycode)
|
||||||
elif hasattr(Keycode, key):
|
elif hasattr(Keycode, key):
|
||||||
# if it's in the Keycode module, use it (allows any valid keycode)
|
# if it's in the Keycode module, use it (allows any valid keycode)
|
||||||
newline.append(getattr(Keycode, key))
|
commands.append(getattr(Keycode, key))
|
||||||
else:
|
else:
|
||||||
# if it's not a known key name, show the error for diagnosis
|
# if it's not a known key name, show the error for diagnosis
|
||||||
print(f"Unknown key: <{key}>")
|
print(f"Unknown key: <{key}>")
|
||||||
# print(newline)
|
# print(commands)
|
||||||
return newline
|
return commands
|
||||||
|
|
||||||
def runScriptLine(line):
|
def runScriptLine(line):
|
||||||
if isinstance(line, str):
|
keys = convertLine(line)
|
||||||
line = convertLine(line)
|
for k in keys:
|
||||||
for k in line:
|
if k > 1000:
|
||||||
kbd.press(k)
|
consumerControl.press(int(k-1000))
|
||||||
kbd.release_all()
|
else:
|
||||||
|
kbd.press(k)
|
||||||
|
for k in reversed(keys):
|
||||||
|
if k > 1000:
|
||||||
|
consumerControl.release()
|
||||||
|
else:
|
||||||
|
kbd.release(k)
|
||||||
|
|
||||||
def sendString(line):
|
def sendString(line):
|
||||||
layout.write(line)
|
layout.write(line)
|
||||||
|
|
||||||
|
def replaceVariables(line):
|
||||||
|
for var in variables:
|
||||||
|
line = line.replace(var, str(variables[var]))
|
||||||
|
for var in internalVariables:
|
||||||
|
line = line.replace(var, str(internalVariables[var]()))
|
||||||
|
return line
|
||||||
|
|
||||||
|
def replaceDefines(line):
|
||||||
|
for define, value in defines.items():
|
||||||
|
line = line.replace(define, value)
|
||||||
|
return line
|
||||||
|
|
||||||
def parseLine(line, script_lines):
|
def parseLine(line, script_lines):
|
||||||
global defaultDelay, variables, functions
|
global defaultDelay, variables, functions, defines
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if(line[0:3] == "REM"):
|
line = line.replace("$_RANDOM_INT", str(random.randint(int(variables.get("$_RANDOM_MIN", 0)), int(variables.get("$_RANDOM_MAX", 65535)))))
|
||||||
# ignore ducky script comments
|
line = replaceDefines(line)
|
||||||
|
if line[:10] == "INJECT_MOD":
|
||||||
|
line = line[11:]
|
||||||
|
elif line.startswith("REM_BLOCK"):
|
||||||
|
while line.startswith("END_REM") == False:
|
||||||
|
line = next(script_lines).strip()
|
||||||
|
# print(line)
|
||||||
|
elif(line[0:3] == "REM"):
|
||||||
pass
|
pass
|
||||||
|
elif line.startswith("HOLD"):
|
||||||
|
# HOLD command to press and hold a key
|
||||||
|
key = line[5:].strip().upper()
|
||||||
|
commandKeycode = duckyKeys.get(key, None)
|
||||||
|
if commandKeycode:
|
||||||
|
kbd.press(commandKeycode)
|
||||||
|
else:
|
||||||
|
print(f"Unknown key to HOLD: <{key}>")
|
||||||
|
elif line.startswith("RELEASE"):
|
||||||
|
# RELEASE command to release a held key
|
||||||
|
key = line[8:].strip().upper()
|
||||||
|
commandKeycode = duckyKeys.get(key, None)
|
||||||
|
if commandKeycode:
|
||||||
|
kbd.release(commandKeycode)
|
||||||
|
else:
|
||||||
|
print(f"Unknown key to RELEASE: <{key}>")
|
||||||
elif(line[0:5] == "DELAY"):
|
elif(line[0:5] == "DELAY"):
|
||||||
|
line = replaceVariables(line)
|
||||||
time.sleep(float(line[6:])/1000)
|
time.sleep(float(line[6:])/1000)
|
||||||
|
elif line == "STRINGLN": #< stringLN block
|
||||||
|
line = next(script_lines).strip()
|
||||||
|
line = replaceVariables(line)
|
||||||
|
while line.startswith("END_STRINGLN") == False:
|
||||||
|
sendString(line)
|
||||||
|
kbd.press(Keycode.ENTER)
|
||||||
|
kbd.release(Keycode.ENTER)
|
||||||
|
line = next(script_lines).strip()
|
||||||
|
line = replaceVariables(line)
|
||||||
|
line = replaceDefines(line)
|
||||||
|
elif(line[0:8] == "STRINGLN"):
|
||||||
|
sendString(replaceVariables(line[9:]))
|
||||||
|
kbd.press(Keycode.ENTER)
|
||||||
|
kbd.release(Keycode.ENTER)
|
||||||
|
elif line == "STRING": #< string block
|
||||||
|
line = next(script_lines).strip()
|
||||||
|
line = replaceVariables(line)
|
||||||
|
while line.startswith("END_STRING") == False:
|
||||||
|
sendString(line)
|
||||||
|
line = next(script_lines).strip()
|
||||||
|
line = replaceVariables(line)
|
||||||
|
line = replaceDefines(line)
|
||||||
elif(line[0:6] == "STRING"):
|
elif(line[0:6] == "STRING"):
|
||||||
sendString(line[7:])
|
sendString(replaceVariables(line[7:]))
|
||||||
elif(line[0:5] == "PRINT"):
|
elif(line[0:5] == "PRINT"):
|
||||||
print("[SCRIPT]: " + line[6:])
|
line = replaceVariables(line[6:])
|
||||||
|
print("[SCRIPT]: " + line)
|
||||||
elif(line[0:6] == "IMPORT"):
|
elif(line[0:6] == "IMPORT"):
|
||||||
runScript(line[7:])
|
runScript(line[7:])
|
||||||
elif(line[0:13] == "DEFAULT_DELAY"):
|
elif(line[0:13] == "DEFAULT_DELAY"):
|
||||||
@@ -103,6 +308,17 @@ def parseLine(line, script_lines):
|
|||||||
led.value = False
|
led.value = False
|
||||||
else:
|
else:
|
||||||
led.value = True
|
led.value = True
|
||||||
|
elif(line[0:3] == "LED"):
|
||||||
|
if(led.value == True):
|
||||||
|
led.value = False
|
||||||
|
else:
|
||||||
|
led.value = True
|
||||||
|
elif(line[:7] == "LED_OFF"):
|
||||||
|
led.value = False
|
||||||
|
elif(line[:5] == "LED_R"):
|
||||||
|
led.value = True
|
||||||
|
elif(line[:5] == "LED_G"):
|
||||||
|
led.value = True
|
||||||
elif(line[0:21] == "WAIT_FOR_BUTTON_PRESS"):
|
elif(line[0:21] == "WAIT_FOR_BUTTON_PRESS"):
|
||||||
button_pressed = False
|
button_pressed = False
|
||||||
# NOTE: we don't use assincio in this case because we want to block code execution
|
# NOTE: we don't use assincio in this case because we want to block code execution
|
||||||
@@ -117,9 +333,30 @@ def parseLine(line, script_lines):
|
|||||||
print("Button 1 pushed")
|
print("Button 1 pushed")
|
||||||
button_pressed = True
|
button_pressed = True
|
||||||
elif line.startswith("VAR"):
|
elif line.startswith("VAR"):
|
||||||
_, var, _, value = line.split()
|
match = re.match(r"VAR\s+\$(\w+)\s*=\s*(.+)", line)
|
||||||
variables[var] = int(value)
|
if match:
|
||||||
|
varName = f"${match.group(1)}"
|
||||||
|
value = evaluateExpression(match.group(2))
|
||||||
|
variables[varName] = value
|
||||||
|
else:
|
||||||
|
raise SyntaxError(f"Invalid variable declaration: {line}")
|
||||||
|
elif line.startswith("$"):
|
||||||
|
match = re.match(r"\$(\w+)\s*=\s*(.+)", line)
|
||||||
|
if match:
|
||||||
|
varName = f"${match.group(1)}"
|
||||||
|
expression = match.group(2)
|
||||||
|
value = evaluateExpression(expression)
|
||||||
|
variables[varName] = value
|
||||||
|
else:
|
||||||
|
raise SyntaxError(f"Invalid variable update, declare variable first: {line}")
|
||||||
|
elif line.startswith("DEFINE"):
|
||||||
|
defineLocation = line.find(" ")
|
||||||
|
valueLocation = line.find(" ", defineLocation + 1)
|
||||||
|
defineName = line[defineLocation+1:valueLocation]
|
||||||
|
defineValue = line[valueLocation+1:]
|
||||||
|
defines[defineName] = defineValue
|
||||||
elif line.startswith("FUNCTION"):
|
elif line.startswith("FUNCTION"):
|
||||||
|
# print("ENTER FUNCTION")
|
||||||
func_name = line.split()[1]
|
func_name = line.split()[1]
|
||||||
functions[func_name] = []
|
functions[func_name] = []
|
||||||
line = next(script_lines).strip()
|
line = next(script_lines).strip()
|
||||||
@@ -127,19 +364,45 @@ def parseLine(line, script_lines):
|
|||||||
functions[func_name].append(line)
|
functions[func_name].append(line)
|
||||||
line = next(script_lines).strip()
|
line = next(script_lines).strip()
|
||||||
elif line.startswith("WHILE"):
|
elif line.startswith("WHILE"):
|
||||||
condition = re.search(r'\((.*?)\)', line).group(1)
|
# print("ENTER WHILE LOOP")
|
||||||
var_name, _, condition_value = condition.split()
|
condition = line[5:].strip()
|
||||||
condition_value = int(condition_value)
|
loopCode = list(_getCodeBlock(script_lines))
|
||||||
loop_code = []
|
while evaluateExpression(condition) == True:
|
||||||
line = next(script_lines).strip()
|
currentIterCode = deepcopy(loopCode)
|
||||||
while line != "END_WHILE":
|
print(loopCode)
|
||||||
if not (line.startswith("WHILE")):
|
while currentIterCode:
|
||||||
loop_code.append(line)
|
loopLine = currentIterCode.pop(0)
|
||||||
line = next(script_lines).strip()
|
currentIterCode = list(parseLine(loopLine, iter(currentIterCode))) #< very inefficient, should be replaced later.
|
||||||
while variables[var_name] > condition_value:
|
|
||||||
for loop_line in loop_code:
|
elif line.upper().startswith("IF"):
|
||||||
parseLine(loop_line, {})
|
# print("ENTER IF")
|
||||||
variables[var_name] -= 1
|
script_lines, ret = IF(_getIfCondition(line), script_lines).runIf()
|
||||||
|
print(f"IF returned {ret} code")
|
||||||
|
elif line.upper().startswith("END_IF"):
|
||||||
|
pass
|
||||||
|
elif line == "RANDOM_LOWERCASE_LETTER":
|
||||||
|
sendString(random.choice(letters))
|
||||||
|
elif line == "RANDOM_UPPERCASE_LETTER":
|
||||||
|
sendString(random.choice(letters.upper()))
|
||||||
|
elif line == "RANDOM_LETTER":
|
||||||
|
sendString(random.choice(letters + letters.upper()))
|
||||||
|
elif line == "RANDOM_NUMBER":
|
||||||
|
sendString(random.choice(numbers))
|
||||||
|
elif line == "RANDOM_SPECIAL":
|
||||||
|
sendString(random.choice(specialChars))
|
||||||
|
elif line == "RANDOM_CHAR":
|
||||||
|
sendString(random.choice(letters + letters.upper() + numbers + specialChars))
|
||||||
|
elif line == "VID_RANDOM" or line == "PID_RANDOM":
|
||||||
|
for _ in range(4):
|
||||||
|
sendString(random.choice("0123456789ABCDEF"))
|
||||||
|
elif line == "MAN_RANDOM" or line == "PROD_RANDOM":
|
||||||
|
for _ in range(12):
|
||||||
|
sendString(random.choice(letters + letters.upper() + numbers))
|
||||||
|
elif line == "SERIAL_RANDOM":
|
||||||
|
for _ in range(12):
|
||||||
|
sendString(random.choice(letters + letters.upper() + numbers + specialChars))
|
||||||
|
elif line == "RESET":
|
||||||
|
kbd.release_all()
|
||||||
elif line in functions:
|
elif line in functions:
|
||||||
updated_lines = []
|
updated_lines = []
|
||||||
inside_while_block = False
|
inside_while_block = False
|
||||||
@@ -157,15 +420,14 @@ def parseLine(line, script_lines):
|
|||||||
elif not (func_line.startswith("END_WHILE") or func_line.startswith("WHILE")):
|
elif not (func_line.startswith("END_WHILE") or func_line.startswith("WHILE")):
|
||||||
parseLine(func_line, iter(functions[line]))
|
parseLine(func_line, iter(functions[line]))
|
||||||
else:
|
else:
|
||||||
newScriptLine = convertLine(line)
|
runScriptLine(line)
|
||||||
runScriptLine(newScriptLine)
|
|
||||||
|
return(script_lines)
|
||||||
|
|
||||||
kbd = Keyboard(usb_hid.devices)
|
kbd = Keyboard(usb_hid.devices)
|
||||||
|
consumerControl = ConsumerControl(usb_hid.devices)
|
||||||
layout = KeyboardLayout(kbd)
|
layout = KeyboardLayout(kbd)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#init button
|
#init button
|
||||||
button1_pin = DigitalInOut(GP22) # defaults to input
|
button1_pin = DigitalInOut(GP22) # defaults to input
|
||||||
button1_pin.pull = Pull.UP # turn on internal pull-up resistor
|
button1_pin.pull = Pull.UP # turn on internal pull-up resistor
|
||||||
@@ -196,22 +458,30 @@ def runScript(file):
|
|||||||
global defaultDelay
|
global defaultDelay
|
||||||
|
|
||||||
duckyScriptPath = file
|
duckyScriptPath = file
|
||||||
|
restart = True
|
||||||
try:
|
try:
|
||||||
with open(duckyScriptPath, "r", encoding='utf-8') as f:
|
while restart:
|
||||||
script_lines = iter(f.readlines())
|
restart = False
|
||||||
previousLine = ""
|
with open(duckyScriptPath, "r", encoding='utf-8') as f:
|
||||||
for line in script_lines:
|
script_lines = iter(f.readlines())
|
||||||
print(f"runScript: {line}")
|
previousLine = ""
|
||||||
|
for line in script_lines:
|
||||||
if(line[0:6] == "REPEAT"):
|
print(f"runScript: {line}")
|
||||||
for i in range(int(line[7:])):
|
if(line[0:6] == "REPEAT"):
|
||||||
#repeat the last command
|
for i in range(int(line[7:])):
|
||||||
parseLine(previousLine, script_lines)
|
#repeat the last command
|
||||||
time.sleep(float(defaultDelay) / 1000)
|
parseLine(previousLine, script_lines)
|
||||||
else:
|
time.sleep(float(defaultDelay) / 1000)
|
||||||
parseLine(line, script_lines)
|
elif line.startswith("RESTART_PAYLOAD"):
|
||||||
previousLine = line
|
restart = True
|
||||||
time.sleep(float(defaultDelay) / 1000)
|
break
|
||||||
|
elif line.startswith("STOP_PAYLOAD"):
|
||||||
|
restart = False
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
parseLine(line, script_lines)
|
||||||
|
previousLine = line
|
||||||
|
time.sleep(float(defaultDelay) / 1000)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print("Unable to open file", file)
|
print("Unable to open file", file)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user