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