Skip to content

Commit

Permalink
See ROGUE-MASTER.NET for the BEST FLIPPER FIRMWARE!
Browse files Browse the repository at this point in the history
Refactor updates from xMasterX and Willy-JL
  • Loading branch information
RogueMaster committed Feb 20, 2024
1 parent 0d486c8 commit 3ff705e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 37 deletions.
13 changes: 12 additions & 1 deletion evil_portal_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <furi.h>
#include <furi_hal.h>
#include <expansion/expansion.h>

static bool evil_portal_app_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
Expand Down Expand Up @@ -79,7 +80,8 @@ Evil_PortalApp* evil_portal_app_alloc() {

void evil_portal_app_free(Evil_PortalApp* app) {
// Send reset event to dev board
evil_portal_uart_tx((uint8_t*)(RESET_CMD "\nstopscan\n"), strlen(RESET_CMD "\nstopscan\n"));
evil_portal_uart_tx(
app->uart, (uint8_t*)(RESET_CMD "\nstopscan\n"), strlen(RESET_CMD "\nstopscan\n"));

furi_assert(app);

Expand Down Expand Up @@ -120,6 +122,11 @@ void evil_portal_app_free(Evil_PortalApp* app) {

int32_t evil_portal_app(void* p) {
UNUSED(p);

// Disable expansion protocol to avoid interference with UART Handle
Expansion* expansion = furi_record_open(RECORD_EXPANSION);
expansion_disable(expansion);

Evil_PortalApp* evil_portal_app = evil_portal_app_alloc();

bool otg_was_enabled = furi_hal_power_is_otg_enabled();
Expand All @@ -144,5 +151,9 @@ int32_t evil_portal_app(void* p) {
furi_hal_power_disable_otg();
}

// Return previous state of expansion
expansion_enable(expansion);
furi_record_close(RECORD_EXPANSION);

return 0;
}
5 changes: 3 additions & 2 deletions evil_portal_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
#include "evil_portal_icons.h"

#include <gui/gui.h>
#include <gui/modules/loading.h>
#include <gui/modules/text_box.h>
#include <gui/modules/text_input.h>
#include <gui/modules/variable_item_list.h>
#include <gui/scene_manager.h>
#include <gui/view_dispatcher.h>
#include <gui/view_stack.h>
#include <dialogs/dialogs.h>
#include <cfw/cfw.h>

#define NUM_MENU_ITEMS (6)

#define EVIL_PORTAL_TEXT_BOX_STORE_SIZE (4096)
#define UART_CH \
(CFW_SETTINGS()->uart_esp_channel == UARTDefault ? FuriHalUartIdUSART1 : FuriHalUartIdLPUART1)
#define UART_CH (cfw_settings.uart_esp_channel)

#define SET_HTML_CMD "sethtml"
#define SET_AP_CMD "setap"
Expand Down
86 changes: 64 additions & 22 deletions evil_portal_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ struct Evil_PortalUart {
Evil_PortalApp* app;
FuriThread* rx_thread;
FuriStreamBuffer* rx_stream;
bool pcap;
uint8_t mark_test_buf[11];
uint8_t mark_test_idx;
uint8_t rx_buf[RX_BUF_SIZE + 1];
void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
FuriHalSerialHandle* serial_handle;
};

typedef enum {
Expand All @@ -24,12 +28,60 @@ void evil_portal_uart_set_handle_rx_data_cb(

#define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)

void evil_portal_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
void evil_portal_uart_on_irq_cb(
FuriHalSerialHandle* handle,
FuriHalSerialRxEvent event,
void* context) {
Evil_PortalUart* uart = (Evil_PortalUart*)context;

if(ev == UartIrqEventRXNE) {
furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
if(event == FuriHalSerialRxEventData) {
uint8_t data = furi_hal_serial_async_rx(handle);
const char* mark_begin = "[BUF/BEGIN]";
const char* mark_close = "[BUF/CLOSE]";
if(uart->mark_test_idx != 0) {
// We are trying to match a marker
if(data == mark_begin[uart->mark_test_idx] ||
data == mark_close[uart->mark_test_idx]) {
// Received char matches next char in a marker, append to test buffer
uart->mark_test_buf[uart->mark_test_idx++] = data;
if(uart->mark_test_idx == sizeof(uart->mark_test_buf)) {
// Test buffer reached max length, parse what marker this is and discard buffer
if(!memcmp(
uart->mark_test_buf, (void*)mark_begin, sizeof(uart->mark_test_buf))) {
uart->pcap = true;
} else if(!memcmp(
uart->mark_test_buf,
(void*)mark_close,
sizeof(uart->mark_test_buf))) {
uart->pcap = false;
}
uart->mark_test_idx = 0;
}
// Don't pass to stream
return;
} else {
// Received char doesn't match any expected next char, send current test buffer
if(!uart->pcap) {
furi_stream_buffer_send(
uart->rx_stream, uart->mark_test_buf, uart->mark_test_idx, 0);
furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
}
// Reset test buffer and try parsing this char from scratch
uart->mark_test_idx = 0;
}
}
// If we reach here the buffer is empty
if(data == mark_begin[0]) {
// Received marker start, append to test buffer
uart->mark_test_buf[uart->mark_test_idx++] = data;
} else {
// Not a marker start and we aren't matching a marker, this is just data
if(!uart->pcap) {
// We want to ignore pcap data from marauder
furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
}
}
}
}

Expand Down Expand Up @@ -75,14 +127,13 @@ static int32_t uart_worker(void* context) {
}
}

furi_hal_uart_set_irq_cb(UART_CH, NULL, NULL);
furi_stream_buffer_free(uart->rx_stream);

return 0;
}

void evil_portal_uart_tx(uint8_t* data, size_t len) {
furi_hal_uart_tx(UART_CH, data, len);
void evil_portal_uart_tx(Evil_PortalUart* uart, uint8_t* data, size_t len) {
furi_hal_serial_tx(uart->serial_handle, data, len);
}

Evil_PortalUart* evil_portal_uart_init(Evil_PortalApp* app) {
Expand All @@ -98,19 +149,13 @@ Evil_PortalUart* evil_portal_uart_init(Evil_PortalApp* app) {

furi_thread_start(uart->rx_thread);

if(UART_CH == FuriHalUartIdUSART1) {
furi_hal_console_disable();
} else if(UART_CH == FuriHalUartIdLPUART1) {
furi_hal_uart_init(UART_CH, app->BAUDRATE);
}

if(app->BAUDRATE == 0) {
app->BAUDRATE = 115200;
}
furi_hal_uart_set_br(UART_CH, app->BAUDRATE);
furi_hal_uart_set_irq_cb(UART_CH, evil_portal_uart_on_irq_cb, uart);

//evil_portal_uart_tx((uint8_t*)("XFW#EVILPORTAL=1\n"), strlen("XFW#EVILPORTAL=1\n"));
uart->serial_handle = furi_hal_serial_control_acquire(UART_CH);
furi_check(uart->serial_handle);
furi_hal_serial_init(uart->serial_handle, app->BAUDRATE);
furi_hal_serial_async_rx_start(uart->serial_handle, evil_portal_uart_on_irq_cb, uart, false);

return uart;
}
Expand All @@ -122,11 +167,8 @@ void evil_portal_uart_free(Evil_PortalUart* uart) {
furi_thread_join(uart->rx_thread);
furi_thread_free(uart->rx_thread);

if(UART_CH == FuriHalUartIdLPUART1) {
furi_hal_uart_deinit(UART_CH);
} else {
furi_hal_console_enable();
}
furi_hal_serial_deinit(uart->serial_handle);
furi_hal_serial_control_release(uart->serial_handle);

free(uart);
}
2 changes: 1 addition & 1 deletion evil_portal_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ typedef struct Evil_PortalUart Evil_PortalUart;
void evil_portal_uart_set_handle_rx_data_cb(
Evil_PortalUart* uart,
void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context));
void evil_portal_uart_tx(uint8_t* data, size_t len);
void evil_portal_uart_tx(Evil_PortalUart* uart, uint8_t* data, size_t len);
Evil_PortalUart* evil_portal_uart_init(Evil_PortalApp* app);
void evil_portal_uart_free(Evil_PortalUart* uart);
29 changes: 18 additions & 11 deletions scenes/evil_portal_scene_console_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ void evil_portal_scene_console_output_on_enter(void* context) {
// Test evil portal syntax and response, marauder ignores it
furi_string_printf(data, "setap=%s\n", (char*)app->ap_name);
furi_string_reset(app->captured_line);
evil_portal_uart_tx((uint8_t*)(furi_string_get_cstr(data)), furi_string_size(data));
evil_portal_uart_tx(
app->uart, (uint8_t*)(furi_string_get_cstr(data)), furi_string_size(data));
// TODO: move timeouts and commands elsewhere, can't block input cycle
for(uint8_t t = 0; t < 69 && !captured(app, "ap set") && !captured(app, "\n>"); t++)
furi_delay_ms(100);
Expand All @@ -131,7 +132,7 @@ void evil_portal_scene_console_output_on_enter(void* context) {
furi_string_printf(data, "clearlist -a -s -c\nssid -a -n '%s'\n", app->ap_name);
furi_string_reset(app->captured_line);
evil_portal_uart_tx(
(uint8_t*)(furi_string_get_cstr(data)), furi_string_size(data));
app->uart, (uint8_t*)(furi_string_get_cstr(data)), furi_string_size(data));
// Marauder echoes the command, maybe still init so wait a while for echo
for(uint8_t t = 0; t < 10 && !captured(app, (char*)app->ap_name); t++)
furi_delay_ms(100);
Expand All @@ -142,6 +143,7 @@ void evil_portal_scene_console_output_on_enter(void* context) {
if(icanhazmarauder) {
furi_string_reset(app->captured_line);
evil_portal_uart_tx(
app->uart,
(uint8_t*)("evilportal -c sethtmlstr\n"),
strlen("evilportal -c sethtmlstr\n"));
for(uint8_t t = 0; t < 10 && !captured(app, "\n>") &&
Expand All @@ -151,19 +153,24 @@ void evil_portal_scene_console_output_on_enter(void* context) {
// Check for active attack
if(!(captured(app, "\n>") && !captured(app, "Setting HTML from serial..."))) {
furi_string_reset(app->captured_line);
evil_portal_uart_tx(app->index_html, strlen((char*)app->index_html));
evil_portal_uart_tx((uint8_t*)("\n"), 1);
evil_portal_uart_tx(
app->uart, app->index_html, strlen((char*)app->index_html));
evil_portal_uart_tx(app->uart, (uint8_t*)("\n"), 1);
for(uint8_t t = 0; t < 20 && !captured(app, "html set"); t++)
furi_delay_ms(100);
evil_portal_uart_tx(
(uint8_t*)("evilportal -c start\n"), strlen("evilportal -c start\n"));
app->uart,
(uint8_t*)("evilportal -c start\n"),
strlen("evilportal -c start\n"));
}
} else {
furi_string_set(data, "sethtml=");
furi_string_cat(data, (char*)app->index_html);
evil_portal_uart_tx(
(uint8_t*)(furi_string_get_cstr(data)), strlen(furi_string_get_cstr(data)));
evil_portal_uart_tx((uint8_t*)("\n"), 1);
app->uart,
(uint8_t*)(furi_string_get_cstr(data)),
strlen(furi_string_get_cstr(data)));
evil_portal_uart_tx(app->uart, (uint8_t*)("\n"), 1);
}

free(app->index_html);
Expand All @@ -172,12 +179,12 @@ void evil_portal_scene_console_output_on_enter(void* context) {
furi_string_free(data);
} else if(0 == strncmp(RESET_CMD, app->selected_tx_string, strlen(RESET_CMD))) {
evil_portal_uart_tx(
(uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
evil_portal_uart_tx((uint8_t*)("\nstopscan\n"), strlen("\nstopscan\n"));
app->uart, (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
evil_portal_uart_tx(app->uart, (uint8_t*)("\nstopscan\n"), strlen("\nstopscan\n"));
} else if(1 == strncmp("help", app->selected_tx_string, strlen("help"))) {
evil_portal_uart_tx(
(uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
evil_portal_uart_tx((uint8_t*)("\n"), 1);
app->uart, (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
evil_portal_uart_tx(app->uart, (uint8_t*)("\n"), 1);
}
}
}
Expand Down

0 comments on commit 3ff705e

Please sign in to comment.