]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfhid.c
Update list of known EMV AIDs. Source: https://www.eftlab.co.uk/index.php/site-map...
[proxmark3-svn] / client / cmdlfhid.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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//-----------------------------------------------------------------------------
b9957414 8// Low frequency HID commands (known)
ab20cc35
MF
9//
10// Useful resources:
11// RF interface, programming a T55x7 clone, 26-bit HID H10301 encoding:
12// http://www.proxmark.org/files/Documents/125%20kHz%20-%20HID/HID_format_example.pdf
13//
14// "Understanding Card Data Formats"
15// https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf
16//
17// "What Format Do You Need?"
18// https://www.hidglobal.com/sites/default/files/resource_files/hid-prox-br-en.pdf
a553f267 19//-----------------------------------------------------------------------------
20
ad939de5 21#include "cmdlfhid.h"
22
7fe9b0b7 23#include <stdio.h>
54a942b0 24#include <string.h>
ad939de5 25#include "comms.h"
7fe9b0b7 26#include "ui.h"
27#include "graph.h"
28#include "cmdparser.h"
6cd2eef4 29#include "cmddata.h" //for g_debugMode, demodbuff cmds
30#include "lfdemod.h" // for HIDdemodFSK
b5a5fc4d 31#include "hidcardformats.h"
32#include "hidcardformatutils.h"
1ee624fe 33#include "util.h" // for param_get8,32,64
7fe9b0b7 34
ab20cc35 35
ab20cc35 36/**
1ee624fe 37 * Converts a hex string to component "hi2", "hi" and "lo" 32-bit integers, one nibble
ab20cc35
MF
38 * at a time.
39 *
40 * Returns the number of nibbles (4 bits) entered.
41 */
5f84531b 42int hid_hexstring_to_int96(/* out */ uint32_t* hi2,/* out */ uint32_t* hi, /* out */ uint32_t* lo, const char* str) {
ab20cc35
MF
43 // TODO: Replace this with param_gethex when it supports arbitrary length
44 // inputs.
45 int n = 0, i = 0;
46
47 while (sscanf(&str[i++], "%1x", &n ) == 1) {
1ee624fe 48 *hi2 = (*hi2 << 4) | (*hi >> 28);
ab20cc35
MF
49 *hi = (*hi << 4) | (*lo >> 28);
50 *lo = (*lo << 4) | (n & 0xf);
51 }
52
53 return i - 1;
54}
55
be59094d 56void usage_encode(){
57 PrintAndLog("Usage: lf hid encode <format> [<field> <value (decimal)>] {...}");
58 PrintAndLog(" Fields: c: Card number");
59 PrintAndLog(" f: Facility code");
60 PrintAndLog(" i: Issue Level");
61 PrintAndLog(" o: OEM code");
62 PrintAndLog(" example: lf hid encode H10301 f 123 c 4567");
63}
64void PrintProxTagId(hidproxmessage_t *packed){
65 if (packed->top != 0) {
66 PrintAndLog("HID Prox TAG ID: %x%08x%08x",
67 (uint32_t)packed->top, (uint32_t)packed->mid, (uint32_t)packed->bot);
68 } else {
69 PrintAndLog("HID Prox TAG ID: %x%08x",
70 (uint32_t)packed->mid, (uint32_t)packed->bot);
71 }
72}
73bool Encode(/* in */ const char *Cmd, /* out */ hidproxmessage_t *packed){
74 int formatIndex = -1;
75 char format[16];
76 memset(format, 0, sizeof(format));
77 if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){
78 usage_encode();
79 return false;
80 } else {
81 param_getstr(Cmd, 0, format, sizeof(format));
82 formatIndex = HIDFindCardFormat(format);
83 if (formatIndex == -1) {
84 printf("Unknown format: %s\r\n", format);
85 return false;
86 }
87 }
88 hidproxcard_t data;
89 memset(&data, 0, sizeof(hidproxcard_t));
90 uint8_t cmdp = 1;
91 while(param_getchar(Cmd, cmdp) != 0x00) {
92 switch(param_getchar(Cmd, cmdp)) {
93 case 'I':
94 case 'i':
95 data.IssueLevel = param_get32ex(Cmd, cmdp+1, 0, 10);
96 cmdp += 2;
97 break;
98 case 'F':
99 case 'f':
100 data.FacilityCode = param_get32ex(Cmd, cmdp+1, 0, 10);
101 cmdp += 2;
102 break;
103 case 'C':
104 case 'c':
105 data.CardNumber = param_get64ex(Cmd, cmdp+1, 0, 10);
106 cmdp += 2;
107 break;
108 case 'o':
109 case 'O':
110 data.OEM = param_get32ex(Cmd, cmdp+1, 0, 10);
111 cmdp += 2;
112 break;
113 default:
114 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
115 return false;
116 }
117 }
118 memset(packed, 0, sizeof(hidproxmessage_t));
119 if (!HIDPack(formatIndex, &data, packed))
120 {
121 PrintAndLog("The card data could not be encoded in the selected format.");
122 return false;
123 } else {
124 return true;
125 }
126
127}
128void Write(hidproxmessage_t *packed){
129 UsbCommand c;
130 c.d.asBytes[0] = (packed->top != 0 && ((packed->mid & 0xFFFFFFC0) != 0))
131 ? 1 : 0; // Writing long format?
132 c.cmd = CMD_HID_CLONE_TAG;
133 c.arg[0] = (packed->top & 0x000FFFFF);
134 c.arg[1] = packed->mid;
135 c.arg[2] = packed->bot;
136 SendCommand(&c);
137}
138
139
6cd2eef4 140//by marshmellow (based on existing demod + holiman's refactor)
141//HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
142//print full HID Prox ID and some bit format details if found
143int CmdFSKdemodHID(const char *Cmd)
7fe9b0b7 144{
6cd2eef4 145 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
146 uint32_t hi2=0, hi=0, lo=0;
147
148 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
149 size_t BitLen = getFromGraphBuf(BitStream);
150 if (BitLen==0) return 0;
151 //get binary from fsk wave
1c70664a 152 int waveIdx = 0;
153 int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo, &waveIdx);
6cd2eef4 154 if (idx<0){
155 if (g_debugMode){
156 if (idx==-1){
157 PrintAndLog("DEBUG: Just Noise Detected");
158 } else if (idx == -2) {
159 PrintAndLog("DEBUG: Error demoding fsk");
160 } else if (idx == -3) {
161 PrintAndLog("DEBUG: Preamble not found");
162 } else if (idx == -4) {
163 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen);
164 } else {
165 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
166 }
167 }
7fe9b0b7 168 return 0;
169 }
6cd2eef4 170 if (hi2==0 && hi==0 && lo==0) {
171 if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
172 return 0;
173 }
1ee624fe 174
b5a5fc4d 175 hidproxmessage_t packed = initialize_proxmessage_object(hi2, hi, lo);
be59094d 176 PrintProxTagId(&packed);
ab20cc35 177
be59094d 178 bool ret = HIDTryUnpack(&packed, false);
1ee624fe 179 if (!ret) {
180 PrintAndLog("Invalid or unsupported tag length.");
7fe9b0b7 181 }
6cd2eef4 182 setDemodBuf(BitStream,BitLen,idx);
1c70664a 183 setClockGrid(50, waveIdx + (idx*50));
6cd2eef4 184 if (g_debugMode){
185 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
186 printDemodBuff();
187 }
188 return 1;
7fe9b0b7 189}
6cd2eef4 190
191int CmdHIDReadFSK(const char *Cmd)
7fe9b0b7 192{
083ca3de 193 int findone=0;
ab20cc35 194 if(Cmd[0]=='1') findone=1;
7fe9b0b7 195 UsbCommand c={CMD_HID_DEMOD_FSK};
083ca3de 196 c.arg[0]=findone;
7fe9b0b7 197 SendCommand(&c);
198 return 0;
199}
200
201int CmdHIDSim(const char *Cmd)
38b20f75 202{
1ee624fe 203 uint32_t hi2 = 0, hi = 0, lo = 0;
5f84531b 204 hid_hexstring_to_int96(&hi2, &hi, &lo, Cmd);
be59094d 205 if (hi2 != 0) {
206 PrintAndLog("Emulating tag with ID %x%08x%08x", hi2, hi, lo);
207 } else {
208 PrintAndLog("Emulating tag with ID %x%08x", hi, lo);
ab20cc35 209 }
38b20f75 210
ab20cc35 211 PrintAndLog("Press pm3-button to abort simulation");
38b20f75 212
be59094d 213 UsbCommand c = {CMD_HID_SIM_TAG, {hi2, hi, lo}};
ab20cc35
MF
214 SendCommand(&c);
215 return 0;
38b20f75 216}
217
218int CmdHIDClone(const char *Cmd)
7fe9b0b7 219{
be59094d 220 unsigned int top = 0, mid = 0, bot = 0;
5f84531b 221 hid_hexstring_to_int96(&top, &mid, &bot, Cmd);
be59094d 222 hidproxmessage_t packed = initialize_proxmessage_object(top, mid, bot);
223 Write(&packed);
ec09b62d 224 return 0;
225}
226
b5a5fc4d 227int CmdHIDDecode(const char *Cmd){
ab20cc35 228 if (strlen(Cmd)<3) {
5d643cc0 229 PrintAndLog("Usage: lf hid decode <id> {p}");
230 PrintAndLog(" (optional) p: Ignore invalid parity");
b5a5fc4d 231 PrintAndLog(" sample: lf hid decode 2006f623ae");
ab20cc35
MF
232 return 0;
233 }
234
b5a5fc4d 235 uint32_t top = 0, mid = 0, bot = 0;
5d643cc0 236 bool ignoreParity = false;
5f84531b 237 hid_hexstring_to_int96(&top, &mid, &bot, Cmd);
b5a5fc4d 238 hidproxmessage_t packed = initialize_proxmessage_object(top, mid, bot);
5d643cc0 239
240 char opt = param_getchar(Cmd, 1);
241 if (opt == 'p') ignoreParity = true;
242
243 HIDTryUnpack(&packed, ignoreParity);
b5a5fc4d 244 return 0;
245}
246int CmdHIDEncode(const char *Cmd) {
247 if (strlen(Cmd) == 0) {
be59094d 248 usage_encode();
b5a5fc4d 249 return 0;
250 }
251
b5a5fc4d 252 hidproxmessage_t packed;
253 memset(&packed, 0, sizeof(hidproxmessage_t));
be59094d 254 if (Encode(Cmd, &packed))
255 PrintProxTagId(&packed);
ab20cc35
MF
256 return 0;
257}
258
b5a5fc4d 259int CmdHIDWrite(const char *Cmd) {
260 if (strlen(Cmd) == 0) {
be59094d 261 usage_encode();
ab20cc35
MF
262 return 0;
263 }
b5a5fc4d 264 hidproxmessage_t packed;
265 memset(&packed, 0, sizeof(hidproxmessage_t));
be59094d 266 if (Encode(Cmd, &packed)){
267 PrintProxTagId(&packed);
268 Write(&packed);
ab20cc35
MF
269 }
270 return 0;
271}
272
be59094d 273int CmdHIDFormats(){
274 HIDListFormats();
275 return 0;
276}
b5a5fc4d 277static int CmdHelp(const char *Cmd); // define this now so the below won't error out.
7fe9b0b7 278static command_t CommandTable[] =
279{
280 {"help", CmdHelp, 1, "This help"},
6cd2eef4 281 {"demod", CmdFSKdemodHID, 1, "Demodulate HID Prox from GraphBuffer"},
282 {"read", CmdHIDReadFSK, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"},
083ca3de 283 {"sim", CmdHIDSim, 0, "<ID> -- HID tag simulator"},
1ee624fe 284 {"clone", CmdHIDClone, 0, "<ID> -- Clone HID to T55x7 (tag must be in antenna)"},
b5a5fc4d 285 {"decode", CmdHIDDecode, 1, "<ID> -- Try to decode an HID tag and show its contents"},
be59094d 286 {"encode", CmdHIDEncode, 1, "<format> <fields> -- Encode an HID ID with the specified format and fields"},
287 {"formats", CmdHIDFormats, 1, "List supported card formats"},
288 {"write", CmdHIDWrite, 0, "<format> <fields> -- Encode and write to a T55x7 tag (tag must be in antenna)"},
7fe9b0b7 289 {NULL, NULL, 0, NULL}
290};
291
292int CmdLFHID(const char *Cmd)
293{
294 CmdsParse(CommandTable, Cmd);
295 return 0;
296}
297
298int CmdHelp(const char *Cmd)
299{
300 CmdsHelp(CommandTable);
301 return 0;
302}
Impressum, Datenschutz