-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcode.py
166 lines (135 loc) · 4.89 KB
/
code.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
#webserver stuff
import socketpool
import wifi
from adafruit_httpserver.server import *
from adafruit_httpserver.request import *
from adafruit_httpserver.response import *
from adafruit_httpserver.route import *
#rubber ducky stuff
import time
import os
import usb_hid
import json
import creds
import binascii
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from adafruit_hid.mouse import Mouse
'''
rubber duck! made by TheSavageTeddy (github)
note: if the filename is 'code.py', it is automatically ran when the pico W is powered
'''
ssid = creds.ssid
wifipassword = creds.wifipassword
print("Connecting to", ssid)
wifi.radio.connect(ssid, wifipassword)
print("Connected to", ssid)
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
pool = socketpool.SocketPool(wifi.radio)
server = HTTPServer(pool)
def _mouseMove(x, y):
print("moving mouse",x,y)
mouse.move(x=x, y=y)
def _mouseClick(button):
mouse.click(button)
def _mouseButton(button, release):
if release:
mouse.release(button)
else:
mouse.press(button)
def _modifierKey(key, release):
print(key, release)
if release:
keyboard.release(eval(f"Keycode.{key}"))
else:
keyboard.press(eval(f"Keycode.{key}"))
def _sendkeys(keys: list):
try:
for key in keys:
print(key)
if type(key) == str:
# Write text by typing the string
layout.write(key)
elif type(key) == list:
command = key[0].upper()
if command == "MOUSE":
mouse_command = key[1].upper()
if mouse_command == "MOVE":
x_pos = int(key[2])
y_pos = int(key[3])
_mouseMove(x_pos, y_pos)
else:
try:
mouse_button = int(key[2])
except IndexError:
# some people might forget to put the mouse button, so default to left
mouse_button = 1
if mouse_command == "DOWN":
_mouseButton(mouse_button, False)
elif mouse_command == "UP":
_mouseButton(mouse_button, True)
elif mouse_command == "CLICK":
_mouseClick(mouse_button)
else:
print(f"Mouse command {mouse_command} not found, ignoring...")
elif command == "SLEEP":
time.sleep(float(key[1])/1000)
else:
release = key[1].upper() == "UP"
modifier_key = key[0].upper()
_modifierKey(modifier_key, release)
except Exception as e:
print(f"An error occured: {e}")
@server.route("/")
def base(request): # pylint: disable=unused-argument
"""Default reponse is /index.html"""
return HTTPResponse(filename="/index.html")
@server.route("/sendkeys", "GET")
def sendkeys(keys):
request = HTTPRequest(raw_request=keys.raw_request)
request_data = request.query_params
try:
keys = binascii.unhexlify(request_data["keys"])
keys = json.loads(keys)
keys = keys["keys"]
except ValueError as e:
print(f"payload not sent: error {e}")
return HTTPResponse(filename="/index.html")
_sendkeys(keys)
return HTTPResponse(filename="/index.html")
@server.route("/sendsinglekey", "GET")
def sendsinglekey(keys):
request = HTTPRequest(raw_request=keys.raw_request)
request_data = request.query_params
try:
key = binascii.unhexlify(request_data["key"])
keyType = "down" if key[1] == 0 else "up"
except Exception as e:
print(f"payload not sent: error {e}")
return HTTPResponse(body="", content_type="text/plain")
try:
# structure: 1 byte for key, 1 byte for type
# 00 - key down
# 01 - key up
print(keyType, key)
if keyType == "up":
keyboard.release(key[0])
else:
keyboard.press(key[0])
except Exception as e:
print(f"key(s) not pressed: error {e}")
return HTTPResponse(body="", content_type="text/plain")
return HTTPResponse(body="", content_type="text/plain")
@server.route("/terminate", "POST")
def terminate(request):
exit(0) # apparently exit isn't even a function so it crashes anyways
return
#setup rubber ducky devices
keyboard = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(keyboard)
mouse = Mouse(usb_hid.devices)
# serve the server
server.serve_forever(str(wifi.radio.ipv4_address), 80)