From 61e76bc85c6bd512be235efadb4e86e767ea1af5 Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Thu, 8 Sep 2022 17:39:33 +0300 Subject: [PATCH] run bruteforce in separate thread --- .../scene/subbrute_scene_run_attack.c | 136 ++++++++++++------ applications/subbrute/subbrute.c | 2 +- applications/subbrute/subbrute.h | 4 + 3 files changed, 100 insertions(+), 42 deletions(-) diff --git a/applications/subbrute/scene/subbrute_scene_run_attack.c b/applications/subbrute/scene/subbrute_scene_run_attack.c index 5d229eb63..b33079ffe 100644 --- a/applications/subbrute/scene/subbrute_scene_run_attack.c +++ b/applications/subbrute/scene/subbrute_scene_run_attack.c @@ -34,32 +34,42 @@ FuriHalSubGhzPreset str_to_preset(string_t preset) { void subbrute_emit(SubBruteState* context) { //FURI_LOG_D(TAG, string_get_cstr(context->flipper_format_string)); - furi_hal_subghz_start_async_tx(subghz_transmitter_yield, context->transmitter); - while(!(furi_hal_subghz_is_async_tx_complete())) { - furi_delay_ms(5); - } - - furi_hal_subghz_stop_async_tx(); -} - -void prepare_emit(SubBruteState* context) { - furi_hal_subghz_init(); - context->transmitter = subghz_transmitter_alloc_init(context->environment, string_get_cstr(context->protocol)); + subghz_transmitter_deserialize(context->transmitter, context->flipper_format); furi_hal_subghz_reset(); furi_hal_subghz_load_preset(str_to_preset(context->preset)); - furi_hal_subghz_set_frequency_and_path(context->frequency); + context->frequency_cal = furi_hal_subghz_set_frequency_and_path(context->frequency); + + furi_hal_subghz_start_async_tx(subghz_transmitter_yield, context->transmitter); + while(!(furi_hal_subghz_is_async_tx_complete())) { + furi_delay_ms(1); + } + + furi_hal_subghz_stop_async_tx(); + subghz_transmitter_stop(context->transmitter); + furi_hal_subghz_idle(); + subghz_transmitter_free(context->transmitter); +} + +void prepare_emit(SubBruteState* context) { + UNUSED(context); + + furi_hal_subghz_init(); + + //furi_hal_subghz_set_frequency_and_path(context->frequency); + + //furi_hal_subghz_reset(); } void clear_emit(SubBruteState* context) { + UNUSED(context); + furi_hal_subghz_stop_async_tx(); furi_hal_subghz_idle(); furi_hal_subghz_sleep(); - - subghz_transmitter_free(context->transmitter); } /* void subbrute_send_raw_packet(SubBruteState* context) { @@ -166,7 +176,6 @@ void subbrute_send_packet_parsed(SubBruteState* context) { stream_clean(context->stream); stream_write_string(context->stream, context->flipper_format_string); - subghz_transmitter_deserialize(context->transmitter, context->flipper_format); subbrute_emit(context); } @@ -180,35 +189,10 @@ void subbrute_send_packet(SubBruteState* context) { string_clear(context->flipper_format_string); } -void subbrute_scene_run_attack_on_enter(SubBruteState* context) { - if(!toSave) { - if(context->attack == SubBruteAttackLoadFile) { - max_value = 0xFF; - } else { - string_t max_value_s; - string_init(max_value_s); - for(uint8_t i = 0; i < context->bit; i++) { - string_cat_printf(max_value_s, "1"); - } - max_value = (uint64_t)strtol(string_get_cstr(max_value_s), NULL, 2); - string_clear(max_value_s); - } - context->str_index = (context->key_index * 3); - string_init_set(context->candidate, context->key); - context->flipper_format = flipper_format_string_alloc(); - context->stream = flipper_format_get_raw_stream(context->flipper_format); - context->environment = subghz_environment_alloc(); - context->transmitter = subghz_transmitter_alloc_init( - context->environment, string_get_cstr(context->protocol)); - prepare_emit(context); - } else { - toSave = false; - } -} - void subbrute_scene_run_attack_on_exit(SubBruteState* context) { if(!toSave) { clear_emit(context); + furi_thread_free(context->bruthread); } } @@ -237,6 +221,65 @@ void subbrute_scene_run_attack_on_tick(SubBruteState* context) { subbrute_counter++; } } +void subbrute_run_timer(SubBruteState* context) { + while(true) { + if(context->close_thread_please) { + context->is_thread_running = false; + break; + } + //furi_delay_ms(10); + subbrute_scene_run_attack_on_tick(context); + } +} + +// entrypoint for worker +static int32_t subbrute_worker_thread(void* ctx) { + SubBruteState* app = ctx; + subbrute_run_timer(app); + //app->is_stop_running = false; + //app->request_exit = false; + return 0; +} + +void start_bruthread(SubBruteState* app) { + //furi_assert(!app->is_thread_running); + //app->is_stop_running = true; + if(!app->is_thread_running) { + furi_thread_start(app->bruthread); + app->is_thread_running = true; + } +} + +void subbrute_scene_run_attack_on_enter(SubBruteState* context) { + if(!toSave) { + if(context->attack == SubBruteAttackLoadFile) { + max_value = 0xFF; + } else { + string_t max_value_s; + string_init(max_value_s); + for(uint8_t i = 0; i < context->bit; i++) { + string_cat_printf(max_value_s, "1"); + } + max_value = (uint64_t)strtol(string_get_cstr(max_value_s), NULL, 2); + string_clear(max_value_s); + } + context->str_index = (context->key_index * 3); + string_init_set(context->candidate, context->key); + context->flipper_format = flipper_format_string_alloc(); + context->stream = flipper_format_get_raw_stream(context->flipper_format); + context->environment = subghz_environment_alloc(); + //context->transmitter = subghz_transmitter_alloc_init( + // context->environment, string_get_cstr(context->protocol)); + prepare_emit(context); + context->bruthread = furi_thread_alloc(); + furi_thread_set_name(context->bruthread, "SubBrute Worker"); + furi_thread_set_stack_size(context->bruthread, 2048); + furi_thread_set_context(context->bruthread, context); + furi_thread_set_callback(context->bruthread, subbrute_worker_thread); + } else { + toSave = false; + } +} void subbrute_scene_run_attack_on_event(SubBruteEvent event, SubBruteState* context) { if(event.evt_type == EventTypeKey) { @@ -266,16 +309,27 @@ void subbrute_scene_run_attack_on_event(SubBruteEvent event, SubBruteState* cont case InputKeyOk: if(!context->is_attacking) { context->is_attacking = true; + start_bruthread(context); notification_message(context->notify, &sequence_blink_start_blue); } else { context->is_attacking = false; + context->close_thread_please = true; + if(context->is_thread_running && context->bruthread) { + furi_thread_join(context->bruthread); // wait until thread is finished + } + context->close_thread_please = false; notification_message(context->notify, &sequence_blink_stop); notification_message(context->notify, &sequence_single_vibro); } break; case InputKeyBack: locked = false; + context->close_thread_please = true; context->is_attacking = false; + if(context->is_thread_running && context->bruthread) { + furi_thread_join(context->bruthread); // wait until thread is finished + } + context->close_thread_please = false; string_reset(context->notification_msg); context->payload = 0x00; subbrute_counter = 0; diff --git a/applications/subbrute/subbrute.c b/applications/subbrute/subbrute.c index 9bc4895e5..bf5c2295d 100644 --- a/applications/subbrute/subbrute.c +++ b/applications/subbrute/subbrute.c @@ -238,7 +238,7 @@ int32_t subbrute_start(void* p) { subbrute_scene_select_field_on_tick(subbrute_state); break; case SceneAttack: - subbrute_scene_run_attack_on_tick(subbrute_state); + //subbrute_scene_run_attack_on_tick(subbrute_state); break; case SceneEntryPoint: subbrute_scene_entrypoint_on_tick(subbrute_state); diff --git a/applications/subbrute/subbrute.h b/applications/subbrute/subbrute.h index dad5787d6..b6389a910 100644 --- a/applications/subbrute/subbrute.h +++ b/applications/subbrute/subbrute.h @@ -56,6 +56,8 @@ typedef struct { // Application stuff bool is_running; bool is_attacking; + bool is_thread_running; + bool close_thread_please; SubBruteScene current_scene; SubBruteScene previous_scene; NotificationApp* notify; @@ -65,6 +67,7 @@ typedef struct { Popup* popup; // SubGhz Stuff + FuriThread* bruthread; FlipperFormat* flipper_format; SubGhzEnvironment* environment; SubGhzTransmitter* transmitter; @@ -75,6 +78,7 @@ typedef struct { Stream* stream; string_t protocol; uint32_t frequency; + uint32_t frequency_cal; uint32_t repeat; uint32_t bit; string_t key;