Browse Source

BleSpam: Fix connection issues + refactor

Willy-JL 1 năm trước cách đây
mục cha
commit
aff925a6c1
1 tập tin đã thay đổi với 37 bổ sung42 xóa
  1. 37 42
      ble_spam/ble_spam.c

+ 37 - 42
ble_spam/ble_spam.c

@@ -148,7 +148,6 @@ typedef struct {
     uint8_t lock_count;
     uint8_t lock_count;
     FuriTimer* lock_timer;
     FuriTimer* lock_timer;
 
 
-    // bool resume;
     bool advertising;
     bool advertising;
     uint8_t delay;
     uint8_t delay;
     GapExtraBeaconConfig config;
     GapExtraBeaconConfig config;
@@ -186,38 +185,52 @@ static void stop_blink(State* state) {
     notification_message_block(state->ctx.notification, &sequence_blink_stop);
     notification_message_block(state->ctx.notification, &sequence_blink_stop);
 }
 }
 
 
-static int32_t adv_thread(void* _ctx) {
-    State* state = _ctx;
+static void randomize_mac(State* state) {
+    furi_hal_random_fill_buf(state->config.address, sizeof(state->config.address));
+}
+
+static void start_extra_beacon(State* state) {
     uint8_t size;
     uint8_t size;
-    uint16_t delay;
     uint8_t* packet;
     uint8_t* packet;
+    uint16_t delay = delays[state->delay];
     GapExtraBeaconConfig* config = &state->config;
     GapExtraBeaconConfig* config = &state->config;
     Payload* payload = &attacks[state->index].payload;
     Payload* payload = &attacks[state->index].payload;
     const Protocol* protocol = attacks[state->index].protocol;
     const Protocol* protocol = attacks[state->index].protocol;
-    if(!payload->random_mac) furi_hal_random_fill_buf(config->address, sizeof(config->address));
+
+    config->min_adv_interval_ms = delay;
+    config->max_adv_interval_ms = delay * 1.5;
+    if(payload->random_mac) randomize_mac(state);
+    furi_check(furi_hal_bt_extra_beacon_set_config(config));
+
+    if(protocol) {
+        protocol->make_packet(&size, &packet, payload);
+    } else {
+        protocols[rand() % protocols_count]->make_packet(&size, &packet, NULL);
+    }
+    furi_check(furi_hal_bt_extra_beacon_set_data(packet, size));
+    free(packet);
+
+    furi_check(furi_hal_bt_extra_beacon_start());
+}
+
+static int32_t adv_thread(void* _ctx) {
+    State* state = _ctx;
+    Payload* payload = &attacks[state->index].payload;
+    const Protocol* protocol = attacks[state->index].protocol;
+    if(!payload->random_mac) randomize_mac(state);
     if(state->ctx.led_indicator) start_blink(state);
     if(state->ctx.led_indicator) start_blink(state);
 
 
     while(state->advertising) {
     while(state->advertising) {
-        if(protocol) {
-            if(payload->mode == PayloadModeBruteforce && payload->bruteforce.counter++ >= 10) {
-                payload->bruteforce.counter = 0;
-                payload->bruteforce.value =
-                    (payload->bruteforce.value + 1) % (1 << (payload->bruteforce.size * 8));
-            }
-            protocol->make_packet(&size, &packet, payload);
-        } else {
-            protocols[rand() % protocols_count]->make_packet(&size, &packet, NULL);
+        if(protocol && payload->mode == PayloadModeBruteforce &&
+           payload->bruteforce.counter++ >= 10) {
+            payload->bruteforce.counter = 0;
+            payload->bruteforce.value =
+                (payload->bruteforce.value + 1) % (1 << (payload->bruteforce.size * 8));
         }
         }
 
 
-        delay = delays[state->delay];
-        config->min_adv_interval_ms = config->max_adv_interval_ms = delay;
-        if(payload->random_mac) furi_hal_random_fill_buf(config->address, sizeof(config->address));
-        furi_check(furi_hal_bt_extra_beacon_set_config(config));
-        furi_check(furi_hal_bt_extra_beacon_set_data(packet, size));
-        free(packet);
+        start_extra_beacon(state);
 
 
-        furi_check(furi_hal_bt_extra_beacon_start());
-        furi_thread_flags_wait(true, FuriFlagWaitAny, delay);
+        furi_thread_flags_wait(true, FuriFlagWaitAny, delays[state->delay]);
         furi_hal_bt_extra_beacon_stop();
         furi_hal_bt_extra_beacon_stop();
     }
     }
 
 
@@ -230,11 +243,8 @@ static void toggle_adv(State* state) {
         state->advertising = false;
         state->advertising = false;
         furi_thread_flags_set(furi_thread_get_id(state->thread), true);
         furi_thread_flags_set(furi_thread_get_id(state->thread), true);
         furi_thread_join(state->thread);
         furi_thread_join(state->thread);
-        // if(state->resume) furi_hal_bt_start_advertising();
     } else {
     } else {
         state->advertising = true;
         state->advertising = true;
-        // state->resume = furi_hal_bt_is_active();
-        // furi_hal_bt_stop_advertising();
         furi_thread_start(state->thread);
         furi_thread_start(state->thread);
     }
     }
 }
 }
@@ -539,25 +549,11 @@ static bool input_callback(InputEvent* input, void* _ctx) {
                 }
                 }
             } else {
             } else {
                 if(!advertising) {
                 if(!advertising) {
-                    // bool resume = furi_hal_bt_is_active();
-                    // furi_hal_bt_stop_advertising();
-                    GapExtraBeaconConfig* config = &state->config;
                     Payload* payload = &attacks[state->index].payload;
                     Payload* payload = &attacks[state->index].payload;
-                    const Protocol* protocol = attacks[state->index].protocol;
-
-                    uint8_t size;
-                    uint8_t* packet;
-                    protocol->make_packet(&size, &packet, payload);
+                    if(input->type == InputTypeLong && !payload->random_mac) randomize_mac(state);
 
 
-                    uint16_t delay = delays[state->delay];
-                    config->min_adv_interval_ms = config->max_adv_interval_ms = delay;
-                    if(payload->random_mac || input->type == InputTypeLong)
-                        furi_hal_random_fill_buf(config->address, sizeof(config->address));
-                    furi_check(furi_hal_bt_extra_beacon_set_config(config));
-                    furi_check(furi_hal_bt_extra_beacon_set_data(packet, size));
-                    free(packet);
+                    start_extra_beacon(state);
 
 
-                    furi_check(furi_hal_bt_extra_beacon_start());
                     if(state->ctx.led_indicator)
                     if(state->ctx.led_indicator)
                         notification_message(state->ctx.notification, &solid_message);
                         notification_message(state->ctx.notification, &solid_message);
                     furi_delay_ms(10);
                     furi_delay_ms(10);
@@ -565,7 +561,6 @@ static bool input_callback(InputEvent* input, void* _ctx) {
 
 
                     if(state->ctx.led_indicator)
                     if(state->ctx.led_indicator)
                         notification_message_block(state->ctx.notification, &sequence_reset_rgb);
                         notification_message_block(state->ctx.notification, &sequence_reset_rgb);
-                    // if(resume) furi_hal_bt_start_advertising();
                 }
                 }
             }
             }
             break;
             break;