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 //-----------------------------------------------------------------------------
17 #include "cmdparser.h"
22 static int CmdHelp(const char *Cmd
);
24 int CmdLFHitagList(const char *Cmd
)
27 GetFromBigBuf(got
,sizeof(got
),0);
29 PrintAndLog("recorded activity:");
30 PrintAndLog(" ETU :rssi: who bytes");
31 PrintAndLog("---------+----+----+-----------");
42 int timestamp
= *((uint32_t *)(got
+i
));
43 if (timestamp
& 0x80000000) {
44 timestamp
&= 0x7fffffff;
51 int parityBits
= *((uint32_t *)(got
+i
+4));
52 // 4 bytes of additional information...
53 // maximum of 32 additional parity bit information
56 // at each quarter bit period we can send power level (16 levels)
57 // or each half bit period in 256 levels.
64 if (i
+ len
>= 1900) {
68 uint8_t *frame
= (got
+i
+9);
70 // Break and stick with current result if buffer was not completely full
71 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
75 for (j
= 0; j
< len
; j
++) {
80 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
83 //if((parityBits >> (len - j - 1)) & 0x01) {
84 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
85 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
88 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
92 char metricString
[100];
94 sprintf(metricString
, "%3d", metric
);
96 strcpy(metricString
, " ");
99 PrintAndLog(" +%7d: %s: %s %s",
100 (prev
< 0 ? 0 : (timestamp
- prev
)),
102 (isResponse
? "TAG" : " "),
111 int CmdLFHitagSnoop(const char *Cmd
) {
112 UsbCommand c
= {CMD_SNOOP_HITAG
};
117 int CmdLFHitagSim(const char *Cmd
) {
118 UsbCommand c
= {CMD_SIMULATE_HITAG
};
121 bool tag_mem_supplied
;
123 param_getstr(Cmd
,0,filename
);
125 if (strlen(filename
) > 0) {
126 if ((pf
= fopen(filename
,"rb+")) == NULL
) {
127 PrintAndLog("Error: Could not open file [%s]",filename
);
130 tag_mem_supplied
= true;
131 fread(c
.d
.asBytes
,48,1,pf
);
134 tag_mem_supplied
= false;
137 // Does the tag comes with memory
138 c
.arg
[0] = (uint32_t)tag_mem_supplied
;
144 int CmdLFHitagReader(const char *Cmd
) {
145 // UsbCommand c = {CMD_READER_HITAG};
147 // param_get32ex(Cmd,1,0,16);
148 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)}};
149 hitag_data
* htd
= (hitag_data
*)c
.d
.asBytes
;
150 hitag_function htf
= param_get32ex(Cmd
,0,0,10);
153 case RHT2F_PASSWORD
: {
154 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->pwd
.password
);
156 case RHT2F_AUTHENTICATE
: {
157 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->auth
.NrAr
);
158 num_to_bytes(param_get32ex(Cmd
,2,0,16),4,htd
->auth
.NrAr
+4);
160 case RHT2F_TEST_AUTH_ATTEMPTS
: {
161 // No additional parameters needed
164 PrintAndLog("Error: unkown reader function %d",htf
);
165 PrintAndLog("Hitag reader functions",htf
);
166 PrintAndLog(" HitagS (0*)",htf
);
167 PrintAndLog(" Hitag1 (1*)",htf
);
168 PrintAndLog(" Hitag2 (2*)",htf
);
169 PrintAndLog(" 21 <password> (password mode)",htf
);
170 PrintAndLog(" 22 <nr> <ar> (authentication)",htf
);
171 PrintAndLog(" 25 (test recorded authentications)",htf
);
176 // Copy the hitag2 function into the first argument
183 static command_t CommandTableHitag
[] =
185 {"help", CmdHelp
, 1, "This help"},
186 {"list", CmdLFHitagList
, 1, "List Hitag trace history"},
187 {"reader", CmdLFHitagReader
, 1, "Act like a Hitag Reader"},
188 {"sim", CmdLFHitagSim
, 1, "Simulate Hitag transponder"},
189 {"snoop", CmdLFHitagSnoop
, 1, "Eavesdrop Hitag communication"},
190 {NULL
, NULL
, 0, NULL
}
193 int CmdLFHitag(const char *Cmd
)
195 CmdsParse(CommandTableHitag
, Cmd
);
199 int CmdHelp(const char *Cmd
)
201 CmdsHelp(CommandTableHitag
);