1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // High frequency ISO14443B commands
17 //-----------------------------------------------------------------------------
22 #include "fileutils.h"
23 #include "cmdparser.h" // command_t
24 #include "commonutil.h" // ARRAYLEN
25 #include "comms.h" // clearCommandBuffer
26 #include "emv/emvcore.h" // TLVPrintFromBuffer
28 #include "cliparser.h"
31 #include "protocols.h" // definitions of ISO14B/7816 protocol
32 #include "iso7816/apduinfo.h" // GetAPDUCodeDescription
33 #include "nfc/ndef.h" // NDEFRecordsDecodeAndPrint
34 #include "aidsearch.h"
35 #include "fileutils.h" // saveFile
36 #include "iclass_cmd.h" // picopass defines
37 #include "cmdhf.h" // handle HF plot
39 #define MAX_14B_TIMEOUT_MS (4949U)
41 // client side time out, waiting for device to ask tag.
44 // client side time out, waiting for device to ask tag a APDU to answer
45 #define APDU_TIMEOUT 2000
48 #define ST25TB_SR_BLOCK_SIZE 4
55 #define ST25_SIZE_512 3
56 #define ST25_SIZE_2K 4
57 #define ST25_SIZE_4K 5
63 const uint8_t apdulen
;
64 } transport_14b_apdu_t
;
67 // iso14b apdu input frame length
68 static uint16_t apdu_frame_length
= 0;
69 //static uint16_t ats_fsc[] = {16, 24, 32, 40, 48, 64, 96, 128, 256};
70 static bool apdu_in_framing_enable
= true;
72 static int CmdHelp(const char *Cmd
);
74 static int switch_off_field_14b(void) {
75 SetISODEPState(ISODEP_INACTIVE
);
76 iso14b_raw_cmd_t packet
= {
77 .flags
= ISO14B_DISCONNECT
,
82 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
86 static int clear_trace_14b(void) {
87 iso14b_raw_cmd_t packet
= {
88 .flags
= ISO14B_CLEARTRACE
,
93 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
97 static void hf14b_aid_search(bool verbose
) {
99 json_t
*root
= AIDSearchInit(verbose
);
101 switch_off_field_14b();
105 PrintAndLogEx(INFO
, "-------------------- " _CYAN_("AID Search") " --------------------");
108 bool leave_signal_on
= true;
109 bool activate_field
= true;
110 for (size_t elmindx
= 0; elmindx
< json_array_size(root
); elmindx
++) {
112 if (kbd_enter_pressed()) {
116 json_t
*data
= AIDSearchGetElm(root
, elmindx
);
117 uint8_t vaid
[200] = {0};
120 if ((AIDGetFromElm(data
, vaid
, sizeof(vaid
), &vaidlen
) == false) || (vaidlen
== 0)) {
126 uint8_t apdu_data
[PM3_CMD_DATA_SIZE
] = {0};
128 sAPDU_t apdu
= (sAPDU_t
) {0x00, 0xa4, 0x04, 0x00, vaidlen
, vaid
};
130 if (APDUEncodeS(&apdu
, false, 0x00, apdu_data
, &apdu_len
)) {
131 PrintAndLogEx(ERR
, "APDU encoding error");
135 PrintAndLogEx(DEBUG
, ">>>> %s", sprint_hex(apdu_data
, apdu_len
));
138 uint8_t result
[1024] = {0};
139 int res
= exchange_14b_apdu(apdu_data
, apdu_len
, activate_field
, leave_signal_on
, result
, sizeof(result
), &resultlen
, -1);
140 activate_field
= false;
145 uint16_t sw
= get_sw(result
, resultlen
);
147 uint8_t dfname
[200] = {0};
148 size_t dfnamelen
= 0;
150 struct tlvdb
*tlv
= tlvdb_parse_multi(result
, resultlen
);
152 // 0x84 Dedicated File (DF) Name
153 const struct tlv
*dfnametlv
= tlvdb_get_tlv(tlvdb_find_full(tlv
, 0x84));
155 dfnamelen
= dfnametlv
->len
;
156 memcpy(dfname
, dfnametlv
->value
, dfnamelen
);
162 if (sw
== ISO7816_OK
|| sw
== ISO7816_INVALID_DF
|| sw
== ISO7816_FILE_TERMINATED
) {
163 if (sw
== ISO7816_OK
) {
165 PrintAndLogEx(SUCCESS
, "Application ( " _GREEN_("ok") " )");
169 PrintAndLogEx(WARNING
, "Application ( " _RED_("blocked") " )");
173 PrintAIDDescriptionBuf(root
, vaid
, vaidlen
, verbose
);
176 if (dfnamelen
== vaidlen
) {
177 if (memcmp(dfname
, vaid
, vaidlen
) == 0) {
179 PrintAndLogEx(INFO
, "(DF) Name found and equal to AID");
182 PrintAndLogEx(INFO
, "(DF) Name not equal to AID: %s :", sprint_hex(dfname
, dfnamelen
));
183 PrintAIDDescriptionBuf(root
, dfname
, dfnamelen
, verbose
);
186 PrintAndLogEx(INFO
, "(DF) Name not equal to AID: %s :", sprint_hex(dfname
, dfnamelen
));
187 PrintAIDDescriptionBuf(root
, dfname
, dfnamelen
, verbose
);
191 PrintAndLogEx(INFO
, "(DF) Name not found");
196 PrintAndLogEx(SUCCESS
, "----------------------------------------------------");
201 switch_off_field_14b();
202 if (verbose
== false && found
) {
203 PrintAndLogEx(INFO
, "----------------------------------------------------");
207 static bool wait_14b_response(bool only_first
, uint8_t *datalen
, uint8_t *data
) {
209 /* We have scenarios.
211 B - only normal respose
212 C - both select and response
215 PacketResponseNG resp
;
216 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
217 PrintAndLogEx(WARNING
, "timeout while waiting for reply (first)");
221 if (resp
.status
== PM3_ETEAROFF
) {
222 PrintAndLogEx(INFO
, "Writing tear off triggered");
226 if (resp
.status
!= PM3_SUCCESS
) {
227 PrintAndLogEx(DEBUG
, "first response failed... %d", resp
.status
);
231 // treat first reponse as same.
235 *datalen
= resp
.length
;
239 memcpy(data
, resp
.data
.asBytes
, resp
.length
);
244 // wait a second time.
245 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
246 PrintAndLogEx(WARNING
, "timeout while waiting for reply");
250 if (resp
.status
!= PM3_SUCCESS
) {
251 PrintAndLogEx(DEBUG
, "second response failed... %d", resp
.status
);
256 *datalen
= resp
.length
;
259 memcpy(data
, resp
.data
.asBytes
, resp
.length
);
265 static bool wait_cmd_14b(bool verbose
, bool is_select
, uint32_t timeout
) {
267 PacketResponseNG resp
;
268 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, MAX(TIMEOUT
, timeout
)) == false) {
269 PrintAndLogEx(WARNING
, "timeout while waiting for reply");
273 if (resp
.status
== PM3_ETEAROFF
) {
274 PrintAndLogEx(INFO
, "Writing tear off triggered");
279 if (resp
.status
== PM3_ECARDEXCHANGE
) {
280 PrintAndLogEx(INFO
, "no response from tag");
283 if (resp
.status
!= PM3_SUCCESS
) {
285 PrintAndLogEx(INFO
, "failed status value... %d", resp
.status
);
291 uint16_t len
= resp
.length
;
292 uint8_t *data
= resp
.data
.asBytes
;
294 // handle select responses OK
295 if (is_select
&& verbose
) {
296 PrintAndLogEx(SUCCESS
, "received " _YELLOW_("%u") " bytes", len
);
297 PrintAndLogEx(SUCCESS
, "%s", sprint_hex(data
, len
));
301 // handle raw bytes responses
304 bool crc
= check_crc(CRC_14443_B
, data
, len
);
306 PrintAndLogEx(SUCCESS
, "received " _YELLOW_("%u") " bytes", len
);
307 PrintAndLogEx(SUCCESS
, "%s[ " _YELLOW_("%02X %02X") " ] ( %s )",
308 sprint_hex(data
, len
- 2),
311 (crc
) ? _GREEN_("ok") : _RED_("fail")
313 } else if (len
== 0) {
314 PrintAndLogEx(INFO
, "no response from tag");
316 PrintAndLogEx(SUCCESS
, "%s", sprint_hex(data
, len
));
322 static bool get_14b_UID(uint8_t *d
, iso14b_type_t
*found_type
) {
325 if (d
== NULL
|| found_type
== NULL
) {
329 *found_type
= ISO14B_NONE
;
331 iso14b_raw_cmd_t packet
= {
332 .flags
= (ISO14B_CONNECT
| ISO14B_SELECT_SR
| ISO14B_DISCONNECT
),
337 PacketResponseNG resp
;
338 clearCommandBuffer();
339 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
340 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
)) {
342 if (resp
.status
== PM3_SUCCESS
) {
343 memcpy(d
, resp
.data
.asBytes
, sizeof(iso14b_card_select_t
));
345 iso14b_card_select_t
*card
= (iso14b_card_select_t
*)d
;
346 uint8_t empty
[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
347 if (memcmp(card
->uid
, empty
, card
->uidlen
) == 0) {
350 *found_type
= ISO14B_SR
;
356 packet
.flags
= (ISO14B_CONNECT
| ISO14B_SELECT_STD
| ISO14B_DISCONNECT
);
358 clearCommandBuffer();
359 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
360 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
)) {
362 if (resp
.status
== PM3_SUCCESS
) {
363 memcpy(d
, resp
.data
.asBytes
, sizeof(iso14b_card_select_t
));
364 *found_type
= ISO14B_STANDARD
;
370 packet
.flags
= (ISO14B_CONNECT
| ISO14B_SELECT_CTS
| ISO14B_DISCONNECT
);
371 clearCommandBuffer();
372 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
373 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
)) {
375 if (resp
.status
== PM3_SUCCESS
) {
376 memcpy(d
, resp
.data
.asBytes
, sizeof(iso14b_cts_card_select_t
));
377 *found_type
= ISO14B_CT
;
382 PrintAndLogEx(WARNING
, "timeout while waiting for reply");
386 /* extract uid from filename
387 * filename must match '^hf-14b-[0-9A-F]{16}'
389 uint8_t *get_uid_from_filename(const char *filename
) {
391 static uint8_t uid
[8];
394 if (strlen(filename
) < 23) {
395 PrintAndLogEx(ERR
, "can't get uid from filename '%s'. Expected format is hf-14b-<uid>...", filename
);
399 char *found
= strstr(filename
, "hf-14b-");
401 PrintAndLogEx(ERR
, "can't get uid from filename '%s'. Expected format is hf-14b-<uid>...", filename
);
405 // extract uid part from filename
406 char uidinhex
[17] = {0};
407 strncpy(uidinhex
, found
+ 7, 16);
410 int len
= hex_to_bytes(uidinhex
, uid
, 8);
412 return SwapEndian64(uid
, 8, 8);
414 PrintAndLogEx(ERR
, "get_uid_from_filename failed: hex_to_bytes returned %d", len
);
420 // print full atqb info
422 // 0,1,2,3 = application data
423 // 4 = bit rate capacity
424 // 5 = max frame size / -4 info
425 // 6 = FWI / Coding options
426 static int print_atqb_resp(uint8_t *data
, uint8_t cid
) {
427 //PrintAndLogEx(SUCCESS, " UID: %s", sprint_hex(data+1,4));
428 PrintAndLogEx(SUCCESS
, " App Data: %s", sprint_hex(data
, 4));
429 PrintAndLogEx(SUCCESS
, " Protocol: %s", sprint_hex(data
+ 4, 3));
430 uint8_t BitRate
= data
[4];
431 if (!BitRate
) PrintAndLogEx(SUCCESS
, " Bit Rate: 106 kbit/s only PICC <-> PCD");
432 if (BitRate
& 0x10) PrintAndLogEx(SUCCESS
, " Bit Rate: 212 kbit/s PICC -> PCD supported");
433 if (BitRate
& 0x20) PrintAndLogEx(SUCCESS
, " Bit Rate: 424 kbit/s PICC -> PCD supported");
434 if (BitRate
& 0x40) PrintAndLogEx(SUCCESS
, " Bit Rate: 847 kbit/s PICC -> PCD supported");
435 if (BitRate
& 0x01) PrintAndLogEx(SUCCESS
, " Bit Rate: 212 kbit/s PICC <- PCD supported");
436 if (BitRate
& 0x02) PrintAndLogEx(SUCCESS
, " Bit Rate: 424 kbit/s PICC <- PCD supported");
437 if (BitRate
& 0x04) PrintAndLogEx(SUCCESS
, " Bit Rate: 847 kbit/s PICC <- PCD supported");
438 if (BitRate
& 0x80) PrintAndLogEx(SUCCESS
, " Same bit rate <-> required");
440 uint16_t maxFrame
= data
[5] >> 4;
441 if (maxFrame
< 5) maxFrame
= 8 * maxFrame
+ 16;
442 else if (maxFrame
== 5) maxFrame
= 64;
443 else if (maxFrame
== 6) maxFrame
= 96;
444 else if (maxFrame
== 7) maxFrame
= 128;
445 else if (maxFrame
== 8) maxFrame
= 256;
448 PrintAndLogEx(SUCCESS
, "Max Frame Size: %u%s bytes", maxFrame
, (maxFrame
== 257) ? "+ RFU" : "");
450 uint8_t protocolT
= data
[5] & 0xF;
451 PrintAndLogEx(SUCCESS
, " Protocol Type: Protocol is %scompliant with ISO/IEC 14443-4", (protocolT
) ? "" : "not ");
453 uint8_t fwt
= data
[6] >> 4;
455 uint32_t etus
= (32 << fwt
);
456 uint32_t fwt_time
= (302 << fwt
);
457 PrintAndLogEx(SUCCESS
, "Frame Wait Integer: %u - %u ETUs | %u us", fwt
, etus
, fwt_time
);
459 PrintAndLogEx(SUCCESS
, "Frame Wait Integer: %u - RFU", fwt
);
462 PrintAndLogEx(SUCCESS
, " App Data Code: Application is %s", (data
[6] & 4) ? "Standard" : "Proprietary");
463 PrintAndLogEx(SUCCESS
, " Frame Options: NAD is %ssupported", (data
[6] & 2) ? "" : "not ");
464 PrintAndLogEx(SUCCESS
, " Frame Options: CID is %ssupported", (data
[6] & 1) ? "" : "not ");
465 PrintAndLogEx(SUCCESS
, "Tag :");
466 PrintAndLogEx(SUCCESS
, " Max Buf Length: %u (MBLI) %s", cid
>> 4, (cid
& 0xF0) ? "" : "chained frames not supported");
467 PrintAndLogEx(SUCCESS
, " CID : %u", cid
& 0x0f);
468 PrintAndLogEx(NORMAL
, "");
470 PrintAndLogEx(INFO
, "--- " _CYAN_("Fingerprint"));
471 if (memcmp(data
, "\x54\x43\x4F\x53", 4) == 0) {
474 uint8_t out
[PM3_CMD_DATA_SIZE
] = {0};
475 uint8_t tcos_version
[] = {0x90, 0xB2, 0x90, 0x00, 0x00};
476 if (exchange_14b_apdu(tcos_version
, sizeof(tcos_version
), true, false, out
, PM3_CMD_DATA_SIZE
, &outlen
, -1) == PM3_SUCCESS
) {
478 PrintAndLogEx(SUCCESS
, "Tiananxin TCOS CPU card... " _YELLOW_("%s"), sprint_ascii(out
, outlen
- 2));
480 PrintAndLogEx(SUCCESS
, "Tiananxin TCOS CPU card... " _RED_("n/a"));
482 PrintAndLogEx(SUCCESS
, "Magic capabilities........ most likely");
486 PrintAndLogEx(INFO
, "n/a");
488 PrintAndLogEx(NORMAL
, "");
492 // get SRx chip model (from UID) // from ST Microelectronics
493 static const char *get_st_chip_model(uint8_t id
) {
496 return "SRIX4K (Special)";
515 static const char *get_st25_chip_model(uint8_t id) {
518 return "ST25TB512-AC";
520 return "ST25TB512-AT";
531 #define ST_LOCK_INFO_EMPTY " "
532 static const char *get_st_lock_info(uint8_t model
, const uint8_t *lockbytes
, uint8_t blk
) {
534 return ST_LOCK_INFO_EMPTY
;
539 case 0x0: // SRIX4K special
570 return ST_LOCK_INFO_EMPTY
;
572 if ((lockbytes
[1] & mask
) == 0) {
575 return ST_LOCK_INFO_EMPTY
;
579 case 0xC: { // SRT512
580 //need data[2] and data[3]
640 if ((lockbytes
[b
] & mask
) == 0) {
643 return ST_LOCK_INFO_EMPTY
;
681 // iceman: this is opposite! need sample to test with.
682 if ((lockbytes
[0] & mask
)) {
685 return ST_LOCK_INFO_EMPTY
;
690 return ST_LOCK_INFO_EMPTY
;
693 static uint8_t get_st_chipid(const uint8_t *uid
) {
698 static uint8_t get_st25_chipid(const uint8_t *uid) {
703 static uint8_t get_st_cardsize(const uint8_t *uid
) {
704 uint8_t chipid
= get_st_chipid(uid
);
721 static uint8_t get_st25_cardsize(const uint8_t *uid) {
722 uint8_t chipid = get_st25_chipid(uid);
726 return ST25_SIZE_512;
737 // print UID info from SRx chips (ST Microelectronics)
738 static void print_st_general_info(uint8_t *data
, uint8_t len
) {
739 //uid = first 8 bytes in data
740 uint8_t mfgid
= data
[6];
741 uint8_t chipid
= get_st_chipid(data
);
742 PrintAndLogEx(NORMAL
, "");
743 PrintAndLogEx(SUCCESS
, " UID: " _GREEN_("%s"), sprint_hex(SwapEndian64(data
, 8, 8), len
));
744 PrintAndLogEx(SUCCESS
, " MFG: %02X, " _YELLOW_("%s"), mfgid
, getTagInfo(mfgid
));
745 PrintAndLogEx(SUCCESS
, "Chip: %02X, " _YELLOW_("%s"), chipid
, get_st_chip_model(chipid
));
748 // print UID info from ASK CT chips
749 static void print_ct_general_info(void *vcard
) {
750 iso14b_cts_card_select_t card
;
751 memcpy(&card
, (iso14b_cts_card_select_t
*)vcard
, sizeof(iso14b_cts_card_select_t
));
753 uint32_t uid32
= MemLeToUint4byte(card
.uid
);
754 PrintAndLogEx(NORMAL
, "");
755 PrintAndLogEx(SUCCESS
, "ASK C-Ticket");
756 PrintAndLogEx(SUCCESS
, " UID: " _GREEN_("%s") " ( " _YELLOW_("%010u") " )", sprint_hex(card
.uid
, sizeof(card
.uid
)), uid32
);
757 PrintAndLogEx(SUCCESS
, " Product Code: %02X", card
.pc
);
758 PrintAndLogEx(SUCCESS
, " Facility Code: %02X", card
.fc
);
759 PrintAndLogEx(NORMAL
, "");
762 static void print_hdr(void) {
763 PrintAndLogEx(NORMAL
, "");
764 PrintAndLogEx(INFO
, " block# | data |lck| ascii");
765 PrintAndLogEx(INFO
, "---------+-------------+---+------");
768 static void print_footer(void) {
769 PrintAndLogEx(INFO
, "---------+-------------+---+------");
770 PrintAndLogEx(NORMAL
, "");
774 static void print_ct_blocks(uint8_t *data, size_t len) {
776 size_t blocks = len / ST25TB_SR_BLOCK_SIZE;
780 for (int i = 0; i <= blocks; i++) {
782 "%3d/0x%02X | %s | %s | %s",
785 sprint_hex(data + (i * 4), 4),
787 sprint_ascii(data + (i * 4), 4)
794 static void print_sr_blocks(uint8_t *data
, size_t len
, const uint8_t *uid
, bool dense_output
) {
796 size_t blocks
= (len
/ ST25TB_SR_BLOCK_SIZE
) - 1 ;
797 uint8_t *systemblock
= data
+ blocks
* ST25TB_SR_BLOCK_SIZE
;
798 uint8_t chipid
= get_st_chipid(uid
);
800 PrintAndLogEx(NORMAL
, "");
801 PrintAndLogEx(INFO
, "-------- " _CYAN_("%s tag memory") " ---------", get_st_chip_model(chipid
));
802 PrintAndLogEx(DEBUG
, "systemblock... " _YELLOW_("%s"), sprint_hex(systemblock
, ST25TB_SR_BLOCK_SIZE
));
803 PrintAndLogEx(DEBUG
, " otp lock... " _YELLOW_("%02x %02x"), *systemblock
, *(systemblock
+ 1));
807 bool in_repeated_block
= false;
810 for (int i
= 0; i
< blocks
; i
++) {
812 // suppress repeating blocks, truncate as such that the first and last block with the same data is shown
813 // but the blocks in between are replaced with a single line of "......" if dense_output is enabled
814 uint8_t *blk
= data
+ (i
* ST25TB_SR_BLOCK_SIZE
);
817 (i
< (blocks
- 1)) &&
818 (in_repeated_block
== false) &&
819 (memcmp(blk
, blk
- ST25TB_SR_BLOCK_SIZE
, ST25TB_SR_BLOCK_SIZE
) == 0) &&
820 (memcmp(blk
, blk
+ ST25TB_SR_BLOCK_SIZE
, ST25TB_SR_BLOCK_SIZE
) == 0) &&
821 (memcmp(blk
, blk
+ (ST25TB_SR_BLOCK_SIZE
* 2), ST25TB_SR_BLOCK_SIZE
) == 0)
823 // we're in a user block that isn't the first user block nor last two user blocks,
824 // and the current block data is the same as the previous and next two block
825 in_repeated_block
= true;
826 PrintAndLogEx(INFO
, " ......");
827 } else if (in_repeated_block
&&
828 (memcmp(blk
, blk
+ ST25TB_SR_BLOCK_SIZE
, ST25TB_SR_BLOCK_SIZE
) || i
== blocks
)
830 // in a repeating block, but the next block doesn't match anymore, or we're at the end block
831 in_repeated_block
= false;
834 if (in_repeated_block
== false) {
836 "%3d/0x%02X | %s| %s | %s",
839 sprint_hex(data
+ (i
* ST25TB_SR_BLOCK_SIZE
), ST25TB_SR_BLOCK_SIZE
),
840 get_st_lock_info(chipid
, systemblock
, i
),
841 sprint_ascii(data
+ (i
* ST25TB_SR_BLOCK_SIZE
), ST25TB_SR_BLOCK_SIZE
)
847 "%3d/0x%02X | %s| %s | %s",
850 sprint_hex(systemblock
, ST25TB_SR_BLOCK_SIZE
),
851 get_st_lock_info(chipid
, systemblock
, 0xFF),
852 sprint_ascii(systemblock
, ST25TB_SR_BLOCK_SIZE
)
859 // 05 00 00 = find one tag in field
860 // 1d xx xx xx xx 00 08 01 00 = attrib xx=UID (resp 10 [f9 e0])
861 // 0200a40400 (resp 02 67 00 [29 5b])
862 // 0200a4040c07a0000002480300 (resp 02 67 00 [29 5b])
863 // 0200a4040c07a0000002480200 (resp 02 67 00 [29 5b])
864 // 0200a4040006a0000000010100 (resp 02 6a 82 [4b 4c])
865 // 0200a4040c09d27600002545500200 (resp 02 67 00 [29 5b])
866 // 0200a404000cd2760001354b414e4d30310000 (resp 02 6a 82 [4b 4c])
867 // 0200a404000ca000000063504b43532d313500 (resp 02 6a 82 [4b 4c])
868 // 0200a4040010a000000018300301000000000000000000 (resp 02 6a 82 [4b 4c])
870 static int CmdHF14BList(const char *Cmd
) {
871 return CmdTraceListAlias(Cmd
, "hf 14b", "14b -c");
874 static int CmdHF14BSim(const char *Cmd
) {
876 CLIParserContext
*ctx
;
877 CLIParserInit(&ctx
, "hf 14b sim",
878 "Simulate a ISO/IEC 14443 type B tag with 4 byte UID / PUPI",
879 "hf 14b sim -u 11AA33BB"
884 arg_str1("u", "uid", "hex", "4byte UID/PUPI"),
887 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
891 int res
= CLIParamHexToBuf(arg_get_str(ctx
, 1), pupi
, sizeof(pupi
), &n
);
895 PrintAndLogEx(FAILED
, "failed to read pupi");
899 PrintAndLogEx(INFO
, "Simulate with PUPI : " _GREEN_("%s"), sprint_hex_inrow(pupi
, sizeof(pupi
)));
900 PrintAndLogEx(INFO
, "Press " _GREEN_("pm3 button") " to abort simulation");
901 clearCommandBuffer();
902 SendCommandNG(CMD_HF_ISO14443B_SIMULATE
, pupi
, sizeof(pupi
));
906 static int CmdHF14BSniff(const char *Cmd
) {
908 CLIParserContext
*ctx
;
909 CLIParserInit(&ctx
, "hf 14b sniff",
910 "Sniff the communication between reader and tag\n"
911 "Use `hf 14b list` to view collected data.",
919 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
922 PrintAndLogEx(INFO
, "Press " _GREEN_("pm3 button") " to abort sniffing");
924 PacketResponseNG resp
;
925 clearCommandBuffer();
926 SendCommandNG(CMD_HF_ISO14443B_SNIFF
, NULL
, 0);
927 WaitForResponse(CMD_HF_ISO14443B_SNIFF
, &resp
);
928 PrintAndLogEx(HINT
, "Try `" _YELLOW_("hf 14b list") "` to view captured tracelog");
929 PrintAndLogEx(HINT
, "Try `" _YELLOW_("trace save -h") "` to save tracelog for later analysing");
933 static int CmdHF14BRaw(const char *Cmd
) {
934 CLIParserContext
*ctx
;
935 CLIParserInit(&ctx
, "hf 14b raw",
936 "Sends raw bytes to card. Activates field by default",
937 "hf 14b raw -cks --data 0200a40400 -> standard select, apdu 0200a4000 (7816)\n"
938 "hf 14b raw -ck --sr --data 0200a40400 -> SRx select\n"
939 "hf 14b raw -ck --cts --data 0200a40400 -> C-ticket select\n"
944 arg_lit0("a", NULL
, "active signal field ON without select"),
945 arg_lit0("c", "crc", "calculate and append CRC"),
946 arg_lit0("k", "keep", "leave the signal field ON after receive response"),
948 arg_str0("d", "data", "<hex>", "data, bytes to send"),
949 arg_lit0("r", NULL
, "do not read response from card"),
950 arg_int0("t", "timeout", "<dec>", "timeout in ms"),
952 arg_lit0("s", "std", "use ISO14B select"),
953 arg_lit0(NULL
, "sr", "use SRx ST select"),
954 arg_lit0(NULL
, "cts", "use ASK C-ticket select"),
955 arg_lit0(NULL
, "xrx", "use Fuji/Xerox select"),
956 arg_lit0(NULL
, "pico", "use Picopass select"),
958 arg_lit0("v", "verbose", "verbose output"),
961 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
963 bool activate_field
= arg_get_lit(ctx
, 1);
964 bool add_crc
= arg_get_lit(ctx
, 2);
965 bool keep_field_on
= arg_get_lit(ctx
, 3);
967 uint8_t data
[PM3_CMD_DATA_SIZE
] = {0x00};
969 CLIParamHexToBuf(arg_get_str(ctx
, 4), data
, sizeof(data
), &datalen
);
971 bool read_reply
= (arg_get_lit(ctx
, 5) == false);
972 int user_timeout
= arg_get_int_def(ctx
, 6, -1);
973 bool select_std
= arg_get_lit(ctx
, 7);
974 bool select_sr
= arg_get_lit(ctx
, 8);
975 bool select_cts
= arg_get_lit(ctx
, 9);
976 bool select_xrx
= arg_get_lit(ctx
, 10);
977 bool select_pico
= arg_get_lit(ctx
, 11);
978 bool verbose
= arg_get_lit(ctx
, 12);
981 // FLAGS for device side
984 if (activate_field
) {
985 flags
|= ISO14B_CONNECT
;
989 flags
|= ISO14B_APPEND_CRC
;
993 flags
|= (ISO14B_CONNECT
| ISO14B_SELECT_STD
| ISO14B_CLEARTRACE
);
995 PrintAndLogEx(INFO
, "using ISO14443-B select");
997 } else if (select_sr
) {
998 flags
|= (ISO14B_CONNECT
| ISO14B_SELECT_SR
| ISO14B_CLEARTRACE
);
1000 PrintAndLogEx(INFO
, "using ST/SRx select");
1002 } else if (select_cts
) {
1003 flags
|= (ISO14B_CONNECT
| ISO14B_SELECT_CTS
| ISO14B_CLEARTRACE
);
1005 PrintAndLogEx(INFO
, "using ASK/C-ticket select");
1007 } else if (select_xrx
) {
1008 flags
|= (ISO14B_CONNECT
| ISO14B_SELECT_XRX
| ISO14B_CLEARTRACE
);
1010 PrintAndLogEx(INFO
, "using Fuji/Xerox select");
1012 } else if (select_pico
) {
1013 flags
|= (ISO14B_CONNECT
| ISO14B_SELECT_PICOPASS
| ISO14B_CLEARTRACE
);
1015 PrintAndLogEx(INFO
, "using Picopass select");
1019 uint32_t time_wait
= 0;
1020 if (user_timeout
> 0) {
1022 flags
|= ISO14B_SET_TIMEOUT
;
1024 if (user_timeout
> MAX_14B_TIMEOUT_MS
) {
1025 user_timeout
= MAX_14B_TIMEOUT_MS
;
1026 PrintAndLogEx(INFO
, "set timeout to 4.9 seconds. The max we can wait for response");
1029 // timeout in ETUs (time to transfer 1 bit, approx. 9.4 us)
1030 time_wait
= (uint32_t)((13560 / 128) * user_timeout
);
1032 PrintAndLogEx(INFO
, " new raw timeout : %u ETU ( %u ms )", time_wait
, user_timeout
);
1035 if (keep_field_on
== false) {
1036 flags
|= ISO14B_DISCONNECT
;
1040 flags
|= ISO14B_RAW
;
1043 // Max buffer is PM3_CMD_DATA_SIZE
1044 datalen
= (datalen
> PM3_CMD_DATA_SIZE
) ? PM3_CMD_DATA_SIZE
: datalen
;
1046 iso14b_raw_cmd_t
*packet
= (iso14b_raw_cmd_t
*)calloc(1, sizeof(iso14b_raw_cmd_t
) + datalen
);
1047 if (packet
== NULL
) {
1048 PrintAndLogEx(FAILED
, "failed to allocate memory");
1052 packet
->flags
= flags
;
1053 packet
->timeout
= time_wait
;
1054 packet
->rawlen
= datalen
;
1055 memcpy(packet
->raw
, data
, datalen
);
1057 clearCommandBuffer();
1058 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)packet
, sizeof(iso14b_raw_cmd_t
) + packet
->rawlen
);
1061 if (read_reply
== false) {
1062 clearCommandBuffer();
1066 bool success
= true;
1068 // Select, device will send back iso14b_card_select_t, don't print it.
1070 success
= wait_cmd_14b(verbose
, true, user_timeout
);
1071 if (verbose
&& success
) {
1072 PrintAndLogEx(SUCCESS
, "Got response for standard select");
1077 success
= wait_cmd_14b(verbose
, true, user_timeout
);
1078 if (verbose
&& success
) {
1079 PrintAndLogEx(SUCCESS
, "Got response for ST/SRx select");
1084 success
= wait_cmd_14b(verbose
, true, user_timeout
);
1085 if (verbose
&& success
) {
1086 PrintAndLogEx(SUCCESS
, "Got response for ASK/C-ticket select");
1091 success
= wait_cmd_14b(verbose
, true, user_timeout
);
1092 if (verbose
&& success
) {
1093 PrintAndLogEx(SUCCESS
, "Got response for Fuji/Xerox select");
1098 success
= wait_cmd_14b(verbose
, true, user_timeout
);
1099 if (verbose
&& success
) {
1100 PrintAndLogEx(SUCCESS
, "Got response for Picopass select");
1104 // get back response from the raw bytes you sent.
1105 if (success
&& datalen
> 0) {
1106 wait_cmd_14b(true, false, user_timeout
);
1112 // 14b get and print Full Info (as much as we know)
1113 static bool HF14B_Std_Info(bool verbose
, bool do_aid_search
) {
1114 // 14b get and print UID only (general info)
1115 iso14b_raw_cmd_t packet
= {
1116 .flags
= (ISO14B_CONNECT
| ISO14B_SELECT_STD
| ISO14B_DISCONNECT
),
1121 clearCommandBuffer();
1122 PacketResponseNG resp
;
1123 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
1124 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
1126 PrintAndLogEx(WARNING
, "timeout while waiting for reply");
1128 switch_off_field_14b();
1132 switch (resp
.status
) {
1135 iso14b_card_select_t card
;
1136 memcpy(&card
, (iso14b_card_select_t
*)resp
.data
.asBytes
, sizeof(iso14b_card_select_t
));
1138 PrintAndLogEx(NORMAL
, "");
1139 PrintAndLogEx(INFO
, "--- " _CYAN_("Tag Information") " ---------------------------");
1140 PrintAndLogEx(SUCCESS
, " UID : " _GREEN_("%s"), sprint_hex(card
.uid
, card
.uidlen
));
1141 PrintAndLogEx(SUCCESS
, " ATQB : %s", sprint_hex(card
.atqb
, sizeof(card
.atqb
)));
1142 PrintAndLogEx(SUCCESS
, " CHIPID : %02X", card
.chipid
);
1143 print_atqb_resp(card
.atqb
, card
.cid
);
1145 if (do_aid_search
) {
1146 hf14b_aid_search(verbose
);
1152 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 STD ATTRIB fail");
1155 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 STD CRC fail");
1158 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-b STD select failed");
1165 // SRx get and print full info (needs more info...)
1166 static bool HF14B_ST_Info(bool verbose
, bool do_aid_search
) {
1168 iso14b_raw_cmd_t packet
= {
1169 .flags
= (ISO14B_CONNECT
| ISO14B_SELECT_SR
| ISO14B_DISCONNECT
),
1174 clearCommandBuffer();
1175 PacketResponseNG resp
;
1176 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
1177 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
1179 PrintAndLogEx(WARNING
, "timeout while waiting for reply");
1184 if (resp
.status
!= PM3_SUCCESS
) {
1188 iso14b_card_select_t card
;
1189 memcpy(&card
, (iso14b_card_select_t
*)resp
.data
.asBytes
, sizeof(iso14b_card_select_t
));
1191 uint8_t empty
[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1192 if ((card
.uidlen
!= 8) || (memcmp(card
.uid
, empty
, card
.uidlen
) == 0)) {
1196 print_st_general_info(card
.uid
, card
.uidlen
);
1198 if (do_aid_search
) {
1199 hf14b_aid_search(verbose
);
1204 // menu command to get and print all info known about any known 14b tag
1205 static int CmdHF14Binfo(const char *Cmd
) {
1206 CLIParserContext
*ctx
;
1207 CLIParserInit(&ctx
, "hf 14b info",
1208 "Tag information for ISO/IEC 14443 type B based tags",
1212 void *argtable
[] = {
1214 arg_lit0("s", "aidsearch", "checks if AIDs from aidlist.json is present on the card and prints information about found AIDs"),
1215 arg_lit0("v", "verbose", "verbose output"),
1218 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
1219 bool do_aid_search
= arg_get_lit(ctx
, 1);
1220 bool verbose
= arg_get_lit(ctx
, 2);
1222 return infoHF14B(verbose
, do_aid_search
);
1225 // #define ISO14443B_READ_BLK 0x08
1226 // #define ISO14443B_WRITE_BLK 0x09
1228 static int read_sr_block(uint8_t blockno
, uint8_t *out
, uint16_t out_len
) {
1233 payload
.blockno
= blockno
;
1235 PacketResponseNG resp
;
1236 clearCommandBuffer();
1237 SendCommandNG(CMD_HF_SRI_READ
, (uint8_t *)&payload
, sizeof(payload
));
1238 if (WaitForResponseTimeout(CMD_HF_SRI_READ
, &resp
, TIMEOUT
) == false) {
1239 return PM3_ETIMEOUT
;
1242 if (resp
.status
== PM3_SUCCESS
&& out
) {
1243 memcpy(out
, resp
.data
.asBytes
, MIN(out_len
, resp
.length
));
1248 static int write_sr_block(uint8_t blockno
, uint8_t datalen
, uint8_t *data
) {
1250 uint8_t psize
= sizeof(iso14b_raw_cmd_t
) + datalen
+ 2;
1251 iso14b_raw_cmd_t
*packet
= (iso14b_raw_cmd_t
*)calloc(1, psize
);
1252 if (packet
== NULL
) {
1253 PrintAndLogEx(FAILED
, "failed to allocate memory");
1257 packet
->flags
= (ISO14B_CONNECT
| ISO14B_SELECT_SR
| ISO14B_RAW
| ISO14B_APPEND_CRC
| ISO14B_DISCONNECT
);
1258 packet
->timeout
= 0;
1260 packet
->raw
[0] = ISO14443B_WRITE_BLK
;
1261 packet
->raw
[1] = blockno
;
1262 packet
->raw
[2] = data
[0];
1263 packet
->raw
[3] = data
[1];
1264 packet
->raw
[4] = data
[2];
1265 packet
->raw
[5] = data
[3];
1267 // SRx get and print general info about SRx chip from UID
1268 clearCommandBuffer();
1269 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)packet
, psize
);
1272 if (wait_14b_response(true, NULL
, NULL
) == false) {
1273 PrintAndLogEx(FAILED
, "SRx write block ( " _RED_("failed") " )");
1279 static bool HF14B_st_reader(bool verbose
) {
1281 iso14b_raw_cmd_t packet
= {
1282 .flags
= (ISO14B_CONNECT
| ISO14B_SELECT_SR
| ISO14B_DISCONNECT
),
1287 // SRx get and print general info about SRx chip from UID
1288 clearCommandBuffer();
1289 PacketResponseNG resp
;
1290 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
1291 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
1293 PrintAndLogEx(WARNING
, "timeout while waiting for reply");
1298 switch (resp
.status
) {
1300 iso14b_card_select_t card
;
1301 memcpy(&card
, (iso14b_card_select_t
*)resp
.data
.asBytes
, sizeof(iso14b_card_select_t
));
1303 uint8_t empty
[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1304 if ((card
.uidlen
!= 8) || (memcmp(card
.uid
, empty
, card
.uidlen
) == 0)) {
1307 print_st_general_info(card
.uid
, card
.uidlen
);
1311 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 ST ATTRIB fail");
1314 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 ST CRC fail");
1316 case PM3_EWRONGANSWER
:
1317 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 ST random chip id fail");
1320 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-b ST select SRx failed");
1326 static bool HF14B_std_reader(bool verbose
) {
1327 iso14b_raw_cmd_t packet
= {
1328 .flags
= (ISO14B_CONNECT
| ISO14B_SELECT_STD
| ISO14B_DISCONNECT
),
1333 // 14b get and print UID only (general info)
1334 clearCommandBuffer();
1335 PacketResponseNG resp
;
1336 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
1337 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
1339 PrintAndLogEx(WARNING
, "timeout while waiting for reply");
1344 switch (resp
.status
) {
1346 iso14b_card_select_t card
;
1347 memcpy(&card
, (iso14b_card_select_t
*)resp
.data
.asBytes
, sizeof(iso14b_card_select_t
));
1349 uint8_t empty
[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1350 if (memcmp(card
.uid
, empty
, card
.uidlen
) == 0) {
1353 PrintAndLogEx(NORMAL
, "");
1354 PrintAndLogEx(SUCCESS
, " UID : " _GREEN_("%s"), sprint_hex(card
.uid
, card
.uidlen
));
1355 PrintAndLogEx(SUCCESS
, " ATQB : %s", sprint_hex(card
.atqb
, sizeof(card
.atqb
)));
1356 PrintAndLogEx(SUCCESS
, " CHIPID : %02X", card
.chipid
);
1357 print_atqb_resp(card
.atqb
, card
.cid
);
1361 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 ATTRIB fail");
1365 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 CRC fail");
1369 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-b card select failed");
1376 static bool HF14B_ask_ct_reader(bool verbose
) {
1378 iso14b_raw_cmd_t packet
= {
1379 .flags
= (ISO14B_CONNECT
| ISO14B_SELECT_CTS
| ISO14B_DISCONNECT
),
1384 // 14b get and print UID only (general info)
1385 clearCommandBuffer();
1386 PacketResponseNG resp
;
1387 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
1388 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
1389 if (verbose
) PrintAndLogEx(WARNING
, "timeout while waiting for reply");
1393 switch (resp
.status
) {
1395 print_ct_general_info(resp
.data
.asBytes
);
1399 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 CTS wrong length");
1403 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 CTS CRC fail");
1407 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-b CTS select failed");
1414 bool HF14B_picopass_reader(bool verbose
, bool info
) {
1416 iso14b_raw_cmd_t packet
= {
1417 .flags
= (ISO14B_CONNECT
| ISO14B_SELECT_PICOPASS
| ISO14B_DISCONNECT
),
1422 // 14b get and print UID only (general info)
1423 clearCommandBuffer();
1424 PacketResponseNG resp
;
1425 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
1426 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
1427 if (verbose
) PrintAndLogEx(WARNING
, "timeout while waiting for reply");
1431 switch (resp
.status
) {
1434 picopass_hdr_t
*card
= calloc(1, sizeof(picopass_hdr_t
));
1436 PrintAndLogEx(FAILED
, "failed to allocate memory");
1439 memcpy(card
, resp
.data
.asBytes
, sizeof(picopass_hdr_t
));
1441 PrintAndLogEx(NORMAL
, "");
1442 PrintAndLogEx(SUCCESS
, "iCLASS / Picopass CSN: " _GREEN_("%s"), sprint_hex(card
->csn
, sizeof(card
->csn
)));
1448 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 wrong length");
1452 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-3 CRC fail");
1456 if (verbose
) PrintAndLogEx(FAILED
, "ISO 14443-b Picopass select failed");
1463 // test for other 14b type tags (mimic another reader - don't have tags to identify)
1464 static bool HF14B_other_reader(bool verbose
) {
1466 iso14b_raw_cmd_t
*packet
= (iso14b_raw_cmd_t
*)calloc(1, sizeof(iso14b_raw_cmd_t
) + 4);
1467 if (packet
== NULL
) {
1468 PrintAndLogEx(FAILED
, "failed to allocate memory");
1471 packet
->flags
= (ISO14B_CONNECT
| ISO14B_SELECT_STD
| ISO14B_RAW
| ISO14B_APPEND_CRC
);
1472 packet
->timeout
= 0;
1474 memcpy(packet
->raw
, "\x00\x0b\x3f\x80", 4);
1476 // 14b get and print UID only (general info)
1478 clearCommandBuffer();
1479 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)packet
, sizeof(iso14b_raw_cmd_t
) + packet
->rawlen
);
1481 // wait for the select message and wait for response
1482 if (wait_14b_response(false, NULL
, NULL
)) {
1483 PrintAndLogEx(SUCCESS
, "\n14443-3b tag found:");
1484 PrintAndLogEx(SUCCESS
, "unknown tag type answered to a " _YELLOW_("0x000b3f80") " command");
1485 switch_off_field_14b();
1491 packet
->raw
[0] = ISO14443B_AUTHENTICATE
;
1492 clearCommandBuffer();
1493 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)packet
, sizeof(iso14b_raw_cmd_t
) + packet
->rawlen
);
1495 if (wait_14b_response(false, NULL
, NULL
)) {
1496 PrintAndLogEx(SUCCESS
, "\n14443-3b tag found:");
1497 PrintAndLogEx(SUCCESS
, "Unknown tag type answered to a " _YELLOW_("0x0A") " command");
1498 switch_off_field_14b();
1503 packet
->raw
[0] = ISO14443B_RESET
;
1504 clearCommandBuffer();
1505 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)packet
, sizeof(iso14b_raw_cmd_t
) + packet
->rawlen
);
1507 if (wait_14b_response(false, NULL
, NULL
)) {
1508 PrintAndLogEx(SUCCESS
, "\n14443-3b tag found:");
1509 PrintAndLogEx(SUCCESS
, "Unknown tag type answered to a " _YELLOW_("0x0C") " command");
1510 switch_off_field_14b();
1514 switch_off_field_14b();
1518 // menu command to get and print general info about all known 14b chips
1519 static int CmdHF14BReader(const char *Cmd
) {
1520 CLIParserContext
*ctx
;
1521 CLIParserInit(&ctx
, "hf 14b reader",
1522 "Act as a 14443B reader to identify a tag",
1524 "hf 14b reader -@ -> continuous reader mode"
1527 void *argtable
[] = {
1529 arg_lit0(NULL
, "plot", "show anticollision signal trace in plot window"),
1530 arg_lit0("v", "verbose", "verbose output"),
1531 arg_lit0("@", NULL
, "optional - continuous reader mode"),
1534 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
1535 bool read_plot
= arg_get_lit(ctx
, 1);
1536 bool verbose
= arg_get_lit(ctx
, 2);
1537 bool cm
= arg_get_lit(ctx
, 3);
1541 PrintAndLogEx(INFO
, "Press " _GREEN_("<Enter>") " to exit");
1546 return readHF14B(cm
, verbose
, read_plot
);
1549 // Read SRI512|SRIX4K block
1550 static int CmdHF14BSriRdBl(const char *Cmd
) {
1552 CLIParserContext
*ctx
;
1553 CLIParserInit(&ctx
, "hf 14b rdbl",
1554 "Read SRI512 | SRIX4K block",
1555 "hf 14b rdbl -b 06\n"
1558 void *argtable
[] = {
1560 arg_int0("b", "block", "<dec>", "block number"),
1563 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
1564 int blockno
= arg_get_int_def(ctx
, 1, -1);
1568 iso14b_card_select_t card;
1569 if (get_14b_UID(&card) == false) {
1570 PrintAndLogEx(WARNING, "no tag found");
1574 if (card.uidlen != 8) {
1575 PrintAndLogEx(FAILED, "current read command only work with SRI4K / SRI512 tags");
1582 uint8_t cardtype = get_st_cardsize(card.uid);
1583 uint8_t blocks = (cardtype == 1) ? 0x7F : 0x0F;
1586 uint8_t out
[ST25TB_SR_BLOCK_SIZE
] = {0};
1587 int status
= read_sr_block(blockno
, out
, sizeof(out
));
1588 if (status
== PM3_SUCCESS
) {
1589 PrintAndLogEx(SUCCESS
, "block %02u... " _GREEN_("%s") " | " _GREEN_("%s"), blockno
, sprint_hex(out
, sizeof(out
)), sprint_ascii(out
, sizeof(out
)));
1594 // New command to write a SRI512/SRIX4K tag.
1595 static int CmdHF14BSriWrbl(const char *Cmd
) {
1597 * For SRIX4K blocks 00 - 7F
1598 * hf 14b raw --sr -c --data [09 $srix4kwblock $srix4kwdata
1600 * For SR512 blocks 00 - 0F
1601 * hf 14b raw --sr -c --data [09 $sr512wblock $sr512wdata]
1603 * Special block FF = otp_lock_reg block.
1607 CLIParserContext
*ctx
;
1608 CLIParserInit(&ctx
, "hf 14b wrbl",
1609 "Write data to a SRI512 or SRIX4K block\n"
1610 "If writing to a block out-of-range, use `--force` to override checks\n"
1611 "Special block at end denots OTP and lock bits among others",
1612 "hf 14b wrbl --4k -b 100 -d 11223344\n"
1613 "hf 14b wrbl --4k --sb -d 11223344 --> special block write\n"
1614 "hf 14b wrbl --512 -b 15 -d 11223344\n"
1615 "hf 14b wrbl --512 --sb -d 11223344 --> special block write\n"
1618 void *argtable
[] = {
1620 arg_int0("b", "block", "<dec>", "block number"),
1621 arg_str1("d", "data", "<hex>", "4 hex bytes"),
1622 arg_lit0(NULL
, "512", "target SRI 512 tag"),
1623 arg_lit0(NULL
, "4k", "target SRIX 4k tag (def)"),
1624 arg_lit0(NULL
, "sb", "special block write at end of memory (0xFF)"),
1625 arg_lit0(NULL
, "force", "overrides block range checks"),
1628 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
1629 int blockno
= arg_get_int_def(ctx
, 1, -1);
1631 uint8_t data
[ST25TB_SR_BLOCK_SIZE
] = {0, 0, 0, 0};
1632 int res
= CLIParamHexToBuf(arg_get_str(ctx
, 2), data
, sizeof(data
), &dlen
);
1638 bool use_sri512
= arg_get_lit(ctx
, 3);
1639 bool use_srix4k
= arg_get_lit(ctx
, 4);
1640 bool special
= arg_get_lit(ctx
, 5);
1641 bool override
= arg_get_lit(ctx
, 6);
1644 if (dlen
!= sizeof(data
)) {
1645 PrintAndLogEx(FAILED
, "data must be 4 hex bytes, got %d", dlen
);
1649 if (use_sri512
+ use_srix4k
> 1) {
1650 PrintAndLogEx(FAILED
, "Select only one card type");
1655 if (use_sri512
== false) {
1659 if (use_srix4k
&& blockno
> 0x7F) {
1660 PrintAndLogEx(FAILED
, "block number out of range, max 127 (0x7F), got " _RED_("%u"), blockno
);
1662 PrintAndLogEx(INFO
, "overriding block check");
1668 if (use_sri512
&& blockno
> 0x0F) {
1669 PrintAndLogEx(FAILED
, "block number out of range, max 15 (0x0F), got " _RED_("%u"), blockno
);
1671 PrintAndLogEx(INFO
, "overriding block check");
1677 // special block at end of memory
1680 PrintAndLogEx(SUCCESS
, _YELLOW_("%s") " Write special block %02X - " _YELLOW_("%s"),
1681 (use_srix4k
) ? "SRIX4K" : "SRI512",
1683 sprint_hex(data
, sizeof(data
))
1686 PrintAndLogEx(SUCCESS
, _YELLOW_("%s") " Write block %02X - " _YELLOW_("%s"),
1687 (use_srix4k
) ? "SRIX4K" : "SRI512",
1689 sprint_hex(data
, sizeof(data
))
1693 int status
= write_sr_block(blockno
, ST25TB_SR_BLOCK_SIZE
, data
);
1694 if (status
!= PM3_SUCCESS
) {
1699 uint8_t out
[ST25TB_SR_BLOCK_SIZE
] = {0};
1700 status
= read_sr_block(blockno
, out
, sizeof(out
));
1701 if (status
== PM3_SUCCESS
) {
1702 if (memcmp(data
, out
, 4) == 0) {
1703 PrintAndLogEx(SUCCESS
, "SRx write block ( " _GREEN_("ok") " )");
1706 PrintAndLogEx(INFO
, "Verifying block ( " _RED_("failed") " )");
1711 // need to write to file
1712 static int CmdHF14BDump(const char *Cmd
) {
1714 CLIParserContext
*ctx
;
1715 CLIParserInit(&ctx
, "hf 14b dump",
1716 "This command dumps the contents of a ISO-14443-B tag and save it to file\n"
1717 "Tries to autodetect cardtype, memory size defaults to SRI4K",
1719 "hf 14b dump -f myfilename\n"
1722 void *argtable
[] = {
1724 arg_str0("f", "file", "<fn>", "(optional) filename, if no <name> UID will be used as filename"),
1725 arg_lit0(NULL
, "ns", "no save to file"),
1726 arg_lit0("z", "dense", "dense dump output style"),
1729 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
1732 char filename
[FILE_PATH_SIZE
] = {0};
1733 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)filename
, FILE_PATH_SIZE
, &fnlen
);
1734 bool nosave
= arg_get_lit(ctx
, 2);
1735 bool dense_output
= (g_session
.dense_output
|| arg_get_lit(ctx
, 3));
1739 uint8_t select
[sizeof(iso14b_card_select_t
)] = {0};
1740 iso14b_type_t select_cardtype
= ISO14B_NONE
;
1741 if (get_14b_UID(select
, &select_cardtype
) == false) {
1742 PrintAndLogEx(WARNING
, "no tag found");
1746 if (select_cardtype
== ISO14B_CT
) {
1747 iso14b_cts_card_select_t ct_card
;
1748 memcpy(&ct_card
, (iso14b_cts_card_select_t
*)&select
, sizeof(iso14b_cts_card_select_t
));
1750 uint32_t uid32
= MemLeToUint4byte(ct_card
.uid
);
1751 PrintAndLogEx(SUCCESS
, "UID: " _GREEN_("%s") " ( " _YELLOW_("%010u") " )", sprint_hex(ct_card
.uid
, 4), uid32
);
1753 // Have to figure out how large one of these are..
1754 PrintAndLogEx(FAILED
, "Dumping CT tags is not implemented yet.");
1756 // print_ct_blocks(data, cardsize);
1757 return switch_off_field_14b();
1760 if (select_cardtype
== ISO14B_STANDARD
) {
1761 // Have to figure out how large one of these are..
1762 PrintAndLogEx(FAILED
, "Dumping Standard ISO14443-B tags is not implemented yet.");
1763 // print_std_blocks(data, cardsize);
1764 return switch_off_field_14b();
1767 if (select_cardtype
== ISO14B_SR
) {
1768 iso14b_card_select_t card
;
1769 memcpy(&card
, (iso14b_card_select_t
*)&select
, sizeof(iso14b_card_select_t
));
1774 uint8_t cardtype
= get_st_cardsize(card
.uid
);
1775 uint8_t lastblock
= 0;
1776 uint16_t cardsize
= 0;
1780 cardsize
= (512 / 8) + ST25TB_SR_BLOCK_SIZE
;
1785 cardsize
= (4096 / 8) + ST25TB_SR_BLOCK_SIZE
;
1790 uint8_t chipid
= get_st_chipid(card
.uid
);
1791 PrintAndLogEx(SUCCESS
, "found a " _GREEN_("%s") " tag", get_st_chip_model(chipid
));
1793 // detect blocksize from card :)
1794 PrintAndLogEx(INFO
, "reading tag memory");
1796 iso14b_raw_cmd_t
*packet
= (iso14b_raw_cmd_t
*)calloc(1, sizeof(iso14b_raw_cmd_t
) + 2);
1797 if (packet
== NULL
) {
1798 PrintAndLogEx(FAILED
, "failed to allocate memory");
1801 packet
->flags
= (ISO14B_CONNECT
| ISO14B_SELECT_SR
);
1802 packet
->timeout
= 0;
1805 clearCommandBuffer();
1806 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)packet
, sizeof(iso14b_raw_cmd_t
));
1807 PacketResponseNG resp
;
1810 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, 2000)) {
1811 if (resp
.status
!= PM3_SUCCESS
) {
1812 PrintAndLogEx(FAILED
, "failed to select ( " _RED_("%d") " )", resp
.status
);
1814 return switch_off_field_14b();
1818 PrintAndLogEx(INFO
, "." NOLF
);
1820 uint8_t data
[cardsize
];
1821 memset(data
, 0, sizeof(data
));
1822 uint16_t blocknum
= 0;
1824 for (int retry
= 0; retry
< 3; retry
++) {
1826 // set up the read command
1827 packet
->flags
= (ISO14B_APPEND_CRC
| ISO14B_RAW
);
1829 packet
->raw
[0] = ISO14443B_READ_BLK
;
1830 packet
->raw
[1] = blocknum
& 0xFF;
1832 clearCommandBuffer();
1833 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)packet
, sizeof(iso14b_raw_cmd_t
) + 2);
1834 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, 2000)) {
1836 if (resp
.status
!= PM3_SUCCESS
) {
1837 PrintAndLogEx(FAILED
, "retrying one more time");
1841 uint8_t *recv
= resp
.data
.asBytes
;
1843 if (check_crc(CRC_14443_B
, recv
, resp
.length
) == false) {
1844 PrintAndLogEx(FAILED
, "crc fail, retrying one more time");
1849 if (blocknum
== 0xFF) {
1850 // we reserved space for this block after 0x0F and 0x7F, ie 0x10, 0x80
1851 memcpy(data
+ ((lastblock
+ 1) * ST25TB_SR_BLOCK_SIZE
), recv
, ST25TB_SR_BLOCK_SIZE
);
1854 memcpy(data
+ (blocknum
* ST25TB_SR_BLOCK_SIZE
), recv
, ST25TB_SR_BLOCK_SIZE
);
1859 if (blocknum
> lastblock
) {
1860 // read config block
1864 PrintAndLogEx(NORMAL
, "." NOLF
);
1870 PrintAndLogEx(NORMAL
, "");
1871 switch_off_field_14b();
1873 if (blocknum
!= 0xFF) {
1874 PrintAndLogEx(FAILED
, "dump failed");
1878 print_sr_blocks(data
, cardsize
, card
.uid
, dense_output
);
1881 PrintAndLogEx(INFO
, "Called with no save option");
1882 PrintAndLogEx(NORMAL
, "");
1888 PrintAndLogEx(INFO
, "using UID as filename");
1889 char *fptr
= filename
+ snprintf(filename
, sizeof(filename
), "hf-14b-");
1890 FillFileNameByUID(fptr
, SwapEndian64(card
.uid
, card
.uidlen
, 8), "-dump", card
.uidlen
);
1893 size_t datalen
= (lastblock
+ 2) * ST25TB_SR_BLOCK_SIZE
;
1894 pm3_save_dump(filename
, data
, datalen
, jsf14b_v2
);
1900 static int CmdHF14BRestore(const char *Cmd
) {
1902 CLIParserContext
*ctx
;
1903 CLIParserInit(&ctx
, "hf 14b restore",
1904 "Restore data from (bin/eml/json) dump file to tag\n"
1905 "If the dump file includes the special block at the end it will be ignored\n",
1906 "hf 14b restore --4k -f myfilename\n"
1907 "hf 14b restore --512 -f myfilename\n"
1910 void *argtable
[] = {
1912 arg_str0("f", "file", "<fn>", "(optional) filename, if no <name> UID will be used as filename"),
1913 arg_lit0(NULL
, "512", "target SRI 512 tag"),
1914 arg_lit0(NULL
, "4k", "target SRIX 4k tag (def)"),
1917 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
1920 char filename
[FILE_PATH_SIZE
] = {0};
1921 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)filename
, FILE_PATH_SIZE
, &fnlen
);
1922 bool use_sri512
= arg_get_lit(ctx
, 2);
1923 bool use_srix4k
= arg_get_lit(ctx
, 3);
1926 if (use_sri512
+ use_srix4k
> 1) {
1927 PrintAndLogEx(FAILED
, "Select only one card type");
1932 if (use_sri512
== false) {
1937 memset(s
, 0, sizeof(s
));
1938 uint16_t block_cnt
= 0;
1941 memcpy(s
, "SRI512", 7);
1942 } else if (use_srix4k
) {
1944 memcpy(s
, "SRIX4K", 7);
1948 uint8_t *data
= NULL
;
1949 size_t bytes_read
= 0;
1950 int res
= pm3_load_dump(filename
, (void **)&data
, &bytes_read
, (ST25TB_SR_BLOCK_SIZE
* block_cnt
));
1951 if (res
!= PM3_SUCCESS
) {
1952 PrintAndLogEx(FAILED
, "Failed to load file");
1956 // Ignore remaining block if the file is 1 block larger than the expected size
1957 // (because hf 14b dump also saves the special block at the end of the memory, it will be ignored by the restore command)
1958 if (bytes_read
!= (block_cnt
* ST25TB_SR_BLOCK_SIZE
) && bytes_read
!= ((block_cnt
+ 1) * ST25TB_SR_BLOCK_SIZE
)) {
1959 PrintAndLogEx(ERR
, "File content error. Read %zu", bytes_read
);
1963 PrintAndLogEx(INFO
, "Copying to %s", s
);
1966 while (bytes_read
) {
1968 int status
= write_sr_block(blockno
, ST25TB_SR_BLOCK_SIZE
, data
+ blockno
* ST25TB_SR_BLOCK_SIZE
);
1969 if (status
!= PM3_SUCCESS
) {
1970 PrintAndLogEx(FAILED
, "Write failed");
1976 uint8_t out
[ST25TB_SR_BLOCK_SIZE
] = {0};
1977 status
= read_sr_block(blockno
, out
, sizeof(out
));
1978 if (status
== PM3_SUCCESS
) {
1979 if (memcmp(data
+ blockno
* ST25TB_SR_BLOCK_SIZE
, out
, ST25TB_SR_BLOCK_SIZE
) == 0) {
1981 PrintAndLogEx(INFO
, "SRx write block %d/%d ( " _GREEN_("ok") " )" NOLF
, blockno
, block_cnt
- 1);
1984 PrintAndLogEx(INFO
, "SRx write block %d/%d ( " _RED_("different") " )", blockno
, block_cnt
- 1);
1988 PrintAndLogEx(INFO
, "Verifying block %d/%d ( " _RED_("failed") " )", blockno
, block_cnt
- 1);
1993 bytes_read
-= ST25TB_SR_BLOCK_SIZE
;
1995 if (blockno
>= block_cnt
) break;
1998 PrintAndLogEx(NORMAL
, "\n");
2001 // confirm number written blocks.
2002 if (blockno
!= block_cnt
) {
2003 PrintAndLogEx(ERR
, "File content error. There must be %d blocks", block_cnt
);
2007 PrintAndLogEx(SUCCESS
, "Card loaded " _YELLOW_("%d") " blocks from file", block_cnt
);
2008 PrintAndLogEx(INFO
, "Done!");
2016 static uint32_t srix4kEncode(uint32_t value) {
2020 // 4 bytes : 00 1A 20 01
2021 // only the lower crumbs.
2022 uint8_t block = (value & 0xFF);
2024 uint8_t valuebytes[] = {0, 0, 0};
2025 Uint3byteToMemBe(valuebytes, value >> 8);
2027 uint32_t value_x = (value >> 8);
2028 PrintAndLogEx(INFO, "value...... %08x %06x", value, value_x);
2029 PrintAndLogEx(INFO, "3b value... %s", sprint_hex_inrow(valuebytes, sizeof(valuebytes)));
2030 PrintAndLogEx(INFO, "block no... %02x", block);
2033 // Crumb swapping of value.
2035 foo |= CRUMB(value_x, 22) << 28;
2036 foo |= CRUMB(value_x, 14) << 26;
2037 foo |= CRUMB(value_x, 6) << 24;
2039 foo |= CRUMB(value_x, 20) << 20;
2040 foo |= CRUMB(value_x, 12) << 18;
2041 foo |= CRUMB(value_x, 4) << 16;
2043 foo |= CRUMB(value_x, 18) << 12;
2044 foo |= CRUMB(value_x, 10) << 10;
2045 foo |= CRUMB(value_x, 2) << 8;
2047 foo |= CRUMB(value_x, 16) << 4;
2048 foo |= CRUMB(value_x, 8) << 2;
2049 foo |= CRUMB(value_x, 0) << 0;
2051 PrintAndLogEx(INFO, "hex........ %02x %02x %02x", CRUMB(value_x, 22), CRUMB(value_x, 14), CRUMB(value_x, 6));
2052 PrintAndLogEx(INFO, "hex........ %02x %02x %02x", CRUMB(value_x, 20), CRUMB(value_x, 12), CRUMB(value_x, 4));
2053 PrintAndLogEx(INFO, "hex........ %02x %02x %02x", CRUMB(value_x, 18), CRUMB(value_x, 10), CRUMB(value_x, 2));
2054 PrintAndLogEx(INFO, "hex........ %02x %02x %02x", CRUMB(value_x, 16), CRUMB(value_x, 8), CRUMB(value_x, 0));
2056 PrintAndLogEx(INFO, "hex........ %08x", foo);
2059 uint32_t chksum = 0xFF - block;
2061 // chksum is reduced by each nibbles of value.
2062 for (i = 0; i < 3; ++i) {
2063 chksum -= NIBBLE_HIGH(valuebytes[i]);
2064 chksum -= NIBBLE_LOW(valuebytes[i]);
2067 // base4 conversion and left shift twice
2069 uint8_t base4[] = {0, 0, 0, 0};
2070 while (chksum != 0) {
2071 base4[i--] = (chksum % 4 << 2);
2074 PrintAndLogEx(INFO, "%s", sprint_hex_inrow(base4, sizeof(base4)));
2076 // merge scambled and chksum parts
2077 uint32_t encvalue = 0;
2079 (NIBBLE_LOW(base4[0]) << 28) |
2080 (NIBBLE_HIGH(temp[0]) << 24) |
2082 (NIBBLE_LOW(base4[1]) << 20) |
2083 (NIBBLE_LOW(temp[0]) << 16) |
2085 (NIBBLE_LOW(base4[2]) << 12) |
2086 (NIBBLE_HIGH(temp[1]) << 8) |
2088 (NIBBLE_LOW(base4[3]) << 4) |
2089 NIBBLE_LOW(temp[1]);
2091 PrintAndLogEx(NORMAL, "ICE encoded | %08X -> %08X", value, encvalue);
2096 static uint32_t srix4k_decode_counter(uint32_t num
) {
2097 uint32_t value
= ~num
;
2102 static uint32_t srix4kDecode(uint32_t value) {
2115 static uint32_t srix4k_get_magicbytes(uint64_t uid
, uint32_t block6
, uint32_t block18
, uint32_t block19
) {
2116 #define MASK 0xFFFFFFFF;
2117 uint32_t uid32
= uid
& MASK
;
2118 uint32_t counter
= srix4k_decode_counter(block6
);
2119 uint32_t decodedBlock18
= 0;
2120 uint32_t decodedBlock19
= 0;
2121 // uint32_t decodedBlock18 = srix4kDecode(block18);
2122 // uint32_t decodedBlock19 = srix4kDecode(block19);
2123 uint32_t doubleBlock
= (decodedBlock18
<< 16 | decodedBlock19
) + 1;
2125 uint32_t result
= (uid32
* doubleBlock
* counter
) & MASK
;
2126 PrintAndLogEx(SUCCESS
, "Magic bytes | %08X", result
);
2130 static int CmdSRIX4kValid(const char *Cmd
) {
2131 CLIParserContext
*ctx
;
2132 CLIParserInit(&ctx
, "hf 14b valid",
2133 "SRIX checksum test",
2137 void *argtable
[] = {
2141 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
2144 uint64_t uid
= 0xD00202501A4532F9;
2145 uint32_t block6
= 0xFFFFFFFF;
2146 uint32_t block18
= 0xC04F42C5;
2147 uint32_t block19
= 0xC1484807;
2148 uint32_t block21
= 0xD1BCABA4;
2150 uint32_t test_b18
= 0x001A2001; // 0x00313918;
2151 uint32_t test_b18_enc
= 0;
2152 // uint32_t test_b18_enc = srix4kEncode(test_b18);
2153 // uint32_t test_b18_dec = srix4kDecode(test_b18_enc);
2154 PrintAndLogEx(SUCCESS
, "ENCODE & CHECKSUM | %08X -> %08X (%s)", test_b18
, test_b18_enc
, "");
2156 uint32_t magic
= srix4k_get_magicbytes(uid
, block6
, block18
, block19
);
2157 PrintAndLogEx(SUCCESS
, "BLOCK 21 | %08X -> %08X (no XOR)", block21
, magic
^ block21
);
2161 int select_card_14443b_4(bool disconnect
, iso14b_card_select_t
*card
) {
2163 memset(card
, 0, sizeof(iso14b_card_select_t
));
2166 switch_off_field_14b();
2168 iso14b_raw_cmd_t packet
= {
2169 .flags
= (ISO14B_CONNECT
| ISO14B_SELECT_STD
| ISO14B_CLEARTRACE
),
2173 // Anticollision + SELECT STD card
2174 PacketResponseNG resp
;
2175 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
2176 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
2178 PrintAndLogEx(INFO
, "Trying 14B Select SRx");
2179 // Anticollision + SELECT SR card
2180 packet
.flags
= (ISO14B_CONNECT
| ISO14B_SELECT_SR
| ISO14B_CLEARTRACE
);
2181 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
2182 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
2184 PrintAndLogEx(INFO
, "Trying 14B Select CTS");
2185 // Anticollision + SELECT ASK C-Ticket card
2186 packet
.flags
= (ISO14B_CONNECT
| ISO14B_SELECT_CTS
| ISO14B_CLEARTRACE
);
2187 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)&packet
, sizeof(iso14b_raw_cmd_t
));
2188 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, TIMEOUT
) == false) {
2189 PrintAndLogEx(ERR
, "connection timeout");
2190 switch_off_field_14b();
2197 if (resp
.status
!= PM3_SUCCESS
) {
2198 PrintAndLogEx(WARNING
, "No ISO14443-B Card in field");
2199 switch_off_field_14b();
2203 SetISODEPState(ISODEP_NFCB
);
2204 apdu_frame_length
= 0;
2205 // get frame length from ATS in card data structure
2206 iso14b_card_select_t
*vcard
= (iso14b_card_select_t
*) resp
.data
.asBytes
;
2207 // uint8_t fsci = vcard->atqb[1] & 0x0f;
2208 // if (fsci < ARRAYLEN(ats_fsc)) {
2209 // apdu_frame_length = ats_fsc[fsci];
2213 memcpy(card
, vcard
, sizeof(iso14b_card_select_t
));
2217 switch_off_field_14b();
2222 static int handle_14b_apdu(bool chainingin
, uint8_t *datain
, int datainlen
,
2223 bool activateField
, uint8_t *dataout
, int maxdataoutlen
,
2224 int *dataoutlen
, bool *chainingout
, int user_timeout
) {
2226 *chainingout
= false;
2228 if (activateField
) {
2229 // select with no disconnect and set frameLength
2230 int selres
= select_card_14443b_4(false, NULL
);
2231 if (selres
!= PM3_SUCCESS
) {
2236 iso14b_raw_cmd_t
*packet
= (iso14b_raw_cmd_t
*)calloc(1, sizeof(iso14b_raw_cmd_t
) + datainlen
);
2237 if (packet
== NULL
) {
2238 PrintAndLogEx(FAILED
, "APDU: failed to allocate memory");
2241 packet
->flags
= (ISO14B_APDU
);
2242 packet
->timeout
= 0;
2246 packet
->flags
= (ISO14B_SEND_CHAINING
| ISO14B_APDU
);
2249 if (user_timeout
> 0) {
2250 packet
->flags
|= ISO14B_SET_TIMEOUT
;
2251 if (user_timeout
> MAX_14B_TIMEOUT_MS
) {
2252 user_timeout
= MAX_14B_TIMEOUT_MS
;
2253 PrintAndLogEx(INFO
, "set timeout to 4.9 seconds. The max we can wait for response");
2257 packet
->timeout
= (uint32_t)((13560 / 128) * user_timeout
);
2260 // "Command APDU" length should be 5+255+1, but javacard's APDU buffer might be smaller - 133 bytes
2261 // https://stackoverflow.com/questions/32994936/safe-max-java-card-apdu-data-command-and-respond-size
2262 // here length PM3_CMD_DATA_SIZE=512
2264 packet
->rawlen
= datainlen
;
2265 memcpy(packet
->raw
, datain
, datainlen
);
2266 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)packet
, sizeof(iso14b_raw_cmd_t
) + packet
->rawlen
);
2268 SendCommandNG(CMD_HF_ISO14443B_COMMAND
, (uint8_t *)packet
, sizeof(iso14b_raw_cmd_t
));
2271 PacketResponseNG resp
;
2272 if (WaitForResponseTimeout(CMD_HF_ISO14443B_COMMAND
, &resp
, MAX(APDU_TIMEOUT
, user_timeout
)) == false) {
2273 PrintAndLogEx(ERR
, "APDU: reply timeout");
2274 return PM3_ETIMEOUT
;
2277 if (resp
.status
!= PM3_SUCCESS
) {
2278 PrintAndLogEx(ERR
, "APDU: no APDU response");
2282 iso14b_raw_apdu_response_t
*apdu
= (iso14b_raw_apdu_response_t
*)resp
.data
.asBytes
;
2285 int dlen
= apdu
->datalen
- 2;
2290 *dataoutlen
+= dlen
;
2292 if (maxdataoutlen
&& *dataoutlen
> maxdataoutlen
) {
2293 PrintAndLogEx(ERR
, "APDU: buffer too small ( " _RED_("%d") " ), needs " _YELLOW_("%d") " bytes", maxdataoutlen
, *dataoutlen
);
2298 if ((apdu
->response_byte
& 0xF2) == 0xA2) {
2300 *chainingout
= true;
2304 // check apdu length
2305 if (apdu
->datalen
< 2) {
2306 PrintAndLogEx(ERR
, "APDU: small APDU response, len " _RED_("%d"), apdu
->datalen
);
2310 // copy to output array
2311 memcpy(dataout
, apdu
->data
, dlen
);
2314 if ((apdu
->response_byte
& 0x10) != 0) {
2315 *chainingout
= true;
2320 int exchange_14b_apdu(uint8_t *datain
, int datainlen
, bool activate_field
,
2321 bool leave_signal_on
, uint8_t *dataout
, int maxdataoutlen
,
2322 int *dataoutlen
, int user_timeout
) {
2325 bool chaining
= false;
2328 // 3 byte here - 1b framing header, 2b crc16
2329 if (apdu_in_framing_enable
&&
2330 ((apdu_frame_length
&& (datainlen
> apdu_frame_length
- 3)) || (datainlen
> PM3_CMD_DATA_SIZE
- 3))) {
2333 bool v_activate_field
= activate_field
;
2336 int vlen
= MIN(apdu_frame_length
- 3, datainlen
- clen
);
2337 bool chainBlockNotLast
= ((clen
+ vlen
) < datainlen
);
2340 res
= handle_14b_apdu(chainBlockNotLast
, &datain
[clen
], vlen
, v_activate_field
, dataout
, maxdataoutlen
, dataoutlen
, &chaining
, user_timeout
);
2342 if (leave_signal_on
== false) {
2343 switch_off_field_14b();
2349 // TODO check this one...
2350 // check R-block ACK
2351 // *dataoutlen!=0. 'A && (!A || B)' is equivalent to 'A && B'
2352 if ((*dataoutlen
== 0) && (chaining
!= chainBlockNotLast
)) {
2353 if (leave_signal_on
== false) {
2354 switch_off_field_14b();
2360 v_activate_field
= false;
2362 if (clen
!= datainlen
) {
2363 PrintAndLogEx(ERR
, "APDU: I-block/R-block sequence error. Data len=%d, Sent=%d, Last packet len=%d", datainlen
, clen
, *dataoutlen
);
2367 } while (clen
< datainlen
);
2370 res
= handle_14b_apdu(false, datain
, datainlen
, activate_field
, dataout
, maxdataoutlen
, dataoutlen
, &chaining
, user_timeout
);
2371 if (res
!= PM3_SUCCESS
) {
2372 if (leave_signal_on
== false) {
2373 switch_off_field_14b();
2380 // I-block with chaining
2381 res
= handle_14b_apdu(false, NULL
, 0, false, &dataout
[*dataoutlen
], maxdataoutlen
, dataoutlen
, &chaining
, user_timeout
);
2382 if (res
!= PM3_SUCCESS
) {
2383 if (leave_signal_on
== false) {
2384 switch_off_field_14b();
2390 if (leave_signal_on
== false) {
2391 switch_off_field_14b();
2397 // ISO14443-4. 7. Half-duplex block transmission protocol
2398 static int CmdHF14BAPDU(const char *Cmd
) {
2399 CLIParserContext
*ctx
;
2400 CLIParserInit(&ctx
, "hf 14b apdu",
2401 "Sends an ISO 7816-4 APDU via ISO 14443-4 block transmission protocol (T=CL).\n"
2402 "works with all apdu types from ISO 7816-4:2013",
2403 "hf 14b apdu -s -d 94a40800043f000002\n"
2404 "hf 14b apdu -s --decode -d 00A404000E325041592E5359532E444446303100 -> decode apdu\n"
2405 "hf 14b apdu -sm 00A40400 -l 256 -d 325041592E5359532E4444463031 -> encode standard apdu\n"
2406 "hf 14b apdu -sm 00A40400 -el 65536 -d 325041592E5359532E4444463031 -> encode extended apdu\n");
2408 void *argtable
[] = {
2410 arg_lit0("s", "select", "activate field and select card"),
2411 arg_lit0("k", "keep", "leave the signal field ON after receive response"),
2412 arg_lit0("t", "tlv", "executes TLV decoder if it possible"),
2413 arg_lit0(NULL
, "decode", "decode apdu request if it possible"),
2414 arg_str0("m", "make", "<hex>", "make apdu with head from this field and data from data field.\n"
2415 " must be 4 bytes: <CLA INS P1 P2>"),
2416 arg_lit0("e", "extended", "make extended length apdu if `m` parameter included"),
2417 arg_int0("l", "le", "<int>", "Le apdu parameter if `m` parameter included"),
2418 arg_str1("d", "data", "<hex>", "<APDU | data> if `m` parameter included"),
2419 arg_int0(NULL
, "timeout", "<dec>", "timeout in ms"),
2422 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
2424 bool activate_field
= arg_get_lit(ctx
, 1);
2425 bool leave_signal_on
= arg_get_lit(ctx
, 2);
2426 bool decode_TLV
= arg_get_lit(ctx
, 3);
2427 bool decode_APDU
= arg_get_lit(ctx
, 4);
2429 uint8_t header
[PM3_CMD_DATA_SIZE
] = {0x00};
2431 CLIGetHexWithReturn(ctx
, 5, header
, &headerlen
);
2433 bool make_APDU
= (headerlen
> 0);
2434 if (make_APDU
&& headerlen
!= 4) {
2435 PrintAndLogEx(ERR
, "header length must be 4 bytes, got " _RED_("%d"), headerlen
);
2440 bool extended_APDU
= arg_get_lit(ctx
, 6);
2441 int le
= arg_get_int_def(ctx
, 7, 0);
2443 uint8_t data
[PM3_CMD_DATA_SIZE
] = {0x00};
2447 uint8_t apdudata
[PM3_CMD_DATA_SIZE
] = {0};
2448 int apdudatalen
= 0;
2450 CLIGetHexBLessWithReturn(ctx
, 8, apdudata
, &apdudatalen
, 1 + 2);
2453 apdu
.cla
= header
[0];
2454 apdu
.ins
= header
[1];
2455 apdu
.p1
= header
[2];
2456 apdu
.p2
= header
[3];
2458 apdu
.lc
= apdudatalen
;
2459 apdu
.data
= apdudata
;
2461 apdu
.extended_apdu
= extended_APDU
;
2464 if (APDUEncode(&apdu
, data
, &datalen
)) {
2465 PrintAndLogEx(ERR
, "can't make apdu with provided parameters.");
2471 if (extended_APDU
) {
2472 PrintAndLogEx(ERR
, "make mode not set but here `e` option.");
2477 PrintAndLogEx(ERR
, "make mode not set but here `l` option.");
2482 // len = data + PCB(1b) + CRC(2b)
2483 CLIGetHexBLessWithReturn(ctx
, 8, data
, &datalen
, 1 + 2);
2485 int user_timeout
= arg_get_int_def(ctx
, 9, -1);
2488 PrintAndLogEx(SUCCESS
, _YELLOW_("%s%s%s"),
2489 activate_field
? "select card" : "",
2490 leave_signal_on
? ", keep field on" : "",
2491 decode_TLV
? ", TLV" : ""
2493 PrintAndLogEx(SUCCESS
, ">>> %s", sprint_hex_inrow(data
, datalen
));
2497 if (APDUDecode(data
, datalen
, &apdu
) == 0)
2500 PrintAndLogEx(WARNING
, "can't decode APDU.");
2503 int res
= exchange_14b_apdu(data
, datalen
, activate_field
, leave_signal_on
, data
, PM3_CMD_DATA_SIZE
, &datalen
, user_timeout
);
2504 if (res
!= PM3_SUCCESS
) {
2508 PrintAndLogEx(INFO
, "<<<< %s - %s", sprint_hex_inrow(data
, datalen
), sprint_ascii(data
, datalen
));
2509 uint16_t sw
= get_sw(data
, datalen
);
2510 if (sw
!= ISO7816_OK
) {
2511 PrintAndLogEx(SUCCESS
, "APDU response: " _YELLOW_("%02x %02x") " - %s"
2514 , GetAPDUCodeDescription(data
[datalen
- 2], data
[datalen
- 1])
2517 PrintAndLogEx(SUCCESS
, "APDU response: " _GREEN_("%02x %02x") " - %s"
2520 , GetAPDUCodeDescription(data
[datalen
- 2], data
[datalen
- 1])
2525 if (decode_TLV
&& datalen
> 4) {
2526 TLVPrintFromBuffer(data
, datalen
- 2);
2532 int CmdHF14BNdefRead(const char *Cmd
) {
2534 CLIParserContext
*ctx
;
2535 CLIParserInit(&ctx
, "hf 14b ndefread",
2536 "Print NFC Data Exchange Format (NDEF)",
2538 "hf 14b ndefread -f myfilename -> save raw NDEF to file"
2540 void *argtable
[] = {
2542 arg_str0("f", "file", "<fn>", "Save raw NDEF to file"),
2543 arg_lit0("v", "verbose", "Verbose output"),
2546 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
2548 char filename
[FILE_PATH_SIZE
] = {0};
2549 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)filename
, FILE_PATH_SIZE
, &fnlen
);
2551 bool verbose
= arg_get_lit(ctx
, 2);
2554 bool activate_field
= true;
2555 bool keep_field_on
= true;
2556 uint8_t response
[PM3_CMD_DATA_SIZE
];
2559 // --------------- Select NDEF Tag application ----------------
2560 uint8_t aSELECT_AID
[80];
2561 int aSELECT_AID_n
= 0;
2562 param_gethex_to_eol("00a4040007d276000085010100", 0, aSELECT_AID
, sizeof(aSELECT_AID
), &aSELECT_AID_n
);
2563 int res
= exchange_14b_apdu(aSELECT_AID
, aSELECT_AID_n
, activate_field
, keep_field_on
, response
, sizeof(response
), &resplen
, -1);
2573 uint16_t sw
= get_sw(response
, resplen
);
2574 if (sw
!= ISO7816_OK
) {
2575 PrintAndLogEx(ERR
, "Selecting NDEF aid failed (%04x - %s).", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
2580 activate_field
= false;
2581 keep_field_on
= true;
2582 // --------------- Send CC select ----------------
2583 // --------------- Read binary ----------------
2585 // --------------- NDEF file reading ----------------
2586 uint8_t aSELECT_FILE_NDEF
[30];
2587 int aSELECT_FILE_NDEF_n
= 0;
2588 param_gethex_to_eol("00a4000c020001", 0, aSELECT_FILE_NDEF
, sizeof(aSELECT_FILE_NDEF
), &aSELECT_FILE_NDEF_n
);
2589 res
= exchange_14b_apdu(aSELECT_FILE_NDEF
, aSELECT_FILE_NDEF_n
, activate_field
, keep_field_on
, response
, sizeof(response
), &resplen
, -1);
2593 sw
= get_sw(response
, resplen
);
2594 if (sw
!= ISO7816_OK
) {
2595 PrintAndLogEx(ERR
, "Selecting NDEF file failed (%04x - %s).", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
2600 // --------------- Read binary ----------------
2601 uint8_t aREAD_NDEF
[30];
2602 int aREAD_NDEF_n
= 0;
2603 param_gethex_to_eol("00b0000002", 0, aREAD_NDEF
, sizeof(aREAD_NDEF
), &aREAD_NDEF_n
);
2604 res
= exchange_14b_apdu(aREAD_NDEF
, aREAD_NDEF_n
, activate_field
, keep_field_on
, response
, sizeof(response
), &resplen
, -1);
2609 sw
= get_sw(response
, resplen
);
2610 if (sw
!= ISO7816_OK
) {
2611 PrintAndLogEx(ERR
, "reading NDEF file failed (%04x - %s).", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
2615 // take offset from response
2616 uint8_t offset
= response
[1];
2618 // --------------- Read binary w offset ----------------
2619 keep_field_on
= false;
2621 param_gethex_to_eol("00b00002", 0, aREAD_NDEF
, sizeof(aREAD_NDEF
), &aREAD_NDEF_n
);
2622 aREAD_NDEF
[4] = offset
;
2623 res
= exchange_14b_apdu(aREAD_NDEF
, aREAD_NDEF_n
, activate_field
, keep_field_on
, response
, sizeof(response
), &resplen
, -1);
2628 sw
= get_sw(response
, resplen
);
2629 if (sw
!= ISO7816_OK
) {
2630 PrintAndLogEx(ERR
, "reading NDEF file failed (%04x - %s).", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
2635 // get total NDEF length before save. If fails, we save it all
2637 if (NDEFGetTotalLength(response
+ 2, resplen
- 4, &n
) != PM3_SUCCESS
)
2640 pm3_save_dump(filename
, response
+ 2, n
, jsfNDEF
);
2642 res
= NDEFRecordsDecodeAndPrint(response
+ 2, resplen
- 4, verbose
);
2645 switch_off_field_14b();
2649 static int CmdHF14BView(const char *Cmd
) {
2651 CLIParserContext
*ctx
;
2652 CLIParserInit(&ctx
, "hf 14b view",
2653 "Print a ISO14443-B dump file (bin/eml/json)\n"
2655 " - command expects the filename to contain a UID\n"
2656 " which is needed to determine card memory type",
2657 "hf 14b view -f hf-14b-01020304-dump.bin"
2659 void *argtable
[] = {
2661 arg_str1("f", "file", "<fn>", "Specify a filename for dump file"),
2662 arg_lit0("v", "verbose", "verbose output"),
2663 arg_lit0("z", "dense", "dense dump output style"),
2666 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
2668 char filename
[FILE_PATH_SIZE
];
2669 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)filename
, FILE_PATH_SIZE
, &fnlen
);
2670 bool verbose
= arg_get_lit(ctx
, 2);
2671 bool dense_output
= (g_session
.dense_output
|| arg_get_lit(ctx
, 3));
2675 uint8_t *dump
= NULL
;
2676 size_t bytes_read
= (ST25TB_SR_BLOCK_SIZE
* 0x100);
2677 int res
= pm3_load_dump(filename
, (void **)&dump
, &bytes_read
, (ST25TB_SR_BLOCK_SIZE
* 0x100));
2678 if (res
!= PM3_SUCCESS
) {
2682 uint16_t block_cnt
= bytes_read
/ ST25TB_SR_BLOCK_SIZE
;
2685 PrintAndLogEx(INFO
, "File size %zu bytes, file blocks %d (0x%x)", bytes_read
, block_cnt
, block_cnt
);
2688 // figure out a way to identify the different dump files.
2689 // STD/SR/CT is difference
2690 print_sr_blocks(dump
, bytes_read
, get_uid_from_filename(filename
), dense_output
);
2691 //print_std_blocks(dump, bytes_read);
2692 //print_ct_blocks(dump, bytes_read);
2698 static int CmdHF14BCalypsoRead(const char *Cmd
) {
2700 CLIParserContext
*ctx
;
2701 CLIParserInit(&ctx
, "hf 14b calypso",
2702 "Reads out the contents of a ISO14443B Calypso card\n",
2705 void *argtable
[] = {
2709 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
2712 transport_14b_apdu_t cmds
[] = {
2713 {"01.Select ICC file", "\x94\xa4\x08\x00\x04\x3f\x00\x00\x02", 9},
2714 {"02.ICC", "\x94\xb2\x01\x04\x1d", 5},
2715 {"03.Select EnvHol file", "\x94\xa4\x08\x00\x04\x20\x00\x20\x01", 9},
2716 {"04.EnvHol1", "\x94\xb2\x01\x04\x1d", 5},
2717 {"05.Select EvLog file", "\x94\xa4\x08\x00\x04\x20\x00\x20\x10", 9},
2718 {"06.EvLog1", "\x94\xb2\x01\x04\x1d", 5},
2719 {"07.EvLog2", "\x94\xb2\x02\x04\x1d", 5},
2720 {"08.EvLog3", "\x94\xb2\x03\x04\x1d", 5},
2721 {"09.Select ConList file", "\x94\xa4\x08\x00\x04\x20\x00\x20\x50", 9},
2722 {"10.ConList", "\x94\xb2\x01\x04\x1d", 5},
2723 {"11.Select Contra file", "\x94\xa4\x08\x00\x04\x20\x00\x20\x20", 9},
2724 {"12.Contra1", "\x94\xb2\x01\x04\x1d", 5},
2725 {"13.Contra2", "\x94\xb2\x02\x04\x1d", 5},
2726 {"14.Contra3", "\x94\xb2\x03\x04\x1d", 5},
2727 {"15.Contra4", "\x94\xb2\x04\x04\x1d", 5},
2728 {"16.Select Counter file", "\x94\xa4\x08\x00\x04\x20\x00\x20\x69", 9},
2729 {"17.Counter", "\x94\xb2\x01\x04\x1d", 5},
2730 {"18.Select SpecEv file", "\x94\xa4\x08\x00\x04\x20\x00\x20\x40", 9},
2731 {"19.SpecEv1", "\x94\xb2\x01\x04\x1d", 5},
2736 local _calypso_cmds = {
2738 -- Break down of command bytes:
2741 -- 0x3F = master file
2742 -- 0x00 = master file id, is constant to 0x00.
2744 -- DF Dedicated File 38nn
2745 -- can be seen as directories
2748 -- ["01.Select ICC file"] = '0294 a4 080004 3f00 0002',
2750 -- EF Elementary File
2754 -- Electronic deposit file
2755 -- Electronic Purse file
2756 -- Electronic Transaction log file
2758 bool activate_field
= true;
2759 bool leave_signal_on
= true;
2760 uint8_t response
[PM3_CMD_DATA_SIZE
] = { 0x00 };
2762 for (int i
= 0; i
< ARRAYLEN(cmds
); i
++) {
2764 int user_timeout
= -1;
2766 int res
= exchange_14b_apdu(
2767 (uint8_t *)cmds
[i
].apdu
,
2777 if (res
!= PM3_SUCCESS
) {
2778 PrintAndLogEx(FAILED
, "sending command failed, aborting!");
2779 switch_off_field_14b();
2783 uint16_t sw
= get_sw(response
, resplen
);
2784 if (sw
!= ISO7816_OK
) {
2785 PrintAndLogEx(ERR
, "Sending command failed (%04x - %s).", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
2786 switch_off_field_14b();
2790 PrintAndLogEx(INFO
, "%s - %s", cmds
[i
].desc
, sprint_hex(response
, resplen
));
2791 activate_field
= false;
2794 switch_off_field_14b();
2798 static int CmdHF14BMobibRead(const char *Cmd
) {
2800 CLIParserContext
*ctx
;
2801 CLIParserInit(&ctx
, "hf 14b mobib",
2802 "Reads out the contents of a ISO14443B Mobib card\n",
2805 void *argtable
[] = {
2809 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
2812 transport_14b_apdu_t cmds
[] = {
2813 {"01.SELECT AID 1TIC.ICA", "\x00\xa4\x04\x00\x08\x31\x54\x49\x43\x2e\x49\x43\x41", 13},
2814 {"02.Select ICC file a", "\x00\xa4\x00\x00\x02\x3f\x00", 7},
2815 {"03.Select ICC file b", "\x00\xa4\x00\x00\x02\x00\x02", 7},
2816 {"04.ICC", "\x00\xb2\x01\x04\x1d", 5},
2817 {"05.Select Holder file", "\x00\xa4\x00\x00\x02\x3f\x1c", 7},
2818 {"06.Holder1", "\x00\xb2\x01\x04\x1d", 5},
2819 {"07.Holder2", "\x00\xb2\x02\x04\x1d", 5},
2820 {"08.Select EnvHol file a", "\x00\xa4\x00\x00\x00", 5},
2821 {"09.Select EnvHol file b", "\x00\xa4\x00\x00\x02\x20\x00", 7},
2822 {"10.Select EnvHol file c", "\x00\xa4\x00\x00\x02\x20\x01", 7},
2823 {"11.EnvHol1", "\x00\xb2\x01\x04\x1D", 5},
2824 {"11.EnvHol2", "\x00\xb2\x02\x04\x1D", 5},
2825 {"12.Select EvLog file", "\x00\xa4\x00\x00\x02\x20\x10", 7},
2826 {"13.EvLog1", "\x00\xb2\x01\x04\x1D", 5},
2827 {"14.EvLog2", "\x00\xb2\x02\x04\x1D", 5},
2828 {"15.EvLog3", "\x00\xb2\x03\x04\x1D", 5},
2829 {"16.Select ConList file", "\x00\xa4\x00\x00\x02\x20\x50", 7},
2830 {"17.ConList", "\x00\xb2\x01\x04\x1D", 5},
2831 {"18.Select Contra file", "\x00\xa4\x00\x00\x02\x20\x20", 7},
2832 {"19.Contra1", "\x00\xb2\x01\x04\x1D", 5},
2833 {"20.Contra2", "\x00\xb2\x02\x04\x1D", 5},
2834 {"21.Contra3", "\x00\xb2\x03\x04\x1D", 5},
2835 {"22.Contra4", "\x00\xb2\x04\x04\x1D", 5},
2836 {"23.Contra5", "\x00\xb2\x05\x04\x1D", 5},
2837 {"24.Contra6", "\x00\xb2\x06\x04\x1D", 5},
2838 {"25.Contra7", "\x00\xb2\x07\x04\x1D", 5},
2839 {"26.Contra8", "\x00\xb2\x08\x04\x1D", 5},
2840 {"27.Contra9", "\x00\xb2\x09\x04\x1D", 5},
2841 {"28.ContraA", "\x00\xb2\x0a\x04\x1D", 5},
2842 {"29.ContraB", "\x00\xb2\x0b\x04\x1D", 5},
2843 {"30.ContraC", "\x00\xb2\x0c\x04\x1D", 5},
2844 {"31.Select Counter file", "\x00\xa4\x00\x00\x02\x20\x69", 7},
2845 {"32.Counter", "\x00\xb2\x01\x04\x1D", 5},
2846 {"33.Select LoadLog file a", "\x00\xa4\x00\x00\x00", 5},
2847 {"34.Select LoadLog file b", "\x00\xa4\x00\x00\x02\x10\x00", 7},
2848 {"35.Select LoadLog file c", "\x00\xa4\x00\x00\x02\x10\x14", 7},
2849 {"36.LoadLog", "\x00\xb2\x01\x04\x1D", 5},
2850 {"37.Select Purcha file", "\x00\xa4\x00\x00\x02\x10\x15", 7},
2851 {"38.Purcha1", "\x00\xb2\x01\x04\x1D", 5},
2852 {"39.Purcha2", "\x00\xb2\x02\x04\x1D", 5},
2853 {"40.Purcha3", "\x00\xb2\x03\x04\x1D", 5},
2854 {"41.Select SpecEv file a", "\x00\xa4\x00\x00\x00", 5},
2855 {"42.Select SpecEv file b", "\x00\xa4\x00\x00\x02\x20\x00", 7},
2856 {"43.Select SpecEv file c", "\x00\xa4\x00\x00\x02\x20\x40", 7},
2857 {"44.SpecEv1", "\x00\xb2\x01\x04\x1D", 5},
2858 {"45.SpecEv2", "\x00\xb2\x02\x04\x1D", 5},
2859 {"46.SpecEv3", "\x00\xb2\x03\x04\x1D", 5},
2860 {"47.SpecEv4", "\x00\xb2\x04\x04\x1d", 5},
2863 bool activate_field
= true;
2864 bool leave_signal_on
= true;
2865 uint8_t response
[PM3_CMD_DATA_SIZE
] = { 0x00 };
2867 for (int i
= 0; i
< ARRAYLEN(cmds
); i
++) {
2869 int user_timeout
= -1;
2871 int res
= exchange_14b_apdu(
2872 (uint8_t *)cmds
[i
].apdu
,
2882 if (res
!= PM3_SUCCESS
) {
2883 PrintAndLogEx(FAILED
, "sending command failed, aborting!");
2884 switch_off_field_14b();
2888 uint16_t sw
= get_sw(response
, resplen
);
2889 if (sw
!= ISO7816_OK
) {
2890 PrintAndLogEx(ERR
, "Sending command failed (%04x - %s).", sw
, GetAPDUCodeDescription(sw
>> 8, sw
& 0xff));
2891 switch_off_field_14b();
2895 PrintAndLogEx(INFO
, "%s - %s", cmds
[i
].desc
, sprint_hex(response
, resplen
));
2896 activate_field
= false;
2899 switch_off_field_14b();
2903 static int CmdHF14BSetUID(const char *Cmd
) {
2905 CLIParserContext
*ctx
;
2906 CLIParserInit(&ctx
, "hf 14b setuid",
2907 "Set UID for magic card (only works with such cards)\n",
2908 "hf 14b setuid -u 11223344\n"
2911 void *argtable
[] = {
2913 arg_str1("u", "uid", "<hex>", "UID, 4 hex bytes"),
2916 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
2918 uint8_t uid
[20] = {0};
2920 CLIGetHexWithReturn(ctx
, 1, uid
, &uidlen
);
2924 PrintAndLogEx(WARNING
, "UID len must be 4 bytes, got " _RED_("%i"), uidlen
);
2928 uint8_t select
[sizeof(iso14b_card_select_t
)] = {0};
2929 iso14b_type_t select_cardtype
= ISO14B_NONE
;
2930 if (get_14b_UID(select
, &select_cardtype
) == false) {
2931 PrintAndLogEx(WARNING
, "no tag found");
2935 if (select_cardtype
!= ISO14B_STANDARD
) {
2936 PrintAndLogEx(FAILED
, "None supported tag");
2937 return switch_off_field_14b();
2940 iso14b_card_select_t
*card
= (iso14b_card_select_t
*)select
;
2941 if (memcmp(card
->atqb
, "\x54\x43\x4F\x53", 4)) {
2942 PrintAndLogEx(FAILED
, "None supported tag");
2943 PrintAndLogEx(NORMAL
, "");
2944 return switch_off_field_14b();
2948 uint8_t out
[PM3_CMD_DATA_SIZE
] = {0};
2949 uint8_t tcos_version
[] = {0x90, 0xB2, 0x90, 0x00, 0x00};
2950 if (exchange_14b_apdu(tcos_version
, sizeof(tcos_version
), true, false, out
, PM3_CMD_DATA_SIZE
, &outlen
, -1) != PM3_SUCCESS
) {
2951 PrintAndLogEx(FAILED
, "None supported tag");
2955 uint8_t cmd
[] = { 0x90, 0xF8, 0xEE, 0xEE, 0x0B, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
2956 memcpy(cmd
+ 6, uid
, uidlen
);
2957 if (exchange_14b_apdu(cmd
, sizeof(cmd
), true, false, out
, PM3_CMD_DATA_SIZE
, &outlen
, -1) != PM3_SUCCESS
) {
2958 PrintAndLogEx(WARNING
, "timeout while waiting for reply");
2962 PrintAndLogEx(INFO
, "Verifying...");
2965 if (get_14b_UID(select
, &select_cardtype
) == false) {
2966 PrintAndLogEx(WARNING
, "no tag found");
2970 if (memcmp(card
->uid
, uid
, uidlen
) == 0) {
2971 PrintAndLogEx(SUCCESS
, "Setting new UID ( " _GREEN_("ok") " )");
2972 PrintAndLogEx(HINT
, "try `" _YELLOW_("hf 14b reader") "` to verify");
2973 PrintAndLogEx(NORMAL
, "");
2974 return PM3_SUCCESS
;;
2977 PrintAndLogEx(FAILED
, "Setting new UID ( " _RED_("fail") " )");
2978 PrintAndLogEx(NORMAL
, "");
2982 static command_t CommandTable
[] = {
2983 {"---------", CmdHelp
, AlwaysAvailable
, "----------------------- " _CYAN_("General") " -----------------------"},
2984 {"help", CmdHelp
, AlwaysAvailable
, "This help"},
2985 {"list", CmdHF14BList
, AlwaysAvailable
, "List ISO-14443-B history"},
2986 {"---------", CmdHelp
, AlwaysAvailable
, "----------------------- " _CYAN_("Operations") " -----------------------"},
2987 {"apdu", CmdHF14BAPDU
, IfPm3Iso14443b
, "Send ISO 14443-4 APDU to tag"},
2988 {"dump", CmdHF14BDump
, IfPm3Iso14443b
, "Read all memory pages of an ISO-14443-B tag, save to file"},
2989 {"info", CmdHF14Binfo
, IfPm3Iso14443b
, "Tag information"},
2990 {"ndefread", CmdHF14BNdefRead
, IfPm3Iso14443b
, "Read NDEF file on tag"},
2991 {"raw", CmdHF14BRaw
, IfPm3Iso14443b
, "Send raw hex data to tag"},
2992 {"rdbl", CmdHF14BSriRdBl
, IfPm3Iso14443b
, "Read SRI512/SRIX4 block"},
2993 {"reader", CmdHF14BReader
, IfPm3Iso14443b
, "Act as a ISO-14443-B reader to identify a tag"},
2994 {"restore", CmdHF14BRestore
, IfPm3Iso14443b
, "Restore from file to all memory pages of an ISO-14443-B tag"},
2995 {"sim", CmdHF14BSim
, IfPm3Iso14443b
, "Fake ISO ISO-14443-B tag"},
2996 {"sniff", CmdHF14BSniff
, IfPm3Iso14443b
, "Eavesdrop ISO-14443-B"},
2997 {"wrbl", CmdHF14BSriWrbl
, IfPm3Iso14443b
, "Write data to a SRI512/SRIX4 tag"},
2998 {"view", CmdHF14BView
, AlwaysAvailable
, "Display content from tag dump file"},
2999 {"valid", CmdSRIX4kValid
, AlwaysAvailable
, "SRIX4 checksum test"},
3000 {"---------", CmdHelp
, AlwaysAvailable
, "------------------ " _CYAN_("Calypso / Mobib") " ------------------"},
3001 {"calypso", CmdHF14BCalypsoRead
, IfPm3Iso14443b
, "Read contents of a Calypso card"},
3002 {"mobib", CmdHF14BMobibRead
, IfPm3Iso14443b
, "Read contents of a Mobib card"},
3003 {"---------", CmdHelp
, IfPm3Iso14443b
, "------------------------- " _CYAN_("Magic") " -----------------------"},
3004 {"setuid", CmdHF14BSetUID
, IfPm3Iso14443b
, "Set UID for magic card"},
3005 {NULL
, NULL
, NULL
, NULL
}
3008 static int CmdHelp(const char *Cmd
) {
3009 (void)Cmd
; // Cmd is not used so far
3010 CmdsHelp(CommandTable
);
3014 int CmdHF14B(const char *Cmd
) {
3015 clearCommandBuffer();
3016 return CmdsParse(CommandTable
, Cmd
);
3019 // get and print all info known about any known 14b tag
3020 int infoHF14B(bool verbose
, bool do_aid_search
) {
3022 // try std 14b (atqb)
3023 if (HF14B_Std_Info(verbose
, do_aid_search
))
3027 if (HF14B_ST_Info(verbose
, do_aid_search
))
3030 // try unknown 14b read commands (to be identified later)
3031 // could be read of calypso, CEPAS, moneo, or pico pass.
3032 if (verbose
) PrintAndLogEx(FAILED
, "no 14443-B tag found");
3033 return PM3_EOPABORTED
;
3036 // get and print general info about all known 14b chips
3037 int readHF14B(bool loop
, bool verbose
, bool read_plot
) {
3040 int res
= PM3_SUCCESS
;
3044 // try std 14b (atqb)
3045 found
|= HF14B_std_reader(verbose
);
3049 // try ST Microelectronics 14b
3050 found
|= HF14B_st_reader(verbose
);
3055 found
|= HF14B_picopass_reader(verbose
, info
);
3060 found
|= HF14B_ask_ct_reader(verbose
);
3064 // try unknown 14b read commands (to be identified later)
3065 // could be read of calypso, CEPAS, moneo, or pico pass.
3066 found
|= HF14B_other_reader(verbose
);
3071 res
= handle_hf_plot(verbose
);
3072 if (res
!= PM3_SUCCESS
) {
3073 PrintAndLogEx(DEBUG
, "plot failed");
3077 } while (loop
&& kbd_enter_pressed() == false);
3079 if (verbose
&& found
== false) {
3080 PrintAndLogEx(FAILED
, "no ISO 14443-B tag found");
3082 return (found
) ? PM3_SUCCESS
: PM3_EOPABORTED
;