Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move and Rename Static Mifare Classic Write Block Function #2626

Merged
merged 3 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions client/src/cmdhfmf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -6784,9 +6769,9 @@ int CmdHFMFNDEFFormat(const char *Cmd) {
}

// 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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions client/src/mifare/mifarehost.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
3 changes: 3 additions & 0 deletions client/src/mifare/mifarehost.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading