proxmark3/client/cmdhfmfdes.c

568 lines
22 KiB
C
Raw Normal View History

2014-09-12 05:23:46 +08:00
//-----------------------------------------------------------------------------
// Copyright (C) 2014 Iceman
//
// 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.
//-----------------------------------------------------------------------------
// High frequency MIFARE Desfire commands
//-----------------------------------------------------------------------------
#include "cmdhfmfdes.h"
#include <stdio.h>
#include <string.h>
#include "cmdparser.h" // command_t
#include "comms.h"
#include "ui.h"
#include "cmdhf14a.h"
#include "mbedtls/des.h"
uint8_t key_zero_data[16] = { 0x00 };
uint8_t key_ones_data[16] = { 0x01 };
2019-03-10 07:00:59 +08:00
uint8_t key_defa_data[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
uint8_t key_picc_data[16] = { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f };
2014-09-12 05:23:46 +08:00
static int CmdHelp(const char *Cmd);
2019-03-10 06:35:06 +08:00
static int CmdHF14ADesInfo(const char *Cmd) {
2019-04-10 18:23:40 +08:00
(void)Cmd; // Cmd is not used so far
2014-09-12 05:23:46 +08:00
SendCommandNG(CMD_HF_DESFIRE_INFO, NULL, 0);
2019-04-18 18:43:35 +08:00
PacketResponseNG resp;
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "Command execute timeout");
2019-12-02 18:47:43 +08:00
return PM3_ETIMEOUT;
2019-03-10 06:35:06 +08:00
}
2019-04-18 05:44:48 +08:00
uint8_t isOK = resp.oldarg[0] & 0xff;
2019-03-10 07:00:59 +08:00
if (!isOK) {
2019-04-18 05:44:48 +08:00
switch (resp.oldarg[1]) {
2019-03-10 06:35:06 +08:00
case 1:
2019-03-10 07:00:59 +08:00
PrintAndLogEx(WARNING, "Can't select card");
break;
2019-03-10 06:35:06 +08:00
case 2:
2019-03-10 07:00:59 +08:00
PrintAndLogEx(WARNING, "Card is most likely not Desfire. Its UID has wrong size");
break;
2019-03-10 06:35:06 +08:00
case 3:
default:
2019-03-10 07:00:59 +08:00
PrintAndLogEx(WARNING, "Command unsuccessful");
break;
2019-03-10 06:35:06 +08:00
}
2019-12-02 18:47:43 +08:00
return PM3_ESOFT;
2019-03-10 06:35:06 +08:00
}
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "-- Desfire Information --------------------------------------");
PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
2019-04-18 05:44:48 +08:00
PrintAndLogEx(NORMAL, " UID : %s", sprint_hex(resp.data.asBytes, 7));
PrintAndLogEx(NORMAL, " Batch number : %s", sprint_hex(resp.data.asBytes + 28, 5));
PrintAndLogEx(NORMAL, " Production date : week %02x, 20%02x", resp.data.asBytes[33], resp.data.asBytes[34]);
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " -----------------------------------------------------------");
PrintAndLogEx(NORMAL, " Hardware Information");
2019-04-18 05:44:48 +08:00
PrintAndLogEx(NORMAL, " Vendor Id : %s", getTagInfo(resp.data.asBytes[7]));
PrintAndLogEx(NORMAL, " Type : 0x%02X", resp.data.asBytes[8]);
PrintAndLogEx(NORMAL, " Subtype : 0x%02X", resp.data.asBytes[9]);
PrintAndLogEx(NORMAL, " Version : %s", getVersionStr(resp.data.asBytes[10], resp.data.asBytes[11]));
PrintAndLogEx(NORMAL, " Storage size : %s", getCardSizeStr(resp.data.asBytes[12]));
PrintAndLogEx(NORMAL, " Protocol : %s", getProtocolStr(resp.data.asBytes[13]));
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " -----------------------------------------------------------");
PrintAndLogEx(NORMAL, " Software Information");
2019-04-18 05:44:48 +08:00
PrintAndLogEx(NORMAL, " Vendor Id : %s", getTagInfo(resp.data.asBytes[14]));
PrintAndLogEx(NORMAL, " Type : 0x%02X", resp.data.asBytes[15]);
PrintAndLogEx(NORMAL, " Subtype : 0x%02X", resp.data.asBytes[16]);
PrintAndLogEx(NORMAL, " Version : %d.%d", resp.data.asBytes[17], resp.data.asBytes[18]);
PrintAndLogEx(NORMAL, " storage size : %s", getCardSizeStr(resp.data.asBytes[19]));
PrintAndLogEx(NORMAL, " Protocol : %s", getProtocolStr(resp.data.asBytes[20]));
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
// Master Key settings
getKeySettings(NULL);
2019-03-10 06:35:06 +08:00
// Free memory on card
uint8_t data[1] = {GET_FREE_MEMORY};
SendCommandOLD(CMD_HF_DESFIRE_COMMAND, (INIT | DISCONNECT), 0x01, 0, data, sizeof(data));
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500))
2019-12-02 18:47:43 +08:00
return PM3_ETIMEOUT;
2019-03-10 06:35:06 +08:00
uint8_t tmp[3];
2019-04-18 05:44:48 +08:00
memcpy(tmp, resp.data.asBytes + 3, 3);
2014-09-12 05:23:46 +08:00
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " Available free memory on card : %d bytes", le24toh(tmp));
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
2019-03-10 06:35:06 +08:00
/*
Card Master key (CMK) 0x00 AID = 00 00 00 (card level)
Application Master Key (AMK) 0x00 AID != 00 00 00
Application keys (APK) 0x01-0x0D
Application free 0x0E
Application never 0x0F
2019-03-10 06:35:06 +08:00
ACCESS RIGHTS:
keys 0,1,2,3 C
keys 4,5,6,7 RW
keys 8,9,10,11 W
keys 12,13,14,15 R
2019-03-10 06:35:06 +08:00
*/
2019-12-02 18:47:43 +08:00
return PM3_SUCCESS;
2014-09-12 05:23:46 +08:00
}
/*
The 7 MSBits (= n) code the storage size itself based on 2^n,
2014-09-12 05:23:46 +08:00
the LSBit is set to '0' if the size is exactly 2^n
2019-03-10 06:35:06 +08:00
and set to '1' if the storage size is between 2^n and 2^(n+1).
For this version of DESFire the 7 MSBits are set to 0x0C (2^12 = 4096) and the LSBit is '0'.
2014-09-12 05:23:46 +08:00
*/
char *getCardSizeStr(uint8_t fsize) {
2019-03-10 06:35:06 +08:00
static char buf[30] = {0x00};
char *retStr = buf;
2014-09-12 05:23:46 +08:00
2019-03-10 07:00:59 +08:00
uint16_t usize = 1 << ((fsize >> 1) + 1);
uint16_t lsize = 1 << (fsize >> 1);
2019-03-10 06:35:06 +08:00
// is LSB set?
2019-03-10 07:00:59 +08:00
if (fsize & 1)
sprintf(retStr, "0x%02X (%d - %d bytes)", fsize, usize, lsize);
2019-03-10 06:35:06 +08:00
else
sprintf(retStr, "0x%02X (%d bytes)", fsize, lsize);
return buf;
2014-09-12 05:23:46 +08:00
}
char *getProtocolStr(uint8_t id) {
2014-09-12 05:23:46 +08:00
2019-03-10 06:35:06 +08:00
static char buf[30] = {0x00};
char *retStr = buf;
2014-09-12 05:23:46 +08:00
2019-03-10 07:00:59 +08:00
if (id == 0x05)
sprintf(retStr, "0x%02X (ISO 14443-3, 14443-4)", id);
2019-03-10 06:35:06 +08:00
else
2019-03-10 07:00:59 +08:00
sprintf(retStr, "0x%02X (Unknown)", id);
2019-03-10 06:35:06 +08:00
return buf;
2014-09-12 05:23:46 +08:00
}
char *getVersionStr(uint8_t major, uint8_t minor) {
2019-03-10 06:35:06 +08:00
static char buf[30] = {0x00};
char *retStr = buf;
2019-03-10 07:00:59 +08:00
if (major == 0x00)
sprintf(retStr, "%d.%d (Desfire MF3ICD40)", major, minor);
else if (major == 0x01 && minor == 0x00)
sprintf(retStr, "%d.%d (Desfire EV1)", major, minor);
else if (major == 0x12 && minor == 0x00)
sprintf(retStr, "%d.%d (Desfire EV2)", major, minor);
2019-03-10 06:35:06 +08:00
else
2019-03-10 07:00:59 +08:00
sprintf(retStr, "%d.%d (Unknown)", major, minor);
2019-03-10 06:35:06 +08:00
return buf;
}
void getKeySettings(uint8_t *aid) {
2019-03-10 06:35:06 +08:00
char messStr[512] = {0x00};
const char *str = messStr;
2019-03-10 06:35:06 +08:00
uint8_t isOK = 0;
2019-04-18 18:43:35 +08:00
PacketResponseNG resp;
2019-03-10 06:35:06 +08:00
//memset(messStr, 0x00, 512);
2019-03-10 07:00:59 +08:00
if (aid == NULL) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " CMK - PICC, Card Master Key settings ");
PrintAndLogEx(NORMAL, "");
{
uint8_t data[1] = {GET_KEY_SETTINGS}; // 0x45
SendCommandOLD(CMD_HF_DESFIRE_COMMAND, INIT | DISCONNECT, sizeof(data), 0, data, sizeof(data));
}
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {return;}
2019-04-18 05:44:48 +08:00
isOK = resp.oldarg[0] & 0xff;
2019-03-10 07:00:59 +08:00
if (!isOK) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Can't select master application");
return;
}
2019-04-18 05:44:48 +08:00
str = (resp.data.asBytes[3] & (1 << 3)) ? "YES" : "NO";
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " [0x08] Configuration changeable : %s", str);
2019-04-18 05:44:48 +08:00
str = (resp.data.asBytes[3] & (1 << 2)) ? "NO" : "YES";
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " [0x04] CMK required for create/delete : %s", str);
2019-04-18 05:44:48 +08:00
str = (resp.data.asBytes[3] & (1 << 1)) ? "NO" : "YES";
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " [0x02] Directory list access with CMK : %s", str);
2019-04-18 05:44:48 +08:00
str = (resp.data.asBytes[3] & (1 << 0)) ? "YES" : "NO";
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " [0x01] CMK is changeable : %s", str);
2019-12-02 18:47:43 +08:00
{
uint8_t data[2] = {GET_KEY_VERSION, 0}; // 0x64
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, INIT | DISCONNECT, sizeof(data), 0, data, sizeof(data));
}
2019-12-02 18:47:43 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)) { return; }
2019-04-18 05:44:48 +08:00
isOK = resp.oldarg[0] & 0xff;
2019-03-10 07:00:59 +08:00
if (!isOK) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Can't read key-version");
return;
}
PrintAndLogEx(NORMAL, "");
2019-04-18 05:44:48 +08:00
PrintAndLogEx(NORMAL, " Max number of keys : %d", resp.data.asBytes[4]);
PrintAndLogEx(NORMAL, " Master key Version : %d (0x%02x)", resp.data.asBytes[3], resp.data.asBytes[3]);
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " ----------------------------------------------------------");
{
uint8_t data[2] = {AUTHENTICATE, 0}; // 0x0A, KEY 0
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, INIT | DISCONNECT, sizeof(data), 0, data, sizeof(data));
}
2019-12-02 18:47:43 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {return;}
2019-04-18 05:44:48 +08:00
isOK = resp.data.asBytes[2] & 0xff;
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " [0x0A] Authenticate : %s", (isOK == 0xAE) ? "NO" : "YES");
2019-03-10 06:35:06 +08:00
{
uint8_t data[2] = {AUTHENTICATE_ISO, 0}; // 0x1A, KEY 0
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, INIT | DISCONNECT, sizeof(data), 0, data, sizeof(data));
}
2019-12-02 18:47:43 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {return;}
2019-04-18 05:44:48 +08:00
isOK = resp.data.asBytes[2] & 0xff;
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " [0x1A] Authenticate ISO : %s", (isOK == 0xAE) ? "NO" : "YES");
2019-03-10 06:35:06 +08:00
{
uint8_t data[2] = {AUTHENTICATE_AES, 0}; // 0xAA, KEY 0
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, INIT | DISCONNECT, sizeof(data), 0, data, sizeof(data));
}
2019-12-02 18:47:43 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {return;}
2019-04-18 05:44:48 +08:00
isOK = resp.data.asBytes[2] & 0xff;
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " [0xAA] Authenticate AES : %s", (isOK == 0xAE) ? "NO" : "YES");
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, " ----------------------------------------------------------");
} else {
PrintAndLogEx(NORMAL, " AMK - Application Master Key settings");
// SELECT AID
{
uint8_t data[4] = {SELECT_APPLICATION}; // 0x5a
memcpy(data + 1, aid, 3);
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, INIT | CLEARTRACE, sizeof(data), 0, data, sizeof(data));
}
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Timed-out");
return;
}
2019-04-18 05:44:48 +08:00
isOK = resp.oldarg[0] & 0xff;
2019-03-10 07:00:59 +08:00
if (!isOK) {
PrintAndLogEx(WARNING, " Can't select AID: %s", sprint_hex(aid, 3));
2019-03-10 06:35:06 +08:00
return;
}
// KEY SETTINGS
{
uint8_t data[1] = {GET_KEY_SETTINGS}; // 0x45
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, NONE, sizeof(data), 0, data, sizeof(data));
}
2019-12-02 18:47:43 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
2019-03-10 06:35:06 +08:00
return;
}
2019-04-18 05:44:48 +08:00
isOK = resp.oldarg[0] & 0xff;
2019-03-10 07:00:59 +08:00
if (!isOK) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Can't read Application Master key settings");
} else {
// Access rights.
2019-04-18 05:44:48 +08:00
uint8_t rights = (resp.data.asBytes[3] >> 4 & 0xff);
2019-03-10 07:00:59 +08:00
switch (rights) {
2019-03-10 06:35:06 +08:00
case 0x00:
str = "AMK authentication is necessary to change any key (default)";
break;
case 0x0e:
str = "Authentication with the key to be changed (same KeyNo) is necessary to change a key";
break;
case 0x0f:
str = "All keys (except AMK,see Bit0) within this application are frozen";
break;
default:
2019-08-09 19:02:10 +08:00
str = "Authentication with the specified key is necessary to change any key. A change key and a PICC master key (CMK) can only be changed after authentication with the master key. For keys other then the master or change key, an authentication with the same key is needed.";
2019-03-10 06:35:06 +08:00
break;
}
PrintAndLogEx(NORMAL, "Changekey Access rights");
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, "-- %s", str);
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "");
// same as CMK
2019-04-18 05:44:48 +08:00
str = (resp.data.asBytes[3] & (1 << 3)) ? "YES" : "NO";
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " 0x08 Configuration changeable : %s", str);
2019-04-18 05:44:48 +08:00
str = (resp.data.asBytes[3] & (1 << 2)) ? "NO" : "YES";
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " 0x04 AMK required for create/delete : %s", str);
2019-04-18 05:44:48 +08:00
str = (resp.data.asBytes[3] & (1 << 1)) ? "NO" : "YES";
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " 0x02 Directory list access with AMK : %s", str);
2019-04-18 05:44:48 +08:00
str = (resp.data.asBytes[3] & (1 << 0)) ? "YES" : "NO";
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " 0x01 AMK is changeable : %s", str);
}
// KEY VERSION - AMK
{
uint8_t data[2] = {GET_KEY_VERSION, 0}; // 0x64
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, NONE, sizeof(data), 0, data, sizeof(data));
}
2019-12-02 18:47:43 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Timed-out");
return;
}
int numOfKeys;
2019-04-18 05:44:48 +08:00
isOK = resp.oldarg[0] & 0xff;
2019-04-10 14:57:23 +08:00
if (isOK == false) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Can't read Application Master key version. Trying all keys");
2019-04-10 14:57:23 +08:00
//numOfKeys = MAX_NUM_KEYS;
2019-03-10 07:00:59 +08:00
} else {
2019-04-18 05:44:48 +08:00
numOfKeys = resp.data.asBytes[4];
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "");
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " Max number of keys : %d", numOfKeys);
2019-04-18 05:44:48 +08:00
PrintAndLogEx(NORMAL, " Application Master key Version : %d (0x%02x)", resp.data.asBytes[3], resp.data.asBytes[3]);
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
}
// LOOP over numOfKeys that we got before.
// From 0x01 to numOfKeys. We already got 0x00. (AMK)
2019-04-10 14:57:23 +08:00
// TODO (iceman)
2019-12-03 21:51:55 +08:00
/*
for (int i = 0x01; i <= 0x0f; ++i) {
}
*/
2019-03-10 06:35:06 +08:00
}
2014-10-07 01:42:50 +08:00
}
static int CmdHF14ADesEnumApplications(const char *Cmd) {
2019-04-10 18:23:40 +08:00
(void)Cmd; // Cmd is not used so far
2019-03-10 06:35:06 +08:00
uint8_t isOK = 0x00;
uint8_t aid[3];
{
uint8_t data[1] = {GET_APPLICATION_IDS}; //0x6a
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, INIT | DISCONNECT, sizeof(data), 0, data, sizeof(data));
}
2019-04-18 18:43:35 +08:00
PacketResponseNG resp;
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
2019-12-02 18:47:43 +08:00
return PM3_ETIMEOUT;
2019-03-10 06:35:06 +08:00
}
2019-12-02 18:47:43 +08:00
2019-04-18 05:44:48 +08:00
isOK = resp.oldarg[0] & 0xff;
2019-03-10 07:00:59 +08:00
if (!isOK) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Command unsuccessful");
2019-12-02 18:47:43 +08:00
return PM3_ESOFT;
2019-03-10 06:35:06 +08:00
}
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "-- Desfire Enumerate Applications ---------------------------");
PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
2019-04-18 18:43:35 +08:00
PacketResponseNG respAid;
PacketResponseNG respFiles;
2019-03-10 06:35:06 +08:00
uint8_t num = 0;
2019-04-18 05:44:48 +08:00
int max = resp.oldarg[1] - 3 - 2;
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
for (int i = 3; i <= max; i += 3) {
2019-04-18 05:44:48 +08:00
PrintAndLogEx(NORMAL, " Aid %d : %02X %02X %02X ", num, resp.data.asBytes[i], resp.data.asBytes[i + 1], resp.data.asBytes[i + 2]);
2019-03-10 06:35:06 +08:00
num++;
2019-04-18 05:44:48 +08:00
aid[0] = resp.data.asBytes[i];
aid[1] = resp.data.asBytes[i + 1];
aid[2] = resp.data.asBytes[i + 2];
getKeySettings(aid);
2019-03-10 06:35:06 +08:00
// Select Application
{
uint8_t data[4] = {SELECT_APPLICATION}; // 0x5a
memcpy(data + 1, &resp.data.asBytes[i], 3);
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, INIT, sizeof(data), 0, data, sizeof(data));
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &respAid, 1500)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Timed-out");
continue;
}
2019-04-18 05:44:48 +08:00
isOK = respAid.data.asBytes[2] & 0xff;
2019-03-10 07:00:59 +08:00
if (isOK != 0x00) {
2019-04-18 05:44:48 +08:00
PrintAndLogEx(WARNING, " Can't select AID: %s", sprint_hex(resp.data.asBytes + i, 3));
2019-03-10 06:35:06 +08:00
continue;
}
// Get File IDs
{
uint8_t data[1] = {GET_FILE_IDS}; // 0x6f
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, NONE, sizeof(data), 0, data, sizeof(data));
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &respFiles, 1500)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Timed-out");
continue;
} else {
2019-04-18 05:44:48 +08:00
isOK = respFiles.data.asBytes[2] & 0xff;
2019-03-10 07:00:59 +08:00
if (!isOK) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Can't get file ids ");
} else {
2019-04-18 05:44:48 +08:00
int respfileLen = resp.oldarg[1] - 3 - 2;
2019-03-10 07:00:59 +08:00
for (int j = 0; j < respfileLen; ++j) {
2019-04-18 05:44:48 +08:00
PrintAndLogEx(NORMAL, " Fileid %d :", resp.data.asBytes[j + 3]);
2019-03-10 06:35:06 +08:00
}
}
}
// Get ISO File IDs
{
uint8_t data[1] = {GET_ISOFILE_IDS}; // 0x61
2019-12-02 18:47:43 +08:00
SendCommandMIX(CMD_HF_DESFIRE_COMMAND, DISCONNECT, sizeof(data), 0, data, sizeof(data));
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &respFiles, 1500)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Timed-out");
continue;
} else {
2019-04-18 05:44:48 +08:00
isOK = respFiles.data.asBytes[2] & 0xff;
2019-03-10 07:00:59 +08:00
if (!isOK) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, " Can't get ISO file ids ");
} else {
2019-04-18 05:44:48 +08:00
int respfileLen = resp.oldarg[1] - 3 - 2;
2019-03-10 07:00:59 +08:00
for (int j = 0; j < respfileLen; ++j) {
2019-04-18 05:44:48 +08:00
PrintAndLogEx(NORMAL, " ISO Fileid %d :", resp.data.asBytes[j + 3]);
2019-03-10 06:35:06 +08:00
}
}
}
}
PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
2019-12-02 18:47:43 +08:00
return PM3_SUCCESS;
2014-09-12 05:23:46 +08:00
}
// MIAFRE DesFire Authentication
//
#define BUFSIZE 256
static int CmdHF14ADesAuth(const char *Cmd) {
2019-03-10 06:35:06 +08:00
// NR DESC KEYLENGHT
// ------------------------
// 1 = DES 8
// 2 = 3DES 16
// 3 = 3K 3DES 24
// 4 = AES 16
2014-10-07 01:42:50 +08:00
2019-03-10 06:35:06 +08:00
uint8_t keylength = 8;
unsigned char key[24];
2019-03-10 07:00:59 +08:00
if (strlen(Cmd) < 3) {
PrintAndLogEx(NORMAL, "Usage: hf mfdes auth <1|2|3> <1|2|3|4> <keyno> <key> ");
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " Auth modes");
PrintAndLogEx(NORMAL, " 1 = normal, 2 = iso, 3 = aes");
PrintAndLogEx(NORMAL, " Crypto");
PrintAndLogEx(NORMAL, " 1 = DES 2 = 3DES 3 = 3K3DES 4 = AES");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " hf mfdes auth 1 1 0 11223344");
PrintAndLogEx(NORMAL, " hf mfdes auth 3 4 0 404142434445464748494a4b4c4d4e4f");
2019-12-02 18:47:43 +08:00
return PM3_SUCCESS;
}
2019-03-10 07:00:59 +08:00
uint8_t cmdAuthMode = param_get8(Cmd, 0);
uint8_t cmdAuthAlgo = param_get8(Cmd, 1);
uint8_t cmdKeyNo = param_get8(Cmd, 2);
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
switch (cmdAuthMode) {
2019-03-10 06:35:06 +08:00
case 1:
2019-03-10 07:00:59 +08:00
if (cmdAuthAlgo != 1 && cmdAuthAlgo != 2) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Crypto algo not valid for the auth mode");
2019-12-02 18:47:43 +08:00
return PM3_EINVARG;
2019-03-10 06:35:06 +08:00
}
break;
case 2:
2019-03-10 07:00:59 +08:00
if (cmdAuthAlgo != 1 && cmdAuthAlgo != 2 && cmdAuthAlgo != 3) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Crypto algo not valid for the auth mode");
2019-12-02 18:47:43 +08:00
return PM3_EINVARG;
2019-03-10 06:35:06 +08:00
}
break;
case 3:
2019-03-10 07:00:59 +08:00
if (cmdAuthAlgo != 4) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Crypto algo not valid for the auth mode");
2019-12-02 18:47:43 +08:00
return PM3_EINVARG;
2019-03-10 06:35:06 +08:00
}
break;
default:
PrintAndLogEx(WARNING, "Wrong Auth mode");
2019-12-02 18:47:43 +08:00
return PM3_EINVARG;
2019-03-10 06:35:06 +08:00
}
2019-03-10 07:00:59 +08:00
switch (cmdAuthAlgo) {
2019-03-10 06:35:06 +08:00
case 2:
keylength = 16;
PrintAndLogEx(NORMAL, "3DES selected");
break;
case 3:
keylength = 24;
PrintAndLogEx(NORMAL, "3 key 3DES selected");
break;
case 4:
keylength = 16;
PrintAndLogEx(NORMAL, "AES selected");
break;
default:
cmdAuthAlgo = 1;
keylength = 8;
PrintAndLogEx(NORMAL, "DES selected");
break;
}
// key
2019-03-10 07:00:59 +08:00
if (param_gethex(Cmd, 3, key, keylength * 2)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "Key must include %d HEX symbols", keylength);
2019-12-02 18:47:43 +08:00
return PM3_EINVARG;
2019-03-10 06:35:06 +08:00
}
2019-12-02 18:47:43 +08:00
// algo, keylength,
uint8_t data[25] = {keylength}; // max length: 1 + 24 (3k3DES)
memcpy(data + 1, key, keylength);
2019-03-10 06:35:06 +08:00
clearCommandBuffer();
SendCommandOLD(CMD_HF_DESFIRE_AUTH1, cmdAuthMode, cmdAuthAlgo, cmdKeyNo, data, keylength + 1);
2019-04-18 18:43:35 +08:00
PacketResponseNG resp;
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 3000)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "Client command execute timeout");
2019-12-02 18:47:43 +08:00
return PM3_ETIMEOUT;
2019-03-10 06:35:06 +08:00
}
2019-04-18 05:44:48 +08:00
uint8_t isOK = resp.oldarg[0] & 0xff;
2019-03-10 07:00:59 +08:00
if (isOK) {
2020-01-06 06:07:33 +08:00
uint8_t *session_key = resp.data.asBytes;
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " Key :%s", sprint_hex(key, keylength));
2020-01-06 06:07:33 +08:00
PrintAndLogEx(NORMAL, " SESSION :%s", sprint_hex(session_key, keylength));
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
//PrintAndLogEx(NORMAL, " Expected :B5 21 9E E8 1A A7 49 9D 21 96 68 7E 13 97 38 56");
2019-03-10 07:00:59 +08:00
} else {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Client command failed.");
}
PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
2019-12-02 18:47:43 +08:00
return PM3_SUCCESS;
2014-09-12 05:23:46 +08:00
}
static command_t CommandTable[] = {
{"help", CmdHelp, AlwaysAvailable, "This help"},
{"info", CmdHF14ADesInfo, IfPm3Iso14443a, "Tag information"},
{"enum", CmdHF14ADesEnumApplications, IfPm3Iso14443a, "Tries enumerate all applications"},
{"auth", CmdHF14ADesAuth, IfPm3Iso14443a, "Tries a MIFARE DesFire Authentication"},
// {"rdbl", CmdHF14ADesRb, IfPm3Iso14443a, "Read MIFARE DesFire block"},
// {"wrbl", CmdHF14ADesWb, IfPm3Iso14443a, "write MIFARE DesFire block"},
{NULL, NULL, NULL, NULL}
2014-09-12 05:23:46 +08:00
};
static int CmdHelp(const char *Cmd) {
(void)Cmd; // Cmd is not used so far
CmdsHelp(CommandTable);
2019-12-02 18:47:43 +08:00
return PM3_SUCCESS;
}
int CmdHFMFDes(const char *Cmd) {
2019-03-10 07:00:59 +08:00
// flush
2019-03-10 06:35:06 +08:00
clearCommandBuffer();
2019-04-19 06:47:51 +08:00
return CmdsParse(CommandTable, Cmd);
2014-09-12 05:23:46 +08:00
}