]>
Commit | Line | Data |
---|---|---|
50564be0 | 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 Farpoint / Pyramid tag commands | |
8 | //----------------------------------------------------------------------------- | |
9 | #include <string.h> | |
10 | #include <inttypes.h> | |
11 | #include "cmdlfguard.h" | |
12 | static int CmdHelp(const char *Cmd); | |
13 | ||
14 | int usage_lf_guard_clone(void){ | |
15 | PrintAndLog("clone a Guardall tag to a T55x7 tag."); | |
16 | PrintAndLog("The facility-code is 8-bit and the card number is 16-bit. Larger values are truncated. "); | |
17 | PrintAndLog("Currently work only on 26bit"); | |
18 | PrintAndLog(""); | |
19 | PrintAndLog("Usage: lf guard clone <Facility-Code> <Card-Number>"); | |
20 | PrintAndLog("Options :"); | |
21 | PrintAndLog(" <Facility-Code> : 8-bit value facility code"); | |
22 | PrintAndLog(" <Card Number> : 16-bit value card number"); | |
23 | PrintAndLog(""); | |
24 | PrintAndLog("Sample : lf guard clone 123 11223"); | |
25 | return 0; | |
26 | } | |
27 | ||
28 | int usage_lf_guard_sim(void) { | |
29 | PrintAndLog("Enables simulation of Guardall card with specified card number."); | |
30 | PrintAndLog("Simulation runs until the button is pressed or another USB command is issued."); | |
31 | PrintAndLog("The facility-code is 8-bit and the card number is 16-bit. Larger values are truncated."); | |
32 | PrintAndLog("Currently work only on 26bit"); | |
33 | PrintAndLog(""); | |
34 | PrintAndLog("Usage: lf guard sim <Card-Number>"); | |
35 | PrintAndLog("Options :"); | |
36 | PrintAndLog(" <Facility-Code> : 8-bit value facility code"); | |
37 | PrintAndLog(" <Card Number> : 16-bit value card number"); | |
38 | PrintAndLog(""); | |
39 | PrintAndLog("Sample : lf guard sim 123 11223"); | |
40 | return 0; | |
41 | } | |
42 | ||
43 | ||
44 | // Works for 26bits. | |
45 | int GetGuardBits(uint32_t fc, uint32_t cn, uint8_t *guardBits) { | |
46 | ||
47 | // Intializes random number generator | |
48 | time_t t; | |
49 | srand((unsigned) time(&t)); | |
50 | ||
51 | uint8_t pre[96]; | |
52 | memset(pre, 0x00, sizeof(pre)); | |
53 | ||
54 | uint8_t index = 8; | |
55 | ||
56 | // preamble 6bits | |
57 | pre[0] = 1; | |
58 | pre[1] = 1; | |
59 | pre[2] = 1; | |
60 | pre[3] = 1; | |
61 | pre[4] = 1; | |
62 | //pre[5] = 0; | |
63 | ||
64 | // add xor key | |
65 | uint8_t xorKey = rand() % 0xFF; | |
66 | num_to_bytebits(xorKey, 8, pre+index); | |
67 | index += 8; | |
68 | ||
69 | // add format length | |
70 | // len | hex | bin wiegand pos fc/cn | |
71 | // 26 | 1A | 0001 1010 | |
72 | num_to_bytebits(26, 8, pre+index); | |
73 | // 36 | 24 | 0010 0100 | |
74 | //num_to_bytebits(36, 8, pre+index); | |
75 | // 40 | 28 | 0010 1000 | |
76 | //num_to_bytebits(40, 8, pre+index); | |
77 | ||
78 | index += 8; | |
79 | ||
80 | // 2bit checksum | |
81 | // unknown today. | |
82 | index += 2; | |
83 | ||
84 | // Get 26 wiegand from FacilityCode, CardNumber | |
85 | uint8_t wiegand[24]; | |
86 | memset(wiegand, 0x00, sizeof(wiegand)); | |
87 | num_to_bytebits(fc, 8, wiegand); | |
88 | num_to_bytebits(cn, 16, wiegand+8); | |
89 | ||
90 | // add wiegand parity bits (dest, source, len) | |
91 | wiegand_add_parity(pre+index, wiegand, 24); | |
92 | ||
93 | uint8_t tmp = 0, i = 0; | |
94 | for (i = 2; i < 12; ++i) { | |
95 | // // xor all bytes | |
96 | // tmp = xorKey ^ bytebits_to_byte(pre + (i*8), 8); | |
97 | ||
98 | // // copy to out.. | |
99 | // num_to_bytebits(tmp, 8, pre + (i*8) ); | |
100 | } | |
101 | ||
102 | // add spacer bit 0 every 5 | |
103 | ||
104 | // swap nibbles | |
105 | ||
106 | ||
107 | // copy to outarray | |
108 | memcpy(guardBits, pre, sizeof(pre)); | |
109 | ||
110 | printf(" | %s\n", sprint_bin(guardBits, 96) ); | |
111 | return 1; | |
112 | } | |
113 | ||
114 | int CmdGuardRead(const char *Cmd) { | |
115 | CmdLFRead("s"); | |
116 | getSamples("30000",false); | |
117 | return CmdG_Prox_II_Demod(""); | |
118 | } | |
119 | ||
120 | int CmdGuardClone(const char *Cmd) { | |
121 | ||
122 | char cmdp = param_getchar(Cmd, 0); | |
123 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_guard_clone(); | |
124 | ||
125 | uint32_t facilitycode=0, cardnumber=0, fc = 0, cn = 0; | |
126 | uint8_t i; | |
127 | uint8_t bs[96]; | |
128 | memset(bs, 0x00, sizeof(bs)); | |
129 | ||
130 | //GuardProxII - compat mode, ASK/Biphase, data rate 64, 3 data blocks | |
131 | uint32_t blocks[5] = {T55x7_MODULATION_BIPHASE | T55x7_BITRATE_RF_64 | 3<<T55x7_MAXBLOCK_SHIFT, 0, 0, 0, 0}; | |
132 | ||
133 | // if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q') | |
134 | // blocks[0] = T5555_MODULATION_FSK2 | 50<<T5555_BITRATE_SHIFT | 4<<T5555_MAXBLOCK_SHIFT; | |
135 | ||
136 | if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_guard_clone(); | |
137 | ||
138 | facilitycode = (fc & 0x000000FF); | |
139 | cardnumber = (cn & 0x0000FFFF); | |
140 | ||
141 | if ( !GetGuardBits(facilitycode, cardnumber, bs)) { | |
142 | PrintAndLog("Error with tag bitstream generation."); | |
143 | return 1; | |
144 | } | |
145 | ||
146 | blocks[1] = bytebits_to_byte(bs,32); | |
147 | blocks[2] = bytebits_to_byte(bs+32,32); | |
148 | blocks[3] = bytebits_to_byte(bs+64,32); | |
149 | ||
150 | PrintAndLog("Preparing to clone Guardall to T55x7 with Facility Code: %u, Card Number: %u", facilitycode, cardnumber); | |
151 | PrintAndLog("Blk | Data "); | |
152 | PrintAndLog("----+------------"); | |
153 | for ( i = 0; i<4; ++i ) | |
154 | PrintAndLog(" %02d | %08x", i, blocks[i]); | |
155 | ||
156 | // UsbCommand resp; | |
157 | // UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}}; | |
158 | ||
159 | // for ( i = 0; i<5; ++i ) { | |
160 | // c.arg[0] = blocks[i]; | |
161 | // c.arg[1] = i; | |
162 | // clearCommandBuffer(); | |
163 | // SendCommand(&c); | |
164 | // if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){ | |
165 | // PrintAndLog("Error occurred, device did not respond during write operation."); | |
166 | // return -1; | |
167 | // } | |
168 | // } | |
169 | return 0; | |
170 | } | |
171 | ||
172 | int CmdGuardSim(const char *Cmd) { | |
173 | ||
174 | char cmdp = param_getchar(Cmd, 0); | |
175 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_guard_sim(); | |
176 | ||
177 | uint32_t facilitycode = 0, cardnumber = 0, fc = 0, cn = 0; | |
178 | ||
179 | uint8_t bs[96]; | |
180 | size_t size = sizeof(bs); | |
181 | memset(bs, 0x00, size); | |
182 | ||
183 | // Pyramid uses: ASK Biphase, clk: 32, invert: 0 | |
184 | uint64_t arg1, arg2; | |
185 | arg1 = (10 << 8) + 8; | |
186 | arg2 = 32 | 0; | |
187 | ||
188 | if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_guard_sim(); | |
189 | ||
190 | facilitycode = (fc & 0x000000FF); | |
191 | cardnumber = (cn & 0x0000FFFF); | |
192 | ||
193 | if ( !GetGuardBits(facilitycode, cardnumber, bs)) { | |
194 | PrintAndLog("Error with tag bitstream generation."); | |
195 | return 1; | |
196 | } | |
197 | ||
198 | PrintAndLog("Simulating Guardall - Facility Code: %u, CardNumber: %u", facilitycode, cardnumber ); | |
199 | ||
200 | UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}}; | |
201 | memcpy(c.d.asBytes, bs, size); | |
202 | clearCommandBuffer(); | |
203 | SendCommand(&c); | |
204 | return 0; | |
205 | } | |
206 | ||
207 | static command_t CommandTable[] = { | |
208 | {"help", CmdHelp, 1, "This help"}, | |
209 | {"read", CmdGuardRead, 0, "Attempt to read and extract tag data"}, | |
2453ca65 | 210 | // {"clone", CmdGuardClone, 0, "<Facility-Code> <Card Number> clone Guardall tag"}, |
211 | // {"sim", CmdGuardSim, 0, "<Facility-Code> <Card Number> simulate Guardall tag"}, | |
50564be0 | 212 | {NULL, NULL, 0, NULL} |
213 | }; | |
214 | ||
215 | int CmdLFGuard(const char *Cmd) { | |
216 | clearCommandBuffer(); | |
217 | CmdsParse(CommandTable, Cmd); | |
218 | return 0; | |
219 | } | |
220 | ||
221 | int CmdHelp(const char *Cmd) { | |
222 | CmdsHelp(CommandTable); | |
223 | return 0; | |
224 | } |