mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-25 03:29:58 -07:00
BadUSB: BLE, media keys, Fn/Globe key commands (#3403)
* BadUSB: media keys, GLOBE command * f18 api table fix * BadUSB over BLE * Made PVS happy Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
#include <gui/gui.h>
|
||||
#include <input/input.h>
|
||||
#include <lib/toolbox/args.h>
|
||||
#include <furi_hal_usb_hid.h>
|
||||
#include <storage/storage.h>
|
||||
#include "ducky_script.h"
|
||||
#include "ducky_script_i.h"
|
||||
@@ -71,39 +70,40 @@ bool ducky_get_number(const char* param, uint32_t* val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void ducky_numlock_on(void) {
|
||||
if((furi_hal_hid_get_led_state() & HID_KB_LED_NUM) == 0) {
|
||||
furi_hal_hid_kb_press(HID_KEYBOARD_LOCK_NUM_LOCK);
|
||||
furi_hal_hid_kb_release(HID_KEYBOARD_LOCK_NUM_LOCK);
|
||||
void ducky_numlock_on(BadUsbScript* bad_usb) {
|
||||
if((bad_usb->hid->get_led_state(bad_usb->hid_inst) & HID_KB_LED_NUM) == 0) {
|
||||
bad_usb->hid->kb_press(bad_usb->hid_inst, HID_KEYBOARD_LOCK_NUM_LOCK);
|
||||
bad_usb->hid->kb_release(bad_usb->hid_inst, HID_KEYBOARD_LOCK_NUM_LOCK);
|
||||
}
|
||||
}
|
||||
bool ducky_numpad_press(const char num) {
|
||||
|
||||
bool ducky_numpad_press(BadUsbScript* bad_usb, const char num) {
|
||||
if((num < '0') || (num > '9')) return false;
|
||||
|
||||
uint16_t key = numpad_keys[num - '0'];
|
||||
furi_hal_hid_kb_press(key);
|
||||
furi_hal_hid_kb_release(key);
|
||||
bad_usb->hid->kb_press(bad_usb->hid_inst, key);
|
||||
bad_usb->hid->kb_release(bad_usb->hid_inst, key);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ducky_altchar(const char* charcode) {
|
||||
bool ducky_altchar(BadUsbScript* bad_usb, const char* charcode) {
|
||||
uint8_t i = 0;
|
||||
bool state = false;
|
||||
|
||||
furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT);
|
||||
bad_usb->hid->kb_press(bad_usb->hid_inst, KEY_MOD_LEFT_ALT);
|
||||
|
||||
while(!ducky_is_line_end(charcode[i])) {
|
||||
state = ducky_numpad_press(charcode[i]);
|
||||
state = ducky_numpad_press(bad_usb, charcode[i]);
|
||||
if(state == false) break;
|
||||
i++;
|
||||
}
|
||||
|
||||
furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT);
|
||||
bad_usb->hid->kb_release(bad_usb->hid_inst, KEY_MOD_LEFT_ALT);
|
||||
return state;
|
||||
}
|
||||
|
||||
bool ducky_altstring(const char* param) {
|
||||
bool ducky_altstring(BadUsbScript* bad_usb, const char* param) {
|
||||
uint32_t i = 0;
|
||||
bool state = false;
|
||||
|
||||
@@ -116,7 +116,7 @@ bool ducky_altstring(const char* param) {
|
||||
char temp_str[4];
|
||||
snprintf(temp_str, 4, "%u", param[i]);
|
||||
|
||||
state = ducky_altchar(temp_str);
|
||||
state = ducky_altchar(bad_usb, temp_str);
|
||||
if(state == false) break;
|
||||
i++;
|
||||
}
|
||||
@@ -140,12 +140,12 @@ bool ducky_string(BadUsbScript* bad_usb, const char* param) {
|
||||
if(param[i] != '\n') {
|
||||
uint16_t keycode = BADUSB_ASCII_TO_KEY(bad_usb, param[i]);
|
||||
if(keycode != HID_KEYBOARD_NONE) {
|
||||
furi_hal_hid_kb_press(keycode);
|
||||
furi_hal_hid_kb_release(keycode);
|
||||
bad_usb->hid->kb_press(bad_usb->hid_inst, keycode);
|
||||
bad_usb->hid->kb_release(bad_usb->hid_inst, keycode);
|
||||
}
|
||||
} else {
|
||||
furi_hal_hid_kb_press(HID_KEYBOARD_RETURN);
|
||||
furi_hal_hid_kb_release(HID_KEYBOARD_RETURN);
|
||||
bad_usb->hid->kb_press(bad_usb->hid_inst, HID_KEYBOARD_RETURN);
|
||||
bad_usb->hid->kb_release(bad_usb->hid_inst, HID_KEYBOARD_RETURN);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
@@ -163,12 +163,12 @@ static bool ducky_string_next(BadUsbScript* bad_usb) {
|
||||
if(print_char != '\n') {
|
||||
uint16_t keycode = BADUSB_ASCII_TO_KEY(bad_usb, print_char);
|
||||
if(keycode != HID_KEYBOARD_NONE) {
|
||||
furi_hal_hid_kb_press(keycode);
|
||||
furi_hal_hid_kb_release(keycode);
|
||||
bad_usb->hid->kb_press(bad_usb->hid_inst, keycode);
|
||||
bad_usb->hid->kb_release(bad_usb->hid_inst, keycode);
|
||||
}
|
||||
} else {
|
||||
furi_hal_hid_kb_press(HID_KEYBOARD_RETURN);
|
||||
furi_hal_hid_kb_release(HID_KEYBOARD_RETURN);
|
||||
bad_usb->hid->kb_press(bad_usb->hid_inst, HID_KEYBOARD_RETURN);
|
||||
bad_usb->hid->kb_release(bad_usb->hid_inst, HID_KEYBOARD_RETURN);
|
||||
}
|
||||
|
||||
bad_usb->string_print_pos++;
|
||||
@@ -201,8 +201,8 @@ static int32_t ducky_parse_line(BadUsbScript* bad_usb, FuriString* line) {
|
||||
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
|
||||
key |= ducky_get_keycode(bad_usb, line_tmp, true);
|
||||
}
|
||||
furi_hal_hid_kb_press(key);
|
||||
furi_hal_hid_kb_release(key);
|
||||
bad_usb->hid->kb_press(bad_usb->hid_inst, key);
|
||||
bad_usb->hid->kb_release(bad_usb->hid_inst, key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -231,6 +231,17 @@ static bool ducky_set_usb_id(BadUsbScript* bad_usb, const char* line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static void bad_usb_hid_state_callback(bool state, void* context) {
|
||||
furi_assert(context);
|
||||
BadUsbScript* bad_usb = context;
|
||||
|
||||
if(state == true) {
|
||||
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtConnect);
|
||||
} else {
|
||||
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtDisconnect);
|
||||
}
|
||||
}
|
||||
|
||||
static bool ducky_script_preload(BadUsbScript* bad_usb, File* script_file) {
|
||||
uint8_t ret = 0;
|
||||
uint32_t line_len = 0;
|
||||
@@ -265,10 +276,11 @@ static bool ducky_script_preload(BadUsbScript* bad_usb, File* script_file) {
|
||||
}
|
||||
|
||||
if(id_set) {
|
||||
furi_check(furi_hal_usb_set_config(&usb_hid, &bad_usb->hid_cfg));
|
||||
bad_usb->hid_inst = bad_usb->hid->init(&bad_usb->hid_cfg);
|
||||
} else {
|
||||
furi_check(furi_hal_usb_set_config(&usb_hid, NULL));
|
||||
bad_usb->hid_inst = bad_usb->hid->init(NULL);
|
||||
}
|
||||
bad_usb->hid->set_state_callback(bad_usb->hid_inst, bad_usb_hid_state_callback, bad_usb);
|
||||
|
||||
storage_file_seek(script_file, 0, true);
|
||||
furi_string_reset(bad_usb->line);
|
||||
@@ -345,17 +357,6 @@ static int32_t ducky_script_execute_next(BadUsbScript* bad_usb, File* script_fil
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void bad_usb_hid_state_callback(bool state, void* context) {
|
||||
furi_assert(context);
|
||||
BadUsbScript* bad_usb = context;
|
||||
|
||||
if(state == true) {
|
||||
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtConnect);
|
||||
} else {
|
||||
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtDisconnect);
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t bad_usb_flags_get(uint32_t flags_mask, uint32_t timeout) {
|
||||
uint32_t flags = furi_thread_flags_get();
|
||||
furi_check((flags & FuriFlagError) == 0);
|
||||
@@ -382,8 +383,6 @@ static int32_t bad_usb_worker(void* context) {
|
||||
bad_usb->line_prev = furi_string_alloc();
|
||||
bad_usb->string_print = furi_string_alloc();
|
||||
|
||||
furi_hal_hid_set_state_callback(bad_usb_hid_state_callback, bad_usb);
|
||||
|
||||
while(1) {
|
||||
if(worker_state == BadUsbStateInit) { // State: initialization
|
||||
if(storage_file_open(
|
||||
@@ -392,7 +391,7 @@ static int32_t bad_usb_worker(void* context) {
|
||||
FSAM_READ,
|
||||
FSOM_OPEN_EXISTING)) {
|
||||
if((ducky_script_preload(bad_usb, script_file)) && (bad_usb->st.line_nb > 0)) {
|
||||
if(furi_hal_hid_is_connected()) {
|
||||
if(bad_usb->hid->is_connected(bad_usb->hid_inst)) {
|
||||
worker_state = BadUsbStateIdle; // Ready to run
|
||||
} else {
|
||||
worker_state = BadUsbStateNotConnected; // USB not connected
|
||||
@@ -408,7 +407,8 @@ static int32_t bad_usb_worker(void* context) {
|
||||
|
||||
} else if(worker_state == BadUsbStateNotConnected) { // State: USB not connected
|
||||
uint32_t flags = bad_usb_flags_get(
|
||||
WorkerEvtEnd | WorkerEvtConnect | WorkerEvtStartStop, FuriWaitForever);
|
||||
WorkerEvtEnd | WorkerEvtConnect | WorkerEvtDisconnect | WorkerEvtStartStop,
|
||||
FuriWaitForever);
|
||||
|
||||
if(flags & WorkerEvtEnd) {
|
||||
break;
|
||||
@@ -490,10 +490,10 @@ static int32_t bad_usb_worker(void* context) {
|
||||
break;
|
||||
} else if(flags & WorkerEvtStartStop) {
|
||||
worker_state = BadUsbStateIdle; // Stop executing script
|
||||
furi_hal_hid_kb_release_all();
|
||||
bad_usb->hid->release_all(bad_usb->hid_inst);
|
||||
} else if(flags & WorkerEvtDisconnect) {
|
||||
worker_state = BadUsbStateNotConnected; // USB disconnected
|
||||
furi_hal_hid_kb_release_all();
|
||||
bad_usb->hid->release_all(bad_usb->hid_inst);
|
||||
} else if(flags & WorkerEvtPauseResume) {
|
||||
pause_state = BadUsbStateRunning;
|
||||
worker_state = BadUsbStatePaused; // Pause
|
||||
@@ -513,12 +513,12 @@ static int32_t bad_usb_worker(void* context) {
|
||||
delay_val = 0;
|
||||
worker_state = BadUsbStateScriptError;
|
||||
bad_usb->st.state = worker_state;
|
||||
furi_hal_hid_kb_release_all();
|
||||
bad_usb->hid->release_all(bad_usb->hid_inst);
|
||||
} else if(delay_val == SCRIPT_STATE_END) { // End of script
|
||||
delay_val = 0;
|
||||
worker_state = BadUsbStateIdle;
|
||||
bad_usb->st.state = BadUsbStateDone;
|
||||
furi_hal_hid_kb_release_all();
|
||||
bad_usb->hid->release_all(bad_usb->hid_inst);
|
||||
continue;
|
||||
} else if(delay_val == SCRIPT_STATE_STRING_START) { // Start printing string with delays
|
||||
delay_val = bad_usb->defdelay;
|
||||
@@ -546,7 +546,7 @@ static int32_t bad_usb_worker(void* context) {
|
||||
worker_state = BadUsbStateRunning;
|
||||
} else if(flags & WorkerEvtDisconnect) {
|
||||
worker_state = BadUsbStateNotConnected; // USB disconnected
|
||||
furi_hal_hid_kb_release_all();
|
||||
bad_usb->hid->release_all(bad_usb->hid_inst);
|
||||
}
|
||||
bad_usb->st.state = worker_state;
|
||||
continue;
|
||||
@@ -561,11 +561,11 @@ static int32_t bad_usb_worker(void* context) {
|
||||
} else if(flags & WorkerEvtStartStop) {
|
||||
worker_state = BadUsbStateIdle; // Stop executing script
|
||||
bad_usb->st.state = worker_state;
|
||||
furi_hal_hid_kb_release_all();
|
||||
bad_usb->hid->release_all(bad_usb->hid_inst);
|
||||
} else if(flags & WorkerEvtDisconnect) {
|
||||
worker_state = BadUsbStateNotConnected; // USB disconnected
|
||||
bad_usb->st.state = worker_state;
|
||||
furi_hal_hid_kb_release_all();
|
||||
bad_usb->hid->release_all(bad_usb->hid_inst);
|
||||
} else if(flags & WorkerEvtPauseResume) {
|
||||
if(pause_state == BadUsbStateRunning) {
|
||||
if(delay_val > 0) {
|
||||
@@ -595,10 +595,10 @@ static int32_t bad_usb_worker(void* context) {
|
||||
break;
|
||||
} else if(flags & WorkerEvtStartStop) {
|
||||
worker_state = BadUsbStateIdle; // Stop executing script
|
||||
furi_hal_hid_kb_release_all();
|
||||
bad_usb->hid->release_all(bad_usb->hid_inst);
|
||||
} else if(flags & WorkerEvtDisconnect) {
|
||||
worker_state = BadUsbStateNotConnected; // USB disconnected
|
||||
furi_hal_hid_kb_release_all();
|
||||
bad_usb->hid->release_all(bad_usb->hid_inst);
|
||||
} else if(flags & WorkerEvtPauseResume) {
|
||||
pause_state = BadUsbStateStringDelay;
|
||||
worker_state = BadUsbStatePaused; // Pause
|
||||
@@ -628,7 +628,8 @@ static int32_t bad_usb_worker(void* context) {
|
||||
}
|
||||
}
|
||||
|
||||
furi_hal_hid_set_state_callback(NULL, NULL);
|
||||
bad_usb->hid->set_state_callback(bad_usb->hid_inst, NULL, NULL);
|
||||
bad_usb->hid->deinit(bad_usb->hid_inst);
|
||||
|
||||
storage_file_close(script_file);
|
||||
storage_file_free(script_file);
|
||||
@@ -647,7 +648,7 @@ static void bad_usb_script_set_default_keyboard_layout(BadUsbScript* bad_usb) {
|
||||
memcpy(bad_usb->layout, hid_asciimap, MIN(sizeof(hid_asciimap), sizeof(bad_usb->layout)));
|
||||
}
|
||||
|
||||
BadUsbScript* bad_usb_script_open(FuriString* file_path) {
|
||||
BadUsbScript* bad_usb_script_open(FuriString* file_path, BadUsbHidInterface interface) {
|
||||
furi_assert(file_path);
|
||||
|
||||
BadUsbScript* bad_usb = malloc(sizeof(BadUsbScript));
|
||||
@@ -657,6 +658,7 @@ BadUsbScript* bad_usb_script_open(FuriString* file_path) {
|
||||
|
||||
bad_usb->st.state = BadUsbStateInit;
|
||||
bad_usb->st.error[0] = '\0';
|
||||
bad_usb->hid = bad_usb_hid_get_interface(interface);
|
||||
|
||||
bad_usb->thread = furi_thread_alloc_ex("BadUsbWorker", 2048, bad_usb_worker, bad_usb);
|
||||
furi_thread_start(bad_usb->thread);
|
||||
|
||||
Reference in New Issue
Block a user