mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-14 02:26:59 -07:00
Check keys in hf mf nested and hf mf chk (#414)
Improve hf mf chk and hf mf nested * hf mf chk. added interrupt of procedure by usb * extract mifare default keys into separate module * arm side multisector `hf mf chk` * hf mf nested. change key search procedure * hf mf nested. added key check after we have found a key. * small fix hf list f * hf mf chk. add timeout (arm side) and some tweaks.
This commit is contained in:
parent
77aecdd286
commit
275d9e61c2
14 changed files with 375 additions and 218 deletions
|
@ -546,7 +546,7 @@ uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, ui
|
|||
|
||||
if (showWaitCycles && !isResponse && next_record_is_response(tracepos, trace)) {
|
||||
uint32_t next_timestamp = *((uint32_t *)(trace + tracepos));
|
||||
PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
|
||||
PrintAndLog(" %10d | %10d | %s | fdt (Frame Delay Time): %d",
|
||||
(EndOfTransmissionTimestamp - first_timestamp),
|
||||
(next_timestamp - first_timestamp),
|
||||
" ",
|
||||
|
|
265
client/cmdhfmf.c
265
client/cmdhfmf.c
|
@ -524,13 +524,6 @@ int CmdHF14AMfRestore(const char *Cmd)
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint64_t Key[2];
|
||||
int foundKey[2];
|
||||
} sector_t;
|
||||
|
||||
|
||||
# define NESTED_KEY_COUNT 15
|
||||
int CmdHF14AMfNested(const char *Cmd)
|
||||
{
|
||||
|
@ -686,35 +679,12 @@ int CmdHF14AMfNested(const char *Cmd)
|
|||
if (e_sector == NULL) return 1;
|
||||
|
||||
//test current key and additional standard keys first
|
||||
memcpy(keyBlock, key, 6);
|
||||
num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));
|
||||
num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));
|
||||
num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));
|
||||
num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));
|
||||
num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));
|
||||
num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 6 * 6));
|
||||
num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 7 * 6));
|
||||
num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 8 * 6));
|
||||
num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 9 * 6));
|
||||
num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 10 * 6));
|
||||
num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 11 * 6));
|
||||
num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 12 * 6));
|
||||
num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 13 * 6));
|
||||
num_to_bytes(0x1a2b3c4d5e6f, 6, (uint8_t*)(keyBlock + 14 * 6));
|
||||
for (int defaultKeyCounter = 0; defaultKeyCounter < MifareDefaultKeysSize; defaultKeyCounter++){
|
||||
num_to_bytes(MifareDefaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));
|
||||
}
|
||||
|
||||
PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);
|
||||
for (i = 0; i < SectorsCnt; i++) {
|
||||
for (j = 0; j < 2; j++) {
|
||||
if (e_sector[i].foundKey[j]) continue;
|
||||
|
||||
res = mfCheckKeys(FirstBlockOfSector(i), j, true, NESTED_KEY_COUNT, keyBlock, &key64);
|
||||
|
||||
if (!res) {
|
||||
e_sector[i].Key[j] = key64;
|
||||
e_sector[i].foundKey[j] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
mfCheckKeysSec(SectorsCnt, 2, MF_CHKKEYS_DEFTIMEOUT, true, NESTED_KEY_COUNT, keyBlock, e_sector);
|
||||
|
||||
// get known key from array
|
||||
bool keyFound = false;
|
||||
|
@ -772,6 +742,9 @@ int CmdHF14AMfNested(const char *Cmd)
|
|||
PrintAndLog("Found valid key:%012" PRIx64, key64);
|
||||
e_sector[sectorNo].foundKey[trgKeyType] = 1;
|
||||
e_sector[sectorNo].Key[trgKeyType] = key64;
|
||||
|
||||
// try to check this key as a key to the other sectors
|
||||
mfCheckKeysSec(SectorsCnt, 2, MF_CHKKEYS_DEFTIMEOUT, true, 1, keyBlock, e_sector);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -781,66 +754,6 @@ int CmdHF14AMfNested(const char *Cmd)
|
|||
PrintAndLog("\n\n-----------------------------------------------\nNested statistic:\nIterations count: %d", iterations);
|
||||
PrintAndLog("Time in nested: %1.3f (%1.3f sec per key)", ((float)(msclock() - msclock1))/1000.0, ((float)(msclock() - msclock1))/iterations/1000.0);
|
||||
|
||||
// check if we have unrecognized keys
|
||||
bool notFoundKeys = false;
|
||||
for (i = 0; i < SectorsCnt; i++) {
|
||||
for (j = 0; j < 2; j++) {
|
||||
if (!e_sector[i].foundKey[j]) {
|
||||
notFoundKeys = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (notFoundKeys) break;
|
||||
}
|
||||
|
||||
if (notFoundKeys) {
|
||||
PrintAndLog("-----------------------------------------------\n");
|
||||
PrintAndLog("We have unrecognized keys. Trying to check if we have this keys on key buffer...");
|
||||
|
||||
// fill keyBlock with known keys
|
||||
int cnt = 0;
|
||||
for (i = 0; i < SectorsCnt; i++) {
|
||||
for (j = 0; j < 2; j++) {
|
||||
if (e_sector[i].foundKey[j]) {
|
||||
// try to insert key to keyBlock
|
||||
if (cnt < NESTED_KEY_COUNT) {
|
||||
|
||||
// search for dublicates
|
||||
bool dubl = false;
|
||||
for (int v = 0; v < NESTED_KEY_COUNT; v++) {
|
||||
if (e_sector[i].Key[j] == bytes_to_num((uint8_t*)(keyBlock + v * 6), 6)) {
|
||||
dubl = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// insert
|
||||
if (!dubl) {
|
||||
num_to_bytes(e_sector[i].Key[j], 6, (uint8_t*)(keyBlock + cnt * 6));
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try to auth with known keys to not recognized sectors keys
|
||||
PrintAndLog("Testing keys. Sector count=%d known keys count:%d", SectorsCnt, cnt);
|
||||
for (i = 0; i < SectorsCnt; i++) {
|
||||
for (j = 0; j < 2; j++) {
|
||||
if (e_sector[i].foundKey[j]) continue;
|
||||
|
||||
res = mfCheckKeys(FirstBlockOfSector(i), j, true, cnt, keyBlock, &key64);
|
||||
|
||||
if (!res) {
|
||||
e_sector[i].Key[j] = key64;
|
||||
e_sector[i].foundKey[j] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // if (notFoundKeys)
|
||||
|
||||
// print result
|
||||
PrintAndLog("|---|----------------|---|----------------|---|");
|
||||
PrintAndLog("|sec|key A |res|key B |res|");
|
||||
|
@ -1022,14 +935,18 @@ int CmdHF14AMfNestedHard(const char *Cmd)
|
|||
int CmdHF14AMfChk(const char *Cmd)
|
||||
{
|
||||
if (strlen(Cmd)<3) {
|
||||
PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
|
||||
PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d|s|ss] [<key (12 hex symbols)>] [<dic (*.dic)>]");
|
||||
PrintAndLog(" * - all sectors");
|
||||
PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
|
||||
PrintAndLog("d - write keys to binary file\n");
|
||||
PrintAndLog("t - write keys to emulator memory");
|
||||
PrintAndLog("s - slow execute. timeout 1ms");
|
||||
PrintAndLog("ss- very slow execute. timeout 5ms");
|
||||
PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
|
||||
PrintAndLog(" hf mf chk *1 ? t");
|
||||
PrintAndLog(" hf mf chk *1 ? d");
|
||||
PrintAndLog(" hf mf chk *1 ? s");
|
||||
PrintAndLog(" hf mf chk *1 ? dss");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1042,42 +959,28 @@ int CmdHF14AMfChk(const char *Cmd)
|
|||
int i, res;
|
||||
int keycnt = 0;
|
||||
char ctmp = 0x00;
|
||||
char ctmp3[3] = {0x00};
|
||||
uint8_t blockNo = 0;
|
||||
uint8_t SectorsCnt = 1;
|
||||
uint8_t SectorsCnt = 0;
|
||||
uint8_t keyType = 0;
|
||||
uint64_t key64 = 0;
|
||||
uint32_t timeout14a = 0; // timeout in us
|
||||
bool param3InUse = false;
|
||||
|
||||
int transferToEml = 0;
|
||||
int createDumpFile = 0;
|
||||
|
||||
sector_t *e_sector = NULL;
|
||||
|
||||
keyBlock = calloc(stKeyBlock, 6);
|
||||
if (keyBlock == NULL) return 1;
|
||||
|
||||
uint64_t defaultKeys[] =
|
||||
{
|
||||
0xffffffffffff, // Default key (first key used by program if no user defined key)
|
||||
0x000000000000, // Blank key
|
||||
0xa0a1a2a3a4a5, // NFCForum MAD key
|
||||
0xb0b1b2b3b4b5,
|
||||
0xaabbccddeeff,
|
||||
0x4d3a99c351dd,
|
||||
0x1a982c7e459a,
|
||||
0xd3f7d3f7d3f7,
|
||||
0x714c5c886e97,
|
||||
0x587ee5f9350f,
|
||||
0xa0478cc39091,
|
||||
0x533cb6c723f6,
|
||||
0x8fd0a4f256e9
|
||||
};
|
||||
int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);
|
||||
|
||||
for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)
|
||||
{
|
||||
num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));
|
||||
int defaultKeysSize = MifareDefaultKeysSize;
|
||||
for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++){
|
||||
num_to_bytes(MifareDefaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));
|
||||
}
|
||||
|
||||
if (param_getchar(Cmd, 0)=='*') {
|
||||
blockNo = 3;
|
||||
SectorsCnt = ParamCardSizeSectors(param_getchar(Cmd + 1, 0));
|
||||
}
|
||||
else
|
||||
|
@ -1086,10 +989,10 @@ int CmdHF14AMfChk(const char *Cmd)
|
|||
ctmp = param_getchar(Cmd, 1);
|
||||
switch (ctmp) {
|
||||
case 'a': case 'A':
|
||||
keyType = !0;
|
||||
keyType = 0;
|
||||
break;
|
||||
case 'b': case 'B':
|
||||
keyType = !1;
|
||||
keyType = 1;
|
||||
break;
|
||||
case '?':
|
||||
keyType = 2;
|
||||
|
@ -1100,11 +1003,33 @@ int CmdHF14AMfChk(const char *Cmd)
|
|||
return 1;
|
||||
};
|
||||
|
||||
// transfer to emulator & create dump file
|
||||
ctmp = param_getchar(Cmd, 2);
|
||||
if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
|
||||
else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
|
||||
if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
|
||||
if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
|
||||
|
||||
param3InUse = transferToEml | createDumpFile;
|
||||
|
||||
timeout14a = 500; // fast by default
|
||||
// double parameters - ts, ds
|
||||
int clen = param_getlength(Cmd, 2);
|
||||
if (clen == 2 || clen == 3){
|
||||
param_getstr(Cmd, 2, ctmp3);
|
||||
ctmp = ctmp3[1];
|
||||
}
|
||||
//parse
|
||||
if (ctmp == 's' || ctmp == 'S') {
|
||||
timeout14a = 1000; // slow
|
||||
if (!param3InUse && clen == 2 && (ctmp3[1] == 's' || ctmp3[1] == 'S')) {
|
||||
timeout14a = 5000; // very slow
|
||||
}
|
||||
if (param3InUse && clen == 3 && (ctmp3[2] == 's' || ctmp3[2] == 'S')) {
|
||||
timeout14a = 5000; // very slow
|
||||
}
|
||||
param3InUse = true;
|
||||
}
|
||||
|
||||
for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {
|
||||
for (i = param3InUse; param_getchar(Cmd, 2 + i); i++) {
|
||||
if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {
|
||||
if ( stKeyBlock - keycnt < 2) {
|
||||
p = realloc(keyBlock, 6*(stKeyBlock+=10));
|
||||
|
@ -1169,6 +1094,7 @@ int CmdHF14AMfChk(const char *Cmd)
|
|||
}
|
||||
}
|
||||
|
||||
// fill with default keys
|
||||
if (keycnt == 0) {
|
||||
PrintAndLog("No key specified, trying default keys");
|
||||
for (;keycnt < defaultKeysSize; keycnt++)
|
||||
|
@ -1178,47 +1104,84 @@ int CmdHF14AMfChk(const char *Cmd)
|
|||
}
|
||||
|
||||
// initialize storage for found keys
|
||||
bool validKey[2][40];
|
||||
uint8_t foundKey[2][40][6];
|
||||
for (uint16_t t = 0; t < 2; t++) {
|
||||
e_sector = calloc(SectorsCnt, sizeof(sector_t));
|
||||
if (e_sector == NULL) return 1;
|
||||
for (uint8_t keyAB = 0; keyAB < 2; keyAB++) {
|
||||
for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {
|
||||
validKey[t][sectorNo] = false;
|
||||
for (uint16_t i = 0; i < 6; i++) {
|
||||
foundKey[t][sectorNo][i] = 0xff;
|
||||
}
|
||||
e_sector[sectorNo].Key[keyAB] = 0xffffffffffff;
|
||||
e_sector[sectorNo].foundKey[keyAB] = 0;
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
for ( int t = !keyType; t < 2; keyType==2?(t++):(t=2) ) {
|
||||
int b=blockNo;
|
||||
for (int i = 0; i < SectorsCnt; ++i) {
|
||||
PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i, b, t?'B':'A', keycnt);
|
||||
uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;
|
||||
bool foundAKey = false;
|
||||
uint32_t max_keys = keycnt > USB_CMD_DATA_SIZE / 6 ? USB_CMD_DATA_SIZE / 6 : keycnt;
|
||||
if (SectorsCnt) {
|
||||
PrintAndLog("To cancel this operation press the button on the proxmark...");
|
||||
printf("--");
|
||||
for (uint32_t c = 0; c < keycnt; c += max_keys) {
|
||||
|
||||
uint32_t size = keycnt-c > max_keys ? max_keys : keycnt-c;
|
||||
res = mfCheckKeysSec(SectorsCnt, keyType, timeout14a * 1.06 / 100, true, size, &keyBlock[6 * c], e_sector); // timeout is (ms * 106)/10 or us*0.0106
|
||||
|
||||
if (res != 1) {
|
||||
if (!res) {
|
||||
printf("o");
|
||||
foundAKey = true;
|
||||
} else {
|
||||
printf(".");
|
||||
}
|
||||
} else {
|
||||
printf("\n");
|
||||
PrintAndLog("Command execute timeout");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int keyAB = keyType;
|
||||
do {
|
||||
for (uint32_t c = 0; c < keycnt; c+=max_keys) {
|
||||
uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;
|
||||
res = mfCheckKeys(b, t, true, size, &keyBlock[6*c], &key64);
|
||||
|
||||
uint32_t size = keycnt-c > max_keys ? max_keys : keycnt-c;
|
||||
res = mfCheckKeys(blockNo, keyAB & 0x01, true, size, &keyBlock[6 * c], &key64);
|
||||
|
||||
if (res != 1) {
|
||||
if (!res) {
|
||||
PrintAndLog("Found valid key:[%012" PRIx64 "]",key64);
|
||||
num_to_bytes(key64, 6, foundKey[t][i]);
|
||||
validKey[t][i] = true;
|
||||
PrintAndLog("Found valid key:[%d:%c]%012" PRIx64, blockNo, (keyAB & 0x01)?'B':'A', key64);
|
||||
foundAKey = true;
|
||||
}
|
||||
} else {
|
||||
PrintAndLog("Command execute timeout");
|
||||
}
|
||||
}
|
||||
b<127?(b+=4):(b+=16);
|
||||
}
|
||||
} while(--keyAB > 0);
|
||||
}
|
||||
|
||||
|
||||
// print result
|
||||
if (foundAKey) {
|
||||
if (SectorsCnt) {
|
||||
PrintAndLog("");
|
||||
PrintAndLog("|---|----------------|---|----------------|---|");
|
||||
PrintAndLog("|sec|key A |res|key B |res|");
|
||||
PrintAndLog("|---|----------------|---|----------------|---|");
|
||||
for (i = 0; i < SectorsCnt; i++) {
|
||||
PrintAndLog("|%03d| %012" PRIx64 " | %d | %012" PRIx64 " | %d |", i,
|
||||
e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);
|
||||
}
|
||||
PrintAndLog("|---|----------------|---|----------------|---|");
|
||||
}
|
||||
} else {
|
||||
PrintAndLog("");
|
||||
PrintAndLog("No valid keys found.");
|
||||
}
|
||||
|
||||
if (transferToEml) {
|
||||
uint8_t block[16];
|
||||
for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {
|
||||
if (validKey[0][sectorNo] || validKey[1][sectorNo]) {
|
||||
if (e_sector[sectorNo].foundKey[0] || e_sector[sectorNo].foundKey[1]) {
|
||||
mfEmlGetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);
|
||||
for (uint16_t t = 0; t < 2; t++) {
|
||||
if (validKey[t][sectorNo]) {
|
||||
memcpy(block + t*10, foundKey[t][sectorNo], 6);
|
||||
if (e_sector[sectorNo].foundKey[t]) {
|
||||
num_to_bytes(e_sector[sectorNo].Key[t], 6, block + t * 10);
|
||||
}
|
||||
}
|
||||
mfEmlSetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);
|
||||
|
@ -1231,16 +1194,22 @@ int CmdHF14AMfChk(const char *Cmd)
|
|||
FILE *fkeys = fopen("dumpkeys.bin","wb");
|
||||
if (fkeys == NULL) {
|
||||
PrintAndLog("Could not create file dumpkeys.bin");
|
||||
free(e_sector);
|
||||
free(keyBlock);
|
||||
return 1;
|
||||
}
|
||||
for (uint16_t t = 0; t < 2; t++) {
|
||||
fwrite(foundKey[t], 1, 6*SectorsCnt, fkeys);
|
||||
uint8_t mkey[6];
|
||||
for (uint8_t t = 0; t < 2; t++) {
|
||||
for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {
|
||||
num_to_bytes(e_sector[sectorNo].Key[t], 6, mkey);
|
||||
fwrite(mkey, 1, 6, fkeys);
|
||||
}
|
||||
}
|
||||
fclose(fkeys);
|
||||
PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
|
||||
}
|
||||
|
||||
free(e_sector);
|
||||
free(keyBlock);
|
||||
PrintAndLog("");
|
||||
return 0;
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
#ifndef CMDHFMF_H__
|
||||
#define CMDHFMF_H__
|
||||
|
||||
#include "mifaredefault.h"
|
||||
|
||||
extern int CmdHFMF(const char *Cmd);
|
||||
|
||||
extern int CmdHF14AMfDbg(const char* cmd);
|
||||
|
|
|
@ -139,7 +139,7 @@ int getCommand(UsbCommand* response)
|
|||
* @param ms_timeout
|
||||
* @return true if command was returned, otherwise false
|
||||
*/
|
||||
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
|
||||
bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning) {
|
||||
|
||||
UsbCommand resp;
|
||||
|
||||
|
@ -155,7 +155,7 @@ bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeou
|
|||
}
|
||||
}
|
||||
msleep(10); // XXX ugh
|
||||
if (dm_seconds == 200) { // Two seconds elapsed
|
||||
if (dm_seconds == 200 && show_warning) { // Two seconds elapsed
|
||||
PrintAndLog("Waiting for a response from the proxmark...");
|
||||
PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
|
||||
}
|
||||
|
@ -163,9 +163,12 @@ bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeou
|
|||
return false;
|
||||
}
|
||||
|
||||
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
|
||||
return WaitForResponseTimeoutW(cmd, response, ms_timeout, true);
|
||||
}
|
||||
|
||||
bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
|
||||
return WaitForResponseTimeout(cmd,response,-1);
|
||||
return WaitForResponseTimeoutW(cmd, response, -1, true);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
extern void UsbCommandReceived(UsbCommand *UC);
|
||||
extern int CommandReceived(char *Cmd);
|
||||
extern bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning);
|
||||
extern bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout);
|
||||
extern bool WaitForResponse(uint32_t cmd, UsbCommand* response);
|
||||
extern void clearCommandBuffer();
|
||||
|
|
40
client/mifaredefault.h
Normal file
40
client/mifaredefault.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) 2017 Merlok
|
||||
//
|
||||
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||
// the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Mifare default constants
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef MIFAREDEFAULT_H__
|
||||
#define MIFAREDEFAULT_H__
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#define MifareDefaultKeysSize sizeof(MifareDefaultKeys) / sizeof(uint64_t)
|
||||
|
||||
static const uint64_t MifareDefaultKeys[] =
|
||||
{
|
||||
0xffffffffffff, // Default key (first key used by program if no user defined key)
|
||||
0x000000000000, // Blank key
|
||||
0xa0a1a2a3a4a5, // NFCForum MAD key
|
||||
0xb0b1b2b3b4b5,
|
||||
0xaabbccddeeff,
|
||||
0x1a2b3c4d5e6f,
|
||||
0x123456789abc,
|
||||
0x010203040506,
|
||||
0x123456abcdef,
|
||||
0xabcdef123456,
|
||||
0x4d3a99c351dd,
|
||||
0x1a982c7e459a,
|
||||
0xd3f7d3f7d3f7,
|
||||
0x714c5c886e97,
|
||||
0x587ee5f9350f,
|
||||
0xa0478cc39091,
|
||||
0x533cb6c723f6,
|
||||
0x8fd0a4f256e9
|
||||
};
|
||||
|
||||
#endif
|
|
@ -228,17 +228,46 @@ int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t key
|
|||
|
||||
*key = -1;
|
||||
|
||||
UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};
|
||||
UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType & 0xff) << 8)), clear_trace, keycnt}};
|
||||
memcpy(c.d.asBytes, keyBlock, 6 * keycnt);
|
||||
SendCommand(&c);
|
||||
|
||||
UsbCommand resp;
|
||||
if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;
|
||||
if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;
|
||||
if ((resp.arg[0] & 0xff) != 0x01) return 2;
|
||||
*key = bytes_to_num(resp.d.asBytes, 6);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mfCheckKeysSec(uint8_t sectorCnt, uint8_t keyType, uint8_t timeout14a, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, sector_t * e_sector){
|
||||
|
||||
uint8_t keyPtr = 0;
|
||||
|
||||
if (e_sector == NULL)
|
||||
return -1;
|
||||
|
||||
UsbCommand c = {CMD_MIFARE_CHKKEYS, {((sectorCnt & 0xff) | ((keyType & 0xff) << 8)), (clear_trace | 0x02)|((timeout14a & 0xff) << 8), keycnt}};
|
||||
memcpy(c.d.asBytes, keyBlock, 6 * keycnt);
|
||||
SendCommand(&c);
|
||||
|
||||
UsbCommand resp;
|
||||
if (!WaitForResponseTimeoutW(CMD_ACK, &resp, MAX(3000, 1000 + 13 * sectorCnt * keycnt * (keyType == 2 ? 2 : 1)), false)) return 1; // timeout: 13 ms / fail auth
|
||||
if ((resp.arg[0] & 0xff) != 0x01) return 2;
|
||||
|
||||
bool foundAKey = false;
|
||||
for(int sec = 0; sec < sectorCnt; sec++){
|
||||
for(int keyAB = 0; keyAB < 2; keyAB++){
|
||||
keyPtr = *(resp.d.asBytes + keyAB * 40 + sec);
|
||||
if (keyPtr){
|
||||
e_sector[sec].foundKey[keyAB] = true;
|
||||
e_sector[sec].Key[keyAB] = bytes_to_num(keyBlock + (keyPtr - 1) * 6, 6);
|
||||
foundAKey = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return foundAKey ? 0 : 3;
|
||||
}
|
||||
|
||||
// Compare 16 Bits out of cryptostate
|
||||
int Compare16Bits(const void * a, const void * b) {
|
||||
if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Merlok, 2011
|
||||
// Merlok, 2011, 2017
|
||||
// people from mifare@nethemba.com, 2010
|
||||
//
|
||||
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||
|
@ -15,6 +15,11 @@
|
|||
#include <stdbool.h>
|
||||
#include "data.h"
|
||||
|
||||
// defaults
|
||||
// timeout in units. (ms * 106)/10 or us*0.0106
|
||||
// 5 == 500us
|
||||
#define MF_CHKKEYS_DEFTIMEOUT 5
|
||||
|
||||
// mfCSetBlock work flags
|
||||
#define CSETBLOCK_UID 0x01
|
||||
#define CSETBLOCK_WUPC 0x02
|
||||
|
@ -24,11 +29,17 @@
|
|||
#define CSETBLOCK_SINGLE_OPER 0x1F
|
||||
#define CSETBLOCK_MAGIC_1B 0x40
|
||||
|
||||
typedef struct {
|
||||
uint64_t Key[2];
|
||||
int foundKey[2];
|
||||
} sector_t;
|
||||
|
||||
extern char logHexFileName[FILE_PATH_SIZE];
|
||||
|
||||
extern int mfDarkside(uint64_t *key);
|
||||
extern int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *ResultKeys, bool calibrate);
|
||||
extern int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t keycnt, uint8_t *keyBlock, uint64_t *key);
|
||||
extern int mfCheckKeysSec(uint8_t sectorCnt, uint8_t keyType, uint8_t timeout14a, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, sector_t * e_sector);
|
||||
|
||||
extern int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount);
|
||||
extern int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue