-
Notifications
You must be signed in to change notification settings - Fork 0
/
ducksy.ino
368 lines (343 loc) · 11.3 KB
/
ducksy.ino
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
#include <BlockDriver.h>
#include <FreeStack.h>
#include <MinimumSerial.h>
#include <SdFat.h>
// Function prototypes
void modKeyPress(uint8_t mod, uint8_t key);
String getInstruction(String msg, int index);
uint8_t extendedCommands(String key);
uint8_t translateKey(String key);
uint8_t translateMod(String mod);
void setScreenResolution(String msg);
void mouseControl(String msg);
void parseCommand(String msg);
// Create classes used for SdFat
SdFatSdioEX sd;
File payload, dropfile;
// Global variables that can be changed before compile/upload
int _parse_delay = 200;
int _resolution_x = 1920;
int _resolution_y = 1080;
char _payload_file[] = "payload.txt";
const size_t _line_buf = 2048;
// Store the previous message to enable the REPEAT command
String _previous_msg;
void setup()
{
char line[_line_buf];
// Begin serial and halt if avaliable to wait for a monitor
// to connect to capture debugging information
Serial.begin(9600);
while (!Serial) {
}
do {
delay(10);
} while (Serial.available() && Serial.read());
// Delay for HID to register on host
delay(2000);
//Initalise default screen size
Mouse.screenSize(_resolution_x, _resolution_y);
// Connect to SD card
if (!sd.begin())
{
sd.initErrorHalt("SdFatSdioEX begin() failed");
}
Serial.println("SD Opened :: Listing DIR Structure");
Serial.println("---");
sd.ls(LS_R);
Serial.println("---");
if (!payload.open(_payload_file, O_RDONLY))
{
sd.errorHalt("payload file open() failed");
}
Serial.println("Payload Opened :: Executing Commands");
while ((payload.fgets(line, sizeof(line))) > 0)
{
parseCommand(String(line).trim());
}
payload.close();
}
void loop()
{
// put your main code here, to run repeatedly:
}
// Press a key and any modifiers
void modKeyPress(uint8_t mod, uint8_t key)
{
// Press and hold our modifier
Keyboard.set_modifier(mod);
Keyboard.send_now();
delay(100);
Keyboard.set_key1(key);
Keyboard.send_now();
delay(200);
// Release everything
Keyboard.set_modifier(0);
Keyboard.set_key1(0);
Keyboard.send_now();
//delay(100);
}
// Simple inefficent tokenisation of a String
String getInstruction(String msg, int index)
{
int count = 0, i = 0, j = 0;
while(msg[i] == ' ') ++i;
for (; i < msg.length(); ++i)
{
if(msg[i] == ' ')
{
if(count++ == index)
{
return msg.substring(j,i);
}
while(msg[i] == ' ') ++i;
j = i;
}
}
if(j == 0 && count == 0) return msg;
else if(count == index) return msg.substring(j,i);
return "";
}
// Translation of extended commands from Duckyscript ref. https://www.pjrc.com/teensy/td_keyboard.html
uint8_t extendedCommands(String key)
{
if (key == "ENTER") return KEY_ENTER;
else if (key == "ESC" || key == "ESCAPE") return KEY_ESC;
else if (key == "BACKSPACE") return KEY_BACKSPACE;
else if (key == "TAB") return KEY_TAB;
else if (key == "SPACE") return KEY_SPACE;
else if (key == "MENU" || key == "APP") return KEY_MENU;
else if (key == "CAPS_LOCK") return KEY_CAPS_LOCK;
else if (key == "F1") return KEY_F1;
else if (key == "F2") return KEY_F2;
else if (key == "F3") return KEY_F3;
else if (key == "F4") return KEY_F4;
else if (key == "F5") return KEY_F5;
else if (key == "F6") return KEY_F6;
else if (key == "F7") return KEY_F7;
else if (key == "F8") return KEY_F8;
else if (key == "F9") return KEY_F9;
else if (key == "F10") return KEY_F10;
else if (key == "F11") return KEY_F11;
else if (key == "F12") return KEY_F12;
else if (key == "PRINTSCREEN") return KEY_PRINTSCREEN;
else if (key == "SCROLLLOCK") return KEY_SCROLL_LOCK;
else if (key == "PAUSE" || key == "BREAK") return KEY_PAUSE;
else if (key == "INSERT") return KEY_INSERT;
else if (key == "HOME") return KEY_HOME;
else if (key == "PAGE_UP") return KEY_PAGE_UP;
else if (key == "DELETE") return KEY_DELETE;
else if (key == "END") return KEY_END;
else if (key == "PAGE_DOWN") return KEY_PAGE_DOWN;
else if (key == "RIGHT" || key == "RIGHTARROW" ) return KEY_RIGHT;
else if (key == "LEFT" || key == "LEFTARROW") return KEY_LEFT;
else if (key == "DOWN" || key == "DOWNARROW") return KEY_DOWN;
else if (key == "UP" || key == "UPARROW") return KEY_UP;
else if (key == "NUM_LOCK") return KEY_NUM_LOCK;
else return 0;
}
// Translation of keys to their numerical values ref. https://www.pjrc.com/teensy/td_keyboard.html
uint8_t translateKey(String key)
{
if (uint8_t val = extendedCommands(key)) return val;
else if (key == "MINUS") return KEY_MINUS;
else if (key == "EQUAL") return KEY_EQUAL;
else if (key == "LEFT_BRACE") return KEY_LEFT_BRACE;
else if (key == "RIGHT_BRACE") return KEY_RIGHT_BRACE;
else if (key == "BACKSLASH") return KEY_BACKSLASH;
else if (key == "SEMICOLON") return KEY_SEMICOLON;
else if (key == "QUOTE") return KEY_QUOTE;
else if (key == "TILDE") return KEY_TILDE;
else if (key == "COMMA") return KEY_COMMA;
else if (key == "PERIOD") return KEY_PERIOD;
else if (key == "SLASH") return KEY_SLASH;
else
{
switch(toupper(key[0]))
{
case 'A': return KEY_A;
case 'B': return KEY_B;
case 'C': return KEY_C;
case 'D': return KEY_D;
case 'E': return KEY_E;
case 'F': return KEY_F;
case 'G': return KEY_G;
case 'H': return KEY_H;
case 'I': return KEY_I;
case 'J': return KEY_J;
case 'K': return KEY_K;
case 'L': return KEY_L;
case 'M': return KEY_M;
case 'N': return KEY_N;
case 'O': return KEY_O;
case 'P': return KEY_P;
case 'Q': return KEY_Q;
case 'R': return KEY_R;
case 'S': return KEY_S;
case 'T': return KEY_T;
case 'U': return KEY_U;
case 'V': return KEY_V;
case 'W': return KEY_W;
case 'X': return KEY_X;
case 'Y': return KEY_Y;
case 'Z': return KEY_Z;
case '1': return KEY_1;
case '2': return KEY_2;
case '3': return KEY_3;
case '4': return KEY_4;
case '5': return KEY_5;
case '6': return KEY_6;
case '7': return KEY_7;
case '8': return KEY_8;
case '9': return KEY_9;
case '0': return KEY_0;
}
}
return 0;
}
// Translation of modifier keys ref. https://www.pjrc.com/teensy/td_keyboard.html
uint8_t translateMod(String mod)
{
if(mod == "CONTROL" || mod == "CTRL") return MODIFIERKEY_CTRL;
else if(mod == "GUI" || mod == "WINDOWS") return MODIFIERKEY_GUI;
else if(mod == "ALT") return MODIFIERKEY_ALT;
else if(mod == "SHIFT") return MODIFIERKEY_SHIFT;
return 0;
}
// Allows changing of the resolution through a command
// Command Format: RESOLUTION [X] [Y]
void setScreenResolution(String msg)
{
String x_str = getInstruction(msg, 1), y_str = getInstruction(msg, 2);
_resolution_x = x_str.toInt(), _resolution_y = y_str.toInt();
Mouse.screenSize(_resolution_x, _resolution_y);
}
// Types out contents of a file over HID keyboard input
// only prints characters that map to the selected keymap
// Command Format: FILE [filename]
void printFile(String msg)
{
String filename = msg.substring(5);
if (dropfile.open(filename.c_str(), O_RDONLY))
{
while (dropfile.available()) Keyboard.write(dropfile.read());
dropfile.close();
}
else
{
sd.errorHalt("dropfile file open() failed");
}
}
/*
'MOUSE' command allows clicking/toggle of buttons and pointer movement
* MOUSE CLICK (MOUSE_LEFT|MOUSE_MIDDLE|MOUSE_RIGHT)
* MOUSE TOGGLE (MOUSE_LEFT|MOUSE_MIDDLE|MOUSE_RIGHT)
* MOUSE MOVE [X] [Y]
* MOUSE MOVETO [X] [Y]
*/
void mouseControl(String msg)
{
String instruction = getInstruction(msg, 1);
/*
Supports (MOUSE_LEFT|MOUSE_MIDDLE|MOUSE_RIGHT) as input
Default if not specified will be MOUSE_LEFT
*/
if(instruction == "CLICK")
{
instruction = getInstruction(msg, 2);
if(instruction == "MOUSE_RIGHT") Mouse.click(MOUSE_RIGHT);
else if(instruction == "MOUSE_MIDDLE") Mouse.click(MOUSE_MIDDLE);
else Mouse.click(MOUSE_LEFT);
}
/*
What's the difference?
* 'MOVE' provides relative movement from current position
* 'MOVETO' locates the mouse at an absolute position based on the defined 'RESOLUTION'
*/
else if(instruction == "MOVE" || instruction == "MOVETO")
{
String x_str = getInstruction(msg, 2), y_str = getInstruction(msg, 3);
int x = x_str.toInt(), y = y_str.toInt();
if(instruction == "MOVE")
{
Mouse.move(x,y);
}
else
{
Mouse.moveTo(x,y);
}
}
// 'TOGGLE' presses the mouse button without release
// Can be combined with 'MOVE' to drag files
else if(instruction == "TOGGLE")
{
uint8_t button = MOUSE_LEFT;
instruction = getInstruction(msg, 2);
if(instruction == "MOUSE_RIGHT") button = MOUSE_RIGHT;
else if(instruction == "MOUSE_MIDDLE") button = MOUSE_MIDDLE;
(Mouse.isPressed(button)) ? Mouse.release(button) : Mouse.press(button);
}
}
// Repeat the previous command
void repeatPrevious(String msg)
{
int occurances = getInstruction(msg, 1).toInt();
if(getInstruction(_previous_msg, 0) != "REPEAT")
{
for(size_t i = 0; i < occurances; ++i)
{
parseCommand(_previous_msg);
}
}
}
/*
Based on Duckyscript with the addition of mouse movement allowed by the Teensy
Should support all commands listed here: https://github.com/hak5darren/USB-Rubber-Ducky/wiki/Duckyscript
Additional Commands avaliable:
* 'MOUSE' provides for mouse three button click and movement tracking
* 'STRINGLN' same as 'STRING' but ends with a newline
* 'RESOLUTION' supports setting the monitor resolution for 'MOUSE MOVETO'
* 'FILE' types out the contents of a file stored on the SD card
*/
void parseCommand(String msg)
{
Serial.print("> ");
Serial.println(msg);
delay(_parse_delay);
String instruction = getInstruction(msg, 0);
uint8_t key = 0;
if(instruction == "") return;
else if(instruction == "STRING") Keyboard.print(msg.substring(instruction.length()+1,msg.length()));
else if(instruction == "STRINGLN") Keyboard.println(msg.substring(instruction.length()+1,msg.length()));
else if(instruction == "ENTER") Keyboard.println();
else if((key = extendedCommands(instruction))) modKeyPress(0,key);
else if((key = translateMod(instruction)))
{
uint8_t currkey = 0, i = 1;
instruction = getInstruction(msg, i);
while(instruction != "")
{
if((currkey = translateMod(instruction)) != 0)
{
key |= currkey;
}
else
{
modKeyPress(key, translateKey(instruction));
return;
}
instruction = getInstruction(msg, ++i);
}
// Should only get here if we're being funny and only pressing modifiers
modKeyPress(key,0);
}
else if(instruction == "MOUSE") mouseControl(msg);
else if(instruction == "REM");
else if(instruction == "DELAY") delay(getInstruction(msg, 1).toInt());
else if(instruction == "DEFUALT_DELAY" || instruction == "DEFAULTDELAY" ) _parse_delay = getInstruction(msg, 1).toInt();
else if(instruction == "RESOLUTION") setScreenResolution(msg);
else if(instruction == "FILE") printFile(msg);
else if(instruction == "REPEAT") repeatPrevious(msg);
else Keyboard.print(msg);
_previous_msg = msg;
}