#include "mifarehost.h"\r
#include "mifare.h"\r
#include "mfkey.h"\r
+#include "hardnested/hardnested_bf_core.h"\r
\r
#define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up\r
\r
return 0;\r
}\r
\r
-# define NESTED_KEY_COUNT 15\r
+//----------------------------------------------\r
+// Nested\r
+//----------------------------------------------\r
+\r
+static void parseParamTDS(const char *Cmd, const uint8_t indx, bool *paramT, bool *paramD, uint8_t *timeout) {\r
+ char ctmp3[3] = {0};\r
+ int len = param_getlength(Cmd, indx);\r
+ if (len > 0 && len < 4){\r
+ param_getstr(Cmd, indx, ctmp3, sizeof(ctmp3));\r
+ \r
+ *paramT |= (ctmp3[0] == 't' || ctmp3[0] == 'T');\r
+ *paramD |= (ctmp3[0] == 'd' || ctmp3[0] == 'D');\r
+ bool paramS1 = *paramT || *paramD;\r
+\r
+ // slow and very slow\r
+ if (ctmp3[0] == 's' || ctmp3[0] == 'S' || ctmp3[1] == 's' || ctmp3[1] == 'S') {\r
+ *timeout = 11; // slow\r
+ \r
+ if (!paramS1 && (ctmp3[1] == 's' || ctmp3[1] == 'S')) {\r
+ *timeout = 53; // very slow\r
+ }\r
+ if (paramS1 && (ctmp3[2] == 's' || ctmp3[2] == 'S')) {\r
+ *timeout = 53; // very slow\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
int CmdHF14AMfNested(const char *Cmd)\r
{\r
int i, j, res, iterations;\r
uint8_t trgKeyType = 0;\r
uint8_t SectorsCnt = 0;\r
uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
- uint8_t keyBlock[NESTED_KEY_COUNT * 6];\r
+ uint8_t keyBlock[MifareDefaultKeysSize * 6];\r
uint64_t key64 = 0;\r
+ // timeout in units. (ms * 106)/10 or us*0.0106\r
+ uint8_t btimeout14a = MF_CHKKEYS_DEFTIMEOUT; // fast by default\r
\r
bool autosearchKey = false;\r
\r
\r
if (strlen(Cmd)<3) {\r
PrintAndLog("Usage:");\r
- PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");\r
- PrintAndLog(" all sectors autosearch key: hf mf nested <card memory> * [t,d]");\r
+ PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t|d|s|ss]");\r
+ PrintAndLog(" all sectors autosearch key: hf mf nested <card memory> * [t|d|s|ss]");\r
PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r
PrintAndLog(" <target block number> <target key A/B> [t]");\r
PrintAndLog(" ");\r
PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r
PrintAndLog("t - transfer keys to emulator memory");\r
PrintAndLog("d - write keys to binary file dumpkeys.bin");\r
+ PrintAndLog("s - Slow (1ms) check keys (required by some non standard cards)");\r
+ PrintAndLog("ss - Very slow (5ms) check keys");\r
PrintAndLog(" ");\r
PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r
PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r
PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r
PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r
PrintAndLog(" sample5: hf mf nested 1 * t");\r
+ PrintAndLog(" sample6: hf mf nested 1 * ss");\r
return 0;\r
}\r
\r
if (param_getchar(Cmd, 1) == '*') {\r
autosearchKey = true;\r
\r
- ctmp = param_getchar(Cmd, 2);\r
- transferToEml |= (ctmp == 't' || ctmp == 'T');\r
- createDumpFile |= (ctmp == 'd' || ctmp == 'D');\r
+ parseParamTDS(Cmd, 2, &transferToEml, &createDumpFile, &btimeout14a);\r
\r
- PrintAndLog("--nested. sectors:%2d, block no:*, eml:%c, dmp=%c ", SectorsCnt, transferToEml?'y':'n', createDumpFile?'y':'n');\r
+ PrintAndLog("--nested. sectors:%2d, block no:*, eml:%c, dmp=%c checktimeout=%d us", \r
+ SectorsCnt, transferToEml?'y':'n', createDumpFile?'y':'n', ((int)btimeout14a * 10000) / 106);\r
} else {\r
blockNo = param_get8(Cmd, 1);\r
\r
if (ctmp != 'A' && ctmp != 'a')\r
trgKeyType = 1;\r
\r
- ctmp = param_getchar(Cmd, 6);\r
- transferToEml |= (ctmp == 't' || ctmp == 'T');\r
- createDumpFile |= (ctmp == 'd' || ctmp == 'D');\r
+ parseParamTDS(Cmd, 6, &transferToEml, &createDumpFile, &btimeout14a);\r
} else {\r
- ctmp = param_getchar(Cmd, 4);\r
- transferToEml |= (ctmp == 't' || ctmp == 'T');\r
- createDumpFile |= (ctmp == 'd' || ctmp == 'D');\r
+ parseParamTDS(Cmd, 4, &transferToEml, &createDumpFile, &btimeout14a);\r
}\r
\r
- PrintAndLog("--nested. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c ", SectorsCnt, blockNo, keyType?'B':'A', transferToEml?'y':'n', createDumpFile?'y':'n');\r
+ PrintAndLog("--nested. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c checktimeout=%d us", \r
+ SectorsCnt, blockNo, keyType?'B':'A', transferToEml?'y':'n', createDumpFile?'y':'n', ((int)btimeout14a * 10000) / 106);\r
}\r
\r
// one-sector nested\r
}\r
\r
PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r
- mfCheckKeysSec(SectorsCnt, 2, MF_CHKKEYS_DEFTIMEOUT, true, NESTED_KEY_COUNT, keyBlock, e_sector);\r
+ mfCheckKeysSec(SectorsCnt, 2, btimeout14a, true, MifareDefaultKeysSize, keyBlock, e_sector);\r
\r
// get known key from array\r
bool keyFound = false;\r
e_sector[sectorNo].Key[trgKeyType] = key64;\r
\r
// try to check this key as a key to the other sectors\r
- mfCheckKeysSec(SectorsCnt, 2, MF_CHKKEYS_DEFTIMEOUT, true, 1, keyBlock, e_sector);\r
+ mfCheckKeysSec(SectorsCnt, 2, btimeout14a, true, 1, keyBlock, e_sector);\r
}\r
}\r
}\r
PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");\r
PrintAndLog(" s: Slower acquisition (required by some non standard cards)");\r
PrintAndLog(" r: Read nonces.bin and start attack");\r
+ PrintAndLog(" iX: set type of SIMD instructions. Without this flag programs autodetect it.");\r
+ PrintAndLog(" i5: AVX512");\r
+ PrintAndLog(" i2: AVX2");\r
+ PrintAndLog(" ia: AVX");\r
+ PrintAndLog(" is: SSE2");\r
+ PrintAndLog(" im: MMX");\r
+ PrintAndLog(" in: none (use CPU regular instruction set)");\r
PrintAndLog(" ");\r
PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");\r
PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");\r
int tests = 0;\r
\r
\r
+ uint16_t iindx = 0;\r
if (ctmp == 'R' || ctmp == 'r') {\r
nonce_file_read = true;\r
+ iindx = 1;\r
if (!param_gethex(Cmd, 1, trgkey, 12)) {\r
know_target_key = true;\r
+ iindx = 2;\r
}\r
} else if (ctmp == 'T' || ctmp == 't') {\r
tests = param_get32ex(Cmd, 1, 100, 10);\r
+ iindx = 2;\r
if (!param_gethex(Cmd, 2, trgkey, 12)) {\r
know_target_key = true;\r
+ iindx = 3;\r
}\r
} else {\r
blockNo = param_get8(Cmd, 0);\r
know_target_key = true;\r
i++;\r
}\r
+ iindx = i;\r
\r
while ((ctmp = param_getchar(Cmd, i))) {\r
if (ctmp == 's' || ctmp == 'S') {\r
slow = true;\r
} else if (ctmp == 'w' || ctmp == 'W') {\r
nonce_file_write = true;\r
+ } else if (param_getlength(Cmd, i) == 2 && ctmp == 'i') {\r
+ iindx = i;\r
} else {\r
- PrintAndLog("Possible options are w and/or s");\r
+ PrintAndLog("Possible options are w , s and/or iX");\r
return 1;\r
}\r
i++;\r
}\r
}\r
+ \r
+ SetSIMDInstr(SIMD_AUTO);\r
+ if (iindx > 0) {\r
+ while ((ctmp = param_getchar(Cmd, iindx))) {\r
+ if (param_getlength(Cmd, iindx) == 2 && ctmp == 'i') {\r
+ switch(param_getchar_indx(Cmd, 1, iindx)) {\r
+ case '5':\r
+ SetSIMDInstr(SIMD_AVX512);\r
+ break;\r
+ case '2':\r
+ SetSIMDInstr(SIMD_AVX2);\r
+ break;\r
+ case 'a':\r
+ SetSIMDInstr(SIMD_AVX);\r
+ break;\r
+ case 's':\r
+ SetSIMDInstr(SIMD_SSE2);\r
+ break;\r
+ case 'm':\r
+ SetSIMDInstr(SIMD_MMX);\r
+ break;\r
+ case 'n':\r
+ SetSIMDInstr(SIMD_NONE);\r
+ break;\r
+ default:\r
+ PrintAndLog("Unknown SIMD type. %c", param_getchar_indx(Cmd, 1, iindx));\r
+ return 1;\r
+ }\r
+ }\r
+ iindx++;\r
+ } \r
+ }\r
\r
PrintAndLog("--target block no:%3d, target key type:%c, known target key: 0x%02x%02x%02x%02x%02x%02x%s, file action: %s, Slow: %s, Tests: %d ",\r
trgBlockNo,\r
int i, res;\r
int keycnt = 0;\r
char ctmp = 0x00;\r
+ int clen = 0;\r
char ctmp3[3] = {0x00};\r
uint8_t blockNo = 0;\r
uint8_t SectorsCnt = 0;\r
blockNo = param_get8(Cmd, 0);\r
\r
ctmp = param_getchar(Cmd, 1);\r
- switch (ctmp) {\r
- case 'a': case 'A':\r
- keyType = 0;\r
- break;\r
- case 'b': case 'B':\r
- keyType = 1;\r
- break;\r
- case '?':\r
- keyType = 2;\r
- break;\r
- default:\r
- PrintAndLog("Key type must be A , B or ?");\r
- free(keyBlock);\r
- return 1;\r
- };\r
+ clen = param_getlength(Cmd, 1);\r
+ if (clen == 1) {\r
+ switch (ctmp) {\r
+ case 'a': case 'A':\r
+ keyType = 0;\r
+ break;\r
+ case 'b': case 'B':\r
+ keyType = 1;\r
+ break;\r
+ case '?':\r
+ keyType = 2;\r
+ break;\r
+ default:\r
+ PrintAndLog("Key type must be A , B or ?");\r
+ free(keyBlock);\r
+ return 1;\r
+ };\r
+ }\r
\r
// transfer to emulator & create dump file\r
ctmp = param_getchar(Cmd, 2);\r
- if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r
- if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r
+ clen = param_getlength(Cmd, 2);\r
+ if (clen == 1 && (ctmp == 't' || ctmp == 'T')) transferToEml = 1;\r
+ if (clen == 1 && (ctmp == 'd' || ctmp == 'D')) createDumpFile = 1;\r
\r
param3InUse = transferToEml | createDumpFile;\r
\r
timeout14a = 500; // fast by default\r
// double parameters - ts, ds\r
- int clen = param_getlength(Cmd, 2);\r
+ clen = param_getlength(Cmd, 2);\r
if (clen == 2 || clen == 3){\r
- param_getstr(Cmd, 2, ctmp3);\r
+ param_getstr(Cmd, 2, ctmp3, sizeof(ctmp3));\r
ctmp = ctmp3[1];\r
}\r
//parse\r
keycnt++;\r
} else {\r
// May be a dic file\r
- if ( param_getstr(Cmd, 2 + i,filename) >= FILE_PATH_SIZE ) {\r
+ if ( param_getstr(Cmd, 2 + i, filename, sizeof(filename)) >= FILE_PATH_SIZE ) {\r
PrintAndLog("File name too long");\r
free(keyBlock);\r
return 2;\r
\r
if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r
\r
- if (!isxdigit(buf[0])){\r
+ if (!isxdigit((unsigned char)buf[0])){\r
PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r
continue;\r
}\r
break;\r
case 'f':\r
case 'F':\r
- len = param_getstr(Cmd, cmdp+1, filename);\r
+ len = param_getstr(Cmd, cmdp+1, filename, sizeof(filename));\r
if (len < 1) {\r
PrintAndLog("error no filename found");\r
return 0;\r
}\r
}\r
\r
- len = param_getstr(Cmd,nameParamNo,filename);\r
+ len = param_getstr(Cmd,nameParamNo,filename,sizeof(filename));\r
\r
if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
\r
}\r
}\r
\r
- len = param_getstr(Cmd,nameParamNo,filename);\r
+ len = param_getstr(Cmd,nameParamNo,filename,sizeof(filename));\r
\r
if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
\r
bool fillCard = false;\r
\r
if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
- PrintAndLog("Usage: hf mf cwipe [card size] [w] [p]");\r
- PrintAndLog("sample: hf mf cwipe 1 w s");\r
+ PrintAndLog("Usage: hf mf cwipe [card size] [w] [f]");\r
+ PrintAndLog("sample: hf mf cwipe 1 w f");\r
PrintAndLog("[card size]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
PrintAndLog("w - Wipe magic Chinese card (only works with gen:1a cards)");\r
PrintAndLog("f - Fill the card with default data and keys (works with gen:1a and gen:1b cards only)");\r
}\r
return 0;\r
} else {\r
- param_getstr(Cmd, 0, filename);\r
+ param_getstr(Cmd, 0, filename, sizeof(filename));\r
\r
len = strlen(filename);\r
if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
}\r
return 0;\r
} else {\r
- param_getstr(Cmd, 0, filename);\r
+ param_getstr(Cmd, 0, filename, sizeof(filename));\r
\r
len = strlen(filename);\r
if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
}\r
\r
UsbCommand resp;\r
- if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
+ if (WaitForResponseTimeoutW(CMD_ACK, &resp, 2000, false)) {\r
res = resp.arg[0] & 0xff;\r
uint16_t traceLen = resp.arg[1];\r
len = resp.arg[2];\r
\r
if (res == 0) { // we are done\r
- free(buf);\r
- return 0;\r
+ break;\r
}\r
\r
if (res == 1) { // there is (more) data to be transferred\r
} // while (true)\r
\r
free(buf);\r
+ \r
+ msleep(300); // wait for exiting arm side.\r
+ PrintAndLog("Done.");\r
return 0;\r
}\r
\r