1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
3 // Copyright (C) 2011 Gerhard de Koning Gans
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // High frequency iClass commands
10 //-----------------------------------------------------------------------------
16 #include "iso14443crc.h" // Can also be used for iClass, using 0xE012 as CRC-type
18 //#include "proxusb.h"
19 #include "proxmark3.h"
21 #include "cmdparser.h"
22 #include "cmdhficlass.h"
27 static int CmdHelp(const char *Cmd
);
29 int xorbits_8(uint8_t val
)
31 uint8_t res
= val
^ (val
>> 1); //1st pass
32 res
= res
^ (res
>> 1); // 2nd pass
33 res
= res
^ (res
>> 2); // 3rd pass
34 res
= res
^ (res
>> 4); // 4th pass
38 int CmdHFiClassList(const char *Cmd
)
41 bool ShowWaitCycles
= false;
42 char param
= param_getchar(Cmd
, 0);
45 PrintAndLog("List data in trace buffer.");
46 PrintAndLog("Usage: hf iclass list");
47 PrintAndLog("h - help");
48 PrintAndLog("sample: hf iclass list");
53 GetFromBigBuf(got
,sizeof(got
),0);
54 WaitForResponse(CMD_ACK
,NULL
);
56 PrintAndLog("Recorded Activity");
58 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
59 PrintAndLog("All times are in carrier periods (1/13.56Mhz)");
61 PrintAndLog(" Start | End | Src | Data");
62 PrintAndLog("-----------|-----------|-----|--------");
65 uint32_t first_timestamp
= 0;
71 uint32_t EndOfTransmissionTimestamp
= 0;
76 //First 32 bits contain
78 // timestamp (remaining)
81 timestamp
= *((uint32_t *)(got
+i
));
82 parityBits
= *((uint32_t *)(got
+i
+4));
85 uint32_t next_timestamp
= (*((uint32_t *)(got
+i
+9))) & 0x7fffffff;
87 tagToReader
= timestamp
& 0x80000000;
88 timestamp
&= 0x7fffffff;
91 first_timestamp
= timestamp
;
94 // Break and stick with current result if buffer was not completely full
95 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[2] == 0x44 && frame
[3] == 0x44) break;
99 if(len
)//We have some data to display
103 for(j
= 0; j
< len
; j
++)
105 oddparity
= 0x01 ^ xorbits_8(frame
[j
] & 0xFF);
107 if (tagToReader
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
108 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
110 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
115 if (ShowWaitCycles
) {
116 sprintf(line
, "fdt (Frame Delay Time): %d", (next_timestamp
- timestamp
));
125 if(!tagToReader
&& len
== 4) {
126 // Rough guess that this is a command from the reader
127 // For iClass the command byte is not part of the CRC
128 ComputeCrc14443(CRC_ICLASS
, &frame
[1], len
-3, &b1
, &b2
);
131 // For other data.. CRC might not be applicable (UPDATE commands etc.)
132 ComputeCrc14443(CRC_ICLASS
, frame
, len
-2, &b1
, &b2
);
135 if (b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
136 crc
= (tagToReader
& (len
< 8)) ? "" : " !crc";
141 EndOfTransmissionTimestamp
= (*((uint32_t *)(got
+i
))) & 0x7fffffff;
143 // Not implemented for iclass on the ARM-side
144 //if (!ShowWaitCycles) i += 9;
146 PrintAndLog(" %9d | %9d | %s | %s %s",
147 (timestamp
- first_timestamp
),
148 (EndOfTransmissionTimestamp
- first_timestamp
),
149 (len
?(tagToReader
? "Tag" : "Rdr"):" "),
155 int CmdHFiClassListOld(const char *Cmd
)
158 GetFromBigBuf(got
,sizeof(got
),0);
160 PrintAndLog("recorded activity:");
161 PrintAndLog(" ETU :rssi: who bytes");
162 PrintAndLog("---------+----+----+-----------");
173 int timestamp
= *((uint32_t *)(got
+i
));
174 if (timestamp
& 0x80000000) {
175 timestamp
&= 0x7fffffff;
184 int parityBits
= *((uint32_t *)(got
+i
+4));
185 // 4 bytes of additional information...
186 // maximum of 32 additional parity bit information
189 // at each quarter bit period we can send power level (16 levels)
190 // or each half bit period in 256 levels.
198 if (i
+ len
>= 1900) {
202 uint8_t *frame
= (got
+i
+9);
204 // Break and stick with current result if buffer was not completely full
205 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
207 char line
[1000] = "";
209 for (j
= 0; j
< len
; j
++) {
210 int oddparity
= 0x01;
214 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
217 //if((parityBits >> (len - j - 1)) & 0x01) {
218 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
219 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
222 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
230 for (j
= 0; j
< (len
- 1); j
++) {
231 // gives problems... search for the reason..
232 /*if(frame[j] == 0xAA) {
235 crc = "[1] Two drops close after each other";
238 crc = "[2] Potential SOC with a drop in second half of bitperiod";
241 crc = "[3] Segment Z after segment X is not possible";
244 crc = "[4] Parity bit of a fully received byte was wrong";
247 crc = "[?] Unknown error";
254 if (strlen(crc
)==0) {
255 if(!isResponse
&& len
== 4) {
256 // Rough guess that this is a command from the reader
257 // For iClass the command byte is not part of the CRC
258 ComputeCrc14443(CRC_ICLASS
, &frame
[1], len
-3, &b1
, &b2
);
261 // For other data.. CRC might not be applicable (UPDATE commands etc.)
262 ComputeCrc14443(CRC_ICLASS
, frame
, len
-2, &b1
, &b2
);
264 //printf("%1x %1x",(unsigned)b1,(unsigned)b2);
265 if (b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
266 crc
= (isResponse
& (len
< 8)) ? "" : " !crc";
275 char metricString
[100];
277 sprintf(metricString
, "%3d", metric
);
279 strcpy(metricString
, " ");
282 PrintAndLog(" +%7d: %s: %s %s %s",
283 (prev
< 0 ? 0 : (timestamp
- prev
)),
285 (isResponse
? "TAG" : " "), line
, crc
);
293 /*void iso14a_set_timeout(uint32_t timeout) {
294 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_SET_TIMEOUT, 0, timeout}};
298 int CmdHFiClassSnoop(const char *Cmd
)
300 UsbCommand c
= {CMD_SNOOP_ICLASS
};
305 int CmdHFiClassSim(const char *Cmd
)
308 uint8_t CSN
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
311 PrintAndLog("Usage: hf iclass sim [0 <CSN>] | x");
312 PrintAndLog(" options");
313 PrintAndLog(" 0 <CSN> simulate the given CSN");
314 PrintAndLog(" 1 simulate default CSN");
315 PrintAndLog(" 2 iterate CSNs, gather MACs");
316 PrintAndLog(" sample: hf iclass sim 0 031FEC8AF7FF12E0");
317 PrintAndLog(" sample: hf iclass sim 2");
321 simType
= param_get8(Cmd
, 0);
325 if (param_gethex(Cmd
, 1, CSN
, 16)) {
326 PrintAndLog("A CSN should consist of 16 HEX symbols");
329 PrintAndLog("--simtype:%02x csn:%s", simType
, sprint_hex(CSN
, 8));
334 PrintAndLog("Undefined simptype %d", simType
);
337 uint8_t numberOfCSNs
=0;
341 UsbCommand c
= {CMD_SIMULATE_TAG_ICLASS
, {simType
,63}};
342 UsbCommand resp
= {0};
345 0x00,0x0B,0x0F,0xFF,0xF7,0xFF,0x12,0xE0 ,
346 0x00,0x13,0x94,0x7e,0x76,0xff,0x12,0xe0 ,
347 0x2a,0x99,0xac,0x79,0xec,0xff,0x12,0xe0 ,
348 0x17,0x12,0x01,0xfd,0xf7,0xff,0x12,0xe0 ,
349 0xcd,0x56,0x01,0x7c,0x6f,0xff,0x12,0xe0 ,
350 0x4b,0x5e,0x0b,0x72,0xef,0xff,0x12,0xe0 ,
351 0x00,0x73,0xd8,0x75,0x58,0xff,0x12,0xe0 ,
352 0x0c,0x90,0x32,0xf3,0x5d,0xff,0x12,0xe0 };
354 memcpy(c
.d
.asBytes
, csns
, 64);
357 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, -1)) {
358 PrintAndLog("Command timed out");
362 uint8_t num_mac_responses
= resp
.arg
[1];
363 PrintAndLog("Mac responses: %d MACs obtained (should be 8)", num_mac_responses
);
365 size_t datalen
= 8*24;
367 * Now, time to dump to file. We'll use this format:
368 * <8-byte CSN><8-byte CC><4 byte NR><4 byte MAC>....
369 * So, it should wind up as
372 * The returndata from the pm3 is on the following format
373 * <4 byte NR><4 byte MAC>
374 * CC are all zeroes, CSN is the same as was sent in
376 void* dump
= malloc(datalen
);
377 memset(dump
,0,datalen
);//<-- Need zeroes for the CC-field
379 for(i
= 0 ; i
< 8 ; i
++)
381 memcpy(dump
+i
*24, csns
+i
*8,8); //CSN
382 //8 zero bytes here...
383 //Then comes NR_MAC (eight bytes from the response)
384 memcpy(dump
+i
*24+16,resp
.d
.asBytes
+i
*8,8);
387 /** Now, save to dumpfile **/
388 saveFile("iclass_mac_attack", "bin", dump
,datalen
);
392 UsbCommand c
= {CMD_SIMULATE_TAG_ICLASS
, {simType
,numberOfCSNs
}};
393 memcpy(c
.d
.asBytes
, CSN
, 8);
399 int CmdHFiClassReader(const char *Cmd
)
401 uint8_t readerType
= 0;
404 PrintAndLog("Usage: hf iclass reader <reader type>");
405 PrintAndLog(" sample: hf iclass reader 0");
409 readerType
= param_get8(Cmd
, 0);
410 PrintAndLog("--readertype:%02x", readerType
);
412 UsbCommand c
= {CMD_READER_ICLASS
, {readerType
}};
413 //memcpy(c.d.asBytes, CSN, 8);
416 /*UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
418 uint8_t isOK = resp->arg[0] & 0xff;
419 PrintAndLog("isOk:%02x", isOK);
421 PrintAndLog("Command execute timeout");
427 static command_t CommandTable
[] =
429 {"help", CmdHelp
, 1, "This help"},
430 {"list", CmdHFiClassList
, 0, "List iClass history"},
431 {"snoop", CmdHFiClassSnoop
, 0, "Eavesdrop iClass communication"},
432 {"sim", CmdHFiClassSim
, 0, "Simulate iClass tag"},
433 {"reader", CmdHFiClassReader
, 0, "Read an iClass tag"},
434 {NULL
, NULL
, 0, NULL
}
437 int CmdHFiClass(const char *Cmd
)
439 CmdsParse(CommandTable
, Cmd
);
443 int CmdHelp(const char *Cmd
)
445 CmdsHelp(CommandTable
);
450 * @brief checks if a file exists
454 int fileExists(const char *filename
) {
456 int result
= stat(filename
, &st
);
460 * @brief Utility function to save data to a file. This method takes a preferred name, but if that
461 * file already exists, it tries with another name until it finds something suitable.
462 * E.g. dumpdata-15.txt
463 * @param preferredName
464 * @param suffix the file suffix. Leave out the ".".
465 * @param data The binary data to write to the file
466 * @param datalen the length of the data
467 * @return 0 for ok, 1 for failz
469 int saveFile(const char *preferredName
, const char *suffix
, const void* data
, size_t datalen
)
471 FILE *f
= fopen(preferredName
, "wb");
472 int size
= sizeof(char) * (strlen(preferredName
)+strlen(suffix
)+5);
473 char * fileName
= malloc(size
);
475 memset(fileName
,0,size
);
477 sprintf(fileName
,"%s.%s", preferredName
, suffix
);
478 while(fileExists(fileName
))
480 sprintf(fileName
,"%s-%d.%s", preferredName
, num
, suffix
);
483 /* We should have a valid filename now, e.g. dumpdata-3.bin */
485 /*Opening file for writing in binary mode*/
486 FILE *fileHandle
=fopen(fileName
,"wb");
488 PrintAndLog("Failed to write to file '%s'", fileName
);
491 fwrite(data
, 1, datalen
, fileHandle
);
493 PrintAndLog("Saved data to '%s'", fileName
);