X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/d714d3effc50b7761dd308c6b0864deea14bd483..refs/heads/unstable:/client/cmdhfmf.c

diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c
index 2dc1ab2b..5d2d3222 100644
--- a/client/cmdhfmf.c
+++ b/client/cmdhfmf.c
@@ -140,80 +140,6 @@ int CmdHF14AMfWrBl(const char *Cmd)
 	return 0;
 }
 
-int CmdHF14AMfUWrBl(const char *Cmd)
-{
-        uint8_t blockNo = 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    <block number> <block data (8 hex symbols)>");
-                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;
-        }
-
-	switch(blockNo)
-	{
-	case 0:
-		PrintAndLog("Access Denied");
-		break;
-	case 1:
-		PrintAndLog("Access Denied");
-		break;
-	case 2:
-		PrintAndLog("--specialblock no:%02x", 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:%02x", 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:%02x", blockNo);
-        	PrintAndLog("--data: %s", sprint_hex(bldata, 4));        	
-  		//UsbCommand e = {CMD_MIFAREU_WRITEBL_COMPAT, {blockNo}};
-        	//memcpy(e.d.asBytes,bldata, 16);
-  		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;
@@ -262,129 +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    <block number>");
-                PrintAndLog("        sample: hf mf urdbl 0");
-                return 0;
-        }       
-        
-        blockNo = param_get8(Cmd, 0);
-        PrintAndLog("--block no:%02x", 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;
-
-        if (sectorNo > 15) {
-                PrintAndLog("Sector number must be less than 16");
-                return 1;
-        }
-        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) 
-                        for (i = 0; i < 16; i++) {
-				switch(i){
-				  case 2:
-					//process lock bytes
-					lockbytes_t=data+(i*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));
-					}
-					//PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]);
-					//PrintAndLog("LB2b %02x %02x %02x %02x %02x %02x %02x %02x",bit[8],bit[9],bit[10],bit[11],bit[12],bit[13],bit[14],bit[15]);		
-					PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4));
-					break;
-				  case 3: 
-					PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[4]);
-					break;
-				  case 4:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[3]);
-					break;
-				  case 5:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[2]);
-					break;
-				  case 6:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[1]);
-					break;
-				  case 7:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[0]);
-					break;
-				  case 8:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[15]);
-					break;
-				  case 9:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[14]);
-					break;
-				  case 10:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[13]);
-					break;
-				  case 11:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[12]);
-					break;
-				  case 12:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[11]);
-					break;
-				  case 13:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[10]);
-					break;
-				  case 14:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[9]);
-					break;
-				  case 15:
-                                        PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[8]);
-					break;
-				  default:
-					PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4));
-					break;
-				}
-                        }
-        } else {
-                PrintAndLog("Command1 execute timeout");
-        }
-  return 0;
-}
-
 int CmdHF14AMfRdSc(const char *Cmd)
 {
 	int i;
@@ -619,13 +422,13 @@ int CmdHF14AMfRestore(const char *Cmd)
 	
 	for (i=0 ; i<16 ; i++) {
 		if (fread(keyA[i], 1, 6, fkeys) == 0) {
-      PrintAndLog("File reading error.");
+			PrintAndLog("File reading error.");
 			return 2;
     }
 	}
 	for (i=0 ; i<16 ; i++) {
 		if (fread(keyB[i], 1, 6, fkeys) == 0) {
-      PrintAndLog("File reading error.");
+			PrintAndLog("File reading error.");
 			return 2;
     }
 	}
@@ -638,8 +441,8 @@ int CmdHF14AMfRestore(const char *Cmd)
 			memcpy(c.d.asBytes, key, 6);
 			
 			if (fread(bldata, 1, 16, fdump) == 0) {
-        PrintAndLog("File reading error.");
-        return 2;
+				PrintAndLog("File reading error.");
+				return 2;
       }
 					
 			if (j == 3) {
@@ -673,7 +476,7 @@ int CmdHF14AMfRestore(const char *Cmd)
 			SendCommand(&c);
 
 			UsbCommand resp;
-      if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
+			if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
 				uint8_t isOK  = resp.arg[0] & 0xff;
 				PrintAndLog("isOk:%02x", isOK);
 			} else {
@@ -1304,24 +1107,28 @@ int CmdHF14AMfESet(const char *Cmd)
 int CmdHF14AMfELoad(const char *Cmd)
 {
 	FILE * f;
-	char filename[20];
+	char filename[255];
 	char * fnameptr = filename;
 	char buf[64];
 	uint8_t buf8[64];
 	int i, len, blockNum;
+	bool 4kcard = 0;
 	
 	memset(filename, 0, sizeof(filename));
 	memset(buf, 0, sizeof(buf));
 
 	if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
 		PrintAndLog("It loads emul dump from the file `filename.eml`");
-		PrintAndLog("Usage:  hf mf eload <file name w/o `.eml`>");
-		PrintAndLog(" sample: hf mf eload filename");
+		PrintAndLog("Usage:  hf mf eload <file name w/o `.eml`> <4 - 4kcard>");
+		PrintAndLog(" sample: hf mf eload filename [4]");
 		return 0;
 	}	
 
+	size = param_getchar(Cmd, 1);
+        if (size == '4') 4kcard = 1;
+
 	len = strlen(Cmd);
-	if (len > 14) len = 14;
+	if (len > 254) len = 254;
 
 	memcpy(filename, Cmd, len);
 	fnameptr += len;
@@ -1367,9 +1174,16 @@ int CmdHF14AMfELoad(const char *Cmd)
 	}
 	fclose(f);
 	
-	if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
-		PrintAndLog("File content error. There must be 64 blocks");
-		return 4;
+	if(4kcard){
+		if (blockNum != 32 * 4 + 8 * 16){
+                	PrintAndLog("File content error. There must be 64 blocks");
+                	return 4;
+        	}
+	}else{
+		if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
+			PrintAndLog("File content error. There must be 64 blocks");
+			return 4;
+		}
 	}
 	PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);
   return 0;
