From 71d670d5b2d8e54da4dd5cb72d162e31ae164b53 Mon Sep 17 00:00:00 2001 From: merlokk Date: Wed, 20 Sep 2017 14:13:26 +0300 Subject: [PATCH] blocks number and sector trails calculates correctly --- armsrc/mifarecmd.c | 60 ++++++++++++++++++++++++++++++++++++++------- client/cmdhfmf.c | 28 ++++++++++----------- client/mifarehost.c | 2 +- client/mifarehost.h | 2 +- 4 files changed, 67 insertions(+), 25 deletions(-) diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index 3f1d7706..e35c776b 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -1171,19 +1171,31 @@ void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai // //----------------------------------------------------------------------------- +bool isBlockTrailer(int blockN) { + if (blockN >= 0 && blockN < 128) { + return ((blockN & 0x03) == 0x03); + } + if (blockN >= 128 && blockN <= 256) { + return ((blockN & 0x0F) == 0x0F); + } + return FALSE; +} + void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){ // var byte_t isOK = 0; - uint8_t numSectors = arg0; + uint32_t numBlocks = arg0; uint8_t needWipe = arg1; uint8_t needFill = arg2; + bool gen1b = FALSE; uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE]; uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE]; -// uint8_t block0[16] = {0x01, 0x02, 0x03, 0x04, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; -// uint8_t block1[16] = {0x00}; -// uint8_t blockK[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + uint8_t block0[16] = {0x01, 0x02, 0x03, 0x04, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + uint8_t block1[16] = {0x00}; + uint8_t blockK[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + uint8_t d_block[18] = {0x00}; // card commands uint8_t wupC1[] = { 0x40 }; @@ -1223,10 +1235,10 @@ void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){ // put default data if (needFill){ // select commands - ReaderTransmitBitsPar(wupC1,7,0, NULL); + ReaderTransmitBitsPar(wupC1, 7, 0, NULL); // gen1b magic tag : do no issue wupC2 and don't expect 0x0a response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command) - if (!(true)) { //workFlags & 0x40 + if (!gen1b) { if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) { if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error"); @@ -1241,18 +1253,48 @@ void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){ } // send blocks command - for (int blockNo = 0; blockNo < numSectors; blockNo++) { + for (int blockNo = 0; blockNo < numBlocks; blockNo++) { if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) { if (MF_DBGLEVEL >= 1) Dbprintf("write block auth command error"); break; }; - // MifareCSetBlock here + + // check type of block and add crc + Dbprintf("--- %d", blockNo); + if (!isBlockTrailer(blockNo)){ + memcpy(d_block, block1, 16); + Dbprintf("1"); + } else { + memcpy(d_block, blockK, 16); + Dbprintf("K"); + } + if (blockNo == 0) { + memcpy(d_block, block0, 16); + Dbprintf("0"); + } + AppendCrc14443a(d_block, 16); + + // send write command + ReaderTransmit(d_block, sizeof(d_block), NULL); + if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) { + if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error"); + break; + }; + } + + // halt + // do no issue halt command for gen1b + if (!gen1b) { + if (mifare_classic_halt(NULL, 0)) { + if (MF_DBGLEVEL > 2) Dbprintf("Halt error"); + break; + } } } break; } - // send response + // send USB response LED_B_ON(); cmd_send(CMD_ACK,isOK,0,0,NULL,0); LED_B_OFF(); diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index a9d5d23d..4a18ff12 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -1868,20 +1868,20 @@ int CmdHF14AMfCSetUID(const char *Cmd) } int ParamGetCardSize(const char c) { - int numSectors = 16; + int numBlocks = 16 * 4; switch (c) { - case '0' : numSectors = 5; break; - case '2' : numSectors = 32; break; - case '4' : numSectors = 40; break; - default: numSectors = 16; + case '0' : numBlocks = 5 * 4; break; + case '2' : numBlocks = 32 * 4; break; + case '4' : numBlocks = 32 * 4 + 8 * 16; break; + default: numBlocks = 16 * 4; } - return numSectors; + return numBlocks; } int CmdHF14AMfCWipe(const char *Cmd) { int res, gen = 0; - int numSectors = 16; + int numBlocks = 16 * 4; bool wipeCard = false; bool fillCard = false; @@ -1895,10 +1895,10 @@ int CmdHF14AMfCWipe(const char *Cmd) } gen = mfCIdentify(); - if ((gen != 1) & (gen != 2)) + if ((gen != 1) && (gen != 2)) return 1; - numSectors = ParamGetCardSize(param_getchar(Cmd, 1)); + numBlocks = ParamGetCardSize(param_getchar(Cmd, 0)); char cmdp = 0; while(param_getchar(Cmd, cmdp) != 0x00){ @@ -1917,20 +1917,20 @@ int CmdHF14AMfCWipe(const char *Cmd) cmdp++; } - if (!wipeCard & !fillCard) + if (!wipeCard && !fillCard) wipeCard = TRUE; - PrintAndLog("--sectors count:%2d wipe:%c fill:%c", numSectors, (wipeCard)?'y':'n', (fillCard)?'y':'n'); + PrintAndLog("--blocks count:%2d wipe:%c fill:%c", numBlocks, (wipeCard)?'y':'n', (fillCard)?'y':'n'); if (gen == 2) { /* generation 1b magic card */ if (wipeCard) { PrintAndLog("WARNING: can't wipe magic card 1b generation"); } - res = mfCWipe(numSectors, false, fillCard); + res = mfCWipe(numBlocks, false, fillCard); } else { /* generation 1a magic card by default */ - res = mfCWipe(numSectors, wipeCard, fillCard); + res = mfCWipe(numBlocks, wipeCard, fillCard); } if (res) { @@ -1957,7 +1957,7 @@ int CmdHF14AMfCSetBlk(const char *Cmd) } gen = mfCIdentify(); - if ((gen != 1) & (gen != 2)) + if ((gen != 1) && (gen != 2)) return 1; blockNo = param_get8(Cmd, 0); diff --git a/client/mifarehost.c b/client/mifarehost.c index fffc36ff..2e120660 100644 --- a/client/mifarehost.c +++ b/client/mifarehost.c @@ -453,7 +453,7 @@ int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uin return 0; } -int mfCWipe(uint8_t numSectors, bool wantWipe, bool wantFill) { +int mfCWipe(uint32_t numSectors, bool wantWipe, bool wantFill) { uint8_t isOK = 0; UsbCommand c = {CMD_MIFARE_CWIPE, {numSectors, wantWipe, wantFill}}; SendCommand(&c); diff --git a/client/mifarehost.h b/client/mifarehost.h index 7e4ecade..7044df6d 100644 --- a/client/mifarehost.h +++ b/client/mifarehost.h @@ -33,7 +33,7 @@ extern int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint extern int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount); extern int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount); -extern int mfCWipe(uint8_t numSectors, bool wantWipe, bool wantFill); +extern int mfCWipe(uint32_t numSectors, bool wantWipe, bool wantFill); extern int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, bool wantWipe, bool wantFill); extern int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params); extern int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params);