]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2012 Chalk <chalk.secu at gmail.com> | |
3 | // 2015 Dake <thomas.cayrou at gmail.com> | |
4 | // 2018 sguerrini97 <sguerrini97 at gmail.com> | |
5 | ||
6 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
7 | // at your option, any later version. See the LICENSE.txt file for the text of | |
8 | // the license. | |
9 | //----------------------------------------------------------------------------- | |
10 | // Low frequency PCF7931 commands | |
11 | //----------------------------------------------------------------------------- | |
12 | ||
13 | #include "cmdlfpcf7931.h" | |
14 | ||
15 | #include <stdio.h> | |
16 | #include <string.h> | |
17 | #include "comms.h" | |
18 | #include "ui.h" | |
19 | #include "util.h" | |
20 | #include "graph.h" | |
21 | #include "cmdparser.h" | |
22 | #include "cmddata.h" | |
23 | #include "cmdmain.h" | |
24 | #include "cmdlf.h" | |
25 | ||
26 | static int CmdHelp(const char *Cmd); | |
27 | ||
28 | #define PCF7931_DEFAULT_INITDELAY 17500 | |
29 | #define PCF7931_DEFAULT_OFFSET_WIDTH 0 | |
30 | #define PCF7931_DEFAULT_OFFSET_POSITION 0 | |
31 | ||
32 | // Default values - Configuration | |
33 | struct pcf7931_config configPcf = { | |
34 | {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}, | |
35 | PCF7931_DEFAULT_INITDELAY, | |
36 | PCF7931_DEFAULT_OFFSET_WIDTH, | |
37 | PCF7931_DEFAULT_OFFSET_POSITION | |
38 | }; | |
39 | ||
40 | // Resets the configuration settings to default values. | |
41 | int pcf7931_resetConfig(){ | |
42 | memset(configPcf.Pwd, 0xFF, sizeof(configPcf.Pwd) ); | |
43 | configPcf.InitDelay = PCF7931_DEFAULT_INITDELAY; | |
44 | configPcf.OffsetWidth = PCF7931_DEFAULT_OFFSET_WIDTH; | |
45 | configPcf.OffsetPosition = PCF7931_DEFAULT_OFFSET_POSITION; | |
46 | return 0; | |
47 | } | |
48 | ||
49 | int pcf7931_printConfig(){ | |
50 | PrintAndLog("Password (LSB first on bytes) : %s", sprint_hex( configPcf.Pwd, sizeof(configPcf.Pwd))); | |
51 | PrintAndLog("Tag initialization delay : %d us", configPcf.InitDelay); | |
52 | PrintAndLog("Offset low pulses width : %d us", configPcf.OffsetWidth); | |
53 | PrintAndLog("Offset low pulses position : %d us", configPcf.OffsetPosition); | |
54 | return 0; | |
55 | } | |
56 | ||
57 | int usage_pcf7931_read(){ | |
58 | PrintAndLog("Usage: lf pcf7931 read [h] "); | |
59 | PrintAndLog("This command tries to read a PCF7931 tag."); | |
60 | PrintAndLog("Options:"); | |
61 | PrintAndLog(" h This help"); | |
62 | PrintAndLog("Examples:"); | |
63 | PrintAndLog(" lf pcf7931 read"); | |
64 | return 0; | |
65 | } | |
66 | ||
67 | int usage_pcf7931_write(){ | |
68 | PrintAndLog("Usage: lf pcf7931 write [h] <block address> <byte address> <data>"); | |
69 | PrintAndLog("This command tries to write a PCF7931 tag."); | |
70 | PrintAndLog("Options:"); | |
71 | PrintAndLog(" h This help"); | |
72 | PrintAndLog(" blockaddress Block to save [0-7]"); | |
73 | PrintAndLog(" byteaddress Index of byte inside block to write [0-15]"); | |
74 | PrintAndLog(" data one byte of data (hex)"); | |
75 | PrintAndLog("Examples:"); | |
76 | PrintAndLog(" lf pcf7931 write 2 1 FF"); | |
77 | return 0; | |
78 | } | |
79 | ||
80 | int usage_pcf7931_bruteforce() | |
81 | { | |
82 | PrintAndLog("Usage: lf pcf7931 bruteforce [h] <start password> <tries>"); | |
83 | PrintAndLog("This command tries to disable PAC of a PCF7931 transponder by bruteforcing the password."); | |
84 | PrintAndLog("!! THIS IS NOT INTENDED TO RECOVER THE FULL PASSWORD !!"); | |
85 | PrintAndLog("!! DO NOT USE UNLESS THE FIRST 5 BYTES OF THE PASSWORD ARE KNOWN !!"); | |
86 | PrintAndLog("Options:"); | |
87 | PrintAndLog(" h This help"); | |
88 | PrintAndLog(" start password hex password to start from"); | |
89 | PrintAndLog(" tries How many times to send the same data frame"); | |
90 | PrintAndLog("Examples:"); | |
91 | PrintAndLog(" lf pcf7931 bruteforce 00000000123456 3"); | |
92 | return 0; | |
93 | } | |
94 | ||
95 | int usage_pcf7931_config(){ | |
96 | PrintAndLog("Usage: lf pcf7931 config [h] [r] <pwd> <delay> <offset width> <offset position>"); | |
97 | PrintAndLog("This command tries to set the configuration used with PCF7931 commands"); | |
98 | PrintAndLog("The time offsets could be useful to correct slew rate generated by the antenna"); | |
99 | PrintAndLog("Caling without some parameter will print the current configuration."); | |
100 | PrintAndLog("Options:"); | |
101 | PrintAndLog(" h This help"); | |
102 | PrintAndLog(" r Reset configuration to default values"); | |
103 | PrintAndLog(" pwd Password, hex, 7bytes, LSB-order"); | |
104 | PrintAndLog(" delay Tag initialization delay (in us) decimal"); | |
105 | PrintAndLog(" offset Low pulses width (in us) decimal"); | |
106 | PrintAndLog(" offset Low pulses position (in us) decimal"); | |
107 | PrintAndLog("Examples:"); | |
108 | PrintAndLog(" lf pcf7931 config"); | |
109 | PrintAndLog(" lf pcf7931 config r"); | |
110 | PrintAndLog(" lf pcf7931 config 11223344556677 20000"); | |
111 | PrintAndLog(" lf pcf7931 config 11223344556677 17500 -10 30"); | |
112 | return 0; | |
113 | } | |
114 | ||
115 | int CmdLFPCF7931Read(const char *Cmd){ | |
116 | ||
117 | uint8_t ctmp = param_getchar(Cmd, 0); | |
118 | if ( ctmp == 'H' || ctmp == 'h' ) return usage_pcf7931_read(); | |
119 | ||
120 | UsbCommand resp; | |
121 | UsbCommand c = {CMD_PCF7931_READ, {0, 0, 0}}; | |
122 | clearCommandBuffer(); | |
123 | SendCommand(&c); | |
124 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) { | |
125 | PrintAndLog("command execution time out"); | |
126 | return 1; | |
127 | } | |
128 | return 0; | |
129 | } | |
130 | ||
131 | int CmdLFPCF7931Config(const char *Cmd){ | |
132 | ||
133 | uint8_t ctmp = param_getchar(Cmd, 0); | |
134 | if ( ctmp == 0) return pcf7931_printConfig(); | |
135 | if ( ctmp == 'H' || ctmp == 'h' ) return usage_pcf7931_config(); | |
136 | if ( ctmp == 'R' || ctmp == 'r' ) return pcf7931_resetConfig(); | |
137 | ||
138 | if ( param_gethex(Cmd, 0, configPcf.Pwd, 14) ) return usage_pcf7931_config(); | |
139 | ||
140 | configPcf.InitDelay = (param_get32ex(Cmd,1,0,10) & 0xFFFF); | |
141 | configPcf.OffsetWidth = (int)(param_get32ex(Cmd,2,0,10) & 0xFFFF); | |
142 | configPcf.OffsetPosition = (int)(param_get32ex(Cmd,3,0,10) & 0xFFFF); | |
143 | ||
144 | pcf7931_printConfig(); | |
145 | return 0; | |
146 | } | |
147 | ||
148 | int CmdLFPCF7931Write(const char *Cmd){ | |
149 | ||
150 | uint8_t ctmp = param_getchar(Cmd, 0); | |
151 | if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_pcf7931_write(); | |
152 | ||
153 | uint8_t block = 0, bytepos = 0, data = 0; | |
154 | ||
155 | if ( param_getdec(Cmd, 0, &block) ) return usage_pcf7931_write(); | |
156 | if ( param_getdec(Cmd, 1, &bytepos) ) return usage_pcf7931_write(); | |
157 | ||
158 | if ( (block > 7) || (bytepos > 15) ) return usage_pcf7931_write(); | |
159 | ||
160 | data = param_get8ex(Cmd, 2, 0, 16); | |
161 | ||
162 | PrintAndLog("Writing block: %d", block); | |
163 | PrintAndLog(" pos: %d", bytepos); | |
164 | PrintAndLog(" data: 0x%02X", data); | |
165 | ||
166 | UsbCommand c = {CMD_PCF7931_WRITE, { block, bytepos, data} }; | |
167 | memcpy(c.d.asDwords, configPcf.Pwd, sizeof(configPcf.Pwd) ); | |
168 | c.d.asDwords[7] = (configPcf.OffsetWidth + 128); | |
169 | c.d.asDwords[8] = (configPcf.OffsetPosition + 128); | |
170 | c.d.asDwords[9] = configPcf.InitDelay; | |
171 | ||
172 | clearCommandBuffer(); | |
173 | SendCommand(&c); | |
174 | //no ack? | |
175 | return 0; | |
176 | } | |
177 | ||
178 | int CmdLFPCF7931BruteForce(const char *Cmd){ | |
179 | ||
180 | uint8_t ctmp = param_getchar(Cmd, 0); | |
181 | if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_pcf7931_bruteforce(); | |
182 | ||
183 | uint64_t start_password = 0; | |
184 | uint8_t tries = 3; | |
185 | ||
186 | if (param_gethex(Cmd, 0, (uint8_t*)(&start_password), 14)) return usage_pcf7931_bruteforce(); | |
187 | if (param_getdec(Cmd, 1, &tries)) return usage_pcf7931_bruteforce(); | |
188 | ||
189 | PrintAndLog("Bruteforcing from password: %02x %02x %02x %02x %02x %02x %02x", | |
190 | start_password & 0xFF, | |
191 | (start_password >> 8) & 0xFF, | |
192 | (start_password >> 16) & 0xFF, | |
193 | (start_password >> 24) & 0xFF, | |
194 | (start_password >> 32) & 0xFF, | |
195 | (start_password >> 48) & 0xFF, | |
196 | (start_password >> 56) & 0xFF); | |
197 | ||
198 | PrintAndLog("Trying each password %d times", tries); | |
199 | ||
200 | UsbCommand c = {CMD_PCF7931_BRUTEFORCE, {start_password, tries} }; | |
201 | ||
202 | c.d.asDwords[7] = (configPcf.OffsetWidth + 128); | |
203 | c.d.asDwords[8] = (configPcf.OffsetPosition + 128); | |
204 | c.d.asDwords[9] = configPcf.InitDelay; | |
205 | ||
206 | clearCommandBuffer(); | |
207 | SendCommand(&c); | |
208 | //no ack? | |
209 | return 0; | |
210 | } | |
211 | ||
212 | static command_t CommandTable[] = | |
213 | { | |
214 | {"help", CmdHelp, 1, "This help"}, | |
215 | {"read", CmdLFPCF7931Read, 0, "Read content of a PCF7931 transponder"}, | |
216 | {"write", CmdLFPCF7931Write, 0, "Write data on a PCF7931 transponder."}, | |
217 | {"config", CmdLFPCF7931Config, 1, "Configure the password, the tags initialization delay and time offsets (optional)"}, | |
218 | {"bruteforce", CmdLFPCF7931BruteForce, 0, "Bruteforce a PCF7931 transponder password."}, | |
219 | {NULL, NULL, 0, NULL} | |
220 | }; | |
221 | ||
222 | int CmdLFPCF7931(const char *Cmd) | |
223 | { | |
224 | CmdsParse(CommandTable, Cmd); | |
225 | return 0; | |
226 | } | |
227 | ||
228 | int CmdHelp(const char *Cmd) | |
229 | { | |
230 | CmdsHelp(CommandTable); | |
231 | return 0; | |
232 | } |