]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf14b.c
CHG: some minor changes in the ouput from hf 14b raw.
[proxmark3-svn] / client / cmdhf14b.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// High frequency ISO14443B commands
9//-----------------------------------------------------------------------------
10
7fe9b0b7 11#include <stdio.h>
12#include <stdlib.h>
13#include <stdbool.h>
17ad0e09 14//#include <string.h>
7fe9b0b7 15#include <stdint.h>
16#include "iso14443crc.h"
902cb3c0 17#include "proxmark3.h"
7fe9b0b7 18#include "data.h"
19#include "graph.h"
3fe4ff4f 20#include "util.h"
7fe9b0b7 21#include "ui.h"
22#include "cmdparser.h"
23#include "cmdhf14b.h"
7cf3ef20 24#include "cmdmain.h"
d71d59db 25#include "cmdhf14a.h"
7fe9b0b7 26
27static int CmdHelp(const char *Cmd);
28
7fe9b0b7 29int CmdHF14BList(const char *Cmd)
30{
388c92bd 31 PrintAndLog("Deprecated command, use 'hf list 14b' instead");
7fe9b0b7 32
388c92bd 33 return 0;
7fe9b0b7 34}
7fe9b0b7 35
22e24700 36int CmdHF14BSim(const char *Cmd)
7fe9b0b7 37{
6de14cec 38 UsbCommand c={CMD_SIMULATE_TAG_ISO_14443B};
17ad0e09 39 clearCommandBuffer();
22e24700 40 SendCommand(&c);
41 return 0;
7fe9b0b7 42}
43
44int CmdHF14BSnoop(const char *Cmd)
45{
132a0217 46 UsbCommand c = {CMD_SNOOP_ISO_14443B};
17ad0e09 47 clearCommandBuffer();
22e24700 48 SendCommand(&c);
49 return 0;
7fe9b0b7 50}
51
52/* New command to read the contents of a SRI512 tag
53 * SRI512 tags are ISO14443-B modulated memory tags,
54 * this command just dumps the contents of the memory
55 */
56int CmdSri512Read(const char *Cmd)
57{
22e24700 58 UsbCommand c = {CMD_READ_SRI512_TAG, {strtol(Cmd, NULL, 0), 0, 0}};
abb21530 59 clearCommandBuffer();
22e24700 60 SendCommand(&c);
61 return 0;
7fe9b0b7 62}
63
64/* New command to read the contents of a SRIX4K tag
65 * SRIX4K tags are ISO14443-B modulated memory tags,
66 * this command just dumps the contents of the memory/
67 */
68int CmdSrix4kRead(const char *Cmd)
69{
22e24700 70 UsbCommand c = {CMD_READ_SRIX4K_TAG, {strtol(Cmd, NULL, 0), 0, 0}};
abb21530 71 clearCommandBuffer();
22e24700 72 SendCommand(&c);
73 return 0;
7fe9b0b7 74}
75
d71d59db 76int rawClose(void){
d71d59db 77 UsbCommand c = {CMD_ISO_14443B_COMMAND, {0, 0, 0}};
abb21530 78 clearCommandBuffer();
d71d59db 79 SendCommand(&c);
d71d59db 80 return 0;
81}
82
22e24700 83int HF14BCmdRaw(bool reply, bool *crc, bool power, uint8_t *data, uint8_t *datalen, bool verbose){
6de14cec 84
85 if(*crc) {
86 ComputeCrc14443(CRC_14443_B, data, *datalen, data+*datalen, data+*datalen+1);
22e24700 87 *datalen += 2;
88 }
89
6de14cec 90 UsbCommand c = {CMD_ISO_14443B_COMMAND, {0, 0, 0}}; // len,recv,power
22e24700 91 c.arg[0] = *datalen;
92 c.arg[1] = reply;
93 c.arg[2] = power;
6de14cec 94 memcpy(c.d.asBytes, data, *datalen);
abb21530 95 clearCommandBuffer();
22e24700 96 SendCommand(&c);
97
98 if (!reply) return 1;
99
6de14cec 100 UsbCommand resp;
17ad0e09 101 if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
22e24700 102 if (verbose) PrintAndLog("timeout while waiting for reply.");
17ad0e09 103 return 0;
104 }
22e24700 105
6de14cec 106 *datalen = resp.arg[0];
107 if ( *datalen < 3 ) return 0;
17ad0e09 108
6de14cec 109 memcpy(data, resp.d.asBytes, *datalen);
110
111 uint8_t first = 0, second = 0;
112 ComputeCrc14443(CRC_14443_B, data, *datalen-2, &first, &second);
113 *crc = ( data[*datalen-2] == first && data[*datalen-1] == second);
114
115 if (verbose)
116 PrintAndLog("[LEN %u] %s[%02X %02X] %s",
117 *datalen,
118 sprint_hex(data, *datalen-2),
119 data[*datalen-2],
120 data[*datalen-1],
121 (*crc)?"OK":"FAIL"
122 );
123
22e24700 124 return 1;
1299c798 125}
126
127int CmdHF14BCmdRaw (const char *Cmd) {
128 bool reply = true;
129 bool crc = false;
22e24700 130 bool power = false;
7cf3ef20 131 char buf[5]="";
17ad0e09 132 uint8_t data[USB_CMD_DATA_SIZE] = {0x00};
1299c798 133 uint8_t datalen = 0;
134 unsigned int temp;
135 int i = 0;
136 if (strlen(Cmd)<3) {
7cf3ef20 137 PrintAndLog("Usage: hf 14b raw [-r] [-c] [-p] <0A 0B 0C ... hex>");
138 PrintAndLog(" -r do not read response");
139 PrintAndLog(" -c calculate and append CRC");
140 PrintAndLog(" -p leave the field on after receive");
141 return 0;
142 }
143
144 // strip
1299c798 145 while (*Cmd==' ' || *Cmd=='\t') Cmd++;
7cf3ef20 146
1299c798 147 while (Cmd[i]!='\0') {
148 if (Cmd[i]==' ' || Cmd[i]=='\t') { i++; continue; }
149 if (Cmd[i]=='-') {
150 switch (Cmd[i+1]) {
7cf3ef20 151 case 'r':
152 case 'R':
1299c798 153 reply = false;
7cf3ef20 154 break;
155 case 'c':
156 case 'C':
1299c798 157 crc = true;
7cf3ef20 158 break;
159 case 'p':
160 case 'P':
22e24700 161 power = true;
7cf3ef20 162 break;
163 default:
164 PrintAndLog("Invalid option");
165 return 0;
166 }
167 i+=2;
168 continue;
169 }
1299c798 170 if ((Cmd[i]>='0' && Cmd[i]<='9') ||
171 (Cmd[i]>='a' && Cmd[i]<='f') ||
172 (Cmd[i]>='A' && Cmd[i]<='F') ) {
7cf3ef20 173 buf[strlen(buf)+1]=0;
1299c798 174 buf[strlen(buf)]=Cmd[i];
7cf3ef20 175 i++;
176
177 if (strlen(buf)>=2) {
178 sscanf(buf,"%x",&temp);
1299c798 179 data[datalen++]=(uint8_t)(temp & 0xff);
7cf3ef20 180 *buf=0;
181 }
182 continue;
183 }
184 PrintAndLog("Invalid char on input");
5ee70129 185 return 1;
7cf3ef20 186 }
06b82e6a 187 if (datalen == 0)
188 {
189 PrintAndLog("Missing data input");
190 return 0;
191 }
1299c798 192
22e24700 193 return HF14BCmdRaw(reply, &crc, power, data, &datalen, true);
1299c798 194}
195
6de14cec 196// print full atqb info
d71d59db 197static void print_atqb_resp(uint8_t *data){
22e24700 198 PrintAndLog (" UID: %s", sprint_hex(data+1,4));
199 PrintAndLog (" App Data: %s", sprint_hex(data+5,4));
200 PrintAndLog (" Protocol: %s", sprint_hex(data+9,3));
201 uint8_t BitRate = data[9];
202 if (!BitRate) PrintAndLog (" Bit Rate: 106 kbit/s only PICC <-> PCD");
203 if (BitRate & 0x10) PrintAndLog (" Bit Rate: 212 kbit/s PICC -> PCD supported");
204 if (BitRate & 0x20) PrintAndLog (" Bit Rate: 424 kbit/s PICC -> PCD supported");
205 if (BitRate & 0x40) PrintAndLog (" Bit Rate: 847 kbit/s PICC -> PCD supported");
206 if (BitRate & 0x01) PrintAndLog (" Bit Rate: 212 kbit/s PICC <- PCD supported");
207 if (BitRate & 0x02) PrintAndLog (" Bit Rate: 424 kbit/s PICC <- PCD supported");
208 if (BitRate & 0x04) PrintAndLog (" Bit Rate: 847 kbit/s PICC <- PCD supported");
209 if (BitRate & 0x80) PrintAndLog (" Same bit rate <-> required");
210
211 uint16_t maxFrame = data[10]>>4;
212 if (maxFrame < 5) maxFrame = 8 * maxFrame + 16;
213 else if (maxFrame == 5) maxFrame = 64;
214 else if (maxFrame == 6) maxFrame = 96;
215 else if (maxFrame == 7) maxFrame = 128;
216 else if (maxFrame == 8) maxFrame = 256;
217 else maxFrame = 257;
218
219 PrintAndLog ("Max Frame Size: %d%s", maxFrame, (maxFrame == 257) ? "+ RFU" : "");
220
221 uint8_t protocolT = data[10] & 0xF;
222 PrintAndLog (" Protocol Type: Protocol is %scompliant with ISO/IEC 14443-4",(protocolT) ? "" : "not " );
223 PrintAndLog ("Frame Wait Int: %d", data[11]>>4);
224 PrintAndLog (" App Data Code: Application is %s",(data[11]&4) ? "Standard" : "Proprietary");
225 PrintAndLog (" Frame Options: NAD is %ssupported",(data[11]&2) ? "" : "not ");
226 PrintAndLog (" Frame Options: CID is %ssupported",(data[11]&1) ? "" : "not ");
227
228 return;
1299c798 229}
230
6de14cec 231// get SRx chip model (from UID) // from ST Microelectronics
d71d59db 232char *get_ST_Chip_Model(uint8_t data){
233 static char model[20];
234 char *retStr = model;
235 memset(model,0, sizeof(model));
236
237 switch (data) {
238 case 0x0: sprintf(retStr, "SRIX4K (Special)"); break;
239 case 0x2: sprintf(retStr, "SR176"); break;
240 case 0x3: sprintf(retStr, "SRIX4K"); break;
241 case 0x4: sprintf(retStr, "SRIX512"); break;
242 case 0x6: sprintf(retStr, "SRI512"); break;
243 case 0x7: sprintf(retStr, "SRI4K"); break;
244 case 0xC: sprintf(retStr, "SRT512"); break;
245 default: sprintf(retStr, "Unknown"); break;
246 }
17ad0e09 247 return retStr;
248}
249
6de14cec 250// print UID info from SRx chips (ST Microelectronics)
251static void print_st_general_info(uint8_t *data){
17ad0e09 252 //uid = first 8 bytes in data
253 PrintAndLog(" UID: %s", sprint_hex(SwapEndian64(data,8,8),8));
254 PrintAndLog(" MFG: %02X, %s", data[6], getTagInfo(data[6]));
255 PrintAndLog("Chip: %02X, %s", data[5]>>2, get_ST_Chip_Model(data[5]>>2));
256 return;
257}
258
6de14cec 259// 14b get and print UID only (general info)
260int HF14BStdReader(uint8_t *data, uint8_t *datalen){
17ad0e09 261 //05 00 00 = find one tag in field
262 //1d xx xx xx xx 20 00 08 01 00 = attrib xx=crc
263 //a3 = ? (resp 03 e2 c2)
264 //02 = ? (resp 02 6a d3)
265 // 022b (resp 02 67 00 [29 5b])
266 // 0200a40400 (resp 02 67 00 [29 5b])
267 // 0200a4040c07a0000002480300 (resp 02 67 00 [29 5b])
268 // 0200a4040c07a0000002480200 (resp 02 67 00 [29 5b])
269 // 0200a4040006a0000000010100 (resp 02 6a 82 [4b 4c])
270 // 0200a4040c09d27600002545500200 (resp 02 67 00 [29 5b])
271 // 0200a404000cd2760001354b414e4d30310000 (resp 02 6a 82 [4b 4c])
272 // 0200a404000ca000000063504b43532d313500 (resp 02 6a 82 [4b 4c])
273 // 0200a4040010a000000018300301000000000000000000 (resp 02 6a 82 [4b 4c])
274 //03 = ? (resp 03 [e3 c2])
275 //c2 = ? (resp c2 [66 15])
6de14cec 276 //b2 = ? (resp a3 [e9 67])
17ad0e09 277 bool crc = true;
278 *datalen = 3;
279 //std read cmd
280 data[0] = 0x05;
281 data[1] = 0x00;
6de14cec 282 data[2] = 0x08;
17ad0e09 283
6de14cec 284 // response, crc, powerfield, data, len, verbose
285 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)==0) return 0;
17ad0e09 286
287 if (data[0] != 0x50 || *datalen != 14 || !crc) return 0;
288
289 PrintAndLog ("\n14443-3b tag found:");
6de14cec 290 PrintAndLog (" UID: %s", sprint_hex(data+1,4));
291
292 return 1;
293}
294
295// 14b get and print Full Info (as much as we know)
296int HF14BStdInfo(uint8_t *data, uint8_t *datalen){
297 if (!HF14BStdReader(data,datalen)) return 0;
298
299 //add more info here
17ad0e09 300 print_atqb_resp(data);
301
302 return 1;
303}
304
6de14cec 305// SRx get and print general info about SRx chip from UID
306int HF14B_ST_Reader(uint8_t *data, uint8_t *datalen){
17ad0e09 307 bool crc = true;
308 *datalen = 2;
309 //wake cmd
310 data[0] = 0x06;
311 data[1] = 0x00;
312
313 //leave power on
314 // verbose on for now for testing - turn off when functional
315 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)==0) return rawClose();
316
317 if (*datalen != 3 || !crc) return rawClose();
318
319 uint8_t chipID = data[0];
320 // select
321 data[0] = 0x0E;
322 data[1] = chipID;
323 *datalen = 2;
324
325 //leave power on
326 // verbose on for now for testing - turn off when functional
327 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)==0) return rawClose();
328
329 if (*datalen != 3 || !crc || data[0] != chipID) return rawClose();
330
331 // get uid
332 data[0] = 0x0B;
333 *datalen = 1;
334
335 //power off
336 // verbose on for now for testing - turn off when functional
337 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)==0) return 0;
338 rawClose();
339 if (*datalen != 10 || !crc) return 0;
340
341 PrintAndLog("\n14443-3b ST tag found:");
6de14cec 342 print_st_general_info(data);
343 return 1;
344}
345
346// SRx get and print full info (needs more info...)
347int HF14B_ST_Info(uint8_t *data, uint8_t *datalen){
348 if (!HF14B_ST_Reader(data, datalen)) return 0;
349
350 //add locking bit information here.
351
352
17ad0e09 353 return 1;
354}
355
356// test for other 14b type tags (mimic another reader - don't have tags to identify)
6de14cec 357int HF14B_Other_Reader(uint8_t *data, uint8_t *datalen){
17ad0e09 358 bool crc = true;
359 *datalen = 4;
360 //std read cmd
361 data[0] = 0x00;
362 data[1] = 0x0b;
363 data[2] = 0x3f;
364 data[3] = 0x80;
365
6de14cec 366 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)!=0) {
17ad0e09 367 if (*datalen > 2 || !crc) {
368 PrintAndLog ("\n14443-3b tag found:");
369 PrintAndLog ("Unknown tag type answered to a 0x000b3f80 command ans:");
370 PrintAndLog ("%s",sprint_hex(data,*datalen));
371 return 1;
372 }
22e24700 373 }
374
375 crc = false;
376 *datalen = 1;
377 data[0] = 0x0a;
378
6de14cec 379 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)!=0) {
22e24700 380 if (*datalen > 0) {
381 PrintAndLog ("\n14443-3b tag found:");
382 PrintAndLog ("Unknown tag type answered to a 0x0A command ans:");
383 PrintAndLog ("%s",sprint_hex(data,*datalen));
384 return 1;
385 }
386 }
387
388 crc = false;
389 *datalen = 1;
390 data[0] = 0x0c;
391
6de14cec 392 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)!=0) {
22e24700 393 if (*datalen > 0) {
394 PrintAndLog ("\n14443-3b tag found:");
395 PrintAndLog ("Unknown tag type answered to a 0x0C command ans:");
396 PrintAndLog ("%s",sprint_hex(data,*datalen));
397 return 1;
398 }
399 }
6de14cec 400 rawClose();
22e24700 401 return 0;
1299c798 402}
403
6de14cec 404// get and print all info known about any known 14b tag
17ad0e09 405int HF14BInfo(bool verbose){
6de14cec 406 uint8_t data[USB_CMD_DATA_SIZE];
22e24700 407 uint8_t datalen = 5;
408
409 // try std 14b (atqb)
17ad0e09 410 if (HF14BStdInfo(data, &datalen)) return 1;
22e24700 411
412 // try st 14b
17ad0e09 413 if (HF14B_ST_Info(data, &datalen)) return 1;
1299c798 414
22e24700 415 // try unknown 14b read commands (to be identified later)
416 // could be read of calypso, CEPAS, moneo, or pico pass.
6de14cec 417 if (HF14B_Other_Reader(data, &datalen)) return 1;
abb21530 418
d71d59db 419 if (verbose) PrintAndLog("no 14443B tag found");
420 return 0;
d71d59db 421}
422
6de14cec 423// menu command to get and print all info known about any known 14b tag
17ad0e09 424int CmdHF14Binfo(const char *Cmd){
425 return HF14BInfo(true);
7cf3ef20 426}
427
6de14cec 428// get and print general info about all known 14b chips
429int HF14BReader(bool verbose){
430 uint8_t data[USB_CMD_DATA_SIZE];
431 uint8_t datalen = 5;
432
433 // try std 14b (atqb)
434 if (HF14BStdReader(data, &datalen)) return 1;
435
436 // try st 14b
437 if (HF14B_ST_Reader(data, &datalen)) return 1;
438
439 // try unknown 14b read commands (to be identified later)
440 // could be read of calypso, CEPAS, moneo, or pico pass.
441 if (HF14B_Other_Reader(data, &datalen)) return 1;
442
443 if (verbose) PrintAndLog("no 14443B tag found");
444 return 0;
445}
446
447// menu command to get and print general info about all known 14b chips
448int CmdHF14BReader(const char *Cmd){
449 return HF14BReader(true);
450}
451
17ad0e09 452int CmdSriWrite( const char *Cmd){
3fe4ff4f 453/*
454 * For SRIX4K blocks 00 - 7F
455 * hf 14b raw -c -p 09 $srix4kwblock $srix4kwdata
456 *
457 * For SR512 blocks 00 - 0F
458 * hf 14b raw -c -p 09 $sr512wblock $sr512wdata
459 *
460 * Special block FF = otp_lock_reg block.
461 * Data len 4 bytes-
462 */
463 char cmdp = param_getchar(Cmd, 0);
464 uint8_t blockno = -1;
465 uint8_t data[4] = {0x00};
466 bool isSrix4k = true;
467 char str[20];
468
b5be31f9 469 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
3fe4ff4f 470 PrintAndLog("Usage: hf 14b write <1|2> <BLOCK> <DATA>");
b5be31f9 471 PrintAndLog(" [1 = SRIX4K]");
472 PrintAndLog(" [2 = SRI512]");
473 PrintAndLog(" [BLOCK number depends on tag, special block == FF]");
474 PrintAndLog(" sample: hf 14b write 1 7F 11223344");
475 PrintAndLog(" : hf 14b write 1 FF 11223344");
476 PrintAndLog(" : hf 14b write 2 15 11223344");
477 PrintAndLog(" : hf 14b write 2 FF 11223344");
3fe4ff4f 478 return 0;
479 }
480
b5be31f9 481 if ( cmdp == '2' )
3fe4ff4f 482 isSrix4k = false;
483
b5be31f9 484 //blockno = param_get8(Cmd, 1);
485
486 if ( param_gethex(Cmd,1, &blockno, 2) ) {
487 PrintAndLog("Block number must include 2 HEX symbols");
488 return 0;
489 }
3fe4ff4f 490
491 if ( isSrix4k ){
492 if ( blockno > 0x7f && blockno != 0xff ){
493 PrintAndLog("Block number out of range");
494 return 0;
495 }
496 } else {
497 if ( blockno > 0x0f && blockno != 0xff ){
498 PrintAndLog("Block number out of range");
499 return 0;
500 }
501 }
502
503 if (param_gethex(Cmd, 2, data, 8)) {
504 PrintAndLog("Data must include 8 HEX symbols");
505 return 0;
506 }
507
508 if ( blockno == 0xff)
b5be31f9 509 PrintAndLog("[%s] Write special block %02X [ %s ]", (isSrix4k)?"SRIX4K":"SRI512" , blockno, sprint_hex(data,4) );
3fe4ff4f 510 else
b5be31f9 511 PrintAndLog("[%s] Write block %02X [ %s ]", (isSrix4k)?"SRIX4K":"SRI512", blockno, sprint_hex(data,4) );
3fe4ff4f 512
fe5b3a44 513 sprintf(str, "-c 09 %02x %02x%02x%02x%02x", blockno, data[0], data[1], data[2], data[3]);
b5be31f9 514
3fe4ff4f 515 CmdHF14BCmdRaw(str);
516 return 0;
517}
518
7fe9b0b7 519static command_t CommandTable[] =
520{
22e24700 521 {"help", CmdHelp, 1, "This help"},
6de14cec 522 {"info", CmdHF14Binfo, 0, "Find and print details about a 14443B tag"},
523 {"list", CmdHF14BList, 0, "[Deprecated] List ISO 14443B history"},
524 {"reader", CmdHF14BReader, 0, "Act as a 14443B reader to identify a tag"},
525 {"sim", CmdHF14BSim, 0, "Fake ISO 14443B tag"},
526 {"snoop", CmdHF14BSnoop, 0, "Eavesdrop ISO 14443B"},
22e24700 527 {"sri512read", CmdSri512Read, 0, "Read contents of a SRI512 tag"},
528 {"srix4kread", CmdSrix4kRead, 0, "Read contents of a SRIX4K tag"},
6de14cec 529 {"sriwrite", CmdSriWrite, 0, "Write data to a SRI512 | SRIX4K tag"},
22e24700 530 {"raw", CmdHF14BCmdRaw, 0, "Send raw hex data to tag"},
22e24700 531 {NULL, NULL, 0, NULL}
7fe9b0b7 532};
533
534int CmdHF14B(const char *Cmd)
535{
22e24700 536 CmdsParse(CommandTable, Cmd);
537 return 0;
7fe9b0b7 538}
539
540int CmdHelp(const char *Cmd)
541{
22e24700 542 CmdsHelp(CommandTable);
543 return 0;
7fe9b0b7 544}
Impressum, Datenschutz