]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfhitag.c
add -s to hf 14b raw to select a std 14b tag first
[proxmark3-svn] / client / cmdlfhitag.c
CommitLineData
db09cb3a 1//-----------------------------------------------------------------------------
2// Copyright (C) 2012 Roel Verdult
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// Low frequency Hitag support
9//-----------------------------------------------------------------------------
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include "data.h"
902cb3c0 15#include "proxmark3.h"
db09cb3a 16#include "ui.h"
17#include "cmdparser.h"
18#include "common.h"
19#include "util.h"
20#include "hitag2.h"
902cb3c0 21#include "sleep.h"
22#include "cmdmain.h"
db09cb3a 23
24static int CmdHelp(const char *Cmd);
25
47e18126 26size_t nbytes(size_t nbits) {
27 return (nbits/8)+((nbits%8)>0);
28}
29
db09cb3a 30int CmdLFHitagList(const char *Cmd)
31{
f71f4deb 32 uint8_t *got = malloc(USB_CMD_DATA_SIZE);
33
34 // Query for the actual size of the trace
35 UsbCommand response;
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);
41 if (p == NULL) {
42 PrintAndLog("Cannot allocate memory for trace");
43 free(got);
44 return 2;
45 }
46 got = p;
47 GetFromBigBuf(got, traceLen, 0);
48 WaitForResponse(CMD_ACK,NULL);
49 }
50
51 PrintAndLog("recorded activity (TraceLen = %d bytes):");
52 PrintAndLog(" ETU :nbits: who bytes");
53 PrintAndLog("---------+-----+----+-----------");
db09cb3a 54
f71f4deb 55 int i = 0;
56 int prev = -1;
57 int len = strlen(Cmd);
b915fda3 58
f71f4deb 59 char filename[FILE_PATH_SIZE] = { 0x00 };
60 FILE* pf = NULL;
b915fda3 61
f71f4deb 62 if (len > FILE_PATH_SIZE)
63 len = FILE_PATH_SIZE;
64 memcpy(filename, Cmd, len);
b915fda3 65
f71f4deb 66 if (strlen(filename) > 0) {
67 if ((pf = fopen(filename,"wb")) == NULL) {
68 PrintAndLog("Error: Could not open file [%s]",filename);
69 return 1;
70 }
b915fda3 71 }
db09cb3a 72
f71f4deb 73 for (;;) {
b915fda3 74
f71f4deb 75 if(i > traceLen) { break; }
76
77 bool isResponse;
78 int timestamp = *((uint32_t *)(got+i));
79 if (timestamp & 0x80000000) {
80 timestamp &= 0x7fffffff;
81 isResponse = 1;
82 } else {
83 isResponse = 0;
84 }
85
86 int parityBits = *((uint32_t *)(got+i+4));
87 // 4 bytes of additional information...
88 // maximum of 32 additional parity bit information
89 //
90 // TODO:
91 // at each quarter bit period we can send power level (16 levels)
92 // or each half bit period in 256 levels.
93
94 int bits = got[i+8];
95 int len = nbytes(got[i+8]);
96
97 if (len > 100) {
98 break;
99 }
100 if (i + len > traceLen) { break;}
101
102 uint8_t *frame = (got+i+9);
103
104 // Break and stick with current result if buffer was not completely full
105 if (frame[0] == 0x44 && frame[1] == 0x44 && frame[3] == 0x44) { break; }
106
107 char line[1000] = "";
108 int j;
109 for (j = 0; j < len; j++) {
110 int oddparity = 0x01;
111 int k;
112
113 for (k=0;k<8;k++) {
114 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
115 }
116
117 //if((parityBits >> (len - j - 1)) & 0x01) {
118 if (isResponse && (oddparity != ((parityBits >> (len - j - 1)) & 0x01))) {
119 sprintf(line+(j*4), "%02x! ", frame[j]);
120 }
121 else {
122 sprintf(line+(j*4), "%02x ", frame[j]);
123 }
124 }
125
126 PrintAndLog(" +%7d: %3d: %s %s",
127 (prev < 0 ? 0 : (timestamp - prev)),
128 bits,
129 (isResponse ? "TAG" : " "),
130 line);
131
132 if (pf) {
133 fprintf(pf," +%7d: %3d: %s %s\n",
134 (prev < 0 ? 0 : (timestamp - prev)),
135 bits,
136 (isResponse ? "TAG" : " "),
137 line);
138 }
139
140 prev = timestamp;
141 i += (len + 9);
142 }
2d495a81 143
f71f4deb 144 if (pf) {
145 fclose(pf);
146 PrintAndLog("Recorded activity succesfully written to file: %s", filename);
147 }
97d582a6 148
f71f4deb 149 free(got);
150 return 0;
db09cb3a 151}
152
153int CmdLFHitagSnoop(const char *Cmd) {
154 UsbCommand c = {CMD_SNOOP_HITAG};
155 SendCommand(&c);
156 return 0;
157}
158
159int CmdLFHitagSim(const char *Cmd) {
b915fda3 160
db09cb3a 161 UsbCommand c = {CMD_SIMULATE_HITAG};
b915fda3 162 char filename[FILE_PATH_SIZE] = { 0x00 };
db09cb3a 163 FILE* pf;
164 bool tag_mem_supplied;
b915fda3 165 int len = strlen(Cmd);
166 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
167 memcpy(filename, Cmd, len);
db09cb3a 168
169 if (strlen(filename) > 0) {
170 if ((pf = fopen(filename,"rb+")) == NULL) {
171 PrintAndLog("Error: Could not open file [%s]",filename);
172 return 1;
173 }
174 tag_mem_supplied = true;
759c16b3 175 if (fread(c.d.asBytes,48,1,pf) == 0) {
176 PrintAndLog("Error: File reading error");
90e278d3 177 fclose(pf);
759c16b3 178 return 1;
179 }
db09cb3a 180 fclose(pf);
181 } else {
182 tag_mem_supplied = false;
183 }
184
185 // Does the tag comes with memory
186 c.arg[0] = (uint32_t)tag_mem_supplied;
187
188 SendCommand(&c);
189 return 0;
190}
191
192int CmdLFHitagReader(const char *Cmd) {
193// UsbCommand c = {CMD_READER_HITAG};
194
195// param_get32ex(Cmd,1,0,16);
196 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)}};
197 hitag_data* htd = (hitag_data*)c.d.asBytes;
198 hitag_function htf = param_get32ex(Cmd,0,0,10);
199
200 switch (htf) {
201 case RHT2F_PASSWORD: {
202 num_to_bytes(param_get32ex(Cmd,1,0,16),4,htd->pwd.password);
203 } break;
204 case RHT2F_AUTHENTICATE: {
205 num_to_bytes(param_get32ex(Cmd,1,0,16),4,htd->auth.NrAr);
206 num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4);
207 } break;
bde10a50 208 case RHT2F_CRYPTO: {
209 num_to_bytes(param_get64ex(Cmd,1,0,16),6,htd->crypto.key);
210// num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4);
211 } break;
db09cb3a 212 case RHT2F_TEST_AUTH_ATTEMPTS: {
213 // No additional parameters needed
214 } break;
215 default: {
216 PrintAndLog("Error: unkown reader function %d",htf);
ab4da50d 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)");
db09cb3a 225 return 1;
226 } break;
227 }
228
229 // Copy the hitag2 function into the first argument
230 c.arg[0] = htf;
231
ab4da50d 232 // Send the command to the proxmark
db09cb3a 233 SendCommand(&c);
ab4da50d 234
235 UsbCommand resp;
236 WaitForResponse(CMD_ACK,&resp);
237
238 // Check the return status, stored in the first argument
239 if (resp.arg[0] == false) return 1;
240
241 uint32_t id = bytes_to_num(resp.d.asBytes,4);
242 char filename[256];
243 FILE* pf = NULL;
244
245 sprintf(filename,"%08x_%04x.ht2",id,(rand() & 0xffff));
246 if ((pf = fopen(filename,"wb")) == NULL) {
247 PrintAndLog("Error: Could not open file [%s]",filename);
248 return 1;
249 }
250
251 // Write the 48 tag memory bytes to file and finalize
252 fwrite(resp.d.asBytes,1,48,pf);
253 fclose(pf);
254
255 PrintAndLog("Succesfully saved tag memory to [%s]",filename);
256
db09cb3a 257 return 0;
258}
259
3fe4ff4f 260static command_t CommandTable[] =
db09cb3a 261{
262 {"help", CmdHelp, 1, "This help"},
b915fda3 263 {"list", CmdLFHitagList, 1, "<outfile> List Hitag trace history"},
db09cb3a 264 {"reader", CmdLFHitagReader, 1, "Act like a Hitag Reader"},
b915fda3 265 {"sim", CmdLFHitagSim, 1, "<infile> Simulate Hitag transponder"},
db09cb3a 266 {"snoop", CmdLFHitagSnoop, 1, "Eavesdrop Hitag communication"},
267 {NULL, NULL, 0, NULL}
268};
269
270int CmdLFHitag(const char *Cmd)
271{
3fe4ff4f 272 CmdsParse(CommandTable, Cmd);
db09cb3a 273 return 0;
274}
275
276int CmdHelp(const char *Cmd)
277{
3fe4ff4f 278 CmdsHelp(CommandTable);
db09cb3a 279 return 0;
280}
Impressum, Datenschutz