X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/07b9e989701955834468e3ce8ce200ee3c2a1013..88f503735c85c716cc14af088707f1cdf76155de:/client/cmdhfmf.c?ds=sidebyside

diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c
index ec542eb3..6ddf845a 100644
--- a/client/cmdhfmf.c
+++ b/client/cmdhfmf.c
@@ -125,6 +125,21 @@ int usage_hf14_chk(void){
 	PrintAndLog("      hf mf chk *1 ? d                        -- target all blocks, all keys, 1K, write to file");
 	return 0;
 }
+int usage_hf14_keybrute(void){
+	PrintAndLog("J_Run's 2nd phase of multiple sector nested authentication key recovery");
+	PrintAndLog("You have a known 4 last bytes of a key recovered with mf_nonce_brute tool.");
+	PrintAndLog("First 2 bytes of key will be bruteforced");
+	PrintAndLog("");
+	PrintAndLog("Usage:  hf mf keybrute [h] <block number> <A|B> <key>");
+	PrintAndLog("options:");
+	PrintAndLog("      h               this help");
+	PrintAndLog("      <block number>  target block number");
+	PrintAndLog("      <A|B>           target key type");
+	PrintAndLog("      <key>           candidate key from mf_nonce_brute tool");
+	PrintAndLog("samples:");
+	PrintAndLog("           hf mf keybrute 1 A 000011223344");
+	return 0;
+}
 
 int CmdHF14AMifare(const char *Cmd) {
 	uint32_t uid = 0;
@@ -198,9 +213,16 @@ start:
 	}	
 	printf("\n");
 	
-	// par == 0
-	if (isOK == -1 && par_list == 0) {
-		if (!nonce2key_ex(uid, nt, nr, ks_list, &r_key) ){
+	// par == 0,  and -4
+	if (isOK == -4 && par_list == 0) {
+		// this special attack when parities is zero, uses checkkeys. Which now with block/keytype option also needs. 
+		// but it uses 0|1 instead of 0x60|0x61...
+		if (nonce2key_ex(blockNo, keytype - 0x60 , uid, nt, nr, ks_list, &r_key) ){
+			PrintAndLog("Key not found (lfsr_common_prefix list is null).");	
+			PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
+			c.arg[0] = false;
+			goto start;
+		} else {
 			PrintAndLog("Found valid key: %012"llx" \n", r_key);
 			goto END;
 		}
@@ -217,6 +239,16 @@ start:
 		c.arg[0] = false;
 		goto start;
 	} else {
+		
+		// nonce2key found a candidate key.  Lets verify it.
+		uint8_t keyblock[] = {0,0,0,0,0,0};
+		num_to_bytes(r_key, 6, keyblock);
+		uint64_t key64 = 0;
+		int res = mfCheckKeys(blockNo, keytype - 0x60 , false, 1, keyblock, &key64);
+		if ( res > 0 ) {
+			PrintAndLog("Candidate Key found (%012"llx") - Test authentication failed. Starting over darkside attack", r_key);	
+			goto start;
+		}
 		PrintAndLog("Found valid key: %012"llx" \n", r_key);
 	}
 END:
@@ -1018,7 +1050,7 @@ int CmdHF14AMfNestedHard(const char *Cmd) {
 			slow ? "Yes" : "No",
 			tests);
 
-	int16_t isOK = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, know_target_key?trgkey:NULL, nonce_file_read, nonce_file_write, slow, tests);
+	int16_t isOK = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, know_target_key ? trgkey : NULL, nonce_file_read, nonce_file_write, slow, tests);
 
 	if (isOK) {
 		switch (isOK) {
@@ -1639,6 +1671,43 @@ int CmdHF14AMfDbg(const char *Cmd) {
 	return 0;
 }
 
+int CmdHF14AMfKeyBrute(const char *Cmd) {
+
+	uint8_t blockNo = 0, keytype = 0;
+	uint8_t key[6] = {0, 0, 0, 0, 0, 0};
+	uint64_t foundkey = 0;
+	
+	char cmdp = param_getchar(Cmd, 0);	
+	if ( cmdp == 'H' || cmdp == 'h') return usage_hf14_keybrute();
+	
+	// block number
+	blockNo = param_get8(Cmd, 0);	 
+	
+	// keytype
+	cmdp = param_getchar(Cmd, 1);
+	if (cmdp == 'B' || cmdp == 'b') keytype = 1;
+	
+	// key
+	if (param_gethex(Cmd, 2, key, 12)) return usage_hf14_keybrute();
+	
+	clock_t t1 = clock();
+	time_t start, end;
+	time(&start);
+	
+	if (mfKeyBrute( blockNo, keytype, key, &foundkey))
+		PrintAndLog("Found valid key: %012"llx" \n", foundkey);
+	else
+		PrintAndLog("Key not found");
+	
+	t1 = clock() - t1;
+	time(&end);
+	unsigned long elapsed_time = difftime(end, start);	
+	if ( t1 > 0 )
+		PrintAndLog("\nTime in keybrute: %.0f ticks %u seconds\n", (float)t1, elapsed_time);
+	
+	return 0;	
+}
+
 void printKeyTable( uint8_t sectorscnt, sector *e_sector ){
 	PrintAndLog("|---|----------------|---|----------------|---|");
 	PrintAndLog("|sec|key A           |res|key B           |res|");
@@ -2379,10 +2448,11 @@ static command_t CommandTable[] = {
 	{"dump",		CmdHF14AMfDump,			0, "Dump MIFARE classic tag to binary file"},
 	{"restore",		CmdHF14AMfRestore,		0, "Restore MIFARE classic binary file to BLANK tag"},
 	{"wrbl",		CmdHF14AMfWrBl,			0, "Write MIFARE classic block"},
-	{"chk",			CmdHF14AMfChk,			0, "Test block keys"},
-	{"mifare",		CmdHF14AMifare,			0, "Read parity error messages."},
-	{"nested",		CmdHF14AMfNested,		0, "Test nested authentication"},
+	{"chk",			CmdHF14AMfChk,			0, "Check keys"},
+	{"mifare",		CmdHF14AMifare,			0, "Darkside attack. read parity error messages."},
+	{"nested",		CmdHF14AMfNested,		0, "Nested attack. Test nested authentication"},
 	{"hardnested", 	CmdHF14AMfNestedHard, 	0, "Nested attack for hardened Mifare cards"},
+	{"keybrute",	CmdHF14AMfKeyBrute,		0, "J_Run's 2nd phase of multiple sector nested authentication key recovery"},
 	{"sniff",		CmdHF14AMfSniff,		0, "Sniff card-reader communication"},
 	{"sim",			CmdHF14AMf1kSim,		0, "Simulate MIFARE card"},
 	{"eclr",		CmdHF14AMfEClear,		0, "Clear simulator memory block"},