1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 iceman
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Proxmark3 RDV40 Smartcard module commands
9 //-----------------------------------------------------------------------------
10 #include "cmdsmartcard.h"
13 #include "cmdparser.h" // command_t
14 #include "commonutil.h" // ARRAYLEN
15 #include "protocols.h"
17 #include "proxmark3.h"
18 #include "comms.h" // getfromdevice
19 #include "emv/emvcore.h" // decodeTVL
20 #include "crypto/libpcrypto.h" // sha512hash
23 #include "fileutils.h"
24 #include "crc16.h" // crc
25 #include "cliparser.h" // cliparsing
27 static int CmdHelp(const char *Cmd
);
29 static int smart_loadjson(const char *preferredName
, json_t
**root
) {
33 if (preferredName
== NULL
) return 1;
36 int res
= searchFile(&path
, RESOURCES_SUBDIR
, preferredName
, ".json", false);
37 if (res
!= PM3_SUCCESS
) {
41 int retval
= PM3_SUCCESS
;
42 *root
= json_load_file(path
, 0, &error
);
44 PrintAndLogEx(ERR
, "json (%s) error on line %d: %s", path
, error
.line
, error
.text
);
49 if (!json_is_array(*root
)) {
50 PrintAndLogEx(ERR
, "Invalid json (%s) format. root must be an array.", path
);
55 PrintAndLogEx(SUCCESS
, "Loaded file (%s) OK.", path
);
61 static uint8_t GetATRTA1(uint8_t *atr
, size_t atrlen
) {
68 return 0x11; // default value is 0x11, corresponding to fmax=5 MHz, Fi=372, Di=1.
79 64, // b0111. This was RFU in ISO/IEC 7816-3:1997 and former. Some card readers or drivers may erroneously reject cards using this value
91 372, // b0000 Historical note: in ISO/IEC 7816-3:1989, this was assigned to cards with internal clock
110 4, // b0000 Historical note: in ISO/IEC 7816-3:1989, this was assigned to cards with internal clock
128 static int GetATRDi(uint8_t *atr
, size_t atrlen
) {
129 uint8_t TA1
= GetATRTA1(atr
, atrlen
);
130 return DiArray
[TA1
& 0x0F]; // The 4 low-order bits of TA1 (4th MSbit to 1st LSbit) encode Di
133 static int GetATRFi(uint8_t *atr
, size_t atrlen
) {
134 uint8_t TA1
= GetATRTA1(atr
, atrlen
);
135 return FiArray
[TA1
>> 4]; // The 4 high-order bits of TA1 (8th MSbit to 5th LSbit) encode fmax and Fi
138 static float GetATRF(uint8_t *atr
, size_t atrlen
) {
139 uint8_t TA1
= GetATRTA1(atr
, atrlen
);
140 return FArray
[TA1
>> 4]; // The 4 high-order bits of TA1 (8th MSbit to 5th LSbit) encode fmax and Fi
143 static void PrintATR(uint8_t *atr
, size_t atrlen
) {
146 uint8_t K
= T0
& 0x0F;
147 uint8_t T1len
= 0, TD1len
= 0, TDilen
= 0;
148 bool protocol_T0_present
= true;
149 bool protocol_T15_present
= false;
152 PrintAndLogEx(INFO
, "\t- TA1 (Maximum clock frequency, proposed bit duration) [ 0x%02x ]", atr
[2 + T1len
]);
157 PrintAndLogEx(INFO
, "\t- TB1 (Deprecated: VPP requirements) [ 0x%02x ]", atr
[2 + T1len
]);
162 PrintAndLogEx(INFO
, "\t- TC1 (Extra delay between bytes required by card) [ 0x%02x ]", atr
[2 + T1len
]);
167 uint8_t TD1
= atr
[2 + T1len
];
168 PrintAndLogEx(INFO
, "\t- TD1 (First offered transmission protocol, presence of TA2..TD2) [ 0x%02x ] Protocol T%d", TD1
, TD1
& 0x0f);
169 protocol_T0_present
= false;
170 if ((TD1
& 0x0f) == 0) {
171 protocol_T0_present
= true;
173 if ((TD1
& 0x0f) == 15) {
174 protocol_T15_present
= true;
180 PrintAndLogEx(INFO
, "\t- TA2 (Specific protocol and parameters to be used after the ATR) [ 0x%02x ]", atr
[2 + T1len
+ TD1len
]);
184 PrintAndLogEx(INFO
, "\t- TB2 (Deprecated: VPP precise voltage requirement) [ 0x%02x ]", atr
[2 + T1len
+ TD1len
]);
188 PrintAndLogEx(INFO
, "\t- TC2 (Maximum waiting time for protocol T=0) [ 0x%02x ]", atr
[2 + T1len
+ TD1len
]);
192 uint8_t TDi
= atr
[2 + T1len
+ TD1len
];
193 PrintAndLogEx(INFO
, "\t- TD2 (A supported protocol or more global parameters, presence of TA3..TD3) [ 0x%02x ] Protocol T%d", TDi
, TDi
& 0x0f);
194 if ((TDi
& 0x0f) == 0) {
195 protocol_T0_present
= true;
197 if ((TDi
& 0x0f) == 15) {
198 protocol_T15_present
= true;
202 bool nextCycle
= true;
207 PrintAndLogEx(INFO
, "\t- TA%d: 0x%02x", vi
, atr
[2 + T1len
+ TD1len
+ TDilen
]);
211 PrintAndLogEx(INFO
, "\t- TB%d: 0x%02x", vi
, atr
[2 + T1len
+ TD1len
+ TDilen
]);
215 PrintAndLogEx(INFO
, "\t- TC%d: 0x%02x", vi
, atr
[2 + T1len
+ TD1len
+ TDilen
]);
219 TDi
= atr
[2 + T1len
+ TD1len
+ TDilen
];
220 PrintAndLogEx(INFO
, "\t- TD%d [ 0x%02x ] Protocol T%d", vi
, TDi
, TDi
& 0x0f);
230 if (!protocol_T0_present
|| protocol_T15_present
) { // there is CRC Check Byte TCK
232 for (int i
= 1; i
< atrlen
; i
++)
236 PrintAndLogEx(WARNING
, "Invalid check sum. Must be 0 got 0x%02X", vxor
);
238 PrintAndLogEx(INFO
, "Check sum OK.");
242 PrintAndLogEx(WARNING
, "Not a direct convention [ 0x%02x ]", atr
[0]);
244 uint8_t calen
= 2 + T1len
+ TD1len
+ TDilen
+ K
;
246 if (atrlen
!= calen
&& atrlen
!= calen
+ 1) // may be CRC
247 PrintAndLogEx(WARNING
, "Invalid ATR length. len: %zu, T1len: %d, TD1len: %d, TDilen: %d, K: %d", atrlen
, T1len
, TD1len
, TDilen
, K
);
250 PrintAndLogEx(INFO
, "Historical bytes | len %02d | format %02x", K
, atr
[2 + T1len
+ TD1len
+ TDilen
]);
253 PrintAndLogEx(INFO
, "\tHistorical bytes");
254 print_buffer(&atr
[2 + T1len
+ TD1len
+ TDilen
], K
, 1);
258 static int smart_wait(uint8_t *out
, int maxoutlen
, bool verbose
) {
262 clearCommandBuffer();
263 PacketResponseNG resp
;
264 if (WaitForResponseTimeout(CMD_SMART_RAW
, &resp
, 1000)) {
266 if (resp
.status
!= PM3_SUCCESS
) {
267 if (verbose
) PrintAndLogEx(WARNING
, "smart card response status failed");
273 if (verbose
) PrintAndLogEx(WARNING
, "smart card response failed");
277 if (len
> maxoutlen
) {
278 if (verbose
) PrintAndLogEx(ERR
, "Response too large. Got %u, expected %d", len
, maxoutlen
);
282 memcpy(out
, resp
.data
.asBytes
, len
);
287 if (out
[len
- 2] == 0x90 && out
[len
- 1] == 0x00) {
288 PrintAndLogEx(SUCCESS
, _GREEN_("%02X%02X") " | %s", out
[len
- 2], out
[len
- 1], GetAPDUCodeDescription(out
[len
- 2], out
[len
- 1]));
290 PrintAndLogEx(SUCCESS
, "%02X%02X | %s", out
[len
- 2], out
[len
- 1], GetAPDUCodeDescription(out
[len
- 2], out
[len
- 1]));
295 PrintAndLogEx(SUCCESS
, " %d | %s", len
, sprint_hex_inrow_ex(out
, len
, 8));
303 PrintAndLogEx(WARNING
, "smart card response timeout");
308 static int smart_responseEx(uint8_t *out
, int maxoutlen
, bool verbose
) {
310 int datalen
= smart_wait(out
, maxoutlen
, verbose
);
311 bool needGetData
= false;
317 if (out
[datalen
- 2] == 0x61 || out
[datalen
- 2] == 0x9F) {
322 int len
= out
[datalen
- 1];
324 if (verbose
) PrintAndLogEx(INFO
, "Requesting " _YELLOW_("0x%02X") " bytes response", len
);
326 uint8_t cmd_getresp
[] = {0x00, ISO7816_GET_RESPONSE
, 0x00, 0x00, len
};
327 smart_card_raw_t
*payload
= calloc(1, sizeof(smart_card_raw_t
) + sizeof(cmd_getresp
));
328 payload
->flags
= SC_RAW
| SC_LOG
;
329 payload
->len
= sizeof(cmd_getresp
);
330 memcpy(payload
->data
, cmd_getresp
, sizeof(cmd_getresp
));
332 clearCommandBuffer();
333 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + sizeof(cmd_getresp
));
336 datalen
= smart_wait(out
, maxoutlen
, verbose
);
343 if (datalen
!= len
+ 2) {
345 if (datalen
== len
+ 2 + 1) { // 2 - response, 1 - ACK
346 if (out
[0] != ISO7816_GET_RESPONSE
) {
348 PrintAndLogEx(ERR
, "GetResponse ACK error. len 0x%x | data[0] %02X", len
, out
[0]);
355 memmove(out
, &out
[1], datalen
);
359 PrintAndLogEx(WARNING
, "GetResponse wrong length. Must be 0x%02X got 0x%02X", len
, datalen
- 3);
369 static int smart_response(uint8_t *out
, int maxoutlen
) {
370 return smart_responseEx(out
, maxoutlen
, true);
373 static int CmdSmartRaw(const char *Cmd
) {
374 CLIParserContext
*ctx
;
375 CLIParserInit(&ctx
, "smart raw",
376 "Sends raw bytes to card",
377 "smart raw -s -0 -d 00a404000e315041592e5359532e4444463031 -> `1PAY.SYS.DDF01` PPSE directory with get ATR\n"
378 "smart raw -0 -d 00a404000e325041592e5359532e4444463031 -> `2PAY.SYS.DDF01` PPSE directory\n"
379 "smart raw -0 -t -d 00a4040007a0000000041010 -> Mastercard\n"
380 "smart raw -0 -t -d 00a4040007a0000000031010 -> Visa"
385 arg_lit0("r", NULL
, "do not read response"),
386 arg_lit0("a", NULL
, "active smartcard without select (reset sc module)"),
387 arg_lit0("s", NULL
, "active smartcard with select (get ATR)"),
388 arg_lit0("t", "tlv", "executes TLV decoder if it possible"),
389 arg_lit0("0", NULL
, "use protocol T=0"),
390 arg_str1("d", "data", "<hex>", "bytes to send"),
393 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
395 bool reply
= (arg_get_lit(ctx
, 1) == false);
396 bool active
= arg_get_lit(ctx
, 2);
397 bool active_select
= arg_get_lit(ctx
, 3);
398 bool decode_tlv
= arg_get_lit(ctx
, 4);
399 bool use_t0
= arg_get_lit(ctx
, 5);
402 uint8_t data
[PM3_CMD_DATA_SIZE
] = {0x00};
403 int res
= CLIParamHexToBuf(arg_get_str(ctx
, 6), data
, sizeof(data
), &dlen
);
407 PrintAndLogEx(FAILED
, "Error parsing bytes");
411 smart_card_raw_t
*payload
= calloc(1, sizeof(smart_card_raw_t
) + dlen
);
412 if (payload
== NULL
) {
413 PrintAndLogEx(FAILED
, "failed to allocate memory");
417 memcpy(payload
->data
, data
, dlen
);
419 payload
->flags
= SC_LOG
;
420 if (active
|| active_select
) {
422 payload
->flags
|= (SC_CONNECT
| SC_CLEARLOG
);
424 payload
->flags
|= SC_SELECT
;
429 payload
->flags
|= SC_RAW_T0
;
431 payload
->flags
|= SC_RAW
;
434 uint8_t *buf
= calloc(PM3_CMD_DATA_SIZE
, sizeof(uint8_t));
436 PrintAndLogEx(DEBUG
, "failed to allocate memory");
441 clearCommandBuffer();
442 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + dlen
);
444 if (reply
== false) {
448 // reading response from smart card
449 int len
= smart_response(buf
, PM3_CMD_DATA_SIZE
);
456 if (buf
[0] == 0x6C) {
458 // request more bytes to download
460 memcpy(payload
->data
, data
, dlen
);
461 clearCommandBuffer();
462 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + dlen
);
464 len
= smart_response(buf
, PM3_CMD_DATA_SIZE
);
469 if (decode_tlv
&& len
> 4)
470 TLVPrintFromBuffer(buf
, len
- 2);
473 PrintAndLogEx(INFO
, "Response data:");
474 PrintAndLogEx(INFO
, " # | bytes | ascii");
475 PrintAndLogEx(INFO
, "---+-------------------------------------------------+-----------------");
476 print_hex_break(buf
, len
, 16);
479 PrintAndLogEx(NORMAL
, "");
486 static int CmdSmartUpgrade(const char *Cmd
) {
487 PrintAndLogEx(INFO
, "-------------------------------------------------------------------");
488 PrintAndLogEx(WARNING
, _RED_("WARNING") " - sim module firmware upgrade");
489 PrintAndLogEx(WARNING
, _RED_("A dangerous command, do wrong and you could brick the sim module"));
490 PrintAndLogEx(INFO
, "-------------------------------------------------------------------");
491 PrintAndLogEx(NORMAL
, "");
493 CLIParserContext
*ctx
;
494 CLIParserInit(&ctx
, "smart upgrade",
495 "Upgrade RDV4.0 sim module firmware",
496 "smart upgrade -f sim011.bin"
501 arg_str1("f", "file", "<filename>", "firmware file name"),
504 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
506 char filename
[FILE_PATH_SIZE
] = {0};
507 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)filename
, sizeof(filename
), &fnlen
);
510 char *bin_extension
= filename
;
511 char *dot_position
= NULL
;
512 while ((dot_position
= strchr(bin_extension
, '.')) != NULL
) {
513 bin_extension
= dot_position
+ 1;
516 // generate filename for the related SHA512 hash file
517 char sha512filename
[FILE_PATH_SIZE
] = {'\0'};
518 if (!strcmp(bin_extension
, "BIN") || !strcmp(bin_extension
, "bin")) {
519 memcpy(sha512filename
, filename
, strlen(filename
) - strlen("bin"));
520 strcat(sha512filename
, "sha512.txt");
522 PrintAndLogEx(FAILED
, "Filename extension of firmware upgrade file must be .BIN");
526 PrintAndLogEx(INFO
, "firmware file " _YELLOW_("%s"), filename
);
527 PrintAndLogEx(INFO
, "Checking integrity " _YELLOW_("%s"), sha512filename
);
529 // load firmware file
530 size_t firmware_size
= 0;
531 uint8_t *firmware
= NULL
;
532 if (loadFile_safe(filename
, "", (void **)&firmware
, &firmware_size
) != PM3_SUCCESS
) {
533 PrintAndLogEx(FAILED
, "Firmware file " _YELLOW_("%s") " not found or locked.", filename
);
538 size_t sha512_size
= 0;
539 char *hashstring
= NULL
;
540 if (loadFile_safe(sha512filename
, "", (void **)&hashstring
, &sha512_size
) != PM3_SUCCESS
) {
541 PrintAndLogEx(FAILED
, "SHA-512 file not found or locked.");
546 if (sha512_size
< 128) {
547 PrintAndLogEx(FAILED
, "SHA-512 file wrong size");
552 hashstring
[128] = '\0';
555 if (param_gethex(hashstring
, 0, hash_1
, 128)) {
556 PrintAndLogEx(FAILED
, "Couldn't read SHA-512 file");
563 if (sha512hash(firmware
, firmware_size
, hash_2
)) {
564 PrintAndLogEx(FAILED
, "Couldn't calculate SHA-512 of firmware");
570 if (memcmp(hash_1
, hash_2
, 64)) {
571 PrintAndLogEx(FAILED
, "Couldn't verify integrity of firmware file " _RED_("(wrong SHA-512 hash)"));
578 PrintAndLogEx(INFO
, _GREEN_("Don\'t turn off your PM3!"));
579 PrintAndLogEx(SUCCESS
, "Sim module firmware uploading to PM3...");
581 PacketResponseNG resp
;
585 uint32_t bytes_sent
= 0;
586 uint32_t bytes_remaining
= firmware_size
;
588 while (bytes_remaining
> 0) {
592 uint32_t bytes_in_packet
;
597 uint32_t bytes_in_packet
= MIN(sizeof(upload
.data
), bytes_remaining
);
599 upload
.idx
= index
+ bytes_sent
;
600 upload
.bytes_in_packet
= bytes_in_packet
;
601 memcpy(upload
.data
, firmware
+ bytes_sent
, bytes_in_packet
);
603 uint8_t a
= 0, b
= 0;
604 compute_crc(CRC_14443_A
, upload
.data
, bytes_in_packet
, &a
, &b
);
605 upload
.crc
= (a
<< 8 | b
);
607 clearCommandBuffer();
608 SendCommandNG(CMD_SMART_UPLOAD
, (uint8_t *)&upload
, sizeof(upload
));
609 if (!WaitForResponseTimeout(CMD_SMART_UPLOAD
, &resp
, 2000)) {
610 PrintAndLogEx(WARNING
, "timeout while waiting for reply.");
615 if (resp
.status
!= PM3_SUCCESS
) {
616 PrintAndLogEx(WARNING
, "uploading to device failed");
620 bytes_remaining
-= bytes_in_packet
;
621 bytes_sent
+= bytes_in_packet
;
622 PrintAndLogEx(INPLACE
, "%d bytes sent", bytes_sent
);
624 PrintAndLogEx(NORMAL
, "");
625 PrintAndLogEx(SUCCESS
, "Sim module firmware updating...");
627 // trigger the firmware upgrade
628 clearCommandBuffer();
633 payload
.fw_size
= firmware_size
;
635 uint8_t a
= 0, b
= 0;
636 compute_crc(CRC_14443_A
, firmware
, firmware_size
, &a
, &b
);
637 payload
.crc
= (a
<< 8 | b
);
640 SendCommandNG(CMD_SMART_UPGRADE
, (uint8_t *)&payload
, sizeof(payload
));
641 if (!WaitForResponseTimeout(CMD_SMART_UPGRADE
, &resp
, 2500)) {
642 PrintAndLogEx(WARNING
, "timeout while waiting for reply.");
646 if (resp
.status
== PM3_SUCCESS
) {
647 PrintAndLogEx(SUCCESS
, "Sim module firmware upgrade " _GREEN_("successful"));
648 PrintAndLogEx(HINT
, "run " _YELLOW_("`hw status`") " to validate the fw version ");
650 PrintAndLogEx(FAILED
, "Sim module firmware upgrade " _RED_("failed"));
655 static int CmdSmartInfo(const char *Cmd
) {
656 CLIParserContext
*ctx
;
657 CLIParserInit(&ctx
, "smart info",
658 "Extract more detailed information from smart card.",
664 arg_lit0("v", "verbose", "verbose"),
667 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
668 bool verbose
= arg_get_lit(ctx
, 1);
671 clearCommandBuffer();
672 SendCommandNG(CMD_SMART_ATR
, NULL
, 0);
673 PacketResponseNG resp
;
674 if (WaitForResponseTimeout(CMD_SMART_ATR
, &resp
, 2500) == false) {
676 PrintAndLogEx(WARNING
, "smart card timeout");
681 if (resp
.status
!= PM3_SUCCESS
) {
683 PrintAndLogEx(WARNING
, "smart card select failed");
688 smart_card_atr_t card
;
689 memcpy(&card
, (smart_card_atr_t
*)resp
.data
.asBytes
, sizeof(smart_card_atr_t
));
692 PrintAndLogEx(INFO
, "--- " _CYAN_("Smartcard Information") " ---------");
693 PrintAndLogEx(INFO
, "ISO7618-3 ATR : %s", sprint_hex(card
.atr
, card
.atr_len
));
694 PrintAndLogEx(INFO
, "http://smartcard-atr.apdu.fr/parse?ATR=%s", sprint_hex_inrow(card
.atr
, card
.atr_len
));
697 PrintAndLogEx(INFO
, "ATR");
698 PrintATR(card
.atr
, card
.atr_len
);
700 // print D/F (brom byte TA1 or defaults)
701 PrintAndLogEx(NORMAL
, "");
702 PrintAndLogEx(INFO
, "D/F (TA1)");
703 int Di
= GetATRDi(card
.atr
, card
.atr_len
);
704 int Fi
= GetATRFi(card
.atr
, card
.atr_len
);
705 float F
= GetATRF(card
.atr
, card
.atr_len
);
706 if (GetATRTA1(card
.atr
, card
.atr_len
) == 0x11)
707 PrintAndLogEx(INFO
, "Using default values...");
709 PrintAndLogEx(INFO
, "\t- Di %d", Di
);
710 PrintAndLogEx(INFO
, "\t- Fi %d", Fi
);
711 PrintAndLogEx(INFO
, "\t- F %.1f MHz", F
);
714 PrintAndLogEx(INFO
, "\t- Cycles/ETU %d", Fi
/ Di
);
715 PrintAndLogEx(INFO
, "\t- %.1f bits/sec at 4 MHz", (float)4000000 / (Fi
/ Di
));
716 PrintAndLogEx(INFO
, "\t- %.1f bits/sec at Fmax (%.1fMHz)", (F
* 1000000) / (Fi
/ Di
), F
);
718 PrintAndLogEx(WARNING
, "\t- Di or Fi is RFU.");
724 static int CmdSmartReader(const char *Cmd
) {
725 CLIParserContext
*ctx
;
726 CLIParserInit(&ctx
, "smart reader",
727 "Act as a smart card reader.",
733 arg_lit0("v", "verbose", "verbose"),
736 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
737 bool verbose
= arg_get_lit(ctx
, 1);
740 clearCommandBuffer();
741 SendCommandNG(CMD_SMART_ATR
, NULL
, 0);
742 PacketResponseNG resp
;
743 if (WaitForResponseTimeout(CMD_SMART_ATR
, &resp
, 2500) == false) {
745 PrintAndLogEx(WARNING
, "smart card select failed");
750 if (resp
.status
!= PM3_SUCCESS
) {
752 PrintAndLogEx(WARNING
, "smart card select failed");
756 smart_card_atr_t
*card
= (smart_card_atr_t
*)resp
.data
.asBytes
;
757 PrintAndLogEx(INFO
, "ISO7816-3 ATR : %s", sprint_hex(card
->atr
, card
->atr_len
));
761 static int CmdSmartSetClock(const char *Cmd
) {
763 CLIParserContext
*ctx
;
764 CLIParserInit(&ctx
, "smart setclock",
765 "Set clock speed for smart card interface.",
766 "smart setclock --4mhz\n"
767 "smart setclock --16mhz"
772 arg_lit0(NULL
, "16mhz", "16 MHz clock speed"),
773 arg_lit0(NULL
, "8mhz", "8 MHz clock speed"),
774 arg_lit0(NULL
, "4mhz", "4 MHz clock speed"),
777 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
778 bool c16
= arg_get_lit(ctx
, 1);
779 bool c8
= arg_get_lit(ctx
, 2);
780 bool c4
= arg_get_lit(ctx
, 3);
783 if ((c16
+ c8
+ c4
) > 1) {
784 PrintAndLogEx(WARNING
, "Only one clock speed can be used at a time");
799 clearCommandBuffer();
800 SendCommandNG(CMD_SMART_SETCLOCK
, (uint8_t *)&payload
, sizeof(payload
));
801 PacketResponseNG resp
;
802 if (!WaitForResponseTimeout(CMD_SMART_SETCLOCK
, &resp
, 2500)) {
803 PrintAndLogEx(WARNING
, "smart card select failed");
807 if (resp
.status
!= PM3_SUCCESS
) {
808 PrintAndLogEx(WARNING
, "smart card set clock failed");
812 switch (payload
.new_clk
) {
814 PrintAndLogEx(SUCCESS
, "Clock changed to " _GREEN_("16") " MHz giving " _GREEN_("10800") " baudrate");
817 PrintAndLogEx(SUCCESS
, "Clock changed to " _GREEN_("8") " MHz giving " _GREEN_("21600") " baudrate");
820 PrintAndLogEx(SUCCESS
, "Clock changed to " _GREEN_("4") " MHz giving " _GREEN_("86400") " baudrate");
828 static int CmdSmartList(const char *Cmd
) {
829 return CmdTraceListAlias(Cmd
, "smart", "7816");
832 static void smart_brute_prim(void) {
834 uint8_t *buf
= calloc(PM3_CMD_DATA_SIZE
, sizeof(uint8_t));
838 uint8_t get_card_data
[] = {
839 0x80, 0xCA, 0x9F, 0x13, 0x00,
840 0x80, 0xCA, 0x9F, 0x17, 0x00,
841 0x80, 0xCA, 0x9F, 0x36, 0x00,
842 0x80, 0xCA, 0x9F, 0x4f, 0x00
845 PrintAndLogEx(INFO
, "Reading primitives");
847 for (int i
= 0; i
< ARRAYLEN(get_card_data
); i
+= 5) {
849 smart_card_raw_t
*payload
= calloc(1, sizeof(smart_card_raw_t
) + 5);
850 payload
->flags
= SC_RAW_T0
;
852 memcpy(payload
->data
, get_card_data
+ i
, 5);
854 clearCommandBuffer();
855 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + 5);
858 int len
= smart_responseEx(buf
, PM3_CMD_DATA_SIZE
, false);
860 PrintAndLogEx(SUCCESS
, "\tHEX %d |: %s", len
, sprint_hex(buf
, len
));
866 static int smart_brute_sfi(bool decodeTLV
) {
868 uint8_t *buf
= calloc(PM3_CMD_DATA_SIZE
, sizeof(uint8_t));
874 uint8_t READ_RECORD
[] = {0x00, 0xB2, 0x00, 0x00, 0x00};
875 PrintAndLogEx(INFO
, "Start SFI brute forcing");
877 for (uint8_t sfi
= 1; sfi
<= 31; sfi
++) {
879 PrintAndLogEx(NORMAL
, "." NOLF
);
881 for (uint16_t rec
= 1; rec
<= 255; rec
++) {
883 if (kbd_enter_pressed()) {
884 PrintAndLogEx(WARNING
, "\naborted via keyboard!\n");
889 READ_RECORD
[2] = rec
;
890 READ_RECORD
[3] = (sfi
<< 3) | 4;
892 smart_card_raw_t
*payload
= calloc(1, sizeof(smart_card_raw_t
) + sizeof(READ_RECORD
));
893 payload
->flags
= SC_RAW_T0
;
894 payload
->len
= sizeof(READ_RECORD
);
895 memcpy(payload
->data
, READ_RECORD
, sizeof(READ_RECORD
));
897 clearCommandBuffer();
898 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + sizeof(READ_RECORD
));
900 len
= smart_responseEx(buf
, PM3_CMD_DATA_SIZE
, false);
902 if (buf
[0] == 0x6C) {
903 READ_RECORD
[4] = buf
[1];
905 memcpy(payload
->data
, READ_RECORD
, sizeof(READ_RECORD
));
906 clearCommandBuffer();
907 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + sizeof(READ_RECORD
));
908 len
= smart_responseEx(buf
, PM3_CMD_DATA_SIZE
, false);
917 PrintAndLogEx(SUCCESS
, "\n\t file %02d, record %02d found", sfi
, rec
);
919 uint8_t modifier
= (buf
[0] == 0xC0) ? 1 : 0;
922 if (!TLVPrintFromBuffer(buf
+ modifier
, len
- 2 - modifier
)) {
923 PrintAndLogEx(SUCCESS
, "\tHEX: %s", sprint_hex(buf
, len
));
927 memset(buf
, 0x00, PM3_CMD_DATA_SIZE
);
934 static void smart_brute_options(bool decodeTLV
) {
936 uint8_t *buf
= calloc(PM3_CMD_DATA_SIZE
, sizeof(uint8_t));
940 // Get processing options command
941 uint8_t GET_PROCESSING_OPTIONS
[] = {0x80, 0xA8, 0x00, 0x00, 0x02, 0x83, 0x00, 0x00};
943 smart_card_raw_t
*payload
= calloc(1, sizeof(smart_card_raw_t
) + sizeof(GET_PROCESSING_OPTIONS
));
944 payload
->flags
= SC_RAW_T0
;
945 payload
->len
= sizeof(GET_PROCESSING_OPTIONS
);
946 memcpy(payload
->data
, GET_PROCESSING_OPTIONS
, sizeof(GET_PROCESSING_OPTIONS
));
948 clearCommandBuffer();
949 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + sizeof(GET_PROCESSING_OPTIONS
));
952 int len
= smart_responseEx(buf
, PM3_CMD_DATA_SIZE
, false);
954 PrintAndLogEx(SUCCESS
, "Got processing options");
956 TLVPrintFromBuffer(buf
, len
- 2);
959 PrintAndLogEx(FAILED
, "Getting processing options failed");
965 static int CmdSmartBruteforceSFI(const char *Cmd
) {
966 CLIParserContext
*ctx
;
967 CLIParserInit(&ctx
, "smart brute",
968 "Tries to bruteforce SFI, using a known list of AID's",
974 arg_lit0("t", "tlv", "executes TLV decoder if it possible"),
975 // arg_lit0("0", NULL, "use protocol T=0"),
978 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
979 bool decode_tlv
= arg_get_lit(ctx
, 1);
980 // bool use_t0 = arg_get_lit(ctx, 2);
983 const char *SELECT
= "00a40400%02zu%s";
985 // uint8_t GENERATE_AC[] = {0x80, 0xAE};
986 // uint8_t GET_CHALLENGE[] = {0x00, 0x84, 0x00};
987 // uint8_t GET_DATA[] = {0x80, 0xCA, 0x00, 0x00, 0x00};
988 // uint8_t SELECT[] = {0x00, 0xA4, 0x04, 0x00};
989 // uint8_t UNBLOCK_PIN[] = {0x84, 0x24, 0x00, 0x00, 0x00};
990 // uint8_t VERIFY[] = {0x00, 0x20, 0x00, 0x80};
992 PrintAndLogEx(INFO
, "Importing AID list");
994 smart_loadjson("aidlist", &root
);
996 uint8_t *buf
= calloc(PM3_CMD_DATA_SIZE
, sizeof(uint8_t));
1000 PrintAndLogEx(INFO
, "Selecting card");
1001 if (!smart_select(false, NULL
)) {
1008 for (int i
= 0; i
< json_array_size(root
); i
++) {
1010 PrintAndLogEx(NORMAL
, "+" NOLF
);
1015 json_t
*data
, *jaid
;
1017 data
= json_array_get(root
, i
);
1018 if (json_is_object(data
) == false) {
1019 PrintAndLogEx(ERR
, "\ndata %d is not an object\n", i
+ 1);
1024 jaid
= json_object_get(data
, "AID");
1025 if (json_is_string(jaid
) == false) {
1026 PrintAndLogEx(ERR
, "\nAID data [%d] is not a string", i
+ 1);
1031 const char *aid
= json_string_value(jaid
);
1035 size_t aidlen
= strlen(aid
);
1036 caid
= calloc(8 + 2 + aidlen
+ 1, sizeof(uint8_t));
1037 snprintf(caid
, 8 + 2 + aidlen
+ 1, SELECT
, aidlen
>> 1, aid
);
1040 uint8_t cmddata
[PM3_CMD_DATA_SIZE
];
1041 int res
= param_gethex_to_eol(caid
, 0, cmddata
, sizeof(cmddata
), &hexlen
);
1045 smart_card_raw_t
*payload
= calloc(1, sizeof(smart_card_raw_t
) + hexlen
);
1046 payload
->flags
= SC_RAW_T0
;
1047 payload
->len
= hexlen
;
1049 memcpy(payload
->data
, cmddata
, hexlen
);
1050 clearCommandBuffer();
1051 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + hexlen
);
1054 int len
= smart_responseEx(buf
, PM3_CMD_DATA_SIZE
, false);
1058 json_t
*jvendor
, *jname
;
1059 jvendor
= json_object_get(data
, "Vendor");
1060 if (json_is_string(jvendor
) == false) {
1061 PrintAndLogEx(ERR
, "Vendor data [%d] is not a string", i
+ 1);
1065 const char *vendor
= json_string_value(jvendor
);
1069 jname
= json_object_get(data
, "Name");
1070 if (json_is_string(jname
) == false) {
1071 PrintAndLogEx(ERR
, "Name data [%d] is not a string", i
+ 1);
1074 const char *name
= json_string_value(jname
);
1078 PrintAndLogEx(SUCCESS
, "\nAID %s | %s | %s", aid
, vendor
, name
);
1080 smart_brute_options(decode_tlv
);
1084 smart_brute_sfi(decode_tlv
);
1086 PrintAndLogEx(SUCCESS
, "\nSFI brute force done\n");
1095 PrintAndLogEx(SUCCESS
, "\nSearch completed.");
1099 static command_t CommandTable
[] = {
1100 {"help", CmdHelp
, AlwaysAvailable
, "This help"},
1101 {"list", CmdSmartList
, AlwaysAvailable
, "List ISO 7816 history"},
1102 {"info", CmdSmartInfo
, IfPm3Smartcard
, "Tag information"},
1103 {"reader", CmdSmartReader
, IfPm3Smartcard
, "Act like an IS07816 reader"},
1104 {"raw", CmdSmartRaw
, IfPm3Smartcard
, "Send raw hex data to tag"},
1105 {"upgrade", CmdSmartUpgrade
, AlwaysAvailable
, "Upgrade sim module firmware"},
1106 {"setclock", CmdSmartSetClock
, IfPm3Smartcard
, "Set clock speed"},
1107 {"brute", CmdSmartBruteforceSFI
, IfPm3Smartcard
, "Bruteforce SFI"},
1108 {NULL
, NULL
, NULL
, NULL
}
1111 static int CmdHelp(const char *Cmd
) {
1112 (void)Cmd
; // Cmd is not used so far
1113 CmdsHelp(CommandTable
);
1117 int CmdSmartcard(const char *Cmd
) {
1118 clearCommandBuffer();
1119 return CmdsParse(CommandTable
, Cmd
);
1122 int ExchangeAPDUSC(bool verbose
, uint8_t *datain
, int datainlen
, bool activateCard
, bool leaveSignalON
, uint8_t *dataout
, int maxdataoutlen
, int *dataoutlen
) {
1126 smart_card_raw_t
*payload
= calloc(1, sizeof(smart_card_raw_t
) + datainlen
);
1127 payload
->flags
= (SC_RAW_T0
| SC_LOG
);
1129 payload
->flags
|= (SC_SELECT
| SC_CONNECT
);
1131 payload
->len
= datainlen
;
1132 memcpy(payload
->data
, datain
, datainlen
);
1134 clearCommandBuffer();
1135 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + datainlen
);
1137 int len
= smart_responseEx(dataout
, maxdataoutlen
, verbose
);
1144 if (len
> 1 && dataout
[len
- 2] == 0x6c && datainlen
> 4) {
1146 payload
->flags
= SC_RAW_T0
;
1148 // transfer length via T=0
1149 datain
[4] = dataout
[len
- 1];
1150 memcpy(payload
->data
, datain
, 5);
1151 clearCommandBuffer();
1152 SendCommandNG(CMD_SMART_RAW
, (uint8_t *)payload
, sizeof(smart_card_raw_t
) + 5);
1154 len
= smart_responseEx(dataout
, maxdataoutlen
, verbose
);
1162 bool smart_select(bool verbose
, smart_card_atr_t
*atr
) {
1164 memset(atr
, 0, sizeof(smart_card_atr_t
));
1166 clearCommandBuffer();
1167 SendCommandNG(CMD_SMART_ATR
, NULL
, 0);
1168 PacketResponseNG resp
;
1169 if (WaitForResponseTimeout(CMD_SMART_ATR
, &resp
, 2500) == false) {
1170 if (verbose
) PrintAndLogEx(WARNING
, "smart card select timeouted");
1174 if (resp
.status
!= PM3_SUCCESS
) {
1175 if (verbose
) PrintAndLogEx(WARNING
, "smart card select failed");
1179 smart_card_atr_t card
;
1180 memcpy(&card
, (smart_card_atr_t
*)resp
.data
.asBytes
, sizeof(smart_card_atr_t
));
1183 memcpy(atr
, &card
, sizeof(smart_card_atr_t
));
1186 PrintAndLogEx(INFO
, "ISO7816-3 ATR : %s", sprint_hex(card
.atr
, card
.atr_len
));