]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhficlass.c
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 //-----------------------------------------------------------------------------
15 #include "iso14443crc.h" // Can also be used for iClass, using 0xE012 as CRC-type
17 //#include "proxusb.h"
18 #include "proxmark3.h"
20 #include "cmdparser.h"
21 #include "cmdhficlass.h"
24 #include "loclass/des.h"
25 #include "loclass/cipherutils.h"
26 #include "loclass/cipher.h"
27 #include "loclass/ikeys.h"
29 static int CmdHelp(const char *Cmd
);
31 int CmdHFiClassList(const char *Cmd
)
34 GetFromBigBuf(got
,sizeof(got
),0);
36 PrintAndLog("recorded activity:");
37 PrintAndLog(" ETU :rssi: who bytes");
38 PrintAndLog("---------+----+----+-----------");
49 int timestamp
= *((uint32_t *)(got
+i
));
50 if (timestamp
& 0x80000000) {
51 timestamp
&= 0x7fffffff;
58 int parityBits
= *((uint32_t *)(got
+i
+4));
59 // 4 bytes of additional information...
60 // maximum of 32 additional parity bit information
63 // at each quarter bit period we can send power level (16 levels)
64 // or each half bit period in 256 levels.
72 if (i
+ len
>= 1900) {
76 uint8_t *frame
= (got
+i
+9);
78 // Break and stick with current result if buffer was not completely full
79 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
83 for (j
= 0; j
< len
; j
++) {
88 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
91 //if((parityBits >> (len - j - 1)) & 0x01) {
92 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
93 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
96 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
104 for (j
= 0; j
< (len
- 1); j
++) {
105 // gives problems... search for the reason..
106 /*if(frame[j] == 0xAA) {
109 crc = "[1] Two drops close after each other";
112 crc = "[2] Potential SOC with a drop in second half of bitperiod";
115 crc = "[3] Segment Z after segment X is not possible";
118 crc = "[4] Parity bit of a fully received byte was wrong";
121 crc = "[?] Unknown error";
128 if (strlen(crc
)==0) {
129 if(!isResponse
&& len
== 4) {
130 // Rough guess that this is a command from the reader
131 // For iClass the command byte is not part of the CRC
132 ComputeCrc14443(CRC_ICLASS
, &frame
[1], len
-3, &b1
, &b2
);
135 // For other data.. CRC might not be applicable (UPDATE commands etc.)
136 ComputeCrc14443(CRC_ICLASS
, frame
, len
-2, &b1
, &b2
);
138 //printf("%1x %1x",(unsigned)b1,(unsigned)b2);
139 if (b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
140 crc
= (isResponse
& (len
< 8)) ? "" : " !crc";
149 char metricString
[100];
151 sprintf(metricString
, "%3d", metric
);
153 strcpy(metricString
, " ");
156 PrintAndLog(" +%7d: %s: %s %s %s",
157 (prev
< 0 ? 0 : (timestamp
- prev
)),
159 (isResponse
? "TAG" : " "), line
, crc
);
167 /*void iso14a_set_timeout(uint32_t timeout) {
168 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_SET_TIMEOUT, 0, timeout}};
172 int CmdHFiClassSnoop(const char *Cmd
)
174 UsbCommand c
= {CMD_SNOOP_ICLASS
};
179 int CmdHFiClassSim(const char *Cmd
)
182 uint8_t CSN
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
185 PrintAndLog("Usage: hf iclass sim <sim type> <CSN (16 hex symbols)>");
186 PrintAndLog(" sample: hf iclass sim 0 031FEC8AF7FF12E0");
190 simType
= param_get8(Cmd
, 0);
191 if (param_gethex(Cmd
, 1, CSN
, 16)) {
192 PrintAndLog("A CSN should consist of 16 HEX symbols");
195 PrintAndLog("--simtype:%02x csn:%s", simType
, sprint_hex(CSN
, 8));
197 UsbCommand c
= {CMD_SIMULATE_TAG_ICLASS
, {simType
}};
198 memcpy(c
.d
.asBytes
, CSN
, 8);
201 /*UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
203 uint8_t isOK = resp->arg[0] & 0xff;
204 PrintAndLog("isOk:%02x", isOK);
206 PrintAndLog("Command execute timeout");
212 int CmdHFiClassReader(const char *Cmd
)
214 uint8_t readerType
= 0;
217 PrintAndLog("Usage: hf iclass reader <reader type>");
218 PrintAndLog(" sample: hf iclass reader 0");
222 readerType
= param_get8(Cmd
, 0);
223 PrintAndLog("--readertype:%02x", readerType
);
225 UsbCommand c
= {CMD_READER_ICLASS
, {readerType
}};
231 int CmdHFiClassReader_Replay(const char *Cmd
)
233 uint8_t readerType
= 0;
234 uint8_t MAC
[4]={0x00, 0x00, 0x00, 0x00};
237 PrintAndLog("Usage: hf iclass replay <MAC>");
238 PrintAndLog(" sample: hf iclass replay 00112233");
242 if (param_gethex(Cmd
, 0, MAC
, 8)) {
243 PrintAndLog("MAC must include 8 HEX symbols");
247 UsbCommand c
= {CMD_READER_ICLASS_REPLAY
, {readerType
}};
248 memcpy(c
.d
.asBytes
, MAC
, 4);
254 int CmdHFiClassReader_Dump(const char *Cmd
)
256 uint8_t readerType
= 0;
257 uint8_t MAC
[4]={0x00,0x00,0x00,0x00};
258 uint8_t KEY
[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
259 uint8_t CSN
[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
260 uint8_t CCNR
[12]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
261 uint8_t CC_temp
[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
262 uint8_t result
[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
263 uint8_t div_key
[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
265 uint64_t crypted_id
=0;
268 PrintAndLog("Usage: hf iclass dump <Key> <CSN> <CC>");
269 PrintAndLog(" sample: hf iclass dump 0011223344556677 aabbccddeeffgghh FFFFFFFFFFFFFFFF");
273 if (param_gethex(Cmd
, 0, KEY
, 16)) {
274 PrintAndLog("KEY must include 16 HEX symbols");
278 if (param_gethex(Cmd
, 1, CSN
, 16)) {
279 PrintAndLog("CSN must include 16 HEX symbols");
282 if (param_gethex(Cmd
, 2, CC_temp
, 16)) {
283 PrintAndLog("CC must include 16 HEX symbols");
287 memcpy(CCNR
,CC_temp
,8);
288 des_setkey_enc( &ctx_enc
, KEY
);
289 des_crypt_ecb(&ctx_enc
,CSN
,result
);
290 PrintAndLog("DES Key: %s",sprint_hex(result
,8));
292 crypted_id
= bytes_to_num(result
,8);
293 uint64_t x
= (crypted_id
& 0xFFFF000000000000 );
294 pushbackSixBitByte(&newz
, getSixBitByte(crypted_id
,0),7);
295 pushbackSixBitByte(&newz
, getSixBitByte(crypted_id
,1),6);
296 pushbackSixBitByte(&newz
, getSixBitByte(crypted_id
,2),5);
297 pushbackSixBitByte(&newz
, getSixBitByte(crypted_id
,3),4);
298 pushbackSixBitByte(&newz
, getSixBitByte(crypted_id
,4),3);
299 pushbackSixBitByte(&newz
, getSixBitByte(crypted_id
,5),2);
300 pushbackSixBitByte(&newz
, getSixBitByte(crypted_id
,6),1);
301 pushbackSixBitByte(&newz
, getSixBitByte(crypted_id
,7),0);
304 num_to_bytes(crypted_id
,8,result
);
305 PrintAndLog("DESr Key: %s",sprint_hex(result
,8));
306 //crypted_id = bytes_to_num(result,8);
307 //memset(result,0,8);
308 hash0(crypted_id
,div_key
);
309 //memcpy(div_key,result,8);
310 PrintAndLog("Div Key: %s",sprint_hex(div_key
,8));
311 calc_iclass_mac(CCNR
,12,div_key
,MAC
);
313 UsbCommand c
= {CMD_READER_ICLASS_REPLAY
, {readerType
}};
314 memcpy(c
.d
.asBytes
, MAC
, 4);
321 static command_t CommandTable
[] =
323 {"help", CmdHelp
, 1, "This help"},
324 {"list", CmdHFiClassList
, 0, "List iClass history"},
325 {"snoop", CmdHFiClassSnoop
, 0, "Eavesdrop iClass communication"},
326 {"sim", CmdHFiClassSim
, 0, "Simulate iClass tag"},
327 {"reader", CmdHFiClassReader
, 0, "Read an iClass tag"},
328 {"replay", CmdHFiClassReader_Replay
, 0, "Read an iClass tag via Reply Attack"},
329 {"dump", CmdHFiClassReader_Dump
, 0, "Authenticate and Dump iClass tag"},
330 {NULL
, NULL
, 0, NULL
}
333 int CmdHFiClass(const char *Cmd
)
335 CmdsParse(CommandTable
, Cmd
);
339 int CmdHelp(const char *Cmd
)
341 CmdsHelp(CommandTable
);