bad_usb_script.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <lib/toolbox/args.h>
  6. #include <furi_hal_usb_hid.h>
  7. #include <storage/storage.h>
  8. #include "bad_usb_script.h"
  9. #include <dolphin/dolphin.h>
  10. #define TAG "BadUSB"
  11. #define WORKER_TAG TAG "Worker"
  12. #define FILE_BUFFER_LEN 16
  13. #define SCRIPT_STATE_ERROR (-1)
  14. #define SCRIPT_STATE_END (-2)
  15. #define SCRIPT_STATE_NEXT_LINE (-3)
  16. typedef enum {
  17. WorkerEvtToggle = (1 << 0),
  18. WorkerEvtEnd = (1 << 1),
  19. WorkerEvtConnect = (1 << 2),
  20. WorkerEvtDisconnect = (1 << 3),
  21. } WorkerEvtFlags;
  22. struct BadUsbScript {
  23. FuriHalUsbHidConfig hid_cfg;
  24. BadUsbState st;
  25. FuriString* file_path;
  26. uint32_t defdelay;
  27. FuriThread* thread;
  28. uint8_t file_buf[FILE_BUFFER_LEN + 1];
  29. uint8_t buf_start;
  30. uint8_t buf_len;
  31. bool file_end;
  32. FuriString* line;
  33. FuriString* line_prev;
  34. uint32_t repeat_cnt;
  35. };
  36. typedef struct {
  37. char* name;
  38. uint16_t keycode;
  39. } DuckyKey;
  40. static const DuckyKey ducky_keys[] = {
  41. {"CTRL-ALT", KEY_MOD_LEFT_CTRL | KEY_MOD_LEFT_ALT},
  42. {"CTRL-SHIFT", KEY_MOD_LEFT_CTRL | KEY_MOD_LEFT_SHIFT},
  43. {"ALT-SHIFT", KEY_MOD_LEFT_ALT | KEY_MOD_LEFT_SHIFT},
  44. {"ALT-GUI", KEY_MOD_LEFT_ALT | KEY_MOD_LEFT_GUI},
  45. {"GUI-SHIFT", KEY_MOD_LEFT_GUI | KEY_MOD_LEFT_SHIFT},
  46. {"CTRL", KEY_MOD_LEFT_CTRL},
  47. {"CONTROL", KEY_MOD_LEFT_CTRL},
  48. {"SHIFT", KEY_MOD_LEFT_SHIFT},
  49. {"ALT", KEY_MOD_LEFT_ALT},
  50. {"GUI", KEY_MOD_LEFT_GUI},
  51. {"WINDOWS", KEY_MOD_LEFT_GUI},
  52. {"DOWNARROW", HID_KEYBOARD_DOWN_ARROW},
  53. {"DOWN", HID_KEYBOARD_DOWN_ARROW},
  54. {"LEFTARROW", HID_KEYBOARD_LEFT_ARROW},
  55. {"LEFT", HID_KEYBOARD_LEFT_ARROW},
  56. {"RIGHTARROW", HID_KEYBOARD_RIGHT_ARROW},
  57. {"RIGHT", HID_KEYBOARD_RIGHT_ARROW},
  58. {"UPARROW", HID_KEYBOARD_UP_ARROW},
  59. {"UP", HID_KEYBOARD_UP_ARROW},
  60. {"ENTER", HID_KEYBOARD_RETURN},
  61. {"BREAK", HID_KEYBOARD_PAUSE},
  62. {"PAUSE", HID_KEYBOARD_PAUSE},
  63. {"CAPSLOCK", HID_KEYBOARD_CAPS_LOCK},
  64. {"DELETE", HID_KEYBOARD_DELETE},
  65. {"BACKSPACE", HID_KEYPAD_BACKSPACE},
  66. {"END", HID_KEYBOARD_END},
  67. {"ESC", HID_KEYBOARD_ESCAPE},
  68. {"ESCAPE", HID_KEYBOARD_ESCAPE},
  69. {"HOME", HID_KEYBOARD_HOME},
  70. {"INSERT", HID_KEYBOARD_INSERT},
  71. {"NUMLOCK", HID_KEYPAD_NUMLOCK},
  72. {"PAGEUP", HID_KEYBOARD_PAGE_UP},
  73. {"PAGEDOWN", HID_KEYBOARD_PAGE_DOWN},
  74. {"PRINTSCREEN", HID_KEYBOARD_PRINT_SCREEN},
  75. {"SCROLLOCK", HID_KEYBOARD_SCROLL_LOCK},
  76. {"SPACE", HID_KEYBOARD_SPACEBAR},
  77. {"TAB", HID_KEYBOARD_TAB},
  78. {"MENU", HID_KEYBOARD_APPLICATION},
  79. {"APP", HID_KEYBOARD_APPLICATION},
  80. {"F1", HID_KEYBOARD_F1},
  81. {"F2", HID_KEYBOARD_F2},
  82. {"F3", HID_KEYBOARD_F3},
  83. {"F4", HID_KEYBOARD_F4},
  84. {"F5", HID_KEYBOARD_F5},
  85. {"F6", HID_KEYBOARD_F6},
  86. {"F7", HID_KEYBOARD_F7},
  87. {"F8", HID_KEYBOARD_F8},
  88. {"F9", HID_KEYBOARD_F9},
  89. {"F10", HID_KEYBOARD_F10},
  90. {"F11", HID_KEYBOARD_F11},
  91. {"F12", HID_KEYBOARD_F12},
  92. };
  93. static const char ducky_cmd_comment[] = {"REM"};
  94. static const char ducky_cmd_id[] = {"ID"};
  95. static const char ducky_cmd_delay[] = {"DELAY "};
  96. static const char ducky_cmd_string[] = {"STRING "};
  97. static const char ducky_cmd_defdelay_1[] = {"DEFAULT_DELAY "};
  98. static const char ducky_cmd_defdelay_2[] = {"DEFAULTDELAY "};
  99. static const char ducky_cmd_repeat[] = {"REPEAT "};
  100. static const char ducky_cmd_sysrq[] = {"SYSRQ "};
  101. static const char ducky_cmd_altchar[] = {"ALTCHAR "};
  102. static const char ducky_cmd_altstr_1[] = {"ALTSTRING "};
  103. static const char ducky_cmd_altstr_2[] = {"ALTCODE "};
  104. static const uint8_t numpad_keys[10] = {
  105. HID_KEYPAD_0,
  106. HID_KEYPAD_1,
  107. HID_KEYPAD_2,
  108. HID_KEYPAD_3,
  109. HID_KEYPAD_4,
  110. HID_KEYPAD_5,
  111. HID_KEYPAD_6,
  112. HID_KEYPAD_7,
  113. HID_KEYPAD_8,
  114. HID_KEYPAD_9,
  115. };
  116. static bool ducky_get_number(const char* param, uint32_t* val) {
  117. uint32_t value = 0;
  118. if(sscanf(param, "%lu", &value) == 1) {
  119. *val = value;
  120. return true;
  121. }
  122. return false;
  123. }
  124. static uint32_t ducky_get_command_len(const char* line) {
  125. uint32_t len = strlen(line);
  126. for(uint32_t i = 0; i < len; i++) {
  127. if(line[i] == ' ') return i;
  128. }
  129. return 0;
  130. }
  131. static bool ducky_is_line_end(const char chr) {
  132. return ((chr == ' ') || (chr == '\0') || (chr == '\r') || (chr == '\n'));
  133. }
  134. static void ducky_numlock_on() {
  135. if((furi_hal_hid_get_led_state() & HID_KB_LED_NUM) == 0) {
  136. furi_hal_hid_kb_press(HID_KEYBOARD_LOCK_NUM_LOCK);
  137. furi_hal_hid_kb_release(HID_KEYBOARD_LOCK_NUM_LOCK);
  138. }
  139. }
  140. static bool ducky_numpad_press(const char num) {
  141. if((num < '0') || (num > '9')) return false;
  142. uint16_t key = numpad_keys[num - '0'];
  143. furi_hal_hid_kb_press(key);
  144. furi_hal_hid_kb_release(key);
  145. return true;
  146. }
  147. static bool ducky_altchar(const char* charcode) {
  148. uint8_t i = 0;
  149. bool state = false;
  150. FURI_LOG_I(WORKER_TAG, "char %s", charcode);
  151. furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT);
  152. while(!ducky_is_line_end(charcode[i])) {
  153. state = ducky_numpad_press(charcode[i]);
  154. if(state == false) break;
  155. i++;
  156. }
  157. furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT);
  158. return state;
  159. }
  160. static bool ducky_altstring(const char* param) {
  161. uint32_t i = 0;
  162. bool state = false;
  163. while(param[i] != '\0') {
  164. if((param[i] < ' ') || (param[i] > '~')) {
  165. i++;
  166. continue; // Skip non-printable chars
  167. }
  168. char temp_str[4];
  169. snprintf(temp_str, 4, "%u", param[i]);
  170. state = ducky_altchar(temp_str);
  171. if(state == false) break;
  172. i++;
  173. }
  174. return state;
  175. }
  176. static bool ducky_string(const char* param) {
  177. uint32_t i = 0;
  178. while(param[i] != '\0') {
  179. uint16_t keycode = HID_ASCII_TO_KEY(param[i]);
  180. if(keycode != HID_KEYBOARD_NONE) {
  181. furi_hal_hid_kb_press(keycode);
  182. furi_hal_hid_kb_release(keycode);
  183. }
  184. i++;
  185. }
  186. return true;
  187. }
  188. static uint16_t ducky_get_keycode(const char* param, bool accept_chars) {
  189. for(uint8_t i = 0; i < (sizeof(ducky_keys) / sizeof(ducky_keys[0])); i++) {
  190. uint8_t key_cmd_len = strlen(ducky_keys[i].name);
  191. if((strncmp(param, ducky_keys[i].name, key_cmd_len) == 0) &&
  192. (ducky_is_line_end(param[key_cmd_len]))) {
  193. return ducky_keys[i].keycode;
  194. }
  195. }
  196. if((accept_chars) && (strlen(param) > 0)) {
  197. return (HID_ASCII_TO_KEY(param[0]) & 0xFF);
  198. }
  199. return 0;
  200. }
  201. static int32_t
  202. ducky_parse_line(BadUsbScript* bad_usb, FuriString* line, char* error, size_t error_len) {
  203. uint32_t line_len = furi_string_size(line);
  204. const char* line_tmp = furi_string_get_cstr(line);
  205. bool state = false;
  206. for(uint32_t i = 0; i < line_len; i++) {
  207. if((line_tmp[i] != ' ') && (line_tmp[i] != '\t') && (line_tmp[i] != '\n')) {
  208. line_tmp = &line_tmp[i];
  209. break; // Skip spaces and tabs
  210. }
  211. if(i == line_len - 1) return SCRIPT_STATE_NEXT_LINE; // Skip empty lines
  212. }
  213. FURI_LOG_D(WORKER_TAG, "line:%s", line_tmp);
  214. // General commands
  215. if(strncmp(line_tmp, ducky_cmd_comment, strlen(ducky_cmd_comment)) == 0) {
  216. // REM - comment line
  217. return (0);
  218. } else if(strncmp(line_tmp, ducky_cmd_id, strlen(ducky_cmd_id)) == 0) {
  219. // ID - executed in ducky_script_preload
  220. return (0);
  221. } else if(strncmp(line_tmp, ducky_cmd_delay, strlen(ducky_cmd_delay)) == 0) {
  222. // DELAY
  223. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  224. uint32_t delay_val = 0;
  225. state = ducky_get_number(line_tmp, &delay_val);
  226. if((state) && (delay_val > 0)) {
  227. return (int32_t)delay_val;
  228. }
  229. if(error != NULL) {
  230. snprintf(error, error_len, "Invalid number %s", line_tmp);
  231. }
  232. return SCRIPT_STATE_ERROR;
  233. } else if(
  234. (strncmp(line_tmp, ducky_cmd_defdelay_1, strlen(ducky_cmd_defdelay_1)) == 0) ||
  235. (strncmp(line_tmp, ducky_cmd_defdelay_2, strlen(ducky_cmd_defdelay_2)) == 0)) {
  236. // DEFAULT_DELAY
  237. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  238. state = ducky_get_number(line_tmp, &bad_usb->defdelay);
  239. if(!state && error != NULL) {
  240. snprintf(error, error_len, "Invalid number %s", line_tmp);
  241. }
  242. return (state) ? (0) : SCRIPT_STATE_ERROR;
  243. } else if(strncmp(line_tmp, ducky_cmd_string, strlen(ducky_cmd_string)) == 0) {
  244. // STRING
  245. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  246. state = ducky_string(line_tmp);
  247. if(!state && error != NULL) {
  248. snprintf(error, error_len, "Invalid string %s", line_tmp);
  249. }
  250. return (state) ? (0) : SCRIPT_STATE_ERROR;
  251. } else if(strncmp(line_tmp, ducky_cmd_altchar, strlen(ducky_cmd_altchar)) == 0) {
  252. // ALTCHAR
  253. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  254. ducky_numlock_on();
  255. state = ducky_altchar(line_tmp);
  256. if(!state && error != NULL) {
  257. snprintf(error, error_len, "Invalid altchar %s", line_tmp);
  258. }
  259. return (state) ? (0) : SCRIPT_STATE_ERROR;
  260. } else if(
  261. (strncmp(line_tmp, ducky_cmd_altstr_1, strlen(ducky_cmd_altstr_1)) == 0) ||
  262. (strncmp(line_tmp, ducky_cmd_altstr_2, strlen(ducky_cmd_altstr_2)) == 0)) {
  263. // ALTSTRING
  264. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  265. ducky_numlock_on();
  266. state = ducky_altstring(line_tmp);
  267. if(!state && error != NULL) {
  268. snprintf(error, error_len, "Invalid altstring %s", line_tmp);
  269. }
  270. return (state) ? (0) : SCRIPT_STATE_ERROR;
  271. } else if(strncmp(line_tmp, ducky_cmd_repeat, strlen(ducky_cmd_repeat)) == 0) {
  272. // REPEAT
  273. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  274. state = ducky_get_number(line_tmp, &bad_usb->repeat_cnt);
  275. if(!state && error != NULL) {
  276. snprintf(error, error_len, "Invalid number %s", line_tmp);
  277. }
  278. return (state) ? (0) : SCRIPT_STATE_ERROR;
  279. } else if(strncmp(line_tmp, ducky_cmd_sysrq, strlen(ducky_cmd_sysrq)) == 0) {
  280. // SYSRQ
  281. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  282. uint16_t key = ducky_get_keycode(line_tmp, true);
  283. furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT | HID_KEYBOARD_PRINT_SCREEN);
  284. furi_hal_hid_kb_press(key);
  285. furi_hal_hid_kb_release_all();
  286. return (0);
  287. } else {
  288. // Special keys + modifiers
  289. uint16_t key = ducky_get_keycode(line_tmp, false);
  290. if(key == HID_KEYBOARD_NONE) {
  291. if(error != NULL) {
  292. snprintf(error, error_len, "No keycode defined for %s", line_tmp);
  293. }
  294. return SCRIPT_STATE_ERROR;
  295. }
  296. if((key & 0xFF00) != 0) {
  297. // It's a modifier key
  298. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  299. key |= ducky_get_keycode(line_tmp, true);
  300. }
  301. furi_hal_hid_kb_press(key);
  302. furi_hal_hid_kb_release(key);
  303. return (0);
  304. }
  305. if(error != NULL) {
  306. strncpy(error, "Unknown error", error_len);
  307. }
  308. return SCRIPT_STATE_ERROR;
  309. }
  310. static bool ducky_set_usb_id(BadUsbScript* bad_usb, const char* line) {
  311. if(sscanf(line, "%lX:%lX", &bad_usb->hid_cfg.vid, &bad_usb->hid_cfg.pid) == 2) {
  312. bad_usb->hid_cfg.manuf[0] = '\0';
  313. bad_usb->hid_cfg.product[0] = '\0';
  314. uint8_t id_len = ducky_get_command_len(line);
  315. if(!ducky_is_line_end(line[id_len + 1])) {
  316. sscanf(
  317. &line[id_len + 1],
  318. "%31[^\r\n:]:%31[^\r\n]",
  319. bad_usb->hid_cfg.manuf,
  320. bad_usb->hid_cfg.product);
  321. }
  322. FURI_LOG_D(
  323. WORKER_TAG,
  324. "set id: %04lX:%04lX mfr:%s product:%s",
  325. bad_usb->hid_cfg.vid,
  326. bad_usb->hid_cfg.pid,
  327. bad_usb->hid_cfg.manuf,
  328. bad_usb->hid_cfg.product);
  329. return true;
  330. }
  331. return false;
  332. }
  333. static bool ducky_script_preload(BadUsbScript* bad_usb, File* script_file) {
  334. uint8_t ret = 0;
  335. uint32_t line_len = 0;
  336. furi_string_reset(bad_usb->line);
  337. do {
  338. ret = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
  339. for(uint16_t i = 0; i < ret; i++) {
  340. if(bad_usb->file_buf[i] == '\n' && line_len > 0) {
  341. bad_usb->st.line_nb++;
  342. line_len = 0;
  343. } else {
  344. if(bad_usb->st.line_nb == 0) { // Save first line
  345. furi_string_push_back(bad_usb->line, bad_usb->file_buf[i]);
  346. }
  347. line_len++;
  348. }
  349. }
  350. if(storage_file_eof(script_file)) {
  351. if(line_len > 0) {
  352. bad_usb->st.line_nb++;
  353. break;
  354. }
  355. }
  356. } while(ret > 0);
  357. const char* line_tmp = furi_string_get_cstr(bad_usb->line);
  358. bool id_set = false; // Looking for ID command at first line
  359. if(strncmp(line_tmp, ducky_cmd_id, strlen(ducky_cmd_id)) == 0) {
  360. id_set = ducky_set_usb_id(bad_usb, &line_tmp[strlen(ducky_cmd_id) + 1]);
  361. }
  362. if(id_set) {
  363. furi_check(furi_hal_usb_set_config(&usb_hid, &bad_usb->hid_cfg));
  364. } else {
  365. furi_check(furi_hal_usb_set_config(&usb_hid, NULL));
  366. }
  367. storage_file_seek(script_file, 0, true);
  368. furi_string_reset(bad_usb->line);
  369. return true;
  370. }
  371. static int32_t ducky_script_execute_next(BadUsbScript* bad_usb, File* script_file) {
  372. int32_t delay_val = 0;
  373. if(bad_usb->repeat_cnt > 0) {
  374. bad_usb->repeat_cnt--;
  375. delay_val = ducky_parse_line(
  376. bad_usb, bad_usb->line_prev, bad_usb->st.error, sizeof(bad_usb->st.error));
  377. if(delay_val == SCRIPT_STATE_NEXT_LINE) { // Empty line
  378. return 0;
  379. } else if(delay_val < 0) { // Script error
  380. bad_usb->st.error_line = bad_usb->st.line_cur - 1;
  381. FURI_LOG_E(WORKER_TAG, "Unknown command at line %u", bad_usb->st.line_cur - 1);
  382. return SCRIPT_STATE_ERROR;
  383. } else {
  384. return (delay_val + bad_usb->defdelay);
  385. }
  386. }
  387. furi_string_set(bad_usb->line_prev, bad_usb->line);
  388. furi_string_reset(bad_usb->line);
  389. while(1) {
  390. if(bad_usb->buf_len == 0) {
  391. bad_usb->buf_len = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
  392. if(storage_file_eof(script_file)) {
  393. if((bad_usb->buf_len < FILE_BUFFER_LEN) && (bad_usb->file_end == false)) {
  394. bad_usb->file_buf[bad_usb->buf_len] = '\n';
  395. bad_usb->buf_len++;
  396. bad_usb->file_end = true;
  397. }
  398. }
  399. bad_usb->buf_start = 0;
  400. if(bad_usb->buf_len == 0) return SCRIPT_STATE_END;
  401. }
  402. for(uint8_t i = bad_usb->buf_start; i < (bad_usb->buf_start + bad_usb->buf_len); i++) {
  403. if(bad_usb->file_buf[i] == '\n' && furi_string_size(bad_usb->line) > 0) {
  404. bad_usb->st.line_cur++;
  405. bad_usb->buf_len = bad_usb->buf_len + bad_usb->buf_start - (i + 1);
  406. bad_usb->buf_start = i + 1;
  407. delay_val = ducky_parse_line(
  408. bad_usb, bad_usb->line, bad_usb->st.error, sizeof(bad_usb->st.error));
  409. if(delay_val < 0) {
  410. bad_usb->st.error_line = bad_usb->st.line_cur;
  411. FURI_LOG_E(WORKER_TAG, "Unknown command at line %u", bad_usb->st.line_cur);
  412. return SCRIPT_STATE_ERROR;
  413. } else {
  414. return (delay_val + bad_usb->defdelay);
  415. }
  416. } else {
  417. furi_string_push_back(bad_usb->line, bad_usb->file_buf[i]);
  418. }
  419. }
  420. bad_usb->buf_len = 0;
  421. if(bad_usb->file_end) return SCRIPT_STATE_END;
  422. }
  423. return 0;
  424. }
  425. static void bad_usb_hid_state_callback(bool state, void* context) {
  426. furi_assert(context);
  427. BadUsbScript* bad_usb = context;
  428. if(state == true)
  429. furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtConnect);
  430. else
  431. furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtDisconnect);
  432. }
  433. static int32_t bad_usb_worker(void* context) {
  434. BadUsbScript* bad_usb = context;
  435. BadUsbWorkerState worker_state = BadUsbStateInit;
  436. int32_t delay_val = 0;
  437. FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
  438. FURI_LOG_I(WORKER_TAG, "Init");
  439. File* script_file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
  440. bad_usb->line = furi_string_alloc();
  441. bad_usb->line_prev = furi_string_alloc();
  442. furi_hal_hid_set_state_callback(bad_usb_hid_state_callback, bad_usb);
  443. while(1) {
  444. if(worker_state == BadUsbStateInit) { // State: initialization
  445. if(storage_file_open(
  446. script_file,
  447. furi_string_get_cstr(bad_usb->file_path),
  448. FSAM_READ,
  449. FSOM_OPEN_EXISTING)) {
  450. if((ducky_script_preload(bad_usb, script_file)) && (bad_usb->st.line_nb > 0)) {
  451. if(furi_hal_hid_is_connected()) {
  452. worker_state = BadUsbStateIdle; // Ready to run
  453. } else {
  454. worker_state = BadUsbStateNotConnected; // USB not connected
  455. }
  456. } else {
  457. worker_state = BadUsbStateScriptError; // Script preload error
  458. }
  459. } else {
  460. FURI_LOG_E(WORKER_TAG, "File open error");
  461. worker_state = BadUsbStateFileError; // File open error
  462. }
  463. bad_usb->st.state = worker_state;
  464. } else if(worker_state == BadUsbStateNotConnected) { // State: USB not connected
  465. uint32_t flags = furi_thread_flags_wait(
  466. WorkerEvtEnd | WorkerEvtConnect, FuriFlagWaitAny, FuriWaitForever);
  467. furi_check((flags & FuriFlagError) == 0);
  468. if(flags & WorkerEvtEnd) {
  469. break;
  470. } else if(flags & WorkerEvtConnect) {
  471. worker_state = BadUsbStateIdle; // Ready to run
  472. }
  473. bad_usb->st.state = worker_state;
  474. } else if(worker_state == BadUsbStateIdle) { // State: ready to start
  475. uint32_t flags = furi_thread_flags_wait(
  476. WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect,
  477. FuriFlagWaitAny,
  478. FuriWaitForever);
  479. furi_check((flags & FuriFlagError) == 0);
  480. if(flags & WorkerEvtEnd) {
  481. break;
  482. } else if(flags & WorkerEvtToggle) { // Start executing script
  483. DOLPHIN_DEED(DolphinDeedBadUsbPlayScript);
  484. delay_val = 0;
  485. bad_usb->buf_len = 0;
  486. bad_usb->st.line_cur = 0;
  487. bad_usb->defdelay = 0;
  488. bad_usb->repeat_cnt = 0;
  489. bad_usb->file_end = false;
  490. storage_file_seek(script_file, 0, true);
  491. worker_state = BadUsbStateRunning;
  492. } else if(flags & WorkerEvtDisconnect) {
  493. worker_state = BadUsbStateNotConnected; // USB disconnected
  494. }
  495. bad_usb->st.state = worker_state;
  496. } else if(worker_state == BadUsbStateRunning) { // State: running
  497. uint16_t delay_cur = (delay_val > 1000) ? (1000) : (delay_val);
  498. uint32_t flags = furi_thread_flags_wait(
  499. WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect, FuriFlagWaitAny, delay_cur);
  500. delay_val -= delay_cur;
  501. if(!(flags & FuriFlagError)) {
  502. if(flags & WorkerEvtEnd) {
  503. break;
  504. } else if(flags & WorkerEvtToggle) {
  505. worker_state = BadUsbStateIdle; // Stop executing script
  506. furi_hal_hid_kb_release_all();
  507. } else if(flags & WorkerEvtDisconnect) {
  508. worker_state = BadUsbStateNotConnected; // USB disconnected
  509. furi_hal_hid_kb_release_all();
  510. }
  511. bad_usb->st.state = worker_state;
  512. continue;
  513. } else if((flags == FuriFlagErrorTimeout) || (flags == FuriFlagErrorResource)) {
  514. if(delay_val > 0) {
  515. bad_usb->st.delay_remain--;
  516. continue;
  517. }
  518. bad_usb->st.state = BadUsbStateRunning;
  519. delay_val = ducky_script_execute_next(bad_usb, script_file);
  520. if(delay_val == SCRIPT_STATE_ERROR) { // Script error
  521. delay_val = 0;
  522. worker_state = BadUsbStateScriptError;
  523. bad_usb->st.state = worker_state;
  524. } else if(delay_val == SCRIPT_STATE_END) { // End of script
  525. delay_val = 0;
  526. worker_state = BadUsbStateIdle;
  527. bad_usb->st.state = BadUsbStateDone;
  528. furi_hal_hid_kb_release_all();
  529. continue;
  530. } else if(delay_val > 1000) {
  531. bad_usb->st.state = BadUsbStateDelay; // Show long delays
  532. bad_usb->st.delay_remain = delay_val / 1000;
  533. }
  534. } else {
  535. furi_check((flags & FuriFlagError) == 0);
  536. }
  537. } else if(
  538. (worker_state == BadUsbStateFileError) ||
  539. (worker_state == BadUsbStateScriptError)) { // State: error
  540. uint32_t flags = furi_thread_flags_wait(
  541. WorkerEvtEnd, FuriFlagWaitAny, FuriWaitForever); // Waiting for exit command
  542. furi_check((flags & FuriFlagError) == 0);
  543. if(flags & WorkerEvtEnd) {
  544. break;
  545. }
  546. }
  547. }
  548. furi_hal_hid_set_state_callback(NULL, NULL);
  549. furi_hal_usb_set_config(usb_mode_prev, NULL);
  550. storage_file_close(script_file);
  551. storage_file_free(script_file);
  552. furi_string_free(bad_usb->line);
  553. furi_string_free(bad_usb->line_prev);
  554. FURI_LOG_I(WORKER_TAG, "End");
  555. return 0;
  556. }
  557. BadUsbScript* bad_usb_script_open(FuriString* file_path) {
  558. furi_assert(file_path);
  559. BadUsbScript* bad_usb = malloc(sizeof(BadUsbScript));
  560. bad_usb->file_path = furi_string_alloc();
  561. furi_string_set(bad_usb->file_path, file_path);
  562. bad_usb->st.state = BadUsbStateInit;
  563. bad_usb->st.error[0] = '\0';
  564. bad_usb->thread = furi_thread_alloc();
  565. furi_thread_set_name(bad_usb->thread, "BadUsbWorker");
  566. furi_thread_set_stack_size(bad_usb->thread, 2048);
  567. furi_thread_set_context(bad_usb->thread, bad_usb);
  568. furi_thread_set_callback(bad_usb->thread, bad_usb_worker);
  569. furi_thread_start(bad_usb->thread);
  570. return bad_usb;
  571. }
  572. void bad_usb_script_close(BadUsbScript* bad_usb) {
  573. furi_assert(bad_usb);
  574. furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtEnd);
  575. furi_thread_join(bad_usb->thread);
  576. furi_thread_free(bad_usb->thread);
  577. furi_string_free(bad_usb->file_path);
  578. free(bad_usb);
  579. }
  580. void bad_usb_script_toggle(BadUsbScript* bad_usb) {
  581. furi_assert(bad_usb);
  582. furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtToggle);
  583. }
  584. BadUsbState* bad_usb_script_get_state(BadUsbScript* bad_usb) {
  585. furi_assert(bad_usb);
  586. return &(bad_usb->st);
  587. }