nrfsniff.c 15 KB

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