Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MainClass.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //
  2. // Created by robin on 8/8/15.
  3. //
  4. #include <iostream>
  5. #include <sysexits.h>
  6. #include <iomanip>
  7. #include <fstream>
  8. #include <DBO/StringUtils.h>
  9. #include <Business/FreeFareDeviceBusiness.h>
  10. #include "DBO/Result.h"
  11. #include "Business/LibNfcBusiness.h"
  12. #include "CommandLineParser.h"
  13. #include "MainClass.h"
  14. #define EX_REDIRECT_ERROR 1
  15. #define EX_KEY_ERROR 10
  16. #define EX_LIB_NFC_ERROR 12
  17. #define EX_NFC_DEVICE_NOT_FOUND 13
  18. #define EX_NFC_TAG_NOT_FOUND 14
  19. #define EX_MAP_KEYS_ERROR 15
  20. #define EX_DUMP_ERROR 16
  21. MainClass::MainClass(int argc, char *argv[])
  22. : _argc(argc)
  23. , _argv(argv)
  24. {
  25. }
  26. int MainClass::main()
  27. {
  28. CommandLineParser parser(_argc, _argv);
  29. CommandLineOption optionVersion(&parser, "version", 'v', "Show libnfc and mifare-tools versions");
  30. CommandLineOption optionHelp(&parser, "help", 'h', "Show this help");
  31. CommandLineOption optionMap(&parser, "map", 'm', "Map keys for the tag");
  32. CommandLineOption optionDump(&parser, "dump", 'd', "Dump the tag");
  33. CommandLineOption optionDevices(&parser, "devices", 'l', "List NFC devices");
  34. CommandLineOption optionTags(&parser, "tags", 't', "List NFC tags");
  35. CommandLineOption optionDevice(&parser, "device", 'e', "Use the device DEVICE", "DEVICE");
  36. CommandLineOption optionTag(&parser, "tag", 'u', "Use the UID tag", "UID");
  37. CommandLineOption optionKeyFile(&parser, "key-file", 'f', "Path to a file containing keys", "FILE");
  38. CommandLineOption optionKey(&parser, "key", 'k', "Key to use to authenticate", "KEY");
  39. CommandLineOption optionOutput(&parser, "output", 'o', "Redirect output to FILE. '-' to use stdout", "FILE");
  40. if (!parser.parse()) {
  41. return parser.showHelp(EX_USAGE);
  42. }
  43. std::string outputFile = "-";
  44. if (optionOutput.isSet()) {
  45. outputFile = optionOutput.getValue();
  46. }
  47. std::streambuf* oldCoutStreamBuf = std::cout.rdbuf();
  48. std::ofstream fileCout;
  49. if (outputFile != "-" && !outputFile.empty()) {
  50. fileCout.open(outputFile);
  51. if (!fileCout) {
  52. std::cerr << "Failed to redirect output: " << strerror(errno) << std::endl;
  53. return EX_REDIRECT_ERROR;
  54. }
  55. std::cout.rdbuf(fileCout.rdbuf());
  56. }
  57. if (optionVersion.isSet()) {
  58. printVersion();
  59. return EX_OK;
  60. }
  61. if (optionHelp.isSet()) {
  62. return parser.showHelp(EX_OK, false);
  63. }
  64. std::string deviceName = "";
  65. if (optionDevice.isSet()) {
  66. deviceName = optionDevice.getValue();
  67. }
  68. std::string tagUid = "";
  69. if (optionTag.isSet()) {
  70. tagUid = optionTag.getValue();
  71. }
  72. std::vector<std::string> keys;
  73. if (optionKeyFile.isSet()) {
  74. for (auto filePath : optionKeyFile.getValues()) {
  75. auto keysResult = readFile(filePath);
  76. if (!keysResult) {
  77. keysResult.print();
  78. return EX_KEY_ERROR;
  79. }
  80. auto fileKeys = keysResult.getData();
  81. keys.insert(keys.end(), fileKeys.begin(), fileKeys.end());
  82. }
  83. }
  84. if (optionKey.isSet()) {
  85. for (auto key : optionKey.getValues()) {
  86. auto keyResult = StringUtils::humanToRaw(key);
  87. key = keyResult.getData();
  88. if (!keyResult || key.length() != 6) {
  89. std::cerr << "Invalid key" << std::endl;
  90. return EX_KEY_ERROR;
  91. }
  92. keys.push_back(key);
  93. }
  94. }
  95. int res = EX_OK;
  96. LibNfcBusiness libNfc;
  97. auto init = libNfc.init();
  98. if (!init) {
  99. init.print();
  100. res = EX_LIB_NFC_ERROR;
  101. }
  102. else
  103. {
  104. auto devicesResult = libNfc.getDevices();
  105. if (!devicesResult)
  106. {
  107. devicesResult.print();
  108. res = EX_LIB_NFC_ERROR;
  109. }
  110. else
  111. {
  112. auto devices = devicesResult.getData();
  113. if (optionDevices.isSet())
  114. {
  115. for (auto device : devices) {
  116. std::cout << device->getConnStr() << std::endl;
  117. }
  118. }
  119. else
  120. {
  121. auto device = getDevice(deviceName, devices);
  122. if (device == 0)
  123. {
  124. std::cerr << "NFC device not found" << std::endl;
  125. res = EX_NFC_DEVICE_NOT_FOUND;
  126. }
  127. else {
  128. auto open = device->open();
  129. if (!open) {
  130. open.print();
  131. res = EX_LIB_NFC_ERROR;
  132. }
  133. else {
  134. FreeFareDeviceBusiness freeFareDevice(device);
  135. auto tagsResult = freeFareDevice.getTags();
  136. if (!tagsResult)
  137. {
  138. tagsResult.print();
  139. res = EX_LIB_NFC_ERROR;
  140. }
  141. auto tags = tagsResult.getData();
  142. if (optionTags.isSet()) {
  143. for (auto tag : tags)
  144. {
  145. std::cout << "UID=" << tag->getUid() << " \tType=" << tag->getType() << std::endl;
  146. }
  147. }
  148. else {
  149. auto tag = getTag(tagUid, tags);
  150. if (tag == 0) {
  151. std::cerr << "Tag not found" << std::endl;
  152. res = EX_NFC_TAG_NOT_FOUND;
  153. }
  154. else {
  155. if (optionDump.isSet()) {
  156. res = dump(tag, keys);
  157. }
  158. else if (optionMap.isSet()) {
  159. res = mapKeys(tag, keys);
  160. }
  161. else {
  162. std::cerr << "Must select an action (map|dump|devices|tags)" << std::endl;
  163. res = EX_USAGE;
  164. }
  165. }
  166. }
  167. }
  168. device->close();
  169. }
  170. }
  171. }
  172. }
  173. libNfc.clean();
  174. std::cout.rdbuf(oldCoutStreamBuf);
  175. if (fileCout) {
  176. fileCout.close();
  177. }
  178. return res;
  179. }
  180. int MainClass::mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
  181. {
  182. auto mappedKeysResult = tag->mapKeys(keys, printPercentMapKeys);
  183. if (isatty(1)) {
  184. std::cout << "\r";
  185. }
  186. if (!mappedKeysResult) {
  187. mappedKeysResult.print();
  188. return EX_MAP_KEYS_ERROR;
  189. }
  190. else {
  191. auto mappedKeys = mappedKeysResult.getData();
  192. for (int s = 0; s < mappedKeys.size(); ++s) {
  193. auto sectorKey = mappedKeys[s];
  194. std::cout << "+Sector: " << s << std::endl;
  195. for (int b = 0; b < 4; ++b) {
  196. std::cout << "+Block: " << b << std::endl;
  197. std::cout << "Key A: " << (!sectorKey.first.empty() ? StringUtils::rawToHuman(sectorKey.first) : std::string(12, '-')) << std::endl;
  198. std::cout << "Key B: " << (!sectorKey.second.empty() ? StringUtils::rawToHuman(sectorKey.second) : std::string(12, '-')) << std::endl;
  199. }
  200. }
  201. }
  202. return EX_OK;
  203. }
  204. int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
  205. {
  206. auto dumpResult = tag->dump(keys, printPercentMapKeys, printPercentDump);
  207. if (isatty(1)) {
  208. std::cout << "\r";
  209. }
  210. if (!dumpResult) {
  211. dumpResult.print();
  212. return EX_DUMP_ERROR;
  213. }
  214. auto dump = dumpResult.getData();
  215. for(int s = 0; s < 16; ++s) {
  216. std::cout << "+Sector: " << s << std::endl;
  217. auto sector = dump[s];
  218. for (int b = 0; b < 3; ++b) {
  219. std::cout << (sector.hasBlock(b) ? StringUtils::rawToHuman(sector.getBlock(b)) : std::string(32, '-')) << std::endl;
  220. }
  221. std::cout << "" << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
  222. << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
  223. << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
  224. std::cout << "+Trailer key A: " << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
  225. << "\t AC bits: " << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
  226. << "\t key B: " << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
  227. AccessBitsDbo accessBitsDbo = sector.getAccessBitsDbo();
  228. for (int b = 0; b < 3; ++b) {
  229. std::cout << "+Block: " << b << " ";
  230. printBlockAccessBits(accessBitsDbo, b);
  231. }
  232. std::cout << "+Block: 4 ";
  233. printTrailerAccessBits(accessBitsDbo);
  234. }
  235. return EX_OK;
  236. }
  237. void MainClass::printBlockAccessBits(const AccessBitsDbo &accessBits, int block)
  238. {
  239. std::cout << "read: " << (accessBits.canKeyAReadBlock(block) ? "A" : " ") << (accessBits.canKeyBReadBlock(block) ? "B" : " ");
  240. std::cout << "\t write: " << (accessBits.canKeyAWriteBlock(block) ? "A" : " ") << (accessBits.canKeyBWriteBlock(block) ? "B" : " ");
  241. std::cout << "\t increment: " << (accessBits.canKeyAIncrementBlock(block) ? "A" : " ") << (accessBits.canKeyBIncrementBlock(block) ? "B" : " ");
  242. std::cout << "\t decrement: " << (accessBits.canKeyADecrementBlock(block) ? "A" : " ") << (accessBits.canKeyBDecrementBlock(block) ? "B" : " ") << std::endl;
  243. }
  244. void MainClass::printTrailerAccessBits(const AccessBitsDbo &accessBits)
  245. {
  246. std::cout << "key A read: " << (accessBits.canKeyAReadKeyATrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyATrailer() ? "B" : " ");
  247. std::cout << "\t key A write: " << (accessBits.canKeyAWriteKeyATrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyATrailer() ? "B" : " ");
  248. std::cout << "\t AC bits read: " << (accessBits.canKeyAReadAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBReadAccessBitsTrailer() ? "B" : " ");
  249. std::cout << "\t AC bits write: " << (accessBits.canKeyAWriteAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBWriteAccessBitsTrailer() ? "B" : " ");
  250. std::cout << "\t key B read: " << (accessBits.canKeyAReadKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyBTrailer() ? "B" : " ");
  251. std::cout << "\t key B write: " << (accessBits.canKeyAWriteKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyBTrailer() ? "B" : " ") << std::endl;;
  252. }
  253. void MainClass::printPercent(int done, int total, const std::string& header)
  254. {
  255. if (isatty(1)) {
  256. std::cout << "\r\033[2K" << header << ": " << std::fixed << std::setprecision(1)
  257. << ((float) done / (float) total * 100.0) << "%" << std::flush;
  258. }
  259. // std::cout << std::fixed << std::setprecision(1) << ((float)done / (float)total * 100.0) << "%" << std::endl;
  260. }
  261. void MainClass::printPercentMapKeys(int done, int total)
  262. {
  263. printPercent(done, total, "Mapping keys");
  264. }
  265. void MainClass::printPercentDump(int done, int total)
  266. {
  267. printPercent(done, total, "Dumping");
  268. }
  269. void MainClass::printVersion() const
  270. {
  271. std::cout << "LibNfc version: " << LibNfcBusiness::getLibNfcVersion() << std::endl;
  272. std::cout << "Mifare-tools version: " << LibNfcBusiness::getMifareToolsVersion() << std::endl;
  273. }
  274. std::shared_ptr<NfcDeviceBusiness> MainClass::getDevice(const std::string &deviceName, std::vector<std::shared_ptr<NfcDeviceBusiness>> devices)
  275. {
  276. if (deviceName.empty()) {
  277. if (devices.size() > 0) {
  278. return devices[0];
  279. }
  280. }
  281. else {
  282. for (auto d : devices) {
  283. if (d->getConnStr() == deviceName) {
  284. return d;
  285. }
  286. }
  287. }
  288. return 0;
  289. }
  290. std::shared_ptr<FreeFareTagBusiness> MainClass::getTag(const std::string &tagUid,
  291. std::vector<std::shared_ptr<FreeFareTagBusiness>> tags)
  292. {
  293. if (tagUid.empty()) {
  294. if (tags.size() > 0) {
  295. return tags[0];
  296. }
  297. }
  298. else {
  299. for (auto t : tags) {
  300. if (t->getUid() == tagUid) {
  301. return t;
  302. }
  303. }
  304. }
  305. return 0;
  306. }
  307. Result<std::vector<std::string>> MainClass::readFile(const std::string &filePath)
  308. {
  309. std::vector<std::string> lines;
  310. std::ifstream fileInput(filePath);
  311. if (fileInput) {
  312. while (!fileInput.eof()) {
  313. std::string line;
  314. std::getline(fileInput, line);
  315. line = StringUtils::removeSpaces(line);
  316. if (line.compare(0, 1, "#") != 0 && line.compare(0, 1, "+") != 0) {
  317. auto keyResult = StringUtils::humanToRaw(line);
  318. if (!keyResult) {
  319. return Result<std::vector<std::string>>::error("Invalid file data");
  320. }
  321. line = keyResult.getData();
  322. lines.push_back(line);
  323. }
  324. }
  325. }
  326. else {
  327. return Result<std::vector<std::string>>::error("Failed to open file: " + std::string(strerror(errno)));
  328. }
  329. return Result<std::vector<std::string>>::ok(lines);
  330. }