Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulate Mouse & Consumer Control #213

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

<br />

## What have I changed? -- By PoulDev
I added some commands to move the mouse pointer and clicking with it, I've also added some customer control commands like increasing the audio volume and more.

## Quick Start Guide
Install and have your USB Rubber Ducky working in less than 5 minutes.

Expand Down
14 changes: 2 additions & 12 deletions code.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


import time
import digitalio
from board import *
import board
from duckyinpython import *
Expand All @@ -21,8 +20,6 @@
time.sleep(.5)

def startWiFi():
import ipaddress
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
Expand All @@ -41,13 +38,6 @@ def startWiFi():
#supervisor.disable_autoreload()
supervisor.runtime.autoreload = False

if(board.board_id == 'raspberry_pi_pico'):
led = pwmio.PWMOut(board.LED, frequency=5000, duty_cycle=0)
elif(board.board_id == 'raspberry_pi_pico_w'):
led = digitalio.DigitalInOut(board.LED)
led.switch_to_output()


progStatus = False
progStatus = getProgrammingStatus()
print("progStatus", progStatus)
Expand All @@ -69,14 +59,14 @@ async def main_loop():

button_task = asyncio.create_task(monitor_buttons(button1))
if(board.board_id == 'raspberry_pi_pico_w'):
pico_led_task = asyncio.create_task(blink_pico_w_led(led))
pico_led_task = asyncio.create_task(blink_pico_w_led())
print("Starting Wifi")
startWiFi()
print("Starting Web Service")
webservice_task = asyncio.create_task(startWebService())
await asyncio.gather(pico_led_task, button_task, webservice_task)
else:
pico_led_task = asyncio.create_task(blink_pico_led(led))
pico_led_task = asyncio.create_task(blink_pico_led())
await asyncio.gather(pico_led_task, button_task)

asyncio.run(main_loop())
155 changes: 102 additions & 53 deletions duckyinpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
from board import *
import pwmio
import asyncio

import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.mouse import Mouse
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

# comment out these lines for non_US keyboards
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS as KeyboardLayout
Expand Down Expand Up @@ -47,6 +51,36 @@
'F12': Keycode.F12,

}

kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayout(kbd)

mouse = Mouse(usb_hid.devices)
cc = ConsumerControl(usb_hid.devices)

#init button
button1_pin = DigitalInOut(GP22) # defaults to input
button1_pin.pull = Pull.UP # turn on internal pull-up resistor
button1 = Debouncer(button1_pin)

#init payload selection switch
payload1Pin = digitalio.DigitalInOut(GP4)
payload1Pin.switch_to_input(pull=digitalio.Pull.UP)
payload2Pin = digitalio.DigitalInOut(GP5)
payload2Pin.switch_to_input(pull=digitalio.Pull.UP)
payload3Pin = digitalio.DigitalInOut(GP10)
payload3Pin.switch_to_input(pull=digitalio.Pull.UP)
payload4Pin = digitalio.DigitalInOut(GP11)
payload4Pin.switch_to_input(pull=digitalio.Pull.UP)

defaultDelay = 0

if(board.board_id == 'raspberry_pi_pico'):
led = pwmio.PWMOut(board.LED, frequency=5000, duty_cycle=0)
elif(board.board_id == 'raspberry_pi_pico_w'):
led = digitalio.DigitalInOut(board.LED)
led.switch_to_output()

def convertLine(line):
newline = []
# print(line)
Expand Down Expand Up @@ -75,53 +109,78 @@ def runScriptLine(line):
def sendString(line):
layout.write(line)

def parseMouseCommand(line):
if line.startswith('CLICK'):
if line[6:] == 'LEFT':
mouse.click(Mouse.LEFT_BUTTON)
elif line[6:] == 'RIGHT':
mouse.click(Mouse.RIGHT_BUTTON)
elif hasattr(Mouse, line[6:]):
mouse.click(getattr(Mouse, line[6:]))

