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