CHG: The input handling for "hf 14b write" is now correct. Thanks Asper for spotting the fault.

Minor code clean up.
Added from Pm3-master which will make this fork one step closer to Pm3-master.
This commit is contained in:
iceman1001 2015-01-07 22:51:34 +01:00
parent c0e6c18bf5
commit 14edfd09c3
7 changed files with 77 additions and 47 deletions

View file

@ -1158,7 +1158,7 @@ void ReaderHitag(hitag_function htf, hitag_data* htd) {
case RHT2F_CRYPTO: {
DbpString("Authenticating using key:");
memcpy(key,htd->crypto.key,6); // 4 or 6 ??
memcpy(key,htd->crypto.key,4); //HACK; 4 or 6?? I read both in the code.
Dbhexdump(6,key,false);
blocknr = 0;
bQuiet = false;

View file

@ -1587,7 +1587,7 @@ void ReaderIClass(uint8_t arg0) {
void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) {
uint8_t card_data[24]={0};
uint8_t card_data[USB_CMD_DATA_SIZE]={0};
uint16_t block_crc_LUT[255] = {0};
{//Generate a lookup table for block crc
@ -1660,7 +1660,10 @@ void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) {
cardsize = memory.k16 ? 255 : 32;
WDT_HIT();
//Set card_data to all zeroes, we'll fill it with data
memset(card_data,0x0,USB_CMD_DATA_SIZE);
uint8_t failedRead =0;
uint8_t stored_data_length =0;
//then loop around remaining blocks
for(int block=0; block < cardsize; block++){
@ -1676,14 +1679,47 @@ void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) {
resp[3], resp[4], resp[5],
resp[6], resp[7]);
}else{
Dbprintf("Failed to dump block %d", block);
//Fill up the buffer
memcpy(card_data+stored_data_length,resp,8);
stored_data_length += 8;
if(stored_data_length +8 > USB_CMD_DATA_SIZE)
{//Time to send this off and start afresh
cmd_send(CMD_ACK,
stored_data_length,//data length
failedRead,//Failed blocks?
0,//Not used ATM
card_data, stored_data_length);
//reset
stored_data_length = 0;
failedRead = 0;
}
}else{
failedRead = 1;
stored_data_length +=8;//Otherwise, data becomes misaligned
Dbprintf("Failed to dump block %d", block);
}
}
//Send off any remaining data
if(stored_data_length > 0)
{
cmd_send(CMD_ACK,
stored_data_length,//data length
failedRead,//Failed blocks?
0,//Not used ATM
card_data, stored_data_length);
}
//If we got here, let's break
break;
}
//Signal end of transmission
cmd_send(CMD_ACK,
0,//data length
0,//Failed blocks?
0,//Not used ATM
card_data, 0);
LED_A_OFF();
}
@ -1702,7 +1738,7 @@ void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_
uint16_t crc = 0;
uint8_t* resp = (((uint8_t *)BigBuf) + RECV_RESP_OFFSET);
uint8_t* resp = (((uint8_t *)BigBuf) + 3560);
// Reset trace buffer
memset(trace, 0x44, RECV_CMD_OFFSET);

View file

