From 3dc77662cb807277cb781ddd492531d9c3dc68d3 Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Wed, 13 Nov 2024 00:08:35 -0600 Subject: [PATCH 01/11] add usb hid mouse functions add mouse functions to BadUsbHidApi --- .../main/bad_usb/helpers/bad_usb_hid.c | 24 +++++++++++++++++++ .../main/bad_usb/helpers/bad_usb_hid.h | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/applications/main/bad_usb/helpers/bad_usb_hid.c b/applications/main/bad_usb/helpers/bad_usb_hid.c index 5d7076314af..ff2077c958e 100644 --- a/applications/main/bad_usb/helpers/bad_usb_hid.c +++ b/applications/main/bad_usb/helpers/bad_usb_hid.c @@ -37,6 +37,26 @@ bool hid_usb_kb_release(void* inst, uint16_t button) { return furi_hal_hid_kb_release(button); } +bool hid_usb_mouse_press(void* inst, uint8_t button) { + UNUSED(inst); + return furi_hal_hid_mouse_press(button); +} + +bool hid_usb_mouse_release(void* inst, uint8_t button) { + UNUSED(inst); + return furi_hal_hid_mouse_release(button); +} + +bool hid_usb_mouse_scroll(void* inst, int8_t delta) { + UNUSED(inst); + return furi_hal_hid_mouse_scroll(delta); +} + +bool hid_usb_mouse_move(void* inst, int8_t dx, int8_t dy) { + UNUSED(inst); + return furi_hal_hid_mouse_move(dx, dy) +} + bool hid_usb_consumer_press(void* inst, uint16_t button) { UNUSED(inst); return furi_hal_hid_consumer_key_press(button); @@ -67,6 +87,10 @@ static const BadUsbHidApi hid_api_usb = { .kb_press = hid_usb_kb_press, .kb_release = hid_usb_kb_release, + .mouse_press = hid_usb_mouse_press, + .mouse_release = hid_usb_mouse_release, + .mouse_scroll = hid_usb_mouse_scroll, + .mouse_move = hid_usb_mouse_move, .consumer_press = hid_usb_consumer_press, .consumer_release = hid_usb_consumer_release, .release_all = hid_usb_release_all, diff --git a/applications/main/bad_usb/helpers/bad_usb_hid.h b/applications/main/bad_usb/helpers/bad_usb_hid.h index 71d3a58e793..e4758ab68c1 100644 --- a/applications/main/bad_usb/helpers/bad_usb_hid.h +++ b/applications/main/bad_usb/helpers/bad_usb_hid.h @@ -20,6 +20,10 @@ typedef struct { bool (*kb_press)(void* inst, uint16_t button); bool (*kb_release)(void* inst, uint16_t button); + bool (*mouse_press)(void* inst, uint8_t button); + bool (*mouse_release)(void* inst, uint8_t button); + bool (*mouse_scroll)(void* inst, int8_t delta); + bool (*mouse_move)(void* inst, int8_t dx, int8_t dy); bool (*consumer_press)(void* inst, uint16_t button); bool (*consumer_release)(void* inst, uint16_t button); bool (*release_all)(void* inst); From 7b5f598766bc9e7c19a1c2c98213f4d64bf9f14d Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Wed, 13 Nov 2024 00:25:45 -0600 Subject: [PATCH 02/11] add ble mouse functionality --- .../main/bad_usb/helpers/bad_usb_hid.c | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/applications/main/bad_usb/helpers/bad_usb_hid.c b/applications/main/bad_usb/helpers/bad_usb_hid.c index ff2077c958e..1b8ce6a6e89 100644 --- a/applications/main/bad_usb/helpers/bad_usb_hid.c +++ b/applications/main/bad_usb/helpers/bad_usb_hid.c @@ -181,6 +181,27 @@ bool hid_ble_kb_release(void* inst, uint16_t button) { return ble_profile_hid_kb_release(ble_hid->profile, button); } +bool hid_ble_mouse_press(void* inst, uint8_t button) { + BleHidInstance* ble_hid = inst; + furi_assert(ble_hid); + return ble_profile_hid_mouse_press(ble_hid->profile, button); +} +bool hid_ble_mouse_release(void* inst, uint8_t button) { + BleHidInstance* ble_hid = inst; + furi_assert(ble_hid); + return ble_profile_hid_mouse_release(ble_hid->profile, button); +} +bool hid_ble_mouse_scroll(void* inst, int8_t delta) { + BleHidInstance* ble_hid = inst; + furi_assert(ble_hid); + return ble_profile_hid_mouse_scroll(ble_hid->profile, delta); +} +bool hid_ble_mouse_move(void* inst, int8_t dx, int8_t dy) { + BleHidInstance* ble_hid = inst; + furi_assert(ble_hid); + return ble_profile_hid_mouse_move(ble_hid->profile, dx, dy); +} + bool hid_ble_consumer_press(void* inst, uint16_t button) { BleHidInstance* ble_hid = inst; furi_assert(ble_hid); @@ -198,6 +219,7 @@ bool hid_ble_release_all(void* inst) { furi_assert(ble_hid); bool state = ble_profile_hid_kb_release_all(ble_hid->profile); state &= ble_profile_hid_consumer_key_release_all(ble_hid->profile); + state &= ble_profile_hid_mouse_release_all(ble_hid->profile); return state; } @@ -215,6 +237,10 @@ static const BadUsbHidApi hid_api_ble = { .kb_press = hid_ble_kb_press, .kb_release = hid_ble_kb_release, + .mouse_press = hid_ble_mouse_press, + .mouse_release = hid_ble_mouse_release, + .mouse_scroll = hid_ble_mouse_scroll, + .mouse_move = hid_ble_mouse_move, .consumer_press = hid_ble_consumer_press, .consumer_release = hid_ble_consumer_release, .release_all = hid_ble_release_all, From a950c25b7eb256277cc8773f1787242937709821 Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Wed, 13 Nov 2024 00:50:44 -0600 Subject: [PATCH 03/11] add hid_usb_mouse_release_all --- applications/main/bad_usb/helpers/bad_usb_hid.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/applications/main/bad_usb/helpers/bad_usb_hid.c b/applications/main/bad_usb/helpers/bad_usb_hid.c index 1b8ce6a6e89..c2ee28b4363 100644 --- a/applications/main/bad_usb/helpers/bad_usb_hid.c +++ b/applications/main/bad_usb/helpers/bad_usb_hid.c @@ -57,6 +57,11 @@ bool hid_usb_mouse_move(void* inst, int8_t dx, int8_t dy) { return furi_hal_hid_mouse_move(dx, dy) } +bool hid_usb_mouse_release_all(void* inst) { + UNUSED(inst); + return furi_hal_hid_mouse_release(0); +} + bool hid_usb_consumer_press(void* inst, uint16_t button) { UNUSED(inst); return furi_hal_hid_consumer_key_press(button); @@ -71,6 +76,7 @@ bool hid_usb_release_all(void* inst) { UNUSED(inst); bool state = furi_hal_hid_kb_release_all(); state &= furi_hal_hid_consumer_key_release_all(); + state &= hid_usb_mouse_release_all(inst); return state; } From 757fbd19a3bf65730dc69109860132ff7e12f6c3 Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Wed, 13 Nov 2024 20:04:54 -0600 Subject: [PATCH 04/11] ducky mouse command skeleton --- .../bad_usb/helpers/ducky_script_commands.c | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/applications/main/bad_usb/helpers/ducky_script_commands.c b/applications/main/bad_usb/helpers/ducky_script_commands.c index 79dcdd531de..7ef487ed825 100644 --- a/applications/main/bad_usb/helpers/ducky_script_commands.c +++ b/applications/main/bad_usb/helpers/ducky_script_commands.c @@ -191,6 +191,31 @@ static int32_t ducky_fnc_waitforbutton(BadUsbScript* bad_usb, const char* line, return SCRIPT_STATE_WAIT_FOR_BTN; } +static int32_t ducky_fnc_mouse(BadUsbScript* bad_usb, const char* line, int32_T param) { + UNUSED(param); + + return SCRIPT_STATE_CMD_UNKNOWN; // not yet implemented +} + +static int32_t ducky_fnc_mouse_click(BadUsbScript* bad_usb, const char* line, int32_T param) { + UNUSED(param); + UNUSED(bad_usb); + UNUSED(line); + + // param == 0 is a mouse click + // param == 1 is a mouse scroll + + return SCRIPT_STATE_CMD_UNKNOWN; // not yet implemented +} + +static int32_t ducky_fnc_mouse_move(BadUsbScript* bad_usb, const char* line, int32_T param) { + UNUSED(param); + UNUSED(bad_usb); + UNUSED(line); + + return SCRIPT_STATE_CMD_UNKNOWN; // not yet implemented +} + static const DuckyCmd ducky_commands[] = { {"REM", NULL, -1}, {"ID", NULL, -1}, @@ -213,6 +238,13 @@ static const DuckyCmd ducky_commands[] = { {"WAIT_FOR_BUTTON_PRESS", ducky_fnc_waitforbutton, -1}, {"MEDIA", ducky_fnc_media, -1}, {"GLOBE", ducky_fnc_globe, -1}, + {"MOUSE", ducky_fnc_mouse, -1}, +}; + +static const DuckyCmd ducky_mouse_commands[] = { + {"CLICK", ducky_fnc_mouse_click, 0}, + {"SCROLL", ducky_fnc_mouse_click, 1}, + {"MOVE", ducky_fnc_mouse_move, -1}, }; #define TAG "BadUsb" @@ -238,4 +270,4 @@ int32_t ducky_execute_cmd(BadUsbScript* bad_usb, const char* line) { } return SCRIPT_STATE_CMD_UNKNOWN; -} +} \ No newline at end of file From 40b65d2de030d2c415570424e82b65e11c09daba Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Wed, 13 Nov 2024 23:47:13 -0600 Subject: [PATCH 05/11] implement mouse click functions --- .../bad_usb/helpers/ducky_script_commands.c | 62 ++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/applications/main/bad_usb/helpers/ducky_script_commands.c b/applications/main/bad_usb/helpers/ducky_script_commands.c index 7ef487ed825..3f91d648a49 100644 --- a/applications/main/bad_usb/helpers/ducky_script_commands.c +++ b/applications/main/bad_usb/helpers/ducky_script_commands.c @@ -191,24 +191,66 @@ static int32_t ducky_fnc_waitforbutton(BadUsbScript* bad_usb, const char* line, return SCRIPT_STATE_WAIT_FOR_BTN; } -static int32_t ducky_fnc_mouse(BadUsbScript* bad_usb, const char* line, int32_T param) { +static int32_t ducky_fnc_mouse(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); + FURI_LOG_D("Worker", "ducky_fnc_mouse"); - return SCRIPT_STATE_CMD_UNKNOWN; // not yet implemented + char* mouse_cmd = line + strcspn(line, " ") + 1; + size_t mouse_cmd_len = strcspn(mouse_cmd, " "); + + for (size_t i = 0; i < COUNT_OF(ducky_mouse_commands); i++) { + size_t cmd_cmp_len = strlen(ducky_mouse_commands[i].length); + + if (mouse_cmd_len != cmd_cmp_len) { + continue; + } + + if (strcmp(ducky_mouse_commands[i].name, mouse_cmd, mouse_cmd_len) == 0) { + if (ducky_mouse_commands[i].callback == NULL) { + return 0; + } else { + return (ducky_mouse_commands[i].callback)(bad_usb, line, ducky_commands[i].param); + } + } + } + + return SCRIPT_STATE_CMD_UNKNOWN; } -static int32_t ducky_fnc_mouse_click(BadUsbScript* bad_usb, const char* line, int32_T param) { +static int32_t ducky_fnc_mouse_leftclick(BadUsbScript* bad_usb, const char* line, int32_t param) { + UNUSED(param); + + bad_usb->hid->mouse_press(bad_usb->hid_inst, HID_MOUSE_BTN_LEFT); + bad_usb->hid->mouse_release(bad_usb->hid_inst, HID_MOUSE_BTN_LEFT); + return 0; +} + +static int32_t ducky_fnc_mouse_rightclick(BadUsbScript* bad_usb, const char* line, int32_t param) { + UNUSED(param); + + bad_usb->hid->mouse_press(bad_usb->hid_inst, HID_MOUSE_BTN_RIGHT); + bad_usb->hid->mouse_release(bad_usb->hid_inst, HID_MOUSE_BTN_RIGHT); + return 0; +} + +static int32_t ducky_fnc_mouse_middleclick(BadUsbScript* bad_usb, const char* line, int32_t param) { + UNUSED(param); + + bad_usb->hid->mouse_press(bad_usb->hid_inst, HID_MOUSE_BTN_WHEEL); + bad_usb->hid->mouse_release(bad_usb->hid_inst, HID_MOUSE_BTN_WHEEL); + return 0; +} + +static int32_t ducky_fnc_mouse_scroll(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); UNUSED(bad_usb); UNUSED(line); - // param == 0 is a mouse click - // param == 1 is a mouse scroll - return SCRIPT_STATE_CMD_UNKNOWN; // not yet implemented } -static int32_t ducky_fnc_mouse_move(BadUsbScript* bad_usb, const char* line, int32_T param) { + +static int32_t ducky_fnc_mouse_move(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); UNUSED(bad_usb); UNUSED(line); @@ -242,8 +284,10 @@ static const DuckyCmd ducky_commands[] = { }; static const DuckyCmd ducky_mouse_commands[] = { - {"CLICK", ducky_fnc_mouse_click, 0}, - {"SCROLL", ducky_fnc_mouse_click, 1}, + {"LEFTCLICK", ducky_fnc_mouse_leftclick, 0}, + {"RIGHTCLICK", ducky_fnc_mouse_rightclick, 0}, + {"MIDDLECLICK", ducky_fnc_mouse_middleclick, 0}, + {"SCROLL", ducky_fnc_mouse_scroll, 0}, {"MOVE", ducky_fnc_mouse_move, -1}, }; From 8b4196023afe081779e8bcbb109595328742be72 Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Thu, 14 Nov 2024 15:51:41 -0600 Subject: [PATCH 06/11] corrected missing semicolon --- applications/main/bad_usb/helpers/bad_usb_hid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/main/bad_usb/helpers/bad_usb_hid.c b/applications/main/bad_usb/helpers/bad_usb_hid.c index c2ee28b4363..c6226cf37b2 100644 --- a/applications/main/bad_usb/helpers/bad_usb_hid.c +++ b/applications/main/bad_usb/helpers/bad_usb_hid.c @@ -54,7 +54,7 @@ bool hid_usb_mouse_scroll(void* inst, int8_t delta) { bool hid_usb_mouse_move(void* inst, int8_t dx, int8_t dy) { UNUSED(inst); - return furi_hal_hid_mouse_move(dx, dy) + return furi_hal_hid_mouse_move(dx, dy); } bool hid_usb_mouse_release_all(void* inst) { From 0e6b37429abb9c889d09a47387e1c43a73e3520f Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Thu, 14 Nov 2024 15:52:13 -0600 Subject: [PATCH 07/11] added mouse functionality --- .../bad_usb/helpers/ducky_script_commands.c | 109 +++++++++++------- 1 file changed, 67 insertions(+), 42 deletions(-) diff --git a/applications/main/bad_usb/helpers/ducky_script_commands.c b/applications/main/bad_usb/helpers/ducky_script_commands.c index 3f91d648a49..64227a2f4bf 100644 --- a/applications/main/bad_usb/helpers/ducky_script_commands.c +++ b/applications/main/bad_usb/helpers/ducky_script_commands.c @@ -1,4 +1,5 @@ #include +#include #include "ducky_script.h" #include "ducky_script_i.h" @@ -191,34 +192,9 @@ static int32_t ducky_fnc_waitforbutton(BadUsbScript* bad_usb, const char* line, return SCRIPT_STATE_WAIT_FOR_BTN; } -static int32_t ducky_fnc_mouse(BadUsbScript* bad_usb, const char* line, int32_t param) { - UNUSED(param); - FURI_LOG_D("Worker", "ducky_fnc_mouse"); - - char* mouse_cmd = line + strcspn(line, " ") + 1; - size_t mouse_cmd_len = strcspn(mouse_cmd, " "); - - for (size_t i = 0; i < COUNT_OF(ducky_mouse_commands); i++) { - size_t cmd_cmp_len = strlen(ducky_mouse_commands[i].length); - - if (mouse_cmd_len != cmd_cmp_len) { - continue; - } - - if (strcmp(ducky_mouse_commands[i].name, mouse_cmd, mouse_cmd_len) == 0) { - if (ducky_mouse_commands[i].callback == NULL) { - return 0; - } else { - return (ducky_mouse_commands[i].callback)(bad_usb, line, ducky_commands[i].param); - } - } - } - - return SCRIPT_STATE_CMD_UNKNOWN; -} - static int32_t ducky_fnc_mouse_leftclick(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); + UNUSED(line); bad_usb->hid->mouse_press(bad_usb->hid_inst, HID_MOUSE_BTN_LEFT); bad_usb->hid->mouse_release(bad_usb->hid_inst, HID_MOUSE_BTN_LEFT); @@ -227,6 +203,7 @@ static int32_t ducky_fnc_mouse_leftclick(BadUsbScript* bad_usb, const char* line static int32_t ducky_fnc_mouse_rightclick(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); + UNUSED(line); bad_usb->hid->mouse_press(bad_usb->hid_inst, HID_MOUSE_BTN_RIGHT); bad_usb->hid->mouse_release(bad_usb->hid_inst, HID_MOUSE_BTN_RIGHT); @@ -235,6 +212,7 @@ static int32_t ducky_fnc_mouse_rightclick(BadUsbScript* bad_usb, const char* lin static int32_t ducky_fnc_mouse_middleclick(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); + UNUSED(line); bad_usb->hid->mouse_press(bad_usb->hid_inst, HID_MOUSE_BTN_WHEEL); bad_usb->hid->mouse_release(bad_usb->hid_inst, HID_MOUSE_BTN_WHEEL); @@ -243,19 +221,74 @@ static int32_t ducky_fnc_mouse_middleclick(BadUsbScript* bad_usb, const char* li static int32_t ducky_fnc_mouse_scroll(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); - UNUSED(bad_usb); - UNUSED(line); - return SCRIPT_STATE_CMD_UNKNOWN; // not yet implemented -} + line = &line[strcspn(line, " ") + 1]; + line = &line[strcspn(line, " ") + 1]; + uint32_t mouse_scroll_count = 0; + bool state = ducky_get_number(line, &mouse_scroll_count); // will break since numbers can be negative + + if (!state) { + return ducky_error(bad_usb, "Invalid number %s", line); + } + bad_usb->hid->mouse_scroll(bad_usb->hid_inst, mouse_scroll_count); + return 0; +} static int32_t ducky_fnc_mouse_move(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); - UNUSED(bad_usb); - UNUSED(line); + line = &line[strcspn(line, " ") + 1]; + line = &line[strcspn(line, " ") + 1]; + int32_t mouse_move_x = 0; + int32_t mouse_move_y = 0; + + if (strint_to_int32(line, NULL, &mouse_move_x, 10) != StrintParseNoError) { + return ducky_error(bad_usb, "Invalid Number %s", line); + } + + line = &line[strcspn(line, " ") + 1]; + + if (strint_to_int32(line, NULL, &mouse_move_y, 10) != StrintParseNoError) { + return ducky_error(bad_usb, "Invalid Number %s", line); + } + + bad_usb->hid->mouse_move(bad_usb->hid_inst, mouse_move_x, mouse_move_y); - return SCRIPT_STATE_CMD_UNKNOWN; // not yet implemented + return 0; +} + +static const DuckyCmd ducky_mouse_commands[] = { + {"LEFTCLICK", ducky_fnc_mouse_leftclick, 0}, + {"RIGHTCLICK", ducky_fnc_mouse_rightclick, 0}, + {"MIDDLECLICK", ducky_fnc_mouse_middleclick, 0}, + {"SCROLL", ducky_fnc_mouse_scroll, 0}, + {"MOVE", ducky_fnc_mouse_move, -1}, +}; + +static int32_t ducky_fnc_mouse(BadUsbScript* bad_usb, const char* line, int32_t param) { + UNUSED(param); + FURI_LOG_D("Worker", "ducky_fnc_mouse"); + + const char* mouse_cmd = &line[strcspn(line, " ") + 1]; + size_t mouse_cmd_len = strcspn(mouse_cmd, " "); + + for (size_t i = 0; i < COUNT_OF(ducky_mouse_commands); i++) { + size_t cmd_cmp_len = strlen(ducky_mouse_commands[i].name); + + if (mouse_cmd_len != cmd_cmp_len) { + continue; + } + + if (strncmp(ducky_mouse_commands[i].name, mouse_cmd, mouse_cmd_len) == 0) { + if (ducky_mouse_commands[i].callback == NULL) { + return 0; + } else { + return (ducky_mouse_commands[i].callback)(bad_usb, line, ducky_mouse_commands[i].param); + } + } + } + + return SCRIPT_STATE_CMD_UNKNOWN; } static const DuckyCmd ducky_commands[] = { @@ -283,14 +316,6 @@ static const DuckyCmd ducky_commands[] = { {"MOUSE", ducky_fnc_mouse, -1}, }; -static const DuckyCmd ducky_mouse_commands[] = { - {"LEFTCLICK", ducky_fnc_mouse_leftclick, 0}, - {"RIGHTCLICK", ducky_fnc_mouse_rightclick, 0}, - {"MIDDLECLICK", ducky_fnc_mouse_middleclick, 0}, - {"SCROLL", ducky_fnc_mouse_scroll, 0}, - {"MOVE", ducky_fnc_mouse_move, -1}, -}; - #define TAG "BadUsb" #define WORKER_TAG TAG "Worker" @@ -314,4 +339,4 @@ int32_t ducky_execute_cmd(BadUsbScript* bad_usb, const char* line) { } return SCRIPT_STATE_CMD_UNKNOWN; -} \ No newline at end of file +} From 91321abe427158bd954afb9e583cd69b9c54496c Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Fri, 15 Nov 2024 00:13:23 -0600 Subject: [PATCH 08/11] corrected mouse scroll functionality --- .../main/bad_usb/helpers/ducky_script_commands.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/applications/main/bad_usb/helpers/ducky_script_commands.c b/applications/main/bad_usb/helpers/ducky_script_commands.c index 64227a2f4bf..09e792f9499 100644 --- a/applications/main/bad_usb/helpers/ducky_script_commands.c +++ b/applications/main/bad_usb/helpers/ducky_script_commands.c @@ -224,15 +224,15 @@ static int32_t ducky_fnc_mouse_scroll(BadUsbScript* bad_usb, const char* line, i line = &line[strcspn(line, " ") + 1]; line = &line[strcspn(line, " ") + 1]; - uint32_t mouse_scroll_count = 0; - bool state = ducky_get_number(line, &mouse_scroll_count); // will break since numbers can be negative + int32_t mouse_scroll_dist = 0; - if (!state) { - return ducky_error(bad_usb, "Invalid number %s", line); + if (strint_to_int32(line, NULL, &mouse_scroll_dist, 10) != StrintParseNoError) { + return ducky_error(bad_usb, "Invalid Number %s", line); } - bad_usb->hid->mouse_scroll(bad_usb->hid_inst, mouse_scroll_count); - return 0; + bad_usb->hid->mouse_scroll(bad_usb->hid_inst, mouse_scroll_dist); + + return 0; } static int32_t ducky_fnc_mouse_move(BadUsbScript* bad_usb, const char* line, int32_t param) { From 62310714633a08f23a8543b85a6987bcaf5fec80 Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Fri, 15 Nov 2024 16:08:30 -0600 Subject: [PATCH 09/11] mouse key functionality removed mouse commands supporting get_mouse_keycode function added mouse buttons as Keys for HOLD function --- .../main/bad_usb/helpers/ducky_script.c | 10 +- .../bad_usb/helpers/ducky_script_commands.c | 101 +++++++----------- .../main/bad_usb/helpers/ducky_script_i.h | 4 + .../bad_usb/helpers/ducky_script_keycodes.c | 22 ++++ 4 files changed, 72 insertions(+), 65 deletions(-) diff --git a/applications/main/bad_usb/helpers/ducky_script.c b/applications/main/bad_usb/helpers/ducky_script.c index ccc3caa811b..5dd135fd9be 100644 --- a/applications/main/bad_usb/helpers/ducky_script.c +++ b/applications/main/bad_usb/helpers/ducky_script.c @@ -193,8 +193,16 @@ static int32_t ducky_parse_line(BadUsbScript* bad_usb, FuriString* line) { return cmd_result; } + // Mouse Keys + uint16_t key = ducky_get_mouse_keycode_by_name(line_tmp); + if (key != HID_MOUSE_INVALID) { + bad_usb->hid->mouse_press(bad_usb->hid_inst, key); + bad_usb->hid->mouse_release(bad_usb->hid_inst, key); + return 0; + } + // Special keys + modifiers - uint16_t key = ducky_get_keycode(bad_usb, line_tmp, false); + key = ducky_get_keycode(bad_usb, line_tmp, false); if(key == HID_KEYBOARD_NONE) { return ducky_error(bad_usb, "No keycode defined for %s", line_tmp); } diff --git a/applications/main/bad_usb/helpers/ducky_script_commands.c b/applications/main/bad_usb/helpers/ducky_script_commands.c index 09e792f9499..a5fe9f82993 100644 --- a/applications/main/bad_usb/helpers/ducky_script_commands.c +++ b/applications/main/bad_usb/helpers/ducky_script_commands.c @@ -125,32 +125,64 @@ static int32_t ducky_fnc_altstring(BadUsbScript* bad_usb, const char* line, int3 static int32_t ducky_fnc_hold(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); + bool isMouse = false; line = &line[ducky_get_command_len(line) + 1]; + + // Handle Keyboard keys here uint16_t key = ducky_get_keycode(bad_usb, line, true); if(key == HID_KEYBOARD_NONE) { return ducky_error(bad_usb, "No keycode defined for %s", line); } + + // Handle Mouse Keys here + key = ducky_get_mouse_keycode_by_name(line); + if (key != HID_MOUSE_INVALID) { + isMouse = true; + } + bad_usb->key_hold_nb++; if(bad_usb->key_hold_nb > (HID_KB_MAX_KEYS - 1)) { return ducky_error(bad_usb, "Too many keys are hold"); } + + if (isMouse) { + bad_usb->hid->mouse_press(bad_usb->hid_inst, key); + return 0; + } + bad_usb->hid->kb_press(bad_usb->hid_inst, key); return 0; } static int32_t ducky_fnc_release(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); + bool isMouse = false; line = &line[ducky_get_command_len(line) + 1]; + + //Handle Keyboard Keys here uint16_t key = ducky_get_keycode(bad_usb, line, true); if(key == HID_KEYBOARD_NONE) { return ducky_error(bad_usb, "No keycode defined for %s", line); } + + // Handle Mouse Keys here + key = ducky_get_mouse_keycode_by_name(line); + if (key != HID_MOUSE_INVALID) { + isMouse = true; + } + if(bad_usb->key_hold_nb == 0) { return ducky_error(bad_usb, "No keys are hold"); } bad_usb->key_hold_nb--; + + if (isMouse) { + bad_usb->hid->mouse_release(bad_usb->hid_inst, key); + return 0; + } + bad_usb->hid->kb_release(bad_usb->hid_inst, key); return 0; } @@ -192,37 +224,9 @@ static int32_t ducky_fnc_waitforbutton(BadUsbScript* bad_usb, const char* line, return SCRIPT_STATE_WAIT_FOR_BTN; } -static int32_t ducky_fnc_mouse_leftclick(BadUsbScript* bad_usb, const char* line, int32_t param) { - UNUSED(param); - UNUSED(line); - - bad_usb->hid->mouse_press(bad_usb->hid_inst, HID_MOUSE_BTN_LEFT); - bad_usb->hid->mouse_release(bad_usb->hid_inst, HID_MOUSE_BTN_LEFT); - return 0; -} - -static int32_t ducky_fnc_mouse_rightclick(BadUsbScript* bad_usb, const char* line, int32_t param) { - UNUSED(param); - UNUSED(line); - - bad_usb->hid->mouse_press(bad_usb->hid_inst, HID_MOUSE_BTN_RIGHT); - bad_usb->hid->mouse_release(bad_usb->hid_inst, HID_MOUSE_BTN_RIGHT); - return 0; -} - -static int32_t ducky_fnc_mouse_middleclick(BadUsbScript* bad_usb, const char* line, int32_t param) { - UNUSED(param); - UNUSED(line); - - bad_usb->hid->mouse_press(bad_usb->hid_inst, HID_MOUSE_BTN_WHEEL); - bad_usb->hid->mouse_release(bad_usb->hid_inst, HID_MOUSE_BTN_WHEEL); - return 0; -} - static int32_t ducky_fnc_mouse_scroll(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); - line = &line[strcspn(line, " ") + 1]; line = &line[strcspn(line, " ") + 1]; int32_t mouse_scroll_dist = 0; @@ -237,7 +241,7 @@ static int32_t ducky_fnc_mouse_scroll(BadUsbScript* bad_usb, const char* line, i static int32_t ducky_fnc_mouse_move(BadUsbScript* bad_usb, const char* line, int32_t param) { UNUSED(param); - line = &line[strcspn(line, " ") + 1]; + line = &line[strcspn(line, " ") + 1]; int32_t mouse_move_x = 0; int32_t mouse_move_y = 0; @@ -257,40 +261,6 @@ static int32_t ducky_fnc_mouse_move(BadUsbScript* bad_usb, const char* line, int return 0; } -static const DuckyCmd ducky_mouse_commands[] = { - {"LEFTCLICK", ducky_fnc_mouse_leftclick, 0}, - {"RIGHTCLICK", ducky_fnc_mouse_rightclick, 0}, - {"MIDDLECLICK", ducky_fnc_mouse_middleclick, 0}, - {"SCROLL", ducky_fnc_mouse_scroll, 0}, - {"MOVE", ducky_fnc_mouse_move, -1}, -}; - -static int32_t ducky_fnc_mouse(BadUsbScript* bad_usb, const char* line, int32_t param) { - UNUSED(param); - FURI_LOG_D("Worker", "ducky_fnc_mouse"); - - const char* mouse_cmd = &line[strcspn(line, " ") + 1]; - size_t mouse_cmd_len = strcspn(mouse_cmd, " "); - - for (size_t i = 0; i < COUNT_OF(ducky_mouse_commands); i++) { - size_t cmd_cmp_len = strlen(ducky_mouse_commands[i].name); - - if (mouse_cmd_len != cmd_cmp_len) { - continue; - } - - if (strncmp(ducky_mouse_commands[i].name, mouse_cmd, mouse_cmd_len) == 0) { - if (ducky_mouse_commands[i].callback == NULL) { - return 0; - } else { - return (ducky_mouse_commands[i].callback)(bad_usb, line, ducky_mouse_commands[i].param); - } - } - } - - return SCRIPT_STATE_CMD_UNKNOWN; -} - static const DuckyCmd ducky_commands[] = { {"REM", NULL, -1}, {"ID", NULL, -1}, @@ -313,7 +283,10 @@ static const DuckyCmd ducky_commands[] = { {"WAIT_FOR_BUTTON_PRESS", ducky_fnc_waitforbutton, -1}, {"MEDIA", ducky_fnc_media, -1}, {"GLOBE", ducky_fnc_globe, -1}, - {"MOUSE", ducky_fnc_mouse, -1}, + {"MOUSEMOVE", ducky_fnc_mouse_move, -1}, + {"MOUSE_MOVE", ducky_fnc_mouse_move, -1}, + {"MOUSESCROLL", ducky_fnc_mouse_scroll, -1}, + {"MOUSE_SCROLL", ducky_fnc_mouse_scroll, -1}, }; #define TAG "BadUsb" diff --git a/applications/main/bad_usb/helpers/ducky_script_i.h b/applications/main/bad_usb/helpers/ducky_script_i.h index 464c8a72bf0..1945d3db11b 100644 --- a/applications/main/bad_usb/helpers/ducky_script_i.h +++ b/applications/main/bad_usb/helpers/ducky_script_i.h @@ -18,6 +18,8 @@ extern "C" { #define FILE_BUFFER_LEN 16 +#define HID_MOUSE_INVALID 0 + struct BadUsbScript { FuriHalUsbHidConfig hid_cfg; const BadUsbHidApi* hid; @@ -55,6 +57,8 @@ uint16_t ducky_get_keycode_by_name(const char* param); uint16_t ducky_get_media_keycode_by_name(const char* param); +uint8_t ducky_get_mouse_keycode_by_name(const char* param); + bool ducky_get_number(const char* param, uint32_t* val); void ducky_numlock_on(BadUsbScript* bad_usb); diff --git a/applications/main/bad_usb/helpers/ducky_script_keycodes.c b/applications/main/bad_usb/helpers/ducky_script_keycodes.c index 290618c131b..b556beda540 100644 --- a/applications/main/bad_usb/helpers/ducky_script_keycodes.c +++ b/applications/main/bad_usb/helpers/ducky_script_keycodes.c @@ -108,6 +108,17 @@ static const DuckyKey ducky_media_keys[] = { {"BRIGHT_DOWN", HID_CONSUMER_BRIGHTNESS_DECREMENT}, }; +static const DuckyKey ducky_mouse_keys[] = { + {"LEFTCLICK", HID_MOUSE_BTN_LEFT}, + {"LEFT_CLICK", HID_MOUSE_BTN_LEFT}, + {"RIGHTCLICK", HID_MOUSE_BTN_RIGHT}, + {"RIGHT_CLICK", HID_MOUSE_BTN_RIGHT}, + {"MIDDLECLICK", HID_MOUSE_BTN_WHEEL}, + {"MIDDLE_CLICK", HID_MOUSE_BTN_WHEEL}, + {"WHEELCLICK", HID_MOUSE_BTN_WHEEL}, + {"WHEEL_CLICK", HID_MOUSE_BTN_WHEEL}, +}; + uint16_t ducky_get_keycode_by_name(const char* param) { for(size_t i = 0; i < COUNT_OF(ducky_keys); i++) { size_t key_cmd_len = strlen(ducky_keys[i].name); @@ -131,3 +142,14 @@ uint16_t ducky_get_media_keycode_by_name(const char* param) { return HID_CONSUMER_UNASSIGNED; } + +uint8_t ducky_get_mouse_keycode_by_name(const char* param) { + for (size_t i = 0; i < COUNT_OF(ducky_mouse_keys); i++) { + size_t key_cmd_len = strlen(ducky_mouse_keys[i].name); + if((strncmp(param, ducky_mouse_keys[i].name, key_cmd_len) == 0) && (ducky_is_line_end(param[key_cmd_len]))) { + return ducky_mouse_keys[i].keycode; + } + } + + return HID_MOUSE_INVALID; +} From 9067423d8def87debe7ecf93ca4c7d00faeb6939 Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Fri, 15 Nov 2024 16:26:47 -0600 Subject: [PATCH 10/11] add mouse commands --- .../file_formats/BadUsbScriptFormat.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/documentation/file_formats/BadUsbScriptFormat.md b/documentation/file_formats/BadUsbScriptFormat.md index 1bac3c4aa43..656788c5d47 100644 --- a/documentation/file_formats/BadUsbScriptFormat.md +++ b/documentation/file_formats/BadUsbScriptFormat.md @@ -177,3 +177,22 @@ Example: `ID 1234:abcd Flipper Devices:Flipper Zero` VID and PID are hex codes and are mandatory. Manufacturer and Product are text strings and are optional. + +## Mouse Commands + +Mouse movement and click commands. Mouse click commands support HOLD functionality. + +| Command | Parameters | Notes | +| ------------- | -------------------------------| -------------------------------- | +| LEFTCLICK | None | | +| LEFT_CLICK | None | functionally same as LEFTCLICK | +| RIGHTCLICK | None | | +| RIGHT_CLICK | None | functionally same as RIGHTCLICK | +| MIDDLECLICK | None | | +| MIDDLE_CLICK | None | functionally same as MIDDLECLICK | +| WHEELCLICK | None | functionally same as MIDDLECLICK | +| WHEEL_CLICK | None | functionally same as MIDDLECLICK | +| MOUSEMOVE | x y: int move mount/direction | | +| MOUSE_MOVE | x y: int move mount/direction | functionally same as MOUSEMOVE | +| MOUSESCROLL | delta: int scroll distance | | +| MOUSE_SCROLL | delta: int scroll distance | functionally same as MOUSESCROLL | From def354aa2f3b5894ad0a5b85cf6636ad8f37a022 Mon Sep 17 00:00:00 2001 From: Ryan Peel Date: Fri, 15 Nov 2024 16:36:06 -0600 Subject: [PATCH 11/11] removed mouse middle click --- documentation/file_formats/BadUsbScriptFormat.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/documentation/file_formats/BadUsbScriptFormat.md b/documentation/file_formats/BadUsbScriptFormat.md index 656788c5d47..11977c9cb2e 100644 --- a/documentation/file_formats/BadUsbScriptFormat.md +++ b/documentation/file_formats/BadUsbScriptFormat.md @@ -188,10 +188,6 @@ Mouse movement and click commands. Mouse click commands support HOLD functionali | LEFT_CLICK | None | functionally same as LEFTCLICK | | RIGHTCLICK | None | | | RIGHT_CLICK | None | functionally same as RIGHTCLICK | -| MIDDLECLICK | None | | -| MIDDLE_CLICK | None | functionally same as MIDDLECLICK | -| WHEELCLICK | None | functionally same as MIDDLECLICK | -| WHEEL_CLICK | None | functionally same as MIDDLECLICK | | MOUSEMOVE | x y: int move mount/direction | | | MOUSE_MOVE | x y: int move mount/direction | functionally same as MOUSEMOVE | | MOUSESCROLL | delta: int scroll distance | |