]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfpresco.c
ADD: first try to implement crc-16 for Legic Advant *not working yet* Can't verify it.
[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 <Card ID - 9 digits> <Q5>");
17 PrintAndLog("Options :");
18 PrintAndLog(" <Card Number> : 9 digit presco card number");
19 //PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)");
20 PrintAndLog("");
21 PrintAndLog("Sample : lf presco clone 123456789");
22 return 0;
23 }
24
25 int usage_lf_presco_sim(void) {
26 PrintAndLog("Enables simulation of presco card with specified card number.");
27 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
28 PrintAndLog("Per presco format, the card number is 9 digit number and can contain *# chars. Larger values are truncated.");
29 PrintAndLog("");
30 PrintAndLog("Usage: lf presco sim <Card-Number>");
31 PrintAndLog("Options :");
32 PrintAndLog(" <Card Number> : 9 digit presco card number");
33 PrintAndLog("");
34 PrintAndLog("Sample : lf presco sim 123456789");
35 return 0;
36 }
37
38 // calc checksum
39 int GetWiegandFromPresco(const char *id, uint32_t *sitecode, uint32_t *usercode) {
40
41 uint8_t val = 0;
42 for (int index =0; index < strlen(id); ++index) {
43
44 // Get value from number string.
45 if ( id[index] == '*' ) val = 10;
46 if ( id[index] == '#') val = 11;
47 if ( id[index] >= 0x30 && id[index] <= 0x39 )
48 val = id[index] - 0x30;
49
50 *sitecode += val;
51
52 // last digit is only added, not multipled.
53 if ( index < strlen(id)-1 )
54 *sitecode *= 12;
55 }
56 *usercode = *sitecode % 65536;
57 *sitecode /= 16777216;
58 return 0;
59 }
60
61 int GetPrescoBits(uint32_t sitecode, uint32_t usercode, uint8_t *prescoBits) {
62 uint8_t pre[66];
63 memset(pre, 0, sizeof(pre));
64 prescoBits[7]=1;
65 num_to_bytebits(26, 8, pre);
66
67 uint8_t wiegand[24];
68 num_to_bytebits(sitecode, 8, wiegand);
69 num_to_bytebits(usercode, 16, wiegand+8);
70
71 wiegand_add_parity(pre+8, wiegand, 24);
72 size_t bitLen = addParity(pre, prescoBits+8, 66, 4, 1);
73
74 if (bitLen != 88) return 0;
75 return 1;
76 }
77 //see ASKDemod for what args are accepted
78 int CmdPrescoDemod(const char *Cmd) {
79 if (!ASKDemod(Cmd, false, false, 1)) {
80 if (g_debugMode) PrintAndLog("ASKDemod failed");
81 return 0;
82 }
83 size_t size = DemodBufferLen;
84 //call lfdemod.c demod for Viking
85 int ans = PrescoDemod(DemodBuffer, &size);
86 if (ans < 0) {
87 if (g_debugMode) PrintAndLog("Error Presco_Demod %d", ans);
88 return 0;
89 }
90 //got a good demod
91 uint32_t raw1 = bytebits_to_byte(DemodBuffer+ans, 32);
92 uint32_t raw2 = bytebits_to_byte(DemodBuffer+ans+32, 32);
93 uint32_t cardid = bytebits_to_byte(DemodBuffer+ans+24, 32);
94 PrintAndLog("Presco Tag Found: Card ID %08X", cardid);
95 PrintAndLog("Raw: %08X%08X", raw1,raw2);
96 setDemodBuf(DemodBuffer+ans, 64, 0);
97
98 // uint32_t sitecode = 0, usercode = 0;
99 // GetWiegandFromPresco(id, &sitecode, &usercode);
100 // PrintAndLog8("SiteCode %d | UserCode %d", sitecode, usercode);
101
102 return 1;
103 }
104
105 //see ASKDemod for what args are accepted
106 int CmdPrescoRead(const char *Cmd) {
107 // Presco Number: 123456789 --> Sitecode 30 | usercode 8665
108
109 // read lf silently
110 CmdLFRead("s");
111 // get samples silently
112 getSamples("30000",false);
113 // demod and output Presco ID
114 return CmdPrescoDemod(Cmd);
115 }
116
117 int CmdPrescoClone(const char *Cmd) {
118
119 char cmdp = param_getchar(Cmd, 0);
120 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_presco_clone();
121
122 uint32_t sitecode=0, usercode=0;
123 uint8_t bits[96];
124 uint8_t *bs = bits;
125 memset(bs,0,sizeof(bits));
126 uint32_t blocks[5] = {T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_32 | 4<<T55x7_MAXBLOCK_SHIFT | T55x7_ST_TERMINATOR, 0, 0, 0, 5};
127
128 if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q')
129 blocks[0] = T5555_MODULATION_MANCHESTER | 32<<T5555_BITRATE_SHIFT | 4<<T5555_MAXBLOCK_SHIFT | T5555_ST_TERMINATOR;
130
131 // get wiegand from printed number.
132 GetWiegandFromPresco(Cmd, &sitecode, &usercode);
133
134 if ((sitecode & 0xFF) != sitecode) {
135 sitecode &= 0xFF;
136 PrintAndLog("Facility-Code Truncated to 8-bits (Presco): %u", sitecode);
137 }
138
139 if ((usercode & 0xFFFF) != usercode) {
140 usercode &= 0xFFFF;
141 PrintAndLog("Card Number Truncated to 16-bits (Presco): %u", usercode);
142 }
143
144 if ( !GetPrescoBits(sitecode, usercode, bs)) {
145 PrintAndLog("Error with tag bitstream generation.");
146 return 1;
147 }
148
149 blocks[1] = bytebits_to_byte(bs,32);
150 blocks[2] = bytebits_to_byte(bs+32,32);
151 blocks[3] = bytebits_to_byte(bs+64,32);
152 blocks[4] = bytebits_to_byte(bs+96,32);
153
154 PrintAndLog("Preparing to clone Presco to T55x7 with SiteCode: %u, UserCode: %u", sitecode, usercode);
155 PrintAndLog("Blk | Data ");
156 PrintAndLog("----+------------");
157 PrintAndLog(" 00 | 0x%08x", blocks[0]);
158 PrintAndLog(" 01 | 0x%08x", blocks[1]);
159 PrintAndLog(" 02 | 0x%08x", blocks[2]);
160 PrintAndLog(" 03 | 0x%08x", blocks[3]);
161 PrintAndLog(" 04 | 0x%08x", blocks[4]);
162
163 // UsbCommand resp;
164 // UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
165
166 // for (uint8_t i=0; i<5; i++) {
167 // c.arg[0] = blocks[i];
168 // c.arg[1] = i;
169 // clearCommandBuffer();
170 // SendCommand(&c);
171 // if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
172 // PrintAndLog("Error occurred, device did not respond during write operation.");
173 // return -1;
174 // }
175 // }
176 return 0;
177 }
178
179 int CmdPrescoSim(const char *Cmd) {
180 // uint32_t id = 0;
181 // uint64_t rawID = 0;
182 // uint8_t clk = 32, encoding = 1, separator = 0, invert = 0;
183
184 // char cmdp = param_getchar(Cmd, 0);
185 // if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_presco_sim();
186
187 // id = param_get32ex(Cmd, 0, 0, 16);
188 // if (id == 0) return usage_lf_presco_sim();
189
190 //rawID = getVikingBits(id);
191
192 // uint16_t arg1, arg2;
193 // size_t size = 64;
194 // arg1 = clk << 8 | encoding;
195 // arg2 = invert << 8 | separator;
196
197 // PrintAndLog("Simulating - ID: %08X, Raw: %08X%08X",id,(uint32_t)(rawID >> 32),(uint32_t) (rawID & 0xFFFFFFFF));
198
199 // UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
200 // num_to_bytebits(rawID, size, c.d.asBytes);
201 // clearCommandBuffer();
202 // SendCommand(&c);
203 return 0;
204 }
205
206 static command_t CommandTable[] = {
207 {"help", CmdHelp, 1, "This help"},
208 {"read", CmdPrescoRead, 0, "Attempt to read and Extract tag data"},
209 {"clone", CmdPrescoClone, 0, "<8 digit ID number> clone presco tag"},
210 // {"sim", CmdPrescoSim, 0, "<8 digit ID number> simulate presco tag"},
211 {NULL, NULL, 0, NULL}
212 };
213
214 int CmdLFPresco(const char *Cmd) {
215 clearCommandBuffer();
216 CmdsParse(CommandTable, Cmd);
217 return 0;
218 }
219
220 int CmdHelp(const char *Cmd) {
221 CmdsHelp(CommandTable);
222 return 0;
223 }
Impressum, Datenschutz