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>
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
9 //-----------------------------------------------------------------------------
10 // Low frequency PCF7931 commands
11 //-----------------------------------------------------------------------------
13 #include "cmdlfpcf7931.h"
21 #include "cmdparser.h"
26 static int CmdHelp(const char *Cmd
);
28 #define PCF7931_DEFAULT_INITDELAY 17500
29 #define PCF7931_DEFAULT_OFFSET_WIDTH 0
30 #define PCF7931_DEFAULT_OFFSET_POSITION 0
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
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
;
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
);
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");
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");
80 int usage_pcf7931_bruteforce()
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");
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");
115 int CmdLFPCF7931Read(const char *Cmd
){
117 uint8_t ctmp
= param_getchar(Cmd
, 0);
118 if ( ctmp
== 'H' || ctmp
== 'h' ) return usage_pcf7931_read();
121 UsbCommand c
= {CMD_PCF7931_READ
, {0, 0, 0}};
122 clearCommandBuffer();
124 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 2500) ) {
125 PrintAndLog("command execution time out");
131 int CmdLFPCF7931Config(const char *Cmd
){
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();
138 if ( param_gethex(Cmd
, 0, configPcf
.Pwd
, 14) ) return usage_pcf7931_config();
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);
144 pcf7931_printConfig();
148 int CmdLFPCF7931Write(const char *Cmd
){
150 uint8_t ctmp
= param_getchar(Cmd
, 0);
151 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_pcf7931_write();
153 uint8_t block
= 0, bytepos
= 0, data
= 0;
155 if ( param_getdec(Cmd
, 0, &block
) ) return usage_pcf7931_write();
156 if ( param_getdec(Cmd
, 1, &bytepos
) ) return usage_pcf7931_write();
158 if ( (block
> 7) || (bytepos
> 15) ) return usage_pcf7931_write();
160 data
= param_get8ex(Cmd
, 2, 0, 16);
162 PrintAndLog("Writing block: %d", block
);
163 PrintAndLog(" pos: %d", bytepos
);
164 PrintAndLog(" data: 0x%02X", data
);
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
;
172 clearCommandBuffer();
178 int CmdLFPCF7931BruteForce(const char *Cmd
){
180 uint8_t ctmp
= param_getchar(Cmd
, 0);
181 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_pcf7931_bruteforce();
183 uint8_t start_password
[7] = {0};
186 if (param_gethex(Cmd
, 0, start_password
, 14)) return usage_pcf7931_bruteforce();
187 if (param_getdec(Cmd
, 1, &tries
)) return usage_pcf7931_bruteforce();
189 PrintAndLog("Bruteforcing from password: %02x %02x %02x %02x %02x %02x %02x",
198 PrintAndLog("Trying each password %d times", tries
);
200 UsbCommand c
= {CMD_PCF7931_BRUTEFORCE
, {bytes_to_num(start_password
, 7), tries
} };
202 c
.d
.asDwords
[7] = (configPcf
.OffsetWidth
+ 128);
203 c
.d
.asDwords
[8] = (configPcf
.OffsetPosition
+ 128);
204 c
.d
.asDwords
[9] = configPcf
.InitDelay
;
206 clearCommandBuffer();
212 static command_t CommandTable
[] =
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
}
222 int CmdLFPCF7931(const char *Cmd
)
224 CmdsParse(CommandTable
, Cmd
);
228 int CmdHelp(const char *Cmd
)
230 CmdsHelp(CommandTable
);