counterfeit ULC: need to merge nonce collection in auth logic for proper timings
This commit is contained in:
8 files changed
+138
-193
No files matched your search
@@ -2049,10 +2049,6 @@ static void PacketReceived(PacketCommandNG *packet) {
|
||||
MifareAcquireNonces(packet->oldarg[0], packet->oldarg[2]);
|
||||
break;
|
||||
}
|
||||
case CMD_HF_MIFAREU3P_ACQ_ENCRYPTED_NONCES: {
|
||||
MifareU3PAcquireEncryptedNonces(packet->oldarg[0], packet->oldarg[1]);
|
||||
break;
|
||||
}
|
||||
case CMD_HF_MIFARE_NESTED: {
|
||||
struct p {
|
||||
uint8_t block;
|
||||
|
||||
+54
-92
@@ -287,11 +287,12 @@ static int MifareUFastRead0(void) {
|
||||
// 1 = failed auth
|
||||
// 0 = correct
|
||||
|
||||
static uint8_t chkKey3Pass(uint8_t keyno, uint8_t *keybytes, uint32_t *auths, bool use_schann, bool check_answer, bool use_fastread0) {
|
||||
static uint8_t chkKey3Pass(uint8_t keyno, uint8_t *keybytes, bool use_schann, bool try_auth, bool check_answer, bool use_fastread0, uint8_t *nonce) {
|
||||
|
||||
uint8_t i = 0, res = 2;
|
||||
bool selected = false;
|
||||
while (i < 5) {
|
||||
|
||||
while (i < 5 && selected == false) {
|
||||
if (use_fastread0) {
|
||||
if (MifareUFastRead0() == 0) {
|
||||
++i;
|
||||
@@ -304,87 +305,21 @@ static uint8_t chkKey3Pass(uint8_t keyno, uint8_t *keybytes, uint32_t *auths, bo
|
||||
}
|
||||
}
|
||||
selected = true;
|
||||
if (g_dbglevel >= DBG_EXTENDED) Dbhexdump(MIFAREU3P_KEY_SIZE, keybytes, false);
|
||||
if (keyno == MIFAREULC_KEY_INDEX) {
|
||||
res = mifare_ultra_3des_auth(keybytes, check_answer) == 1 ? 0 : 1; // 0 = correct, 1 = failed auth
|
||||
} else {
|
||||
res = mifare_ultra_aes_auth(keyno, keybytes, use_schann, check_answer) == 1 ? 0 : 1; // 0 = correct, 1 = failed auth
|
||||
}
|
||||
(*auths)++;
|
||||
break;
|
||||
}
|
||||
if (selected == false) {
|
||||
Dbprintf("chkKey: Failed at fast selecting the card!");
|
||||
res = 4;
|
||||
return 4;
|
||||
}
|
||||
|
||||
if (g_dbglevel >= DBG_EXTENDED) Dbhexdump(MIFAREU3P_KEY_SIZE, keybytes, false);
|
||||
if (keyno == MIFAREULC_KEY_INDEX) {
|
||||
res = mifare_ultra_3des_auth(keybytes, try_auth, check_answer, nonce) == 1 ? 0 : 1; // 0 = correct, 1 = failed auth
|
||||
} else {
|
||||
res = mifare_ultra_aes_auth(keyno, keybytes, use_schann, try_auth, check_answer, nonce) == 1 ? 0 : 1; // 0 = correct, 1 = failed auth
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// acquire encrypted nonces of Ultralight C or Ultralight AES
|
||||
//-----------------------------------------------------------------------------
|
||||
void MifareU3PAcquireEncryptedNonces(uint8_t keyno, uint8_t num_nonces_to_acquire) {
|
||||
uint8_t i = 0;
|
||||
int res = PM3_SUCCESS;
|
||||
bool selected = false;
|
||||
// Buf first byte is nr of nonces in the buffer, uint8_t
|
||||
uint8_t buf[PM3_CMD_DATA_SIZE] = {0x00};
|
||||
uint8_t nonce_size = keyno == MIFAREULC_KEY_INDEX ? 8 : 16;
|
||||
uint8_t max_nonces_per_response = (PM3_CMD_DATA_SIZE - sizeof(uint8_t)) / nonce_size;
|
||||
uint8_t acquired_nonces = 0;
|
||||
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
|
||||
clear_trace();
|
||||
set_tracing(false);
|
||||
set_tracing(true);
|
||||
LED_A_ON();
|
||||
LED_C_ON();
|
||||
if (num_nonces_to_acquire > max_nonces_per_response) {
|
||||
Dbprintf("Limiting number of nonces to acquire to %u per response", max_nonces_per_response);
|
||||
num_nonces_to_acquire = max_nonces_per_response;
|
||||
}
|
||||
for (acquired_nonces = 0; acquired_nonces < num_nonces_to_acquire; acquired_nonces++) {
|
||||
|
||||
// Needed for USCUID-UL
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
SpinDelayUsPrecision(500);
|
||||
|
||||
selected = false;
|
||||
while (i < 5 && selected == false) {
|
||||
if (MifareUFastRead0() == 0) {
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
selected = true;
|
||||
}
|
||||
if (selected == false) {
|
||||
Dbprintf("acquire nonces: Failed at fast selecting the card!");
|
||||
res = PM3_ESOFT;
|
||||
break;
|
||||
}
|
||||
uint16_t len = 0;
|
||||
uint8_t resp[19] = {0x00};
|
||||
if (keyno == MIFAREULC_KEY_INDEX) {
|
||||
len = mifare_sendcmd_short(NULL, CRYPT_NONE, MIFARE_ULC_AUTH_1, 0x00, resp, sizeof(resp), NULL, NULL);
|
||||
if (len != 1 + nonce_size + 2) {
|
||||
res = PM3_ESOFT;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
len = mifare_sendcmd_short(NULL, CRYPT_NONE, MIFARE_ULAES_AUTH_1, keyno, resp, sizeof(resp), NULL, NULL);
|
||||
if (len != 1 + nonce_size + 2) {
|
||||
res = PM3_ESOFT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
memcpy(buf + 1 + acquired_nonces * nonce_size, resp + 1, nonce_size);
|
||||
}
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
LED_B_ON();
|
||||
buf[0] = acquired_nonces;
|
||||
reply_ng(CMD_HF_MIFAREU3P_ACQ_ENCRYPTED_NONCES, res, buf, sizeof(uint8_t) + acquired_nonces * nonce_size);
|
||||
LEDsoff();
|
||||
}
|
||||
|
||||
void MifareU3PassAuth(mful_3passauth_t *packet) {
|
||||
|
||||
LED_A_ON();
|
||||
@@ -392,9 +327,21 @@ void MifareU3PassAuth(mful_3passauth_t *packet) {
|
||||
LED_C_OFF();
|
||||
uint32_t auths = 0;
|
||||
int res = PM3_ESOFT;
|
||||
const uint8_t nonce_size = packet->keyno == MIFAREULC_KEY_INDEX ? 8 : 16;
|
||||
const uint16_t nonce_buf_size = PM3_CMD_DATA_SIZE - sizeof(uint32_t) * 2;
|
||||
const uint8_t max_nonces_per_response = packet->get_nonces ? nonce_buf_size / nonce_size : 0;
|
||||
|
||||
if (max_nonces_per_response > 0) {
|
||||
if (1 + packet->retries > max_nonces_per_response) {
|
||||
Dbprintf("Limiting number of nonces to acquire to %u", max_nonces_per_response);
|
||||
packet->retries = max_nonces_per_response - 1;
|
||||
}
|
||||
}
|
||||
|
||||
struct rp {
|
||||
uint32_t auths;
|
||||
uint32_t ticks;
|
||||
uint8_t nonces[PM3_CMD_DATA_SIZE - sizeof(uint32_t) * 2]; // nonce_buf_size
|
||||
} PACKED rpayload;
|
||||
|
||||
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
|
||||
@@ -406,21 +353,34 @@ void MifareU3PassAuth(mful_3passauth_t *packet) {
|
||||
|
||||
for (uint16_t r = 0; r < 1 + packet->retries; r++) {
|
||||
WDT_HIT();
|
||||
if (chkKey3Pass(packet->keyno, packet->key, &auths, packet->use_schann, packet->check_answer, packet->use_fastread0) == 0) {
|
||||
res = PM3_SUCCESS;
|
||||
goto out;
|
||||
if (packet->reset_field) {
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
SpinDelayUsPrecision(500);
|
||||
}
|
||||
if (chkKey3Pass(packet->keyno, packet->key, packet->use_schann, packet->try_auth, packet->check_answer, packet->use_fastread0, packet->get_nonces ? (rpayload.nonces + auths * nonce_size) : NULL) == 0) {
|
||||
if (packet->try_auth) {
|
||||
auths++;
|
||||
res = PM3_SUCCESS;
|
||||
goto out;
|
||||
}
|
||||
} else {
|
||||
if (!packet->try_auth) {
|
||||
res = PM3_ESOFT;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
auths++;
|
||||
}
|
||||
|
||||
out:
|
||||
rpayload.auths = auths;
|
||||
rpayload.ticks = GetTickCountDelta(ti);
|
||||
if (g_dbglevel >= DBG_ERROR) {
|
||||
if (res != PM3_SUCCESS) {
|
||||
if (res != PM3_SUCCESS && packet->try_auth) {
|
||||
Dbprintf("Authentication failed");
|
||||
}
|
||||
}
|
||||
reply_ng(CMD_HF_MIFAREU3P_AUTH, res, (uint8_t *)&rpayload, sizeof(rpayload));
|
||||
reply_ng(CMD_HF_MIFAREU3P_AUTH, res, (uint8_t *)&rpayload, packet->get_nonces ? sizeof(uint32_t) * 2 + nonce_size * auths : sizeof(uint32_t) * 2);
|
||||
if (packet->turn_off_field) {
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
LEDsoff();
|
||||
@@ -504,12 +464,14 @@ void MifareU3PassChkKeys(mful_3passchk_t *packet) {
|
||||
memcpy(fullkeybytes + (packet->segment * keysize), packet->data + (i * keysize), keysize);
|
||||
}
|
||||
}
|
||||
if (chkKey3Pass(packet->key_index, fullkeybytes, &auths, false, packet->check_answer, packet->use_fastread0) == 0) {
|
||||
if (chkKey3Pass(packet->key_index, fullkeybytes, false, true, packet->check_answer, packet->use_fastread0, NULL) == 0) {
|
||||
auths++;
|
||||
foundkeys++;
|
||||
memcpy(rpayload.key, fullkeybytes, MIFAREU3P_KEY_SIZE);
|
||||
res = PM3_SUCCESS;
|
||||
goto out;
|
||||
}
|
||||
auths++;
|
||||
}
|
||||
out:
|
||||
rpayload.auths = auths;
|
||||
@@ -550,7 +512,7 @@ void MifareUReadBlock(mful_readblock_t *packet) {
|
||||
|
||||
// UL-C authentication
|
||||
if (useCKey) {
|
||||
if (mifare_ultra_3des_auth(packet->key, true) == 0) {
|
||||
if (mifare_ultra_3des_auth(packet->key, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_READBL, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
@@ -558,7 +520,7 @@ void MifareUReadBlock(mful_readblock_t *packet) {
|
||||
|
||||
// UL-AES authentication, hardcode to use keyno 0
|
||||
if (useAESKey) {
|
||||
if (mifare_ultra_aes_auth(0, packet->key, packet->use_schann, true) == 0) {
|
||||
if (mifare_ultra_aes_auth(0, packet->key, packet->use_schann, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_READBL, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
@@ -631,7 +593,7 @@ void MifareUReadCard(mful_readblock_t *packet) {
|
||||
|
||||
// UL-C authentication
|
||||
if (useCKey) {
|
||||
if (mifare_ultra_3des_auth(packet->key, true) == 0) {
|
||||
if (mifare_ultra_3des_auth(packet->key, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_READCARD, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
@@ -639,7 +601,7 @@ void MifareUReadCard(mful_readblock_t *packet) {
|
||||
|
||||
// UL-AES authentication
|
||||
if (useAESKey) {
|
||||
if (mifare_ultra_aes_auth(0, packet->key, schann, true) == 0) {
|
||||
if (mifare_ultra_aes_auth(0, packet->key, schann, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_READCARD, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
@@ -817,7 +779,7 @@ static void MifareUWriteBlockEx(mful_writeblock_t *packet, bool reply) {
|
||||
|
||||
// UL-C authentication
|
||||
if (useCKey) {
|
||||
if (mifare_ultra_3des_auth(packet->key, true) == 0) {
|
||||
if (mifare_ultra_3des_auth(packet->key, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_READBL, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
@@ -825,7 +787,7 @@ static void MifareUWriteBlockEx(mful_writeblock_t *packet, bool reply) {
|
||||
|
||||
// UL-AES authentication
|
||||
if (useAESKey) {
|
||||
if (mifare_ultra_aes_auth(0, packet->key, packet->use_schann, true) == 0) {
|
||||
if (mifare_ultra_aes_auth(0, packet->key, packet->use_schann, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_READBL, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
@@ -901,7 +863,7 @@ void MifareUWriteBlockCompat(mful_writeblock_t *packet) {
|
||||
|
||||
// UL-C authentication
|
||||
if (useCKey) {
|
||||
if (mifare_ultra_3des_auth(packet->key, true) == 0) {
|
||||
if (mifare_ultra_3des_auth(packet->key, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_WRITEBL_COMPAT, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
@@ -909,7 +871,7 @@ void MifareUWriteBlockCompat(mful_writeblock_t *packet) {
|
||||
|
||||
// UL-AES authentication
|
||||
if (useAESKey) {
|
||||
if (mifare_ultra_aes_auth(0, packet->key, packet->use_schann, true) == 0) {
|
||||
if (mifare_ultra_aes_auth(0, packet->key, packet->use_schann, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_WRITEBL_COMPAT, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
@@ -976,7 +938,7 @@ void MifareUSetKey(mful_setkey_t *packet) {
|
||||
|
||||
// UL-C authentication
|
||||
if (useCKey && packet->has_auth_key) {
|
||||
if (mifare_ultra_3des_auth(packet->auth_key, true) == 0) {
|
||||
if (mifare_ultra_3des_auth(packet->auth_key, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_SETKEY, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
@@ -984,7 +946,7 @@ void MifareUSetKey(mful_setkey_t *packet) {
|
||||
|
||||
// UL-AES authentication
|
||||
if (useAESKey && packet->has_auth_key) {
|
||||
if (mifare_ultra_aes_auth(0, packet->auth_key, packet->use_schann, true) == 0) {
|
||||
if (mifare_ultra_aes_auth(0, packet->auth_key, packet->use_schann, true, true, NULL) == 0) {
|
||||
OnErrorNG(CMD_HF_MIFAREU_SETKEY, PM3_ESOFT);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,6 @@ void MifareUWriteBlock(mful_writeblock_t *packet);
|
||||
|
||||
void MifareU3PassAuth(mful_3passauth_t *packet);
|
||||
void MifareU3PassChkKeys(mful_3passchk_t *packet);
|
||||
void MifareU3PAcquireEncryptedNonces(uint8_t keyno, uint8_t num_nonces_to_acquire);
|
||||
|
||||
void MifareNested(uint8_t blockNo, uint8_t keyType, uint8_t targetBlockNo, uint8_t targetKeyType, bool calibrate, uint8_t *key);
|
||||
void MifareStaticNested(uint8_t blockNo, uint8_t keyType, uint8_t targetBlockNo, uint8_t targetKeyType, uint8_t *key, uint8_t forceDetectDist);
|
||||
|
||||
+14
-2
@@ -402,7 +402,7 @@ int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int mifare_ultra_3des_auth(uint8_t *keybytes, bool check_answer) {
|
||||
int mifare_ultra_3des_auth(uint8_t *keybytes, bool try_auth, bool check_answer, uint8_t *nonce) {
|
||||
|
||||
/// 3des2k
|
||||
uint8_t random_a[8] = {1, 1, 1, 1, 1, 1, 1, 2};
|
||||
@@ -423,6 +423,12 @@ int mifare_ultra_3des_auth(uint8_t *keybytes, bool check_answer) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (nonce != NULL) {
|
||||
memcpy(nonce, resp + 1, 8);
|
||||
}
|
||||
if (!try_auth) {
|
||||
return 1;
|
||||
}
|
||||
// decrypt nonce.
|
||||
tdes_nxp_receive((void *)(resp + 1), (void *)random_b, sizeof(random_b), (const void *)key, IV, 2);
|
||||
rol(random_b, 8);
|
||||
@@ -480,7 +486,7 @@ int mifare_ultra_3des_auth(uint8_t *keybytes, bool check_answer) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int mifare_ultra_aes_auth(uint8_t keyno, uint8_t *keybytes, bool schann, bool check_answer) {
|
||||
int mifare_ultra_aes_auth(uint8_t keyno, uint8_t *keybytes, bool schann, bool try_auth, bool check_answer, uint8_t *nonce) {
|
||||
|
||||
/// aes-128
|
||||
uint8_t random_a[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2};
|
||||
@@ -509,6 +515,12 @@ int mifare_ultra_aes_auth(uint8_t keyno, uint8_t *keybytes, bool schann, bool ch
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (nonce != NULL) {
|
||||
memcpy(nonce, resp + 1, 16);
|
||||
}
|
||||
if (!try_auth) {
|
||||
return 1;
|
||||
}
|
||||
// decrypt tag nonce.
|
||||
mbedtls_aes_crypt_cbc(&actx, MBEDTLS_AES_DECRYPT, sizeof(random_b), IV, resp + 1, random_b);
|
||||
|
||||
|
||||
+2
-2
@@ -104,8 +104,8 @@ int mifare_classic_value(struct Crypto1State *pcs, uint8_t blockNo, uint8_t *blo
|
||||
|
||||
// Ultralight/NTAG...
|
||||
int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack);
|
||||
int mifare_ultra_3des_auth(uint8_t *keybytes, bool check_answer);
|
||||
int mifare_ultra_aes_auth(uint8_t keyno, uint8_t *keybytes, bool schann, bool check_answer);
|
||||
int mifare_ultra_3des_auth(uint8_t *keybytes, bool try_auth, bool check_answer, uint8_t *nonce);
|
||||
int mifare_ultra_aes_auth(uint8_t keyno, uint8_t *keybytes, bool schann, bool try_auth, bool check_answer, uint8_t *nonce);
|
||||
int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData);
|
||||
int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData);
|
||||
int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData);
|
||||
|
||||
@@ -575,7 +575,7 @@ def main():
|
||||
return
|
||||
common_nonces.append((challenges["challenge_100"], max_occurrence))
|
||||
else:
|
||||
p.console(f"hf mfu cauth -c {num_challenges}")
|
||||
p.console(f"hf mfu cauth --collect --read0 --noauth --reset --retries {num_challenges - 1}")
|
||||
for line in p.grabbed_output.splitlines():
|
||||
match = re.search(r"\[=\]\s+([0-9a-fA-F]{16})\s+\(count:\s*(\d+)\)", line)
|
||||
if match:
|
||||
|
||||
+57
-84
@@ -524,14 +524,17 @@ Default AES key is 00-00h. Both the data and UID one.
|
||||
Data key is 00, UID is 01. Authenticity is 02h
|
||||
Auth is 1A[Key ID][CRC] - AF[RndB] - AF[RndA][RndB'] - 00[RndA']
|
||||
*/
|
||||
static int ul3pass_authentication(const uint8_t *key, uint8_t keyno, bool switch_off_field, int retries, uint32_t *auths, uint32_t *ms, bool schann, bool check_answer, bool use_fastread0) {
|
||||
static int ul3pass_authentication(const uint8_t *key, uint8_t keyno, bool switch_off_field, int retries, uint32_t *auths, uint32_t *ms, bool schann, bool try_auth, bool check_answer, bool use_fastread0, bool get_nonces, uint8_t* nonces, bool reset_field) {
|
||||
// keyno < 3: ULAES
|
||||
// keyno = 3: ULC
|
||||
mful_3passauth_t payload = {
|
||||
.turn_off_field = switch_off_field,
|
||||
.try_auth = try_auth,
|
||||
.check_answer = check_answer,
|
||||
.use_schann = schann,
|
||||
.use_fastread0 = use_fastread0,
|
||||
.get_nonces = get_nonces,
|
||||
.reset_field = reset_field,
|
||||
.keyno = keyno,
|
||||
.retries = retries,
|
||||
};
|
||||
@@ -545,6 +548,7 @@ static int ul3pass_authentication(const uint8_t *key, uint8_t keyno, bool switch
|
||||
struct rp {
|
||||
uint32_t auths;
|
||||
uint32_t ticks;
|
||||
uint8_t nonces[PM3_CMD_DATA_SIZE - sizeof(uint32_t) * 2];
|
||||
} PACKED;
|
||||
struct rp *rpayload = (struct rp *) resp.data.asBytes;
|
||||
|
||||
@@ -554,6 +558,9 @@ static int ul3pass_authentication(const uint8_t *key, uint8_t keyno, bool switch
|
||||
if (ms != NULL) {
|
||||
*ms += rpayload->ticks;
|
||||
}
|
||||
if (get_nonces && nonces != NULL) {
|
||||
memcpy(nonces, rpayload->nonces, MIN(sizeof(rpayload->nonces), rpayload->auths * (keyno == 3 ? 8 : 16)));
|
||||
}
|
||||
return resp.status;
|
||||
}
|
||||
|
||||
@@ -637,7 +644,7 @@ static int try_default_3des_keys(bool override, uint8_t **correct_key, bool use_
|
||||
|
||||
for (uint8_t i = 0; i < ARRAYLEN(default_3des_keys); ++i) {
|
||||
uint8_t *key = default_3des_keys[i];
|
||||
if (ul3pass_authentication(key, MIFAREULC_KEY_INDEX, true, 0, NULL, NULL, false, true, use_fastread0) == PM3_SUCCESS) {
|
||||
if (ul3pass_authentication(key, MIFAREULC_KEY_INDEX, true, 0, NULL, NULL, false, true, true, use_fastread0, false, NULL, false) == PM3_SUCCESS) {
|
||||
*correct_key = key;
|
||||
res = PM3_SUCCESS;
|
||||
break;
|
||||
@@ -674,7 +681,7 @@ static int try_default_aes_keys(bool override, bool use_schann, bool use_fastrea
|
||||
|
||||
for (uint8_t keyno = 0; keyno < 3; keyno++) {
|
||||
|
||||
if (ul3pass_authentication(key, keyno, true, 0, NULL, NULL, use_schann, true, use_fastread0) == PM3_SUCCESS) {
|
||||
if (ul3pass_authentication(key, keyno, true, 0, NULL, NULL, use_schann, true, true, use_fastread0, false, NULL, false) == PM3_SUCCESS) {
|
||||
|
||||
char keystr[20] = {0};
|
||||
switch (keyno) {
|
||||
@@ -714,13 +721,13 @@ static int ul_auth_select(iso14a_card_select_t *card, uint64_t tagtype, bool has
|
||||
bool use_fastread0 = false;
|
||||
if (hasAuthKey && (tagtype & MFU_TT_UL_C)) {
|
||||
//will select card automatically and close connection on error
|
||||
if (ul3pass_authentication(authkey, MIFAREULC_KEY_INDEX, false, 0, NULL, NULL, false, true, use_fastread0) != PM3_SUCCESS) {
|
||||
if (ul3pass_authentication(authkey, MIFAREULC_KEY_INDEX, false, 0, NULL, NULL, false, true, true, use_fastread0, false, NULL, false) != PM3_SUCCESS) {
|
||||
PrintAndLogEx(WARNING, "Authentication Failed UL-C");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
} else if (hasAuthKey && (tagtype & MFU_TT_UL_AES)) {
|
||||
//will select card automatically and close connection on error
|
||||
if (ul3pass_authentication(authkey, 0, false, 0, NULL, NULL, use_schann, true, use_fastread0) != PM3_SUCCESS) {
|
||||
if (ul3pass_authentication(authkey, 0, false, 0, NULL, NULL, use_schann, true, true, use_fastread0, false, NULL, false) != PM3_SUCCESS) {
|
||||
PrintAndLogEx(WARNING, "Authentication Failed UL-AES");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
@@ -4473,68 +4480,11 @@ static int mfu_3pass_check_keys(uint8_t key_index, uint8_t firstChunk, uint8_t l
|
||||
// Ultralight C Methods
|
||||
//-------------------------------------------------------------------------------
|
||||
|
||||
static uint32_t acquire_ul3p_nonces(uint8_t keytype, uint32_t num_nonces_to_acquire, uint64_t *nonces) {
|
||||
|
||||
uint32_t num_acquired_nonces = 0;
|
||||
uint8_t nonce_size = keytype == 1 ? 8 : 16;
|
||||
uint8_t max_nonces_per_response = (PM3_CMD_DATA_SIZE - sizeof(uint8_t)) / nonce_size;
|
||||
PacketResponseNG resp;
|
||||
memset(&resp, 0, sizeof(resp));
|
||||
|
||||
if (keytype != 1) {
|
||||
PrintAndLogEx(ERR, "Currently, only supported keytype for acquiring nonces is ULC");
|
||||
return PM3_ENOTIMPL;
|
||||
}
|
||||
|
||||
do {
|
||||
clearCommandBuffer();
|
||||
SendCommandMIX(CMD_HF_MIFAREU3P_ACQ_ENCRYPTED_NONCES, MIFAREULC_KEY_INDEX, MIN(num_nonces_to_acquire - num_acquired_nonces, max_nonces_per_response), 0, NULL, 0);
|
||||
if (WaitForResponseTimeout(CMD_HF_MIFAREU3P_ACQ_ENCRYPTED_NONCES, &resp, 1500 + num_nonces_to_acquire * 15) == false) {
|
||||
PrintAndLogEx(WARNING, "Timeout");
|
||||
return num_acquired_nonces;
|
||||
}
|
||||
|
||||
if (resp.status != PM3_SUCCESS) {
|
||||
return num_acquired_nonces;
|
||||
}
|
||||
|
||||
uint8_t num_sampled_nonces = resp.data.asBytes[0];
|
||||
uint8_t *bufp = resp.data.asBytes + 1;
|
||||
|
||||
for (uint16_t i = 0; i < num_sampled_nonces; i++) {
|
||||
uint64_t nt_enc = bytes_to_num(bufp + i * nonce_size, nonce_size);
|
||||
if (nonces != NULL) {
|
||||
nonces[num_acquired_nonces] = nt_enc;
|
||||
}
|
||||
num_acquired_nonces++;
|
||||
}
|
||||
|
||||
} while (num_acquired_nonces < num_nonces_to_acquire);
|
||||
return num_acquired_nonces;
|
||||
}
|
||||
|
||||
static int collect_and_stat_nonces(uint32_t collect_nonces) {
|
||||
PrintAndLogEx(INFO, "Collecting %d nonces...", collect_nonces);
|
||||
uint64_t *nonces = calloc(collect_nonces, sizeof(uint64_t));
|
||||
if (nonces == NULL) {
|
||||
PrintAndLogEx(WARNING, "Failed to allocate memory");
|
||||
return PM3_EMALLOC;
|
||||
}
|
||||
|
||||
uint32_t num_sampled_nonces = acquire_ul3p_nonces(1, collect_nonces, nonces);
|
||||
if (num_sampled_nonces == 0) {
|
||||
PrintAndLogEx(WARNING, "Failed to acquire nonces");
|
||||
free(nonces);
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
static int stat_ulc_nonces(uint16_t num_sampled_nonces, uint64_t* nonces) {
|
||||
for (uint16_t i = 0; i < num_sampled_nonces; i++) {
|
||||
PrintAndLogEx(DEBUG, "Encrypted nonce: %016" PRIx64 "\n", nonces[i]);
|
||||
}
|
||||
|
||||
if (num_sampled_nonces != collect_nonces) {
|
||||
PrintAndLogEx(WARNING, "Failed to acquire all nonces, got %u/%u", num_sampled_nonces, collect_nonces);
|
||||
}
|
||||
|
||||
// Count nonce frequencies
|
||||
typedef struct {
|
||||
uint64_t nonce;
|
||||
@@ -4544,7 +4494,6 @@ static int collect_and_stat_nonces(uint32_t collect_nonces) {
|
||||
nonce_count_t *counts = calloc(num_sampled_nonces, sizeof(nonce_count_t));
|
||||
if (counts == NULL) {
|
||||
PrintAndLogEx(WARNING, "Failed to allocate memory for counts");
|
||||
free(nonces);
|
||||
return PM3_EMALLOC;
|
||||
}
|
||||
uint32_t unique_count = 0;
|
||||
@@ -4584,16 +4533,16 @@ static int collect_and_stat_nonces(uint32_t collect_nonces) {
|
||||
uint8_t topn = 10;
|
||||
uint32_t show_count = recurring_count < topn ? recurring_count : topn;
|
||||
if (counts[0].count == 1) {
|
||||
PrintAndLogEx(INFO, "All %u collected nonces are unique.", num_sampled_nonces);
|
||||
free(nonces);
|
||||
if (unique_count > 1) {
|
||||
PrintAndLogEx(INFO, "All %u collected nonces are unique.", num_sampled_nonces);
|
||||
}
|
||||
free(counts);
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
PrintAndLogEx(INFO, "Top %u most common nonces:", show_count);
|
||||
for (uint32_t i = 0; i < show_count; i++) {
|
||||
PrintAndLogEx(INFO, " %016" PRIx64 " (count: %u)", counts[i].nonce, counts[i].count);
|
||||
PrintAndLogEx(INFO, " %016" PRIx64 " (count: %u)", BSWAP_64(counts[i].nonce), counts[i].count);
|
||||
}
|
||||
free(nonces);
|
||||
free(counts);
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
@@ -4613,11 +4562,13 @@ static int CmdHF14AMfUCAuth(const char *Cmd) {
|
||||
arg_param_begin,
|
||||
arg_str0(NULL, "key", "<hex>", "Authentication key (16 bytes in hex)"),
|
||||
arg_lit0("l", NULL, "Swap entered key's endianness"),
|
||||
arg_lit0("k", NULL, "Keep field on (only if a key is provided)"),
|
||||
arg_lit0("k", NULL, "Keep field on at the end (only if a key is provided)"),
|
||||
arg_int0("r", "retries", "<n>", "Number of retries with provided key (def: 0)"),
|
||||
arg_lit0("n", "nocheck", "Skip checking tag answer correctness (only if a key is provided)"),
|
||||
arg_lit0("0", "read0", "Use fast READ0 (skip anticol)"),
|
||||
arg_int0("c", "collect", "<n>", "Collect <n> nonces and show top 10 (def: 0, no collection)"),
|
||||
arg_lit0(NULL, "noauth", "Skip authentication (when collecting nonces)"),
|
||||
arg_lit0(NULL, "reset", "Reset field between each attempt"),
|
||||
arg_lit0(NULL, "collect", "Collect nonces and show top 10"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
@@ -4631,7 +4582,9 @@ static int CmdHF14AMfUCAuth(const char *Cmd) {
|
||||
int retries = arg_get_int_def(ctx, 4, 0);
|
||||
bool check_answer = !arg_get_lit(ctx, 5);
|
||||
bool use_fastread0 = arg_get_lit(ctx, 6);
|
||||
int collect_nonces = arg_get_int_def(ctx, 7, 0);
|
||||
bool skip_auth = arg_get_lit(ctx, 7);
|
||||
bool reset_field = arg_get_lit(ctx, 8);
|
||||
bool collect_nonces = arg_get_lit(ctx, 9);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
if (ak_len != 16 && ak_len != 0) {
|
||||
@@ -4642,7 +4595,7 @@ static int CmdHF14AMfUCAuth(const char *Cmd) {
|
||||
PrintAndLogEx(ERR, "Invalid retries (must be 0..10000)");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
if ((retries > 0) && (ak_len == 0)) {
|
||||
if ((retries > 0) && (ak_len == 0) && !skip_auth) {
|
||||
PrintAndLogEx(WARNING, "ERROR: Key is required for retries");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
@@ -4651,12 +4604,18 @@ static int CmdHF14AMfUCAuth(const char *Cmd) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if (collect_nonces > 0) {
|
||||
if (ak_len != 0 || keep_field_on || retries > 0 || check_answer == false) {
|
||||
PrintAndLogEx(WARNING, "ERROR: Nonce collection incompatible with other options");
|
||||
return PM3_EINVARG;
|
||||
if (skip_auth && !collect_nonces) {
|
||||
PrintAndLogEx(WARNING, "ERROR: noauth option only valid with collect option");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
uint8_t *nonces = NULL;
|
||||
if (collect_nonces) {
|
||||
nonces = calloc((1 + retries) * sizeof(uint64_t), sizeof(uint8_t));
|
||||
if (nonces == NULL) {
|
||||
PrintAndLogEx(WARNING, "Failed to allocate memory");
|
||||
return PM3_EMALLOC;
|
||||
}
|
||||
return collect_and_stat_nonces(collect_nonces);
|
||||
}
|
||||
|
||||
// Swap endianness
|
||||
@@ -4669,26 +4628,40 @@ static int CmdHF14AMfUCAuth(const char *Cmd) {
|
||||
uint32_t ms = 0;
|
||||
|
||||
// If no hex key is specified, try default keys
|
||||
if (ak_len == 0) {
|
||||
if (ak_len == 0 && !collect_nonces && !skip_auth) {
|
||||
|
||||
PrintAndLogEx(INFO, "Called with no key, checking default keys...");
|
||||
isok = try_default_3des_keys(false, &auth_key_ptr, use_fastread0);
|
||||
} else {
|
||||
// try user-supplied
|
||||
|
||||
isok = ul3pass_authentication(auth_key_ptr, MIFAREULC_KEY_INDEX, !keep_field_on, retries, &auths, &ms, false, check_answer, use_fastread0);
|
||||
do {
|
||||
uint16_t max_retries_per_call = retries;
|
||||
if (collect_nonces) {
|
||||
// Not strictly needed, but to avoid fw warning
|
||||
max_retries_per_call = ((PM3_CMD_DATA_SIZE - sizeof(uint32_t) * 2) / sizeof(uint64_t)) - 1;
|
||||
}
|
||||
isok = ul3pass_authentication(auth_key_ptr, MIFAREULC_KEY_INDEX, !keep_field_on, MIN(retries - auths, max_retries_per_call), &auths, &ms, false, !skip_auth, check_answer, use_fastread0, collect_nonces, nonces + auths * sizeof(uint64_t), reset_field);
|
||||
} while (skip_auth && auths < 1 + retries);
|
||||
}
|
||||
|
||||
if (isok == PM3_SUCCESS) {
|
||||
PrintAndLogEx(SUCCESS, "Authentication 3DES key... " _GREEN_("%s") " ( " _GREEN_("ok")" )", sprint_hex_inrow(auth_key_ptr, 16));
|
||||
} else {
|
||||
PrintAndLogEx(WARNING, "Authentication ( " _RED_("fail") " )");
|
||||
if (collect_nonces) {
|
||||
stat_ulc_nonces(auths, (uint64_t*)nonces);
|
||||
}
|
||||
|
||||
if (!skip_auth) {
|
||||
if (isok == PM3_SUCCESS) {
|
||||
PrintAndLogEx(SUCCESS, "Authentication 3DES key... " _GREEN_("%s") " ( " _GREEN_("ok")" )", sprint_hex_inrow(auth_key_ptr, 16));
|
||||
} else {
|
||||
PrintAndLogEx(WARNING, "Authentication ( " _RED_("fail") " )");
|
||||
}
|
||||
}
|
||||
if (retries > 0) {
|
||||
PrintAndLogEx(INFO, "Time spent " _YELLOW_("%.1fs"), (float)(ms / 1000.0));
|
||||
PrintAndLogEx(INFO, "Authentication attempts: %u", auths);
|
||||
PrintAndLogEx(INFO, "Speed: %.1f auths/s", (float)(auths * 1000.0 / ms));
|
||||
}
|
||||
free(nonces);
|
||||
return isok;
|
||||
}
|
||||
|
||||
@@ -4879,7 +4852,7 @@ static int CmdHF14AMfUAESAuth(const char *Cmd) {
|
||||
uint32_t auths = 0;
|
||||
uint32_t ms = 0;
|
||||
|
||||
int result = ul3pass_authentication(auth_key_ptr, key_index, !keep_field_on, retries, &auths, &ms, use_schann, check_answer, use_fastread0);
|
||||
int result = ul3pass_authentication(auth_key_ptr, key_index, !keep_field_on, retries, &auths, &ms, use_schann, true , check_answer, use_fastread0, false, NULL, false);
|
||||
if (result == PM3_SUCCESS) {
|
||||
PrintAndLogEx(SUCCESS, "Authentication with " _YELLOW_("%s") " " _GREEN_("%s") " ( " _GREEN_("ok")" )"
|
||||
, key_type[key_index]
|
||||
|
||||
+10
-7
@@ -376,12 +376,15 @@ typedef struct {
|
||||
} PACKED mfc_eload_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t turn_off_field : 1;
|
||||
uint8_t check_answer : 1;
|
||||
uint8_t keyno : 2;
|
||||
uint8_t use_schann : 1;
|
||||
uint8_t use_fastread0 : 1;
|
||||
uint8_t reserved : 2;
|
||||
uint16_t turn_off_field : 1;
|
||||
uint16_t try_auth : 1;
|
||||
uint16_t check_answer : 1;
|
||||
uint16_t keyno : 2;
|
||||
uint16_t use_schann : 1;
|
||||
uint16_t use_fastread0 : 1;
|
||||
uint16_t get_nonces : 1;
|
||||
uint16_t reset_field : 1;
|
||||
uint16_t reserved : 7;
|
||||
uint16_t retries;
|
||||
uint8_t key[16];
|
||||
} PACKED mful_3passauth_t;
|
||||
@@ -835,7 +838,7 @@ typedef struct {
|
||||
|
||||
// ultralight-C & AES
|
||||
#define CMD_HF_MIFAREU3P_AUTH 0x0724
|
||||
#define CMD_HF_MIFAREU3P_ACQ_ENCRYPTED_NONCES 0x0725
|
||||
// 0x0725 is free
|
||||
#define CMD_HF_MIFAREU3P_CHKKEY 0x0726
|
||||
#define CMD_HF_MIFAREU_SETKEY 0x0727
|
||||
|
||||
|
||||
Reference in new issue
Block a user