Beispiel #1
0
/* send a command before reading */
int CmdLFCommandRead(const char *Cmd)
{
	static char dummy[3] = {0x20,0x00,0x00};
	UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
	bool errors = FALSE;
	//uint8_t divisor = 95; //125khz
	uint8_t cmdp = 0;
	int strLength = 0;
	while(param_getchar(Cmd, cmdp) != 0x00)
	{
		switch(param_getchar(Cmd, cmdp))
		{
		case 'h':
			return usage_lf_cmdread();
		case 'H':
			//divisor = 88;
			dummy[1]='h';
			cmdp++;
			break;
		case 'L':
			cmdp++;
			break;
		case 'c':
			strLength = param_getstr(Cmd, cmdp+1, (char *)&c.d.asBytes);
			cmdp+=2;
			break;
		case 'd':
			c.arg[0] = param_get32ex(Cmd, cmdp+1, 0, 10);
			cmdp+=2;
			break;
		case 'z':
			c.arg[1] = param_get32ex(Cmd, cmdp+1, 0, 10);
			cmdp+=2;
			break;
		case 'o':
			c.arg[2] = param_get32ex(Cmd, cmdp+1, 0, 10);
			cmdp+=2;
			break;
		default:
			PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
			errors = 1;
			break;
		}
		if(errors) break;
	}
	// No args
	if(cmdp == 0) errors = 1;

	//Validations
	if(errors) return usage_lf_cmdread();
	
	// in case they specified 'H'
	strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);

	clearCommandBuffer();
	SendCommand(&c);
	return 0;
}
Beispiel #2
0
int CmdPyramidClone(const char *Cmd) {

	char cmdp = param_getchar(Cmd, 0);
	if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_pyramid_clone();

	uint32_t facilitycode=0, cardnumber=0, fc = 0, cn = 0;
	uint32_t blocks[5];
	uint8_t i;
	uint8_t bs[128];
	memset(bs, 0x00, sizeof(bs));

	if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_pyramid_clone();

	facilitycode = (fc & 0x000000FF);
	cardnumber = (cn & 0x0000FFFF);
	
	if ( !GetPyramidBits(facilitycode, cardnumber, bs)) {
		PrintAndLog("Error with tag bitstream generation.");
		return 1;
	}

	//Pyramid - compat mode, FSK2a, data rate 50, 4 data blocks
	blocks[0] = T55x7_MODULATION_FSK2a | T55x7_BITRATE_RF_50 | 4<<T55x7_MAXBLOCK_SHIFT;

	if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q')
		blocks[0] = T5555_MODULATION_FSK2 | T5555_INVERT_OUTPUT | 50<<T5555_BITRATE_SHIFT | 4<<T5555_MAXBLOCK_SHIFT;

	blocks[1] = bytebits_to_byte(bs,32);
	blocks[2] = bytebits_to_byte(bs+32,32);
	blocks[3] = bytebits_to_byte(bs+64,32);
	blocks[4] = bytebits_to_byte(bs+96,32);

	PrintAndLog("Preparing to clone Farpointe/Pyramid to T55x7 with Facility Code: %u, Card Number: %u", facilitycode, cardnumber);
	PrintAndLog("Blk | Data ");
	PrintAndLog("----+------------");
	for ( i = 0; i<5; ++i )
		PrintAndLog(" %02d | %08" PRIx32, i, blocks[i]);

	UsbCommand resp;
	UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};

	for ( i = 0; i<5; ++i ) {
		c.arg[0] = blocks[i];
		c.arg[1] = i;
		clearCommandBuffer();
		SendCommand(&c);
		if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
			PrintAndLog("Error occurred, device did not respond during write operation.");
			return -1;
		}
	}
	return 0;
}
Beispiel #3
0
int CmdVisa2kClone(const char *Cmd) {

	uint64_t id = 0;
	uint32_t blocks[4] = {T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_64 | T55x7_ST_TERMINATOR | 3 << T55x7_MAXBLOCK_SHIFT, BL0CK1, 0};

	char cmdp = param_getchar(Cmd, 0);
	if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_visa2k_clone();

	id = param_get32ex(Cmd, 0, 0, 10);
	
	//Q5
	if (param_getchar(Cmd, 1) == 'Q' || param_getchar(Cmd, 1) == 'q') {
		//t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
		blocks[0] = T5555_MODULATION_MANCHESTER | ((64-2)>>1) << T5555_BITRATE_SHIFT | T5555_ST_TERMINATOR | 3 << T5555_MAXBLOCK_SHIFT;
	}
Beispiel #4
0
int CmdLFPCF7931Write(const char *Cmd) {

    uint8_t ctmp = param_getchar(Cmd, 0);
    if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_pcf7931_write();

    uint8_t block = 0, bytepos = 0, data = 0;

    if ( param_getdec(Cmd, 0, &block) ) return usage_pcf7931_write();
    if ( param_getdec(Cmd, 1, &bytepos) ) return usage_pcf7931_write();

    if ( (block > 7) || (bytepos > 15) ) return usage_pcf7931_write();

    data  = param_get8ex(Cmd, 2, 0, 16);

    PrintAndLog("Writing block: %d", block);
    PrintAndLog("          pos: %d", bytepos);
    PrintAndLog("         data: 0x%02X", data);

    UsbCommand c = {CMD_PCF7931_WRITE, { block, bytepos, data} };
    memcpy(c.d.asDwords, configPcf.Pwd, sizeof(configPcf.Pwd) );
    c.d.asDwords[7] = (configPcf.OffsetWidth + 128);
    c.d.asDwords[8] = (configPcf.OffsetPosition + 128);
    c.d.asDwords[9] = configPcf.InitDelay;

    clearCommandBuffer();
    SendCommand(&c);
    //no ack?
    return 0;
}
Beispiel #5
0
int CmdLFNedapSim(const char *Cmd) {

	uint32_t cardnumber = 0, cn = 0;
	
	char cmdp = param_getchar(Cmd, 0);
	if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_nedap_sim();

	if (sscanf(Cmd, "%u", &cn ) != 1) return usage_lf_nedap_sim();
	
	cardnumber = (cn & 0x00FFFFFF);
	
	uint8_t bs[128];
	size_t size = sizeof(bs);
	memset(bs, 0x00, size);
	
	// NEDAP,  Biphase = 2, clock 64, inverted,  (DIPhase == inverted BIphase
	uint8_t  clk = 64, encoding = 2, separator = 0, invert = 1;
	uint16_t arg1, arg2;
	arg1 = clk << 8 | encoding;
	arg2 = invert << 8 | separator;
	
	if ( !GetNedapBits(cardnumber, bs)) {
		PrintAndLogEx(WARNING, "Error with tag bitstream generation.");
		return 1;
	}	

	PrintAndLogEx(NORMAL, "bin  %s", sprint_bin_break(bs, 128, 32));
	PrintAndLogEx(NORMAL, "Simulating Nedap - CardNumber: %u", cardnumber );
	
	UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
	memcpy(c.d.asBytes, bs, size);
	clearCommandBuffer();
	SendCommand(&c);
	return 0;
}
Beispiel #6
0
// Simulation is still not working very good
int CmdHF15Sim(const char *Cmd)
{
	char cmdp = param_getchar(Cmd, 0);
	uint8_t uid[8] = {0x00};

	//E0 16 24 00 00 00 00 00
	if (cmdp == 'h' || cmdp == 'H') {
		PrintAndLog("Usage:  hf 15 sim <UID>");
		PrintAndLog("");
		PrintAndLog("     sample: hf 15 sim E016240000000000");
		return 0;
	}

	if (param_gethex(Cmd, 0, uid, 16)) {
		PrintAndLog("UID must include 16 HEX symbols");
		return 0;
	}
	
	PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
			uid[0],uid[1],uid[2],uid[3],uid[4], uid[5], uid[6], uid[7]);

	UsbCommand c = {CMD_SIMTAG_ISO_15693, {0, 0, 0}};
	memcpy(c.d.asBytes,uid,8);
	
	SendCommand(&c);
	return 0;
}
Beispiel #7
0
int CmdHF15Reader(const char *Cmd) {
	char cmdp = param_getchar(Cmd, 0);
	if (cmdp == 'h' || cmdp == 'H') return usage_15_reader();
	
	HF15Reader(Cmd, true);
	return 0;
}
Beispiel #8
0
int CmdPyramidSim(const char *Cmd) {

	char cmdp = param_getchar(Cmd, 0);
	if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_pyramid_sim();

	uint32_t facilitycode = 0, cardnumber = 0, fc = 0, cn = 0;

	uint8_t bs[128];
	size_t size = sizeof(bs);
	memset(bs, 0x00, size);

	// Pyramid uses:  fcHigh: 10, fcLow: 8, clk: 50, invert: 0
	uint64_t arg1, arg2;
	arg1 = (10 << 8) + 8;
	arg2 = 50 | 0;

	if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_pyramid_sim();

	facilitycode = (fc & 0x000000FF);
	cardnumber = (cn & 0x0000FFFF);

	if ( !GetPyramidBits(facilitycode, cardnumber, bs)) {
		PrintAndLog("Error with tag bitstream generation.");
		return 1;
	}

	PrintAndLog("Simulating Farpointe/Pyramid - Facility Code: %u, CardNumber: %u", facilitycode, cardnumber );

	UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
	memcpy(c.d.asBytes, bs, size);
	clearCommandBuffer();
	SendCommand(&c);
	return 0;
}
Beispiel #9
0
int CmdLegicLoad(const char *Cmd)
{
	char filename[FILE_PATH_SIZE] = {0x00};
	int len = 0;
	
	if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
		PrintAndLog("It loads datasamples from the file `filename`");
		PrintAndLog("Usage:  hf legic load <file name>");
		PrintAndLog(" sample: hf legic load filename");
		return 0;
	}

	len = strlen(Cmd);	
	if (len > FILE_PATH_SIZE) {
		PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE);
		return 0;
	}
	memcpy(filename, Cmd, len);

    FILE *f = fopen(filename, "r");
    if(!f) {
        PrintAndLog("couldn't open '%s'", Cmd);
        return -1;
    }
    char line[80]; int offset = 0; unsigned int data[8];
    while(fgets(line, sizeof(line), f)) {
        int res = sscanf(line, "%x %x %x %x %x %x %x %x", 
            &data[0], &data[1], &data[2], &data[3],
            &data[4], &data[5], &data[6], &data[7]);
        if(res != 8) {
          PrintAndLog("Error: could not read samples");
          fclose(f);
          return -1;
        }
        UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
        int j; for(j = 0; j < 8; j++) {
            c.d.asBytes[j] = data[j];
        }
        SendCommand(&c);
        WaitForResponse(CMD_ACK, NULL);
        offset += 8;
    }
    fclose(f);
    PrintAndLog("loaded %u samples", offset);
    return 0;
}
Beispiel #10
0
// Record Activity without enabeling carrier
//helptext
int CmdHF15Record(const char *Cmd) {
	char cmdp = param_getchar(Cmd, 0);
	if (cmdp == 'h' || cmdp == 'H') return usage_15_record();
	
	UsbCommand c = {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693, {0,0,0}};
	clearCommandBuffer();
	SendCommand(&c);
	return 0;
}
Beispiel #11
0
int CmdFdxClone(const char *Cmd) {

	uint32_t countryid = 0;
	uint64_t animalid = 0;
	uint32_t blocks[5] = {T55x7_MODULATION_DIPHASE | T55x7_BITRATE_RF_32 | 4 << T55x7_MAXBLOCK_SHIFT, 0, 0, 0, 0};
	uint8_t bits[128];
	uint8_t *bs = bits;
	memset(bs, 0, sizeof(bits));
	
	char cmdp = param_getchar(Cmd, 0);
	if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_fdx_clone();

	countryid = param_get32ex(Cmd, 0, 0, 10);
	animalid = param_get64ex(Cmd, 1, 0, 10);
	
	//Q5
	if (param_getchar(Cmd, 2) == 'Q' || param_getchar(Cmd, 2) == 'q') {
		//t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
		blocks[0] = T5555_MODULATION_BIPHASE | T5555_INVERT_OUTPUT | ((32-2)>>1) << T5555_BITRATE_SHIFT | 4 << T5555_MAXBLOCK_SHIFT;
	}
Beispiel #12
0
// finds the AFI (Application Family Idendifier) of a card, by trying all values
// (There is no standard way of reading the AFI, allthough some tags support this)
// helptext
int CmdHF15Afi(const char *Cmd) {
	char cmdp = param_getchar(Cmd, 0);
	if (cmdp == 'h' || cmdp == 'H') return usage_15_findafi();

	PrintAndLogEx(NORMAL, "press pm3-button to cancel");
	
	UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}};
	clearCommandBuffer();
	SendCommand(&c);
	return 0;
}
Beispiel #13
0
// * Acquire Samples as Reader (enables carrier, sends inquiry)
//helptext
int CmdHF15Samples(const char *Cmd) {
	char cmdp = param_getchar(Cmd, 0);
	if (cmdp == 'h' || cmdp == 'H') return usage_15_samples();

	UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693, {0,0,0}};
	clearCommandBuffer();	
	SendCommand(&c);
	
	//download samples 
	getSamples(0, false);
	return 0;
}
Beispiel #14
0
int CmdLFPCF7931Read(const char *Cmd) {

    uint8_t ctmp = param_getchar(Cmd, 0);
    if ( ctmp == 'H' || ctmp == 'h' ) return usage_pcf7931_read();

    UsbCommand resp;
    UsbCommand c = {CMD_PCF7931_READ, {0, 0, 0}};
    clearCommandBuffer();
    SendCommand(&c);
    if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) {
        PrintAndLog("command execution time out");
        return 1;
    }
    return 0;
}
Beispiel #15
0
int CmdLFPCF7931Config(const char *Cmd) {

    uint8_t ctmp = param_getchar(Cmd, 0);
    if ( ctmp == 0) return pcf7931_printConfig();
    if ( ctmp == 'H' || ctmp == 'h' ) return usage_pcf7931_config();
    if ( ctmp == 'R' || ctmp == 'r' ) return pcf7931_resetConfig();

    if ( param_gethex(Cmd, 0, configPcf.Pwd, 14) ) return usage_pcf7931_config();

    configPcf.InitDelay = (param_get32ex(Cmd,1,0,10) & 0xFFFF);
    configPcf.OffsetWidth = (int)(param_get32ex(Cmd,2,0,10) & 0xFFFF);
    configPcf.OffsetPosition = (int)(param_get32ex(Cmd,3,0,10) & 0xFFFF);

    pcf7931_printConfig();
    return 0;
}
int CmdLegicRFRead(const char *Cmd) {
	
	// params:
	// offset in data
	// number of bytes.
	char cmdp = param_getchar(Cmd, 0);
	if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_read();
	
	int byte_count=0, offset=0;
	sscanf(Cmd, "%i %i", &offset, &byte_count);
	if(byte_count == 0) byte_count = -1;
	if(byte_count + offset > 1024) byte_count = 1024 - offset;

	UsbCommand c= {CMD_READER_LEGIC_RF, {offset, byte_count, 0}};
	clearCommandBuffer();
	SendCommand(&c);
	return 0;
}
Beispiel #17
0
// Simulation is still not working very good
// helptext
int CmdHF15Sim(const char *Cmd) {
	char cmdp = param_getchar(Cmd, 0);
	if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') return usage_15_sim();

	uint8_t uid[8] = {0,0,0,0,0,0,0,0};	
	if (param_gethex(Cmd, 0, uid, 16)) {
		PrintAndLogEx(NORMAL, "UID must include 16 HEX symbols");
		return 0;
	}
	
	PrintAndLogEx(NORMAL, "Starting simulating UID %s", sprint_hex(uid, sizeof(uid)) );

	UsbCommand c = {CMD_SIMTAG_ISO_15693, {0, 0, 0}};
	memcpy(c.d.asBytes, uid, 8);
	clearCommandBuffer();
	SendCommand(&c);
	return 0;
}
Beispiel #18
0
int CmdHFDESDbg(const char *Cmd)
{
    int dbgMode = param_get32ex(Cmd, 0, 0, 10);
    if (dbgMode > 4) {
        PrintAndLog("Max debud mode parameter is 4 \n");
    }

    if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {
        PrintAndLog("Usage:  hf des dbg  <debug level>");
        PrintAndLog(" 0 - no debug messages");
        PrintAndLog(" 1 - error messages");
        PrintAndLog(" 2 - all messages");
        PrintAndLog(" 4 - extended debug mode");
        return 0;
    }

  UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
  SendCommand(&c);

  return 0;
}
Beispiel #19
0
// Reads all memory pages
// need to write to file
int CmdHF15Dump(const char*Cmd) {
	
	uint8_t fileNameLen = 0;
	char filename[FILE_PATH_SIZE] = {0};
	char * fptr = filename;
	bool errors = false;
	uint8_t cmdp = 0;
	uint8_t uid[8] = {0,0,0,0,0,0,0,0};	
	
	while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
		switch(param_getchar(Cmd, cmdp)) {
		case 'h':
		case 'H':
			return usage_15_dump();
		case 'f':
		case 'F':
			fileNameLen = param_getstr(Cmd, cmdp+1, filename, FILE_PATH_SIZE); 
			cmdp += 2;
			break;
		default:
			PrintAndLogEx(WARNING, "Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
			errors = true;
			break;
		}
	}

	//Validations
	if (errors) return usage_15_dump();
	
	if (fileNameLen < 1) {

		PrintAndLogEx(INFO, "Using UID as filename");

		if (!getUID(uid)) {
			PrintAndLogEx(WARNING, "No tag found.");
			return 1;
		}
		
		fptr += sprintf(fptr, "hf-15-"); 
		FillFileNameByUID(fptr,uid,"-dump",sizeof(uid));

	}	
	// detect blocksize from card :)
	
	PrintAndLogEx(NORMAL, "Reading memory from tag UID %s", sprintUID(NULL, uid));

	int blocknum = 0;
	uint8_t *recv = NULL;

	// memory.
	t15memory mem[256];
	
	uint8_t data[256*4] = {0};
	memset(data, 0, sizeof(data));

	UsbCommand resp;
	UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
	uint8_t *req = c.d.asBytes;
	req[0] = ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
	req[1] = ISO15_CMD_READ;

	// copy uid to read command
	memcpy(req+2, uid, sizeof(uid));
	
	for (int retry = 0; retry < 5; retry++) {
	
		req[10] = blocknum;
		AddCrc(req, 11);
		c.arg[0] = 13;
	
		clearCommandBuffer();
		SendCommand(&c);
				
		if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {

			uint8_t len = resp.arg[0];
			if ( len < 2 ) {
				PrintAndLogEx(FAILED, "iso15693 card select failed");
				continue;		
			}
			
			recv = resp.d.asBytes;
			
			if ( !CheckCrc(recv, len) ) {
				PrintAndLogEx(FAILED, "crc fail");
				continue;
			}

			if (recv[0] & ISO15_RES_ERROR) {
				PrintAndLogEx(FAILED, "Tag returned Error %i: %s", recv[1], TagErrorStr(recv[1]) ); 
				break;
			}
								
			mem[blocknum].lock = resp.d.asBytes[0];
			memcpy(mem[blocknum].block, resp.d.asBytes + 1, 4);					
			memcpy(data + (blocknum * 4), resp.d.asBytes + 1, 4);
			
			retry = 0;
			blocknum++;
			
			printf("."); fflush(stdout);
		} 
	}
	PrintAndLogEx(NORMAL, "\n");

	PrintAndLogEx(NORMAL, "block#   | data         |lck| ascii");
	PrintAndLogEx(NORMAL, "---------+--------------+---+----------");	
	for (int i = 0; i < blocknum; i++) {
		PrintAndLogEx(NORMAL, "%3d/0x%02X | %s | %d | %s", i, i, sprint_hex(mem[i].block, 4 ), mem[i].lock, sprint_ascii(mem[i].block, 4) );
	}
	PrintAndLogEx(NORMAL, "\n");

	size_t datalen = blocknum * 4;
	saveFileEML(filename, "eml", data, datalen, 4);	
	saveFile(filename, "bin", data, datalen);
	return 0;
}
Beispiel #20
0
int CmdHF15Restore(const char*Cmd) {
	FILE *file;
	
	uint8_t uid[8]={0x00};
	char filename[FILE_PATH_SIZE] = {0x00};
	char buff[255] = {0x00};
	size_t blocksize=4;
	uint8_t cmdp = 0;
	char newCmdPrefix[255] = {0x00}, tmpCmd[255] = {0x00};
	char param[FILE_PATH_SIZE]="";
	char hex[255]="";
	uint8_t retries = 3, tried = 0;
	int retval=0;
	size_t bytes_read;
	uint8_t i=0;
		while(param_getchar(Cmd, cmdp) != 0x00) {
		switch(tolower(param_getchar(Cmd, cmdp))) {
			case '-':
				param_getstr(Cmd, cmdp, param, sizeof(param));
				switch(param[1])
				{
					case '2':
					case 'o': strncpy(newCmdPrefix, " ",sizeof(newCmdPrefix)-1);
						strncat(newCmdPrefix, param, sizeof(newCmdPrefix)-1);
								break;
					default:
						PrintAndLogEx(WARNING, "Unknown parameter '%s'", param);
						return usage_15_restore();
				}
				break;
			case 'f':
				param_getstr(Cmd, cmdp+1, filename, FILE_PATH_SIZE);
				cmdp++;	
				break;
			case 'r':
				retries=param_get8ex(Cmd, cmdp+1, 3, 10);
				cmdp++;	
				break;
			case 'b':
				blocksize=param_get8ex(Cmd, cmdp+1, 4, 10);
				cmdp++;	
				break;
			case 'u':
				param_getstr(Cmd, cmdp+1, buff, FILE_PATH_SIZE);
				cmdp++;	
				snprintf(filename,sizeof(filename),"hf-15-dump-%s-bin",buff);				
				break;
			case 'h':
				return usage_15_restore();
			default:
				PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
				return usage_15_restore();
				break;
		}
		cmdp++;
	}
	PrintAndLogEx(INFO,"Blocksize: %u",blocksize);
	if(filename[0]=='\0')
	{
		PrintAndLogEx(WARNING,"Please provide a filename");
		return 1;
	}

	if ((file = fopen(filename,"rb")) == NULL) {
		PrintAndLogEx(WARNING, "Could not find file %s", filename);
		return 2;
	}
	
	if (!getUID(uid)) {
		PrintAndLogEx(WARNING, "No tag found");
		return 3;
	}
	while (1) {
		tried=0;
		hex[0]=0x00;
		tmpCmd[0]=0x00;
		
		bytes_read = fread( buff, 1, blocksize, file );
		if ( bytes_read == 0) {
			PrintAndLogEx(SUCCESS, "File reading done (%s).", filename);
			fclose(file);
			return 0;
		}
		else if ( bytes_read != blocksize) {
			PrintAndLogEx(WARNING, "File reading error (%s), %u bytes read instead of %u bytes.", filename, bytes_read, blocksize);
			fclose(file);
			return 2;
		}
		for(int j=0;j<blocksize;j++)
			snprintf(hex+j*2,3,"%02X", (unsigned char)buff[j]);
		for(int j=0;j<sizeof(uid)/sizeof(uid[0]);j++)
			snprintf(buff+j*2,3,"%02X", uid[j]);
		
		//TODO: Addressed mode currently not work
		//snprintf(tmpCmd, sizeof(tmpCmd), "%s %s %d %s", newCmdPrefix, buff, i, hex);
		snprintf(tmpCmd, sizeof(tmpCmd), "%s u %d %s", newCmdPrefix, i, hex);
		PrintAndLogEx(DEBUG, "Command to be sent: %s", tmpCmd);

		for(tried=0;tried<retries;tried++)
			if(!(retval=CmdHF15Write(tmpCmd)))
				break;
		if(tried >= retries)
			return retval;
		i++;
	}
	fclose(file);
}
int CmdLegicLoad(const char *Cmd) {
	
	char cmdp = param_getchar(Cmd, 0);
	if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load();

	char filename[FILE_PATH_SIZE] = {0x00};
	int len = strlen(Cmd);
	
	if (len > FILE_PATH_SIZE) {
		PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE);
		return 0;
	}
	memcpy(filename, Cmd, len);

    FILE *f = fopen(filename, "r");
    if(!f) {
        PrintAndLog("couldn't open '%s'", Cmd);
        return -1;
    }
	
    char line[80]; 
	int offset = 0; 
	uint8_t data[USB_CMD_DATA_SIZE] = {0x00};
	int index = 0;
	int totalbytes = 0;
    while ( fgets(line, sizeof(line), f) ) {
        int res = sscanf(line, "%x %x %x %x %x %x %x %x", 
            (unsigned int *)&data[index],
			(unsigned int *)&data[index + 1],
			(unsigned int *)&data[index + 2],
			(unsigned int *)&data[index + 3],
            (unsigned int *)&data[index + 4],
			(unsigned int *)&data[index + 5],
			(unsigned int *)&data[index + 6],
			(unsigned int *)&data[index + 7]);
			
        if(res != 8) {
          PrintAndLog("Error: could not read samples");
          fclose(f);
          return -1;
        }
		index += res;
			
		if ( index == USB_CMD_DATA_SIZE ){
//			PrintAndLog("sent %d | %d | %d", index, offset, totalbytes);
			UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
			memcpy(c.d.asBytes, data, sizeof(data));
			clearCommandBuffer();
			SendCommand(&c);
			if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
				PrintAndLog("Command execute timeout");
				fclose(f);
				return 1;
			}
			offset += index;
			totalbytes += index;
			index = 0;
		}
    }
    fclose(f);
	
	// left over bytes?
	if ( index != 0 ) {
		UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
		memcpy(c.d.asBytes, data, 8);
		clearCommandBuffer();
		SendCommand(&c);
		if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
				PrintAndLog("Command execute timeout");
				return 1;
		}
		totalbytes += index;		
	}
	
    PrintAndLog("loaded %u samples", totalbytes);
    return 0;
}
Beispiel #22
0
/**
 * Commandline handling: HF15 CMD SYSINFO
 * get system information from tag/VICC
 */
