-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmain.cpp
122 lines (99 loc) · 3.23 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
extern "C" {
#include <nfc/nfc-types.h>
#include <nfc/nfc.h>
}
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include "headers/application.h"
#include "headers/device_nfc.h"
#include "headers/tools.h"
enum Mode { fast, full, GPO, UNKNOWN };
void brute_device_records(DeviceNFC &device, Application &app, Mode mode) {
device.select_application(app);
std::cerr << std::endl;
size_t to_record = (mode == Mode::fast ? 16:255);
for (size_t sfi = 1; sfi <= 31; ++sfi) {
bool file_exist = false;
for (size_t record = 1; record <= to_record; ++record) {
APDU res = device.read_record(sfi, record);
byte_t sw1 = res.data[res.size-2];
byte_t sw2 = res.data[res.size-1];
if (sw1 == 0x90 && sw2 == 0x00) {
file_exist = true;
} else if (sw1 == 0x6A && sw2 == 0x82) {
break; // FileNotExist at this SFI
}
}
if (file_exist) {
std::cerr << std::endl;
}
}
}
void walk_through_gpo_files(DeviceNFC &device, Application &app) {
device.select_application(app);
APDU gpo = device.get_processing_options(app);
// Find AFL tag to locate files on card
size_t i = 0;
while (i < gpo.size && gpo.data[i] != 0x94) {
i++;
}
if (i >= gpo.size) {
std::cerr << "Can't get AFL" << std::endl;
return;
}
APDU afl = {0, {0}};
afl.size = parse_TLV(afl.data, gpo.data, i);
std::cerr << std::endl;
for (size_t j = 0; j < afl.size; j+=4) {
byte_t sfi = afl.data[j] >> 3;
byte_t from_record = afl.data[j+1];
byte_t to_record = afl.data[j+2];
for (size_t record = from_record; record <= to_record; ++record) {
device.read_record(sfi, record);
}
std::cerr << std::endl;
}
}
int main(int argc, char *argv[]) {
Mode mode = Mode::UNKNOWN;
if (argc == 1) {
std::cerr << GREEN("[Info]") << " Use mode 'fast', 'full' or 'GPO'" << std::endl;
return 0;
}
std::string mode_str(argv[1]);
if (mode_str == "fast") {
mode = Mode::fast;
} else if (mode_str == "full") {
mode = Mode::full;
} else if (mode_str == "GPO") {
mode = Mode::GPO;
} else {
std::cerr << RED("[Error]") << " Unknown mode" << std::endl;
return 0;
}
try {
DeviceNFC device;
std::cerr << GREEN("[Info]") << " NFC reader: " << device.get_name() << " opened.\n";
while (!device.pool_target()) {
std::cerr << GREEN("[Info]") << " Searching card..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::cerr << std::endl;
std::vector<Application> list = device.load_applications_list();
if (mode == Mode::fast || mode == Mode::full) {
for (Application &app : list) {
brute_device_records(device, app, mode);
}
} else if (mode == Mode::GPO) {
for (Application &app : list) {
walk_through_gpo_files(device, app);
}
}
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}