Merge pull request #2626 from pingu2211/hf-mifare-refacor

Move and Rename Static Mifare Classic Write Block Function
This commit is contained in:
Iceman 2024-11-10 14:47:34 +01:00 committed by GitHub
commit e676f15b19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 22 deletions

View file

@ -45,6 +45,7 @@
#include "mifare/gen4.h"
#include "generator.h" // keygens.
#include "fpga.h"
#include "mifare/mifarehost.h"
static int CmdHelp(const char *Cmd);
@ -494,22 +495,6 @@ void mf_print_sector_hdr(uint8_t sector) {
PrintAndLogEx(INFO, "----+-------------------------------------------------+-----------------");
}
static bool mf_write_block(const uint8_t *key, uint8_t keytype, uint8_t blockno, uint8_t *block) {
uint8_t data[26];
memcpy(data, key, MIFARE_KEY_SIZE);
memcpy(data + 10, block, MFBLOCK_SIZE);
clearCommandBuffer();
SendCommandMIX(CMD_HF_MIFARE_WRITEBL, blockno, keytype, 0, data, sizeof(data));
PacketResponseNG resp;
if (WaitForResponseTimeout(CMD_ACK, &resp, 1500) == false) {
PrintAndLogEx(FAILED, "command execution time out");
return false;
}
return ((resp.oldarg[0] & 0xff) == 1);
}
// assumes n is in number of blocks 0..255
static void mf_analyse_acl(uint16_t n, uint8_t *d) {
@ -6784,9 +6769,9 @@ skipfile:
}
// write to card, try B key first
if (mf_write_block(keyB[i], MF_KEY_B, b, block) == 0) {
if (mfWriteBlock(b, MF_KEY_B, keyB[i], block) != PM3_SUCCESS) {
// try A key,
if (mf_write_block(keyA[i], MF_KEY_A, b, block) == 0) {
if (mfWriteBlock(b, MF_KEY_A, keyA[i], block) != PM3_SUCCESS) {
return PM3_EFAILED;
}
}
@ -7027,10 +7012,11 @@ int CmdHFMFNDEFWrite(const char *Cmd) {
}
// write to card, try B key first
if (mf_write_block(g_mifare_default_key, MF_KEY_B, block_no, block) == 0) {
if (mfWriteBlock(block_no, MF_KEY_B, g_mifare_default_key, block) != PM3_SUCCESS) {
// try A key,
if (mf_write_block(g_mifare_ndef_key, MF_KEY_A, block_no, block) == 0) {
if (mfWriteBlock(block_no, MF_KEY_A, g_mifare_ndef_key, block) != PM3_SUCCESS) {
return PM3_EFAILED;
}
}
@ -9507,8 +9493,8 @@ static int CmdHFMFHidEncode(const char *Cmd) {
PrintAndLogEx(INFO, "Writing %u - %s", (i + 1), sprint_hex_inrow(blocks + (i * MFBLOCK_SIZE), MFBLOCK_SIZE));
}
if (mf_write_block(empty, MF_KEY_A, (i + 1), blocks + (i * MFBLOCK_SIZE)) == false) {
if (mf_write_block(empty, MF_KEY_B, (i + 1), blocks + (i * MFBLOCK_SIZE)) == false) {
if (mfWriteBlock((i + 1), MF_KEY_A, empty, blocks + (i * MFBLOCK_SIZE)) == PM3_EFAILED) {
if (mfWriteBlock((i + 1), MF_KEY_B, empty, blocks + (i * MFBLOCK_SIZE)) == PM3_EFAILED) {
PrintAndLogEx(WARNING, "failed writing block %d using default empty key", (i + 1));
res = false;
break;

View file

@ -994,6 +994,34 @@ int mfReadBlock(uint8_t blockNo, uint8_t keyType, const uint8_t *key, uint8_t *d
return PM3_SUCCESS;
}
int mfWriteBlock(uint8_t blockno, uint8_t keyType, const uint8_t *key, uint8_t *block) {
uint8_t data[26];
memcpy(data, key, MIFARE_KEY_SIZE);
memcpy(data + 10, block, MFBLOCK_SIZE);
clearCommandBuffer();
SendCommandMIX(CMD_HF_MIFARE_WRITEBL, blockno, keytype, 0, data, sizeof(data));
PacketResponseNG resp;
if (WaitForResponseTimeout(CMD_ACK, &resp, 1500) == false) {
PrintAndLogEx(FAILED, "mfWriteBlock execution time out");
return PM3_ETIMEOUT;
}
return ((resp.oldarg[0] & 0xff) == 1)?PM3_SUCCESS:PM3_EFAILED;
}
int mfWriteSector(uint8_t sectorNo, uint8_t keyType, const uint8_t *key, uint8_t *sector){
int res;
for (int i=0;i<4; i++){
res = mfWriteBlock((sectorNo*4)+i, keyType, key, sector+(i*MFBLOCK_SIZE));
if (res != PM3_SUCCESS){
return (i==0)?PM3_EFAILED:PM3_EPARTIAL;
}
}
return PM3_SUCCESS;
}
// EMULATOR
int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {

View file

@ -88,6 +88,9 @@ int mfKeyBrute(uint8_t blockNo, uint8_t keyType, const uint8_t *key, uint64_t *r
int mfReadSector(uint8_t sectorNo, uint8_t keyType, const uint8_t *key, uint8_t *data);
int mfReadBlock(uint8_t blockNo, uint8_t keyType, const uint8_t *key, uint8_t *data);
int mfWriteBlock(uint8_t blockno, uint8_t keyType, const uint8_t *key, uint8_t *block);
int mfWriteSector(uint8_t sectorNo, uint8_t keyType, const uint8_t *key, uint8_t *sector);
int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount);
int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount);
int mfEmlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth);