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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // Created by robin on 8/8/15.
  3. //
  4. #include <iostream>
  5. #include <sysexits.h>
  6. #include <DBO/StringUtils.h>
  7. #include <Business/FreeFareDeviceBusiness.h>
  8. #include <iomanip>
  9. #include "DBO/Result.h"
  10. #include "Business/LibNfcBusiness.h"
  11. #include "CommandLineParser.h"
  12. #include "MainClass.h"
  13. MainClass::MainClass(int argc, char *argv[])
  14. : _argc(argc)
  15. , _argv(argv)
  16. {
  17. }
  18. int MainClass::main()
  19. {
  20. std::cout << "LibNfc version: " << LibNfcBusiness::getVersion() << std::endl;
  21. LibNfcBusiness libNfc;
  22. auto init = libNfc.init();
  23. if (!init) {
  24. init.print();
  25. return 1;
  26. }
  27. auto devicesResult = libNfc.getDevices();
  28. if (!devicesResult) {
  29. devicesResult.print();
  30. return 2;
  31. }
  32. auto devices = devicesResult.getData();
  33. if (devices.size() == 0) {
  34. std::cerr << "No NFC device found" << std::endl;
  35. return 3;
  36. }
  37. std::cout << "Found " << devices.size() << " devices: " << std::endl;
  38. for (size_t i = 0; i < devices.size(); ++i) {
  39. std::cout << devices[i]->getConnStr() << std::endl;
  40. }
  41. std::cout << "Using first device" << std::endl;
  42. auto device = devices[0];
  43. auto open = device->open();
  44. if (!open) {
  45. open.print();
  46. return 4;
  47. }
  48. FreeFareDeviceBusiness freeFareDevice(device);
  49. auto tagsResult = freeFareDevice.getTags();
  50. if (!tagsResult) {
  51. tagsResult.print();
  52. return 5;
  53. }
  54. auto tags = tagsResult.getData();
  55. if (tags.size() == 0) {
  56. std::cerr << "No tag found" << std::endl;
  57. return 6;
  58. }
  59. std::cout << "Found " << tagsResult.getData().size() << " tags:" << std::endl;
  60. for (size_t i = 0; i < tagsResult.getData().size(); ++i) {
  61. auto tag = tagsResult.getData()[i];
  62. std::cout << "UID: " << tag->getUid() << std::endl;
  63. std::cout << "Type: " << tag->getType() << std::endl;
  64. }
  65. std::cout << "Using first tag" << std::endl;
  66. auto tag = tags[0];
  67. std::vector<std::string> keys;
  68. // keys.push_back(StringUtils::humanToRaw("8829da9daf76").getData());
  69. // keys.push_back(StringUtils::humanToRaw("ffffffffffff").getData());
  70. keys.push_back(StringUtils::humanToRaw("484558414354").getData());
  71. int res = dump(tag, keys);
  72. // int res = mapKeys(tag, keys);
  73. device->close();
  74. libNfc.clean();
  75. return res;
  76. }
  77. int MainClass::mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
  78. {
  79. auto mappedKeysResult = tag->mapKeys(keys, printPercent);
  80. std::cout << "\r";
  81. if (!mappedKeysResult) {
  82. mappedKeysResult.print();
  83. }
  84. else {
  85. auto mappedKeys = mappedKeysResult.getData();
  86. for (int s = 0; s < mappedKeys.size(); ++s) {
  87. auto sectorKeys = mappedKeys[s];
  88. std::cout << "+Sector: " << s << std::endl;
  89. for (int b = 0; b < sectorKeys.size(); ++b) {
  90. auto blockKeys = sectorKeys[b];
  91. std::cout << "+Block: " << b << std::endl;
  92. std::cout << "Key A: " << StringUtils::rawToHuman(blockKeys.first) << std::endl;
  93. std::cout << "Key B: " << StringUtils::rawToHuman(blockKeys.second) << std::endl;
  94. }
  95. }
  96. }
  97. return 0;
  98. }
  99. int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
  100. {
  101. auto dumpResult = tag->dump(keys, printPercent, printPercent);
  102. std::cout << "\r";
  103. if (!dumpResult) {
  104. dumpResult.print();
  105. return 7;
  106. }
  107. auto dump = dumpResult.getData();
  108. for(int s = 0; s < 16; ++s) {
  109. std::cout << "+Sector: " << s << std::endl;
  110. auto sector = dump[s];
  111. for (int b = 0; b < 3; ++b) {
  112. std::cout << (sector.hasBlock(b) ? StringUtils::rawToHuman(sector.getBlock(b)) : std::string(32, '-')) << std::endl;
  113. }
  114. std::cout << "" << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
  115. << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
  116. << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
  117. std::cout << "+Trailer key A: " << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
  118. << "\t AC bits: " << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
  119. << "\t key B: " << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
  120. AccessBitsDbo accessBitsDbo = sector.getAccessBitsDbo();
  121. for (int b = 0; b < 3; ++b) {
  122. std::cout << "+Block: " << b << " ";
  123. printBlockAccessBits(accessBitsDbo, b);
  124. }
  125. std::cout << "+Block: 4 ";
  126. printTrailerAccessBits(accessBitsDbo);
  127. }
  128. return 0;
  129. }
  130. void MainClass::printBlockAccessBits(const AccessBitsDbo &accessBits, int block)
  131. {
  132. std::cout << "read: " << (accessBits.canKeyAReadBlock(block) ? "A" : "") << (accessBits.canKeyBReadBlock(block) ? "B" : "");
  133. std::cout << "\t write: " << (accessBits.canKeyAWriteBlock(block) ? "A" : "") << (accessBits.canKeyBWriteBlock(block) ? "B" : "");
  134. std::cout << "\t increment: " << (accessBits.canKeyAIncrementBlock(block) ? "A" : "") << (accessBits.canKeyBIncrementBlock(block) ? "B" : "");
  135. std::cout << "\t decrement: " << (accessBits.canKeyADecrementBlock(block) ? "A" : "") << (accessBits.canKeyBDecrementBlock(block) ? "B" : "") << std::endl;
  136. }
  137. void MainClass::printTrailerAccessBits(const AccessBitsDbo &accessBits)
  138. {
  139. std::cout << "key A read: " << (accessBits.canKeyAReadKeyATrailer() ? "A" : "") << (accessBits.canKeyBReadKeyATrailer() ? "B" : "");
  140. std::cout << "\t key A write: " << (accessBits.canKeyAWriteKeyATrailer() ? "A" : "") << (accessBits.canKeyBWriteKeyATrailer() ? "B" : "");
  141. std::cout << "\t AC bits read: " << (accessBits.canKeyAReadAccessBitsTrailer() ? "A" : "") << (accessBits.canKeyBReadAccessBitsTrailer() ? "B" : "");
  142. std::cout << "\t AC bits write: " << (accessBits.canKeyAWriteAccessBitsTrailer() ? "A" : "") << (accessBits.canKeyBWriteAccessBitsTrailer() ? "B" : "");
  143. std::cout << "\t key B read: " << (accessBits.canKeyAReadKeyBTrailer() ? "A" : "") << (accessBits.canKeyBReadKeyBTrailer() ? "B" : "");
  144. std::cout << "\t key B write: " << (accessBits.canKeyAWriteKeyBTrailer() ? "A" : "") << (accessBits.canKeyBWriteKeyBTrailer() ? "B" : "") << std::endl;;
  145. }
  146. void MainClass::printPercent(int done, int total)
  147. {
  148. std::cout << "\r" << std::fixed << std::setprecision(1) << ((float)done / (float)total * 100.0) << "%" << std::flush;
  149. }