nrfsniff.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <notification/notification_messages.h>
  6. #include <stdlib.h>
  7. #include <nrf24.h>
  8. #include <toolbox/stream/file_stream.h>
  9. #define LOGITECH_MAX_CHANNEL 85
  10. #define COUNT_THRESHOLD 2
  11. #define DEFAULT_SAMPLE_TIME 4000
  12. #define MAX_ADDRS 100
  13. #define MAX_CONFIRMED 32
  14. #define NRFSNIFF_APP_PATH_FOLDER STORAGE_APP_DATA_PATH_PREFIX
  15. #define NRFSNIFF_APP_FILENAME "addresses.txt"
  16. #define TAG "nrfsniff"
  17. typedef enum {
  18. EventTypeTick,
  19. EventTypeKey,
  20. } EventType;
  21. typedef struct {
  22. EventType type;
  23. InputEvent input;
  24. } PluginEvent;
  25. typedef struct {
  26. FuriMutex* mutex;
  27. } PluginState;
  28. char rate_text_fmt[] = "Transfer rate: %dMbps";
  29. char sample_text_fmt[] = "Sample Time: %d ms";
  30. char channel_text_fmt[] = "Channel: %d Sniffing: %s";
  31. char preamble_text_fmt[] = "Preamble: %02X";
  32. char sniff_text_fmt[] = "Found: %d Unique: %u";
  33. char addresses_header_text[] = "Address,rate";
  34. char sniffed_address_fmt[] = "%s,%d";
  35. char rate_text[46];
  36. char channel_text[38];
  37. char sample_text[32];
  38. char preamble_text[14];
  39. char sniff_text[38];
  40. char sniffed_address[14];
  41. uint8_t target_channel = 0;
  42. uint32_t found_count = 0;
  43. uint32_t unique_saved_count = 0;
  44. uint32_t sample_time = DEFAULT_SAMPLE_TIME;
  45. uint8_t target_rate = 8; // rate can be either 8 (2Mbps) or 0 (1Mbps)
  46. uint8_t target_preamble[] = {0xAA, 0x00};
  47. uint8_t sniffing_state = false;
  48. char top_address[12];
  49. uint8_t candidates[MAX_ADDRS][5] = {0}; // last 100 sniffed addresses
  50. uint32_t counts[MAX_ADDRS];
  51. uint8_t confirmed[MAX_CONFIRMED][5] = {0}; // first 32 confirmed addresses
  52. uint8_t confirmed_idx = 0;
  53. uint32_t total_candidates = 0;
  54. uint32_t candidate_idx = 0;
  55. static int get_addr_index(uint8_t* addr, uint8_t addr_size) {
  56. for(uint32_t i = 0; i < total_candidates; i++) {
  57. uint8_t* arr_item = candidates[i];
  58. if(!memcmp(arr_item, addr, addr_size)) return i;
  59. }
  60. return -1;
  61. }
  62. static int get_highest_idx() {
  63. uint32_t highest = 0;
  64. int highest_idx = 0;
  65. for(uint32_t i = 0; i < total_candidates; i++) {
  66. if(counts[i] > highest) {
  67. highest = counts[i];
  68. highest_idx = i;
  69. }
  70. }
  71. return highest_idx;
  72. }
  73. // if array is full, start over from beginning
  74. static void insert_addr(uint8_t* addr, uint8_t addr_size) {
  75. if(candidate_idx >= MAX_ADDRS) candidate_idx = 0;
  76. memcpy(candidates[candidate_idx], addr, addr_size);
  77. counts[candidate_idx] = 1;
  78. if(total_candidates < MAX_ADDRS) total_candidates++;
  79. candidate_idx++;
  80. }
  81. static void render_callback(Canvas* const canvas, void* ctx) {
  82. furi_assert(ctx);
  83. const PluginState* plugin_state = ctx;
  84. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  85. uint8_t rate = 2;
  86. char sniffing[] = "Yes";
  87. // border around the edge of the screen
  88. canvas_draw_frame(canvas, 0, 0, 128, 64);
  89. canvas_set_font(canvas, FontSecondary);
  90. if(target_rate == 0) rate = 1;
  91. if(!sniffing_state) strcpy(sniffing, "No");
  92. snprintf(rate_text, sizeof(rate_text), rate_text_fmt, (int)rate);
  93. snprintf(channel_text, sizeof(channel_text), channel_text_fmt, (int)target_channel, sniffing);
  94. snprintf(sample_text, sizeof(sample_text), sample_text_fmt, (int)sample_time);
  95. //snprintf(preamble_text, sizeof(preamble_text), preamble_text_fmt, target_preamble[0]);
  96. snprintf(sniff_text, sizeof(sniff_text), sniff_text_fmt, found_count, unique_saved_count);
  97. snprintf(
  98. sniffed_address, sizeof(sniffed_address), sniffed_address_fmt, top_address, (int)rate);
  99. canvas_draw_str_aligned(canvas, 10, 10, AlignLeft, AlignBottom, rate_text);
  100. canvas_draw_str_aligned(canvas, 10, 20, AlignLeft, AlignBottom, sample_text);
  101. canvas_draw_str_aligned(canvas, 10, 30, AlignLeft, AlignBottom, channel_text);
  102. //canvas_draw_str_aligned(canvas, 10, 30, AlignLeft, AlignBottom, preamble_text);
  103. canvas_draw_str_aligned(canvas, 10, 40, AlignLeft, AlignBottom, sniff_text);
  104. canvas_draw_str_aligned(canvas, 30, 50, AlignLeft, AlignBottom, addresses_header_text);
  105. canvas_draw_str_aligned(canvas, 30, 60, AlignLeft, AlignBottom, sniffed_address);
  106. furi_mutex_release(plugin_state->mutex);
  107. }
  108. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  109. furi_assert(event_queue);
  110. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  111. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  112. }
  113. static void hexlify(uint8_t* in, uint8_t size, char* out) {
  114. memset(out, 0, size * 2);
  115. for(int i = 0; i < size; i++)
  116. snprintf(out + strlen(out), sizeof(out + strlen(out)), "%02X", in[i]);
  117. }
  118. static bool save_addr_to_file(
  119. Storage* storage,
  120. uint8_t* data,
  121. uint8_t size,
  122. NotificationApp* notification) {
  123. size_t file_size = 0;
  124. uint8_t linesize = 0;
  125. char filepath[42] = {0};
  126. char addrline[14] = {0};
  127. char ending[4];
  128. uint8_t* file_contents;
  129. uint8_t rate = 1;
  130. Stream* stream = file_stream_alloc(storage);
  131. if(target_rate == 8) rate = 2;
  132. snprintf(ending, sizeof(ending), ",%d\n", rate);
  133. hexlify(data, size, addrline);
  134. strcat(addrline, ending);
  135. linesize = strlen(addrline);
  136. strcpy(filepath, NRFSNIFF_APP_PATH_FOLDER);
  137. strcat(filepath, "/");
  138. strcat(filepath, NRFSNIFF_APP_FILENAME);
  139. stream_seek(stream, 0, StreamOffsetFromStart);
  140. // check if address already exists in file
  141. if(file_stream_open(stream, filepath, FSAM_READ_WRITE, FSOM_OPEN_APPEND)) {
  142. bool found = false;
  143. file_size = stream_size(stream);
  144. stream_seek(stream, 0, StreamOffsetFromStart);
  145. if(file_size > 0) {
  146. file_contents = malloc(file_size + 1);
  147. memset(file_contents, 0, file_size + 1);
  148. if(stream_read(stream, file_contents, file_size) > 0) {
  149. char* line = strtok((char*)file_contents, "\n");
  150. while(line != NULL) {
  151. if(!memcmp(line, addrline, 12)) {
  152. found = true;
  153. break;
  154. }
  155. line = strtok(NULL, "\n");
  156. }
  157. }
  158. free(file_contents);
  159. }
  160. if(found) {
  161. FURI_LOG_I(TAG, "Address exists in file. Ending save process.");
  162. stream_free(stream);
  163. return false;
  164. } else {
  165. if(stream_write(stream, (uint8_t*)addrline, linesize) != linesize) {
  166. FURI_LOG_I(TAG, "Failed to write bytes to file stream.");
  167. stream_free(stream);
  168. return false;
  169. } else {
  170. FURI_LOG_I(TAG, "Found a new address: %s", addrline);
  171. FURI_LOG_I(TAG, "Save successful!");
  172. notification_message(notification, &sequence_success);
  173. stream_free(stream);
  174. unique_saved_count++;
  175. return true;
  176. }
  177. }
  178. } else {
  179. FURI_LOG_I(TAG, "Cannot open file \"%s\"", filepath);
  180. stream_free(stream);
  181. return false;
  182. }
  183. }
  184. void alt_address(uint8_t* addr, uint8_t* altaddr) {
  185. uint8_t macmess_hi_b[4];
  186. uint32_t macmess_hi;
  187. uint8_t macmess_lo;
  188. uint8_t preserved;
  189. uint8_t tmpaddr[5];
  190. // swap bytes
  191. for(int i = 0; i < 5; i++) tmpaddr[i] = addr[4 - i];
  192. // get address into 32-bit and 8-bit variables
  193. memcpy(macmess_hi_b, tmpaddr, 4);
  194. macmess_lo = tmpaddr[4];
  195. macmess_hi = bytes_to_int32(macmess_hi_b, true);
  196. //preserve lowest bit from hi to shift to low
  197. preserved = macmess_hi & 1;
  198. macmess_hi >>= 1;
  199. macmess_lo >>= 1;
  200. macmess_lo = (preserved << 7) | macmess_lo;
  201. int32_to_bytes(macmess_hi, macmess_hi_b, true);
  202. memcpy(tmpaddr, macmess_hi_b, 4);
  203. tmpaddr[4] = macmess_lo;
  204. // swap bytes back
  205. for(int i = 0; i < 5; i++) altaddr[i] = tmpaddr[4 - i];
  206. }
  207. static bool previously_confirmed(uint8_t* addr) {
  208. bool found = false;
  209. for(int i = 0; i < MAX_CONFIRMED; i++) {
  210. if(!memcmp(confirmed[i], addr, 5)) {
  211. found = true;
  212. break;
  213. }
  214. }
  215. return found;
  216. }
  217. static void wrap_up(Storage* storage, NotificationApp* notification) {
  218. uint8_t ch;
  219. uint8_t addr[5];
  220. uint8_t altaddr[5];
  221. char trying[12];
  222. int idx;
  223. uint8_t rate = 0;
  224. if(target_rate == 8) rate = 2;
  225. nrf24_set_idle(nrf24_HANDLE);
  226. while(true) {
  227. idx = get_highest_idx();
  228. if(counts[idx] < COUNT_THRESHOLD) break;
  229. counts[idx] = 0;
  230. memcpy(addr, candidates[idx], 5);
  231. hexlify(addr, 5, trying);
  232. FURI_LOG_I(TAG, "trying address %s", trying);
  233. ch = nrf24_find_channel(nrf24_HANDLE, addr, addr, 5, rate, 2, LOGITECH_MAX_CHANNEL, false);
  234. FURI_LOG_I(TAG, "find_channel returned %d", (int)ch);
  235. if(ch > LOGITECH_MAX_CHANNEL) {
  236. alt_address(addr, altaddr);
  237. hexlify(altaddr, 5, trying);
  238. FURI_LOG_I(TAG, "trying alternate address %s", trying);
  239. ch = nrf24_find_channel(
  240. nrf24_HANDLE, altaddr, altaddr, 5, rate, 2, LOGITECH_MAX_CHANNEL, false);
  241. FURI_LOG_I(TAG, "find_channel returned %d", (int)ch);
  242. memcpy(addr, altaddr, 5);
  243. }
  244. if(ch <= LOGITECH_MAX_CHANNEL) {
  245. hexlify(addr, 5, top_address);
  246. found_count++;
  247. save_addr_to_file(storage, addr, 5, notification);
  248. if(confirmed_idx < MAX_CONFIRMED) memcpy(confirmed[confirmed_idx++], addr, 5);
  249. break;
  250. }
  251. }
  252. }
  253. static void clear_cache() {
  254. found_count = 0;
  255. unique_saved_count = 0;
  256. confirmed_idx = 0;
  257. candidate_idx = 0;
  258. target_channel = 2;
  259. total_candidates = 0;
  260. memset(candidates, 0, sizeof(candidates));
  261. memset(counts, 0, sizeof(counts));
  262. memset(confirmed, 0, sizeof(confirmed));
  263. }
  264. static void start_sniffing() {
  265. nrf24_init_promisc_mode(nrf24_HANDLE, target_channel, target_rate);
  266. }
  267. int32_t nrfsniff_app(void* p) {
  268. UNUSED(p);
  269. uint8_t address[5] = {0};
  270. uint32_t start = 0;
  271. hexlify(address, 5, top_address);
  272. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  273. PluginState* plugin_state = malloc(sizeof(PluginState));
  274. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  275. if(!plugin_state->mutex) {
  276. furi_message_queue_free(event_queue);
  277. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  278. free(plugin_state);
  279. return 255;
  280. }
  281. uint8_t attempts = 0;
  282. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  283. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  284. furi_hal_power_enable_otg();
  285. furi_delay_ms(10);
  286. }
  287. furi_delay_ms(100);
  288. nrf24_init();
  289. bool nrf_ready = false;
  290. if(nrf24_check_connected(nrf24_HANDLE)) {
  291. nrf_ready = true;
  292. } else {
  293. nrf_ready = false;
  294. FURI_LOG_E(TAG, "NRF24 not connected");
  295. }
  296. // Set system callbacks
  297. ViewPort* view_port = view_port_alloc();
  298. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  299. view_port_input_callback_set(view_port, input_callback, event_queue);
  300. // Open GUI and register view_port
  301. Gui* gui = furi_record_open(RECORD_GUI);
  302. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  303. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  304. Storage* storage = furi_record_open(RECORD_STORAGE);
  305. storage_common_migrate(storage, EXT_PATH("nrfsniff"), NRFSNIFF_APP_PATH_FOLDER);
  306. storage_common_mkdir(storage, NRFSNIFF_APP_PATH_FOLDER);
  307. PluginEvent event;
  308. for(bool processing = true; processing;) {
  309. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  310. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  311. if(event_status == FuriStatusOk) {
  312. // press events
  313. if(event.type == EventTypeKey) {
  314. if(event.input.type == InputTypePress ||
  315. (event.input.type == InputTypeLong && event.input.key == InputKeyBack)) {
  316. switch(event.input.key) {
  317. case InputKeyUp:
  318. // toggle rate 1/2Mbps
  319. if(!sniffing_state) {
  320. if(target_rate == 0)
  321. target_rate = 8;
  322. else
  323. target_rate = 0;
  324. }
  325. break;
  326. case InputKeyDown:
  327. // toggle preamble
  328. if(!sniffing_state) {
  329. if(target_preamble[0] == 0x55)
  330. target_preamble[0] = 0xAA;
  331. else
  332. target_preamble[0] = 0x55;
  333. nrf24_set_src_mac(nrf24_HANDLE, target_preamble, 2);
  334. }
  335. break;
  336. case InputKeyRight:
  337. // increment channel
  338. //if(!sniffing_state && target_channel <= LOGITECH_MAX_CHANNEL)
  339. // target_channel++;
  340. sample_time += 500;
  341. break;
  342. case InputKeyLeft:
  343. // decrement channel
  344. //if(!sniffing_state && target_channel > 0) target_channel--;
  345. if(sample_time > 500) sample_time -= 500;
  346. break;
  347. case InputKeyOk:
  348. // toggle sniffing
  349. if(nrf_ready) {
  350. sniffing_state = !sniffing_state;
  351. if(sniffing_state) {
  352. clear_cache();
  353. start_sniffing();
  354. start = furi_get_tick();
  355. } else {
  356. wrap_up(storage, notification);
  357. }
  358. } else {
  359. notification_message(notification, &sequence_error);
  360. if(nrf24_check_connected(nrf24_HANDLE)) {
  361. nrf_ready = true;
  362. } else {
  363. nrf_ready = false;
  364. FURI_LOG_E(TAG, "NRF24 not connected");
  365. }
  366. }
  367. break;
  368. case InputKeyBack:
  369. if(nrf_ready) {
  370. if(sniffing_state) {
  371. wrap_up(storage, notification);
  372. }
  373. } else {
  374. if(nrf24_check_connected(nrf24_HANDLE)) {
  375. nrf_ready = true;
  376. } else {
  377. nrf_ready = false;
  378. FURI_LOG_E(TAG, "NRF24 not connected");
  379. }
  380. }
  381. processing = false;
  382. break;
  383. default:
  384. break;
  385. }
  386. }
  387. }
  388. }
  389. if(sniffing_state) {
  390. if(nrf24_sniff_address(nrf24_HANDLE, 5, address)) {
  391. int idx;
  392. uint8_t* top_addr;
  393. if(!previously_confirmed(address)) {
  394. idx = get_addr_index(address, 5);
  395. if(idx == -1)
  396. insert_addr(address, 5);
  397. else
  398. counts[idx]++;
  399. top_addr = candidates[get_highest_idx()];
  400. hexlify(top_addr, 5, top_address);
  401. }
  402. }
  403. if(furi_get_tick() - start >= sample_time) {
  404. target_channel++;
  405. if(target_channel > LOGITECH_MAX_CHANNEL) target_channel = 2;
  406. {
  407. wrap_up(storage, notification);
  408. start_sniffing();
  409. }
  410. start = furi_get_tick();
  411. }
  412. }
  413. furi_mutex_release(plugin_state->mutex);
  414. view_port_update(view_port);
  415. }
  416. clear_cache();
  417. sample_time = DEFAULT_SAMPLE_TIME;
  418. target_rate = 8; // rate can be either 8 (2Mbps) or 0 (1Mbps)
  419. sniffing_state = false;
  420. nrf24_deinit();
  421. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  422. furi_hal_power_disable_otg();
  423. }
  424. view_port_enabled_set(view_port, false);
  425. gui_remove_view_port(gui, view_port);
  426. furi_record_close(RECORD_GUI);
  427. furi_record_close(RECORD_NOTIFICATION);
  428. furi_record_close(RECORD_STORAGE);
  429. view_port_free(view_port);
  430. furi_message_queue_free(event_queue);
  431. furi_mutex_free(plugin_state->mutex);
  432. free(plugin_state);
  433. return 0;
  434. }