diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 658318018..980380568 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -2281,7 +2281,11 @@ static void PacketReceived(PacketCommandNG *packet) { break; } case CMD_HF_MIFARE_VALUE: { - MifareValue(packet->oldarg[0], packet->oldarg[1], packet->oldarg[2], packet->data.asBytes); + if (packet->length != sizeof(mf_value_t)) { + reply_ng(CMD_HF_MIFARE_VALUE, PM3_EINVARG, NULL, 0); + break; + } + MifareValue((mf_value_t *)packet->data.asBytes); break; } case CMD_HF_MIFAREU_WRITEBL: { diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index a485d2ba4..edecc2271 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -668,23 +668,21 @@ void MifareUReadCard(mful_readblock_t *packet) { set_tracing(false); } -void MifareValue(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) { +void MifareValue(const mf_value_t *payload) { // params - uint8_t blockNo = arg0; - uint8_t keyType = arg1; - uint8_t transferKeyType = arg2; - uint64_t ui64Key = 0; - uint64_t transferUi64Key = 0; + uint8_t blockNo = payload->blockno; + uint8_t keyType = payload->keytype; + uint8_t transferKeyType = payload->transfer_keytype; uint8_t blockdata[16] = {0x00}; - ui64Key = bytes_to_num(datain, 6); - memcpy(blockdata, datain + 11, 16); - transferUi64Key = bytes_to_num(datain + 27, 6); + uint64_t ui64Key = bytes_to_num(payload->key, 6); + uint64_t transferUi64Key = bytes_to_num(payload->transfer_key, 6); + memcpy(blockdata, payload->blockdata, sizeof(blockdata)); // variables - uint8_t action = datain[9]; - uint8_t transferBlk = datain[10]; - bool needAuth = datain[33]; + uint8_t action = payload->action; + uint8_t transferBlk = payload->transfer_blockno; + bool needAuth = payload->need_auth; uint8_t isOK = 0; uint8_t uid[10] = {0x00}; uint32_t cuid = 0; @@ -748,7 +746,7 @@ void MifareValue(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) { if (g_dbglevel >= DBG_INFO) DbpString("WRITE BLOCK FINISHED"); - reply_mix(CMD_ACK, isOK, 0, 0, 0, 0); + reply_ng(CMD_HF_MIFARE_VALUE, (isOK) ? PM3_SUCCESS : PM3_EUNDEF, NULL, 0); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); LEDsoff(); diff --git a/armsrc/mifarecmd.h b/armsrc/mifarecmd.h index 89f817b05..7827f805e 100644 --- a/armsrc/mifarecmd.h +++ b/armsrc/mifarecmd.h @@ -23,7 +23,7 @@ int16_t mifare_cmd_readblocks(MifareWakeupType wakeup, uint8_t key_auth_cmd, uint8_t *key, uint8_t read_cmd, uint8_t block_no, uint8_t count, uint8_t *block_data); int16_t mifare_cmd_writeblocks(MifareWakeupType wakeup, uint8_t key_auth_cmd, uint8_t *key, uint8_t write_cmd, uint8_t block_no, uint8_t count, uint8_t *block_data); void MifareReadSector(uint8_t sector_no, uint8_t key_type, uint8_t *key); -void MifareValue(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain); +void MifareValue(const mf_value_t *payload); void MifareUReadBlock(mful_readblock_t *packet); void MifareUReadCard(mful_readblock_t *packet); diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index 50807788c..ce818cfd6 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -10926,9 +10926,14 @@ static int CmdHF14AMfValue(const char *Cmd) { uint8_t block[MFBLOCK_SIZE] = {0x00}; memcpy(block, (uint8_t *)&value, 4); - uint8_t cmddata[34]; - memcpy(cmddata, key, sizeof(key)); - // Key == 6 data went to 10, so lets offset 9 for inc/dec + mf_value_t payload = { + .blockno = blockno, + .keytype = keytype, + .transfer_keytype = transferkeytype, + // 00 if increment, 01 if decrement, 02 if restore + .action = action, + }; + memcpy(payload.key, key, sizeof(key)); if (action == 0) { PrintAndLogEx(INFO, "Value incremented by : %d", (int32_t)value); @@ -10937,22 +10942,19 @@ static int CmdHF14AMfValue(const char *Cmd) { PrintAndLogEx(INFO, "Value decremented by : %d", (int32_t)value); } - // 00 if increment, 01 if decrement, 02 if restore - cmddata[9] = action; - if (trnval != -1) { // transfer to block - cmddata[10] = trnval; + payload.transfer_blockno = trnval; - memcpy(cmddata + 27, transferkey, sizeof(transferkey)); + memcpy(payload.transfer_key, transferkey, sizeof(transferkey)); if (mfSectorNum(trnval) != mfSectorNum(blockno)) { - cmddata[33] = 1; // should send nested auth + payload.need_auth = 1; // should send nested auth } PrintAndLogEx(INFO, "Transfer block no %u to block %" PRId64, blockno, trnval); } else { - cmddata[10] = 0; + payload.transfer_blockno = 0; if (keytype < 2) { PrintAndLogEx(INFO, "Writing block no %u, key type:%c - %s", blockno, (keytype == MF_KEY_B) ? 'B' : 'A', sprint_hex_inrow(key, sizeof(key))); } else { @@ -10960,17 +10962,17 @@ static int CmdHF14AMfValue(const char *Cmd) { } } - memcpy(cmddata + 11, block, sizeof(block)); + memcpy(payload.blockdata, block, sizeof(block)); clearCommandBuffer(); - SendCommandMIX(CMD_HF_MIFARE_VALUE, blockno, keytype, transferkeytype, cmddata, sizeof(cmddata)); + SendCommandNG(CMD_HF_MIFARE_VALUE, (uint8_t *)&payload, sizeof(payload)); PacketResponseNG resp; - if (WaitForResponseTimeout(CMD_ACK, &resp, 1500) == false) { + if (WaitForResponseTimeout(CMD_HF_MIFARE_VALUE, &resp, 1500) == false) { PrintAndLogEx(FAILED, "command execution time out"); return PM3_ETIMEOUT; } - isok = resp.oldarg[0] & 0xff; + isok = (resp.status == PM3_SUCCESS); } else { // set value // To set a value block (or setup) we can use the normal mifare classic write block // So build the command options can call CMD_HF_MIFARE_WRITEBL diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index a5bd7e78c..69b864114 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -399,6 +399,23 @@ typedef struct { uint8_t key[6]; } PACKED mf_readsector_t; +// CMD_HF_MIFARE_VALUE payload. +// Replaces a 34 byte blob addressed by hardcoded offsets: +// arg0/1/2 = blockno / keytype / transfer keytype +// [0..5] key, [9] action, [10] transfer block, [11..26] block data, +// [27..32] transfer key, [33] nested auth flag +typedef struct { + uint8_t blockno; + uint8_t keytype; + uint8_t transfer_keytype; + uint8_t action; // 0 increment, 1 decrement, 2 restore + uint8_t transfer_blockno; + uint8_t need_auth; // nested auth needed when transferring across sectors + uint8_t key[6]; + uint8_t transfer_key[6]; + uint8_t blockdata[16]; +} PACKED mf_value_t; + // CMD_HF_MIFARE_CSETBL / CMD_HF_MIFARE_CGETBL payload. // data[] carries the block to write for CSETBL, and is empty for CGETBL. typedef struct {