fix 'lf pcf7931 bruteforce' (bug reported in http://www.proxmark.org/forum/viewtopic.php?id=6490)

(and whitespace fixes)
This commit is contained in:
pwpiwi 2019-05-22 18:55:02 +02:00
commit 42c135a3b9
2 changed files with 83 additions and 89 deletions

View file

@ -107,7 +107,7 @@ size_t DemodPCF7931(uint8_t **outBlocks) {
for(j = 0; j < 16; ++j) {
blocks[num_blocks][j] =
128 * bits[j*8 + 7]+
64 * bits[j*8 + 6] +
64 * bits[j*8 + 6] +
32 * bits[j*8 + 5] +
16 * bits[j*8 + 4] +
8 * bits[j*8 + 3] +
@ -161,7 +161,7 @@ bool IsBlock1PCF7931(uint8_t *block) {
void ReadPCF7931() {
int found_blocks = 0; // successfully read blocks
int max_blocks = 8; // readable blocks
int max_blocks = 8; // readable blocks
uint8_t memory_blocks[8][17]; // PCF content
uint8_t single_blocks[8][17]; // PFC blocks with unknown position
@ -219,9 +219,9 @@ void ReadPCF7931() {
}
++tries;
continue;
}
}
Dbprintf("(dbg) got %d blocks (%d/%d found) (%d tries, %d errors)", n, found_blocks, (max_blocks == 0 ? found_blocks : max_blocks), tries, errors);
Dbprintf("(dbg) got %d blocks (%d/%d found) (%d tries, %d errors)", n, found_blocks, (max_blocks == 0 ? found_blocks : max_blocks), tries, errors);
i = 0;
if(!found_0_1) {
@ -335,10 +335,10 @@ static void RealWritePCF7931(uint8_t *pass, uint16_t init_delay, int32_t l, int3
//block adress on 6 bits
for (u = 0; u < 6; ++u) {
if (address & (1 << u)) { // bit 1
if (address & (1 << u)) { // bit 1
++parity;
AddBitPCF7931(1, tab, l, p);
} else { // bit 0
} else { // bit 0
AddBitPCF7931(0, tab, l, p);
}
}
@ -346,28 +346,28 @@ static void RealWritePCF7931(uint8_t *pass, uint16_t init_delay, int32_t l, int3
//byte address on 4 bits
for (u = 0; u < 4; ++u)
{
if (byte & (1 << u)) { // bit 1
if (byte & (1 << u)) { // bit 1
parity++;
AddBitPCF7931(1, tab, l, p);
}
else // bit 0
else // bit 0
AddBitPCF7931(0, tab, l, p);
}
//data on 8 bits
for (u=0; u<8; u++)
{
if (data&(1<<u)) { // bit 1
if (data&(1<<u)) { // bit 1
parity++;
AddBitPCF7931(1, tab, l, p);
}
else //bit 0
else //bit 0
AddBitPCF7931(0, tab, l, p);
}
//parity bit
if ((parity % 2) == 0)
AddBitPCF7931(0, tab, l, p); //even parity
AddBitPCF7931(0, tab, l, p); //even parity
else
AddBitPCF7931(1, tab, l, p);//odd parity
@ -401,13 +401,7 @@ void BruteForcePCF7931(uint64_t password, uint8_t tries, uint16_t init_delay, in
return;
}
pass_array[0] = password & 0xFF;
pass_array[1] = (password >> 8) & 0xFF;
pass_array[2] = (password >> 16) & 0xFF;
pass_array[3] = (password >> 24) & 0xFF;
pass_array[4] = (password >> 32) & 0xFF;
pass_array[5] = (password >> 40) & 0xFF;
pass_array[6] = (password >> 48) & 0xFF;
num_to_bytes(password, 7, pass_array);
Dbprintf("Trying: %02x %02x %02x %02x %02x %02x %02x ...",
pass_array[0],
@ -437,7 +431,7 @@ void BruteForcePCF7931(uint64_t password, uint8_t tries, uint16_t init_delay, in
/* Write on a byte of a PCF7931 tag
* @param address : address of the block to write
@param byte : address of the byte to write
@param data : data to write
@param data : data to write
*/
void WritePCF7931(uint8_t pass1, uint8_t pass2, uint8_t pass3, uint8_t pass4, uint8_t pass5, uint8_t pass6, uint8_t pass7, uint16_t init_delay, int32_t l, int32_t p, uint8_t address, uint8_t byte, uint8_t data) {
Dbprintf("Initialization delay : %d us", init_delay);
@ -519,7 +513,7 @@ void SendCmdPCF7931(uint32_t * tab) {
bool AddBytePCF7931(uint8_t byte, uint32_t * tab, int32_t l, int32_t p) {
uint32_t u;
for (u = 0; u < 8; ++u) {
if (byte & (1 << u)) { //bit is 1
if (byte & (1 << u)) { //bit is 1
if(AddBitPCF7931(1, tab, l, p)==1)return 1;
} else { //bit is 0
if(AddBitPCF7931(0, tab, l, p)==1)return 1;
@ -540,17 +534,17 @@ bool AddBitPCF7931(bool b, uint32_t * tab, int32_t l, int32_t p) {
for (u = 0; tab[u] != 0; u += 3){} //we put the cursor at the last value of the array
if (b == 1) { //add a bit 1
if (u == 0) tab[u] = 34 * T0_PCF + p;
else tab[u] = 34 * T0_PCF + tab[u-1] + p;
if (b == 1) { //add a bit 1
if (u == 0) tab[u] = 34 * T0_PCF + p;
else tab[u] = 34 * T0_PCF + tab[u-1] + p;
tab[u+1] = 6 * T0_PCF+tab[u] + l;
tab[u+2] = 88 * T0_PCF+tab[u + 1] - l - p;
return 0;
} else { //add a bit 0
} else { //add a bit 0
if (u == 0) tab[u] = 98 * T0_PCF + p;
else tab[u] = 98 * T0_PCF + tab[u-1] + p;
if (u == 0) tab[u] = 98 * T0_PCF + p;
else tab[u] = 98 * T0_PCF + tab[u-1] + p;
tab[u + 1] = 6 * T0_PCF + tab[u] + l;
tab[u + 2] = 24 * T0_PCF + tab[u + 1] - l - p;
@ -570,8 +564,8 @@ bool AddPatternPCF7931(uint32_t a, uint32_t b, uint32_t c, uint32_t * tab) {
uint32_t u = 0;
for(u = 0; tab[u] != 0; u += 3){} //we put the cursor at the last value of the array
if (u == 0) tab[u] = a;
else tab[u] = a + tab[u - 1];
if (u == 0) tab[u] = a;
else tab[u] = a + tab[u - 1];
tab[u + 1] = b + tab[u];
tab[u + 2] = c + tab[u + 1];

View file

@ -1,7 +1,7 @@
//-----------------------------------------------------------------------------
// Copyright (C) 2012 Chalk <chalk.secu at gmail.com>
// 2015 Dake <thomas.cayrou at gmail.com>
// 2018 sguerrini97 <sguerrini97 at gmail.com>
// 2018 sguerrini97 <sguerrini97 at gmail.com>
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
@ -180,24 +180,24 @@ int CmdLFPCF7931BruteForce(const char *Cmd){
uint8_t ctmp = param_getchar(Cmd, 0);
if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_pcf7931_bruteforce();
uint64_t start_password = 0;
uint8_t start_password[7] = {0};
uint8_t tries = 3;
if (param_gethex(Cmd, 0, (uint8_t*)(&start_password), 14)) return usage_pcf7931_bruteforce();
if (param_gethex(Cmd, 0, start_password, 14)) return usage_pcf7931_bruteforce();
if (param_getdec(Cmd, 1, &tries)) return usage_pcf7931_bruteforce();
PrintAndLog("Bruteforcing from password: %02x %02x %02x %02x %02x %02x %02x",
start_password & 0xFF,
(start_password >> 8) & 0xFF,
(start_password >> 16) & 0xFF,
(start_password >> 24) & 0xFF,
(start_password >> 32) & 0xFF,
(start_password >> 48) & 0xFF,
(start_password >> 56) & 0xFF);
start_password[0],
start_password[1],
start_password[2],
start_password[3],
start_password[4],
start_password[5],
start_password[6]);
PrintAndLog("Trying each password %d times", tries);
UsbCommand c = {CMD_PCF7931_BRUTEFORCE, {start_password, tries} };
UsbCommand c = {CMD_PCF7931_BRUTEFORCE, {bytes_to_num(start_password, 7), tries} };
c.d.asDwords[7] = (configPcf.OffsetWidth + 128);
c.d.asDwords[8] = (configPcf.OffsetPosition + 128);