init_plugin.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include <furi_hal.h>
  2. #include <inttypes.h>
  3. #include <toolbox/keys_dict.h>
  4. #include <toolbox/stream/buffered_file_stream.h>
  5. #include <nfc/protocols/mf_classic/mf_classic.h>
  6. #include "mfkey.h"
  7. #include "common.h"
  8. #include "crypto1.h"
  9. #include "plugin_interface.h"
  10. #include <flipper_application/flipper_application.h>
  11. // TODO: Remove defines that are not needed
  12. #define KEYS_DICT_SYSTEM_PATH EXT_PATH("nfc/assets/mf_classic_dict.nfc")
  13. #define KEYS_DICT_USER_PATH EXT_PATH("nfc/assets/mf_classic_dict_user.nfc")
  14. #define MF_CLASSIC_NONCE_PATH EXT_PATH("nfc/.mfkey32.log")
  15. #define MF_CLASSIC_NESTED_NONCE_PATH EXT_PATH("nfc/.nested")
  16. #define TAG "MFKey"
  17. #define MAX_NAME_LEN 32
  18. #define MAX_PATH_LEN 64
  19. #define LF_POLY_ODD (0x29CE5C)
  20. #define LF_POLY_EVEN (0x870804)
  21. #define CONST_M1_1 (LF_POLY_EVEN << 1 | 1)
  22. #define CONST_M2_1 (LF_POLY_ODD << 1)
  23. #define CONST_M1_2 (LF_POLY_ODD)
  24. #define CONST_M2_2 (LF_POLY_EVEN << 1 | 1)
  25. #define BIT(x, n) ((x) >> (n) & 1)
  26. #define BEBIT(x, n) BIT(x, (n) ^ 24)
  27. #define SWAPENDIAN(x) \
  28. ((x) = ((x) >> 8 & 0xff00ff) | ((x) & 0xff00ff) << 8, (x) = (x) >> 16 | (x) << 16)
  29. bool key_already_found_for_nonce_in_dict(KeysDict* dict, MfClassicNonce* nonce) {
  30. bool found = false;
  31. uint8_t key_bytes[sizeof(MfClassicKey)];
  32. keys_dict_rewind(dict);
  33. while(keys_dict_get_next_key(dict, key_bytes, sizeof(MfClassicKey))) {
  34. uint64_t k = napi_nfc_util_bytes2num(key_bytes, sizeof(MfClassicKey));
  35. struct Crypto1State temp = {0, 0};
  36. for(int i = 0; i < 24; i++) {
  37. (&temp)->odd |= (BIT(k, 2 * i + 1) << (i ^ 3));
  38. (&temp)->even |= (BIT(k, 2 * i) << (i ^ 3));
  39. }
  40. if(nonce->attack == mfkey32) {
  41. crypt_word_noret(&temp, nonce->uid_xor_nt1, 0);
  42. crypt_word_noret(&temp, nonce->nr1_enc, 1);
  43. if(nonce->ar1_enc == (crypt_word(&temp) ^ nonce->p64b)) {
  44. found = true;
  45. break;
  46. }
  47. } else if(nonce->attack == static_nested) {
  48. uint32_t expected_ks1 = crypt_word_ret(&temp, nonce->uid_xor_nt0, 0);
  49. if(nonce->ks1_1_enc == expected_ks1) {
  50. found = true;
  51. break;
  52. }
  53. }
  54. }
  55. return found;
  56. }
  57. bool napi_mf_classic_mfkey32_nonces_check_presence() {
  58. Storage* storage = furi_record_open(RECORD_STORAGE);
  59. bool nonces_present = storage_common_stat(storage, MF_CLASSIC_NONCE_PATH, NULL) == FSE_OK;
  60. furi_record_close(RECORD_STORAGE);
  61. return nonces_present;
  62. }
  63. bool distance_in_nonces_file(const char* file_path, const char* file_name) {
  64. char full_path[MAX_PATH_LEN];
  65. snprintf(full_path, sizeof(full_path), "%s/%s", file_path, file_name);
  66. bool distance_present = false;
  67. Storage* storage = furi_record_open(RECORD_STORAGE);
  68. Stream* file_stream = buffered_file_stream_alloc(storage);
  69. FuriString* line_str;
  70. line_str = furi_string_alloc();
  71. if(buffered_file_stream_open(file_stream, full_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  72. while(true) {
  73. if(!stream_read_line(file_stream, line_str)) break;
  74. if(furi_string_search_str(line_str, "distance") != FURI_STRING_FAILURE) {
  75. distance_present = true;
  76. break;
  77. }
  78. }
  79. }
  80. buffered_file_stream_close(file_stream);
  81. stream_free(file_stream);
  82. furi_string_free(line_str);
  83. furi_record_close(RECORD_STORAGE);
  84. return distance_present;
  85. }
  86. bool napi_mf_classic_nested_nonces_check_presence() {
  87. Storage* storage = furi_record_open(RECORD_STORAGE);
  88. if(!(storage_dir_exists(storage, MF_CLASSIC_NESTED_NONCE_PATH))) {
  89. furi_record_close(RECORD_STORAGE);
  90. return false;
  91. }
  92. bool nonces_present = false;
  93. File* dir = storage_file_alloc(storage);
  94. char filename_buffer[MAX_NAME_LEN];
  95. FileInfo file_info;
  96. if(storage_dir_open(dir, MF_CLASSIC_NESTED_NONCE_PATH)) {
  97. while(storage_dir_read(dir, &file_info, filename_buffer, MAX_NAME_LEN)) {
  98. // We only care about Static Nested files
  99. if(!(file_info.flags & FSF_DIRECTORY) && strstr(filename_buffer, ".nonces") &&
  100. !(distance_in_nonces_file(MF_CLASSIC_NESTED_NONCE_PATH, filename_buffer))) {
  101. nonces_present = true;
  102. break;
  103. }
  104. }
  105. }
  106. storage_dir_close(dir);
  107. storage_file_free(dir);
  108. furi_record_close(RECORD_STORAGE);
  109. return nonces_present;
  110. }
  111. int binaryStringToInt(const char* binStr) {
  112. int result = 0;
  113. while(*binStr) {
  114. result <<= 1;
  115. if(*binStr == '1') {
  116. result |= 1;
  117. }
  118. binStr++;
  119. }
  120. return result;
  121. }
  122. bool load_mfkey32_nonces(
  123. MfClassicNonceArray* nonce_array,
  124. ProgramState* program_state,
  125. KeysDict* system_dict,
  126. bool system_dict_exists,
  127. KeysDict* user_dict) {
  128. bool array_loaded = false;
  129. do {
  130. // https://github.com/flipperdevices/flipperzero-firmware/blob/5134f44c09d39344a8747655c0d59864bb574b96/applications/services/storage/filesystem_api_defines.h#L8-L22
  131. if(!buffered_file_stream_open(
  132. nonce_array->stream, MF_CLASSIC_NONCE_PATH, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
  133. buffered_file_stream_close(nonce_array->stream);
  134. break;
  135. }
  136. // Check for newline ending
  137. if(!stream_eof(nonce_array->stream)) {
  138. if(!stream_seek(nonce_array->stream, -1, StreamOffsetFromEnd)) break;
  139. uint8_t last_char = 0;
  140. if(stream_read(nonce_array->stream, &last_char, 1) != 1) break;
  141. if(last_char != '\n') {
  142. //FURI_LOG_D(TAG, "Adding new line ending");
  143. if(stream_write_char(nonce_array->stream, '\n') != 1) break;
  144. }
  145. if(!stream_rewind(nonce_array->stream)) break;
  146. }
  147. // Read total amount of nonces
  148. FuriString* next_line;
  149. next_line = furi_string_alloc();
  150. while(!(program_state->close_thread_please)) {
  151. if(!stream_read_line(nonce_array->stream, next_line)) {
  152. //FURI_LOG_T(TAG, "No nonces left");
  153. break;
  154. }
  155. /*
  156. FURI_LOG_T(
  157. TAG,
  158. "Read line: %s, len: %zu",
  159. furi_string_get_cstr(next_line),
  160. furi_string_size(next_line));
  161. */
  162. if(!furi_string_start_with_str(next_line, "Sec")) continue;
  163. const char* next_line_cstr = furi_string_get_cstr(next_line);
  164. MfClassicNonce res = {0};
  165. res.attack = mfkey32;
  166. int i = 0;
  167. char* endptr;
  168. for(i = 0; i <= 17; i++) {
  169. if(i != 0) {
  170. next_line_cstr = strchr(next_line_cstr, ' ');
  171. if(next_line_cstr) {
  172. next_line_cstr++;
  173. } else {
  174. break;
  175. }
  176. }
  177. unsigned long value = strtoul(next_line_cstr, &endptr, 16);
  178. switch(i) {
  179. case 5:
  180. res.uid = value;
  181. break;
  182. case 7:
  183. res.nt0 = value;
  184. break;
  185. case 9:
  186. res.nr0_enc = value;
  187. break;
  188. case 11:
  189. res.ar0_enc = value;
  190. break;
  191. case 13:
  192. res.nt1 = value;
  193. break;
  194. case 15:
  195. res.nr1_enc = value;
  196. break;
  197. case 17:
  198. res.ar1_enc = value;
  199. break;
  200. default:
  201. break; // Do nothing
  202. }
  203. next_line_cstr = endptr;
  204. }
  205. res.p64 = prng_successor(res.nt0, 64);
  206. res.p64b = prng_successor(res.nt1, 64);
  207. res.uid_xor_nt0 = res.uid ^ res.nt0;
  208. res.uid_xor_nt1 = res.uid ^ res.nt1;
  209. (program_state->total)++;
  210. if((system_dict_exists && key_already_found_for_nonce_in_dict(system_dict, &res)) ||
  211. (key_already_found_for_nonce_in_dict(user_dict, &res))) {
  212. (program_state->cracked)++;
  213. (program_state->num_completed)++;
  214. continue;
  215. }
  216. //FURI_LOG_I(TAG, "No key found for %8lx %8lx", res.uid, res.ar1_enc);
  217. // TODO: Refactor
  218. nonce_array->remaining_nonce_array = realloc( //-V701
  219. nonce_array->remaining_nonce_array,
  220. sizeof(MfClassicNonce) * ((nonce_array->remaining_nonces) + 1));
  221. nonce_array->remaining_nonces++;
  222. nonce_array->remaining_nonce_array[(nonce_array->remaining_nonces) - 1] = res;
  223. nonce_array->total_nonces++;
  224. }
  225. furi_string_free(next_line);
  226. buffered_file_stream_close(nonce_array->stream);
  227. //stream_free(nonce_array->stream);
  228. array_loaded = true;
  229. //FURI_LOG_I(TAG, "Loaded %lu Mfkey32 nonces", nonce_array->total_nonces);
  230. } while(false);
  231. return array_loaded;
  232. }
  233. bool load_nested_nonces(
  234. MfClassicNonceArray* nonce_array,
  235. ProgramState* program_state,
  236. KeysDict* system_dict,
  237. bool system_dict_exists,
  238. KeysDict* user_dict) {
  239. Storage* storage = furi_record_open(RECORD_STORAGE);
  240. File* dir = storage_file_alloc(storage);
  241. char filename_buffer[MAX_NAME_LEN];
  242. FileInfo file_info;
  243. FuriString* next_line = furi_string_alloc();
  244. if(!storage_dir_open(dir, MF_CLASSIC_NESTED_NONCE_PATH)) {
  245. storage_dir_close(dir);
  246. storage_file_free(dir);
  247. furi_record_close(RECORD_STORAGE);
  248. furi_string_free(next_line);
  249. return false;
  250. }
  251. while(storage_dir_read(dir, &file_info, filename_buffer, MAX_NAME_LEN)) {
  252. if(!(file_info.flags & FSF_DIRECTORY) && strstr(filename_buffer, ".nonces") &&
  253. !(distance_in_nonces_file(MF_CLASSIC_NESTED_NONCE_PATH, filename_buffer))) {
  254. char full_path[MAX_PATH_LEN];
  255. snprintf(
  256. full_path,
  257. sizeof(full_path),
  258. "%s/%s",
  259. MF_CLASSIC_NESTED_NONCE_PATH,
  260. filename_buffer);
  261. // TODO: We should only need READ_WRITE here if we plan on adding a newline to the end of the file if has none
  262. if(!buffered_file_stream_open(
  263. nonce_array->stream, full_path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
  264. buffered_file_stream_close(nonce_array->stream);
  265. continue;
  266. }
  267. while(stream_read_line(nonce_array->stream, next_line)) {
  268. if(furi_string_search_str(next_line, "Nested:") != FURI_STRING_FAILURE) {
  269. MfClassicNonce res = {0};
  270. res.attack = static_nested;
  271. int parsed = sscanf(
  272. furi_string_get_cstr(next_line),
  273. "Nested: %*s %*s cuid 0x%" PRIx32 " nt0 0x%" PRIx32 " ks0 0x%" PRIx32
  274. " par0 %4[01] nt1 0x%" PRIx32 " ks1 0x%" PRIx32 " par1 %4[01]",
  275. &res.uid,
  276. &res.nt0,
  277. &res.ks1_1_enc,
  278. res.par_1_str,
  279. &res.nt1,
  280. &res.ks1_2_enc,
  281. res.par_2_str);
  282. if(parsed != 7) continue;
  283. res.par_1 = binaryStringToInt(res.par_1_str);
  284. res.par_2 = binaryStringToInt(res.par_2_str);
  285. res.uid_xor_nt0 = res.uid ^ res.nt0;
  286. res.uid_xor_nt1 = res.uid ^ res.nt1;
  287. (program_state->total)++;
  288. if((system_dict_exists &&
  289. key_already_found_for_nonce_in_dict(system_dict, &res)) ||
  290. (key_already_found_for_nonce_in_dict(user_dict, &res))) {
  291. (program_state->cracked)++;
  292. (program_state->num_completed)++;
  293. continue;
  294. }
  295. nonce_array->remaining_nonce_array = realloc(
  296. nonce_array->remaining_nonce_array,
  297. sizeof(MfClassicNonce) * (nonce_array->remaining_nonces + 1));
  298. nonce_array->remaining_nonce_array[nonce_array->remaining_nonces] = res;
  299. nonce_array->remaining_nonces++;
  300. nonce_array->total_nonces++;
  301. }
  302. }
  303. buffered_file_stream_close(nonce_array->stream);
  304. }
  305. }
  306. storage_dir_close(dir);
  307. storage_file_free(dir);
  308. furi_record_close(RECORD_STORAGE);
  309. furi_string_free(next_line);
  310. //FURI_LOG_I(TAG, "Loaded %lu Static Nested nonces", nonce_array->total_nonces);
  311. return true;
  312. }
  313. MfClassicNonceArray* napi_mf_classic_nonce_array_alloc(
  314. KeysDict* system_dict,
  315. bool system_dict_exists,
  316. KeysDict* user_dict,
  317. ProgramState* program_state) {
  318. MfClassicNonceArray* nonce_array = malloc(sizeof(MfClassicNonceArray));
  319. MfClassicNonce* remaining_nonce_array_init = malloc(sizeof(MfClassicNonce) * 1);
  320. nonce_array->remaining_nonce_array = remaining_nonce_array_init;
  321. Storage* storage = furi_record_open(RECORD_STORAGE);
  322. nonce_array->stream = buffered_file_stream_alloc(storage);
  323. furi_record_close(RECORD_STORAGE);
  324. bool array_loaded = false;
  325. if(program_state->mfkey32_present) {
  326. array_loaded = load_mfkey32_nonces(
  327. nonce_array, program_state, system_dict, system_dict_exists, user_dict);
  328. }
  329. if(program_state->nested_present) {
  330. array_loaded |= load_nested_nonces(
  331. nonce_array, program_state, system_dict, system_dict_exists, user_dict);
  332. }
  333. if(!array_loaded) {
  334. free(nonce_array);
  335. nonce_array = NULL;
  336. }
  337. return nonce_array;
  338. }
  339. void napi_mf_classic_nonce_array_free(MfClassicNonceArray* nonce_array) {
  340. // TODO: Track free state at the time this is called to ensure double free does not happen
  341. furi_assert(nonce_array);
  342. furi_assert(nonce_array->stream);
  343. buffered_file_stream_close(nonce_array->stream);
  344. stream_free(nonce_array->stream);
  345. free(nonce_array);
  346. }
  347. /* Actual implementation of app<>plugin interface */
  348. static const MfkeyPlugin init_plugin = {
  349. .name = "Initialization Plugin",
  350. .napi_mf_classic_mfkey32_nonces_check_presence =
  351. &napi_mf_classic_mfkey32_nonces_check_presence,
  352. .napi_mf_classic_nested_nonces_check_presence = &napi_mf_classic_nested_nonces_check_presence,
  353. .napi_mf_classic_nonce_array_alloc = &napi_mf_classic_nonce_array_alloc,
  354. .napi_mf_classic_nonce_array_free = &napi_mf_classic_nonce_array_free,
  355. };
  356. /* Plugin descriptor to comply with basic plugin specification */
  357. static const FlipperAppPluginDescriptor init_plugin_descriptor = {
  358. .appid = PLUGIN_APP_ID,
  359. .ep_api_version = PLUGIN_API_VERSION,
  360. .entry_point = &init_plugin,
  361. };
  362. /* Plugin entry point - must return a pointer to const descriptor */
  363. const FlipperAppPluginDescriptor* init_plugin_ep() {
  364. return &init_plugin_descriptor;
  365. }