int CmdHF15Info(const char *Cmd) {

	char cmdp = param_getchar(Cmd, 0);
	if (strlen(Cmd)<1 || cmdp == 'h' || cmdp == 'H') return usage_15_info();

	UsbCommand resp;
	uint8_t *recv;
	UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
	uint8_t *req = c.d.asBytes;
	char cmdbuf[100];
	char *cmd = cmdbuf;
	memset(cmdbuf, 0, sizeof(cmdbuf));
	
	strncpy(cmd, Cmd, 99);
	
	if ( !prepareHF15Cmd(&cmd, &c, ISO15_CMD_SYSINFO) )
		return 0;

	AddCrc(req,  c.arg[0]);
	c.arg[0] += 2;

	//PrintAndLogEx(NORMAL, "cmd %s", sprint_hex(c.d.asBytes, reqlen) ); 
	
	clearCommandBuffer();
	SendCommand(&c);

	if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2000) ) {
		PrintAndLogEx(NORMAL, "iso15693 card select failed");
		return 1;
	}
	
	uint32_t status = resp.arg[0];
	
	if ( status < 2 ) {
		PrintAndLogEx(NORMAL, "iso15693 card doesn't answer to systeminfo command");
		return 1;		
	}
	
	recv = resp.d.asBytes;	
	
	if ( recv[0] & ISO15_RES_ERROR ) {
		PrintAndLogEx(NORMAL, "iso15693 card returned error %i: %s", recv[0], TagErrorStr(recv[0])); 
		return 3;
	}
	
	PrintAndLogEx(NORMAL, "  UID  : %s", sprintUID(NULL, recv+2));
	PrintAndLogEx(NORMAL, "  TYPE : %s", getTagInfo_15(recv+2));
	PrintAndLogEx(NORMAL, "  SYSINFO : %s", sprint_hex(recv, status-2));

	// DSFID
	if (recv[1] & 0x01) 
		PrintAndLogEx(NORMAL, "     - DSFID supported        [0x%02X]", recv[10]);
	else 
		PrintAndLogEx(NORMAL, "     - DSFID not supported");
	
	// AFI
	if (recv[1] & 0x02) 
		PrintAndLogEx(NORMAL, "     - AFI   supported        [0x%02X]", recv[11]);
	else 
		PrintAndLogEx(NORMAL, "     - AFI   not supported");

	// IC reference
	if (recv[1] & 0x08) 
		PrintAndLogEx(NORMAL, "     - IC reference supported [0x%02X]", recv[14]);
	else 
		PrintAndLogEx(NORMAL, "     - IC reference not supported");

	// memory 
	if (recv[1] & 0x04) {
		PrintAndLogEx(NORMAL, "     - Tag provides info on memory layout (vendor dependent)");
		uint8_t blocks = recv[12]+1;
		uint8_t size = (recv[13] & 0x1F);
		PrintAndLogEx(NORMAL, "           %u (or %u) bytes/blocks x %u blocks", size+1, size, blocks );
	} else {
		PrintAndLogEx(NORMAL, "     - Tag does not provide information on memory layout");
	}
	PrintAndLogEx(NORMAL, "\n");
	return 0;
}
Beispiel #23
0
int CmdHF15Raw(const char *Cmd) {

	char cmdp = param_getchar(Cmd, 0);
	if (strlen(Cmd)<3 || cmdp == 'h' || cmdp == 'H') return usage_15_raw();

	UsbCommand resp;
	UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
	int reply = 1, fast = 1, i = 0;
	bool crc = false;
	char buf[5] = "";
	uint8_t data[100];
	uint32_t datalen = 0, temp;
	
	// strip
	while (*Cmd==' ' || *Cmd=='\t') Cmd++;
	
	while (Cmd[i]!='\0') {
		if (Cmd[i]==' ' || Cmd[i]=='\t') { i++; continue; }
		if (Cmd[i]=='-') {
			switch (Cmd[i+1]) {
				case 'r': 
				case 'R': 
					reply = 0;
					break;
				case '2':
					fast = 0;
					break;
				case 'c':
				case 'C':				
					crc = true;
					break;
				default:
					PrintAndLogEx(WARNING, "Invalid option");
					return 0;
			}
			i+=2;
			continue;
		}
		if ((Cmd[i]>='0' && Cmd[i]<='9') ||
		    (Cmd[i]>='a' && Cmd[i]<='f') ||
		    (Cmd[i]>='A' && Cmd[i]<='F') ) {
		    buf[strlen(buf)+1] = 0;
		    buf[strlen(buf)] = Cmd[i];
		    i++;
		    
		    if (strlen(buf) >= 2) {
		    	sscanf(buf, "%x", &temp);
		    	data[datalen] = (uint8_t)(temp & 0xff);
		    	datalen++;
		    	*buf = 0;
		    }
		    continue;
		}
		PrintAndLogEx(WARNING, "Invalid char on input");
		return 0;
	}
	
	if (crc) {
		AddCrc(data, datalen);
		datalen += 2;
	}
	
	c.arg[0] = datalen;
	c.arg[1] = fast;
	c.arg[2] = reply;
	memcpy(c.d.asBytes, data, datalen);

	clearCommandBuffer();	
	SendCommand(&c);
	
	if (reply) {
		if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
			uint8_t len = resp.arg[0];
			PrintAndLogEx(NORMAL, "received %i octets", len);
			PrintAndLogEx(NORMAL, "%s", sprint_hex(resp.d.asBytes, len) );
		} else {
			PrintAndLogEx(WARNING, "timeout while waiting for reply.");
		}		
	}
	return 0;
}
Beispiel #24
0
// Mode 3
//helptext
int CmdHF15Demod(const char *Cmd) {
	char cmdp = param_getchar(Cmd, 0);
	if (cmdp == 'h' || cmdp == 'H') return usage_15_demod();
	
	// The sampling rate is 106.353 ksps/s, for T = 18.8 us
	int i, j;
	int max = 0, maxPos = 0;
	int skip = 4;

	if (GraphTraceLen < 1000) return 0;

	// First, correlate for SOF
	for (i = 0; i < 1000; i++) {
		int corr = 0;
		for (j = 0; j < ARRAYLEN(FrameSOF); j += skip) {
			corr += FrameSOF[j] * GraphBuffer[i + (j / skip)];
		}
		if (corr > max) {
			max = corr;
			maxPos = i;
		}
	}

	PrintAndLogEx(NORMAL, "SOF at %d, correlation %d", maxPos, max / (ARRAYLEN(FrameSOF) / skip));
	
	i = maxPos + ARRAYLEN(FrameSOF) / skip;
	int k = 0;
	uint8_t outBuf[20];
	memset(outBuf, 0, sizeof(outBuf));
	uint8_t mask = 0x01;
	for (;;) {
		int corr0 = 0, corr1 = 0, corrEOF = 0;
		for (j = 0; j < ARRAYLEN(Logic0); j += skip) {
			corr0 += Logic0[j] * GraphBuffer[i + (j / skip)];
		}
		for (j = 0; j < ARRAYLEN(Logic1); j += skip) {
			corr1 += Logic1[j] * GraphBuffer[i + (j / skip)];
		}
		for (j = 0; j < ARRAYLEN(FrameEOF); j += skip) {
			corrEOF += FrameEOF[j] * GraphBuffer[i + (j / skip)];
		}
		// Even things out by the length of the target waveform.
		corr0 *= 4;
		corr1 *= 4;
		
		if (corrEOF > corr1 && corrEOF > corr0) {
			PrintAndLogEx(NORMAL, "EOF at %d", i);
			break;
		} else if (corr1 > corr0) {
			i += ARRAYLEN(Logic1) / skip;
			outBuf[k] |= mask;
		} else {
			i += ARRAYLEN(Logic0) / skip;
		}
		mask <<= 1;
		if (mask == 0) {
			k++;
			mask = 0x01;
		}
		if ((i + (int)ARRAYLEN(FrameEOF)) >= GraphTraceLen) {
			PrintAndLogEx(NORMAL, "ran off end!");
			break;
		}
	}
	
	if (mask != 0x01) {
		PrintAndLogEx(WARNING, "Error, uneven octet! (discard extra bits!)");
		PrintAndLogEx(NORMAL, "   mask = %02x", mask);
	}
	PrintAndLogEx(NORMAL, "%d octets", k);
	
	for (i = 0; i < k; i++)
		PrintAndLogEx(NORMAL, "# %2d: %02x ", i, outBuf[i]);

	PrintAndLogEx(NORMAL, "CRC %04x", Crc(outBuf, k - 2));
	return 0;
}