]>
Commit | Line | Data |
---|---|---|
54a942b0 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2012 Chalk <chalk.secu at gmail.com> | |
dc4300ba | 3 | // 2015 Dake <thomas.cayrou at gmail.com> |
caaa4293 | 4 | // 2018 sguerrini97 <sguerrini97 at gmail.com> |
dc4300ba | 5 | |
54a942b0 | 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 | //----------------------------------------------------------------------------- | |
ad939de5 | 12 | |
13 | #include "cmdlfpcf7931.h" | |
14 | ||
54a942b0 | 15 | #include <stdio.h> |
16 | #include <string.h> | |
ad939de5 | 17 | #include "comms.h" |
54a942b0 | 18 | #include "ui.h" |
d10e08ae | 19 | #include "util.h" |
54a942b0 | 20 | #include "graph.h" |
21 | #include "cmdparser.h" | |
22 | #include "cmddata.h" | |
23 | #include "cmdmain.h" | |
24 | #include "cmdlf.h" | |
54a942b0 | 25 | |
26 | static int CmdHelp(const char *Cmd); | |
27 | ||
d10e08ae | 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, | |
caaa4293 | 36 | PCF7931_DEFAULT_OFFSET_WIDTH, |
d10e08ae | 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; | |
caaa4293 | 44 | configPcf.OffsetWidth = PCF7931_DEFAULT_OFFSET_WIDTH; |
45 | configPcf.OffsetPosition = PCF7931_DEFAULT_OFFSET_POSITION; | |
d10e08ae | 46 | return 0; |
47 | } | |
dc4300ba | 48 | |
d10e08ae | 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]"); | |
7cfc777b | 73 | PrintAndLog(" byteaddress Index of byte inside block to write [0-15]"); |
d10e08ae | 74 | PrintAndLog(" data one byte of data (hex)"); |
75 | PrintAndLog("Examples:"); | |
76 | PrintAndLog(" lf pcf7931 write 2 1 FF"); | |
77 | return 0; | |
54a942b0 | 78 | } |
79 | ||
818e15b0 S |
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 | ||
d10e08ae | 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"); | |
caaa4293 | 106 | PrintAndLog(" offset Low pulses position (in us) decimal"); |
d10e08ae | 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; | |
dc4300ba D |
113 | } |
114 | ||
caaa4293 | 115 | int CmdLFPCF7931Read(const char *Cmd){ |
dc4300ba | 116 | |
d10e08ae | 117 | uint8_t ctmp = param_getchar(Cmd, 0); |
118 | if ( ctmp == 'H' || ctmp == 'h' ) return usage_pcf7931_read(); | |
dc4300ba | 119 | |
d10e08ae | 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; | |
dc4300ba D |
129 | } |
130 | ||
caaa4293 | 131 | int CmdLFPCF7931Config(const char *Cmd){ |
d10e08ae | 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(); | |
caaa4293 | 136 | if ( ctmp == 'R' || ctmp == 'r' ) return pcf7931_resetConfig(); |
d10e08ae | 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); | |
caaa4293 | 151 | if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_pcf7931_write(); |
d10e08ae | 152 | |
153 | uint8_t block = 0, bytepos = 0, data = 0; | |
caaa4293 | 154 | |
d10e08ae | 155 | if ( param_getdec(Cmd, 0, &block) ) return usage_pcf7931_write(); |
156 | if ( param_getdec(Cmd, 1, &bytepos) ) return usage_pcf7931_write(); | |
caaa4293 | 157 | |
59b5b7e1 | 158 | if ( (block > 7) || (bytepos > 15) ) return usage_pcf7931_write(); |
d10e08ae | 159 | |
160 | data = param_get8ex(Cmd, 2, 0, 16); | |
caaa4293 | 161 | |
d10e08ae | 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 | } | |
dc4300ba | 177 | |
818e15b0 S |
178 | int CmdLFPCF7931BruteForce(const char *Cmd){ |
179 | ||
180 | uint8_t ctmp = param_getchar(Cmd, 0); | |
caaa4293 | 181 | if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_pcf7931_bruteforce(); |
818e15b0 | 182 | |
caaa4293 | 183 | uint8_t start_password[7] = {0}; |
818e15b0 | 184 | uint8_t tries = 3; |
caaa4293 | 185 | |
186 | if (param_gethex(Cmd, 0, start_password, 14)) return usage_pcf7931_bruteforce(); | |
818e15b0 | 187 | if (param_getdec(Cmd, 1, &tries)) return usage_pcf7931_bruteforce(); |
caaa4293 | 188 | |
818e15b0 | 189 | PrintAndLog("Bruteforcing from password: %02x %02x %02x %02x %02x %02x %02x", |
caaa4293 | 190 | start_password[0], |
191 | start_password[1], | |
192 | start_password[2], | |
193 | start_password[3], | |
194 | start_password[4], | |
195 | start_password[5], | |
196 | start_password[6]); | |
197 | ||
818e15b0 S |
198 | PrintAndLog("Trying each password %d times", tries); |
199 | ||
caaa4293 | 200 | UsbCommand c = {CMD_PCF7931_BRUTEFORCE, {bytes_to_num(start_password, 7), tries} }; |
201 | ||
818e15b0 S |
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 | ||
caaa4293 | 212 | static command_t CommandTable[] = |
54a942b0 | 213 | { |
d10e08ae | 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)"}, | |
818e15b0 | 218 | {"bruteforce", CmdLFPCF7931BruteForce, 0, "Bruteforce a PCF7931 transponder password."}, |
d10e08ae | 219 | {NULL, NULL, 0, NULL} |
54a942b0 | 220 | }; |
221 | ||
222 | int CmdLFPCF7931(const char *Cmd) | |
223 | { | |
d10e08ae | 224 | CmdsParse(CommandTable, Cmd); |
225 | return 0; | |
54a942b0 | 226 | } |
227 | ||
228 | int CmdHelp(const char *Cmd) | |
229 | { | |
d10e08ae | 230 | CmdsHelp(CommandTable); |
231 | return 0; | |
54a942b0 | 232 | } |