#include <stdio.h>\r
#include <stdlib.h>\r
#include <ctype.h>\r
-#include "proxmark3.h"\r
+#include "comms.h"\r
#include "cmdmain.h"\r
#include "cmdhfmfhard.h"\r
+#include "parity.h"\r
#include "util.h"\r
#include "util_posix.h"\r
#include "usb_cmd.h"\r
#include "mifare.h"\r
#include "mfkey.h"\r
#include "hardnested/hardnested_bf_core.h"\r
+#include "cliparser/cliparser.h"\r
+#include "cmdhf14a.h"\r
+#include "mifare4.h"\r
\r
#define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up\r
\r
blockNo = i * 4;\r
keyType = j;\r
num_to_bytes(e_sector[i].Key[j], 6, key);\r
- \r
keyFound = true;\r
break;\r
}\r
// Can't found a key....\r
if (!keyFound) {\r
PrintAndLog("Can't found any of the known keys.");\r
+ free(e_sector);\r
return 4;\r
}\r
PrintAndLog("--auto key. block no:%3d, key type:%c key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r
\r
// initialize storage for found keys\r
e_sector = calloc(SectorsCnt, sizeof(sector_t));\r
- if (e_sector == NULL) return 1;\r
+ if (e_sector == NULL) {\r
+ free(keyBlock);\r
+ return 1;\r
+ }\r
for (uint8_t keyAB = 0; keyAB < 2; keyAB++) {\r
for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
e_sector[sectorNo].Key[keyAB] = 0xffffffffffff;\r
//var\r
int res = 0;\r
int len = 0;\r
+ int parlen = 0;\r
int blockLen = 0;\r
int pckNum = 0;\r
int num = 0;\r
uint8_t *buf = NULL;\r
uint16_t bufsize = 0;\r
uint8_t *bufPtr = NULL;\r
+ uint8_t parity[16];\r
\r
char ctmp = param_getchar(Cmd, 0);\r
if ( ctmp == 'h' || ctmp == 'H' ) {\r
} else {\r
isTag = false;\r
}\r
+ parlen = (len - 1) / 8 + 1;\r
bufPtr += 2;\r
if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {\r
memcpy(uid, bufPtr + 2, 7);\r
if (wantDecrypt)\r
mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r
} else {\r
- PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
+ oddparitybuf(bufPtr, len, parity);\r
+ PrintAndLog("%s(%d):%s [%s] c[%s]%c", \r
+ isTag ? "TAG":"RDR", \r
+ num, \r
+ sprint_hex(bufPtr, len), \r
+ printBitsPar(bufPtr + len, len), \r
+ printBitsPar(parity, len),\r
+ memcmp(bufPtr + len, parity, len / 8 + 1) ? '!' : ' ');\r
if (wantLogToFile)\r
AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
if (wantDecrypt)\r
- mfTraceDecode(bufPtr, len, wantSaveToEmlFile);\r
+ mfTraceDecode(bufPtr, len, bufPtr[len], wantSaveToEmlFile);\r
num++;\r
}\r
bufPtr += len;\r
- bufPtr += ((len-1)/8+1); // ignore parity\r
+ bufPtr += parlen; // ignore parity\r
}\r
pckNum = 0;\r
}\r
return tryDecryptWord(param_get32ex(Cmd,0,0,16),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16),data,len/2);\r
}\r
\r
+int CmdHF14AMfAuth4(const char *cmd) {\r
+ uint8_t keyn[20] = {0};\r
+ int keynlen = 0;\r
+ uint8_t key[16] = {0};\r
+ int keylen = 0;\r
+\r
+ CLIParserInit("hf mf auth4", \r
+ "Executes AES authentication command in ISO14443-4", \r
+ "Usage:\n\thf mf auth4 4000 000102030405060708090a0b0c0d0e0f -> executes authentication\n"\r
+ "\thf mf auth4 9003 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> executes authentication\n");\r
+\r
+ void* argtable[] = {\r
+ arg_param_begin,\r
+ arg_str1(NULL, NULL, "<Key Num (HEX 2 bytes)>", NULL),\r
+ arg_str1(NULL, NULL, "<Key Value (HEX 16 bytes)>", NULL),\r
+ arg_param_end\r
+ };\r
+ CLIExecWithReturn(cmd, argtable, true);\r
+ \r
+ CLIGetHexWithReturn(1, keyn, &keynlen);\r
+ CLIGetHexWithReturn(2, key, &keylen);\r
+ CLIParserFree();\r
+ \r
+ if (keynlen != 2) {\r
+ PrintAndLog("ERROR: <Key Num> must be 2 bytes long instead of: %d", keynlen);\r
+ return 1;\r
+ }\r
+ \r
+ if (keylen != 16) {\r
+ PrintAndLog("ERROR: <Key Value> must be 16 bytes long instead of: %d", keylen);\r
+ return 1;\r
+ }\r
+\r
+ return MifareAuth4(NULL, keyn, key, true, false, true);\r
+}\r
+\r
static command_t CommandTable[] =\r
{\r
{"help", CmdHelp, 1, "This help"},\r
{"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r
{"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r
{"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r
+ {"auth4", CmdHF14AMfAuth4, 0, "ISO14443-4 AES authentication"},\r
{"chk", CmdHF14AMfChk, 0, "Test block keys"},\r
{"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r
{"hardnested", CmdHF14AMfNestedHard, 0, "Nested attack for hardened Mifare cards"},\r
\r
int CmdHFMF(const char *Cmd)\r
{\r
- // flush\r
- WaitForResponseTimeout(CMD_ACK,NULL,100);\r
-\r
- CmdsParse(CommandTable, Cmd);\r
- return 0;\r
+ (void)WaitForResponseTimeout(CMD_ACK,NULL,100);\r
+ CmdsParse(CommandTable, Cmd);\r
+ return 0;\r
}\r
\r
int CmdHelp(const char *Cmd)\r