blocks number and sector trails calculates correctly

This commit is contained in:
merlokk 2017-09-20 14:13:26 +03:00
commit 71d670d5b2
4 changed files with 67 additions and 25 deletions

View file

@ -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){ void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
// var // var
byte_t isOK = 0; byte_t isOK = 0;
uint8_t numSectors = arg0; uint32_t numBlocks = arg0;
uint8_t needWipe = arg1; uint8_t needWipe = arg1;
uint8_t needFill = arg2; uint8_t needFill = arg2;
bool gen1b = FALSE;
uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE]; uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_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 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 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 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 // card commands
uint8_t wupC1[] = { 0x40 }; 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 // put default data
if (needFill){ if (needFill){
// select commands // 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) // 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(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error"); 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 // 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 ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
if (MF_DBGLEVEL >= 1) Dbprintf("write block auth command error"); if (MF_DBGLEVEL >= 1) Dbprintf("write block auth command error");
break; 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; break;
} }
// send response // send USB response
LED_B_ON(); LED_B_ON();
cmd_send(CMD_ACK,isOK,0,0,NULL,0); cmd_send(CMD_ACK,isOK,0,0,NULL,0);
LED_B_OFF(); LED_B_OFF();

View file

@ -1868,20 +1868,20 @@ int CmdHF14AMfCSetUID(const char *Cmd)
} }
int ParamGetCardSize(const char c) { int ParamGetCardSize(const char c) {
int numSectors = 16; int numBlocks = 16 * 4;
switch (c) { switch (c) {
case '0' : numSectors = 5; break; case '0' : numBlocks = 5 * 4; break;
case '2' : numSectors = 32; break; case '2' : numBlocks = 32 * 4; break;
case '4' : numSectors = 40; break; case '4' : numBlocks = 32 * 4 + 8 * 16; break;
default: numSectors = 16; default: numBlocks = 16 * 4;
} }
return numSectors; return numBlocks;
} }
int CmdHF14AMfCWipe(const char *Cmd) int CmdHF14AMfCWipe(const char *Cmd)
{ {
int res, gen = 0; int res, gen = 0;
int numSectors = 16; int numBlocks = 16 * 4;
bool wipeCard = false; bool wipeCard = false;
bool fillCard = false; bool fillCard = false;
@ -1895,10 +1895,10 @@ int CmdHF14AMfCWipe(const char *Cmd)
} }
gen = mfCIdentify(); gen = mfCIdentify();
if ((gen != 1) & (gen != 2)) if ((gen != 1) && (gen != 2))
return 1; return 1;
numSectors = ParamGetCardSize(param_getchar(Cmd, 1)); numBlocks = ParamGetCardSize(param_getchar(Cmd, 0));
char cmdp = 0; char cmdp = 0;
while(param_getchar(Cmd, cmdp) != 0x00){ while(param_getchar(Cmd, cmdp) != 0x00){
@ -1917,20 +1917,20 @@ int CmdHF14AMfCWipe(const char *Cmd)
cmdp++; cmdp++;
} }
if (!wipeCard & !fillCard) if (!wipeCard && !fillCard)
wipeCard = TRUE; 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) { if (gen == 2) {
/* generation 1b magic card */ /* generation 1b magic card */
if (wipeCard) { if (wipeCard) {
PrintAndLog("WARNING: can't wipe magic card 1b generation"); PrintAndLog("WARNING: can't wipe magic card 1b generation");
} }
res = mfCWipe(numSectors, false, fillCard); res = mfCWipe(numBlocks, false, fillCard);
} else { } else {
/* generation 1a magic card by default */ /* generation 1a magic card by default */
res = mfCWipe(numSectors, wipeCard, fillCard); res = mfCWipe(numBlocks, wipeCard, fillCard);
} }
if (res) { if (res) {
@ -1957,7 +1957,7 @@ int CmdHF14AMfCSetBlk(const char *Cmd)
} }
gen = mfCIdentify(); gen = mfCIdentify();
if ((gen != 1) & (gen != 2)) if ((gen != 1) && (gen != 2))
return 1; return 1;
blockNo = param_get8(Cmd, 0); blockNo = param_get8(Cmd, 0);

View file

@ -453,7 +453,7 @@ int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uin
return 0; return 0;
} }
int mfCWipe(uint8_t numSectors, bool wantWipe, bool wantFill) { int mfCWipe(uint32_t numSectors, bool wantWipe, bool wantFill) {
uint8_t isOK = 0; uint8_t isOK = 0;
UsbCommand c = {CMD_MIFARE_CWIPE, {numSectors, wantWipe, wantFill}}; UsbCommand c = {CMD_MIFARE_CWIPE, {numSectors, wantWipe, wantFill}};
SendCommand(&c); SendCommand(&c);

View file

@ -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 mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount);
extern int mfEmlSetMem(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 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 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); extern int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params);