From 5589cbf847889106940330337265aa061a736c9a Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 29 Aug 2026 14:31:34 +0200 Subject: [PATCH] OLD/MIX convert to NG: Phase C5 --- armsrc/appmain.c | 11 ++++++++++- armsrc/mifarecmd.c | 25 +++++++++++++------------ armsrc/mifarecmd.h | 2 +- client/src/cmdhfmf.c | 6 +++--- client/src/mifare/mifarehost.c | 29 ++++++++++++++++++++--------- include/pm3_cmd.h | 21 +++++++++++++++++++++ 6 files changed, 68 insertions(+), 26 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 980380568..e07016406 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -2339,7 +2339,16 @@ static void PacketReceived(PacketCommandNG *packet) { break; } case CMD_HF_MIFARE_CHKKEYS_FAST: { - MifareChkKeys_fast(packet->oldarg[0], packet->oldarg[1], packet->oldarg[2], packet->data.asBytes); + if (packet->length < sizeof(mf_chkkeys_fast_t)) { + reply_ng(CMD_HF_MIFARE_CHKKEYS_FAST, PM3_EINVARG, NULL, 0); + break; + } + mf_chkkeys_fast_t *payload = (mf_chkkeys_fast_t *)packet->data.asBytes; + if ((payload->key_count * 6) > (packet->length - sizeof(mf_chkkeys_fast_t))) { + reply_ng(CMD_HF_MIFARE_CHKKEYS_FAST, PM3_EINVARG, NULL, 0); + break; + } + MifareChkKeys_fast(payload); break; } case CMD_HF_MIFARE_CHKKEYS_FILE: { diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index edecc2271..15470dc43 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -2100,16 +2100,17 @@ static void chkKey_loopBonly(struct chk_t *c, struct sector_t *k_sector, uint8_t // arg1 = clear trace // arg2 = antal nycklar i keychunk // datain = keys as array -void MifareChkKeys_fast(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain) { +void MifareChkKeys_fast(const mf_chkkeys_fast_t *payload) { // first call or - uint8_t sectorcnt = arg0 & 0xFF; // 16; - uint8_t firstchunk = (arg0 >> 8) & 0xF; - uint8_t lastchunk = (arg0 >> 12) & 0xF; - uint16_t singleSectorParams = (arg0 >> 16) & 0xFFFF; - uint8_t strategy = arg1 & 0xFF; - uint8_t use_flashmem = (arg1 >> 8) & 0xFF; - uint16_t keyCount = arg2 & 0xFF; + uint8_t sectorcnt = payload->sectorcnt; + uint8_t firstchunk = payload->first_chunk; + uint8_t lastchunk = payload->last_chunk; + uint16_t singleSectorParams = payload->singlesector_params; + uint8_t strategy = payload->strategy; + uint8_t use_flashmem = payload->use_flashmemory; + uint16_t keyCount = payload->key_count; + const uint8_t *datain = payload->keys; uint8_t status = 0; bool singleSectorMode = (singleSectorParams >> 15) & 1; uint8_t keytype = (singleSectorParams >> 8) & 1; @@ -2233,11 +2234,11 @@ void MifareChkKeys_fast(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *da chk_data.key = bytes_to_num(datain + (i * MF_KEY_LENGTH), MF_KEY_LENGTH); if (chkKey(&chk_data) == 0) { foundkeys++; - reply_old(CMD_ACK, 1, 0, 0, datain + (i * MF_KEY_LENGTH), MF_KEY_LENGTH); + reply_ng(CMD_HF_MIFARE_CHKKEYS_FAST, 1, datain + (i * MF_KEY_LENGTH), MF_KEY_LENGTH); goto out; } } - reply_mix(CMD_ACK, 0, 0, 0, 0, 0); + reply_ng(CMD_HF_MIFARE_CHKKEYS_FAST, 0, NULL, 0); out: LEDsoff(); crypto1_deinit(pcs); @@ -2470,7 +2471,7 @@ OUT: tmp[488] = bar & 0xFF; tmp[489] = bar >> 8 & 0xFF; - reply_old(CMD_ACK, foundkeys, 0, 0, tmp, 480 + 10); + reply_ng(CMD_HF_MIFARE_CHKKEYS_FAST, foundkeys, tmp, 480 + 10); set_tracing(false); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); @@ -2503,7 +2504,7 @@ OUT: } } else { // partial/none keys found - reply_mix(CMD_ACK, foundkeys, 0, 0, 0, 0); + reply_ng(CMD_HF_MIFARE_CHKKEYS_FAST, foundkeys, NULL, 0); } g_dbglevel = oldbg; diff --git a/armsrc/mifarecmd.h b/armsrc/mifarecmd.h index 7827f805e..fb0181465 100644 --- a/armsrc/mifarecmd.h +++ b/armsrc/mifarecmd.h @@ -41,7 +41,7 @@ void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, int MifareAcquireStaticEncryptedNonces(uint32_t flags, const uint8_t *key, bool reply, uint8_t first_block_no, uint8_t first_key_type); void MifareAcquireNonces(uint32_t arg0, uint32_t flags); void MifareChkKeys(uint8_t *datain, uint8_t reserved_mem); -void MifareChkKeys_fast(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain); +void MifareChkKeys_fast(const mf_chkkeys_fast_t *payload); void MifareChkKeys_file(uint8_t *fn); int MifareECardLoad(uint8_t sectorcnt, uint8_t keytype, uint8_t *key); diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index ce818cfd6..786b8808d 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -3395,7 +3395,7 @@ static int CmdHF14AMfAutoPWN(const char *Cmd) { res = mf_check_keys_fast(sector_cnt, true, true, 1, key_cnt, keyBlock, e_sector, use_flashmemory, verbose); } else { - uint32_t chunksize = key_cnt > (PM3_CMD_DATA_SIZE / MIFARE_KEY_SIZE) ? (PM3_CMD_DATA_SIZE / MIFARE_KEY_SIZE) : key_cnt; + uint32_t chunksize = key_cnt > MFC_CHKKEYS_FAST_MAX_KEYS ? MFC_CHKKEYS_FAST_MAX_KEYS : key_cnt; bool firstChunk = true, lastChunk = false; for (uint8_t strategy = 1; strategy < 3; strategy++) { @@ -4045,7 +4045,7 @@ static int CmdHF14AMfChk_fast(const char *Cmd) { return PM3_EMALLOC; } - uint32_t chunksize = (keycnt > (PM3_CMD_DATA_SIZE / MIFARE_KEY_SIZE)) ? (PM3_CMD_DATA_SIZE / MIFARE_KEY_SIZE) : keycnt; + uint32_t chunksize = (keycnt > MFC_CHKKEYS_FAST_MAX_KEYS) ? MFC_CHKKEYS_FAST_MAX_KEYS : keycnt; bool firstChunk = true, lastChunk = false; int i = 0; @@ -4240,7 +4240,7 @@ static int CmdHF14AMfSmartBrute(const char *Cmd) { return PM3_EINVARG; } - uint32_t chunksize = 100 > (PM3_CMD_DATA_SIZE / MIFARE_KEY_SIZE) ? (PM3_CMD_DATA_SIZE / MIFARE_KEY_SIZE) : 100; + uint32_t chunksize = 100 > MFC_CHKKEYS_FAST_MAX_KEYS ? MFC_CHKKEYS_FAST_MAX_KEYS : 100; uint8_t *keyBlock = calloc(MIFARE_KEY_SIZE, chunksize); if (keyBlock == NULL) diff --git a/client/src/mifare/mifarehost.c b/client/src/mifare/mifarehost.c index 9d2cad94e..517d74b69 100644 --- a/client/src/mifare/mifarehost.c +++ b/client/src/mifare/mifarehost.c @@ -261,19 +261,30 @@ int mf_check_keys_fast_ex(uint8_t sectorsCnt, uint8_t firstChunk, uint8_t lastCh uint64_t t2 = msclock(); + if (size > MFC_CHKKEYS_FAST_MAX_KEYS) { + PrintAndLogEx(WARNING, "chunk of %u keys is too large for one frame, max %zu", size, (size_t)MFC_CHKKEYS_FAST_MAX_KEYS); + return PM3_EOUTOFBOUND; + } + + uint8_t buf[PM3_CMD_DATA_SIZE] = {0}; + mf_chkkeys_fast_t *payload = (mf_chkkeys_fast_t *)buf; + payload->sectorcnt = sectorsCnt; + payload->first_chunk = firstChunk; + payload->last_chunk = lastChunk; + payload->strategy = strategy; + payload->use_flashmemory = use_flashmemory; + payload->key_count = size; + payload->singlesector_params = singleSectorParams; + memcpy(payload->keys, keyBlock, MIFARE_KEY_SIZE * size); + // send keychunk clearCommandBuffer(); - SendCommandOLD(CMD_HF_MIFARE_CHKKEYS_FAST - , (sectorsCnt | (firstChunk << 8) | (lastChunk << 12) | (singleSectorParams << 16)) - , ((use_flashmemory << 8) | strategy) - , size - , keyBlock - , (MIFARE_KEY_SIZE * size) - ); + SendCommandNG(CMD_HF_MIFARE_CHKKEYS_FAST, buf, sizeof(mf_chkkeys_fast_t) + (MIFARE_KEY_SIZE * size)); + PacketResponseNG resp; uint32_t timeout = 0; - while (WaitForResponseTimeout(CMD_ACK, &resp, 2000) == false) { + while (WaitForResponseTimeout(CMD_HF_MIFARE_CHKKEYS_FAST, &resp, 2000) == false) { while (kbd_enter_pressed()) { SendCommandNG(CMD_BREAK_LOOP, NULL, 0); @@ -304,7 +315,7 @@ int mf_check_keys_fast_ex(uint8_t sectorsCnt, uint8_t firstChunk, uint8_t lastCh PrintAndLogEx(NORMAL, ""); } // time to convert the returned data. - uint8_t curr_keys = resp.oldarg[0]; + uint8_t curr_keys = (uint8_t)resp.status; if ((singleSectorParams >> 15) & 1) { diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 69b864114..c42b307f2 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -399,6 +399,27 @@ typedef struct { uint8_t key[6]; } PACKED mf_readsector_t; +// CMD_HF_MIFARE_CHKKEYS_FAST payload. +// Replaces three bit-packed oldargs: +// arg0 = sectorcnt | firstchunk<<8 | lastchunk<<12 | singlesector_params<<16 +// arg1 = strategy | use_flashmemory<<8 +// arg2 = key count +// The key count is carried explicitly rather than derived, so a short frame is +// caught instead of silently checking fewer keys. +typedef struct { + uint8_t sectorcnt; + uint8_t first_chunk; + uint8_t last_chunk; + uint8_t strategy; + uint8_t use_flashmemory; + uint8_t key_count; + uint16_t singlesector_params; + uint8_t keys[]; // key_count * MIFARE_KEY_SIZE +} PACKED mf_chkkeys_fast_t; + +// most keys that fit alongside the header in one frame +#define MFC_CHKKEYS_FAST_MAX_KEYS ((PM3_CMD_DATA_SIZE - sizeof(mf_chkkeys_fast_t)) / 6) + // CMD_HF_MIFARE_VALUE payload. // Replaces a 34 byte blob addressed by hardcoded offsets: // arg0/1/2 = blockno / keytype / transfer keytype