]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhid.c
68e9b07f6dc29891f97b51e2dde50eac0d5b1247
[proxmark3-svn] / client / cmdlfhid.c
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 //-----------------------------------------------------------------------------
8 // Low frequency HID commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <string.h>
13 #include "proxmark3.h"
14 #include "ui.h"
15 #include "graph.h"
16 #include "cmdparser.h"
17 #include "cmddata.h" //for g_debugMode, demodbuff cmds
18 #include "lfdemod.h" // for HIDdemodFSK
19
20 static int CmdHelp(const char *Cmd);
21
22 //by marshmellow (based on existing demod + holiman's refactor)
23 //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
24 //print full HID Prox ID and some bit format details if found
25 int CmdFSKdemodHID(const char *Cmd)
26 {
27 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
28 uint32_t hi2=0, hi=0, lo=0;
29
30 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
31 size_t BitLen = getFromGraphBuf(BitStream);
32 if (BitLen==0) return 0;
33 //get binary from fsk wave
34 int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo);
35 if (idx<0){
36 if (g_debugMode){
37 if (idx==-1){
38 PrintAndLog("DEBUG: Just Noise Detected");
39 } else if (idx == -2) {
40 PrintAndLog("DEBUG: Error demoding fsk");
41 } else if (idx == -3) {
42 PrintAndLog("DEBUG: Preamble not found");
43 } else if (idx == -4) {
44 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen);
45 } else {
46 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
47 }
48 }
49 return 0;
50 }
51 if (hi2==0 && hi==0 && lo==0) {
52 if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
53 return 0;
54 }
55 if (hi2 != 0){ //extra large HID tags
56 PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)",
57 (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF);
58 }
59 else { //standard HID tags <38 bits
60 uint8_t fmtLen = 0;
61 uint32_t fc = 0;
62 uint32_t cardnum = 0;
63 if (((hi>>5)&1)==1){//if bit 38 is set then < 37 bit format is used
64 uint32_t lo2=0;
65 lo2=(((hi & 31) << 12) | (lo>>20)); //get bits 21-37 to check for format len bit
66 uint8_t idx3 = 1;
67 while(lo2>1){ //find last bit set to 1 (format len bit)
68 lo2=lo2>>1;
69 idx3++;
70 }
71 fmtLen =idx3+19;
72 fc =0;
73 cardnum=0;
74 if(fmtLen==26){
75 cardnum = (lo>>1)&0xFFFF;
76 fc = (lo>>17)&0xFF;
77 }
78 if(fmtLen==34){
79 cardnum = (lo>>1)&0xFFFF;
80 fc= ((hi&1)<<15)|(lo>>17);
81 }
82 if(fmtLen==35){
83 cardnum = (lo>>1)&0xFFFFF;
84 fc = ((hi&1)<<11)|(lo>>21);
85 }
86 }
87 else { //if bit 38 is not set then 37 bit format is used
88 fmtLen = 37;
89 fc = 0;
90 cardnum = 0;
91 if(fmtLen == 37){
92 cardnum = (lo>>1)&0x7FFFF;
93 fc = ((hi&0xF)<<12)|(lo>>20);
94 }
95 }
96 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d",
97 (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
98 (unsigned int) fmtLen, (unsigned int) fc, (unsigned int) cardnum);
99 }
100 setDemodBuf(BitStream,BitLen,idx);
101 if (g_debugMode){
102 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
103 printDemodBuff();
104 }
105 return 1;
106 }
107
108 int CmdHIDReadFSK(const char *Cmd)
109 {
110 int findone=0;
111 if(Cmd[0]=='1') findone=1;
112 UsbCommand c={CMD_HID_DEMOD_FSK};
113 c.arg[0]=findone;
114 SendCommand(&c);
115 return 0;
116 }
117
118 int CmdHIDSim(const char *Cmd)
119 {
120 unsigned int hi = 0, lo = 0;
121 int n = 0, i = 0;
122
123 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
124 hi = (hi << 4) | (lo >> 28);
125 lo = (lo << 4) | (n & 0xf);
126 }
127
128 PrintAndLog("Emulating tag with ID %x%16x", hi, lo);
129 PrintAndLog("Press pm3-button to abort simulation");
130
131 UsbCommand c = {CMD_HID_SIM_TAG, {hi, lo, 0}};
132 SendCommand(&c);
133 return 0;
134 }
135
136 int CmdHIDClone(const char *Cmd)
137 {
138 unsigned int hi2 = 0, hi = 0, lo = 0;
139 int n = 0, i = 0;
140 UsbCommand c;
141
142 if (strchr(Cmd,'l') != 0) {
143 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
144 hi2 = (hi2 << 4) | (hi >> 28);
145 hi = (hi << 4) | (lo >> 28);
146 lo = (lo << 4) | (n & 0xf);
147 }
148
149 PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo);
150
151 c.d.asBytes[0] = 1;
152 }
153 else {
154 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
155 hi = (hi << 4) | (lo >> 28);
156 lo = (lo << 4) | (n & 0xf);
157 }
158
159 PrintAndLog("Cloning tag with ID %x%08x", hi, lo);
160
161 hi2 = 0;
162 c.d.asBytes[0] = 0;
163 }
164
165 c.cmd = CMD_HID_CLONE_TAG;
166 c.arg[0] = hi2;
167 c.arg[1] = hi;
168 c.arg[2] = lo;
169
170 SendCommand(&c);
171 return 0;
172 }
173
174 static command_t CommandTable[] =
175 {
176 {"help", CmdHelp, 1, "This help"},
177 {"demod", CmdFSKdemodHID, 1, "Demodulate HID Prox from GraphBuffer"},
178 {"read", CmdHIDReadFSK, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"},
179 {"sim", CmdHIDSim, 0, "<ID> -- HID tag simulator"},
180 {"clone", CmdHIDClone, 0, "<ID> ['l'] -- Clone HID to T55x7 (tag must be in antenna)(option 'l' for 84bit ID)"},
181 {NULL, NULL, 0, NULL}
182 };
183
184 int CmdLFHID(const char *Cmd)
185 {
186 CmdsParse(CommandTable, Cmd);
187 return 0;
188 }
189
190 int CmdHelp(const char *Cmd)
191 {
192 CmdsHelp(CommandTable);
193 return 0;
194 }
Impressum, Datenschutz