nrfsniff.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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, FuriMessageQueue* event_queue) {
  110. furi_assert(event_queue);
  111. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  112. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  113. }
  114. static void hexlify(uint8_t* in, uint8_t size, char* out) {
  115. memset(out, 0, size * 2);
  116. for(int i = 0; i < size; i++)
  117. snprintf(out + strlen(out), sizeof(out + strlen(out)), "%02X", in[i]);
  118. }
  119. static bool save_addr_to_file(
  120. Storage* storage,
  121. uint8_t* data,
  122. uint8_t size,
  123. NotificationApp* notification) {
  124. size_t file_size = 0;
  125. uint8_t linesize = 0;
  126. char filepath[42] = {0};
  127. char addrline[14] = {0};
  128. char ending[4];
  129. uint8_t* file_contents;
  130. uint8_t rate = 1;
  131. Stream* stream = file_stream_alloc(storage);
  132. if(target_rate == 8) rate = 2;
  133. snprintf(ending, sizeof(ending), ",%d\n", rate);
  134. hexlify(data, size, addrline);
  135. strcat(addrline, ending);
  136. linesize = strlen(addrline);
  137. strcpy(filepath, NRFSNIFF_APP_PATH_FOLDER);
  138. strcat(filepath, "/");
  139. strcat(filepath, NRFSNIFF_APP_FILENAME);
  140. stream_seek(stream, 0, StreamOffsetFromStart);
  141. // check if address already exists in file
  142. if(file_stream_open(stream, filepath, FSAM_READ_WRITE, FSOM_OPEN_APPEND)) {
  143. bool found = false;
  144. file_size = stream_size(stream);
  145. stream_seek(stream, 0, StreamOffsetFromStart);
  146. if(file_size > 0) {
  147. file_contents = malloc(file_size + 1);
  148. memset(file_contents, 0, file_size + 1);
  149. if(stream_read(stream, file_contents, file_size) > 0) {
  150. char* line = strtok((char*)file_contents, "\n");
  151. while(line != NULL) {
  152. if(!memcmp(line, addrline, 12)) {
  153. found = true;
  154. break;
  155. }
  156. line = strtok(NULL, "\n");
  157. }
  158. }
  159. free(file_contents);
  160. }
  161. if(found) {
  162. FURI_LOG_I(TAG, "Address exists in file. Ending save process.");
  163. stream_free(stream);
  164. return false;
  165. } else {
  166. if(stream_write(stream, (uint8_t*)addrline, linesize) != linesize) {
  167. FURI_LOG_I(TAG, "Failed to write bytes to file stream.");
  168. stream_free(stream);
  169. return false;
  170. } else {
  171. FURI_LOG_I(TAG, "Found a new address: %s", addrline);
  172. FURI_LOG_I(TAG, "Save successful!");
  173. notification_message(notification, &sequence_success);
  174. stream_free(stream);
  175. unique_saved_count++;
  176. return true;
  177. }
  178. }
  179. } else {
  180. FURI_LOG_I(TAG, "Cannot open file \"%s\"", filepath);
  181. stream_free(stream);
  182. return false;
  183. }
  184. }
  185. void alt_address(uint8_t* addr, uint8_t* altaddr) {
  186. uint8_t macmess_hi_b[4];
  187. uint32_t macmess_hi;
  188. uint8_t macmess_lo;
  189. uint8_t preserved;
  190. uint8_t tmpaddr[5];
  191. // swap bytes
  192. for(int i = 0; i < 5; i++) tmpaddr[i] = addr[4 - i];
  193. // get address into 32-bit and 8-bit variables
  194. memcpy(macmess_hi_b, tmpaddr, 4);
  195. macmess_lo = tmpaddr[4];
  196. macmess_hi = bytes_to_int32(macmess_hi_b, true);
  197. //preserve lowest bit from hi to shift to low
  198. preserved = macmess_hi & 1;
  199. macmess_hi >>= 1;
  200. macmess_lo >>= 1;
  201. macmess_lo = (preserved << 7) | macmess_lo;
  202. int32_to_bytes(macmess_hi, macmess_hi_b, true);
  203. memcpy(tmpaddr, macmess_hi_b, 4);
  204. tmpaddr[4] = macmess_lo;
  205. // swap bytes back
  206. for(int i = 0; i < 5; i++) altaddr[i] = tmpaddr[4 - i];
  207. }
  208. static bool previously_confirmed(uint8_t* addr) {
  209. bool found = false;
  210. for(int i = 0; i < MAX_CONFIRMED; i++) {
  211. if(!memcmp(confirmed[i], addr, 5)) {
  212. found = true;
  213. break;
  214. }
  215. }
  216. return found;
  217. }
  218. static void wrap_up(Storage* storage, NotificationApp* notification) {
  219. uint8_t ch;
  220. uint8_t addr[5];
  221. uint8_t altaddr[5];
  222. char trying[12];
  223. int idx;
  224. uint8_t rate = 0;
  225. if(target_rate == 8) rate = 2;
  226. nrf24_set_idle(nrf24_HANDLE);
  227. while(true) {
  228. idx = get_highest_idx();
  229. if(counts[idx] < COUNT_THRESHOLD) break;
  230. counts[idx] = 0;
  231. memcpy(addr, candidates[idx], 5);
  232. hexlify(addr, 5, trying);
  233. FURI_LOG_I(TAG, "trying address %s", trying);
  234. //ch = nrf24_find_channel(nrf24_HANDLE, addr, addr, 5, rate, 2, LOGITECH_MAX_CHANNEL, false);
  235. ch = nrf24_find_channel(
  236. nrf24_HANDLE, addr, addr, 5, rate, MICROSOFT_MIN_CHANNEL, LOGITECH_MAX_CHANNEL, false);
  237. FURI_LOG_I(TAG, "find_channel returned %d", (int)ch);
  238. if(ch > LOGITECH_MAX_CHANNEL) {
  239. alt_address(addr, altaddr);
  240. hexlify(altaddr, 5, trying);
  241. FURI_LOG_I(TAG, "trying alternate address %s", trying);
  242. ch = nrf24_find_channel(
  243. //nrf24_HANDLE, altaddr, altaddr, 5, rate, 2, LOGITECH_MAX_CHANNEL, false);
  244. nrf24_HANDLE,
  245. altaddr,
  246. altaddr,
  247. 5,
  248. rate,
  249. MICROSOFT_MIN_CHANNEL,
  250. LOGITECH_MAX_CHANNEL,
  251. false);
  252. FURI_LOG_I(TAG, "find_channel returned %d", (int)ch);
  253. memcpy(addr, altaddr, 5);
  254. }
  255. if(ch <= LOGITECH_MAX_CHANNEL) {
  256. hexlify(addr, 5, top_address);
  257. found_count++;
  258. save_addr_to_file(storage, addr, 5, notification);
  259. if(confirmed_idx < MAX_CONFIRMED) memcpy(confirmed[confirmed_idx++], addr, 5);
  260. break;
  261. }
  262. }
  263. }
  264. static void clear_cache() {
  265. found_count = 0;
  266. unique_saved_count = 0;
  267. confirmed_idx = 0;
  268. candidate_idx = 0;
  269. //target_channel = 2;
  270. target_channel = MICROSOFT_MIN_CHANNEL;
  271. total_candidates = 0;
  272. memset(candidates, 0, sizeof(candidates));
  273. memset(counts, 0, sizeof(counts));
  274. memset(confirmed, 0, sizeof(confirmed));
  275. }
  276. static void start_sniffing() {
  277. nrf24_init_promisc_mode(nrf24_HANDLE, target_channel, target_rate);
  278. }
  279. int32_t nrfsniff_app(void* p) {
  280. UNUSED(p);
  281. uint8_t address[5] = {0};
  282. uint32_t start = 0;
  283. hexlify(address, 5, top_address);
  284. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  285. PluginState* plugin_state = malloc(sizeof(PluginState));
  286. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  287. if(!plugin_state->mutex) {
  288. furi_message_queue_free(event_queue);
  289. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  290. free(plugin_state);
  291. return 255;
  292. }
  293. nrf24_init();
  294. // Set system callbacks
  295. ViewPort* view_port = view_port_alloc();
  296. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  297. view_port_input_callback_set(view_port, input_callback, event_queue);
  298. // Open GUI and register view_port
  299. Gui* gui = furi_record_open(RECORD_GUI);
  300. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  301. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  302. Storage* storage = furi_record_open(RECORD_STORAGE);
  303. storage_common_mkdir(storage, NRFSNIFF_APP_PATH_FOLDER);
  304. PluginEvent event;
  305. for(bool processing = true; processing;) {
  306. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  307. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  308. if(event_status == FuriStatusOk) {
  309. // press events
  310. if(event.type == EventTypeKey) {
  311. if(event.input.type == InputTypePress ||
  312. (event.input.type == InputTypeLong && event.input.key == InputKeyBack)) {
  313. switch(event.input.key) {
  314. case InputKeyUp:
  315. // toggle rate 1/2Mbps
  316. if(!sniffing_state) {
  317. if(target_rate == 0)
  318. target_rate = 8;
  319. else
  320. target_rate = 0;
  321. }
  322. break;
  323. case InputKeyDown:
  324. // toggle preamble
  325. if(!sniffing_state) {
  326. if(target_preamble[0] == 0x55)
  327. target_preamble[0] = 0xAA;
  328. else
  329. target_preamble[0] = 0x55;
  330. nrf24_set_src_mac(nrf24_HANDLE, target_preamble, 2);
  331. }
  332. break;
  333. case InputKeyRight:
  334. // increment channel
  335. //if(!sniffing_state && target_channel <= LOGITECH_MAX_CHANNEL)
  336. // target_channel++;
  337. sample_time += 500;
  338. break;
  339. case InputKeyLeft:
  340. // decrement channel
  341. //if(!sniffing_state && target_channel > 0) target_channel--;
  342. if(sample_time > 500) sample_time -= 500;
  343. break;
  344. case InputKeyOk:
  345. // toggle sniffing
  346. sniffing_state = !sniffing_state;
  347. if(sniffing_state) {
  348. clear_cache();
  349. start_sniffing();
  350. start = furi_get_tick();
  351. } else
  352. wrap_up(storage, notification);
  353. break;
  354. case InputKeyBack:
  355. if(event.input.type == InputTypeLong) processing = false;
  356. break;
  357. default:
  358. break;
  359. }
  360. }
  361. }
  362. }
  363. if(sniffing_state) {
  364. if(nrf24_sniff_address(nrf24_HANDLE, 5, address)) {
  365. int idx;
  366. uint8_t* top_addr;
  367. if(!previously_confirmed(address)) {
  368. idx = get_addr_index(address, 5);
  369. if(idx == -1)
  370. insert_addr(address, 5);
  371. else
  372. counts[idx]++;
  373. top_addr = candidates[get_highest_idx()];
  374. hexlify(top_addr, 5, top_address);
  375. }
  376. }
  377. if(furi_get_tick() - start >= sample_time) {
  378. target_channel++;
  379. //if(target_channel > LOGITECH_MAX_CHANNEL) target_channel = 2;
  380. if(target_channel > LOGITECH_MAX_CHANNEL) target_channel = MICROSOFT_MIN_CHANNEL;
  381. {
  382. wrap_up(storage, notification);
  383. start_sniffing();
  384. }
  385. start = furi_get_tick();
  386. }
  387. }
  388. view_port_update(view_port);
  389. furi_mutex_release(plugin_state->mutex);
  390. }
  391. clear_cache();
  392. sample_time = DEFAULT_SAMPLE_TIME;
  393. target_rate = 8; // rate can be either 8 (2Mbps) or 0 (1Mbps)
  394. sniffing_state = false;
  395. nrf24_deinit();
  396. view_port_enabled_set(view_port, false);
  397. gui_remove_view_port(gui, view_port);
  398. furi_record_close(RECORD_GUI);
  399. furi_record_close(RECORD_NOTIFICATION);
  400. furi_record_close(RECORD_STORAGE);
  401. view_port_free(view_port);
  402. furi_message_queue_free(event_queue);
  403. furi_mutex_free(plugin_state->mutex);
  404. free(plugin_state);
  405. return 0;
  406. }