您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MainClass.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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, printPercentMapKeys);
  116. if (isatty(1)) {
  117. std::cout << "\r";
  118. }
  119. if (!mappedKeysResult) {
  120. mappedKeysResult.print();
  121. }
  122. else {
  123. auto mappedKeys = mappedKeysResult.getData();
  124. for (int s = 0; s < mappedKeys.size(); ++s) {
  125. auto sectorKey = mappedKeys[s];
  126. std::cout << "+Sector: " << s << std::endl;
  127. for (int b = 0; b < 4; ++b) {
  128. std::cout << "+Block: " << b << std::endl;
  129. std::cout << "Key A: " << StringUtils::rawToHuman(sectorKey.first) << std::endl;
  130. std::cout << "Key B: " << StringUtils::rawToHuman(sectorKey.second) << std::endl;
  131. }
  132. }
  133. }
  134. return 0;
  135. }
  136. int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
  137. {
  138. auto dumpResult = tag->dump(keys, printPercentMapKeys, printPercentDump);
  139. if (isatty(1)) {
  140. std::cout << "\r";
  141. }
  142. if (!dumpResult) {
  143. dumpResult.print();
  144. return 7;
  145. }
  146. auto dump = dumpResult.getData();
  147. for(int s = 0; s < 16; ++s) {
  148. std::cout << "+Sector: " << s << std::endl;
  149. auto sector = dump[s];
  150. for (int b = 0; b < 3; ++b) {
  151. std::cout << (sector.hasBlock(b) ? StringUtils::rawToHuman(sector.getBlock(b)) : std::string(32, '-')) << std::endl;
  152. }
  153. std::cout << "" << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
  154. << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
  155. << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
  156. std::cout << "+Trailer key A: " << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
  157. << "\t AC bits: " << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
  158. << "\t key B: " << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
  159. AccessBitsDbo accessBitsDbo = sector.getAccessBitsDbo();
  160. for (int b = 0; b < 3; ++b) {
  161. std::cout << "+Block: " << b << " ";
  162. printBlockAccessBits(accessBitsDbo, b);
  163. }
  164. std::cout << "+Block: 4 ";
  165. printTrailerAccessBits(accessBitsDbo);
  166. }
  167. return 0;
  168. }
  169. void MainClass::printBlockAccessBits(const AccessBitsDbo &accessBits, int block)
  170. {
  171. std::cout << "read: " << (accessBits.canKeyAReadBlock(block) ? "A" : " ") << (accessBits.canKeyBReadBlock(block) ? "B" : " ");
  172. std::cout << "\t write: " << (accessBits.canKeyAWriteBlock(block) ? "A" : " ") << (accessBits.canKeyBWriteBlock(block) ? "B" : " ");
  173. std::cout << "\t increment: " << (accessBits.canKeyAIncrementBlock(block) ? "A" : " ") << (accessBits.canKeyBIncrementBlock(block) ? "B" : " ");
  174. std::cout << "\t decrement: " << (accessBits.canKeyADecrementBlock(block) ? "A" : " ") << (accessBits.canKeyBDecrementBlock(block) ? "B" : " ") << std::endl;
  175. }
  176. void MainClass::printTrailerAccessBits(const AccessBitsDbo &accessBits)
  177. {
  178. std::cout << "key A read: " << (accessBits.canKeyAReadKeyATrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyATrailer() ? "B" : " ");
  179. std::cout << "\t key A write: " << (accessBits.canKeyAWriteKeyATrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyATrailer() ? "B" : " ");
  180. std::cout << "\t AC bits read: " << (accessBits.canKeyAReadAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBReadAccessBitsTrailer() ? "B" : " ");
  181. std::cout << "\t AC bits write: " << (accessBits.canKeyAWriteAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBWriteAccessBitsTrailer() ? "B" : " ");
  182. std::cout << "\t key B read: " << (accessBits.canKeyAReadKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyBTrailer() ? "B" : " ");
  183. std::cout << "\t key B write: " << (accessBits.canKeyAWriteKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyBTrailer() ? "B" : " ") << std::endl;;
  184. }
  185. void MainClass::printPercent(int done, int total, const std::string& header)
  186. {
  187. if (isatty(1)) {
  188. std::cout << "\r\033[2K" << header << ": " << std::fixed << std::setprecision(1)
  189. << ((float) done / (float) total * 100.0) << "%" << std::flush;
  190. }
  191. // std::cout << std::fixed << std::setprecision(1) << ((float)done / (float)total * 100.0) << "%" << std::endl;
  192. }
  193. void MainClass::printPercentMapKeys(int done, int total)
  194. {
  195. printPercent(done, total, "Mapping keys");
  196. }
  197. void MainClass::printPercentDump(int done, int total)
  198. {
  199. printPercent(done, total, "Dumping");
  200. }
  201. void MainClass::printVersion() const
  202. {
  203. std::cout << "LibNfc version: " << LibNfcBusiness::getVersion() << std::endl;
  204. std::cout << "Mifare-tools version: " << QUOTE(GIT_REF_NAME) << "-" << QUOTE(GIT_SHA1) << std::endl;
  205. }
  206. std::shared_ptr<NfcDeviceBusiness> MainClass::getDevice(const std::string &deviceName, std::vector<std::shared_ptr<NfcDeviceBusiness>> devices)
  207. {
  208. if (deviceName.empty()) {
  209. if (devices.size() > 0) {
  210. return devices[0];
  211. }
  212. }
  213. else {
  214. for (auto d : devices) {
  215. if (d->getConnStr() == deviceName) {
  216. return d;
  217. }
  218. }
  219. }
  220. return 0;
  221. }
  222. std::shared_ptr<FreeFareTagBusiness> MainClass::getTag(const std::string &tagUid,
  223. std::vector<std::shared_ptr<FreeFareTagBusiness>> tags)
  224. {
  225. if (tagUid.empty()) {
  226. if (tags.size() > 0) {
  227. return tags[0];
  228. }
  229. }
  230. else {
  231. for (auto t : tags) {
  232. if (t->getUid() == tagUid) {
  233. return t;
  234. }
  235. }
  236. }
  237. return 0;
  238. }
  239. Result<std::vector<std::string>> MainClass::readFile(const std::string &filePath)
  240. {
  241. std::vector<std::string> lines;
  242. std::ifstream fileInput(filePath);
  243. if (fileInput) {
  244. while (!fileInput.eof()) {
  245. std::string line;
  246. std::getline(fileInput, line);
  247. line = StringUtils::removeSpaces(line);
  248. if (line.compare(0, 1, "#") != 0 && line.compare(0, 1, "+") != 0) {
  249. auto keyResult = StringUtils::humanToRaw(line);
  250. if (!keyResult) {
  251. return Result<std::vector<std::string>>::error("Invalid file data");
  252. }
  253. line = keyResult.getData();
  254. lines.push_back(line);
  255. }
  256. }
  257. }
  258. else {
  259. return Result<std::vector<std::string>>::error("Failed to open file: " + std::string(strerror(errno)));
  260. }
  261. return Result<std::vector<std::string>>::ok(lines);
  262. }