Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MainClass.cpp 9.5KB

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