X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/aea4d76687f3aa3d5dcd0fc5cf3d36f8f753994c..f5c5499ab80d650e549a006e75ce30922d5fa144:/client/cmdhfmf.c diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index ac4f1087..8c91e84c 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -712,6 +712,7 @@ int CmdHF14AMfChk(const char *Cmd) { FILE * f; char filename[256]={0}; + char buf[13]; uint8_t *keyBlock = NULL, *p; uint8_t stKeyBlock = 20; @@ -810,20 +811,24 @@ int CmdHF14AMfChk(const char *Cmd) return 2; } - char * line = NULL; - size_t len = 0; - ssize_t read; if ( (f = fopen( filename , "r")) ) { - while((read = getline(&line, &len, f)) != -1){ + while( !feof(f) ){ + memset(buf, 0, sizeof(buf)); + fgets(buf, sizeof(buf), f); + + if (strlen(buf) < 12 || buf[11] == '\n') + continue; - if( line[0]=='#' || line[0]=='\n' ) continue; //The line start with # is remcommnet,skip + while (fgetc(f) != '\n' && !feof(f)) ; //goto next line + + if( buf[0]=='#' ) continue; //The line start with # is remcommnet,skip - if (read < 12 || !isxdigit(line[0])){ - PrintAndLog("File content error. '%s' must include 12 HEX symbols",line); + if (!isxdigit(buf[0])){ + PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf); continue; } - line[12] = 0; + buf[12] = 0; if ( stKeyBlock - keycnt < 2) { p = realloc(keyBlock, 6*(stKeyBlock+=10)); @@ -835,11 +840,10 @@ int CmdHF14AMfChk(const char *Cmd) keyBlock = p; } memset(keyBlock + 6 * keycnt, 0, 6); - num_to_bytes(strtoll(line, NULL, 16), 6, keyBlock + 6*keycnt); + num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt); PrintAndLog("chk custom key[%d] %012llx", keycnt, bytes_to_num(keyBlock + 6*keycnt, 6)); keycnt++; } - free(line); } else { PrintAndLog("File: %s: not found or locked.", filename); free(keyBlock); @@ -1214,6 +1218,85 @@ 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); + 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) +{ + return 0; +} + static command_t CommandTable[] = { {"help", CmdHelp, 1, "This help"}, @@ -1234,6 +1317,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, "(n/a)Load dump into magic Chinese card"}, {NULL, NULL, 0, NULL} };