init_plugin.c 12 KB

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