term_cmd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /**
  2. * Copyright (C) 2011 Anders Sundman <anders@4zm.org>
  3. *
  4. * This file is part of mfterm.
  5. *
  6. * mfterm is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. * mfterm is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License
  15. * along with mfterm. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Parts of code used in this file are from the GNU readline library file
  18. * fileman.c (GPLv3). Copyright (C) 1987-2009 Free Software Foundation, Inc
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <strings.h>
  23. #include <stdlib.h>
  24. #include "mfterm.h"
  25. #include "tag.h"
  26. #include "term_cmd.h"
  27. #include "mifare_ctrl.h"
  28. #include "dictionary.h"
  29. #include "spec_syntax.h"
  30. #include "util.h"
  31. #include "mac.h"
  32. command_t commands[] = {
  33. { "help", com_help, 0, 0, "Display this text" },
  34. { "?", com_help, 0, 0, "Synonym for 'help'" },
  35. { "quit", com_quit, 0, 1, "Exit the program" },
  36. { "exit", com_quit, 0, 0, "Synonym for 'quit'" },
  37. { "load", com_load_tag, 1, 1, "Load tag data from a file" },
  38. { "save", com_save_tag, 1, 1, "Save tag data to a file" },
  39. { "clear", com_clear_tag, 0, 1, "Clear the current tag data" },
  40. { "read", com_read_tag, 0, 1, "A|B : Read tag data from a physical tag" },
  41. { "write", com_write_tag, 0, 1, "A|B : Write tag data to a physical tag" },
  42. { "print", com_print, 0, 1, "1k|4k : Print tag data" },
  43. { "print keys", com_print_keys, 0, 1, "1k|4k : Print tag's keys" },
  44. { "set", com_set, 0, 1, "#block #offset = xx xx xx : Set tag data" },
  45. { "keys load", com_keys_load, 1, 1, "Load keys from a file" },
  46. { "keys save", com_keys_save, 1, 1, "Save keys to a file" },
  47. { "keys clear", com_keys_clear, 0, 1, "Clear the keys" },
  48. { "keys set", com_keys_set, 0, 1, "A|B #S key : Set a key value" },
  49. { "keys import", com_keys_import, 0, 1, "Import keys from the current tag" },
  50. { "keys test", com_keys_test, 0, 1, "Try to authenticate with the keys" },
  51. { "keys", com_keys_print, 0, 1, "1k|4k : Print the keys" },
  52. { "dict load", com_dict_load, 1, 1, "Load a dictionary key file" },
  53. { "dict clear", com_dict_clear, 0, 1, "Clear the key dictionary" },
  54. { "dict attack", com_dict_attack, 0, 1, "Find keys of a physical tag"},
  55. { "dict", com_dict_print, 0, 1, "Print the key dictionary" },
  56. { "spec load", com_spec_load, 1, 1, "Load a specification file" },
  57. { "spec clear", com_spec_clear, 0, 1, "Unload the specification" },
  58. { "spec", com_spec_print, 0, 1, "Print the specification" },
  59. { "mac key", com_mac_key_get_set, 0, 1, "<k0..k7> : Get or set MAC key" },
  60. { "mac compute", com_mac_block_compute, 0, 1, "#block : Compute block MAC" },
  61. { "mac update", com_mac_block_update, 0, 1, "#block : Compute block MAC" },
  62. { (char *)NULL, (cmd_func_t)NULL, 0, 0, (char *)NULL }
  63. };
  64. // Parse a Mifare size type argument (1k|4k)
  65. mf_size_t parse_size(const char* str);
  66. // Parse a Mifare size type argument (1k|4k). Return the default
  67. // argument value if the string is NULL.
  68. mf_size_t parse_size_default(const char* str, mf_size_t default_size);
  69. // Parse a Mifare key type argument (A|B)
  70. mf_key_type_t parse_key_type(const char* str);
  71. // Parse a Mifare key type argument (A|B). Return the default
  72. // argument value if the string is NULL.
  73. mf_key_type_t parse_key_type_default(const char* str,
  74. mf_key_type_t default_type);
  75. // Compute the MAC using the current_mac_key. If update is nonzero,
  76. // the mac of the current tag is updated. If not, the MAC is simply
  77. // printed.
  78. int com_mac_block_compute_impl(char* arg, int update);
  79. /* Look up NAME as the name of a command, and return a pointer to that
  80. command. Return a NULL pointer if NAME isn't a command name. */
  81. command_t* find_command(const char *name) {
  82. command_t* cmd = NULL;
  83. size_t cmd_len = 0;
  84. for (int i = 0; commands[i].name; i++) {
  85. size_t l = strlen(commands[i].name);
  86. if (l > cmd_len && strncmp(name, commands[i].name, l) == 0) {
  87. cmd = &commands[i];
  88. cmd_len = l;
  89. }
  90. }
  91. return cmd;
  92. }
  93. /**
  94. * Helper function to print the specified command alligned with the longest
  95. * command name.
  96. */
  97. void print_help_(size_t cmd) {
  98. // Find longest command (and cache the result)
  99. static int cmd_len_max = 0;
  100. if (cmd_len_max == 0) {
  101. for (int i = 0; commands[i].name; i++) {
  102. size_t cmd_len = strlen(commands[i].name);
  103. cmd_len_max = cmd_len > cmd_len_max ? cmd_len : cmd_len_max;
  104. }
  105. }
  106. // Format: 4x' ' | cmd | ' '-pad-to-longest-cmd | 4x' ' | doc
  107. printf (" %s", commands[cmd].name);
  108. for (int j = cmd_len_max - strlen(commands[cmd].name); j >= 0; --j)
  109. printf(" ");
  110. printf (" %s.\n", commands[cmd].doc);
  111. }
  112. int com_help(char* arg) {
  113. // Help request for specific command?
  114. if (arg) {
  115. for (int i = 0; commands[i].name; ++i) {
  116. if (strcmp(arg, commands[i].name) == 0) {
  117. print_help_(i);
  118. return 0;
  119. }
  120. }
  121. printf ("No commands match '%s'\n", arg);
  122. }
  123. // Help for all commands (with doc flag)
  124. for (int i = 0; commands[i].name; i++) {
  125. if (commands[i].document)
  126. print_help_(i);
  127. }
  128. return 0;
  129. }
  130. int com_quit(char *arg) {
  131. stop_input_loop();
  132. return 0;
  133. }
  134. int com_load_tag(char *arg) {
  135. int res = load_tag(arg);
  136. if (res == 0)
  137. printf("Successfully loaded tag from: %s\n", arg);
  138. return 0;
  139. }
  140. int com_save_tag(char* arg) {
  141. int res = save_tag(arg);
  142. if (res == 0)
  143. printf("Successfully wrote tag to: %s\n", arg);
  144. return 0;
  145. }
  146. int com_clear_tag(char* arg) {
  147. clear_tag(&current_tag);
  148. return 0;
  149. }
  150. int com_read_tag(char* arg) {
  151. // Add option to choose key
  152. char* ab = strtok(arg, " ");
  153. if (ab && strtok(NULL, " ") != (char*)NULL) {
  154. printf("Too many arguments\n");
  155. return -1;
  156. }
  157. if (!ab)
  158. printf("No key argument (A|B) given. Defaulting to A\n");
  159. // Parse key selection
  160. mf_key_type_t key_type = parse_key_type_default(ab, MF_KEY_A);
  161. if (key_type == MF_INVALID_KEY_TYPE) {
  162. printf("Invalid argument (A|B): %s\n", ab);
  163. return -1;
  164. }
  165. // Issue the read request
  166. mf_read_tag(&current_tag, key_type);
  167. return 0;
  168. }
  169. int com_write_tag(char* arg) {
  170. // Add option to choose key
  171. char* ab = strtok(arg, " ");
  172. if (!ab) {
  173. printf("Too few arguments: (A|B)\n");
  174. return -1;
  175. }
  176. if (strtok(NULL, " ") != (char*)NULL) {
  177. printf("Too many arguments\n");
  178. return -1;
  179. }
  180. // Parse key selection
  181. mf_key_type_t key_type = parse_key_type(ab);
  182. if (key_type == MF_INVALID_KEY_TYPE) {
  183. printf("Invalid argument (A|B): %s\n", ab);
  184. return -1;
  185. }
  186. // Issue the read request
  187. mf_write_tag(&current_tag, key_type);
  188. return 0;
  189. }
  190. int com_print(char* arg) {
  191. char* a = strtok(arg, " ");
  192. if (a && strtok(NULL, " ") != (char*)NULL) {
  193. printf("Too many arguments\n");
  194. return -1;
  195. }
  196. mf_size_t size = parse_size_default(a, MF_1K);
  197. if (size == MF_INVALID_SIZE) {
  198. printf("Unknown argument: %s\n", a);
  199. return -1;
  200. }
  201. print_tag(size);
  202. return 0;
  203. }
  204. int com_set(char* arg) {
  205. char* block_str = strtok(arg, " ");
  206. char* offset_str = strtok(NULL, " ");
  207. char* byte_str = strtok(NULL, " ");
  208. if (!block_str || !offset_str || !byte_str) {
  209. printf("Too few arguments: #block #offset xx xx xx .. xx\n");
  210. return -1;
  211. }
  212. int block = strtol(block_str, &block_str, 16);
  213. if (*block_str != '\0') {
  214. printf("Invalid block character (non hex): %s\n", block_str);
  215. return -1;
  216. }
  217. if (block < 0 || block > 0xff) {
  218. printf("Invalid block [0,ff]: %x\n", block);
  219. return -1;
  220. }
  221. int offset = strtol(offset_str, &offset_str, 16);
  222. if (*offset_str != '\0') {
  223. printf("Invalid offset character (non hex): %s\n", offset_str);
  224. return -1;
  225. }
  226. if (offset < 0 || offset > 0x0f) {
  227. printf("Invalid offset [0,f]: %x\n", offset);
  228. return -1;
  229. }
  230. // Consume the byte tokens
  231. do {
  232. int byte = strtol(byte_str, &byte_str, 16);
  233. if (*byte_str != '\0') {
  234. printf("Invalid byte character (non hex): %s\n", byte_str);
  235. return -1;
  236. }
  237. if (byte < 0 || byte > 0xff) {
  238. printf("Invalid byte value [0,ff]: %x\n", byte);
  239. return -1;
  240. }
  241. if (offset > 0x0f) {
  242. printf("Too many bytes specified.\n");
  243. return -1;
  244. }
  245. // Write the data
  246. current_tag.amb[block].mbd.abtData[offset++] = byte;
  247. } while((byte_str = strtok(NULL, " ")) != (char*)NULL);
  248. return 0;
  249. }
  250. int com_print_keys(char* arg) {
  251. char* a = strtok(arg, " ");
  252. if (a && strtok(NULL, " ") != (char*)NULL) {
  253. printf("Too many arguments\n");
  254. return -1;
  255. }
  256. mf_size_t size = parse_size_default(a, MF_1K);
  257. if (size == MF_INVALID_SIZE) {
  258. printf("Unknown argument: %s\n", a);
  259. return -1;
  260. }
  261. print_keys(&current_tag, size);
  262. return 0;
  263. }
  264. int com_keys_load(char* arg) {
  265. int res = load_auth(arg);
  266. if (res == 0)
  267. printf("Successfully loaded keys from: %s\n", arg);
  268. return 0;
  269. }
  270. int com_keys_save(char* arg) {
  271. int res = save_auth(arg);
  272. if (res == 0)
  273. printf("Successfully wrote keys to: %s\n", arg);
  274. return 0;
  275. }
  276. int com_keys_clear(char* arg) {
  277. clear_tag(&current_auth);
  278. return 0;
  279. }
  280. int com_keys_set(char* arg) {
  281. // Arg format: A|B #S key
  282. char* ab = strtok(arg, " ");
  283. char* sector_str = strtok(NULL, " ");
  284. char* key_str = strtok(NULL, " ");
  285. if (strtok(NULL, " ") != (char*)NULL) {
  286. printf("Too many arguments\n");
  287. return -1;
  288. }
  289. if (!ab || !sector_str || !key_str) {
  290. printf("Too few arguments: (A|B) #sector key\n");
  291. return -1;
  292. }
  293. // Read sector
  294. int sector = strtol(sector_str, &sector_str, 16);
  295. // Sanity check sector range
  296. if (*sector_str != '\0') {
  297. printf("Invalid sector character (non hex): %s\n", sector_str);
  298. return -1;
  299. }
  300. if (sector < 0 || sector > 0x1b) {
  301. printf("Invalid sector [0,1b]: %x\n", sector);
  302. return -1;
  303. }
  304. // Sanity check key length
  305. if (strncmp(key_str, "0x", 2) == 0)
  306. key_str += 2;
  307. if (strlen(key_str) != 12) {
  308. printf("Invalid key (6 byte hex): %s\n", key_str);
  309. return -1;
  310. }
  311. // Compute the block that houses the key for the desired sector
  312. size_t block = sector_to_trailer(sector);
  313. // Parse key selection and point to appropriate key
  314. uint8_t* key;
  315. mf_key_type_t key_type = parse_key_type(ab);
  316. if (key_type == MF_KEY_A)
  317. key = current_auth.amb[block].mbt.abtKeyA;
  318. else if (key_type == MF_KEY_B)
  319. key = current_auth.amb[block].mbt.abtKeyB;
  320. else {
  321. printf("Invalid argument (A|B): %s\n", ab);
  322. return -1;
  323. }
  324. // Parse the key
  325. if (read_key(key, key_str) == NULL) {
  326. printf("Invalid key character (non hex)\n");
  327. return -1;
  328. }
  329. return 0;
  330. }
  331. int com_keys_import(char* arg) {
  332. import_auth();
  333. return 0;
  334. }
  335. int com_keys_test(char* arg) {
  336. // Arg format: 1k|4k A|B
  337. char* s = strtok(arg, " ");
  338. char* ab = strtok(NULL, " ");
  339. if (s && ab && strtok(NULL, " ") != NULL) {
  340. printf("Too many arguments\n");
  341. return -1;
  342. }
  343. if (!s || !ab) {
  344. printf("Too few arguments: (1k|4k) (A|B)\n");
  345. return -1;
  346. }
  347. // Parse arguments
  348. mf_size_t size = parse_size(s);
  349. if (size == MF_INVALID_SIZE) {
  350. printf("Unknown size argument (1k|4k): %s\n", s);
  351. return -1;
  352. }
  353. mf_key_type_t key_type = parse_key_type(ab);
  354. if (key_type == MF_INVALID_KEY_TYPE) {
  355. printf("Unknown key type argument (A|B): %s\n", ab);
  356. return -1;
  357. }
  358. // Run the auth test
  359. mf_test_auth(&current_auth, size, key_type);
  360. return 0;
  361. }
  362. int com_keys_print(char* arg) {
  363. char* a = strtok(arg, " ");
  364. if (a && strtok(NULL, " ") != (char*)NULL) {
  365. printf("Too many arguments\n");
  366. return -1;
  367. }
  368. mf_size_t size = parse_size_default(a, MF_1K);
  369. if (size == MF_INVALID_SIZE) {
  370. printf("Unknown argument: %s\n", a);
  371. return -1;
  372. }
  373. print_keys(&current_auth, size);
  374. return 0;
  375. }
  376. int com_dict_load(char* arg) {
  377. FILE* dict_file = fopen(arg, "r");
  378. if (dict_file == NULL) {
  379. printf("Could not open file: %s\n", arg);
  380. return 1;
  381. }
  382. dictionary_import(dict_file);
  383. fclose(dict_file);
  384. return 0;
  385. }
  386. int com_dict_clear(char* arg) {
  387. dictionary_clear();
  388. return 0;
  389. }
  390. int com_dict_attack(char* arg) {
  391. // Not much point if we don't have any keys
  392. if (!dictionary_get()) {
  393. printf("Dictionary is empty!");
  394. return -1;
  395. }
  396. mf_dictionary_attack(&current_auth);
  397. return 0;
  398. }
  399. int com_dict_print(char* arg) {
  400. key_list_t* kl = dictionary_get();
  401. int count = 0;
  402. while(kl) {
  403. printf("%s\n", sprint_key(kl->key));
  404. kl = kl->next;
  405. ++count;
  406. }
  407. printf("Dictionary contains: %d keys\n", count);
  408. return 0;
  409. }
  410. int com_spec_print(char* arg) {
  411. print_instance_tree();
  412. return 0;
  413. }
  414. int com_spec_load(char* arg) {
  415. // Start by clearing the current hierarcy
  416. clear_instance_tree();
  417. tt_clear();
  418. // Open the file
  419. FILE* spec_file = fopen(arg, "r");
  420. if (spec_file == NULL) {
  421. printf("Could not open file: %s\n", arg);
  422. return 1;
  423. }
  424. // Parse the specification
  425. spec_import(spec_file);
  426. fclose(spec_file);
  427. return 0;
  428. }
  429. int com_spec_clear(char* arg) {
  430. clear_instance_tree();
  431. tt_clear();
  432. return 0;
  433. }
  434. int com_mac_key_get_set(char* arg) {
  435. char* key_str = strtok(arg, " ");
  436. if (key_str == 0) {
  437. printf("Current MAC key: \n");
  438. print_hex_array_sep(current_mac_key, 8, " ");
  439. printf("\n");
  440. return 0;
  441. }
  442. unsigned char key[8];
  443. int key_ptr = 0;
  444. // Consume the key tokens
  445. do {
  446. int byte = strtol(key_str, &key_str, 16);
  447. if (*key_str != '\0') {
  448. printf("Invalid key character (non hex): %s\n", key_str);
  449. return -1;
  450. }
  451. if (byte < 0 || byte > 0xff) {
  452. printf("Invalid byte value [0,ff]: %x\n", byte);
  453. return -1;
  454. }
  455. if (key_ptr > sizeof(key)) {
  456. printf("Too many bytes specified in key (should be 8).\n");
  457. return -1;
  458. }
  459. // Accept the byte and add it to the key
  460. key[key_ptr++] = byte;
  461. } while((key_str = strtok(NULL, " ")) != (char*)NULL);
  462. if (key_ptr != sizeof(key)) {
  463. printf("Too few bytes specified in key (should be 8).\n");
  464. return -1;
  465. }
  466. // Everything ok, so update the global
  467. memcpy(current_mac_key, key, 8);
  468. return 0;
  469. }
  470. int com_mac_block_compute(char* arg) {
  471. return com_mac_block_compute_impl(arg, 0);
  472. }
  473. int com_mac_block_update(char* arg) {
  474. return com_mac_block_compute_impl(arg, 1);
  475. }
  476. int com_mac_block_compute_impl(char* arg, int update) {
  477. char* block_str = strtok(arg, " ");
  478. if (!block_str) {
  479. printf("Too few arguments: #block\n");
  480. return -1;
  481. }
  482. int block = strtol(block_str, &block_str, 16);
  483. if (*block_str != '\0') {
  484. printf("Invalid block character (non hex): %s\n", block_str);
  485. return -1;
  486. }
  487. if (block < 0 || block > 0xff) {
  488. printf("Invalid block [0,ff]: %x\n", block);
  489. return -1;
  490. }
  491. // Use the key
  492. unsigned char* mac = compute_block_mac(block, current_mac_key, update);
  493. // MAC is null on error, else 8 bytes
  494. if (mac == 0)
  495. return -1;
  496. // Only need 16 MSBs.
  497. printf("Block %2.2x, MAC : ", block);
  498. print_hex_array_sep(mac, 2, " ");
  499. printf("\n");
  500. return 0;
  501. }
  502. mf_size_t parse_size(const char* str) {
  503. if (str == NULL)
  504. return MF_INVALID_SIZE;
  505. if (strcasecmp(str, "1k") == 0)
  506. return MF_1K;
  507. if (strcasecmp(str, "4k") == 0)
  508. return MF_4K;
  509. return MF_INVALID_SIZE;
  510. }
  511. mf_size_t parse_size_default(const char* str, mf_size_t default_size) {
  512. if (str == NULL)
  513. return default_size;
  514. return parse_size(str);
  515. }
  516. mf_key_type_t parse_key_type(const char* str) {
  517. if (str == NULL)
  518. return MF_INVALID_KEY_TYPE;
  519. if (strcasecmp(str, "a") == 0)
  520. return MF_KEY_A;
  521. if (strcasecmp(str, "b") == 0)
  522. return MF_KEY_B;
  523. return MF_INVALID_KEY_TYPE;
  524. }
  525. mf_key_type_t parse_key_type_default(const char* str,
  526. mf_key_type_t default_type) {
  527. if (str == NULL)
  528. return default_type;
  529. return parse_key_type(str);
  530. }
  531. // Any command starting with '.' - path spec
  532. int exec_path_command(const char *line) {
  533. instance_t* inst = parse_spec_path(line);
  534. if (inst)
  535. print_tag_data_range(inst->offset_bytes, inst->offset_bits,
  536. inst->size_bytes, inst->size_bits);
  537. else
  538. printf("Invalid Path\n");
  539. return 0;
  540. }