CHG: 'hf mf hardnested' now use hf-mf-<UID>-nonces.bin by default, with parameter 'u' or 'f' to override.

This commit is contained in:
Brian Pow 2018-02-08 23:01:05 +08:00
commit 35e62e7d1f
4 changed files with 109 additions and 78 deletions

View file

@ -122,15 +122,17 @@ int usage_hf14_hardnested(void){
PrintAndLog(" ");
PrintAndLog("options:");
PrintAndLog(" h this help");
PrintAndLog(" w acquire nonces and write them to binary file nonces.bin");
PrintAndLog(" w acquire nonces and UID, and write them to binary file with default name hf-mf-<UID>-nonces.bin");
PrintAndLog(" s slower acquisition (required by some non standard cards)");
PrintAndLog(" r read nonces.bin and start attack");
PrintAndLog(" u <UID> read/write hf-mf-<UID>-nonces.bin instead of default name");
PrintAndLog(" f <name> read/write <name> instead of default name");
PrintAndLog(" t tests?");
PrintAndLog(" ");
PrintAndLog("samples:");
PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A f nonces.bin w s");
PrintAndLog(" hf mf hardnested r");
PrintAndLog(" hf mf hardnested r a0a1a2a3a4a5");
PrintAndLog(" ");
@ -1230,11 +1232,10 @@ int CmdHF14AMfNestedHard(const char *Cmd) {
uint8_t trgKeyType = 0;
uint8_t key[6] = {0, 0, 0, 0, 0, 0};
uint8_t trgkey[6] = {0, 0, 0, 0, 0, 0};
uint8_t cmdp=0;
char filename[FILE_PATH_SIZE], *fptr;
char szTemp[FILE_PATH_SIZE];
char ctmp;
ctmp = param_getchar(Cmd, 0);
if (ctmp == 'H' || ctmp == 'h' ) return usage_hf14_hardnested();
if (ctmp != 'R' && ctmp != 'r' && ctmp != 'T' && ctmp != 't' && strlen(Cmd) < 20) return usage_hf14_hardnested();
bool know_target_key = false;
bool nonce_file_read = false;
@ -1242,20 +1243,26 @@ int CmdHF14AMfNestedHard(const char *Cmd) {
bool slow = false;
int tests = 0;
if (ctmp == 'R' || ctmp == 'r') {
switch(tolower(param_getchar(Cmd, cmdp))) {
case 'h': return usage_hf14_hardnested();
case 'r':
strcpy(filename,"nonces.bin");
nonce_file_read = true;
if (!param_gethex(Cmd, 1, trgkey, 12)) {
if (!param_gethex(Cmd, cmdp+1, trgkey, 12)) {
know_target_key = true;
}
} else if (ctmp == 'T' || ctmp == 't') {
tests = param_get32ex(Cmd, 1, 100, 10);
if (!param_gethex(Cmd, 2, trgkey, 12)) {
cmdp++;
break;
case 't':
tests = param_get32ex(Cmd, cmdp+1, 100, 10);
if (!param_gethex(Cmd, cmdp+2, trgkey, 12)) {
know_target_key = true;
}
} else {
blockNo = param_get8(Cmd, 0);
ctmp = param_getchar(Cmd, 1);
cmdp+=2;
break;
default:
blockNo = param_get8(Cmd, cmdp);
ctmp = param_getchar(Cmd, cmdp+1);
if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
PrintAndLog("Key type must be A or B");
return 1;
@ -1264,13 +1271,13 @@ int CmdHF14AMfNestedHard(const char *Cmd) {
keyType = 1;
}
if (param_gethex(Cmd, 2, key, 12)) {
if (param_gethex(Cmd, cmdp+2, key, 12)) {
PrintAndLog("Key must include 12 HEX symbols");
return 1;
}
trgBlockNo = param_get8(Cmd, 3);
ctmp = param_getchar(Cmd, 4);
trgBlockNo = param_get8(Cmd, cmdp+3);
ctmp = param_getchar(Cmd, cmdp+4);
if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
PrintAndLog("Target key type must be A or B");
return 1;
@ -1278,25 +1285,42 @@ int CmdHF14AMfNestedHard(const char *Cmd) {
if (ctmp != 'A' && ctmp != 'a') {
trgKeyType = 1;
}
uint16_t i = 5;
if (!param_gethex(Cmd, 5, trgkey, 12)) {
cmdp+=5;
}
if (!param_gethex(Cmd, cmdp, trgkey, 12)) {
know_target_key = true;
i++;
cmdp++;
}
while ((ctmp = param_getchar(Cmd, i))) {
if (ctmp == 's' || ctmp == 'S') {
while ((ctmp = param_getchar(Cmd, cmdp))) {
switch(tolower(ctmp))
{
case 's':
slow = true;
} else if (ctmp == 'w' || ctmp == 'W') {
break;
case 'w':
nonce_file_write = true;
} else {
PrintAndLog("Possible options are w and/or s");
fptr=GenerateFilename("hf-mf-","-nonces.bin");
if (fptr == NULL)
return 1;
strncpy(filename, fptr, FILE_PATH_SIZE);
break;
case 'u':
param_getstr(Cmd, cmdp+1, szTemp, FILE_PATH_SIZE);
snprintf(filename, FILE_PATH_SIZE, "hf-mf-%s-nonces.bin", szTemp);
cmdp++;
break;
case 'f':
param_getstr(Cmd, cmdp+1, szTemp, FILE_PATH_SIZE);
strncpy(filename, szTemp, FILE_PATH_SIZE);
cmdp++;
break;
default:
PrintAndLog("Unknown parameter '%c'\n", ctmp);
usage_hf14_hardnested();
return 1;
}
i++;
}
cmdp++;
}
if ( !know_target_key ) {
@ -1319,7 +1343,7 @@ int CmdHF14AMfNestedHard(const char *Cmd) {
tests);
uint64_t foundkey = 0;
int16_t isOK = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, know_target_key ? trgkey : NULL, nonce_file_read, nonce_file_write, slow, tests, &foundkey);
int16_t isOK = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, know_target_key ? trgkey : NULL, nonce_file_read, nonce_file_write, slow, tests, &foundkey, filename);
DropField();
if (isOK) {

View file

@ -1054,9 +1054,10 @@ static void estimate_sum_a8(void)
}
static int read_nonce_file(void)
static int read_nonce_file(char *filename)
{
FILE *fnonces = NULL;
char progress_text[80]="";
size_t bytes_read;
uint8_t trgBlockNo;
uint8_t trgKeyType;
@ -1065,12 +1066,12 @@ static int read_nonce_file(void)
uint8_t par_enc;
num_acquired_nonces = 0;
if ((fnonces = fopen("nonces.bin","rb")) == NULL) {
PrintAndLog("Could not open file nonces.bin");
if ((fnonces = fopen(filename,"rb")) == NULL) {
PrintAndLog("Could not open file %s",filename);
return 1;
}
hardnested_print_progress(0, "Reading nonces from file nonces.bin...", (float)(1LL<<47), 0);
snprintf(progress_text, 80, "Reading nonces from file %s...",filename);
hardnested_print_progress(0, progress_text, (float)(1LL<<47), 0);
bytes_read = fread(read_buf, 1, 6, fnonces);
if (bytes_read != 6) {
PrintAndLog("File reading error.");
@ -1387,7 +1388,7 @@ static void simulate_acquire_nonces()
}
static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, bool nonce_file_write, bool slow)
static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, bool nonce_file_write, bool slow, char *filename)
{
last_sample_clock = msclock();
sample_period = 2000; // initial rough estimate. Will be refined.
@ -1400,6 +1401,7 @@ static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_
uint32_t total_num_nonces = 0;
float brute_force;
bool reported_suma8 = false;
char progress_text[80];
FILE *fnonces = NULL;
UsbCommand resp;
@ -1432,11 +1434,12 @@ static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_
cuid = resp.arg[1];
if (nonce_file_write && fnonces == NULL) {
if ((fnonces = fopen("nonces.bin","wb")) == NULL) {
PrintAndLog("Could not create file nonces.bin");
if ((fnonces = fopen(filename,"wb")) == NULL) {
PrintAndLog("Could not create file %s", filename);
return 3;
}
hardnested_print_progress(0, "Writing acquired nonces to binary file nonces.bin", (float)(1LL<<47), 0);
snprintf(progress_text, 80, "Writing acquired nonces to binary file %s", filename);
hardnested_print_progress(0, progress_text, (float)(1LL<<47), 0);
num_to_bytes(cuid, 4, write_buf);
fwrite(write_buf, 1, 4, fnonces);
fwrite(&trgBlockNo, 1, 1, fnonces);
@ -2203,7 +2206,7 @@ static void set_test_state(uint8_t byte)
}
int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *trgkey, bool nonce_file_read, bool nonce_file_write, bool slow, int tests, uint64_t *foundkey)
int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *trgkey, bool nonce_file_read, bool nonce_file_write, bool slow, int tests, uint64_t *foundkey, char *filename)
{
char progress_text[80];
@ -2334,7 +2337,7 @@ int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBloc
update_reduction_rate(0.0, true);
if (nonce_file_read) { // use pre-acquired data from file nonces.bin
if (read_nonce_file() != 0) {
if (read_nonce_file(filename) != 0) {
free_bitflip_bitarrays();
free_nonces_memory();
free_bitarray(all_bitflips_bitarray[ODD_STATE]);
@ -2348,7 +2351,7 @@ int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBloc
float brute_force;
shrink_key_space(&brute_force);
} else { // acquire nonces.
uint16_t is_OK = acquire_nonces(blockNo, keyType, key, trgBlockNo, trgKeyType, nonce_file_write, slow);
uint16_t is_OK = acquire_nonces(blockNo, keyType, key, trgBlockNo, trgKeyType, nonce_file_write, slow, filename);
if (is_OK != 0) {
free_bitflip_bitarrays();
free_nonces_memory();

View file

@ -41,7 +41,7 @@ typedef struct noncelist {
noncelistentry_t *first;
} noncelist_t;
extern int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *trgkey, bool nonce_file_read, bool nonce_file_write, bool slow, int tests, uint64_t *foundkey);
extern int mfnestedhard(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *trgkey, bool nonce_file_read, bool nonce_file_write, bool slow, int tests, uint64_t *foundkey, char *filename);
extern void hardnested_print_progress(uint32_t nonces, char *activity, float brute_force, uint64_t min_diff_print_time);
#endif

View file

@ -545,11 +545,15 @@ static int l_hardnested(lua_State *L){
const char *p_tests = luaL_checklstring(L, 10, &size);
if(size != 1) return returnToLuaWithError(L,"Wrong size of tests, got %d bytes, expected 1", (int) size);
char filename[FILE_PATH_SIZE]="nonces.bin";
const char *p_filename = luaL_checklstring(L, 11, &size);
if(size != 0)
strcpy(filename, p_filename);
uint32_t blockNo = 0, keyType = 0;
uint32_t trgBlockNo = 0, trgKeyType = 0;
uint32_t slow = 0, tests = 0;
uint32_t nonce_file_read = 0, nonce_file_write = 0;
sscanf(p_blockno, "%02x", &blockNo);
sscanf(p_keytype, "%x", &keyType);
sscanf(p_trg_blockno, "%02x", &trgBlockNo);
@ -572,7 +576,7 @@ static int l_hardnested(lua_State *L){
}
uint64_t foundkey = 0;
int retval = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, haveTarget ? trgkey : NULL, nonce_file_read, nonce_file_write, slow, tests, &foundkey);
int retval = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, haveTarget ? trgkey : NULL, nonce_file_read, nonce_file_write, slow, tests, &foundkey, filename);
DropField();
//Push the key onto the stack