elif line.startswith('MOVE'):
x, y = line[5:].split(',')
x, y = int(x), int(y)

mouse.move(x, y)
elif line.startswith('WHEEL'):
wheel = int(line[5:])
mouse.move(wheel=wheel)

elif line.startswith('PRESS'):
if line[6:] == 'LEFT':
mouse.press(Mouse.LEFT_BUTTON)
elif line[6:] == 'RIGHT':
mouse.press(Mouse.RIGHT_BUTTON)
elif hasattr(Mouse, line[6:]):
mouse.press(getattr(Mouse, line[6:]))

elif line.startswith('RELEASE'):
if line[8:] == 'LEFT':
mouse.release(Mouse.LEFT_BUTTON)
elif line[8:] == 'RIGHT':
mouse.release(Mouse.RIGHT_BUTTON)
elif hasattr(Mouse, line[8:]):
mouse.release(getattr(Mouse, line[8:]))

def parseLine(line):
global defaultDelay
if(line[0:3] == "REM"):
# ignore ducky script comments
pass
elif(line[0:5] == "DELAY"):

if line.startswith("REM"):
pass # ignore ducky script comment

elif line.startswith("DELAY"):
time.sleep(float(line[6:])/1000)
elif(line[0:6] == "STRING"):

elif line.startswith("STRING"):
sendString(line[7:])
elif(line[0:5] == "PRINT"):

elif line.startswith("MOUSE"):
parseMouseCommand(line[6:])

elif line.startswith("CC"):
CC_KEY = line[3:]
if hasattr(ConsumerControlCode, CC_KEY):
cc.send(getattr(ConsumerControlCode, CC_KEY))

elif line.startswith("PRINT"):
print("[SCRIPT]: " + line[6:])
elif(line[0:6] == "IMPORT"):

elif line.startswith("IMPORT"):
runScript(line[7:])
elif(line[0:13] == "DEFAULT_DELAY"):
defaultDelay = int(line[14:]) * 10
elif(line[0:12] == "DEFAULTDELAY"):
defaultDelay = int(line[13:]) * 10
elif(line[0:3] == "LED"):
if(led.value == True):
led.value = False
else:
led.value = True

elif line.startswith("DEFAULT_DELAY"):
defaultDelay = int(line[14:])

elif line.startswith("DEFAULTDELAY"):
defaultDelay = int(line[13:])

elif line.startswith("LED"):
led.value = not led.value
else:
newScriptLine = convertLine(line)
runScriptLine(newScriptLine)

kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayout(kbd)




#init button
button1_pin = DigitalInOut(GP22) # defaults to input
button1_pin.pull = Pull.UP # turn on internal pull-up resistor
button1 = Debouncer(button1_pin)

#init payload selection switch
payload1Pin = digitalio.DigitalInOut(GP4)
payload1Pin.switch_to_input(pull=digitalio.Pull.UP)
payload2Pin = digitalio.DigitalInOut(GP5)
payload2Pin.switch_to_input(pull=digitalio.Pull.UP)
payload3Pin = digitalio.DigitalInOut(GP10)
payload3Pin.switch_to_input(pull=digitalio.Pull.UP)
payload4Pin = digitalio.DigitalInOut(GP11)
payload4Pin.switch_to_input(pull=digitalio.Pull.UP)

def getProgrammingStatus():
# check GP0 for setup mode
# see setup mode for instructions
Expand All @@ -130,9 +189,6 @@ def getProgrammingStatus():
progStatus = not progStatusPin.value
return(progStatus)


defaultDelay = 0

def runScript(file):
global defaultDelay

Expand Down Expand Up @@ -167,16 +223,16 @@ def selectPayload():
payload3State = not payload3Pin.value
payload4State = not payload4Pin.value

if(payload1State == True):
if payload1State:
payload = "payload.dd"

elif(payload2State == True):
elif payload2State:
payload = "payload2.dd"

