]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfpresco.c
FIX: @marshmellow42 's ST detection fix.
[proxmark3-svn] / client / cmdlfpresco.c
1 //-----------------------------------------------------------------------------
2 //
3 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
4 // at your option, any later version. See the LICENSE.txt file for the text of
5 // the license.
6 //-----------------------------------------------------------------------------
7 // Low frequency Presco tag commands
8 //-----------------------------------------------------------------------------
9 #include <string.h>
10 #include <inttypes.h>
11 #include "cmdlfpresco.h"
12 static int CmdHelp(const char *Cmd);
13
14 int usage_lf_presco_clone(void){
15 PrintAndLog("clone a Presco tag to a T55x7 tag.");
16 PrintAndLog("Usage: lf presco clone d <Card-ID> H <hex-ID> <Q5>");
17 PrintAndLog("Options :");
18 PrintAndLog(" d <Card-ID> : 9 digit presco card ID");
19 PrintAndLog(" H <hex-ID> : 8 digit hex card number");
20 PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)");
21 PrintAndLog("");
22 PrintAndLog("Sample : lf presco clone d 123456789");
23 return 0;
24 }
25
26 int usage_lf_presco_sim(void) {
27 PrintAndLog("Enables simulation of presco card with specified card number.");
28 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
29 PrintAndLog("Per presco format, the card number is 9 digit number and can contain *# chars. Larger values are truncated.");
30 PrintAndLog("");
31 PrintAndLog("Usage: lf presco sim d <Card-ID> or H <hex-ID>");
32 PrintAndLog("Options :");
33 PrintAndLog(" d <Card-ID> : 9 digit presco card number");
34 PrintAndLog(" H <hex-ID> : 8 digit hex card number");
35 PrintAndLog("");
36 PrintAndLog("Sample : lf presco sim d 123456789");
37 return 0;
38 }
39
40 // convert base 12 ID to sitecode & usercode & 8 bit other unknown code
41 int GetWiegandFromPresco(const char *Cmd, uint32_t *sitecode, uint32_t *usercode, uint32_t *fullcode, bool *Q5) {
42
43 uint8_t val = 0;
44 bool hex = false, errors = false;
45 uint8_t cmdp = 0;
46 char id[11];
47 int stringlen = 0;
48 memset(id, 0x00, sizeof(id));
49
50 while(param_getchar(Cmd, cmdp) != 0x00) {
51 switch(param_getchar(Cmd, cmdp)) {
52 case 'h':
53 return -1;
54 case 'H':
55 hex = true;
56 //get hex
57 *fullcode = param_get32ex(Cmd, cmdp+1, 0, 16);
58 cmdp+=2;
59 break;
60 case 'P':
61 case 'p':
62 //param get string int param_getstr(const char *line, int paramnum, char * str)
63 stringlen = param_getstr(Cmd, cmdp+1, id);
64 if (stringlen < 2) return -1;
65 cmdp+=2;
66 break;
67 case 'Q':
68 case 'q':
69 *Q5 = true;
70 cmdp++;
71 break;
72 default:
73 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
74 errors = 1;
75 break;
76 }
77 if(errors) break;
78 }
79 // No args
80 if(cmdp == 0) errors = 1;
81
82 //Validations
83 if(errors) return -1;
84
85 if (!hex) {
86 for (int index =0; index < strlen(id); ++index) {
87
88 // Get value from number string.
89 if ( id[index] == '*' ) val = 10;
90 if ( id[index] == '#') val = 11;
91 if ( id[index] >= 0x30 && id[index] <= 0x39 )
92 val = id[index] - 0x30;
93
94 *fullcode += val;
95
96 // last digit is only added, not multipled.
97 if ( index < strlen(id)-1 )
98 *fullcode *= 12;
99 }
100 }
101
102 *usercode = *fullcode & 0x0000FFFF; //% 65566
103 *sitecode = (*fullcode >> 24) & 0x000000FF; // /= 16777216;
104 return 0;
105 }
106
107 // calc not certain - intended to get bitstream for programming / sim
108 int GetPrescoBits(uint32_t fullcode, uint8_t *prescoBits) {
109 num_to_bytebits(0x10D00000, 32, prescoBits);
110 num_to_bytebits(0x00000000, 32, prescoBits+32);
111 num_to_bytebits(0x00000000, 32, prescoBits+64);
112 num_to_bytebits(fullcode , 32, prescoBits+96);
113 return 1;
114 }
115
116 //see ASKDemod for what args are accepted
117 int CmdPrescoDemod(const char *Cmd) {
118 bool st = true;
119 if (!ASKDemod_ext("32 0 0 0 0 a", FALSE, FALSE, 1, &st)) {
120 if (g_debugMode) PrintAndLog("DEBUG: Error Presco ASKDemod failed");
121 return 0;
122 }
123 size_t size = DemodBufferLen;
124 //call lfdemod.c demod for Presco
125 int ans = PrescoDemod(DemodBuffer, &size);
126 if (ans < 0) {
127 if (g_debugMode){
128 if (ans == -1)
129 PrintAndLog("DEBUG: Error - Presco: too few bits found");
130 else if (ans == -2)
131 PrintAndLog("DEBUG: Error - Presco: preamble not found");
132 else if (ans == -3)
133 PrintAndLog("DEBUG: Error - Presco: Size not correct: %d", size);
134 else
135 PrintAndLog("DEBUG: Error - Presco: ans: %d", ans);
136 }
137 return 0;
138 }
139 setDemodBuf(DemodBuffer, 128, ans);
140
141 //got a good demod
142 uint32_t raw1 = bytebits_to_byte(DemodBuffer, 32);
143 uint32_t raw2 = bytebits_to_byte(DemodBuffer+32, 32);
144 uint32_t raw3 = bytebits_to_byte(DemodBuffer+64, 32);
145 uint32_t raw4 = bytebits_to_byte(DemodBuffer+96, 32);
146 uint32_t cardid = raw4;
147 PrintAndLog("Presco Tag Found: Card ID %08X, Raw: %08X%08X%08X%08X", cardid, raw1, raw2, raw3, raw4);
148
149 uint32_t sitecode = 0, usercode = 0, fullcode = 0;
150 bool Q5 = false;
151 char cmd[12] = {0};
152 sprintf(cmd, "H %08X", cardid);
153 GetWiegandFromPresco(cmd, &sitecode, &usercode, &fullcode, &Q5);
154 PrintAndLog("SiteCode %u, UserCode %u, FullCode, %08X", sitecode, usercode, fullcode);
155 return 1;
156 }
157
158 //see ASKDemod for what args are accepted
159 int CmdPrescoRead(const char *Cmd) {
160 // Presco Number: 123456789 --> Sitecode 30 | usercode 8665
161
162 // read lf silently
163 CmdLFRead("s");
164 // get samples silently
165 getSamples("12000", TRUE);
166 // demod and output Presco ID
167 return CmdPrescoDemod(Cmd);
168 }
169
170 // takes base 12 ID converts to hex
171 // Or takes 8 digit hex ID
172 int CmdPrescoClone(const char *Cmd) {
173
174 bool Q5 = false;
175 uint32_t sitecode=0, usercode=0, fullcode=0;
176 uint32_t blocks[5] = {T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_32 | 4 << T55x7_MAXBLOCK_SHIFT | T55x7_ST_TERMINATOR, 0, 0, 0, 0};
177
178 // get wiegand from printed number.
179 if (GetWiegandFromPresco(Cmd, &sitecode, &usercode, &fullcode, &Q5) == -1) return usage_lf_presco_clone();
180
181 if (Q5)
182 //t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
183 blocks[0] = T5555_MODULATION_MANCHESTER | ((32-2)>>1) << T5555_BITRATE_SHIFT | 4 << T5555_MAXBLOCK_SHIFT | T5555_ST_TERMINATOR;
184
185 if ((sitecode & 0xFF) != sitecode) {
186 sitecode &= 0xFF;
187 PrintAndLog("Facility-Code Truncated to 8-bits (Presco): %u", sitecode);
188 }
189
190 if ((usercode & 0xFFFF) != usercode) {
191 usercode &= 0xFFFF;
192 PrintAndLog("Card Number Truncated to 16-bits (Presco): %u", usercode);
193 }
194
195 blocks[1] = 0x10D00000; //preamble
196 blocks[2] = 0x00000000;
197 blocks[3] = 0x00000000;
198 blocks[4] = fullcode;
199
200 PrintAndLog("Preparing to clone Presco to T55x7 with SiteCode: %u, UserCode: %u, FullCode: %08x", sitecode, usercode, fullcode);
201 PrintAndLog("Blk | Data ");
202 PrintAndLog("----+------------");
203 PrintAndLog(" 00 | 0x%08x", blocks[0]);
204 PrintAndLog(" 01 | 0x%08x", blocks[1]);
205 PrintAndLog(" 02 | 0x%08x", blocks[2]);
206 PrintAndLog(" 03 | 0x%08x", blocks[3]);
207 PrintAndLog(" 04 | 0x%08x", blocks[4]);
208
209 UsbCommand resp;
210 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
211
212 for (int i=4; i>=0; i--) {
213 c.arg[0] = blocks[i];
214 c.arg[1] = i;
215 clearCommandBuffer();
216 SendCommand(&c);
217 if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)){
218 PrintAndLog("Error occurred, device did not respond during write operation.");
219 return -1;
220 }
221 }
222 return 0;
223 }
224
225 // takes base 12 ID converts to hex
226 // Or takes 8 digit hex ID
227 int CmdPrescoSim(const char *Cmd) {
228 uint32_t sitecode=0, usercode=0, fullcode=0;
229 bool Q5=false;
230 // get wiegand from printed number.
231 if (GetWiegandFromPresco(Cmd, &sitecode, &usercode, &fullcode, &Q5) == -1) return usage_lf_presco_sim();
232
233 uint8_t clk = 32, encoding = 1, separator = 1, invert = 0;
234 uint16_t arg1, arg2;
235 size_t size = 128;
236 arg1 = clk << 8 | encoding;
237 arg2 = invert << 8 | separator;
238
239 PrintAndLog("Simulating Presco - SiteCode: %u, UserCode: %u, FullCode: %08X",sitecode, usercode, fullcode);
240
241 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
242 GetPrescoBits(fullcode, c.d.asBytes);
243 clearCommandBuffer();
244 SendCommand(&c);
245 return 0;
246 }
247
248 static command_t CommandTable[] = {
249 {"help", CmdHelp, 1, "This help"},
250 {"read", CmdPrescoRead, 0, "Attempt to read and Extract tag data"},
251 {"clone", CmdPrescoClone, 0, "d <9 digit ID> or h <hex> [Q5] clone presco tag"},
252 {"sim", CmdPrescoSim, 0, "d <9 digit ID> or h <hex> simulate presco tag"},
253 {NULL, NULL, 0, NULL}
254 };
255
256 int CmdLFPresco(const char *Cmd) {
257 clearCommandBuffer();
258 CmdsParse(CommandTable, Cmd);
259 return 0;
260 }
261
262 int CmdHelp(const char *Cmd) {
263 CmdsHelp(CommandTable);
264 return 0;
265 }
Impressum, Datenschutz