]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfjablotron.c
ADD: LF JABLOTRON functionality. with clone/sim and detection in LF SEARCH.
[proxmark3-svn] / client / cmdlfjablotron.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 "cmdlfjablotron.h"
10
11 static int CmdHelp(const char *Cmd);
12
13 int usage_lf_jablotron_clone(void){
14 PrintAndLog("clone a Jablotron tag to a T55x7 tag.");
15 PrintAndLog("Usage: lf jablotron clone d <Card-ID> <Q5>");
16 PrintAndLog("Options :");
17 PrintAndLog(" d <Card-ID> : jablotron card ID");
18 PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)");
19 PrintAndLog("");
20 PrintAndLog("Sample : lf jablotron clone d 123456789");
21 return 0;
22 }
23
24 int usage_lf_jablotron_sim(void) {
25 PrintAndLog("Enables simulation of jablotron card with specified card number.");
26 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
27 PrintAndLog("Per jablotron format, the card number is 9 digit number and can contain *# chars. Larger values are truncated.");
28 PrintAndLog("");
29 PrintAndLog("Usage: lf jablotron sim d <Card-ID> or H <hex-ID>");
30 PrintAndLog("Options :");
31 PrintAndLog(" d <Card-ID> : jablotron card number");
32 // PrintAndLog(" H <hex-ID> : 8 digit hex card number");
33 PrintAndLog("");
34 PrintAndLog("Sample : lf jablotron sim d 123456789");
35 return 0;
36 }
37
38 int getJablotronBits(uint64_t fullcode, uint8_t *bits) {
39 //preamp
40 num_to_bytebits(0xFFFF, 16, bits);
41
42 //fullcode
43 num_to_bytebits(fullcode, 40, bits+16);
44
45 //chksum byte
46 uint8_t crc = 0;
47 for (int i=16; i < 56; i += 8) {
48 crc += bytebits_to_byte(bits+i,8);
49 }
50 crc ^= 0x3A;
51 num_to_bytebits(crc, 8, bits+56);
52
53 return 1;
54 }
55
56 //see ASKDemod for what args are accepted
57 int CmdJablotronDemod(const char *Cmd) {
58
59 //Differential Biphase / di-phase (inverted biphase)
60 //get binary from ask wave
61 if (!ASKbiphaseDemod("0 64 1 0", FALSE)) {
62 if (g_debugMode) PrintAndLog("Error Jablotron: ASKbiphaseDemod failed");
63 return 0;
64 }
65 size_t size = DemodBufferLen;
66 int ans = JablotronDemod(DemodBuffer, &size);
67 if (ans < 0){
68 if (g_debugMode){
69 // if (ans == -5)
70 // PrintAndLog("DEBUG: Error - not enough samples");
71 // else if (ans == -1)
72 // PrintAndLog("DEBUG: Error - only noise found");
73 // else if (ans == -2)
74 // PrintAndLog("DEBUG: Error - problem during ASK/Biphase demod");
75 if (ans == -3)
76 PrintAndLog("DEBUG: Error - Size not correct: %d", size);
77 else if (ans == -4)
78 PrintAndLog("DEBUG: Error - Jablotron preamble not found");
79 else
80 PrintAndLog("DEBUG: Error - ans: %d", ans);
81 }
82 return 0;
83 }
84 //got a good demod
85 uint32_t raw1 = bytebits_to_byte(DemodBuffer+ans, 32);
86 uint32_t raw2 = bytebits_to_byte(DemodBuffer+ans+32, 32);
87 uint64_t cardid = (raw1 & 0x0000FFFF);
88 cardid <<= 32;
89 cardid |= (raw2 >> 8);
90
91 PrintAndLog("Jablotron Tag Found: Card ID %12X", cardid);
92 PrintAndLog("Raw: %08X%08X", raw1 ,raw2);
93
94 setDemodBuf(DemodBuffer+ans, 64, 0);
95
96 //PrintAndLog("1410-%u-%u-%08X-%02X", fullcode);
97 return 1;
98 }
99
100 int CmdJablotronRead(const char *Cmd) {
101 // read lf silently
102 CmdLFRead("s");
103 // get samples silently
104 getSamples("30000",false);
105 // demod and output Presco ID
106 return CmdJablotronDemod(Cmd);
107 }
108
109 int CmdJablotronClone(const char *Cmd) {
110
111 uint64_t fullcode = 0;
112 uint32_t blocks[3] = {T55x7_MODULATION_DIPHASE | T55x7_BITRATE_RF_64 | 2<<T55x7_MAXBLOCK_SHIFT, 0, 0};
113
114 uint8_t bits[64];
115 uint8_t *bs = bits;
116 memset(bs, 0, sizeof(bits));
117
118 char cmdp = param_getchar(Cmd, 0);
119 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_jablotron_clone();
120
121 fullcode = param_get64ex(Cmd, 1, 0, 16);
122
123 //Q5
124 if (param_getchar(Cmd, 2) == 'Q' || param_getchar(Cmd, 2) == 'q') {
125 //t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
126 blocks[0] = T5555_MODULATION_BIPHASE | T5555_INVERT_OUTPUT | 64<<T5555_BITRATE_SHIFT | 2<<T5555_MAXBLOCK_SHIFT;
127 }
128
129 if ((fullcode & 0xFFFFFFFFFFFF) != fullcode) {
130 fullcode &= 0xFFFFFFFFFFFF;
131 PrintAndLog("Card Number Truncated to 40-bits: %u", fullcode);
132 }
133
134 if ( !getJablotronBits(fullcode, bs)) {
135 PrintAndLog("Error with tag bitstream generation.");
136 return 1;
137 }
138
139 //
140 blocks[1] = bytebits_to_byte(bs,32);
141 blocks[2] = bytebits_to_byte(bs+32,32);
142
143 PrintAndLog("Preparing to clone Jablotron to T55x7 with FullCode: %12X", fullcode);
144 PrintAndLog("Blk | Data ");
145 PrintAndLog("----+------------");
146 PrintAndLog(" 00 | 0x%08x", blocks[0]);
147 PrintAndLog(" 01 | 0x%08x", blocks[1]);
148 PrintAndLog(" 02 | 0x%08x", blocks[2]);
149
150 UsbCommand resp;
151 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
152
153 for (int i=4; i>=0; i--) {
154 c.arg[0] = blocks[i];
155 c.arg[1] = i;
156 clearCommandBuffer();
157 SendCommand(&c);
158 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
159 PrintAndLog("Error occurred, device did not respond during write operation.");
160 return -1;
161 }
162 }
163 return 0;
164 }
165
166 int CmdJablotronSim(const char *Cmd) {
167 uint64_t fullcode = 0;
168
169 char cmdp = param_getchar(Cmd, 0);
170 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_jablotron_sim();
171
172 fullcode = param_get64ex(Cmd, 1, 0, 16);
173
174 uint8_t clk = 64, encoding = 2, separator = 0, invert = 1;
175 uint16_t arg1, arg2;
176 size_t size = 64;
177 arg1 = clk << 8 | encoding;
178 arg2 = invert << 8 | separator;
179
180 PrintAndLog("Simulating Jablotron - FullCode: %12X", fullcode);
181
182 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
183 getJablotronBits(fullcode, c.d.asBytes);
184 clearCommandBuffer();
185 SendCommand(&c);
186 return 0;
187 }
188
189 static command_t CommandTable[] = {
190 {"help", CmdHelp, 1, "This help"},
191 {"read", CmdJablotronRead, 0, "Attempt to read and Extract tag data"},
192 {"clone", CmdJablotronClone, 0, "h <hex> [Q5] clone jablotron tag"},
193 {"sim", CmdJablotronSim, 0, "h <hex> simulate jablotron tag"},
194 {NULL, NULL, 0, NULL}
195 };
196
197 int CmdLFJablotron(const char *Cmd) {
198 clearCommandBuffer();
199 CmdsParse(CommandTable, Cmd);
200 return 0;
201 }
202
203 int CmdHelp(const char *Cmd) {
204 CmdsHelp(CommandTable);
205 return 0;
206 }
Impressum, Datenschutz