From 5ee701292ff2e28abf098e2266a42954f381e3ad Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 20 Jan 2015 21:23:04 +0100 Subject: [PATCH] Step 2 - Ultralight / Ultralight-C With this the Pentura Labs / Midnitsnakes's original ultralight / ultralight-c implementation is enhanced and move to its own file. cmdhfmfu.c --- client/Makefile | 1 + client/cmdhf.c | 2 + client/cmdhf14a.c | 4 +- client/cmdhf14b.c | 4 +- client/cmdhfmf.c | 221 +++------------------------------------------- client/cmdlf.c | 2 +- common/usb_cdc.c | 2 +- 7 files changed, 20 insertions(+), 216 deletions(-) diff --git a/client/Makefile b/client/Makefile index 77fee4e6..12d92631 100644 --- a/client/Makefile +++ b/client/Makefile @@ -78,6 +78,7 @@ CMDSRCS = nonce2key/crapto1.c\ cmdhflegic.c \ cmdhficlass.c \ cmdhfmf.c \ + cmdhfmfu.c \ cmdhw.c \ cmdlf.c \ cmdlfio.c \ diff --git a/client/cmdhf.c b/client/cmdhf.c index 9acc9825..637b2b08 100644 --- a/client/cmdhf.c +++ b/client/cmdhf.c @@ -22,6 +22,7 @@ #include "cmdhflegic.h" #include "cmdhficlass.h" #include "cmdhfmf.h" +#include "cmdhfmfu.h" static int CmdHelp(const char *Cmd); @@ -601,6 +602,7 @@ static command_t CommandTable[] = {"legic", CmdHFLegic, 0, "{ LEGIC RFIDs... }"}, {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"}, {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"}, + {"mfu", CmdHFMFUltra, 1, "{ MIFARE Ultralight RFIDs... }"}, {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"}, {"list", CmdHFList, 1, "List protocol data in trace buffer"}, {NULL, NULL, 0, NULL} diff --git a/client/cmdhf14a.c b/client/cmdhf14a.c index 01602d76..593661a5 100644 --- a/client/cmdhf14a.c +++ b/client/cmdhf14a.c @@ -412,9 +412,9 @@ int CmdHF14ASim(const char *Cmd) PrintAndLog(" syntax: hf 14a sim "); PrintAndLog(" types: 1 = MIFARE Classic"); PrintAndLog(" 2 = MIFARE Ultralight"); - PrintAndLog(" 3 = MIFARE DESFIRE"); + PrintAndLog(" 3 = MIFARE Desfire"); PrintAndLog(" 4 = ISO/IEC 14443-4"); - PrintAndLog(" 5 = MIFARE TNP3XXX"); + PrintAndLog(" 5 = MIFARE Tnp3xxx"); PrintAndLog(""); return 1; } diff --git a/client/cmdhf14b.c b/client/cmdhf14b.c index e3d0fc23..0350fc31 100644 --- a/client/cmdhf14b.c +++ b/client/cmdhf14b.c @@ -280,7 +280,7 @@ int CmdHF14BCmdRaw (const char *cmd) { uint8_t power=0; char buf[5]=""; int i=0; - uint8_t data[100]; + uint8_t data[100] = {0x00}; unsigned int datalen=0, temp; char *hexout; @@ -334,7 +334,7 @@ int CmdHF14BCmdRaw (const char *cmd) { continue; } PrintAndLog("Invalid char on input"); - return 0; + return 1; } if (datalen == 0) { diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index aae6290d..28f7b078 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -140,117 +140,6 @@ int CmdHF14AMfWrBl(const char *Cmd) return 0; } -int CmdHF14AMfUWrBl(const char *Cmd) -{ - uint8_t blockNo = 0; - bool chinese_card=0; - uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - UsbCommand resp; - - if (strlen(Cmd)<3) { - PrintAndLog("Usage: hf mf uwrbl "); - PrintAndLog(" sample: hf mf uwrbl 0 01020304"); - return 0; - } - - blockNo = param_get8(Cmd, 0); - if (param_gethex(Cmd, 1, bldata, 8)) { - PrintAndLog("Block data must include 8 HEX symbols"); - return 1; - } - - if (strchr(Cmd,'w') != 0) { - chinese_card=1; - } - - switch(blockNo){ - case 0: - if (!chinese_card){ - PrintAndLog("Access Denied"); - }else{ - PrintAndLog("--specialblock no:%d", blockNo); - PrintAndLog("--data: %s", sprint_hex(bldata, 4)); - UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; - memcpy(d.d.asBytes,bldata, 4); - SendCommand(&d); - - if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { - uint8_t isOK = resp.arg[0] & 0xff; - PrintAndLog("isOk:%02x", isOK); - } else { - PrintAndLog("Command execute timeout"); - } - } - break; - case 1: - if (!chinese_card){ - PrintAndLog("Access Denied"); - }else{ - PrintAndLog("--specialblock no:%d", blockNo); - PrintAndLog("--data: %s", sprint_hex(bldata, 4)); - UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; - memcpy(d.d.asBytes,bldata, 4); - SendCommand(&d); - - if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { - uint8_t isOK = resp.arg[0] & 0xff; - PrintAndLog("isOk:%02x", isOK); - } else { - PrintAndLog("Command execute timeout"); - } - } - break; - case 2: - if (!chinese_card){ - PrintAndLog("Access Denied"); - }else{ - PrintAndLog("--specialblock no:%d", blockNo); - PrintAndLog("--data: %s", sprint_hex(bldata, 4)); - UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}}; - memcpy(c.d.asBytes, bldata, 4); - SendCommand(&c); - - if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { - uint8_t isOK = resp.arg[0] & 0xff; - PrintAndLog("isOk:%02x", isOK); - } else { - PrintAndLog("Command execute timeout"); - } - } - break; - case 3: - PrintAndLog("--specialblock no:%d", blockNo); - PrintAndLog("--data: %s", sprint_hex(bldata, 4)); - UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; - memcpy(d.d.asBytes,bldata, 4); - SendCommand(&d); - - if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { - uint8_t isOK = resp.arg[0] & 0xff; - PrintAndLog("isOk:%02x", isOK); - } else { - PrintAndLog("Command execute timeout"); - } - break; - default: - PrintAndLog("--block no:%d", blockNo); - PrintAndLog("--data: %s", sprint_hex(bldata, 4)); - UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}}; - memcpy(e.d.asBytes,bldata, 4); - SendCommand(&e); - - if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { - uint8_t isOK = resp.arg[0] & 0xff; - PrintAndLog("isOk:%02x", isOK); - } else { - PrintAndLog("Command execute timeout"); - } - break; - } - return 0; -} - - int CmdHF14AMfRdBl(const char *Cmd) { uint8_t blockNo = 0; @@ -299,87 +188,6 @@ int CmdHF14AMfRdBl(const char *Cmd) return 0; } -int CmdHF14AMfURdBl(const char *Cmd) -{ - uint8_t blockNo = 0; - - if (strlen(Cmd)<1) { - PrintAndLog("Usage: hf mf urdbl "); - PrintAndLog(" sample: hf mf urdbl 0"); - return 0; - } - - blockNo = param_get8(Cmd, 0); - PrintAndLog("--block no:%d", blockNo); - - UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}}; - SendCommand(&c); - - UsbCommand resp; - if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { - uint8_t isOK = resp.arg[0] & 0xff; - uint8_t *data = resp.d.asBytes; - - if (isOK) - PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 4)); - else - PrintAndLog("isOk:%02x", isOK); - } else { - PrintAndLog("Command execute timeout"); - } - - return 0; -} - - -int CmdHF14AMfURdCard(const char *Cmd) -{ - int i; - uint8_t sectorNo = 0; - uint8_t *lockbytes_t=NULL; - uint8_t lockbytes[2]={0,0}; - bool bit[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - - uint8_t isOK = 0; - uint8_t * data = NULL; - - PrintAndLog("Attempting to Read Ultralight... "); - - UsbCommand c = {CMD_MIFAREU_READCARD, {sectorNo}}; - SendCommand(&c); - - UsbCommand resp; - if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { - isOK = resp.arg[0] & 0xff; - data = resp.d.asBytes; - - PrintAndLog("isOk:%02x", isOK); - if (isOK) - { // bit 0 and 1 - PrintAndLog("Block %3d:%s ", 0,sprint_hex(data + 0 * 4, 4)); - PrintAndLog("Block %3d:%s ", 1,sprint_hex(data + 1 * 4, 4)); - // bit 2 - //process lock bytes - lockbytes_t=data+(2*4); - lockbytes[0]=lockbytes_t[2]; - lockbytes[1]=lockbytes_t[3]; - for(int j=0; j<16; j++){ - bit[j]=lockbytes[j/8] & ( 1 <<(7-j%8)); - } - //remaining - for (i = 3; i < 16; i++) { - int bitnum = (23-i) % 16; - PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[bitnum]); - } - - } - } else { - PrintAndLog("Command execute timeout"); - } - return 0; -} - - int CmdHF14AMfRdSc(const char *Cmd) { int i; @@ -1288,7 +1096,7 @@ int CmdHF14AMfDbg(const char *Cmd) int CmdHF14AMfEGet(const char *Cmd) { uint8_t blockNo = 0; - uint8_t data[16]; + uint8_t data[16] = {0x00}; if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') { PrintAndLog("Usage: hf mf eget "); @@ -1355,14 +1163,11 @@ int CmdHF14AMfELoad(const char *Cmd) FILE * f; char filename[FILE_PATH_SIZE]; char *fnameptr = filename; - char buf[64]; - uint8_t buf8[64]; + char buf[64] = {0x00}; + uint8_t buf8[64] = {0x00}; int i, len, blockNum, numBlocks; int nameParamNo = 1; - memset(filename, 0, sizeof(filename)); - memset(buf, 0, sizeof(buf)); - char ctmp = param_getchar(Cmd, 0); if ( ctmp == 'h' || ctmp == 0x00) { @@ -1432,11 +1237,13 @@ int CmdHF14AMfELoad(const char *Cmd) fclose(f); return 3; } + printf("."); blockNum++; if (blockNum >= numBlocks) break; } fclose(f); + printf("\n"); if ((blockNum != numBlocks)) { PrintAndLog("File content error. Got %d must be %d blocks.",blockNum, numBlocks); @@ -1653,11 +1460,10 @@ int CmdHF14AMfCSetUID(const char *Cmd) int CmdHF14AMfCSetBlk(const char *Cmd) { - uint8_t uid[8]; - uint8_t memBlock[16]; + uint8_t uid[8] = {0x00}; + uint8_t memBlock[16] = {0x00}; uint8_t blockNo = 0; int res; - memset(memBlock, 0x00, sizeof(memBlock)); if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') { PrintAndLog("Usage: hf mf csetblk "); @@ -1814,10 +1620,9 @@ int CmdHF14AMfCGetBlk(const char *Cmd) { int CmdHF14AMfCGetSc(const char *Cmd) { - uint8_t memBlock[16]; + uint8_t memBlock[16] = {0x00}; uint8_t sectorNo = 0; int i, res, flags; - memset(memBlock, 0x00, sizeof(memBlock)); if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') { PrintAndLog("Usage: hf mf cgetsc "); @@ -1957,14 +1762,13 @@ int CmdHF14AMfSniff(const char *Cmd){ int blockLen = 0; int num = 0; int pckNum = 0; - uint8_t uid[7]; + uint8_t uid[7] = {0x00}; uint8_t uid_len; - uint8_t atqa[2]; + uint8_t atqa[2] = {0x00}; uint8_t sak; bool isTag; - uint8_t buf[3000]; + uint8_t buf[3000] = {0x00}; uint8_t * bufPtr = buf; - memset(buf, 0x00, 3000); if (param_getchar(Cmd, 0) == 'h') { PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file."); @@ -2080,9 +1884,6 @@ static command_t CommandTable[] = {"help", CmdHelp, 1, "This help"}, {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"}, {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"}, - {"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"}, - {"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"}, - {"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"}, {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"}, {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"}, {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"}, diff --git a/client/cmdlf.c b/client/cmdlf.c index e3361cb5..491fd082 100644 --- a/client/cmdlf.c +++ b/client/cmdlf.c @@ -465,7 +465,7 @@ int CmdLFSnoop(const char *Cmd) sscanf(Cmd, "h %"lli, &c.arg[1]); } else if (sscanf(Cmd, "%"lli" %"lli, &c.arg[0], &c.arg[1]) < 1) { PrintAndLog("usage 1: snoop"); - PrintAndLog(" 2: snoop {l,h} [trigger threshold]"); + PrintAndLog(" 2: snoop [trigger threshold]"); PrintAndLog(" 3: snoop [trigger threshold]"); return 0; } diff --git a/common/usb_cdc.c b/common/usb_cdc.c index 54f6a8e8..ccbb3c50 100644 --- a/common/usb_cdc.c +++ b/common/usb_cdc.c @@ -370,7 +370,7 @@ uint32_t usb_write(const byte_t* data, const size_t len) { //* \fn AT91F_USB_SendData //* \brief Send Data through the control endpoint //*---------------------------------------------------------------------------- -unsigned int csrTab[100]; +unsigned int csrTab[100] = {0x00}; unsigned char csrIdx = 0; static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) { -- 2.39.2