mad: restore fixes lost during rebase (e.g. harden callers, fix MAD2 read gating...)
This commit is contained in:
4 files changed
+35
-29
No files matched your search
@@ -992,36 +992,33 @@ static int hfgal_update_mad(uint8_t cred_sector, uint8_t cad_sector,
|
||||
const uint8_t *mad_key, uint8_t mad_key_type,
|
||||
bool verbose) {
|
||||
// Read current MAD (sector 0)
|
||||
uint8_t sector0[4 * MFBLOCK_SIZE] = {0};
|
||||
int res = mf_read_sector(0, mad_key_type, mad_key, sector0);
|
||||
mad1_sector_t sector0 = {0};
|
||||
int res = mf_read_sector(0, mad_key_type, mad_key, (uint8_t *)§or0);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed reading MAD sector 0");
|
||||
return res;
|
||||
}
|
||||
|
||||
// Set AID for credential sector (0x4812)
|
||||
if (cred_sector >= 1 && cred_sector <= 15) {
|
||||
sector0[16 + 2 + (cred_sector - 1) * 2] = CLASSIC_CRED_AID & 0xFF;
|
||||
sector0[16 + 2 + (cred_sector - 1) * 2 + 1] = (CLASSIC_CRED_AID >> 8) & 0xFF;
|
||||
if (cred_sector >= 1 && cred_sector <= MAD1_NUM_AIDS) {
|
||||
sector0.mad.aid[cred_sector - 1] = CLASSIC_CRED_AID;
|
||||
}
|
||||
|
||||
// Set AID for CAD sector (0x4811)
|
||||
if (cad_sector >= 1 && cad_sector <= 15) {
|
||||
sector0[16 + 2 + (cad_sector - 1) * 2] = CLASSIC_CAD_AID & 0xFF;
|
||||
sector0[16 + 2 + (cad_sector - 1) * 2 + 1] = (CLASSIC_CAD_AID >> 8) & 0xFF;
|
||||
if (cad_sector >= 1 && cad_sector <= MAD1_NUM_AIDS) {
|
||||
sector0.mad.aid[cad_sector - 1] = CLASSIC_CAD_AID;
|
||||
}
|
||||
|
||||
// Recalculate CRC over bytes 17..47 (info byte + 15 AID entries)
|
||||
sector0[16] = CRC8Mad(§or0[16 + 1], 15 + 16);
|
||||
sector0.mad.crc = CRC8Mad((uint8_t *)§or0.mad.info, sizeof(mad1_t) - 1);
|
||||
|
||||
// Write blocks 1 and 2 of sector 0 back (block 0 is manufacturer block, don't touch)
|
||||
res = mf_write_block(1, mad_key_type, mad_key, §or0[MFBLOCK_SIZE]);
|
||||
res = mf_write_block(1, mad_key_type, mad_key, MF_SECTOR_BLOCK(sector0, 1));
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed writing MAD block 1");
|
||||
return res;
|
||||
}
|
||||
|
||||
res = mf_write_block(2, mad_key_type, mad_key, §or0[2 * MFBLOCK_SIZE]);
|
||||
res = mf_write_block(2, mad_key_type, mad_key, MF_SECTOR_BLOCK(sector0, 2));
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed writing MAD block 2");
|
||||
return res;
|
||||
|
||||
+17
-10
@@ -7045,18 +7045,19 @@ static int CmdHF14AMfMAD(const char *Cmd) {
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
got_first = true;
|
||||
const mad2_sector_t *pmad2 = NULL;
|
||||
bool got_mad2 = false;
|
||||
if (mf_read_sector(MF_MAD2_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, (uint8_t *)&mad2_sector) != PM3_SUCCESS) {
|
||||
if (verbose) {
|
||||
PrintAndLogEx(ERR, "error, read sector 0x10. card doesn't have MAD 2 or doesn't have MAD 2 on default keys");
|
||||
}
|
||||
got_first = false;
|
||||
} else {
|
||||
PrintAndLogEx(INFO, "Authentication ( " _GREEN_("ok") " )");
|
||||
got_mad2 = true;
|
||||
}
|
||||
|
||||
// User supplied key
|
||||
if (got_first == false && keylen == 6) {
|
||||
if (got_mad2 == false && keylen == 6) {
|
||||
PrintAndLogEx(INFO, "Trying user specified key...");
|
||||
if (mf_read_sector(MF_MAD2_SECTOR, MF_KEY_A, userkey, (uint8_t *)&mad2_sector) != PM3_SUCCESS) {
|
||||
if (verbose) {
|
||||
@@ -7064,21 +7065,25 @@ static int CmdHF14AMfMAD(const char *Cmd) {
|
||||
}
|
||||
} else {
|
||||
PrintAndLogEx(INFO, "Authentication ( " _GREEN_("ok") " )");
|
||||
got_mad2 = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (got_mad2)
|
||||
pmad2 = &mad2_sector;
|
||||
|
||||
MADPrintHeader();
|
||||
|
||||
bool haveMAD2 = false;
|
||||
MAD1DecodeAndPrint(§or0, swapmad, verbose, &haveMAD2);
|
||||
|
||||
if (haveMAD2) {
|
||||
MAD2DecodeAndPrint(&mad2_sector, swapmad, verbose);
|
||||
if (haveMAD2 && pmad2) {
|
||||
MAD2DecodeAndPrint(pmad2, swapmad, verbose);
|
||||
}
|
||||
|
||||
if (aidlen == 2 || decodeholder) {
|
||||
mad_entry_list_t mad_list = {0};
|
||||
if (MADDecode(§or0, &mad2_sector, &mad_list, swapmad, override)) {
|
||||
if (MADDecode(§or0, pmad2, &mad_list, swapmad, override)) {
|
||||
PrintAndLogEx(ERR, "can't decode MAD");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
@@ -7155,10 +7160,12 @@ static int CmdHF14AMfMAD(const char *Cmd) {
|
||||
PrintAndLogEx(INFO, "[%d] %s", i, sprint_hex(MF_SECTOR_BLOCK(sector0, i), MFBLOCK_SIZE));
|
||||
}
|
||||
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
PrintAndLogEx(INFO, "------------ " _CYAN_("MAD v2 sector raw") " -------------");
|
||||
for (int i = 0; i < 4; i ++) {
|
||||
PrintAndLogEx(INFO, "[%d] %s", i, sprint_hex(MF_SECTOR_BLOCK(mad2_sector, i), MFBLOCK_SIZE));
|
||||
if (pmad2) {
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
PrintAndLogEx(INFO, "------------ " _CYAN_("MAD v2 sector raw") " -------------");
|
||||
for (int i = 0; i < 4; i ++) {
|
||||
PrintAndLogEx(INFO, "[%d] %s", i, sprint_hex(MF_SECTOR_BLOCK(*pmad2, i), MFBLOCK_SIZE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <string.h> // memcpy memset
|
||||
#include "ui.h"
|
||||
#include "crc.h"
|
||||
#include "mad.h"
|
||||
|
||||
#include "mifare/gallaghercore.h"
|
||||
|
||||
@@ -137,9 +138,9 @@ static bool test_mad_crc(void) {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x77, 0x88, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
// MAD v1 CRC: computed over sector0[17..47] (info byte + 15 AID pairs = 31 bytes)
|
||||
uint8_t expected_crc = sector0[16]; // 0xBD
|
||||
uint8_t computed_crc = CRC8Mad(§or0[16 + 1], 15 + 16);
|
||||
const mad1_sector_t *m = (const mad1_sector_t *)sector0;
|
||||
uint8_t expected_crc = m->mad.crc;
|
||||
uint8_t computed_crc = CRC8Mad((uint8_t *)&m->mad.info, sizeof(mad1_t) - 1);
|
||||
if (computed_crc != expected_crc) {
|
||||
PrintAndLogEx(INFO, "MAD CRC test failed: expected 0x%02X, got 0x%02X", expected_crc, computed_crc);
|
||||
return false;
|
||||
|
||||
@@ -117,14 +117,15 @@ static json_t *mad_lookup_aid(json_t *root, uint16_t aid) {
|
||||
for (uint32_t idx = 0; idx < json_array_size(root); idx++) {
|
||||
json_t *data = json_array_get(root, idx);
|
||||
if (!json_is_object(data)) {
|
||||
PrintAndLogEx(ERR, "data [%d] is not an object\n", idx);
|
||||
PrintAndLogEx(ERR, "data [%u] is not an object", idx);
|
||||
continue;
|
||||
}
|
||||
const char *fmad = mad_json_get_str(data, "mad");
|
||||
if (fmad == NULL)
|
||||
continue;
|
||||
char lfmad[strlen(fmad) + 1];
|
||||
strcpy(lfmad, fmad);
|
||||
char lfmad[16];
|
||||
strncpy(lfmad, fmad, sizeof(lfmad) - 1);
|
||||
lfmad[sizeof(lfmad) - 1] = '\0';
|
||||
str_lower(lfmad);
|
||||
if (strcmp(lmad, lfmad) == 0) {
|
||||
return data;
|
||||
@@ -154,7 +155,7 @@ static void mad_print_aid_verbose(json_t *elm) {
|
||||
const char *provider = mad_json_get_str(elm, "service_provider");
|
||||
const char *integrator = mad_json_get_str(elm, "system_integrator");
|
||||
|
||||
PrintAndLogEx(SUCCESS, " MAD................. %s", vmad);
|
||||
PrintAndLogEx(SUCCESS, " MAD................. %s", vmad ? vmad : "n/a");
|
||||
if (application) {
|
||||
PrintAndLogEx(SUCCESS, " Application......... %s", application);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user