]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhitag.c
331f2c87a687f42e04c6e73503fbba62267a2816
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"
18 #include "../include/common.h"
20 #include "../include/hitag2.h"
24 static int CmdHelp(const char *Cmd
);
26 size_t nbytes(size_t nbits
) {
27 return (nbits
/8)+((nbits
%8)>0);
30 int CmdLFHitagList(const char *Cmd
)
33 GetFromBigBuf(got
,sizeof(got
),0);
34 WaitForResponse(CMD_ACK
,NULL
);
36 PrintAndLog("recorded activity:");
37 PrintAndLog(" ETU :nbits: who bytes");
38 PrintAndLog("---------+-----+----+-----------");
52 int timestamp
= *((uint32_t *)(got
+i
));
53 if (timestamp
& 0x80000000) {
54 timestamp
&= 0x7fffffff;
60 int parityBits
= *((uint32_t *)(got
+i
+4));
61 // 4 bytes of additional information...
62 // maximum of 32 additional parity bit information
65 // at each quarter bit period we can send power level (16 levels)
66 // or each half bit period in 256 levels.
69 int len
= nbytes(got
[i
+8]);
74 if (i
+ len
>= 1900) {
78 uint8_t *frame
= (got
+i
+9);
80 // Break and stick with current result if buffer was not completely full
81 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
85 for (j
= 0; j
< len
; j
++) {
90 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
93 //if((parityBits >> (len - j - 1)) & 0x01) {
94 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
95 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
98 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
102 PrintAndLog(" +%7d: %3d: %s %s",
103 (prev
< 0 ? 0 : (timestamp
- prev
)),
105 (isResponse
? "TAG" : " "),
110 fprintf(pf
," +%7d: %3d: %s %s\n",
111 (prev
< 0 ? 0 : (timestamp
- prev
)),
113 (isResponse
? "TAG" : " "),
122 PrintAndLog("Recorded activity succesfully written to file: %s", filename
);
129 int CmdLFHitagSnoop(const char *Cmd
) {
130 UsbCommand c
= {CMD_SNOOP_HITAG
};
135 int CmdLFHitagSim(const char *Cmd
) {
136 UsbCommand c
= {CMD_SIMULATE_HITAG
};
137 char filename
[256] = { 0x00 };
139 bool tag_mem_supplied
;
141 param_getstr(Cmd
,0,filename
);
143 if (strlen(filename
) > 0) {
144 if ((pf
= fopen(filename
,"rb+")) == NULL
) {
145 PrintAndLog("Error: Could not open file [%s]",filename
);
148 tag_mem_supplied
= true;
149 if (fread(c
.d
.asBytes
,48,1,pf
) == 0) {
150 PrintAndLog("Error: File reading error");
155 tag_mem_supplied
= false;
158 // Does the tag comes with memory
159 c
.arg
[0] = (uint32_t)tag_mem_supplied
;
165 int CmdLFHitagReader(const char *Cmd
) {
166 // UsbCommand c = {CMD_READER_HITAG};
168 // param_get32ex(Cmd,1,0,16);
169 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)}};
170 hitag_data
* htd
= (hitag_data
*)c
.d
.asBytes
;
171 hitag_function htf
= param_get32ex(Cmd
,0,0,10);
174 case RHT2F_PASSWORD
: {
175 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->pwd
.password
);
177 case RHT2F_AUTHENTICATE
: {
178 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->auth
.NrAr
);
179 num_to_bytes(param_get32ex(Cmd
,2,0,16),4,htd
->auth
.NrAr
+4);
182 num_to_bytes(param_get64ex(Cmd
,1,0,16),6,htd
->crypto
.key
);
183 // num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4);
185 case RHT2F_TEST_AUTH_ATTEMPTS
: {
186 // No additional parameters needed
189 PrintAndLog("Error: unkown reader function %d",htf
);
190 PrintAndLog("Hitag reader functions");
191 PrintAndLog(" HitagS (0*)");
192 PrintAndLog(" Hitag1 (1*)");
193 PrintAndLog(" Hitag2 (2*)");
194 PrintAndLog(" 21 <password> (password mode)");
195 PrintAndLog(" 22 <nr> <ar> (authentication)");
196 PrintAndLog(" 23 <key> (authentication) key is in format: ISK high + ISK low");
197 PrintAndLog(" 25 (test recorded authentications)");
202 // Copy the hitag2 function into the first argument
205 // Send the command to the proxmark
209 WaitForResponse(CMD_ACK
,&resp
);
211 // Check the return status, stored in the first argument
212 if (resp
.arg
[0] == false) return 1;
214 uint32_t id
= bytes_to_num(resp
.d
.asBytes
,4);
218 sprintf(filename
,"%08x_%04x.ht2",id
,(rand() & 0xffff));
219 if ((pf
= fopen(filename
,"wb")) == NULL
) {
220 PrintAndLog("Error: Could not open file [%s]",filename
);
224 // Write the 48 tag memory bytes to file and finalize
225 fwrite(resp
.d
.asBytes
,1,48,pf
);
228 PrintAndLog("Succesfully saved tag memory to [%s]",filename
);
233 static command_t CommandTable
[] =
235 {"help", CmdHelp
, 1, "This help"},
236 {"list", CmdLFHitagList
, 1, "List Hitag trace history"},
237 {"reader", CmdLFHitagReader
, 1, "Act like a Hitag Reader"},
238 {"sim", CmdLFHitagSim
, 1, "Simulate Hitag transponder"},
239 {"snoop", CmdLFHitagSnoop
, 1, "Eavesdrop Hitag communication"},
240 {NULL
, NULL
, 0, NULL
}
243 int CmdLFHitag(const char *Cmd
)
245 CmdsParse(CommandTable
, Cmd
);
249 int CmdHelp(const char *Cmd
)
251 CmdsHelp(CommandTable
);