elif(payload3State == True):
elif payload3State:
payload = "payload3.dd"

elif(payload4State == True):
elif payload4State:
payload = "payload4.dd"

else:
Expand All @@ -186,14 +242,7 @@ def selectPayload():

return payload

async def blink_led(led):
print("Blink")
if(board.board_id == 'raspberry_pi_pico'):
blink_pico_led(led)
elif(board.board_id == 'raspberry_pi_pico_w'):
blink_pico_w_led(led)

async def blink_pico_led(led):
async def blink_pico_led():
print("starting blink_pico_led")
led_state = False
while True:
Expand All @@ -217,7 +266,7 @@ async def blink_pico_led(led):
led_state = True
await asyncio.sleep(0)

async def blink_pico_w_led(led):
async def blink_pico_w_led():
print("starting blink_pico_w_led")
led_state = False
while True:
Expand Down
16 changes: 16 additions & 0 deletions examples/consumerControl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Consumer Control
Simulate a Consumer Control Device from the Raspberry Pi Pico

## Avaible Commands:
```
CC [ConsumerControlCode]
```
Every ConsumerControlCode can be found [here](https://docs.circuitpython.org/projects/hid/en/latest/api.html#adafruit_hid.consumer_control_code.ConsumerControlCode)

## Examples:
```
CC BRIGHTNESS_INCREMENT
CC PLAY_PAUSE
CC MUTE
CC EJECT
```
14 changes: 14 additions & 0 deletions examples/consumerControl/brightness.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
REM Make the monitor as dark as possible while the
REM payload is running, then set it to maximum brightness.

REM The system needs a bit of time to adjust the brightness
DEFAULT_DELAY 200

CC BRIGHTNESS_DECREMENT
REPEAT 9

REM Payload Script Here
DELAY 2000

CC BRIGHTNESS_INCREMENT
REPEAT 9
6 changes: 6 additions & 0 deletions examples/consumerControl/volume.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
REM Max out the volume
REM Every time you press the button, the volume increases by 2,
REM so to make sure the volume is at its maximum, you need to press it 50 times.

CC VOLUME_INCREMENT
REPEAT 49
21 changes: 21 additions & 0 deletions examples/mouse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Mouse
Simulate a Mouse from the Raspberry Pi Pico

## Avaible Commands:
```
MOUSE CLICK LEFT/RIGHT
MOUSE MOVE x,y
MOUSE WHEEL x
MOUSE PRESS LEFT/RIGHT
MOUSE RELEASE LEFT/RIGHT
```

## Examples:
```
MOUSE CLICK LEFT
MOUSE MOVE 200,-100
MOUSE WHEEL 50
MOUSE WHEEL -50
MOUSE PRESS LEFT
MOUSE RELEASE LEFT
```
8 changes: 8 additions & 0 deletions examples/mouse/click_and_movements.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
REM Test mouse click and movement

MOUSE CLICK RIGHT
DELAY 500
MOUSE CLICK LEFT
MOUSE WHEEL 10
DELAY 200
MOUSE WHEEL -10
14 changes: 14 additions & 0 deletions examples/mouse/hide_window.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
REM Open the CMD and move it to the bottom of the screen
GUI r
DELAY 500
STRING cmd
ENTER
DELAY 1000
ALT SPACE
DELAY 100
STRING s
DELAY 100
DOWNARROW
DELAY 100
MOUSE MOVE 0,1500
ENTER
6 changes: 6 additions & 0 deletions examples/mouse/select_text.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
REM Select something on the screen

MOUSE PRESS LEFT
MOUSE MOVE 100,100
MOUSE RELEASE LEFT

6 changes: 4 additions & 2 deletions payload.dd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
REM The next four lines open Notepad in Windows and type "Hello World!"

GUI r
DELAY 400
STRING notepad
ENTER
DELAY 250
STRING Hello World!
DELAY 400
STRING Hello World!