]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfhid.c
Redesign of lf hid card format handler as discussed with @marshmellow42
[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 */
1ee624fe 42int 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
6cd2eef4 56//by marshmellow (based on existing demod + holiman's refactor)
57//HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
58//print full HID Prox ID and some bit format details if found
59int CmdFSKdemodHID(const char *Cmd)
7fe9b0b7 60{
6cd2eef4 61 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
62 uint32_t hi2=0, hi=0, lo=0;
63
64 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
65 size_t BitLen = getFromGraphBuf(BitStream);
66 if (BitLen==0) return 0;
67 //get binary from fsk wave
1c70664a 68 int waveIdx = 0;
69 int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo, &waveIdx);
6cd2eef4 70 if (idx<0){
71 if (g_debugMode){
72 if (idx==-1){
73 PrintAndLog("DEBUG: Just Noise Detected");
74 } else if (idx == -2) {
75 PrintAndLog("DEBUG: Error demoding fsk");
76 } else if (idx == -3) {
77 PrintAndLog("DEBUG: Preamble not found");
78 } else if (idx == -4) {
79 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen);
80 } else {
81 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
82 }
83 }
7fe9b0b7 84 return 0;
85 }
6cd2eef4 86 if (hi2==0 && hi==0 && lo==0) {
87 if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
88 return 0;
89 }
1ee624fe 90
1ee624fe 91 if (hi2 != 0)
b5a5fc4d 92 PrintAndLog("HID Prox TAG ID: %x%08x%08x",
93 (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo
94 );
1ee624fe 95 else
b5a5fc4d 96 PrintAndLog("HID Prox TAG ID: %x%08x",
97 (unsigned int) hi, (unsigned int) lo
98 );
99
100 hidproxmessage_t packed = initialize_proxmessage_object(hi2, hi, lo);
101 bool ret = HIDTryUnpack(&packed);
ab20cc35 102
1ee624fe 103 if (!ret) {
104 PrintAndLog("Invalid or unsupported tag length.");
7fe9b0b7 105 }
6cd2eef4 106 setDemodBuf(BitStream,BitLen,idx);
1c70664a 107 setClockGrid(50, waveIdx + (idx*50));
6cd2eef4 108 if (g_debugMode){
109 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
110 printDemodBuff();
111 }
112 return 1;
7fe9b0b7 113}
6cd2eef4 114
115int CmdHIDReadFSK(const char *Cmd)
7fe9b0b7 116{
083ca3de 117 int findone=0;
ab20cc35 118 if(Cmd[0]=='1') findone=1;
7fe9b0b7 119 UsbCommand c={CMD_HID_DEMOD_FSK};
083ca3de 120 c.arg[0]=findone;
7fe9b0b7 121 SendCommand(&c);
122 return 0;
123}
124
125int CmdHIDSim(const char *Cmd)
38b20f75 126{
1ee624fe 127 uint32_t hi2 = 0, hi = 0, lo = 0;
128 hexstring_to_int96(&hi2, &hi, &lo, Cmd);
129 if (hi >= 0x40 || hi2 != 0) {
ab20cc35
MF
130 PrintAndLog("This looks like a long tag ID. Use 'lf simfsk' for long tags. Aborting!");
131 return 0;
132 }
38b20f75 133
ab20cc35
MF
134 PrintAndLog("Emulating tag with ID %x%08x", hi, lo);
135 PrintAndLog("Press pm3-button to abort simulation");
38b20f75 136
ab20cc35
MF
137 UsbCommand c = {CMD_HID_SIM_TAG, {hi, lo, 0}};
138 SendCommand(&c);
139 return 0;
38b20f75 140}
141
142int CmdHIDClone(const char *Cmd)
7fe9b0b7 143{
54a942b0 144 unsigned int hi2 = 0, hi = 0, lo = 0;
54a942b0 145 UsbCommand c;
1ee624fe 146 hexstring_to_int96(&hi2, &hi, &lo, Cmd);
147
148 if (hi >= 0x40 || hi2 != 0) {
54a942b0 149 PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo);
54a942b0 150 c.d.asBytes[0] = 1;
1ee624fe 151 } else {
54a942b0 152 PrintAndLog("Cloning tag with ID %x%08x", hi, lo);
54a942b0 153 c.d.asBytes[0] = 0;
7fe9b0b7 154 }
38b20f75 155
156 c.cmd = CMD_HID_CLONE_TAG;
b5a5fc4d 157 c.arg[0] = (hi2 & 0x000FFFFF);
d16d20b1 158 c.arg[1] = hi;
159 c.arg[2] = lo;
ec09b62d 160
ec09b62d 161 SendCommand(&c);
162 return 0;
163}
164
b5a5fc4d 165int CmdHIDDecode(const char *Cmd){
ab20cc35 166 if (strlen(Cmd)<3) {
b5a5fc4d 167 PrintAndLog("Usage: lf hid decode <id>");
168 PrintAndLog(" sample: lf hid decode 2006f623ae");
ab20cc35
MF
169 return 0;
170 }
171
b5a5fc4d 172 uint32_t top = 0, mid = 0, bot = 0;
173 hexstring_to_int96(&top, &mid, &bot, Cmd);
174 hidproxmessage_t packed = initialize_proxmessage_object(top, mid, bot);
175 HIDTryUnpack(&packed);
176 return 0;
177}
178int CmdHIDEncode(const char *Cmd) {
179 if (strlen(Cmd) == 0) {
180 PrintAndLog("Usage: lf hid encode <format> <facility code (decimal)> <card number (decimal)>");
181 PrintAndLog(" sample: lf hid encode H10301 123 4567");
182 return 0;
183 }
184
185 int formatIndex = -1;
186 if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){
187 HIDListFormats();
188 return 0;
189 } else {
190 char format[16];
191 memset(format, 0, sizeof(format));
192 param_getstr(Cmd, 0, format, sizeof(format));
193 formatIndex = HIDFindCardFormat(format);
194 if (formatIndex == -1) {
195 HIDListFormats();
196 return 0;
197 }
198 }
ab20cc35 199
b5a5fc4d 200 hidproxcard_t card;
201 memset(&card, 0, sizeof(hidproxcard_t));
202 card.FacilityCode = param_get32ex(Cmd, 1, 0, 10);
203 card.CardNumber = param_get64ex(Cmd, 2, 0, 10);
204 card.ParitySupported = true; // Try to encode parity if supported.
205
206 hidproxmessage_t packed;
207 memset(&packed, 0, sizeof(hidproxmessage_t));
208 if (HIDPack(formatIndex, &card, &packed)){
209 if (packed.top != 0) {
210 PrintAndLog("HID Prox TAG ID: %x%08x%08x",
211 (unsigned int)packed.top, (unsigned int)packed.mid, (unsigned int)packed.bot);
1ee624fe 212 } else {
b5a5fc4d 213 PrintAndLog("HID Prox TAG ID: %x%08x",
214 (unsigned int)packed.mid, (unsigned int)packed.bot);
1ee624fe 215 }
ab20cc35 216 } else {
b5a5fc4d 217 PrintAndLog("The provided data could not be encoded with the selected format.");
ab20cc35
MF
218 }
219 return 0;
220}
221
b5a5fc4d 222int CmdHIDWrite(const char *Cmd) {
223 if (strlen(Cmd) == 0) {
224 PrintAndLog("Usage: lf hid write <format> <facility code (decimal)> <card number (decimal)>");
225 PrintAndLog(" sample: lf hid write H10301 123 4567");
226 return 0;
227 }
ab20cc35 228
b5a5fc4d 229 int formatIndex = -1;
230 if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){
231 HIDListFormats();
ab20cc35 232 return 0;
b5a5fc4d 233 } else {
234 char format[16];
235 memset(format, 0, sizeof(format));
236 param_getstr(Cmd, 0, format, sizeof(format));
237 formatIndex = HIDFindCardFormat(format);
238 if (formatIndex == -1) {
239 HIDListFormats();
240 return 0;
241 }
ab20cc35
MF
242 }
243
b5a5fc4d 244 hidproxcard_t card;
245 memset(&card, 0, sizeof(hidproxcard_t));
246 card.FacilityCode = param_get32ex(Cmd, 1, 0, 10);
247 card.CardNumber = param_get64ex(Cmd, 2, 0, 10);
248 card.ParitySupported = true; // Try to encode parity if supported.
249
250 hidproxmessage_t packed;
251 memset(&packed, 0, sizeof(hidproxmessage_t));
252 if (HIDPack(formatIndex, &card, &packed)){
253 UsbCommand c;
254 if (packed.top != 0) {
255 PrintAndLog("HID Prox TAG ID: %x%08x%08x",
256 (unsigned int)packed.top, (unsigned int)packed.mid, (unsigned int)packed.bot);
257 c.d.asBytes[0] = 1;
258 } else {
259 PrintAndLog("HID Prox TAG ID: %x%08x",
260 (unsigned int)packed.mid, (unsigned int)packed.bot);
261 c.d.asBytes[0] = 0;
262 }
263
264 c.cmd = CMD_HID_CLONE_TAG;
265 c.arg[0] = (packed.top & 0x000FFFFF);
266 c.arg[1] = packed.mid;
267 c.arg[2] = packed.bot;
268 SendCommand(&c);
ab20cc35 269
1ee624fe 270 } else {
b5a5fc4d 271 PrintAndLog("The provided data could not be encoded with the selected format.");
ab20cc35
MF
272 }
273 return 0;
274}
275
b5a5fc4d 276static int CmdHelp(const char *Cmd); // define this now so the below won't error out.
7fe9b0b7 277static command_t CommandTable[] =
278{
279 {"help", CmdHelp, 1, "This help"},
6cd2eef4 280 {"demod", CmdFSKdemodHID, 1, "Demodulate HID Prox from GraphBuffer"},
281 {"read", CmdHIDReadFSK, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"},
083ca3de 282 {"sim", CmdHIDSim, 0, "<ID> -- HID tag simulator"},
1ee624fe 283 {"clone", CmdHIDClone, 0, "<ID> -- Clone HID to T55x7 (tag must be in antenna)"},
b5a5fc4d 284 {"decode", CmdHIDDecode, 1, "<ID> -- Try to decode an HID tag and show its contents"},
285 {"encode", CmdHIDEncode, 1, "<format> <fc> <num> -- Encode an HID ID with the specified format, facility code and card number"},
286 {"write", CmdHIDWrite, 0, "<format> <fc> <num> -- Encode and write to a T55x7 tag (tag must be in antenna)"},
7fe9b0b7 287 {NULL, NULL, 0, NULL}
288};
289
290int CmdLFHID(const char *Cmd)
291{
292 CmdsParse(CommandTable, Cmd);
293 return 0;
294}
295
296int CmdHelp(const char *Cmd)
297{
298 CmdsHelp(CommandTable);
299 return 0;
300}
Impressum, Datenschutz