nrfsniff.c 16 KB

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