chg: 'hf iclass loclass' - break attack earlier if one csn byte recovery fails. No need to try run it. and some colors...

This commit is contained in:
iceman1001 2019-08-19 20:24:08 +02:00
commit f06846e2d0
2 changed files with 22 additions and 9 deletions

View file

@ -39,7 +39,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "util.h" // sprint_hex
#include "commonutil.h" // ARRAYLEN #include "commonutil.h" // ARRAYLEN
#include "fileutils.h" #include "fileutils.h"
@ -160,23 +160,26 @@ void printarr(const char *name, uint8_t *arr, int len) {
} }
void printvar(const char *name, uint8_t *arr, int len) { void printvar(const char *name, uint8_t *arr, int len) {
int cx, i; /*
int cx, i;
size_t outsize = 40 + strlen(name) + len * 2; size_t outsize = 40 + strlen(name) + len * 2;
char *output = calloc(outsize, sizeof(char)); char *output = calloc(outsize, sizeof(char));
cx = snprintf(output, outsize, "%s = ", name); cx = snprintf(output, outsize, "%s = ", name);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
cx += snprintf(output + cx, outsize - cx, "%02x", *(arr + i)); //2 bytes per byte cx += snprintf(output + cx, outsize - cx, "%02x", *(arr + i)); //2 bytes per byte
} }
PrintAndLogEx(NORMAL, output); PrintAndLogEx(NORMAL, output);
free(output); free(output);
*/
PrintAndLogEx(NORMAL, "%s = " _YELLOW_("%s"), name, sprint_hex(arr, len) );
} }
void printarr_human_readable(const char *title, uint8_t *arr, int len) { void printarr_human_readable(const char *title, uint8_t *arr, int len) {
int cx, i; int cx = 0, i;
size_t outsize = 100 + strlen(title) + len * 4; size_t outsize = 100 + strlen(title) + len * 4;
char *output = calloc(outsize, sizeof(char)); char *output = calloc(outsize, sizeof(char));
cx = snprintf(output, outsize, "\n\t%s\n", title); PrintAndLogEx(NORMAL, "\n %s", title);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (i % 16 == 0) if (i % 16 == 0)
cx += snprintf(output + cx, outsize - cx, "\n%02x| ", i); cx += snprintf(output + cx, outsize - cx, "\n%02x| ", i);

View file

@ -479,7 +479,7 @@ int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[]) {
return 1; return 1;
} else { } else {
PrintAndLogEx(NORMAL, "\n"); PrintAndLogEx(NORMAL, "\n");
PrintAndLogEx(SUCCESS, "Key verified ok!\n"); PrintAndLogEx(SUCCESS, _GREEN_("Key verified ok!") );
} }
return 0; return 0;
} }
@ -502,11 +502,19 @@ int bruteforceDump(uint8_t dump[], size_t dumpsize, uint16_t keytable[]) {
for (i = 0 ; i * itemsize < dumpsize ; i++) { for (i = 0 ; i * itemsize < dumpsize ; i++) {
memcpy(attack, dump + i * itemsize, itemsize); memcpy(attack, dump + i * itemsize, itemsize);
errors += bruteforceItem(*attack, keytable); errors += bruteforceItem(*attack, keytable);
if ( errors )
break;
} }
free(attack); free(attack);
t1 = msclock() - t1; t1 = msclock() - t1;
PrintAndLogEx(SUCCESS, "time: %" PRIu64 " seconds", t1 / 1000); PrintAndLogEx(SUCCESS, "time: %" PRIu64 " seconds", t1 / 1000);
if ( errors ) {
PrintAndLogEx(ERR, "loclass exiting. Try run " _YELLOW_("`hf iclass sim 2`") "again and collect new data");
return 1;
}
// Pick out the first 16 bytes of the keytable. // Pick out the first 16 bytes of the keytable.
// The keytable is now in 16-bit ints, where the upper 8 bits // The keytable is now in 16-bit ints, where the upper 8 bits
// indicate crack-status. Those must be discarded for the // indicate crack-status. Those must be discarded for the
@ -516,8 +524,10 @@ int bruteforceDump(uint8_t dump[], size_t dumpsize, uint16_t keytable[]) {
for (i = 0 ; i < 16 ; i++) { for (i = 0 ; i < 16 ; i++) {
first16bytes[i] = keytable[i] & 0xFF; first16bytes[i] = keytable[i] & 0xFF;
if (!(keytable[i] & CRACKED)) if (!(keytable[i] & CRACKED)) {
PrintAndLogEx(WARNING, "Warning: we are missing byte %d, custom key calculation will fail...", i); PrintAndLogEx(WARNING, "Warning: we are missing byte %d, custom key calculation will fail...", i);
return 1;
}
} }
errors += calculateMasterKey(first16bytes, NULL); errors += calculateMasterKey(first16bytes, NULL);
return errors; return errors;
@ -532,7 +542,7 @@ int bruteforceDump(uint8_t dump[], size_t dumpsize, uint16_t keytable[]) {
int bruteforceFile(const char *filename, uint16_t keytable[]) { int bruteforceFile(const char *filename, uint16_t keytable[]) {
FILE *f = fopen(filename, "rb"); FILE *f = fopen(filename, "rb");
if (!f) { if (!f) {
PrintAndLogEx(WARNING, "Failed to read from file '%s'", filename); PrintAndLogEx(WARNING, "Failed to read from file " _YELLOW_("%s"), filename);
return 1; return 1;
} }
@ -612,7 +622,7 @@ static int _testBruteforce() {
} else if (fileExists("client/loclass/iclass_dump.bin")) { } else if (fileExists("client/loclass/iclass_dump.bin")) {
errors |= bruteforceFile("client/loclass/iclass_dump.bin", keytable); errors |= bruteforceFile("client/loclass/iclass_dump.bin", keytable);
} else { } else {
PrintAndLogEx(ERR, "Error: The file iclass_dump.bin was not found!"); PrintAndLogEx(ERR, "Error: The file " _YELLOW_("iclass_dump.bin") "was not found!");
} }
} }
return errors; return errors;