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