-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdump_tag.c
329 lines (288 loc) · 11.4 KB
/
dump_tag.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright 2019-2020 Giacomo Ferretti
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <nfc/nfc.h>
#include <inttypes.h>
#include "logging.h"
#include "nfc_utils.h"
static void print_usage(const char *executable) {
printf("Usage: %s [dump.bin] [-h] [-v] [-u] [-s] [-a] [-r] [-y] [-t x4k|512]\n", executable);
printf("\nOptional arguments:\n");
printf(" [dump.bin] dump EEPROM to file\n");
printf("\nOptions:\n");
printf(" -h show this help message\n");
printf(" -v enable verbose - print debugging data\n");
printf(" -s print system block\n");
printf(" -u print UID\n");
printf(" -a enable -s and -u flags together\n");
printf(" -r fix read direction\n");
printf(" -y answer YES to all questions\n");
printf(" -t x4k|512 select SRIX4K or SRI512 tag type [default: x4k]\n");
}
int main(int argc, char *argv[], char *envp[]) {
bool print_system_block = false;
bool print_uid = false;
bool fix_read_direction = false;
bool skip_confirmation = false;
char *output_path = NULL;
uint32_t eeprom_size = SRIX4K_EEPROM_SIZE;
uint32_t eeprom_blocks_amount = SRIX4K_EEPROM_BLOCKS;
// Parse options
int opt = 0;
while ((opt = getopt(argc, argv, "hvusaryt:")) != -1) {
switch (opt) {
case 'v':
set_verbose(true);
break;
case 'a':
print_system_block = true;
print_uid = true;
break;
case 's':
print_system_block = true;
break;
case 'u':
print_uid = true;
break;
case 'r':
fix_read_direction = true;
break;
case 'y':
skip_confirmation = true;
break;
case 't':
if (strcmp(optarg, "512") == 0) {
eeprom_size = SRI512_EEPROM_SIZE;
eeprom_blocks_amount = SRI512_EEPROM_BLOCKS;
}
break;
default:
case 'h':
print_usage(argv[0]);
exit(0);
}
}
// Check arguments
if ((argc - optind) > 0) {
output_path = argv[optind];
}
// Initialize NFC
nfc_context *context = NULL;
nfc_device *reader = NULL;
nfc_init(&context);
if (context == NULL) {
lerror("Unable to init libnfc. Exiting...\n");
exit(1);
}
// Display libnfc version
lverbose("libnfc version: %s\n", nfc_version());
// Search for readers
lverbose("Searching for readers... ");
nfc_connstring connstrings[MAX_DEVICE_COUNT] = {};
size_t num_readers = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
lverbose("found %zu.\n", num_readers);
// Check if no readers are available
if (num_readers == 0) {
lerror("No readers available. Exiting...\n");
close_nfc(context, reader);
exit(1);
}
// Print out readers
for (unsigned int i = 0; i < num_readers; i++) {
if (i == num_readers - 1) {
lverbose("└── ");
} else {
lverbose("├── ");
}
lverbose("[%d] %s\n", i, connstrings[i]);
}
lverbose("Opening %s...\n", connstrings[0]);
// Open first reader
reader = nfc_open(context, connstrings[0]);
if (reader == NULL) {
lerror("Unable to open NFC device. Exiting...\n");
close_nfc(context, reader);
exit(1);
}
// Set opened NFC device to initiator mode
if (nfc_initiator_init(reader) < 0) {
lerror("nfc_initiator_init => %s\n", nfc_strerror(reader));
close_nfc(context, reader);
exit(1);
}
lverbose("NFC reader: %s\n", nfc_device_get_name(reader));
nfc_target target_key[MAX_TARGET_COUNT];
/*
* This is a known bug from libnfc.
* To read ISO14443B2SR you have to initiate first ISO14443B to configure internal registers.
*
* https://github.com/nfc-tools/libnfc/issues/436#issuecomment-326686914
*/
lverbose("Searching for ISO14443B targets... found %d.\n", nfc_initiator_list_passive_targets(reader, nmISO14443B, target_key, MAX_TARGET_COUNT));
lverbose("Searching for ISO14443B2SR targets...");
int ISO14443B2SR_targets = nfc_initiator_list_passive_targets(reader, nmISO14443B2SR, target_key, MAX_TARGET_COUNT);
lverbose(" found %d.\n", ISO14443B2SR_targets);
// Check for tags
if (ISO14443B2SR_targets == 0) {
printf("Waiting for tag...\n");
// Infinite select for tag
if (nfc_initiator_select_passive_target(reader, nmISO14443B2SR, NULL, 0, target_key) <= 0) {
lerror("nfc_initiator_select_passive_target => %s\n", nfc_strerror(reader));
close_nfc(context, reader);
exit(1);
}
}
// Read UID
uint8_t uid_rx_bytes[MAX_RESPONSE_LEN] = {};
uint8_t uid_bytes_read = nfc_srix_get_uid(reader, uid_rx_bytes);
// Check for errors
if (uid_bytes_read != 8) {
lerror("Error while reading UID. Exiting...\n");
lverbose("Received %d bytes instead of 8.\n", uid_bytes_read);
close_nfc(context, reader);
exit(1);
}
// Convert to uint64
uint64_t uid = (uint64_t) uid_rx_bytes[7] << 56u | (uint64_t) uid_rx_bytes[6] << 48u |
(uint64_t) uid_rx_bytes[5] << 40u | (uint64_t) uid_rx_bytes[4] << 32u |
(uint64_t) uid_rx_bytes[3] << 24u | (uint64_t) uid_rx_bytes[2] << 16u |
(uint64_t) uid_rx_bytes[1] << 8u | (uint64_t) uid_rx_bytes[0];
// Print UID
if (print_uid) {
printf("UID: %016" PRIX64 "\n", uid);
// Convert uint64 to binary char array
char uid_binary[65] = {};
for (unsigned int i = 0; i < sizeof(uid); i++) {
uint8_t tmp = (uid >> (sizeof(uid) - 1 - i) * 8u) & 0xFFu;
sprintf(uid_binary + i * 8 + 0, "%c", tmp & 0x80u ? '1' : '0');
sprintf(uid_binary + i * 8 + 1, "%c", tmp & 0x40u ? '1' : '0');
sprintf(uid_binary + i * 8 + 2, "%c", tmp & 0x20u ? '1' : '0');
sprintf(uid_binary + i * 8 + 3, "%c", tmp & 0x10u ? '1' : '0');
sprintf(uid_binary + i * 8 + 4, "%c", tmp & 0x08u ? '1' : '0');
sprintf(uid_binary + i * 8 + 5, "%c", tmp & 0x04u ? '1' : '0');
sprintf(uid_binary + i * 8 + 6, "%c", tmp & 0x02u ? '1' : '0');
sprintf(uid_binary + i * 8 + 7, "%c", tmp & 0x01u ? '1' : '0');
}
printf("├── Prefix: %02" PRIX64 "\n", uid >> 56u);
printf("├── IC manufacturer code: %02" PRIX64, (uid >> 48u) & 0xFFu);
switch ((uid >> 48u) & 0xFFu) {
case 0x02:
printf(" (STMicroelectronics)\n");
break;
default:
printf(" (unknown)\n");
}
// Print 6bit IC code
char ic_code[7] = {};
memcpy(ic_code, uid_binary + 16, 6);
printf("├── IC code: %s [%" PRIu64 "]\n", ic_code, (uid >> 42u) & 0x7u);
// Print 42bit unique serial number
char unique_serial_number[43] = {};
memcpy(unique_serial_number, uid_binary + 22, 42);
printf("└── 42bit unique serial number: %s [%" PRIu64 "]\n", unique_serial_number, uid & 0x3FFFFFFFFFFu);
}
// Read EEPROM
uint8_t *eeprom_bytes = malloc(sizeof(uint8_t) * eeprom_size);
lverbose("Reading %d blocks...\n", eeprom_blocks_amount);
for (int i = 0; i < eeprom_blocks_amount; i++) {
uint8_t *current_block = eeprom_bytes + (i * 4);
uint8_t block_bytes_read = nfc_srix_read_block(reader, current_block, i);
// Check for errors
if (block_bytes_read != 4) {
lerror("Error while reading block %d. Exiting...\n", i);
lverbose("Received %d bytes instead of 4.\n", block_bytes_read);
close_nfc(context, reader);
exit(1);
}
printf("[%02X] ", i);
if (fix_read_direction) {
printf("%02X %02X %02X %02X ", current_block[3], current_block[2], current_block[1], current_block[0]);
} else {
printf("%02X %02X %02X %02X ", current_block[0], current_block[1], current_block[2], current_block[3]);
}
printf(DIM);
printf("--- %s\n", srix_get_block_type(i));
printf(RESET);
}
if (print_system_block) {
uint8_t system_block_bytes[4] = {};
uint8_t system_block_bytes_read = nfc_srix_read_block(reader, system_block_bytes, 0xFF);
// Check for errors
if (system_block_bytes_read != 4) {
lerror("Error while reading block %d. Exiting...\n", 0xFF);
lverbose("Received %d bytes instead of 4.\n", system_block_bytes_read);
close_nfc(context, reader);
exit(1);
}
uint32_t system_block = system_block_bytes[3] << 24u | system_block_bytes[2] << 16u | system_block_bytes[1] << 8u | system_block_bytes[0];
printf("System block: %02X %02X %02X %02X\n", system_block_bytes[3], system_block_bytes[2], system_block_bytes[1], system_block_bytes[0]);
printf("├── CHIP_ID: %02X\n", system_block_bytes[0]);
printf("├── ST reserved: %02X%02X\n", system_block_bytes[1], system_block_bytes[2]);
printf("└── OTP_Lock_Reg:\n");
for (uint8_t i = 24; i < 32; i++) {
if (i == 31) {
printf(" └── b%d = %d - ", i, (system_block >> i) & 1u);
} else {
printf(" ├── b%d = %d - ", i, (system_block >> i) & 1u);
}
if (i == 24) {
printf("Block 07 and 08 are ");
} else {
printf("Block %02X is ", i - 16);
}
if (((system_block >> i) & 1u) == 0) {
printf(RED);
printf("LOCKED\n");
printf(RESET);
} else {
printf(GREEN);
printf("unlocked\n");
printf(RESET);
}
}
}
// Check if file already exists
FILE *file = fopen(output_path, "r");
if (file) {
fclose(file);
// Ask for confirmation
if (!skip_confirmation) {
printf("\"%s\" already exists.\n", output_path);
printf("Do you want to overwrite it? [Y/N] ");
char c = 'n';
scanf(" %c", &c);
if (c != 'Y' && c != 'y') {
printf("Exiting...\n");
close_nfc(context, reader);
exit(0);
}
}
}
// Dump to file
if (output_path != NULL) {
FILE *fp = fopen(output_path, "w");
fwrite(eeprom_bytes, eeprom_size, 1, fp);
fclose(fp);
printf("Written dump to \"%s\".\n", output_path);
}
// Close NFC
close_nfc(context, reader);
return 0;
}