-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMiCmd.cpp
More file actions
577 lines (459 loc) · 16.2 KB
/
MiCmd.cpp
File metadata and controls
577 lines (459 loc) · 16.2 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// MiCmd.cpp : Defines the entry point for the console application.
//
/*
* COPYLEFT Michal Boška aka Whitewash.
* This program is released under GPLv3 license ( http://www.gnu.org/licenses/gpl-3.0.txt )
*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <string>
#include <iostream>
#include <ios>
#include <sstream>
#include <ctype.h>
#include <math.h>
#ifdef WIN32
#include <windows.h>
#endif
extern "C" {
#include <nfc/nfc.h>
//#include <nfc/mifaretag.h>
}
using namespace std;
typedef unsigned char byte;
typedef unsigned int UINT;
typedef struct {
bool c1,c2,c3;
} AccessCondition;
static nfc_device_t* pdi;
static nfc_target_info_t ti;
static mifare_param param;
const string VERSION = "0.011";
bool connected = false;
bool b4k;
void set_console_size() {
#ifdef WIN32
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd,SW_MAXIMIZE);
#endif
}
byte* string_to_bytearray(string src, int* return_length)
{
int length = src.length() / 2;
byte* buffer = new byte[length];
byte* ptr = buffer;
*return_length = length;
byte c1,c2;
for (UINT i=0; i < src.length(); i += 2) {
if ((src[i] >= '0') && (src[i] <= '9'))
c1 = src[i] - '0';
if ((src[i] >= 'a') && (src[i] <= 'f'))
c1 = 10 + src[i] - 'a';
if ((src[i] >= 'A') && (src[i] <= 'F'))
c1 = 10 + src[i] - 'A';
if ((src[i+1] >= '0') && (src[i+1] <= '9'))
c2 = src[i+1] - '0';
if ((src[i+1] >= 'a') && (src[i+1] <= 'f'))
c2 = 10 + src[i+1] - 'a';
if ((src[i+1] >= 'A') && (src[i+1] <= 'F'))
c2 = 10 + src[i+1] - 'A';
*ptr++ = c1*16 + c2;
}
return buffer;
}
string bytearray_to_string(byte* arr, int length, bool spacing=true) {
string buffer = "";
char table[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
for (int i = 0; i < length; i++) {
buffer.append(&(table[*arr / 16]), 1);
buffer.append(&(table[*arr % 16]), 1);
if (spacing)
buffer.append(" ");
arr++;
}
return buffer;
}
bool is_first_block(uint32_t uiBlock)
{
// Test if we are in the small or big sectors
if (uiBlock < 128) return ((uiBlock)%4 == 0); else return ((uiBlock)%16 == 0);
}
bool is_trailer_block(uint32_t uiBlock)
{
// Test if we are in the small or big sectors
if (uiBlock < 128) return ((uiBlock+1)%4 == 0); else return ((uiBlock+1)%16 == 0);
}
uint32_t get_trailer_block(uint32_t uiFirstBlock)
{
// Test if we are in the small or big sectors
if (uiFirstBlock<128) return uiFirstBlock+3; else return uiFirstBlock+15;
}
void press_to_continue() {
string dummy = "";
cout << "Press ENTER to continue... ";
getline(cin, dummy);
}
void cls() {
#ifdef WIN32
system("cls");
#endif
#ifndef WIN32
system("clear");
#endif
}
void print_menu() {
cout << "\nMain menu:\n";
/* MC_AUTH_A = 0x60,
MC_AUTH_B = 0x61,
MC_READ = 0x30,
MC_WRITE = 0xA0,
MC_TRANSFER = 0xB0,
MC_DECREMENT = 0xC0,
MC_INCREMENT = 0xC1,
MC_STORE = 0xC2, */
cout << "h - display main menu\n";
cout << "o - Open connection\n";
cout << "at - analyse manually input trailer data\n";
cout << "cls, clear - Clear screen\n";
cout << "q - Exit\n";
cout << "\n-- After connection is successfully opened, you may use following\n-- additional commands:\n";
cout << "a - Authenticate with A key\n";
cout << "b - Authenticate with B key\n";
cout << "r - Read specific block data\n";
cout << "w - Write specific block data\n";
cout << "t - Transfer value block to volatile memory\n";
cout << "d - Decrement value block\n";
cout << "i - Increment value block\n";
cout << "s - ReStore value block\n";
cout << "c - Close existing connection\n";
cout << "\n";
}
void close_connection() {
cout << "Closing connection to " << pdi->acName << endl;
nfc_disconnect(pdi);
pdi = 0;
connected = false;
cout << "Connection closed." << endl;
}
bool open_connection() {
pdi = nfc_connect(NULL);
if (!pdi) {
cout << "Could not connect to the device." << endl;
return false;
}
nfc_initiator_init(pdi);
nfc_configure(pdi,NDO_ACTIVATE_FIELD,false);
nfc_configure(pdi,NDO_INFINITE_SELECT,false);
nfc_configure(pdi,NDO_HANDLE_CRC,true);
nfc_configure(pdi,NDO_HANDLE_PARITY,true);
nfc_configure(pdi,NDO_ACTIVATE_FIELD,true);
cout << "Connected to " << pdi->acName << endl;
if (!nfc_initiator_select_tag(pdi, NM_ISO14443A_106, NULL, 0, &ti)) {
cout << "No MIFARE tag found!" << endl;
press_to_continue();
close_connection();
return false;
}
if ((ti.nai.btSak & 0x08) == 0) {
cout << "This is not a valid MIFARE _classic_ tag!" << endl;
press_to_continue();
close_connection();
return false;
}
b4k = (ti.nai.abtAtqa[1] == 0x02);
cout << "Found MIFARE Classic " << (b4k ? 4 : 1) << "K tag, UID: " << bytearray_to_string(ti.nai.abtUid, 4) << endl;
return true;
}
// b3de9843c86d
bool authenticate(byte* key, bool keyB, uint8_t sector) {
uint8_t block = (sector+1)*4 - 1;
memcpy(param.mpa.abtUid, ti.nai.abtUid,4);
memcpy(param.mpa.abtKey, key, 6);
bool res = nfc_initiator_mifare_cmd(pdi, (keyB ? MC_AUTH_B : MC_AUTH_A), block, ¶m);
if (res)
cout << "Authentication successful. :-P" << endl;
else {
cout << "Authentication FAILURE! :'( Tag halted, reconnecting..." << endl;
close_connection();
connected = open_connection();
}
return res;
}
void parse_trailer(byte* data) {
byte keyA[6];
byte keyB[6];
byte AC[4];
byte mask = 0x01;
uint32_t ACint = 0;
memcpy(keyA, data, 6);
memcpy(AC, data+6, 4);
memcpy(keyB, data+10, 6);
memcpy(&ACint, data+6, 4);
cout << "Key A: " << bytearray_to_string(keyA, 6) << endl;
cout << "Key B: " << bytearray_to_string(keyB, 6) << endl << endl;
cout << "Access Conditions: " << bytearray_to_string(AC, 4) << endl;
cout << "AC matrix (x = bit set to 1; - = bit set to 0)\n" << endl;
cout << " C1 C2 C3 " << endl;
AccessCondition acs[4];
for (int i = 0; i <= 3; i++)
acs[i].c1 = ((AC[1] & (mask << (i+4))) > 0);
for (int i = 0; i <= 3; i++)
acs[i].c2 = ((AC[2] & (mask << (i))) > 0);
for (int i = 0; i <= 3; i++)
acs[i].c3 = ((AC[2] & (mask << (i+4))) > 0);
for (int i=3; i >= 0; i--) {
cout << "|" << (acs[i].c1 ? " x " : " - ") << (acs[i].c2 ? " x " : " - ") << (acs[i].c3 ? " x " : " - ") << "| block " << i << ((i == 3) ? " (trailer) " : "") << endl;
}
cout << endl;
cout << "-- Trailer block has following AC set: " << endl;
if ((!acs[3].c1) && (!acs[3].c2) && (!acs[3].c3))
cout << "Key A may be written by itself, AC may be read by key A, key B may be read and written by key A." << endl;
if ((!acs[3].c1) && (acs[3].c2) && (!acs[3].c3))
cout << "Key A cannot be changed, AC may be read by key A, key B may be read by key A." << endl;
if ((acs[3].c1) && (!acs[3].c2) && (!acs[3].c3))
cout << "Key A may be changed by key B, AC may be read by both keys, key B may be changed by key B." << endl;
if ((acs[3].c1) && (acs[3].c2) && (!acs[3].c3))
cout << "AC may be read by both keys, nothing else on the trailer block allowed." << endl;
if ((!acs[3].c1) && (!acs[3].c2) && (acs[3].c3))
cout << "Transport configuration, everything on trailer block (except reading key A) may be done by key A." << endl;
if ((!acs[3].c1) && (acs[3].c2) && (acs[3].c3))
cout << "Key A may be changed by key B, AC may be read by both keys, changed by key B, key B may be changed by key B." << endl;
if ((acs[3].c1) && (!acs[3].c2) && (acs[3].c3))
cout << "AC may be read by both keys, written by key B. Nothing else on the trailer block allowed." << endl;
if ((acs[3].c1) && (acs[3].c2) && (acs[3].c3))
cout << "AC may be read by both keys, nothing else on the trailer block allowed." << endl;
cout << endl;
for (int i=2; i >= 0; i--) {
cout << "-- Block " << i << " (relative to sector beginning) has following AC set: " << endl;
if ((!acs[i].c1) && (!acs[i].c2) && (!acs[i].c3))
cout << "Transport configuration; may be read, written, incremented and decremented by both keys." << endl;
if ((!acs[i].c1) && (acs[i].c2) && (!acs[i].c3))
cout << "May be read by both keys, read-only." << endl;
if ((acs[i].c1) && (!acs[i].c2) && (!acs[i].c3))
cout << "May be read by both keys, written by key B. Value operations not allowed." << endl;
if ((acs[i].c1) && (acs[i].c2) && (!acs[i].c3))
cout << "May be read and decremented by both keys, written and incremented only by key B" << endl;
if ((!acs[i].c1) && (!acs[i].c2) && (acs[i].c3))
cout << "Decrement-only block. May be read and decremented by both keys. Nothing else allowed." << endl;
if ((!acs[i].c1) && (acs[i].c2) && (acs[i].c3))
cout << "May be accessed (read/write) only by key B. Value operations not allowed." << endl;
if ((acs[i].c1) && (!acs[i].c2) && (acs[i].c3))
cout << "May be read only by key B, read-only." << endl;
if ((acs[i].c1) && (acs[i].c2) && (acs[i].c3))
cout << "Dead block, no operation is allowed." << endl;
cout << endl;
}
}
bool readblock(uint8_t block) {
bool res = nfc_initiator_mifare_cmd(pdi, MC_READ, block, ¶m);
if (res) {
cout << bytearray_to_string(param.mpd.abtData, 16) << "\n( " << bytearray_to_string(param.mpd.abtData, 16, false) << " ) " << endl;
if (is_trailer_block(block)) {
cout << "\nThis is the TRAILER block!" << endl;
parse_trailer(param.mpd.abtData);
}
} else {
cout << "Could not read the data block! Tag halted, reconnecting..." << endl;
close_connection();
connected = open_connection();
}
return res;
}
bool writeblock(uint8_t block, byte* data) {
if (is_trailer_block(block)) {
string response = "";
cout << "----- !!!!! WARNING !!!!! -----" << endl;
cout << "-- You are trying to write data into a trailer block of certain sector." << endl;
cout << "-- Writing incorrect data may damage your MIFARE card." << endl;
cout << "Proceed only if you are completely sure what you are doing." << endl;
cout << "Continue?? (y - yes, anything else - no) : " << endl;
getline(cin, response);
if (response.compare("y") != 0) {
cout << "Aborting." << endl;
return false;
}
}
memcpy(param.mpd.abtData, data, 16);
bool res = nfc_initiator_mifare_cmd(pdi, MC_WRITE, block, ¶m);
if (res) {
cout << "Successfully wrote " << bytearray_to_string(param.mpd.abtData, 16) << "into block " << (UINT) block << endl;
} else {
cout << "Could not write the data block! Tag halted, reconnecting..." << endl;
close_connection();
connected = open_connection();
}
return res;
}
bool valueblock(const mifare_cmd cmd, uint8_t block, byte* data) {
memcpy(param.mpv.abtValue, data, 4);
bool res = nfc_initiator_mifare_cmd(pdi, cmd, block, ¶m);
if (res) {
cout << "Command successfully completed." << endl;
if ((cmd == MC_INCREMENT) || (cmd == MC_DECREMENT))
cout << "Don't forget to TRANSFER(t) the data back to permanent memory of the chip" << endl;
} else {
cout << "Something failed. Command was NOT completed. Tag halted, reconnecting..." << endl;
close_connection();
connected = open_connection();
}
return res;
}
int main(int argc, char* argv[])
{
//set_console_size();
string menu_option;
connected = false;
cout << "\n*** MiCmd " << VERSION << " -- MIFARE(R) command line ***\n";
print_menu();
while (true) {
cout << '\n';
if (connected) {
cout << "You are CONNECTED to " << pdi->acName << endl;
cout << "Found MIFARE Classic " << (b4k ? 4 : 1) << "K tag, UID: " << bytearray_to_string(ti.nai.abtUid, 4) << endl;
}
else
cout << "You are NOT connected, additional commands will not work.\n";
cout << "Type h for help.\n";
cout << "MiCmd " << VERSION << "> " ;
getline(cin, menu_option);
cout << endl;
if ((menu_option.compare("q")) == 0) {
if (connected)
close_connection();
return 0;
}
if ((menu_option.compare("h")) == 0) {
print_menu();
continue;
}
if ((menu_option.compare("o")) == 0) {
if (connected) {
cout << "You are already connected, disconnect with c first!" << endl;
continue;
}
connected = open_connection();
continue;
}
if (((menu_option.compare("cls")) == 0) || (menu_option.compare("clear") == 0)) {
cls();
continue;
}
if (menu_option.compare("at") == 0) {
string data = "";
int length = 0;
cout << "Enter trailer block data (16B HEX value, without spaces): ";
getline(cin, data);
cout << endl;
cout << "-- !!!! ATTENTION !!!!\nManual parsing does NOT detect inner format violation of AC part of the trailer block." << endl;
cout << "-- It only computes AC bits from 4-byte value by applying bitwise operations." << endl;
cout << "-- It totally ignores inverse AC bits in those 4 bytes." << endl;
cout << "-- DO NOT rely on this when calculating correct 4-byte value to write into the trailer block!" << endl;
cout << "-- !!!! ATTENTION !!!!" << endl << endl;
parse_trailer(string_to_bytearray(data, &length));
continue;
}
if (connected) {
if ((menu_option.compare("c")) == 0) {
close_connection();
continue;
}
if (((menu_option.compare("a")) == 0) || ((menu_option.compare("b")) == 0)) {
byte* key_c;
int bbbb;
char tmp[20];
uint8_t block = 0;
cout << "Enter sector number: ";
fgets(tmp, 20, stdin);
sscanf(tmp, "%d", &block);
cout << "Enter key (6B HEX value, WITHOUT spaces): ";
string key = "";
getline(cin, key);
key_c = string_to_bytearray(key, &bbbb);
authenticate(key_c, (menu_option.compare("b") == 0), block);
continue;
}
if ((menu_option.compare("r")) == 0) {
char tmp[20];
uint8_t block;
cout << "Enter block number: ";
fgets(tmp, 20, stdin);
sscanf(tmp, "%d", &block);
readblock(block);
continue;
}
if ((menu_option.compare("w")) == 0) {
int length = 0;
char tmp[20];
uint8_t block;
string data = "";
cout << "Enter block number: ";
fgets(tmp, 20, stdin);
sscanf(tmp, "%d", &block);
cout << "Enter data (16B hex, WITHOUT spaces) to write: ";
getline(cin, data);
writeblock(block, string_to_bytearray(data, &length));
continue;
}
if ((menu_option.compare("s")) == 0) {
char tmp[20];
string data = "";
int length = 0;
uint8_t block;
cout << "Enter block number: ";
fgets(tmp, 20, stdin);
sscanf(tmp, "%d", &block);
cout << "Enter data (enter 4B HEX value, WITHOUT spaces): ";
getline(cin, data);
valueblock(MC_STORE, block, string_to_bytearray(data, &length));
continue;
}
if ((menu_option.compare("i")) == 0) {
char tmp[20];
byte* data = new byte[4];
string how_many = "";
int length = 0;
uint8_t block;
cout << "Enter block number: ";
fgets(tmp, 20, stdin);
sscanf(tmp, "%d", &block);
cout << "Increment by what number (enter 4B HEX value, WITHOUT spaces)? ";
getline(cin, how_many);
valueblock(MC_INCREMENT, block, string_to_bytearray(how_many, &length));
continue;
}
if ((menu_option.compare("d")) == 0) {
char tmp[20];
byte* data = new byte[4];
string how_many = "";
int length = 0;
uint8_t block;
cout << "Enter block number: ";
fgets(tmp, 20, stdin);
sscanf(tmp, "%d", &block);
cout << "Decrement by what number (enter 4B HEX value, WITHOUT spaces)? ";
getline(cin, how_many);
valueblock(MC_DECREMENT, block, string_to_bytearray(how_many, &length));
continue;
}
if ((menu_option.compare("t")) == 0) {
char tmp[20];
string data = "";
int length = 0;
uint8_t block;
cout << "Enter block number: ";
fgets(tmp, 20, stdin);
sscanf(tmp, "%d", &block);
cout << "Enter data (4B HEX value, WITHOUT spaces): ";
getline(cin, data);
valueblock(MC_TRANSFER, block, string_to_bytearray(data, &length));
continue;
}
}
cout << "Command " << menu_option << " not understood.\nRemember, to use additional commands like Read,Write,...\nyou have to be connected to the reader first." << endl;
press_to_continue();
}
}