]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhitag.c
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"
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
)
32 uint8_t *got
= malloc(USB_CMD_DATA_SIZE
);
34 // Query for the actual size of the trace
36 GetFromBigBuf(got
, USB_CMD_DATA_SIZE
, 0);
37 WaitForResponse(CMD_ACK
, &response
);
38 uint16_t traceLen
= response
.arg
[2];
39 if (traceLen
> USB_CMD_DATA_SIZE
) {
40 uint8_t *p
= realloc(got
, traceLen
);
42 PrintAndLog("Cannot allocate memory for trace");
47 GetFromBigBuf(got
, traceLen
, 0);
48 WaitForResponse(CMD_ACK
,NULL
);
51 PrintAndLog("recorded activity (TraceLen = %d bytes):");
52 PrintAndLog(" ETU :nbits: who bytes");
53 PrintAndLog("---------+-----+----+-----------");
57 int len
= strlen(Cmd
);
59 char filename
[FILE_PATH_SIZE
] = { 0x00 };
62 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
63 memcpy(filename
, Cmd
, len
);
65 if (strlen(filename
) > 0) {
66 if ((pf
= fopen(filename
,"wb")) == NULL
) {
67 PrintAndLog("Error: Could not open file [%s]",filename
);
74 if(i
>= traceLen
) { break; }
77 int timestamp
= *((uint32_t *)(got
+i
));
78 if (timestamp
& 0x80000000) {
79 timestamp
&= 0x7fffffff;
85 int parityBits
= *((uint32_t *)(got
+i
+4));
86 // 4 bytes of additional information...
87 // maximum of 32 additional parity bit information
90 // at each quarter bit period we can send power level (16 levels)
91 // or each half bit period in 256 levels.
94 int len
= nbytes(got
[i
+8]);
99 if (i
+ len
> traceLen
) { break;}
101 uint8_t *frame
= (got
+i
+9);
103 // Break and stick with current result if buffer was not completely full
104 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
106 char line
[1000] = "";
108 for (j
= 0; j
< len
; j
++) {
109 int oddparity
= 0x01;
113 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
116 //if((parityBits >> (len - j - 1)) & 0x01) {
117 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
118 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
121 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
125 PrintAndLog(" +%7d: %3d: %s %s",
126 (prev
< 0 ? 0 : (timestamp
- prev
)),
128 (isResponse
? "TAG" : " "),
132 fprintf(pf
," +%7d: %3d: %s %s\n",
133 (prev
< 0 ? 0 : (timestamp
- prev
)),
135 (isResponse
? "TAG" : " "),
145 PrintAndLog("Recorded activity succesfully written to file: %s", filename
);
152 int CmdLFHitagSnoop(const char *Cmd
) {
153 UsbCommand c
= {CMD_SNOOP_HITAG
};
154 clearCommandBuffer();
159 int CmdLFHitagSim(const char *Cmd
) {
161 UsbCommand c
= {CMD_SIMULATE_HITAG
};
162 char filename
[FILE_PATH_SIZE
] = { 0x00 };
164 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
;
189 clearCommandBuffer();
194 int CmdLFHitagReader(const char *Cmd
) {
197 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)}};
198 hitag_data
* htd
= (hitag_data
*)c
.d
.asBytes
;
199 hitag_function htf
= param_get32ex(Cmd
,0,0,10);
202 case RHT2F_PASSWORD
: {
203 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->pwd
.password
);
205 case RHT2F_AUTHENTICATE
: {
206 num_to_bytes(param_get32ex(Cmd
,1,0,16),4,htd
->auth
.NrAr
);
207 num_to_bytes(param_get32ex(Cmd
,2,0,16),4,htd
->auth
.NrAr
+4);
210 num_to_bytes(param_get64ex(Cmd
,1,0,16),6,htd
->crypto
.key
);
212 case RHT2F_TEST_AUTH_ATTEMPTS
: {
213 // No additional parameters needed
216 PrintAndLog("Error: unkown reader function %d",htf
);
217 PrintAndLog("Hitag reader functions");
218 PrintAndLog(" HitagS (0*)");
219 PrintAndLog(" Hitag1 (1*)");
220 PrintAndLog(" Hitag2 (2*)");
221 PrintAndLog(" 21 <password> (password mode)");
222 PrintAndLog(" 22 <nr> <ar> (authentication)");
223 PrintAndLog(" 23 <key> (authentication) key is in format: ISK high + ISK low");
224 PrintAndLog(" 25 (test recorded authentications)");
229 // Copy the hitag2 function into the first argument
232 clearCommandBuffer();
233 // Send the command to the proxmark
237 WaitForResponse(CMD_ACK
,&resp
);
239 // Check the return status, stored in the first argument
240 if (resp
.arg
[0] == false) return 1;
242 uint32_t id
= bytes_to_num(resp
.d
.asBytes
,4);
243 char filename
[FILE_PATH_SIZE
];
246 sprintf(filename
,"%08x_%04x.ht2",id
,(rand() & 0xffff));
247 if ((pf
= fopen(filename
,"wb")) == NULL
) {
248 PrintAndLog("Error: Could not open file [%s]",filename
);
252 // Write the 48 tag memory bytes to file and finalize
253 fwrite(resp
.d
.asBytes
,1,48,pf
);
256 PrintAndLog("Succesfully saved tag memory to [%s]",filename
);
260 static command_t CommandTable
[] = {
261 {"help", CmdHelp
, 1, "This help"},
262 {"list", CmdLFHitagList
, 1, "<outfile> List Hitag trace history"},
263 {"reader", CmdLFHitagReader
, 1, "Act like a Hitag Reader"},
264 {"sim", CmdLFHitagSim
, 1, "<infile> Simulate Hitag transponder"},
265 {"snoop", CmdLFHitagSnoop
, 1, "Eavesdrop Hitag communication"},
266 {NULL
, NULL
, 0, NULL
}
269 int CmdLFHitag(const char *Cmd
)
271 CmdsParse(CommandTable
, Cmd
);
275 int CmdHelp(const char *Cmd
)
277 CmdsHelp(CommandTable
);