You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainClass.cpp 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. MainClass::MainClass(int argc, char *argv[])
  15. : _argc(argc)
  16. , _argv(argv)
  17. {
  18. }
  19. int MainClass::main()
  20. {
  21. CommandLineParser parser(_argc, _argv);
  22. CommandLineOption optionVersion(&parser, "version", 'v', "Show libnfc and mifare-tools versions");
  23. CommandLineOption optionHelp(&parser, "help", 'h', "Show this help");
  24. CommandLineOption optionDevice(&parser, "device", 'd', "Use the device DEVICE", "DEVICE");
  25. CommandLineOption optionUid(&parser, "uid", 'u', "Use the UID tag", "UID");
  26. CommandLineOption optionKeyFile(&parser, "key-file", 'f', "Path to a file containing keys", "FILE");
  27. if (!parser.parse()) {
  28. return parser.showHelp(EX_USAGE);
  29. }
  30. if (optionVersion.isSet()) {
  31. printVersion();
  32. return 0;
  33. }
  34. if (optionHelp.isSet()) {
  35. return parser.showHelp(0, false);
  36. }
  37. std::string deviceName = "";
  38. if (!optionDevice.isSet()) {
  39. deviceName = optionDevice.getValue();
  40. }
  41. std::string tagUid = "";
  42. if (!optionUid.isSet()) {
  43. tagUid = optionUid.getValue();
  44. }
  45. std::vector<std::string> keys;
  46. if (optionKeyFile.isSet()) {
  47. for (auto filePath : optionKeyFile.getValues()) {
  48. auto keysResult = readFile(filePath);
  49. if (!keysResult) {
  50. keysResult.print();
  51. return 1;
  52. }
  53. auto fileKeys = keysResult.getData();
  54. keys.insert(keys.end(), fileKeys.begin(), fileKeys.end());
  55. }
  56. }
  57. LibNfcBusiness libNfc;
  58. auto init = libNfc.init();
  59. if (!init) {
  60. init.print();
  61. return 1;
  62. }
  63. auto devicesResult = libNfc.getDevices();
  64. if (!devicesResult) {
  65. devicesResult.print();
  66. return 2;
  67. }
  68. auto devices = devicesResult.getData();
  69. std::shared_ptr<NfcDeviceBusiness> device = getDevice(deviceName, devices);
  70. if (device == 0) {
  71. std::cerr << "NFC device not found" << std::endl;
  72. return 3;
  73. }
  74. auto open = device->open();
  75. if (!open) {
  76. open.print();
  77. return 4;
  78. }
  79. FreeFareDeviceBusiness freeFareDevice(device);
  80. auto tagsResult = freeFareDevice.getTags();
  81. if (!tagsResult) {
  82. tagsResult.print();
  83. return 5;
  84. }
  85. auto tags = tagsResult.getData();
  86. if (tags.size() == 0) {
  87. std::cerr << "No tag found" << std::endl;
  88. return 6;
  89. }
  90. auto tag = getTag(tagUid, tags);
  91. if (tag == 0) {
  92. std::cerr << "Tag not found" << std::endl;
  93. return 6;
  94. }
  95. // std::vector<std::string> keys;
  96. // keys.push_back(StringUtils::humanToRaw("8829da9daf76").getData());
  97. // keys.push_back(StringUtils::humanToRaw("ffffffffffff").getData());
  98. keys.push_back(StringUtils::humanToRaw("484558414354").getData());
  99. int res = dump(tag, keys);
  100. // int res = mapKeys(tag, keys);
  101. device->close();
  102. libNfc.clean();
  103. return res;
  104. }
  105. int MainClass::mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
  106. {
  107. auto mappedKeysResult = tag->mapKeys(keys, printPercentMapKeys);
  108. if (isatty(1)) {
  109. std::cout << "\r";
  110. }
  111. if (!mappedKeysResult) {
  112. mappedKeysResult.print();
  113. }
  114. else {
  115. auto mappedKeys = mappedKeysResult.getData();
  116. for (int s = 0; s < mappedKeys.size(); ++s) {
  117. auto sectorKey = mappedKeys[s];
  118. std::cout << "+Sector: " << s << std::endl;
  119. for (int b = 0; b < 4; ++b) {
  120. std::cout << "+Block: " << b << std::endl;
  121. std::cout << "Key A: " << StringUtils::rawToHuman(sectorKey.first) << std::endl;
  122. std::cout << "Key B: " << StringUtils::rawToHuman(sectorKey.second) << std::endl;
  123. }
  124. }
  125. }
  126. return 0;
  127. }
  128. int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
  129. {
  130. auto dumpResult = tag->dump(keys, printPercentMapKeys, printPercentDump);
  131. if (isatty(1)) {
  132. std::cout << "\r";
  133. }
  134. if (!dumpResult) {
  135. dumpResult.print();
  136. return 7;
  137. }
  138. auto dump = dumpResult.getData();
  139. for(int s = 0; s < 16; ++s) {
  140. std::cout << "+Sector: " << s << std::endl;
  141. auto sector = dump[s];
  142. for (int b = 0; b < 3; ++b) {
  143. std::cout << (sector.hasBlock(b) ? StringUtils::rawToHuman(sector.getBlock(b)) : std::string(32, '-')) << std::endl;
  144. }
  145. std::cout << "" << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
  146. << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
  147. << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
  148. std::cout << "+Trailer key A: " << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
  149. << "\t AC bits: " << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
  150. << "\t key B: " << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
  151. AccessBitsDbo accessBitsDbo = sector.getAccessBitsDbo();
  152. for (int b = 0; b < 3; ++b) {
  153. std::cout << "+Block: " << b << " ";
  154. printBlockAccessBits(accessBitsDbo, b);
  155. }
  156. std::cout << "+Block: 4 ";
  157. printTrailerAccessBits(accessBitsDbo);
  158. }
  159. return 0;
  160. }
  161. void MainClass::printBlockAccessBits(const AccessBitsDbo &accessBits, int block)
  162. {
  163. std::cout << "read: " << (accessBits.canKeyAReadBlock(block) ? "A" : " ") << (accessBits.canKeyBReadBlock(block) ? "B" : " ");
  164. std::cout << "\t write: " << (accessBits.canKeyAWriteBlock(block) ? "A" : " ") << (accessBits.canKeyBWriteBlock(block) ? "B" : " ");
  165. std::cout << "\t increment: " << (accessBits.canKeyAIncrementBlock(block) ? "A" : " ") << (accessBits.canKeyBIncrementBlock(block) ? "B" : " ");
  166. std::cout << "\t decrement: " << (accessBits.canKeyADecrementBlock(block) ? "A" : " ") << (accessBits.canKeyBDecrementBlock(block) ? "B" : " ") << std::endl;
  167. }
  168. void MainClass::printTrailerAccessBits(const AccessBitsDbo &accessBits)
  169. {
  170. std::cout << "key A read: " << (accessBits.canKeyAReadKeyATrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyATrailer() ? "B" : " ");
  171. std::cout << "\t key A write: " << (accessBits.canKeyAWriteKeyATrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyATrailer() ? "B" : " ");
  172. std::cout << "\t AC bits read: " << (accessBits.canKeyAReadAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBReadAccessBitsTrailer() ? "B" : " ");
  173. std::cout << "\t AC bits write: " << (accessBits.canKeyAWriteAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBWriteAccessBitsTrailer() ? "B" : " ");
  174. std::cout << "\t key B read: " << (accessBits.canKeyAReadKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyBTrailer() ? "B" : " ");
  175. std::cout << "\t key B write: " << (accessBits.canKeyAWriteKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyBTrailer() ? "B" : " ") << std::endl;;
  176. }
  177. void MainClass::printPercent(int done, int total, const std::string& header)
  178. {
  179. if (isatty(1)) {
  180. std::cout << "\r\033[2K" << header << ": " << std::fixed << std::setprecision(1)
  181. << ((float) done / (float) total * 100.0) << "%" << std::flush;
  182. }
  183. // std::cout << std::fixed << std::setprecision(1) << ((float)done / (float)total * 100.0) << "%" << std::endl;
  184. }
  185. void MainClass::printPercentMapKeys(int done, int total)
  186. {
  187. printPercent(done, total, "Mapping keys");
  188. }
  189. void MainClass::printPercentDump(int done, int total)
  190. {
  191. printPercent(done, total, "Dumping");
  192. }
  193. void MainClass::printVersion() const
  194. {
  195. std::cout << "LibNfc version: " << LibNfcBusiness::getLibNfcVersion() << std::endl;
  196. std::cout << "Mifare-tools version: " << LibNfcBusiness::getMifareToolsVersion() << std::endl;
  197. }
  198. std::shared_ptr<NfcDeviceBusiness> MainClass::getDevice(const std::string &deviceName, std::vector<std::shared_ptr<NfcDeviceBusiness>> devices)
  199. {
  200. if (deviceName.empty()) {
  201. if (devices.size() > 0) {
  202. return devices[0];
  203. }
  204. }
  205. else {
  206. for (auto d : devices) {
  207. if (d->getConnStr() == deviceName) {
  208. return d;
  209. }
  210. }
  211. }
  212. return 0;
  213. }
  214. std::shared_ptr<FreeFareTagBusiness> MainClass::getTag(const std::string &tagUid,
  215. std::vector<std::shared_ptr<FreeFareTagBusiness>> tags)
  216. {
  217. if (tagUid.empty()) {
  218. if (tags.size() > 0) {
  219. return tags[0];
  220. }
  221. }
  222. else {
  223. for (auto t : tags) {
  224. if (t->getUid() == tagUid) {
  225. return t;
  226. }
  227. }
  228. }
  229. return 0;
  230. }
  231. Result<std::vector<std::string>> MainClass::readFile(const std::string &filePath)
  232. {
  233. std::vector<std::string> lines;
  234. std::ifstream fileInput(filePath);
  235. if (fileInput) {
  236. while (!fileInput.eof()) {
  237. std::string line;
  238. std::getline(fileInput, line);
  239. line = StringUtils::removeSpaces(line);
  240. if (line.compare(0, 1, "#") != 0 && line.compare(0, 1, "+") != 0) {
  241. auto keyResult = StringUtils::humanToRaw(line);
  242. if (!keyResult) {
  243. return Result<std::vector<std::string>>::error("Invalid file data");
  244. }
  245. line = keyResult.getData();
  246. lines.push_back(line);
  247. }
  248. }
  249. }
  250. else {
  251. return Result<std::vector<std::string>>::error("Failed to open file: " + std::string(strerror(errno)));
  252. }
  253. return Result<std::vector<std::string>>::ok(lines);
  254. }