1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2012 Roel Verdult
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 // Low frequency Hitag support
9 //-----------------------------------------------------------------------------
15 #include "proxmark3.h"
17 #include "cmdparser.h"
25 static int CmdHelp(const char *Cmd
);
27 size_t nbytes(size_t nbits
) {
28 return (nbits
/8)+((nbits
%8)>0);
31 int CmdLFHitagList(const char *Cmd
)
33 uint8_t *got
= malloc(USB_CMD_DATA_SIZE
);
35 // Query for the actual size of the trace
37 GetFromBigBuf(got
, USB_CMD_DATA_SIZE
, 0);
38 WaitForResponse(CMD_ACK
, &response
);
39 uint16_t traceLen
= response
.arg
[2];
40 if (traceLen
> USB_CMD_DATA_SIZE
) {
41 uint8_t *p
= realloc(got
, traceLen
);
43 PrintAndLog("Cannot allocate memory for trace");
48 GetFromBigBuf(got
, traceLen
, 0);
49 WaitForResponse(CMD_ACK
,NULL
);
52 PrintAndLog("recorded activity (TraceLen = %d bytes):");
53 PrintAndLog(" ETU :nbits: who bytes");
54 PrintAndLog("---------+-----+----+-----------");
58 int len
= strlen(Cmd
);
60 char filename
[FILE_PATH_SIZE
] = { 0x00 };
63 if (len
> FILE_PATH_SIZE
)
65 memcpy(filename
, Cmd
, len
);
67 if (strlen(filename
) > 0) {
68 if ((pf
= fopen(filename
,"wb")) == NULL
) {
69 PrintAndLog("Error: Could not open file [%s]",filename
);
76 if(i
> traceLen
) { break; }
79 int timestamp
= *((uint32_t *)(got
+i
));
80 if (timestamp
& 0x80000000) {
81 timestamp
&= 0x7fffffff;
87 int parityBits
= *((uint32_t *)(got
+i
+4));
88 // 4 bytes of additional information...
89 // maximum of 32 additional parity bit information
92 // at each quarter bit period we can send power level (16 levels)
93 // or each half bit period in 256 levels.
96 int len
= nbytes(got
[i
+8]);
101 if (i
+ len
> traceLen
) { break;}
103 uint8_t *frame
= (got
+i
+9);
105 // Break and stick with current result if buffer was not completely full
106 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
108 char line
[1000] = "";
110 for (j
= 0; j
< len
; j
++) {
111 int oddparity
= 0x01;
115 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
118 //if((parityBits >> (len - j - 1)) & 0x01) {
119 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
120 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
123 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
127 PrintAndLog(" +%7d: %3d: %s %s",
128 (prev
< 0 ? 0 : (timestamp
- prev
)),
130 (isResponse
? "TAG" : " "),
134 fprintf(pf
," +%7d: %3d: %s %s\n",
135 (prev
< 0 ? 0 : (timestamp
- prev
)),
137 (isResponse
? "TAG" : " "),
147 PrintAndLog("Recorded activity succesfully written to file: %s", filename
);
154 int CmdLFHitagSnoop(const char *Cmd
) {
155 UsbCommand c
= {CMD_SNOOP_HITAG
};
160 int CmdLFHitagSim(const char *Cmd
) {
162 UsbCommand c
= {CMD_SIMULATE_HITAG
};
163 char filename
[FILE_PATH_SIZE
] = { 0x00 };
165 bool tag_mem_supplied
;
166 int len
= strlen(Cmd
);
167 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
168 memcpy(filename
, Cmd
, len
);
170 if (strlen(filename
) > 0) {
171 if ((pf
= fopen(filename
,"rb+")) == NULL
) {
172 PrintAndLog("Error: Could not open file [%s]",filename
);
175 tag_mem_supplied
= true;
176 if (fread(c
.d
.asBytes
,48,1,pf
) == 0) {
177 PrintAndLog("Error: File reading error");
183 tag_mem_supplied
= false;
186 // Does the tag comes with memory
187 c
.arg
[0] = (uint32_t)tag_mem_supplied
;
193 int CmdLFHitagReader(const char *Cmd
) {
194 UsbCommand c
= {CMD_READER_HITAG
};//, {param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16),param_get32ex(Cmd,3,0,16)}};
195 hitag_data
* htd
= (hitag_data
*)c
.d
.asBytes
;
196 hitag_function htf
= param_get32ex(Cmd
,0,0,10);
199 case 01: { //RHTSF_CHALLENGE
200 c
= (UsbCommand
){ CMD_READ_HITAG_S
};
201 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->auth
.NrAr
);
202 num_to_bytes(param_get32ex(Cmd
,2,0,16),4,htd
->auth
.NrAr
+4);
204 case 02: { //RHTSF_KEY
205 c
= (UsbCommand
){ CMD_READ_HITAG_S
};
206 num_to_bytes(param_get64ex(Cmd
,1,0,16),6,htd
->crypto
.key
);
208 case RHT2F_PASSWORD
: {
209 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->pwd
.password
);
211 case RHT2F_AUTHENTICATE
: {
212 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->auth
.NrAr
);
213 num_to_bytes(param_get32ex(Cmd
,2,0,16),4,htd
->auth
.NrAr
+4);
216 num_to_bytes(param_get64ex(Cmd
,1,0,16),6,htd
->crypto
.key
);
217 // num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4);
219 case RHT2F_TEST_AUTH_ATTEMPTS
: {
220 // No additional parameters needed
223 PrintAndLog("Error: unkown reader function %d",htf
);
224 PrintAndLog("Hitag reader functions");
225 PrintAndLog(" HitagS (0*)");
226 PrintAndLog(" 01 <nr> <ar> (Challenge) read all pages from a Hitag S tag");
227 PrintAndLog(" 02 <key> (set to 0 if no authentication is needed) read all pages from a Hitag S tag");
228 PrintAndLog(" Hitag1 (1*)");
229 PrintAndLog(" Hitag2 (2*)");
230 PrintAndLog(" 21 <password> (password mode)");
231 PrintAndLog(" 22 <nr> <ar> (authentication)");
232 PrintAndLog(" 23 <key> (authentication) key is in format: ISK high + ISK low");
233 PrintAndLog(" 25 (test recorded authentications)");
238 // Copy the hitag2 function into the first argument
241 // Send the command to the proxmark
245 WaitForResponse(CMD_ACK
,&resp
);
247 // Check the return status, stored in the first argument
248 if (resp
.arg
[0] == false) return 1;
250 uint32_t id
= bytes_to_num(resp
.d
.asBytes
,4);
254 sprintf(filename
,"%08x_%04x.ht2",id
,(rand() & 0xffff));
255 if ((pf
= fopen(filename
,"wb")) == NULL
) {
256 PrintAndLog("Error: Could not open file [%s]",filename
);
260 // Write the 48 tag memory bytes to file and finalize
261 fwrite(resp
.d
.asBytes
,1,48,pf
);
264 PrintAndLog("Succesfully saved tag memory to [%s]",filename
);
270 int CmdLFHitagSimS(const char *Cmd
) {
271 UsbCommand c
= { CMD_SIMULATE_HITAG_S
};
272 char filename
[FILE_PATH_SIZE
] = { 0x00 };
274 bool tag_mem_supplied
;
275 int len
= strlen(Cmd
);
276 if (len
> FILE_PATH_SIZE
)
277 len
= FILE_PATH_SIZE
;
278 memcpy(filename
, Cmd
, len
);
280 if (strlen(filename
) > 0) {
281 if ((pf
= fopen(filename
, "rb+")) == NULL
) {
282 PrintAndLog("Error: Could not open file [%s]", filename
);
285 tag_mem_supplied
= true;
286 if (fread(c
.d
.asBytes
, 4*64, 1, pf
) == 0) {
287 PrintAndLog("Error: File reading error");
293 tag_mem_supplied
= false;
296 // Does the tag comes with memory
297 c
.arg
[0] = (uint32_t) tag_mem_supplied
;
303 int CmdLFHitagCheckChallenges(const char *Cmd
) {
304 UsbCommand c
= { CMD_TEST_HITAGS_TRACES
};
305 char filename
[FILE_PATH_SIZE
] = { 0x00 };
308 int len
= strlen(Cmd
);
309 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
310 memcpy(filename
, Cmd
, len
);
312 if (strlen(filename
) > 0) {
313 if ((pf
= fopen(filename
,"rb+")) == NULL
) {
314 PrintAndLog("Error: Could not open file [%s]",filename
);
318 if (fread(c
.d
.asBytes
,8*60,1,pf
) == 0) {
319 PrintAndLog("Error: File reading error");
328 //file with all the challenges to try
329 c
.arg
[0] = (uint32_t)file_given
;
336 int CmdLFHitagWP(const char *Cmd
) {
337 UsbCommand c
= { CMD_WR_HITAG_S
};
338 hitag_data
* htd
= (hitag_data
*)c
.d
.asBytes
;
339 hitag_function htf
= param_get32ex(Cmd
,0,0,10);
341 case 03: { //WHTSF_CHALLENGE
342 num_to_bytes(param_get64ex(Cmd
,1,0,16),8,htd
->auth
.NrAr
);
343 c
.arg
[2]= param_get32ex(Cmd
, 2, 0, 10);
344 num_to_bytes(param_get32ex(Cmd
,3,0,16),4,htd
->auth
.data
);
346 case 04: { //WHTSF_KEY
347 num_to_bytes(param_get64ex(Cmd
,1,0,16),6,htd
->crypto
.key
);
348 c
.arg
[2]= param_get32ex(Cmd
, 2, 0, 10);
349 num_to_bytes(param_get32ex(Cmd
,3,0,16),4,htd
->crypto
.data
);
353 PrintAndLog("Error: unkown writer function %d",htf
);
354 PrintAndLog("Hitag writer functions");
355 PrintAndLog(" HitagS (0*)");
356 PrintAndLog(" 03 <nr,ar> (Challenge) <page> <byte0...byte3> write page on a Hitag S tag");
357 PrintAndLog(" 04 <key> (set to 0 if no authentication is needed) <page> <byte0...byte3> write page on a Hitag S tag");
358 PrintAndLog(" Hitag1 (1*)");
359 PrintAndLog(" Hitag2 (2*)");
363 // Copy the hitag function into the first argument
366 // Send the command to the proxmark
370 WaitForResponse(CMD_ACK
,&resp
);
372 // Check the return status, stored in the first argument
373 if (resp
.arg
[0] == false) return 1;
378 static command_t CommandTable
[] =
380 {"help", CmdHelp
, 1, "This help"},
381 {"list", CmdLFHitagList
, 1, "<outfile> List Hitag trace history"},
382 {"reader", CmdLFHitagReader
, 1, "Act like a Hitag Reader"},
383 {"sim", CmdLFHitagSim
, 1, "<infile> Simulate Hitag transponder"},
384 {"snoop", CmdLFHitagSnoop
, 1, "Eavesdrop Hitag communication"},
385 {"writer", CmdLFHitagWP
, 1, "Act like a Hitag Writer" },
386 {"simS", CmdLFHitagSimS
, 1, "<hitagS.hts> Simulate HitagS transponder" },
387 {"checkChallenges", CmdLFHitagCheckChallenges
, 1, "<challenges.cc> test all challenges" }, {
391 int CmdLFHitag(const char *Cmd
)
393 CmdsParse(CommandTable
, Cmd
);
397 int CmdHelp(const char *Cmd
)
399 CmdsHelp(CommandTable
);