diff --git a/armsrc/BigBuf.h b/armsrc/BigBuf.h index fa763cf22..d690b4cd5 100644 --- a/armsrc/BigBuf.h +++ b/armsrc/BigBuf.h @@ -22,7 +22,10 @@ #include "common.h" #define MAX_FRAME_SIZE 256 // maximum allowed ISO14443 frame -#define MAX_PARITY_SIZE ((MAX_FRAME_SIZE + 7) / 8) +// The 14a demodulator stores 8 parity bits per 8 received bytes and then flushes +// one more byte once the receive buffer is full, so a full MAX_FRAME_SIZE frame +// needs (MAX_FRAME_SIZE / 8) + 1 parity bytes, not (MAX_FRAME_SIZE + 7) / 8 +#define MAX_PARITY_SIZE ((MAX_FRAME_SIZE / 8) + 1) #define MAX_MIFARE_FRAME_SIZE 19 // biggest Mifare frame is UL AES answer to AUTH (1 + 16 Bytes) + 2 Bytes CRC #define MAX_MIFARE_PARITY_SIZE 3 // need 19 parity bits for the 19 Byte above. 3 Bytes are enough to store these #define CARD_MEMORY_SIZE 4096 diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 27dc36962..beeb956f1 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -2632,12 +2632,11 @@ static void PacketReceived(PacketCommandNG *packet) { break; } case CMD_HF_MIFARE_CIDENT: { - struct p { - uint8_t is_mfc; - uint8_t keytype; - uint8_t key[6]; - } PACKED; - struct p *payload = (struct p *) packet->data.asBytes; + if (packet->length < sizeof(mf_chinese_ident_t)) { + reply_ng(CMD_HF_MIFARE_CIDENT, PM3_EINVARG, NULL, 0); + break; + } + mf_chinese_ident_t *payload = (mf_chinese_ident_t *)packet->data.asBytes; MifareCIdent(payload->is_mfc, payload->keytype, payload->key); break; } diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 5c94e8e5b..344666f02 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -600,7 +600,9 @@ void Demod14aReset(void) { } void Demod14aInit(uint8_t *d, uint16_t n, uint8_t *par) { - Demod.output_len = n; + // No ISO14443-A frame can be longer than MAX_FRAME_SIZE (FSD/FSC max out at 256 bytes), + // while callers happily pass PM3_CMD_DATA_SIZE sized receive buffers + Demod.output_len = MIN(n, MAX_FRAME_SIZE); Demod.output = d; Demod.parity = par; Demod14aReset(); @@ -718,9 +720,11 @@ RAMFUNC int ManchesterDecoding(uint8_t bit, uint16_t offset, uint32_t non_real_t RAMFUNC int ManchesterDecoding_Thinfilm(uint8_t bit, uint32_t non_real_time) { if (Demod.len == Demod.output_len) { - // Flush last parity bits - Demod.parityBits <<= (8 - (Demod.len & 0x0007)); // left align remaining parity bits - Demod.parity[Demod.parityLen++] = Demod.parityBits; // and store them + // Flush last parity bits. Thinfilm has no parity bits and passes a NULL buffer + if (Demod.parity != NULL) { + Demod.parityBits <<= (8 - (Demod.len & 0x0007)); // left align remaining parity bits + Demod.parity[Demod.parityLen++] = Demod.parityBits; // and store them + } return true; } diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index 7ae30964b..07708b964 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -3274,9 +3274,17 @@ void MifareCIdent(bool is_mfc, uint8_t keytype, uint8_t *key) { uint8_t superGen1[9] = {0x0A, 0x00, 0x00, 0xA6, 0xB0, 0x00, 0x10, 0x14, 0x1D}; uint8_t uid[10]; uint8_t *par = BigBuf_calloc(MAX_PARITY_SIZE); - uint8_t *buf = BigBuf_calloc(PM3_CMD_DATA_SIZE); + uint8_t *buf = BigBuf_calloc(MAX_FRAME_SIZE); iso14a_card_select_t *card = (iso14a_card_select_t *) BigBuf_calloc(sizeof(iso14a_card_select_t)); + if (par == NULL || buf == NULL || card == NULL) { + if (g_dbglevel >= DBG_ERROR) DbpString("Magic ident: failed to allocate buffers"); + reply_ng(CMD_HF_MIFARE_CIDENT, PM3_EMALLOC, NULL, 0); + switch_off(); + BigBuf_free(); + return; + } + bool isGen2 = false; uint16_t flag = MAGIC_FLAG_NONE; uint32_t cuid = 0; @@ -3297,13 +3305,13 @@ void MifareCIdent(bool is_mfc, uint8_t keytype, uint8_t *key) { // check for GDM config ReaderTransmit(gen4gdmGetConf, sizeof(gen4gdmGetConf), NULL); - res = ReaderReceive(buf, PM3_CMD_DATA_SIZE, par); + res = ReaderReceive(buf, MAX_FRAME_SIZE, par); if (res > 1) { // could be ZUID or full USCUID, the magic blocks don't exist on ZUID so // a failure here indicates a feature limited chip like ZUID // check for GDM hidden block read ReaderTransmit(gen4gdmGetMagicBlock, sizeof(gen4gdmGetMagicBlock), NULL); - res = ReaderReceive(buf, PM3_CMD_DATA_SIZE, par); + res = ReaderReceive(buf, MAX_FRAME_SIZE, par); if (res > 1) { flag |= MAGIC_FLAG_GDM_WUP_40; } else { @@ -3320,7 +3328,7 @@ void MifareCIdent(bool is_mfc, uint8_t keytype, uint8_t *key) { // Get config should return 30 or 32 bytes AddCrc14A(gen4GetConf, sizeof(gen4GetConf) - 2); ReaderTransmit(gen4GetConf, sizeof(gen4GetConf), NULL); - res = ReaderReceive(buf, PM3_CMD_DATA_SIZE, par); + res = ReaderReceive(buf, MAX_FRAME_SIZE, par); if (res == 32 || res == 34) { flag |= MAGIC_FLAG_GEN_4GTU; } @@ -3368,7 +3376,7 @@ void MifareCIdent(bool is_mfc, uint8_t keytype, uint8_t *key) { // test for super card ReaderTransmit(superGen1, sizeof(superGen1), NULL); - res = ReaderReceive(buf, PM3_CMD_DATA_SIZE, par); + res = ReaderReceive(buf, MAX_FRAME_SIZE, par); if (res == 22) { uint8_t isGen = MAGIC_FLAG_SUPER_GEN1; @@ -3379,7 +3387,7 @@ void MifareCIdent(bool is_mfc, uint8_t keytype, uint8_t *key) { res = iso14443a_select_card(uid, NULL, &cuid, true, 0, true); if (res) { ReaderTransmit(rdbl00, sizeof(rdbl00), NULL); - res = ReaderReceive(buf, PM3_CMD_DATA_SIZE, par); + res = ReaderReceive(buf, MAX_FRAME_SIZE, par); if (res == 18) { isGen = MAGIC_FLAG_SUPER_GEN2; } @@ -3395,7 +3403,7 @@ void MifareCIdent(bool is_mfc, uint8_t keytype, uint8_t *key) { res = iso14443a_select_card(uid, NULL, &cuid, true, 0, true); if (res == 2) { ReaderTransmit(rdblf0, sizeof(rdblf0), NULL); - res = ReaderReceive(buf, PM3_CMD_DATA_SIZE, par); + res = ReaderReceive(buf, MAX_FRAME_SIZE, par); if (res == 18) { flag |= MAGIC_FLAG_NTAG21X; } @@ -3420,7 +3428,7 @@ void MifareCIdent(bool is_mfc, uint8_t keytype, uint8_t *key) { uint64_t tmpkey = bytes_to_num(key, 6); if (mifare_classic_authex(pcs, cuid, 0, keytype, tmpkey, AUTH_FIRST, NULL, NULL) == 0) { - if ((mifare_sendcmd_short(pcs, 1, ISO14443A_CMD_WRITEBLOCK, 0, buf, PM3_CMD_DATA_SIZE, par, NULL) == 1) && (buf[0] == 0x0A)) { + if ((mifare_sendcmd_short(pcs, 1, ISO14443A_CMD_WRITEBLOCK, 0, buf, MAX_FRAME_SIZE, par, NULL) == 1) && (buf[0] == 0x0A)) { flag |= MAGIC_FLAG_GEN_2; // turn off immediately to ensure nothing ever accidentally writes to the block FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); @@ -3436,7 +3444,7 @@ void MifareCIdent(bool is_mfc, uint8_t keytype, uint8_t *key) { res = iso14443a_select_card(uid, NULL, &cuid, true, 0, true); if (res) { ReaderTransmit(rdbl00, sizeof(rdbl00), NULL); - res = ReaderReceive(buf, PM3_CMD_DATA_SIZE, par); + res = ReaderReceive(buf, MAX_FRAME_SIZE, par); if (res == 18) { flag |= MAGIC_FLAG_GEN_3; } @@ -3448,7 +3456,7 @@ void MifareCIdent(bool is_mfc, uint8_t keytype, uint8_t *key) { res = iso14443a_select_card(uid, NULL, &cuid, true, 0, true); if (res) { ReaderTransmit(gen4gdmAuth, sizeof(gen4gdmAuth), NULL); - res = ReaderReceive(buf, PM3_CMD_DATA_SIZE, par); + res = ReaderReceive(buf, MAX_FRAME_SIZE, par); if (res == 4) { flag |= MAGIC_FLAG_GDM_AUTH; } @@ -3765,6 +3773,10 @@ int DoGen3Cmd(uint8_t *cmd, uint8_t cmd_len) { int retval = PM3_SUCCESS; uint8_t *par = BigBuf_calloc(MAX_PARITY_SIZE); uint8_t *buf = BigBuf_calloc(PM3_CMD_DATA_SIZE); + if (par == NULL || buf == NULL) { + if (g_dbglevel >= DBG_ERROR) DbpString("Gen3 cmd: failed to allocate buffers"); + return PM3_EMALLOC; + } LED_B_ON(); uint32_t save_iso14a_timeout = iso14a_get_timeout(); diff --git a/client/src/cmdhfmfu.c b/client/src/cmdhfmfu.c index e9b5c3fe8..60c48719f 100644 --- a/client/src/cmdhfmfu.c +++ b/client/src/cmdhfmfu.c @@ -1678,8 +1678,11 @@ static uint64_t ul_magic_test(void) { // check for GEN1A, GEN1B and NTAG21x PacketResponseNG resp; clearCommandBuffer(); - uint8_t payload[] = { 0 }; - SendCommandNG(CMD_HF_MIFARE_CIDENT, payload, sizeof(payload)); + mf_chinese_ident_t payload = { + .is_mfc = false, + .keytype = MF_KEY_A, + }; + SendCommandNG(CMD_HF_MIFARE_CIDENT, (uint8_t *)&payload, sizeof(payload)); uint16_t is_generation = MAGIC_FLAG_NONE; if (WaitForResponseTimeout(CMD_HF_MIFARE_CIDENT, &resp, 1500)) { diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 4c87dc52f..fe8f52a03 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -598,6 +598,15 @@ typedef struct { uint8_t data[]; } PACKED mf_chinese_blk_t; +// CMD_HF_MIFARE_CIDENT payload. +// key/keytype are only read when is_mfc is set, but the device reads the whole +// struct, so senders must always transmit all of it +typedef struct { + uint8_t is_mfc; + uint8_t keytype; + uint8_t key[6]; +} PACKED mf_chinese_ident_t; + // CMD_HF_MIFARE_GEN3UID payload typedef struct { uint8_t uidlen; @@ -1266,6 +1275,15 @@ typedef struct { (len == 4 ? FLAG_4B_UID_IN_DATA : (len == 7 ? FLAG_7B_UID_IN_DATA : (len == 10 ? FLAG_10B_UID_IN_DATA : FLAG_UID_IN_EMUL)))) #define IS_FLAG_UID_IN_EMUL(flags) ((flags & FLAG_MASK_UID) == FLAG_UID_IN_EMUL) +// CMD_HF_MIFARE_EML_MEMSET flags +// a whole-dump upload sets this on its first chunk so the device zeroes what was +// there before writing. A partial update (esetblk) never sets it. +#define MFEMUL_MEMSET_CLEAR 0x01 + +// "hf 14a antifuzz" - which flavour of anticollision abuse to run +#define ANTIFUZZ_MODE_CASCADE 0x00 // grow the reader's UID past cascade level 3 +#define ANTIFUZZ_MODE_COLLISION 0x01 // every answered bit is a collision + // internal constants, use the function macros instead #define MIFARE_4K_MAX_BYTES 4096 #define MIFARE_2K_MAX_BYTES 2048