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 "proxusb.h"
16 #include "proxmark3.h"
18 #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
)
34 GetFromBigBuf(got
,sizeof(got
),0);
35 WaitForResponse(CMD_ACK
,NULL
);
37 PrintAndLog("recorded activity:");
38 PrintAndLog(" ETU :nbits: who bytes");
39 PrintAndLog("---------+-----+----+-----------");
53 int timestamp
= *((uint32_t *)(got
+i
));
54 if (timestamp
& 0x80000000) {
55 timestamp
&= 0x7fffffff;
61 int parityBits
= *((uint32_t *)(got
+i
+4));
62 // 4 bytes of additional information...
63 // maximum of 32 additional parity bit information
66 // at each quarter bit period we can send power level (16 levels)
67 // or each half bit period in 256 levels.
70 int len
= nbytes(got
[i
+8]);
75 if (i
+ len
>= 1900) {
79 uint8_t *frame
= (got
+i
+9);
81 // Break and stick with current result if buffer was not completely full
82 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
86 for (j
= 0; j
< len
; j
++) {
91 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
94 //if((parityBits >> (len - j - 1)) & 0x01) {
95 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
96 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
99 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
103 PrintAndLog(" +%7d: %3d: %s %s",
104 (prev
< 0 ? 0 : (timestamp
- prev
)),
106 (isResponse
? "TAG" : " "),
111 fprintf(pf
," +%7d: %3d: %s %s\n",
112 (prev
< 0 ? 0 : (timestamp
- prev
)),
114 (isResponse
? "TAG" : " "),
123 PrintAndLog("Recorded activity succesfully written to file: %s", filename
);
130 int CmdLFHitagSnoop(const char *Cmd
) {
131 UsbCommand c
= {CMD_SNOOP_HITAG
};
136 int CmdLFHitagSim(const char *Cmd
) {
137 UsbCommand c
= {CMD_SIMULATE_HITAG
};
138 char filename
[256] = { 0x00 };
140 bool tag_mem_supplied
;
142 param_getstr(Cmd
,0,filename
);
144 if (strlen(filename
) > 0) {
145 if ((pf
= fopen(filename
,"rb+")) == NULL
) {
146 PrintAndLog("Error: Could not open file [%s]",filename
);
149 tag_mem_supplied
= true;
150 if (fread(c
.d
.asBytes
,48,1,pf
) == 0) {
151 PrintAndLog("Error: File reading error");
156 tag_mem_supplied
= false;
159 // Does the tag comes with memory
160 c
.arg
[0] = (uint32_t)tag_mem_supplied
;
166 int CmdLFHitagReader(const char *Cmd
) {
167 // UsbCommand c = {CMD_READER_HITAG};
169 // param_get32ex(Cmd,1,0,16);
170 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)}};
171 hitag_data
* htd
= (hitag_data
*)c
.d
.asBytes
;
172 hitag_function htf
= param_get32ex(Cmd
,0,0,10);
175 case RHT2F_PASSWORD
: {
176 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->pwd
.password
);
178 case RHT2F_AUTHENTICATE
: {
179 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->auth
.NrAr
);
180 num_to_bytes(param_get32ex(Cmd
,2,0,16),4,htd
->auth
.NrAr
+4);
183 num_to_bytes(param_get64ex(Cmd
,1,0,16),6,htd
->crypto
.key
);
184 // num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4);
186 case RHT2F_TEST_AUTH_ATTEMPTS
: {
187 // No additional parameters needed
190 PrintAndLog("Error: unkown reader function %d",htf
);
191 PrintAndLog("Hitag reader functions");
192 PrintAndLog(" HitagS (0*)");
193 PrintAndLog(" Hitag1 (1*)");
194 PrintAndLog(" Hitag2 (2*)");
195 PrintAndLog(" 21 <password> (password mode)");
196 PrintAndLog(" 22 <nr> <ar> (authentication)");
197 PrintAndLog(" 23 <key> (authentication) key is in format: ISK high + ISK low");
198 PrintAndLog(" 25 (test recorded authentications)");
203 // Copy the hitag2 function into the first argument
206 // Send the command to the proxmark
210 WaitForResponse(CMD_ACK
,&resp
);
212 // Check the return status, stored in the first argument
213 if (resp
.arg
[0] == false) return 1;
215 uint32_t id
= bytes_to_num(resp
.d
.asBytes
,4);
219 sprintf(filename
,"%08x_%04x.ht2",id
,(rand() & 0xffff));
220 if ((pf
= fopen(filename
,"wb")) == NULL
) {
221 PrintAndLog("Error: Could not open file [%s]",filename
);
225 // Write the 48 tag memory bytes to file and finalize
226 fwrite(resp
.d
.asBytes
,1,48,pf
);
229 PrintAndLog("Succesfully saved tag memory to [%s]",filename
);
234 static command_t CommandTableHitag
[] =
236 {"help", CmdHelp
, 1, "This help"},
237 {"list", CmdLFHitagList
, 1, "List Hitag trace history"},
238 {"reader", CmdLFHitagReader
, 1, "Act like a Hitag Reader"},
239 {"sim", CmdLFHitagSim
, 1, "Simulate Hitag transponder"},
240 {"snoop", CmdLFHitagSnoop
, 1, "Eavesdrop Hitag communication"},
241 {NULL
, NULL
, 0, NULL
}
244 int CmdLFHitag(const char *Cmd
)
246 CmdsParse(CommandTableHitag
, Cmd
);
250 int CmdHelp(const char *Cmd
)
252 CmdsHelp(CommandTableHitag
);