1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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 // High frequency ISO14443B commands
9 //-----------------------------------------------------------------------------
16 #include "iso14443crc.h"
17 #include "proxmark3.h"
22 #include "cmdparser.h"
26 static int CmdHelp(const char *Cmd
);
28 int CmdHF14BList(const char *Cmd
)
30 PrintAndLog("Deprecated command, use 'hf list 14b' instead");
35 int CmdHF14BSim(const char *Cmd
)
37 UsbCommand c
={CMD_SIMULATE_TAG_ISO_14443B
};
42 int CmdHF14BSnoop(const char *Cmd
)
44 UsbCommand c
= {CMD_SNOOP_ISO_14443B
};
49 /* New command to read the contents of a SRI512 tag
50 * SRI512 tags are ISO14443-B modulated memory tags,
51 * this command just dumps the contents of the memory
53 int CmdSri512Read(const char *Cmd
)
55 UsbCommand c
= {CMD_READ_SRI512_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
60 /* New command to read the contents of a SRIX4K tag
61 * SRIX4K tags are ISO14443-B modulated memory tags,
62 * this command just dumps the contents of the memory/
64 int CmdSrix4kRead(const char *Cmd
)
66 UsbCommand c
= {CMD_READ_SRIX4K_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
71 int CmdHF14BCmdRaw (const char *cmd
) {
74 UsbCommand c
= {CMD_ISO_14443B_COMMAND
, {0, 0, 0}}; // len,recv?
80 uint8_t data
[100] = {0x00};
81 unsigned int datalen
=0, temp
;
85 PrintAndLog("Usage: hf 14b raw [-r] [-c] [-p] <0A 0B 0C ... hex>");
86 PrintAndLog(" -r do not read response");
87 PrintAndLog(" -c calculate and append CRC");
88 PrintAndLog(" -p leave the field on after receive");
93 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
95 while (cmd
[i
]!='\0') {
96 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
112 PrintAndLog("Invalid option");
118 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
119 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
120 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
121 buf
[strlen(buf
)+1]=0;
122 buf
[strlen(buf
)]=cmd
[i
];
125 if (strlen(buf
)>=2) {
126 sscanf(buf
,"%x",&temp
);
127 data
[datalen
]=(uint8_t)(temp
& 0xff);
133 PrintAndLog("Invalid char on input");
138 PrintAndLog("Missing data input");
143 uint8_t first
, second
;
144 ComputeCrc14443(CRC_14443_B
, data
, datalen
, &first
, &second
);
145 data
[datalen
++] = first
;
146 data
[datalen
++] = second
;
152 memcpy(c
.d
.asBytes
,data
,datalen
);
157 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
158 recv
= resp
.d
.asBytes
;
159 PrintAndLog("received %i octets",resp
.arg
[0]);
162 hexout
= (char *)malloc(resp
.arg
[0] * 3 + 1);
163 if (hexout
!= NULL
) {
164 uint8_t first
, second
;
165 for (int i
= 0; i
< resp
.arg
[0]; i
++) { // data in hex
166 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
168 PrintAndLog("%s", hexout
);
170 if (resp
.arg
[0] > 2) {
171 ComputeCrc14443(CRC_14443_B
, recv
, resp
.arg
[0]-2, &first
, &second
);
172 if(recv
[resp
.arg
[0]-2]==first
&& recv
[resp
.arg
[0]-1]==second
) {
173 PrintAndLog("CRC OK");
175 PrintAndLog("CRC failed");
179 PrintAndLog("malloc failed your client has low memory?");
182 PrintAndLog("timeout while waiting for reply.");
188 int CmdHF14BWrite( const char *Cmd
){
191 * For SRIX4K blocks 00 - 7F
192 * hf 14b raw -c -p 09 $srix4kwblock $srix4kwdata
194 * For SR512 blocks 00 - 0F
195 * hf 14b raw -c -p 09 $sr512wblock $sr512wdata
197 * Special block FF = otp_lock_reg block.
200 char cmdp
= param_getchar(Cmd
, 0);
201 uint8_t blockno
= -1;
202 uint8_t data
[4] = {0x00};
203 bool isSrix4k
= true;
206 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
207 PrintAndLog("Usage: hf 14b write <1|2> <BLOCK> <DATA>");
208 PrintAndLog(" [1 = SRIX4K]");
209 PrintAndLog(" [2 = SRI512]");
210 PrintAndLog(" [BLOCK number depends on tag, special block == FF]");
211 PrintAndLog(" sample: hf 14b write 1 7F 11223344");
212 PrintAndLog(" : hf 14b write 1 FF 11223344");
213 PrintAndLog(" : hf 14b write 2 15 11223344");
214 PrintAndLog(" : hf 14b write 2 FF 11223344");
221 //blockno = param_get8(Cmd, 1);
223 if ( param_gethex(Cmd
,1, &blockno
, 2) ) {
224 PrintAndLog("Block number must include 2 HEX symbols");
229 if ( blockno
> 0x7f && blockno
!= 0xff ){
230 PrintAndLog("Block number out of range");
234 if ( blockno
> 0x0f && blockno
!= 0xff ){
235 PrintAndLog("Block number out of range");
240 if (param_gethex(Cmd
, 2, data
, 8)) {
241 PrintAndLog("Data must include 8 HEX symbols");
245 if ( blockno
== 0xff)
246 PrintAndLog("[%s] Write special block %02X [ %s ]", (isSrix4k
)?"SRIX4K":"SRI512" , blockno
, sprint_hex(data
,4) );
248 PrintAndLog("[%s] Write block %02X [ %s ]", (isSrix4k
)?"SRIX4K":"SRI512", blockno
, sprint_hex(data
,4) );
250 sprintf(str
, "-c 09 %02x %02x%02x%02x%02x", blockno
, data
[0], data
[1], data
[2], data
[3]);
256 static command_t CommandTable
[] =
258 {"help", CmdHelp
, 1, "This help"},
259 {"list", CmdHF14BList
, 0, "[Deprecated] List ISO 14443b history"},
260 {"sim", CmdHF14BSim
, 0, "Fake ISO 14443B tag"},
261 {"snoop", CmdHF14BSnoop
, 0, "Eavesdrop ISO 14443B"},
262 {"sri512read", CmdSri512Read
, 0, "Read contents of a SRI512 tag"},
263 {"srix4kread", CmdSrix4kRead
, 0, "Read contents of a SRIX4K tag"},
264 {"raw", CmdHF14BCmdRaw
, 0, "Send raw hex data to tag"},
265 {"write", CmdHF14BWrite
, 0, "Write data to a SRI512 | SRIX4K tag"},
266 {NULL
, NULL
, 0, NULL
}
269 int CmdHF14B(const char *Cmd
)
271 CmdsParse(CommandTable
, Cmd
);
275 int CmdHelp(const char *Cmd
)
277 CmdsHelp(CommandTable
);