@@ -1378,7 +1192,7 @@ int CmdHF14AMfELoad(const char *Cmd)
 int CmdHF14AMfESave(const char *Cmd)
 {
 	FILE * f;
-	char filename[20];
+	char filename[255];
 	char * fnameptr = filename;
 	uint8_t buf[64];
 	int i, j, len;
@@ -1395,7 +1209,7 @@ int CmdHF14AMfESave(const char *Cmd)
 	}	
 
 	len = strlen(Cmd);
-	if (len > 14) len = 14;
+	if (len > 254) len = 254;
 	
 	if (len < 1) {
 		// get filename
@@ -1847,9 +1661,8 @@ int CmdHF14AMfSniff(const char *Cmd){
 	printf("Press the key on pc keyboard to abort the client.\n");
 	printf("-------------------------------------------------------------------------\n");
 
-	UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
-	clearCommandBuffer();
-	SendCommand(&c);
+  UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
+  SendCommand(&c);
 
 	// wait cycle
 	while (true) {
@@ -1896,7 +1709,7 @@ int CmdHF14AMfSniff(const char *Cmd){
 						sak = bufPtr[11];
 						
 						PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid, 7), atqa[0], atqa[1], sak);
-						if (wantLogToFile || wantDecrypt) {
+						if (wantLogToFile) {
 							FillFileNameByUID(logHexFileName, uid, ".log", 7);
 							AddLogCurrentDT(logHexFileName);
 						}						
@@ -1912,38 +1725,34 @@ int CmdHF14AMfSniff(const char *Cmd){
 			}
 		} // resp not NILL
 	} // while (true)
-	
-	return 0;
+  return 0;
 }
 
 static command_t CommandTable[] =
 {
   {"help",		CmdHelp,						1, "This help"},
-  {"dbg",			CmdHF14AMfDbg,			0, "Set default debug mode"},
+  {"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"},
+  {"restore",		CmdHF14AMfRestore,	0, "Restore MIFARE classic binary file to BLANK tag"},
   {"wrbl",		CmdHF14AMfWrBl,			0, "Write MIFARE classic block"},
-  {"chk",			CmdHF14AMfChk,			0, "Test block keys"},
-  {"mifare",	CmdHF14AMifare,			0, "Read parity error messages."},
-  {"nested",	CmdHF14AMfNested,		0, "Test nested authentication"},
+  {"chk",		CmdHF14AMfChk,			0, "Test block keys"},
+  {"mifare",		CmdHF14AMifare,			0, "Read parity error messages."},
+  {"nested",		CmdHF14AMfNested,		0, "Test nested authentication"},
   {"sniff",		CmdHF14AMfSniff,		0, "Sniff card-reader communication"},
-  {"sim",			CmdHF14AMf1kSim,		0, "Simulate MIFARE card"},
+  {"sim",		CmdHF14AMf1kSim,		0, "Simulate MIFARE card"},
   {"eclr",		CmdHF14AMfEClear,		0, "Clear simulator memory block"},
   {"eget",		CmdHF14AMfEGet,			0, "Get simulator memory block"},
   {"eset",		CmdHF14AMfESet,			0, "Set simulator memory block"},
   {"eload",		CmdHF14AMfELoad,		0, "Load from file emul dump"},
   {"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"},
-  {"cgetblk",	CmdHF14AMfCGetBlk,	0, "Read block from magic Chinese card"},
-  {"cgetsc",	CmdHF14AMfCGetSc,		0, "Read sector from magic Chinese card"},
+  {"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"},
+  {"cgetblk",		CmdHF14AMfCGetBlk,	0, "Read block from magic Chinese card"},
+  {"cgetsc",		CmdHF14AMfCGetSc,		0, "Read sector from magic Chinese card"},
   {"cload",		CmdHF14AMfCLoad,		0, "Load dump into magic Chinese card"},
   {"csave",		CmdHF14AMfCSave,		0, "Save dump from magic Chinese card into file or emulator"},
   {NULL, NULL, 0, NULL}