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