@ -407,18 +407,23 @@ int CmdHF14BWrite( const char *Cmd){
PrintAndLog("Usage: hf 14b write <1|2> <BLOCK> <DATA>");
PrintAndLog(" [1 = SRIX4K]");
PrintAndLog(" [2 = SRI512]");
PrintAndLog(" [BLOCK number depends on which tag, special block == 255]");
PrintAndLog(" sample: hf 14b write 1 127 11223344");
PrintAndLog(" : hf 14b write 1 255 11223344");
PrintAndLog(" [BLOCK number depends on tag, special block == FF]");
PrintAndLog(" sample: hf 14b write 1 7F 11223344");
PrintAndLog(" : hf 14b write 1 FF 11223344");
PrintAndLog(" : hf 14b write 2 15 11223344");
PrintAndLog(" : hf 14b write 2 255 11223344");
PrintAndLog(" : hf 14b write 2 FF 11223344");
return 0;
}
if ( param_getchar(Cmd, 0) == '2' )
if ( cmdp == '2' )
isSrix4k = false;
blockno = param_get8(Cmd, 1);
//blockno = param_get8(Cmd, 1);
if ( param_gethex(Cmd,1, &blockno, 2) ) {
PrintAndLog("Block number must include 2 HEX symbols");
return 0;
}
if ( isSrix4k ){
if ( blockno > 0x7f && blockno != 0xff ){
@ -438,11 +443,12 @@ int CmdHF14BWrite( const char *Cmd){
}
if ( blockno == 0xff)
PrintAndLog("Writing to special block %02X [ %s]", blockno, sprint_hex(data,4) );
PrintAndLog("[%s] Write special block %02X [ %s ]", (isSrix4k)?"SRIX4K":"SRI512" , blockno, sprint_hex(data,4) );
else
PrintAndLog("Writing to block %02X [ %s]", blockno, sprint_hex(data,4) );
PrintAndLog("[%s] Write block %02X [ %s ]", (isSrix4k)?"SRIX4K":"SRI512", blockno, sprint_hex(data,4) );
sprintf(str, "-c -p 09 %02x %02x%02x%02x%02x", blockno, data[0], data[1], data[2], data[3]);
CmdHF14BCmdRaw(str);
return 0;
}

View file

@ -460,7 +460,7 @@ int CmdHF14AMfRestore(const char *Cmd)
default: numSectors = 16;
}
if (cmdp == 'h' || cmdp == 'H') {
if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
PrintAndLog("Usage: hf mf restore [card memory]");
PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
PrintAndLog("");

View file

@ -18,6 +18,7 @@
#include "cmddata.h"
#include "cmdhw.h"
#include "cmdmain.h"
#include "cmddata.h"
/* low-level hardware control */

View file

@ -76,14 +76,14 @@ int saveFile(const char *preferredName, const char *suffix, const void* data, si
/* We should have a valid filename now, e.g. dumpdata-3.bin */
/*Opening file for writing in binary mode*/
FILE *fh=fopen(fileName,"wb");
if(!fh) {
FILE *fileHandle=fopen(fileName,"wb");
if(!fileHandle) {
PrintAndLog("Failed to write to file '%s'", fileName);
free(fh);
free(fileName);
return 1;
}
fwrite(data, 1, datalen, fh);
fclose(fh);
fwrite(data, 1, datalen, fileHandle);
fclose(fileHandle);
PrintAndLog("Saved data to '%s'", fileName);
free(fileName);

View file

@ -37,13 +37,8 @@ void SendCommand(UsbCommand *c) {
#if 0
printf("Sending %d bytes\n", sizeof(UsbCommand));
#endif
/*
if (txcmd_pending) {
ERR("Sending command failed, previous command is still pending");
}
*/
if(offline)
{
if (offline) {
PrintAndLog("Sending bytes to proxmark failed - offline");
return;
}
@ -82,7 +77,7 @@ static void *uart_receiver(void *targ) {
continue;
}
cmd_count = (prx-rx) / sizeof(UsbCommand);
// printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count);
for (size_t i=0; i<cmd_count; i++) {
UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
}
@ -109,43 +104,37 @@ static void *main_loop(void *targ) {
if (arg->usb_present == 1) {
rarg.run=1;
// pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
}
FILE *script_file = NULL;
char script_cmd_buf[256]; // iceman, needs lua script the same file_path_buffer as the rest
if (arg->script_cmds_file)
{
if (arg->script_cmds_file) {
script_file = fopen(arg->script_cmds_file, "r");
if (script_file)
{
if (script_file) {
printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
}
}
read_history(".history");
while(1)
{
while(1) {
// If there is a script file
if (script_file)
{
if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file))
{
if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) {
fclose(script_file);
script_file = NULL;
}
else
{
} else {
char *nl;
nl = strrchr(script_cmd_buf, '\r');
if (nl) *nl = '\0';
nl = strrchr(script_cmd_buf, '\n');
if (nl) *nl = '\0';
if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL)
{
if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL) {
memset(cmd, 0, strlen(script_cmd_buf));
strcpy(cmd, script_cmd_buf);
printf("%s\n", cmd);
@ -153,12 +142,12 @@ static void *main_loop(void *targ) {
}
}
if (!script_file)
{
if (!script_file) {
cmd = readline(PROXPROMPT);
}
if (cmd) {
while(cmd[strlen(cmd) - 1] == ' ')
cmd[strlen(cmd) - 1] = 0x00;
@ -167,7 +156,6 @@ static void *main_loop(void *targ) {
exit(0);
break;
}
CommandReceived(cmd);
add_history(cmd);
}
@ -185,8 +173,7 @@ static void *main_loop(void *targ) {
pthread_join(reader_thread, NULL);
}
if (script_file)
{
if (script_file) {
fclose(script_file);
script_file = NULL;
}