X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/836138032cb134139d9ab5247c6baee983b2c312..208a0166b948d88b276a67dd334f3b1ecca62420:/client/cmdhfmf.c diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index 081d197c..eafb2919 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -1218,6 +1218,175 @@ int CmdHF14AMfEKeyPrn(const char *Cmd) return 0; } +int CmdHF14AMfCSetUID(const char *Cmd) +{ + uint8_t wipeCard = 0; + uint8_t uid[8]; + uint8_t oldUid[8]; + int res; + + if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') { + PrintAndLog("Usage: hf mf csetuid "); + PrintAndLog("sample: hf mf csetuid 01020304 w"); + PrintAndLog("Set UID for magic Chinese card (only works with!!!)"); + PrintAndLog("If you want wipe card then add 'w' into command line. \n"); + return 0; + } + + if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) { + PrintAndLog("UID must include 8 HEX symbols"); + return 1; + } + + char ctmp = param_getchar(Cmd, 1); + if (ctmp == 'w' || ctmp == 'W') wipeCard = 1; + + PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4)); + + res = mfCSetUID(uid, oldUid, wipeCard); + if (res) { + PrintAndLog("Can't set UID. error=%d", res); + return 1; + } + + PrintAndLog("old UID:%s", sprint_hex(oldUid, 4)); + return 0; +} + +int CmdHF14AMfCSetBlk(const char *Cmd) +{ + uint8_t uid[8]; + uint8_t memBlock[16]; + 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 "); + PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516"); + PrintAndLog("Set block data for magic Chinese card (only works with!!!)"); + PrintAndLog("If you want wipe card then add 'w' into command line. \n"); + return 0; + } + + blockNo = param_get8(Cmd, 0); + if (blockNo >= 32 * 4 + 8 * 16) { + PrintAndLog("Block number must be in [0..255] as in MIFARE classic."); + return 1; + } + + if (param_gethex(Cmd, 1, memBlock, 32)) { + PrintAndLog("block data must include 32 HEX symbols"); + return 1; + } + + PrintAndLog("--block number:%02x data:%s", blockNo, sprint_hex(memBlock, 16)); + + res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER); + if (res) { + PrintAndLog("Can't write block. error=%d", res); + return 1; + } + + PrintAndLog("UID:%s", sprint_hex(uid, 4)); + return 0; +} + +int CmdHF14AMfCLoad(const char *Cmd) +{ + FILE * f; + char filename[20]; + char * fnameptr = filename; + char buf[64]; + uint8_t buf8[64]; + uint8_t fillFromEmulator = 0; + int i, len, blockNum, flags; + + memset(filename, 0, sizeof(filename)); + memset(buf, 0, sizeof(buf)); + + if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) { + PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`"); + PrintAndLog("or from emulator memory (option `e`)"); + PrintAndLog("Usage: hf mf cload "); + PrintAndLog(" or: hf mf cload e "); + PrintAndLog(" sample: hf mf cload filename"); + return 0; + } + + char ctmp = param_getchar(Cmd, 0); + if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1; + + if (fillFromEmulator) { + flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; + for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) { + if (mfEmlGetMem(buf8, blockNum, 1)) { + PrintAndLog("Cant get block: %d", blockNum); + return 2; + } + + if (blockNum == 2) flags = 0; + if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; + + if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) { + PrintAndLog("Cant set magic card block: %d", blockNum); + return 3; + } + } + return 0; + } else { + len = strlen(Cmd); + if (len > 14) len = 14; + + memcpy(filename, Cmd, len); + fnameptr += len; + + sprintf(fnameptr, ".eml"); + + // open file + f = fopen(filename, "r"); + if (f == NULL) { + PrintAndLog("File not found or locked."); + return 1; + } + + blockNum = 0; + flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; + while(!feof(f)){ + memset(buf, 0, sizeof(buf)); + fgets(buf, sizeof(buf), f); + + if (strlen(buf) < 32){ + if(strlen(buf) && feof(f)) + break; + PrintAndLog("File content error. Block data must include 32 HEX symbols"); + return 2; + } + for (i = 0; i < 32; i += 2) + sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]); + + if (blockNum == 2) flags = 0; + if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; + + if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) { + PrintAndLog("Cant set magic card block: %d", blockNum); + return 3; + } + blockNum++; + + if (blockNum >= 16 * 4) break; // magic card type - mifare 1K + } + fclose(f); + + if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){ + PrintAndLog("File content error. There must be 64 blocks"); + return 4; + } + PrintAndLog("Loaded from file: %s", filename); + return 0; + } +} + static command_t CommandTable[] = { {"help", CmdHelp, 1, "This help"}, @@ -1238,6 +1407,9 @@ static command_t CommandTable[] = {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"}, {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"}, {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"}, + {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"}, + {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"}, + {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"}, {NULL, NULL, 0, NULL} };