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 //-----------------------------------------------------------------------------
16 #include "proxmark3.h"
18 #include "cmdparser.h"
25 static int CmdHelp(const char *Cmd
);
27 int CmdLFHitagList(const char *Cmd
)
30 GetFromBigBuf(got
,sizeof(got
),0);
31 WaitForResponse(CMD_ACK
,NULL
);
36 if (param_getstr(Cmd
,0,filename
)) {
37 if (strlen(filename
) > 0) {
38 if ((pf
= fopen(filename
,"w")) == NULL
) {
39 PrintAndLog("Error: Could not open file [%s]",filename
);
45 PrintAndLog("recorded activity:");
46 PrintAndLog(" ETU :rssi: who bytes");
47 PrintAndLog("---------+----+----+-----------");
58 int timestamp
= *((uint32_t *)(got
+i
));
59 if (timestamp
& 0x80000000) {
60 timestamp
&= 0x7fffffff;
67 int parityBits
= *((uint32_t *)(got
+i
+4));
68 // 4 bytes of additional information...
69 // maximum of 32 additional parity bit information
72 // at each quarter bit period we can send power level (16 levels)
73 // or each half bit period in 256 levels.
80 if (i
+ len
>= 1900) {
84 uint8_t *frame
= (got
+i
+9);
86 // Break and stick with current result if buffer was not completely full
87 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
91 for (j
= 0; j
< len
; j
++) {
96 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
99 //if((parityBits >> (len - j - 1)) & 0x01) {
100 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
101 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
104 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
108 char metricString
[100];
110 sprintf(metricString
, "%3d", metric
);
112 strcpy(metricString
, " ");
115 PrintAndLog(" +%7d: %s: %s %s",
116 (prev
< 0 ? 0 : (timestamp
- prev
)),
118 (isResponse
? "TAG" : " "),
123 fprintf(pf
," +%7d: %s: %s %s %s",
124 (prev
< 0 ? 0 : (timestamp
- prev
)),
126 (isResponse
? "TAG" : " "),
136 PrintAndLog("Recorded activity succesfully written to file: %s", filename
);
143 int CmdLFHitagSnoop(const char *Cmd
) {
144 UsbCommand c
= {CMD_SNOOP_HITAG
};
149 int CmdLFHitagSim(const char *Cmd
) {
150 UsbCommand c
= {CMD_SIMULATE_HITAG
};
151 char filename
[256] = { 0x00 };
153 bool tag_mem_supplied
;
155 param_getstr(Cmd
,0,filename
);
157 if (strlen(filename
) > 0) {
158 if ((pf
= fopen(filename
,"rb+")) == NULL
) {
159 PrintAndLog("Error: Could not open file [%s]",filename
);
162 tag_mem_supplied
= true;
163 fread(c
.d
.asBytes
,48,1,pf
);
166 tag_mem_supplied
= false;
169 // Does the tag comes with memory
170 c
.arg
[0] = (uint32_t)tag_mem_supplied
;
176 int CmdLFHitagReader(const char *Cmd
) {
177 // UsbCommand c = {CMD_READER_HITAG};
179 // param_get32ex(Cmd,1,0,16);
180 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)}};
181 hitag_data
* htd
= (hitag_data
*)c
.d
.asBytes
;
182 hitag_function htf
= param_get32ex(Cmd
,0,0,10);
185 case RHT2F_PASSWORD
: {
186 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->pwd
.password
);
188 case RHT2F_AUTHENTICATE
: {
189 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->auth
.NrAr
);
190 num_to_bytes(param_get32ex(Cmd
,2,0,16),4,htd
->auth
.NrAr
+4);
193 num_to_bytes(param_get64ex(Cmd
,1,0,16),6,htd
->crypto
.key
);
194 // num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4);
196 case RHT2F_TEST_AUTH_ATTEMPTS
: {
197 // No additional parameters needed
200 PrintAndLog("Error: unkown reader function %d",htf
);
201 PrintAndLog("Hitag reader functions",htf
);
202 PrintAndLog(" HitagS (0*)",htf
);
203 PrintAndLog(" Hitag1 (1*)",htf
);
204 PrintAndLog(" Hitag2 (2*)",htf
);
205 PrintAndLog(" 21 <password> (password mode)",htf
);
206 PrintAndLog(" 22 <nr> <ar> (authentication)",htf
);
207 PrintAndLog(" 23 <key> (authentication) key is in format: ISK high + ISK low",htf
);
208 PrintAndLog(" 25 (test recorded authentications)",htf
);
213 // Copy the hitag2 function into the first argument
220 static command_t CommandTableHitag
[] =
222 {"help", CmdHelp
, 1, "This help"},
223 {"list", CmdLFHitagList
, 1, "List Hitag trace history"},
224 {"reader", CmdLFHitagReader
, 1, "Act like a Hitag Reader"},
225 {"sim", CmdLFHitagSim
, 1, "Simulate Hitag transponder"},
226 {"snoop", CmdLFHitagSnoop
, 1, "Eavesdrop Hitag communication"},
227 {NULL
, NULL
, 0, NULL
}
230 int CmdLFHitag(const char *Cmd
)
232 CmdsParse(CommandTableHitag
, Cmd
);
236 int CmdHelp(const char *Cmd
)
238 CmdsHelp(CommandTableHitag
);