This repository was archived by the owner on Aug 28, 2025. It is now read-only.
forked from msommacal/duckuino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
453 lines (419 loc) · 11.9 KB
/
parser.y
File metadata and controls
453 lines (419 loc) · 11.9 KB
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
%{
/*
* This file is part of the Capibara zero (https://github.com/CapibaraZero/fw or https://capibarazero.github.io/).
* Copyright (c) 2020 msommacal
* Copyright (c) 2024 Andrea Canale.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string>
#include <string.h>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <map>
#include "usb_hid/USBHid.hpp"
#include "Arduino.h"
#include "SDBridge32.hpp"
std::map<std::string, int> num_constants;
std::map<std::string, std::string> string_constants;
#define KEY_SPACE 0x20
#define KEY_MENU 0x76+0x88
#define KEY_PAUSE 0x48+0x88
#define KEY_NUMLOCK 0x53+0x88
#define KEY_PRINTSCREEN 0x46+0x88
#define KEY_SCROLLLOCK 0x47+0x88
#ifdef ARDUINO_NANO_ESP32
#define SERIAL_DEVICE Serial
#else
//#define SERIAL_DEVICE Serial0
#define SERIAL_DEVICE Serial
#endif
/* Mock values */
#define LED_G_PIN 2
#define LED_R_PIN 3
extern "C" int yylex();
int yyerror(char *s);
extern FILE *yyin;
void print_default_delay();
USBHid hid = USBHid();
bool initialized = false;
#ifndef ARDUINO_ARCH_RP2040
SDBridge32 msc = SDBridge32();
#endif
int d = 0;
int rand_min = 0;
int rand_max = 65535;
bool button_enabled = false;
bool button_push_received = false;
char random_lowercase_letter() {
return rand() % (122 + 1 - 97) + 97;
}
char random_uppercase_letter() {
return rand() % (90 + 1 - 65) + 65;
}
char random_letter() {
return rand() % (122 + 1 - 65) + 65;
}
int random_number() {
return rand() % 10;
}
char random_special() {
int chars[10] = {33, 64, 35, 36, 37, 94, 38, 42, 40, 41};
return chars[rand() % 11];
}
int random_number_range() {
return rand() % (rand_max + 1 - rand_min) + rand_min;
}
bool find_and_replace_constant(char *str) {
const char *found_pos = NULL;
for(auto i = string_constants.begin(); i != string_constants.end(); i++){
if((found_pos = strstr(str, ("#" + i->first).c_str())) != NULL){
strcpy((char *)found_pos, i->second.c_str());
return true;
}
}
for(auto i = num_constants.begin(); i != num_constants.end(); i++){
if((found_pos = strstr(str, ("#" + i->first).c_str())) != NULL){
char int_ascii[1] = { (char)('0' + i->second) };
strcpy((char *)found_pos, int_ascii);
return true;
}
}
return false;
}
void initial_trim(char *str) {
for(int i = 0; i < strlen(str); i++) {
if(str[i] == ' ') {
memmove(str, str+1, strlen(str));
}else {
memmove(str, str+2, strlen(str));
return;
}
}
}
/* Convert modifiers to real value. */
uint8_t key_to_value(char *raw_val) {
if(!strcmp(raw_val, "UPARROW"))
return KEY_UP_ARROW;
if(!strcmp(raw_val, "DOWNARROW"))
return KEY_DOWN_ARROW;
if(!strcmp(raw_val, "LEFTARROW"))
return KEY_LEFT_ARROW;
if(!strcmp(raw_val, "RIGHTARROW"))
return KEY_RIGHT_ARROW;
if(!strcmp(raw_val, "PAGEUP"))
return KEY_PAGE_UP;
if(!strcmp(raw_val, "PAGEDOWN"))
return KEY_PAGE_DOWN;
if(!strcmp(raw_val, "HOME"))
return KEY_HOME;
if(!strcmp(raw_val, "END"))
return KEY_END;
if(!strcmp(raw_val, "INSERT"))
return KEY_INSERT;
if(!strcmp(raw_val, "DELETE"))
return KEY_DELETE;
if(!strcmp(raw_val, "BACKSPACE"))
return KEY_BACKSPACE;
if(!strcmp(raw_val, "TAB"))
return KEY_TAB;
if(!strcmp(raw_val, "SPACE"))
return KEY_SPACE;
if(!strcmp(raw_val, "ENTER"))
return KEY_RETURN;
if(!strcmp(raw_val, "ESCAPE"))
return 0;
if(!strcmp(raw_val, "PAUSE"))
return 0;
if(!strcmp(raw_val, "BREAK"))
return 0;
if(!strcmp(raw_val, "PRINTSCREEN"))
return 0;
if(!strcmp(raw_val, "MENU"))
return KEY_MENU;
if(!strcmp(raw_val, "APP"))
return 0;
if(!strcmp(raw_val, "F1"))
return KEY_F1;
if(!strcmp(raw_val, "F2"))
return KEY_F2;
if(!strcmp(raw_val, "F3"))
return KEY_F3;
if(!strcmp(raw_val, "F4"))
return KEY_F4;
if(!strcmp(raw_val, "F5"))
return KEY_F5;
if(!strcmp(raw_val, "F6"))
return KEY_F6;
if(!strcmp(raw_val, "F7"))
return KEY_F7;
if(!strcmp(raw_val, "F8"))
return KEY_F8;
if(!strcmp(raw_val, "F9"))
return KEY_F9;
if(!strcmp(raw_val, "F10"))
return KEY_F10;
if(!strcmp(raw_val, "F11"))
return KEY_F11;
if(!strcmp(raw_val, "F12"))
return KEY_F12;
if(!strcmp(raw_val, "SHIFT"))
return KEY_LEFT_SHIFT;
if(!strcmp(raw_val, "ALT"))
return KEY_LEFT_ALT;
if(!strcmp(raw_val, "CONTROL"))
return KEY_LEFT_CTRL;
if(!strcmp(raw_val, "CTRL"))
return KEY_LEFT_CTRL;
if(!strcmp(raw_val, "WINDOWS"))
return KEY_LEFT_GUI;
if(!strcmp(raw_val, "GUI"))
return KEY_LEFT_GUI;
return 1;
}
%}
%token alt altgr backspace default_delay delay_key menu pause_key capslock ctrl delete_key down end enter esc function gui home insert layout led_red led_green left letter numlock pagedown pageup printscreen repeat right scrolllock separator shift space string multiline_string multiline_stringln stringln tab up if_statement release hold
%token<s> num_var str_var num_define str_define math_operator end_if
%type<i> expr
// Attack mode
%type<s> modes
%token<s> attackmode
// Combo modifiers
%type<s> combos
%token<s> ctrl_alt ctrl_shift alt_shift
// Button
%type btn
%token wait_for_button_press enable_button disable_button
// Payload control
%type payload_control
%token restart_payload stop_payload reset
// Jitter
%type jitter
%token jitter_enabled_key jitter_max_key
// Randomization
%type randomization
%token random_lowercase_letter_keyword random_uppercase_letter_keyword random_letter_keyword random_number_keyword random_special_keyword random_char_keyword
// required to get text
%union {
char *text;
int integer;
}
%%
file: {} blocs {}
blocs: bloc blocs
| bloc
bloc: line repeat
| line
| expr
| modes
| combos
line: keys {
hid.release_all();
print_default_delay();
}
| delay_key {delay(yylval.integer);}
| default_delay {d = yylval.integer;}
| string {
hid.print_string(yylval.text);
print_default_delay();
}
| layout {
hid.begin(yylval.text);
}
| multiline_string {
initial_trim(yylval.text);
hid.print_string(yylval.text);
}
| multiline_stringln {
initial_trim(yylval.text);
hid.print_string(yylval.text);
hid.press(KEY_RETURN);
}
| stringln {hid.print_string(yylval.text);
print_default_delay();
}
| btn
| payload_control
| jitter
| randomization
keys: key separator keys
| key
key: alt {hid.press(KEY_LEFT_ALT);}
| altgr {hid.press(KEY_RIGHT_ALT);}
| backspace {hid.press(KEY_BACKSPACE);}
| menu {hid.press(KEY_MENU);}
| pause_key {hid.press(KEY_PAUSE);}
| capslock {hid.press(KEY_CAPS_LOCK);}
| ctrl {hid.press(KEY_LEFT_CTRL);}
| delete_key {hid.press(KEY_DELETE);}
| down {hid.press(KEY_DOWN_ARROW);}
| end {hid.press(KEY_END);}
| enter {hid.press(KEY_RETURN);}
| esc {hid.press(KEY_ESC);}
| function {
//0xC1 is F1(0xC2) - 1. So if we addiction function key number, we can obtain the keyboard code for function keys
hid.press(0xC1 + yylval.integer);
}
| gui {hid.press(KEY_LEFT_GUI);}
| home {hid.press(KEY_HOME);}
| insert {hid.press(KEY_INSERT);}
| left {hid.press(KEY_LEFT_ARROW);}
| numlock {hid.press(KEY_NUMLOCK);}
| pagedown {hid.press(KEY_PAGE_DOWN);}
| pageup {hid.press(KEY_PAGE_UP);}
| printscreen {hid.press(KEY_PRINTSCREEN);}
| right {hid.press(KEY_RIGHT_ARROW);}
| scrolllock {hid.press(KEY_SCROLLLOCK);}
| shift {hid.press(KEY_LEFT_SHIFT);}
| space {hid.press(' ');}
| tab {hid.press(KEY_TAB);}
| up {hid.press(KEY_UP_ARROW);}
| letter {hid.press(yylval.text[0]);}
| hold {
int value = key_to_value(yylval.text);
hid.press(value);
}
| release {
int value = key_to_value(yylval.text);
hid.release(value);
}
| led_green { digitalWrite(LED_R_PIN, LOW);
digitalWrite(LED_G_PIN, HIGH);
}
| led_red { digitalWrite(LED_G_PIN, LOW);
digitalWrite(LED_R_PIN, HIGH);
}
expr: num_var {SERIAL_DEVICE.printf("NOT IMPLEMENTED: int %s;", yylval.text);}
| str_var { SERIAL_DEVICE.printf("NOT IMPLEMENTED: std::string %s\";", yylval.text);}
| str_define {
std::stringstream define = std::stringstream(yylval.text);
std::string temp_str;
std::string define_name;
int i = 0;
while(getline(define, temp_str, ' ')) {
if(i++ == 0) {
define_name = temp_str;
} else if(i++ == 1)
continue;
}
string_constants[define_name] = temp_str;
}
| num_define {
std::stringstream define = std::stringstream(yylval.text);
std::string temp_str;
std::string define_name;
int i = 0;
while(getline(define, temp_str, ' ')) {
if(i++ == 0) {
define_name = temp_str;
} else if(i++ == 1)
continue;
}
num_constants[define_name] = atoi(temp_str.c_str());
}
| math_operator {SERIAL_DEVICE.printf("NOT IMPLEMENTED MATH OPERATOR %s;", yylval.text);}
| if_statement {SERIAL_DEVICE.printf("NOT IMPLEMENTED: if%s{\n", yylval.text);}
| end_if {SERIAL_DEVICE.printf("NOT IMPLEMENTED }\n");}
modes: attackmode {
if(strcmp(yylval.text, "STORAGE") == 0){
hid.end();
#ifndef ARDUINO_ARCH_RP2040
msc.begin("CapibaraZero", "DuckyESP", "1.1.0");
#endif
}
else if(strcmp(yylval.text, "HID") == 0) {
#ifndef ARDUINO_ARCH_RP2040
msc.end();
#endif
hid.begin();
}
else
yyerror("Invalid ATTACKMODE");
}
combos: ctrl_alt {
hid.press(KEY_LEFT_CTRL);
hid.press(KEY_LEFT_ALT);
hid.press(key_to_value(yylval.text));
hid.release_all();
}
| ctrl_shift {
hid.press(KEY_LEFT_CTRL);
hid.press(KEY_LEFT_SHIFT);
hid.press(key_to_value(yylval.text));
hid.release_all();
}
| alt_shift {
hid.press(KEY_LEFT_ALT);
hid.press(KEY_LEFT_SHIFT);
hid.press(key_to_value(yylval.text));
hid.release_all();
}
/* TODO: Make implementation of this */
btn: wait_for_button_press {SERIAL_DEVICE.println("NOT IMPLEMENTED: Wait for button press.\n");}
| enable_button {
button_enabled = true;
SERIAL_DEVICE.println("NOT IMPLEMENTED: Enable button.");
}
| disable_button {
button_enabled = false;
SERIAL_DEVICE.println("NOT IMPLEMENTED Disable button.");
}
payload_control: restart_payload {
SERIAL_DEVICE.println("NOT IMPLEMENTED: Restart payload");
rewind(yyin); // Reset FILE pointer to position 0
}
| stop_payload {
YYACCEPT; // Stop parser
}
| reset {
hid.release_all(); // Probably not the same meaning of DuckyScript
}
jitter: jitter_enabled_key {
if(strstr(yylval.text, "TRUE")!= NULL){
hid.set_jitter_status(true);
}else {
hid.set_jitter_status(false);
}
}
| jitter_max_key {
hid.set_jitter_level(atoi(yylval.text));
}
randomization: random_lowercase_letter_keyword {
hid.print_char(random_lowercase_letter());
}
| random_uppercase_letter_keyword {
hid.print_char(random_uppercase_letter());
}
| random_letter_keyword {
hid.print_char(random_letter());
}
| random_number_keyword {
hid.print_char(random_number());
}
| random_special_keyword {
hid.print_char(random_special());
}
| random_char_keyword {
SERIAL_DEVICE.println("NOT IMPLEMENTED: Keyboard.print(random_char());\n");
}
%%
int yyerror(char *s) {
SERIAL_DEVICE.printf("\033[31merror:\033[0m %s \033[0m",s);
raise(SIGSEGV);
return 0;
}
void print_default_delay() {
if (d != 0)
delay(d);
}