Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MainClass.cpp 5.8KB

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