123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- //
- // Created by robin on 8/8/15.
- //
-
- #include <iostream>
- #include <sysexits.h>
- #include <iomanip>
- #include <fstream>
- #include <libnfc_cpptools/StringUtils.h>
- #include <libnfc_cpptools/FreeFareDevice.h>
- #include <libnfc_cpptools/Result.h>
- #include <libnfc_cpptools/LibNfc.h>
- #include "CommandLineParser.h"
- #include "MainClass.h"
-
- #define EX_OUTPUT_ERROR 1
- #define EX_INPUT_ERROR 2
- #define EX_KEY_ERROR 10
- #define EX_LIB_NFC_ERROR 12
- #define EX_NFC_DEVICE_NOT_FOUND 13
- #define EX_NFC_TAG_NOT_FOUND 14
- #define EX_MAP_KEYS_ERROR 15
- #define EX_READ_ERROR 16
- #define EX_WRITE_ERROR 17
-
- MainClass::MainClass(int argc, char *argv[])
- : _argc(argc)
- , _argv(argv)
- {
- }
-
- int MainClass::main()
- {
- CommandLineParser parser(_argc, _argv);
- CommandLineOption optionVersion(&parser, "version", 'v', "Show libnfc and mifare-tools versions");
- CommandLineOption optionHelp(&parser, "help", 'h', "Show this help");
-
- CommandLineOption optionMap(&parser, "map", 'm', "Map keys for the tag");
- CommandLineOption optionRead(&parser, "read", 'r', "Read the tag");
- CommandLineOption optionWrite(&parser, "write", 'w', "Write the tag");
- CommandLineOption optionDevices(&parser, "devices", 'd', "List NFC devices");
- CommandLineOption optionTags(&parser, "tags", 't', "List NFC tags");
-
- CommandLineOption optionDevice(&parser, "device", 'e', "Use the device DEVICE", "DEVICE");
- CommandLineOption optionTag(&parser, "tag", 'u', "Use the UID tag", "UID");
-
- CommandLineOption optionKeyFile(&parser, "key-file", 'f', "Path to a file containing keys", "FILE");
- CommandLineOption optionKey(&parser, "key", 'k', "Key to use to authenticate", "KEY");
-
- CommandLineOption optionOutput(&parser, "output", 'o', "Redirect output to FILE. '-' to use stdout", "FILE", "-");
- CommandLineOption optionInput(&parser, "input", 'i', "Read input from FILE. '-' to use stdin", "FILE", "-");
-
- if (!parser.parse()) {
- return parser.showHelp(EX_USAGE);
- }
- std::string outputFile = optionOutput.getDefaultValue();
- if (optionOutput.isSet()) {
- outputFile = optionOutput.getValue();
- }
-
- if (outputFile != "-" && !outputFile.empty()) {
- std::shared_ptr<std::ofstream> fileCout = std::make_shared<std::ofstream>();
- fileCout->open(outputFile);
- if (!*fileCout) {
- std::cerr << "Failed to redirect output: " << strerror(errno) << std::endl;
- return EX_OUTPUT_ERROR;
- }
- _outputStream = fileCout;
- }
-
- std::string inputFile = optionInput.getDefaultValue();
- if (optionInput.isSet()) {
- inputFile = optionInput.getValue();
- }
- if (inputFile != "-" && !inputFile.empty()) {
- std::shared_ptr<std::ifstream> fileCin = std::make_shared<std::ifstream>();
- fileCin->open(inputFile);
- if (!*fileCin) {
- std::cerr << "Failed to open input file: " << strerror(errno) << std::endl;
- return EX_INPUT_ERROR;
- }
- _inputStream = fileCin;
- }
-
- if (optionVersion.isSet()) {
- printVersion();
- return EX_OK;
- }
- if (optionHelp.isSet()) {
- return parser.showHelp(EX_OK, false);
- }
-
- std::string deviceName = optionDevice.getDefaultValue();
- if (optionDevice.isSet()) {
- deviceName = optionDevice.getValue();
- }
-
- std::string tagUid = optionTag.getDefaultValue();
- if (optionTag.isSet()) {
- tagUid = optionTag.getValue();
- }
-
- std::vector<std::string> keys;
- if (optionKeyFile.isSet()) {
- for (auto filePath : optionKeyFile.getValues()) {
- auto keysResult = readFile(filePath);
- if (!keysResult) {
- keysResult.print();
- return EX_KEY_ERROR;
- }
- auto fileKeys = keysResult.getData();
- keys.insert(keys.end(), fileKeys.begin(), fileKeys.end());
- }
- }
- if (optionKey.isSet()) {
- for (auto key : optionKey.getValues()) {
- auto keyResult = LibNfc::Utils::StringUtils::humanToRaw(key);
- key = keyResult.getData();
- if (!keyResult || key.length() != 6) {
- std::cerr << "Invalid key" << std::endl;
- return EX_KEY_ERROR;
- }
- keys.push_back(key);
- }
- }
-
- std::string inputData = "";
-
- int res = EX_OK;
- Actions action;
- if (optionRead.isSet()) {
- action = Read;
- }
- else if (optionMap.isSet()) {
- action = Map;
- }
- else if (optionWrite.isSet()) {
- auto readResult = readStream(cin());
- if (!readResult) {
- readResult.print();
- return EX_INPUT_ERROR;
- }
- for (auto data : readResult.getData()) {
- inputData += data;
- }
- action = Write;
- }
- else if (optionDevices.isSet()) {
- action = Devices;
- }
- else if (optionTags.isSet()) {
- action = Tags;
- }
- else {
- std::cerr << "Must select an action (map|read|write|devices|tags)" << std::endl;
- return EX_USAGE;
- }
-
- LibNfc::Core::LibNfcContext libNfc;
- auto init = libNfc.init();
- if (!init) {
- init.print();
- res = EX_LIB_NFC_ERROR;
- }
- else
- {
- auto devicesResult = libNfc.getDevices();
- if (!devicesResult)
- {
- devicesResult.print();
- res = EX_LIB_NFC_ERROR;
- }
- else
- {
- auto devices = devicesResult.getData();
-
- if (action == Devices)
- {
- for (auto device : devices) {
- cout() << device->getConnStr() << std::endl;
- }
- }
- else
- {
- auto device = getDevice(deviceName, devices);
- if (device == 0)
- {
- std::cerr << "NFC device not found" << std::endl;
- res = EX_NFC_DEVICE_NOT_FOUND;
- }
- else {
- auto open = device->open();
- if (!open) {
- open.print();
- res = EX_LIB_NFC_ERROR;
- }
- else {
-
- LibNfc::FreeFare::FreeFareDevice freeFareDevice(device);
- auto tagsResult = freeFareDevice.getTags();
- if (!tagsResult) {
- tagsResult.print();
- res = EX_LIB_NFC_ERROR;
- }
-
- auto tags = tagsResult.getData();
-
- if (action == Tags) {
- for (auto tag : tags) {
- cout() << "UID=" << tag->getUid() << " \tType="
- << LibNfc::FreeFare::FreeFareTag::getTagTypeString(tag->getType()) << std::endl;
- }
- }
- else {
- auto tag = getTag(tagUid, tags);
- if (tag == 0) {
- std::cerr << "Tag not found" << std::endl;
- res = EX_NFC_TAG_NOT_FOUND;
- }
- else {
- if (action == Read) {
- res = read(tag, keys);
- }
- else if (action == Map) {
- res = mapKeys(tag, keys);
- }
- else if (action == Write) {
- res = write(tag, keys, inputData);
- }
- }
- }
- }
- device->close();
- }
- }
- }
- libNfc.clean();
- }
-
- if (_outputStream != 0) {
- _outputStream->close();
- }
- if (_inputStream != 0) {
- _inputStream->close();
- }
- return res;
- }
-
- int MainClass::mapKeys(std::shared_ptr<LibNfc::FreeFare::FreeFareTag> tag, std::vector<std::string> keys)
- {
- auto mappedKeysResult = tag->mapKeys(keys, printPercentMapKeys);
- if (!mappedKeysResult) {
- mappedKeysResult.print();
- return EX_MAP_KEYS_ERROR;
- }
- else {
- auto mappedKeys = mappedKeysResult.getData();
- for (int s = 0; s < mappedKeys.size(); ++s) {
- auto sectorKey = mappedKeys[s];
- cout() << "+Sector: " << s << std::endl;
- for (int b = 0; b < 4; ++b) {
- cout() << "+Block: " << b << std::endl;
- cout() << "+Key: A" << std::endl << (!sectorKey.first.empty() ? LibNfc::Utils::StringUtils::rawToHuman(sectorKey.first) : std::string(12, '-')) << std::endl;
- cout() << "+Key: B" << std::endl << (!sectorKey.second.empty() ? LibNfc::Utils::StringUtils::rawToHuman(sectorKey.second) : std::string(12, '-')) << std::endl;
- }
- }
- }
- return EX_OK;
- }
-
- int MainClass::read(std::shared_ptr<LibNfc::FreeFare::FreeFareTag> tag, std::vector<std::string> keys)
- {
- auto readResult = tag->read(keys, printPercentMapKeys, printPercentDump);
- if (!readResult) {
- readResult.print();
- return EX_READ_ERROR;
- }
- auto read = readResult.getData();
- printSectors(read);
- return EX_OK;
- }
-
- int MainClass::write(std::shared_ptr<LibNfc::FreeFare::FreeFareTag> tag, std::vector<std::string> keys, const std::string &data)
- {
- auto writeResult = tag->write(keys, data, false, printPercentMapKeys, printPercentWrite);
- if (!writeResult) {
- writeResult.print();
- return EX_WRITE_ERROR;
- }
- // std::vector<LibNfc::FreeFare::FreeFareSector> sectors;
- // std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 1024);
- // for (int i = 0; i < 16; ++i) {
- // LibNfc::FreeFare::FreeFareSector LibNfc::FreeFare::FreeFareSector(d.substr(i * 64, 64));
- // sectors.push_back(LibNfc::FreeFare::FreeFareSector);
- // }
- // printSectors(sectors);
- return EX_OK;
- }
-
- void MainClass::printSectors(const std::vector<LibNfc::FreeFare::FreeFareSector> §ors)
- {
- for(int s = 0; s < sectors.size(); ++s) {
- cout() << "+Sector: " << s << std::endl;
- auto sector = sectors[s];
- for (int b = 0; b < 3; ++b) {
- cout() << (sector.hasBlock(b) ? LibNfc::Utils::StringUtils::rawToHuman(sector.getBlock(b)) : std::string(32, '-')) << std::endl;
- }
- cout() << (sector.hasKeyA() ? LibNfc::Utils::StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
- << (sector.hasAccessBits() ? LibNfc::Utils::StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
- << (sector.hasKeyB() ? LibNfc::Utils::StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
-
-
- cout() << "+Trailer key A: " << (sector.hasKeyA() ? LibNfc::Utils::StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
- << "\t AC bits: " << (sector.hasAccessBits() ? LibNfc::Utils::StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
- << "\t key B: " << (sector.hasKeyB() ? LibNfc::Utils::StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
- LibNfc::FreeFare::FreeFareAccessBits accessBitsDbo = sector.getAccessBitsDbo();
- for (int b = 0; b < 3; ++b) {
- cout() << "+Block: " << b << " ";
- printBlockAccessBits(accessBitsDbo, b);
- }
- cout() << "+Block: 3 ";
- printTrailerAccessBits(accessBitsDbo);
- }
- }
-
- void MainClass::printBlockAccessBits(const LibNfc::FreeFare::FreeFareAccessBits &accessBits, int block)
- {
- cout() << "read: " << (accessBits.canKeyAReadBlock(block) ? "A" : " ") << (accessBits.canKeyBReadBlock(block) ? "B" : " ");
- cout() << "\t write: " << (accessBits.canKeyAWriteBlock(block) ? "A" : " ") << (accessBits.canKeyBWriteBlock(block) ? "B" : " ");
- cout() << "\t increment: " << (accessBits.canKeyAIncrementBlock(block) ? "A" : " ") << (accessBits.canKeyBIncrementBlock(block) ? "B" : " ");
- cout() << "\t decrement: " << (accessBits.canKeyADecrementBlock(block) ? "A" : " ") << (accessBits.canKeyBDecrementBlock(block) ? "B" : " ") << std::endl;
- }
-
- void MainClass::printTrailerAccessBits(const LibNfc::FreeFare::FreeFareAccessBits &accessBits)
- {
- cout() << "key A read: " << (accessBits.canKeyAReadKeyATrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyATrailer() ? "B" : " ");
- cout() << "\t key A write: " << (accessBits.canKeyAWriteKeyATrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyATrailer() ? "B" : " ");
-
- cout() << "\t AC bits read: " << (accessBits.canKeyAReadAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBReadAccessBitsTrailer() ? "B" : " ");
- cout() << "\t AC bits write: " << (accessBits.canKeyAWriteAccessBitsTrailer() ? "A" : " ") << (accessBits.canKeyBWriteAccessBitsTrailer() ? "B" : " ");
-
- cout() << "\t key B read: " << (accessBits.canKeyAReadKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBReadKeyBTrailer() ? "B" : " ");
- cout() << "\t key B write: " << (accessBits.canKeyAWriteKeyBTrailer() ? "A" : " ") << (accessBits.canKeyBWriteKeyBTrailer() ? "B" : " ") << std::endl;;
- }
-
- void MainClass::printPercent(int done, int total, const std::string& header)
- {
- if (isatty(fileno(stdout))) {
- std::cout << "\r\033[2K" << header << ": " << std::fixed << std::setprecision(1)
- << ((float) done / (float) total * 100.0) << "%" << std::flush;
- if (done == total) {
- std::cout << std::endl;
- }
- }
- }
-
- void MainClass::printPercentMapKeys(int done, int total)
- {
- printPercent(done, total, "Mapping keys");
- }
-
- void MainClass::printPercentDump(int done, int total)
- {
- printPercent(done, total, "Dumping");
- }
-
- void MainClass::printPercentWrite(int done, int total)
- {
- printPercent(done, total, "Writing");
- }
-
- void MainClass::printVersion()
- {
- cout() << "LibNfc version: " << LibNfc::Core::LibNfcContext::getLibNfcVersion() << std::endl;
- cout() << "Mifare-tools version: " << LibNfc::Core::LibNfcContext::getMifareToolsVersion() << std::endl;
- }
-
- std::shared_ptr<LibNfc::Core::NfcDevice> MainClass::getDevice(const std::string &deviceName, std::vector<std::shared_ptr<LibNfc::Core::NfcDevice>> devices)
- {
- if (deviceName.empty()) {
- if (devices.size() > 0) {
- return devices[0];
- }
- }
- else {
- for (auto d : devices) {
- if (d->getConnStr() == deviceName) {
- return d;
- }
- }
- }
- return 0;
- }
-
- std::shared_ptr<LibNfc::FreeFare::FreeFareTag> MainClass::getTag(const std::string &tagUid,
- std::vector<std::shared_ptr<LibNfc::FreeFare::FreeFareTag>> tags)
- {
- if (tagUid.empty()) {
- if (tags.size() > 0) {
- return tags[0];
- }
- }
- else {
- for (auto t : tags) {
- if (t->getUid() == tagUid) {
- return t;
- }
- }
- }
- return 0;
- }
-
- LibNfc::Utils::Result<std::vector<std::string>> MainClass::readFile(const std::string &filePath)
- {
- std::ifstream fileInput(filePath);
- if (fileInput) {
- return readStream(fileInput);
- }
- else {
- return LibNfc::Utils::Result<std::vector<std::string>>::error("Failed to open file: " + std::string(strerror(errno)));
- }
- }
-
- LibNfc::Utils::Result<std::vector<std::string>> MainClass::readStream(std::istream &stream)
- {
- std::vector<std::string> lines;
- while (!stream.eof()) {
- std::string line;
- std::getline(stream, line);
- line = LibNfc::Utils::StringUtils::removeSpaces(line);
- if (line.compare(0, 1, "#") != 0 && line.compare(0, 1, "+") != 0) {
- std::replace(line.begin(), line.end(), '-', '0');
- auto keyResult = LibNfc::Utils::StringUtils::humanToRaw(line);
- if (!keyResult) {
- return LibNfc::Utils::Result<std::vector<std::string>>::error("Invalid data");
- }
- line = keyResult.getData();
- lines.push_back(line);
- }
- }
- return LibNfc::Utils::Result<std::vector<std::string>>::ok(lines);
- }
-
- std::ostream &MainClass::cout()
- {
- return _outputStream == 0 ? std::cout : *_outputStream;
- }
-
- std::istream &MainClass::cin()
- {
- return _inputStream == 0 ? std::cin : *_inputStream;
- }
|