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