Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BadUsb: STRINGDELAY feature, worker signal handling refactoring #2269

Merged
merged 20 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions applications/main/bad_usb/bad_usb_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct BadUsbScript {
BadUsbState st;
FuriString* file_path;
uint32_t defdelay;
uint32_t stringdelay;
FuriThread* thread;
uint8_t file_buf[FILE_BUFFER_LEN + 1];
uint8_t buf_start;
Expand Down Expand Up @@ -109,6 +110,8 @@ static const char ducky_cmd_delay[] = {"DELAY "};
static const char ducky_cmd_string[] = {"STRING "};
static const char ducky_cmd_defdelay_1[] = {"DEFAULT_DELAY "};
static const char ducky_cmd_defdelay_2[] = {"DEFAULTDELAY "};
static const char ducky_cmd_stringdelay_1[] = {"STRINGDELAY "};
skotopes marked this conversation as resolved.
Show resolved Hide resolved
static const char ducky_cmd_stringdelay_2[] = {"STRING_DELAY "};
static const char ducky_cmd_repeat[] = {"REPEAT "};
static const char ducky_cmd_sysrq[] = {"SYSRQ "};

Expand Down Expand Up @@ -205,16 +208,38 @@ static bool ducky_altstring(const char* param) {
return state;
}

static bool ducky_string(const char* param) {
static bool ducky_string(BadUsbScript* bad_usb, const char* param) {
uint32_t i = 0;
while(param[i] != '\0') {
uint16_t keycode = HID_ASCII_TO_KEY(param[i]);
if(keycode != HID_KEYBOARD_NONE) {
furi_hal_hid_kb_press(keycode);
furi_hal_hid_kb_release(keycode);
uint32_t timing = 0;

if(bad_usb->stringdelay == 0) {
timing = 0;
while(param[i] != '\0') {
uint16_t keycode = HID_ASCII_TO_KEY(param[i]);
if(keycode != HID_KEYBOARD_NONE) {
furi_hal_hid_kb_press(keycode);
furi_hal_hid_kb_release(keycode);
}
i++;
}
} else if(bad_usb->stringdelay > 0) {
if(bad_usb->stringdelay > 1000) {
timing = bad_usb->stringdelay / 2 / 10;
n30f0x marked this conversation as resolved.
Show resolved Hide resolved
} else {
timing = bad_usb->stringdelay / 2;
}
while(param[i] != '\0') {
uint16_t keycode = HID_ASCII_TO_KEY(param[i]);
if(keycode != HID_KEYBOARD_NONE) {
furi_delay_ms(timing);
furi_hal_hid_kb_press(keycode);
furi_delay_ms(timing);
furi_hal_hid_kb_release(keycode);
}
i++;
}
i++;
}
bad_usb->stringdelay = 0;
return true;
}

Expand Down Expand Up @@ -273,10 +298,24 @@ static int32_t
snprintf(error, error_len, "Invalid number %s", line_tmp);
}
return (state) ? (0) : SCRIPT_STATE_ERROR;
} else if(
(strncmp(line_tmp, ducky_cmd_stringdelay_1, strlen(ducky_cmd_stringdelay_1)) == 0) ||
(strncmp(line_tmp, ducky_cmd_stringdelay_2, strlen(ducky_cmd_stringdelay_2)) == 0)) {
//STRINGDELAY, finally it's here
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
state = ducky_get_number(line_tmp, &bad_usb->stringdelay);
if((state) && (bad_usb->stringdelay > 0)) {
return state;
}
if(error != NULL) {
snprintf(error, error_len, "Invalid number %s", line_tmp);
}
return SCRIPT_STATE_ERROR;

} else if(strncmp(line_tmp, ducky_cmd_string, strlen(ducky_cmd_string)) == 0) {
// STRING
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
state = ducky_string(line_tmp);
state = ducky_string(bad_usb, line_tmp);
if(!state && error != NULL) {
snprintf(error, error_len, "Invalid string %s", line_tmp);
}
Expand Down Expand Up @@ -546,6 +585,7 @@ static int32_t bad_usb_worker(void* context) {
bad_usb->buf_len = 0;
bad_usb->st.line_cur = 0;
bad_usb->defdelay = 0;
bad_usb->stringdelay = 0;
bad_usb->repeat_cnt = 0;
bad_usb->file_end = false;
storage_file_seek(script_file, 0, true);
Expand All @@ -569,11 +609,12 @@ static int32_t bad_usb_worker(void* context) {
bad_usb->buf_len = 0;
bad_usb->st.line_cur = 0;
bad_usb->defdelay = 0;
bad_usb->stringdelay = 0;
bad_usb->repeat_cnt = 0;
bad_usb->file_end = false;
storage_file_seek(script_file, 0, true);
// extra time for PC to recognize Flipper as keyboard
furi_thread_flags_wait(0, FuriFlagWaitAny, 1500);
furi_delay_ms(1500);
worker_state = BadUsbStateRunning;
} else if(flags & WorkerEvtToggle) { // Cancel scheduled execution
worker_state = BadUsbStateNotConnected;
Expand Down
8 changes: 8 additions & 0 deletions documentation/file_formats/BadUsbScriptFormat.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ Can be combined with a special key command or a single character.
| ------- | ----------- | ----------------- |
| STRING | Text string | Print text string |

## String delay

Delay will be split between each key press and release. If delay is too big it will be halfed and divided by 10 to avoid too long keypresses.
|Command|Parameters|Notes|
|-|-|-|
|STRING_DELAY|Delay value in ms|Applied once to next appearing string|
|STRINGDELAY|Delay value in ms|Same as STRING_DELAY|

## Repeat

| Command | Parameters | Notes |
Expand Down