fix hf iclass sim

* sim 2: add responses to read(1) (Config) and read(5) (AIA)
* sim 2/3: don't restrict CC to 00 bytes only
* sim 3: add responding to read block commands
* sim 2/3: add responding to READ_CHECK_KC
* fix sizes of pre-encoded tag answers
* change default card challenge
* remove commented code
* use #defines instead of numerical constants for simulation modes
* some reformatting
This commit is contained in:
pwpiwi 2019-08-28 11:57:53 +02:00
commit 0ab9002f36
5 changed files with 227 additions and 224 deletions

View file

@ -391,7 +391,6 @@ static RAMFUNC int ManchesterDecoding(int v) {
Demod.shiftReg = 0; Demod.shiftReg = 0;
Demod.samples = 0; Demod.samples = 0;
if (Demod.posCount) { if (Demod.posCount) {
//if (trigger) LED_A_OFF(); // Not useful in this case...
switch (Demod.syncBit) { switch (Demod.syncBit) {
case 0x08: Demod.samples = 3; break; case 0x08: Demod.samples = 3; break;
case 0x04: Demod.samples = 2; break; case 0x04: Demod.samples = 2; break;
@ -414,6 +413,7 @@ static RAMFUNC int ManchesterDecoding(int v) {
} }
} else { } else {
// state is DEMOD is in SYNC from here on.
modulation = bit & Demod.syncBit; modulation = bit & Demod.syncBit;
modulation |= ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit; modulation |= ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
@ -428,14 +428,6 @@ static RAMFUNC int ManchesterDecoding(int v) {
} }
} else { } else {
Demod.posCount = 0; Demod.posCount = 0;
/*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
if (Demod.state!=DEMOD_ERROR_WAIT) {
Demod.state = DEMOD_ERROR_WAIT;
Demod.output[Demod.len] = 0xaa;
error = 0x01;
}
}*/
//else if (modulation) {
if (modulation) { if (modulation) {
if (Demod.sub == SUB_FIRST_HALF) { if (Demod.sub == SUB_FIRST_HALF) {
Demod.sub = SUB_BOTH; Demod.sub = SUB_BOTH;
@ -447,23 +439,16 @@ static RAMFUNC int ManchesterDecoding(int v) {
Demod.output[Demod.len] = 0x0f; Demod.output[Demod.len] = 0x0f;
Demod.len++; Demod.len++;
Demod.state = DEMOD_UNSYNCD; Demod.state = DEMOD_UNSYNCD;
// error = 0x0f;
return true; return true;
} else { } else {
Demod.state = DEMOD_ERROR_WAIT; Demod.state = DEMOD_ERROR_WAIT;
error = 0x33; error = 0x33;
} }
/*if (Demod.state!=DEMOD_ERROR_WAIT) {
Demod.state = DEMOD_ERROR_WAIT;
Demod.output[Demod.len] = 0xaa;
error = 0x01;
}*/
} }
switch(Demod.state) { switch(Demod.state) {
case DEMOD_START_OF_COMMUNICATION: case DEMOD_START_OF_COMMUNICATION:
if (Demod.sub == SUB_BOTH) { if (Demod.sub == SUB_BOTH) {
//Demod.state = DEMOD_MANCHESTER_D;
Demod.state = DEMOD_START_OF_COMMUNICATION2; Demod.state = DEMOD_START_OF_COMMUNICATION2;
Demod.posCount = 1; Demod.posCount = 1;
Demod.sub = SUB_NONE; Demod.sub = SUB_NONE;
@ -484,10 +469,7 @@ static RAMFUNC int ManchesterDecoding(int v) {
break; break;
case DEMOD_START_OF_COMMUNICATION3: case DEMOD_START_OF_COMMUNICATION3:
if (Demod.sub == SUB_SECOND_HALF) { if (Demod.sub == SUB_SECOND_HALF) {
// Demod.state = DEMOD_MANCHESTER_D;
Demod.state = DEMOD_SOF_COMPLETE; Demod.state = DEMOD_SOF_COMPLETE;
//Demod.output[Demod.len] = Demod.syncBit & 0xFF;
//Demod.len++;
} else { } else {
Demod.output[Demod.len] = 0xab; Demod.output[Demod.len] = 0xab;
Demod.state = DEMOD_ERROR_WAIT; Demod.state = DEMOD_ERROR_WAIT;
@ -543,16 +525,6 @@ static RAMFUNC int ManchesterDecoding(int v) {
break; break;
} }
/*if (Demod.bitCount>=9) {
Demod.output[Demod.len] = Demod.shiftReg & 0xff;
Demod.len++;
Demod.parityBits <<= 1;
Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
Demod.bitCount = 0;
Demod.shiftReg = 0;
}*/
if (Demod.bitCount >= 8) { if (Demod.bitCount >= 8) {
Demod.shiftReg >>= 1; Demod.shiftReg >>= 1;
Demod.output[Demod.len] = (Demod.shiftReg & 0xff); Demod.output[Demod.len] = (Demod.shiftReg & 0xff);
@ -787,8 +759,7 @@ void rotateCSN(uint8_t* originalCSN, uint8_t* rotatedCSN) {
// Stop when button is pressed // Stop when button is pressed
// Or return true when command is captured // Or return true when command is captured
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static int GetIClassCommandFromReader(uint8_t *received, int *len, int maxLen) static int GetIClassCommandFromReader(uint8_t *received, int *len, int maxLen) {
{
// Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
// only, since we are receiving, not transmitting). // only, since we are receiving, not transmitting).
// Signal field is off with the appropriate LED // Signal field is off with the appropriate LED
@ -825,7 +796,7 @@ static uint8_t encode4Bits(const uint8_t b) {
// The columns are // The columns are
// 1 - Bit value to send // 1 - Bit value to send
// 2 - Reversed (big-endian) // 2 - Reversed (big-endian)
// 3 - Encoded // 3 - Manchester Encoded
// 4 - Hex values // 4 - Hex values
switch(c){ switch(c){
@ -935,20 +906,19 @@ static int SendIClassAnswer(uint8_t *resp, int respLen, int delay) {
AT91C_BASE_SSC->SSC_THR = 0x00; AT91C_BASE_SSC->SSC_THR = 0x00;
FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR); FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR);
while (!BUTTON_PRESS()) { while (true) {
if ((AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)){ if ((AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)){
b = AT91C_BASE_SSC->SSC_RHR; (void) b; b = AT91C_BASE_SSC->SSC_RHR;
(void) b;
} }
if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)){ if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)){
b = 0x00; b = 0x00;
if (d < delay) { if (d < delay) {
// send 0x00 byte (causing a 2048/13,56MHz = 151us delay)
d++; d++;
} } else {
else {
if (i < respLen) { if (i < respLen) {
b = resp[i]; b = resp[i];
//Hack
//b = 0xAC;
} }
i++; i++;
} }
@ -957,15 +927,13 @@ static int SendIClassAnswer(uint8_t *resp, int respLen, int delay) {
// if (i > respLen +4) break; // if (i > respLen +4) break;
if (i > respLen + 1) break; if (i > respLen + 1) break;
// send 2 more 0x00 bytes (causing a 302us delay)
} }
return 0; return 0;
} }
#define MODE_SIM_CSN 0
#define MODE_EXIT_AFTER_MAC 1
#define MODE_FULLSIM 2
/** /**
* @brief Does the actual simulation * @brief Does the actual simulation
@ -973,14 +941,16 @@ static int SendIClassAnswer(uint8_t *resp, int respLen, int delay) {
* @param breakAfterMacReceived if true, returns after reader MAC has been received. * @param breakAfterMacReceived if true, returns after reader MAC has been received.
*/ */
int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) { int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
// free eventually allocated BigBuf memory // free eventually allocated BigBuf memory
BigBuf_free_keep_EM(); BigBuf_free_keep_EM();
State cipher_state; State cipher_state;
// State cipher_state_reserve;
uint8_t *csn = BigBuf_get_EM_addr(); uint8_t *emulator = BigBuf_get_EM_addr();
uint8_t *emulator = csn; uint8_t *csn = emulator;
uint8_t sof_data[] = { 0x0F } ; uint8_t sof_data[] = { 0x0F } ;
// CSN followed by two CRC bytes // CSN followed by two CRC bytes
uint8_t anticoll_data[10] = { 0 }; uint8_t anticoll_data[10] = { 0 };
uint8_t csn_data[10] = { 0 }; uint8_t csn_data[10] = { 0 };
@ -991,13 +961,14 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
rotateCSN(csn_data, anticoll_data); rotateCSN(csn_data, anticoll_data);
// Compute CRC on both CSNs // Compute CRC on both CSNs
ComputeCrc14443(CRC_ICLASS, anticoll_data, 8, &anticoll_data[8], &anticoll_data[9]); AppendCrc(anticoll_data, 8);
ComputeCrc14443(CRC_ICLASS, csn_data, 8, &csn_data[8], &csn_data[9]); AppendCrc(csn_data, 8);
uint8_t diversified_key[8] = { 0 }; uint8_t diversified_key[8] = { 0 };
// e-Purse // e-Purse
uint8_t card_challenge_data[8] = { 0x00 }; uint8_t card_challenge_data[8] = { 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
if (simulationMode == MODE_FULLSIM) { //uint8_t card_challenge_data[8] = { 0 };
if (simulationMode == ICLASS_SIM_MODE_FULL) {
// The diversified key should be stored on block 3 // The diversified key should be stored on block 3
// Get the diversified key from emulator memory // Get the diversified key from emulator memory
memcpy(diversified_key, emulator + (8 * 3), 8); memcpy(diversified_key, emulator + (8 * 3), 8);
@ -1006,6 +977,10 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
// Precalculate the cipher state, feeding it the CC // Precalculate the cipher state, feeding it the CC
cipher_state = opt_doTagMAC_1(card_challenge_data, diversified_key); cipher_state = opt_doTagMAC_1(card_challenge_data, diversified_key);
} }
// save card challenge for sim2,4 attack
if (reader_mac_buf != NULL) {
memcpy(reader_mac_buf, card_challenge_data, 8);
}
int exitLoop = 0; int exitLoop = 0;
// Reader 0a // Reader 0a
@ -1026,19 +1001,31 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
// Anticollision CSN (rotated CSN) // Anticollision CSN (rotated CSN)
// 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte) // 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte)
uint8_t *resp_anticoll = BigBuf_malloc(28); uint8_t *resp_anticoll = BigBuf_malloc(22);
int resp_anticoll_len; int resp_anticoll_len;
// CSN // CSN (block 0)
// 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte) // 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte)
uint8_t *resp_csn = BigBuf_malloc(30); uint8_t *resp_csn = BigBuf_malloc(22);
int resp_csn_len; int resp_csn_len;
// e-Purse // configuration (block 1) picopass 2ks
uint8_t *resp_conf = BigBuf_malloc(22);
int resp_conf_len;
uint8_t conf_data[10] = {0x12, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0xFF, 0x3C, 0x00, 0x00};
AppendCrc(conf_data, 8);
// e-Purse (block 2)
// 18: Takes 2 bytes for SOF/EOF and 8 * 2 = 16 bytes (2 bytes/bit) // 18: Takes 2 bytes for SOF/EOF and 8 * 2 = 16 bytes (2 bytes/bit)
uint8_t *resp_cc = BigBuf_malloc(20); uint8_t *resp_cc = BigBuf_malloc(18);
int resp_cc_len; int resp_cc_len;
// Application Issuer Area (block 5)
uint8_t *resp_aia = BigBuf_malloc(22);
int resp_aia_len;
uint8_t aia_data[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00};
AppendCrc(aia_data, 8);
uint8_t *receivedCmd = BigBuf_malloc(MAX_FRAME_SIZE); uint8_t *receivedCmd = BigBuf_malloc(MAX_FRAME_SIZE);
int len; int len;
@ -1055,25 +1042,30 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
memcpy(resp_anticoll, ToSend, ToSendMax); memcpy(resp_anticoll, ToSend, ToSendMax);
resp_anticoll_len = ToSendMax; resp_anticoll_len = ToSendMax;
// CSN // CSN (block 0)
CodeIClassTagAnswer(csn_data, sizeof(csn_data)); CodeIClassTagAnswer(csn_data, sizeof(csn_data));
memcpy(resp_csn, ToSend, ToSendMax); memcpy(resp_csn, ToSend, ToSendMax);
resp_csn_len = ToSendMax; resp_csn_len = ToSendMax;
// e-Purse // Configuration (block 1)
CodeIClassTagAnswer(conf_data, sizeof(conf_data));
memcpy(resp_conf, ToSend, ToSendMax);
resp_conf_len = ToSendMax;
// e-Purse (block 2)
CodeIClassTagAnswer(card_challenge_data, sizeof(card_challenge_data)); CodeIClassTagAnswer(card_challenge_data, sizeof(card_challenge_data));
memcpy(resp_cc, ToSend, ToSendMax); resp_cc_len = ToSendMax; memcpy(resp_cc, ToSend, ToSendMax);
resp_cc_len = ToSendMax;
// Application Issuer Area (block 5)
CodeIClassTagAnswer(aia_data, sizeof(aia_data));
memcpy(resp_aia, ToSend, ToSendMax);
resp_aia_len = ToSendMax;
//This is used for responding to READ-block commands or other data which is dynamically generated //This is used for responding to READ-block commands or other data which is dynamically generated
//First the 'trace'-data, not encoded for FPGA
uint8_t *data_generic_trace = BigBuf_malloc(8 + 2); // 8 bytes data + 2byte CRC is max tag answer uint8_t *data_generic_trace = BigBuf_malloc(8 + 2); // 8 bytes data + 2byte CRC is max tag answer
//Then storage for the modulated data
//Each bit is doubled when modulated for FPGA, and we also have SOF and EOF (2 bytes)
uint8_t *data_response = BigBuf_malloc( (8 + 2) * 2 + 2); uint8_t *data_response = BigBuf_malloc( (8 + 2) * 2 + 2);
// Start from off (no field generated)
//FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
//SpinDelay(200);
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN); FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN);
SpinDelay(100); SpinDelay(100);
StartCountSspClk(); StartCountSspClk();
@ -1081,8 +1073,6 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
SetAdcMuxFor(GPIO_MUXSEL_HIPKD); SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO14443A); FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO14443A);
// To control where we are in the protocol
int cmdsRecvd = 0;
uint32_t time_0 = GetCountSspClk(); uint32_t time_0 = GetCountSspClk();
uint32_t t2r_time =0; uint32_t t2r_time =0;
uint32_t r2t_time =0; uint32_t r2t_time =0;
@ -1091,6 +1081,7 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
bool buttonPressed = false; bool buttonPressed = false;
uint8_t response_delay = 1; uint8_t response_delay = 1;
while (!exitLoop) { while (!exitLoop) {
WDT_HIT();
response_delay = 1; response_delay = 1;
LED_B_OFF(); LED_B_OFF();
//Signal tracer //Signal tracer
@ -1105,41 +1096,95 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
//Signal tracer //Signal tracer
LED_C_ON(); LED_C_ON();
// Okay, look at the command now. // Now look at the reader command and provide appropriate responses
// default is no response:
modulated_response = NULL;
modulated_response_size = 0;
trace_data = NULL;
trace_data_size = 0;
if (receivedCmd[0] == ICLASS_CMD_ACTALL) { if (receivedCmd[0] == ICLASS_CMD_ACTALL) {
// Reader in anticollission phase // Reader in anticollission phase
modulated_response = resp_sof; modulated_response = resp_sof;
modulated_response_size = resp_sof_Len; //order = 1; modulated_response_size = resp_sof_Len;
trace_data = sof_data; trace_data = sof_data;
trace_data_size = sizeof(sof_data); trace_data_size = sizeof(sof_data);
} else if (receivedCmd[0] == ICLASS_CMD_READ_OR_IDENTIFY && len == 1) {
} else if (receivedCmd[0] == ICLASS_CMD_READ_OR_IDENTIFY && len == 1) { // identify
// Reader asks for anticollission CSN // Reader asks for anticollission CSN
modulated_response = resp_anticoll; modulated_response = resp_anticoll;
modulated_response_size = resp_anticoll_len; //order = 2; modulated_response_size = resp_anticoll_len;
trace_data = anticoll_data; trace_data = anticoll_data;
trace_data_size = sizeof(anticoll_data); trace_data_size = sizeof(anticoll_data);
//DbpString("Reader requests anticollission CSN:"); //DbpString("Reader requests anticollission CSN:");
} else if (receivedCmd[0] == ICLASS_CMD_READ_OR_IDENTIFY && len == 4) { // read block
uint16_t blockNo = receivedCmd[1];
if (simulationMode != ICLASS_SIM_MODE_FULL) {
// provide defaults for blocks 0, 1, 2, 5
switch (blockNo) {
case 0: // csn (block 00)
modulated_response = resp_csn;
modulated_response_size = resp_csn_len;
trace_data = csn_data;
trace_data_size = sizeof(csn_data);
break;
case 1: // configuration (block 01)
modulated_response = resp_conf;
modulated_response_size = resp_conf_len;
trace_data = conf_data;
trace_data_size = sizeof(conf_data);
break;
case 2: // e-purse (block 02)
modulated_response = resp_cc;
modulated_response_size = resp_cc_len;
trace_data = card_challenge_data;
trace_data_size = sizeof(card_challenge_data);
// set epurse of sim2,4 attack
if (reader_mac_buf != NULL) {
memcpy(reader_mac_buf, card_challenge_data, 8);
}
break;
case 5: // Application Issuer Area (block 05)
modulated_response = resp_aia;
modulated_response_size = resp_aia_len;
trace_data = aia_data;
trace_data_size = sizeof(aia_data);
break;
// default: don't respond
}
} else { // use data from emulator memory
memcpy(data_generic_trace, emulator + (receivedCmd[1] << 3), 8);
AppendCrc(data_generic_trace, 8);
trace_data = data_generic_trace;
trace_data_size = 10;
CodeIClassTagAnswer(trace_data, trace_data_size);
memcpy(data_response, ToSend, ToSendMax);
modulated_response = data_response;
modulated_response_size = ToSendMax;
}
} else if (receivedCmd[0] == ICLASS_CMD_SELECT) { } else if (receivedCmd[0] == ICLASS_CMD_SELECT) {
// Reader selects anticollission CSN. // Reader selects anticollission CSN.
// Tag sends the corresponding real CSN // Tag sends the corresponding real CSN
modulated_response = resp_csn; modulated_response = resp_csn;
modulated_response_size = resp_csn_len; //order = 3; modulated_response_size = resp_csn_len;
trace_data = csn_data; trace_data = csn_data;
trace_data_size = sizeof(csn_data); trace_data_size = sizeof(csn_data);
//DbpString("Reader selects anticollission CSN:");
} else if (receivedCmd[0] == ICLASS_CMD_READCHECK_KD) { } else if (receivedCmd[0] == ICLASS_CMD_READCHECK_KD
// Read e-purse (88 02) || receivedCmd[0] == ICLASS_CMD_READCHECK_KC) {
// Read e-purse (88 02 || 18 02)
modulated_response = resp_cc; modulated_response = resp_cc;
modulated_response_size = resp_cc_len; //order = 4; modulated_response_size = resp_cc_len;
trace_data = card_challenge_data; trace_data = card_challenge_data;
trace_data_size = sizeof(card_challenge_data); trace_data_size = sizeof(card_challenge_data);
LED_B_ON(); LED_B_ON();
} else if (receivedCmd[0] == ICLASS_CMD_CHECK) { } else if (receivedCmd[0] == ICLASS_CMD_CHECK) {
// Reader random and reader MAC!!! // Reader random and reader MAC!!!
if (simulationMode == MODE_FULLSIM) { if (simulationMode == ICLASS_SIM_MODE_FULL) {
//NR, from reader, is in receivedCmd +1 //NR, from reader, is in receivedCmd +1
opt_doTagMAC_2(cipher_state, receivedCmd+1, data_generic_trace, diversified_key); opt_doTagMAC_2(cipher_state, receivedCmd+1, data_generic_trace, diversified_key);
trace_data = data_generic_trace; trace_data = data_generic_trace;
trace_data_size = 4; trace_data_size = 4;
CodeIClassTagAnswer(trace_data, trace_data_size); CodeIClassTagAnswer(trace_data, trace_data_size);
@ -1150,11 +1195,7 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
//exitLoop = true; //exitLoop = true;
} else { // Not fullsim, we don't respond } else { // Not fullsim, we don't respond
// We do not know what to answer, so lets keep quiet // We do not know what to answer, so lets keep quiet
modulated_response = resp_sof; if (simulationMode == ICLASS_SIM_MODE_EXIT_AFTER_MAC) {
modulated_response_size = 0;
trace_data = NULL;
trace_data_size = 0;
if (simulationMode == MODE_EXIT_AFTER_MAC) {
// dbprintf:ing ... // dbprintf:ing ...
Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x" Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x"
,csn[0],csn[1],csn[2],csn[3],csn[4],csn[5],csn[6],csn[7]); ,csn[0],csn[1],csn[2],csn[3],csn[4],csn[5],csn[6],csn[7]);
@ -1163,7 +1204,8 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
receivedCmd[3], receivedCmd[4], receivedCmd[5], receivedCmd[3], receivedCmd[4], receivedCmd[5],
receivedCmd[6], receivedCmd[7], receivedCmd[8]); receivedCmd[6], receivedCmd[7], receivedCmd[8]);
if (reader_mac_buf != NULL) { if (reader_mac_buf != NULL) {
memcpy(reader_mac_buf, receivedCmd+1, 8); // save NR and MAC for sim 2,4
memcpy(reader_mac_buf + 8, receivedCmd + 1, 8);
} }
exitLoop = true; exitLoop = true;
} }
@ -1172,27 +1214,15 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
} else if (receivedCmd[0] == ICLASS_CMD_HALT && len == 1) { } else if (receivedCmd[0] == ICLASS_CMD_HALT && len == 1) {
// Reader ends the session // Reader ends the session
modulated_response = resp_sof; modulated_response = resp_sof;
modulated_response_size = 0; //order = 0; modulated_response_size = 0;
trace_data = NULL; trace_data = NULL;
trace_data_size = 0; trace_data_size = 0;
} else if (simulationMode == MODE_FULLSIM && receivedCmd[0] == ICLASS_CMD_READ_OR_IDENTIFY && len == 4) {
//Read block } else if (receivedCmd[0] == ICLASS_CMD_UPDATE && simulationMode == ICLASS_SIM_MODE_FULL) {
uint16_t blk = receivedCmd[1];
//Take the data...
memcpy(data_generic_trace, emulator + (blk << 3), 8);
//Add crc
AppendCrc(data_generic_trace, 8);
trace_data = data_generic_trace;
trace_data_size = 10;
CodeIClassTagAnswer(trace_data, trace_data_size);
memcpy(data_response, ToSend, ToSendMax);
modulated_response = data_response;
modulated_response_size = ToSendMax;
} else if (receivedCmd[0] == ICLASS_CMD_UPDATE && simulationMode == MODE_FULLSIM) {
// Probably the reader wants to update the nonce. Let's just ignore that for now. // Probably the reader wants to update the nonce. Let's just ignore that for now.
// OBS! If this is implemented, don't forget to regenerate the cipher_state // OBS! If this is implemented, don't forget to regenerate the cipher_state
//We're expected to respond with the data+crc, exactly what's already in the receivedcmd // We're expected to respond with the data+crc, exactly what's already in the receivedCmd
//receivedcmd is now UPDATE 1b | ADDRESS 1b| DATA 8b| Signature 4b or CRC 2b| // receivedCmd is now UPDATE 1b | ADDRESS 1b | DATA 8b | Signature 4b or CRC 2b
//Take the data... //Take the data...
memcpy(data_generic_trace, receivedCmd + 2, 8); memcpy(data_generic_trace, receivedCmd + 2, 8);
@ -1204,35 +1234,23 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
memcpy(data_response, ToSend, ToSendMax); memcpy(data_response, ToSend, ToSendMax);
modulated_response = data_response; modulated_response = data_response;
modulated_response_size = ToSendMax; modulated_response_size = ToSendMax;
} else if (receivedCmd[0] == ICLASS_CMD_PAGESEL) { } else if (receivedCmd[0] == ICLASS_CMD_PAGESEL) {
// Pagesel // Pagesel
// Pagesel enables to select a page in the selected chip memory and return its configuration block // Pagesel enables to select a page in the selected chip memory and return its configuration block
// Chips with a single page will not answer to this command // Chips with a single page will not answer to this command
// It appears we're fine ignoring this. // It appears we're fine ignoring this.
// Otherwise, we should answer 8bytes (block) + 2bytes CRC // Otherwise, we should answer 8bytes (block) + 2bytes CRC
} else { } else {
//#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
// Never seen this command before // Never seen this command before
Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x", print_result("Unhandled command received from reader ", receivedCmd, len);
len,
receivedCmd[0], receivedCmd[1], receivedCmd[2],
receivedCmd[3], receivedCmd[4], receivedCmd[5],
receivedCmd[6], receivedCmd[7], receivedCmd[8]);
// Do not respond // Do not respond
modulated_response = resp_sof;
modulated_response_size = 0; //order = 0;
trace_data = NULL;
trace_data_size = 0;
} }
if (cmdsRecvd > 100) {
//DbpString("100 commands later...");
//break;
} else {
cmdsRecvd++;
}
/** /**
A legit tag has about 380us delay between reader EOT and tag SOF. A legit tag has about 330us delay between reader EOT and tag SOF.
**/ **/
if (modulated_response_size > 0) { if (modulated_response_size > 0) {
SendIClassAnswer(modulated_response, modulated_response_size, response_delay); SendIClassAnswer(modulated_response, modulated_response_size, response_delay);
@ -1253,7 +1271,6 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
} }
} }
//Dbprintf("%x", cmdsRecvd);
LED_A_OFF(); LED_A_OFF();
LED_B_OFF(); LED_B_OFF();
LED_C_OFF(); LED_C_OFF();
@ -1280,6 +1297,7 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain) { void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain) {
uint32_t simType = arg0; uint32_t simType = arg0;
uint32_t numberOfCSNS = arg1; uint32_t numberOfCSNS = arg1;
FpgaDownloadAndGo(FPGA_BITSTREAM_HF); FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
// Enable and clear the trace // Enable and clear the trace
@ -1288,35 +1306,35 @@ void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain
//Use the emulator memory for SIM //Use the emulator memory for SIM
uint8_t *emulator = BigBuf_get_EM_addr(); uint8_t *emulator = BigBuf_get_EM_addr();
if (simType == 0) { if (simType == ICLASS_SIM_MODE_CSN) {
// Use the CSN from commandline // Use the CSN from commandline
memcpy(emulator, datain, 8); memcpy(emulator, datain, 8);
doIClassSimulation(MODE_SIM_CSN,NULL); doIClassSimulation(ICLASS_SIM_MODE_CSN, NULL);
} else if (simType == 1) { } else if (simType == ICLASS_SIM_MODE_CSN_DEFAULT) {
//Default CSN //Default CSN
uint8_t csn_crc[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 }; uint8_t csn_crc[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
// Use the CSN from commandline // Use the CSN from commandline
memcpy(emulator, csn_crc, 8); memcpy(emulator, csn_crc, 8);
doIClassSimulation(MODE_SIM_CSN,NULL); doIClassSimulation(ICLASS_SIM_MODE_CSN, NULL);
} else if (simType == 2) { } else if (simType == ICLASS_SIM_MODE_READER_ATTACK) {
uint8_t mac_responses[USB_CMD_DATA_SIZE] = { 0 }; uint8_t mac_responses[USB_CMD_DATA_SIZE] = { 0 };
Dbprintf("Going into attack mode, %d CSNS sent", numberOfCSNS); Dbprintf("Going into attack mode, %d CSNS sent", numberOfCSNS);
// In this mode, a number of csns are within datain. We'll simulate each one, one at a time // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
// in order to collect MAC's from the reader. This can later be used in an offlne-attack // in order to collect MAC's from the reader. This can later be used in an offline-attack
// in order to obtain the keys, as in the "dismantling iclass"-paper. // in order to obtain the keys, as in the "dismantling iclass"-paper.
int i = 0; int i;
for ( ; i < numberOfCSNS && i*8+8 < USB_CMD_DATA_SIZE; i++) { for (i = 0; i < numberOfCSNS && i*16+16 <= USB_CMD_DATA_SIZE; i++) {
// The usb data is 512 bytes, fitting 65 8-byte CSNs in there. // The usb data is 512 bytes, fitting 32 responses (8 byte CC + 4 Byte NR + 4 Byte MAC = 16 Byte response).
memcpy(emulator, datain+(i*8), 8); memcpy(emulator, datain+(i*8), 8);
if (doIClassSimulation(MODE_EXIT_AFTER_MAC,mac_responses+i*8)) { if (doIClassSimulation(ICLASS_SIM_MODE_EXIT_AFTER_MAC, mac_responses+i*16)) {
cmd_send(CMD_ACK, CMD_SIMULATE_TAG_ICLASS, i, 0, mac_responses, i*8); // Button pressed
return; // Button pressed break;
} }
} }
cmd_send(CMD_ACK, CMD_SIMULATE_TAG_ICLASS, i, 0, mac_responses, i*8); cmd_send(CMD_ACK, CMD_SIMULATE_TAG_ICLASS, i, 0, mac_responses, i*16);
} else if (simType == 3) { } else if (simType == ICLASS_SIM_MODE_FULL) {
//This is 'full sim' mode, where we use the emulator storage for data. //This is 'full sim' mode, where we use the emulator storage for data.
doIClassSimulation(MODE_FULLSIM, NULL); doIClassSimulation(ICLASS_SIM_MODE_FULL, NULL);
} else { } else {
// We may want a mode here where we hardcode the csns to use (from proxclone). // We may want a mode here where we hardcode the csns to use (from proxclone).
// That will speed things up a little, but not required just yet. // That will speed things up a little, but not required just yet.
@ -1683,10 +1701,10 @@ void ReaderIClass(uint8_t arg0) {
// 0 : CSN // 0 : CSN
// 1 : Configuration // 1 : Configuration
// 2 : e-purse // 2 : e-purse
// (3,4 write-only, kc and kd) // 3 : kd / debit / aa2 (write-only)
// 5 Application issuer area // 4 : kc / credit / aa1 (write-only)
// // 5 : AIA, Application issuer area
//Then we can 'ship' back the 8 * 6 bytes of data, //Then we can 'ship' back the 6 * 8 bytes of data,
// with 0xFF:s in block 3 and 4. // with 0xFF:s in block 3 and 4.
LED_B_ON(); LED_B_ON();

View file

@ -110,24 +110,16 @@ int CmdHFiClassSim(const char *Cmd) {
} }
simType = param_get8ex(Cmd, 0, 0, 10); simType = param_get8ex(Cmd, 0, 0, 10);
if(simType == 0) if (simType == ICLASS_SIM_MODE_CSN) {
{
if (param_gethex(Cmd, 1, CSN, 16)) { if (param_gethex(Cmd, 1, CSN, 16)) {
PrintAndLog("A CSN should consist of 16 HEX symbols"); PrintAndLog("A CSN should consist of 16 HEX symbols");
return usage_hf_iclass_sim(); return usage_hf_iclass_sim();
} }
PrintAndLog("--simtype:%02x csn:%s", simType, sprint_hex(CSN, 8)); PrintAndLog("--simtype:%02x csn:%s", simType, sprint_hex(CSN, 8));
} }
if(simType > 3)
{
PrintAndLog("Undefined simptype %d", simType);
return usage_hf_iclass_sim();
}
uint8_t numberOfCSNs = 0; uint8_t numberOfCSNs = 0;
if(simType == 2) if (simType == ICLASS_SIM_MODE_READER_ATTACK) {
{
UsbCommand c = {CMD_SIMULATE_TAG_ICLASS, {simType, NUM_CSNS}}; UsbCommand c = {CMD_SIMULATE_TAG_ICLASS, {simType, NUM_CSNS}};
UsbCommand resp = {0}; UsbCommand resp = {0};
@ -167,28 +159,29 @@ int CmdHFiClassSim(const char *Cmd) {
* 8 * 24 bytes. * 8 * 24 bytes.
* *
* The returndata from the pm3 is on the following format * The returndata from the pm3 is on the following format
* <4 byte NR><4 byte MAC> * <8 byte CC><4 byte NR><4 byte MAC>
* CC are all zeroes, CSN is the same as was sent in * CSN is the same as was sent in
**/ **/
void* dump = malloc(datalen); void* dump = malloc(datalen);
memset(dump,0,datalen);//<-- Need zeroes for the CC-field for(int i = 0; i < NUM_CSNS; i++) {
uint8_t i = 0;
for(i = 0 ; i < NUM_CSNS ; i++)
{
memcpy(dump + i*24, csns+i*8, 8); //CSN memcpy(dump + i*24, csns+i*8, 8); //CSN
//8 zero bytes here... //copy CC from response
memcpy(dump + i*24 + 8, resp.d.asBytes + i*16, 8);
//Then comes NR_MAC (eight bytes from the response) //Then comes NR_MAC (eight bytes from the response)
memcpy(dump+i*24+16,resp.d.asBytes+i*8,8); memcpy(dump + i*24 + 16, resp.d.asBytes + i*16 + 8, 8);
} }
/** Now, save to dumpfile **/ /** Now, save to dumpfile **/
saveFile("iclass_mac_attack", "bin", dump,datalen); saveFile("iclass_mac_attack", "bin", dump,datalen);
free(dump); free(dump);
}else
{ } else if (simType == ICLASS_SIM_MODE_CSN || simType == ICLASS_SIM_MODE_CSN_DEFAULT) {
UsbCommand c = {CMD_SIMULATE_TAG_ICLASS, {simType, numberOfCSNs}}; UsbCommand c = {CMD_SIMULATE_TAG_ICLASS, {simType, numberOfCSNs}};
memcpy(c.d.asBytes, CSN, 8); memcpy(c.d.asBytes, CSN, 8);
SendCommand(&c); SendCommand(&c);
} else {
PrintAndLog("Undefined simtype %d", simType);
return usage_hf_iclass_sim();
} }
return 0; return 0;
@ -1265,24 +1258,18 @@ int CmdHFiClass_loclass(const char *Cmd) {
return 0; return 0;
} }
char fileName[255] = {0}; char fileName[255] = {0};
if(opt == 'f') if(opt == 'f') {
{ if(param_getstr(Cmd, 1, fileName, sizeof(fileName)) > 0) {
if(param_getstr(Cmd, 1, fileName, sizeof(fileName)) > 0)
{
return bruteforceFileNoKeys(fileName); return bruteforceFileNoKeys(fileName);
}else } else {
{
PrintAndLog("You must specify a filename"); PrintAndLog("You must specify a filename");
} }
} } else if(opt == 't') {
else if(opt == 't')
{
int errors = testCipherUtils(); int errors = testCipherUtils();
errors += testMAC(); errors += testMAC();
errors += doKeyTests(0); errors += doKeyTests(0);
errors += testElite(); errors += testElite();
if(errors) if(errors) {
{
prnlog("OBS! There were errors!!!"); prnlog("OBS! There were errors!!!");
} }
return errors; return errors;

View file

@ -240,6 +240,7 @@ void doMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4])
//free(cc_nr); //free(cc_nr);
return; return;
} }
void doMAC_N(uint8_t *address_data_p, uint8_t address_data_size, uint8_t *div_key_p, uint8_t mac[4]) void doMAC_N(uint8_t *address_data_p, uint8_t address_data_size, uint8_t *div_key_p, uint8_t mac[4])
{ {
uint8_t *address_data; uint8_t *address_data;

View file

@ -334,15 +334,13 @@ int bruteforceItem(dumpdata item, uint16_t keytable[])
**/ **/
uint8_t bytes_to_recover[3] = {0}; uint8_t bytes_to_recover[3] = {0};
uint8_t numbytes_to_recover = 0; uint8_t numbytes_to_recover = 0;
int i;
for(i =0 ; i < 8 ; i++) for(int i = 0; i < 8; i++) {
{
if (keytable[key_index[i]] & (CRACKED | BEING_CRACKED)) continue; if (keytable[key_index[i]] & (CRACKED | BEING_CRACKED)) continue;
bytes_to_recover[numbytes_to_recover++] = key_index[i]; bytes_to_recover[numbytes_to_recover++] = key_index[i];
keytable[key_index[i]] |= BEING_CRACKED; keytable[key_index[i]] |= BEING_CRACKED;
if(numbytes_to_recover > 3) if(numbytes_to_recover > 3) {
{
prnlog("The CSN requires > 3 byte bruteforce, not supported"); prnlog("The CSN requires > 3 byte bruteforce, not supported");
printvar("CSN", item.csn,8); printvar("CSN", item.csn,8);
printvar("HASH1", key_index,8); printvar("HASH1", key_index,8);
@ -370,15 +368,14 @@ int bruteforceItem(dumpdata item, uint16_t keytable[])
uint32_t endmask = 1 << 8*numbytes_to_recover; uint32_t endmask = 1 << 8*numbytes_to_recover;
for(i =0 ; i < numbytes_to_recover && numbytes_to_recover > 1; i++) for (int i = 0; i < numbytes_to_recover && numbytes_to_recover > 1; i++) {
prnlog("Bruteforcing byte %d", bytes_to_recover[i]); prnlog("Bruteforcing byte %d", bytes_to_recover[i]);
}
while(!found && !(brute & endmask)) while(!found && !(brute & endmask)) {
{
//Update the keytable with the brute-values //Update the keytable with the brute-values
for(i =0 ; i < numbytes_to_recover; i++) for (int i = 0; i < numbytes_to_recover; i++) {
{
keytable[bytes_to_recover[i]] &= 0xFF00; keytable[bytes_to_recover[i]] &= 0xFF00;
keytable[bytes_to_recover[i]] |= (brute >> (i*8) & 0xFF); keytable[bytes_to_recover[i]] |= (brute >> (i*8) & 0xFF);
} }
@ -396,40 +393,32 @@ int bruteforceItem(dumpdata item, uint16_t keytable[])
//Calc mac //Calc mac
doMAC(item.cc_nr, div_key, calculated_MAC); doMAC(item.cc_nr, div_key, calculated_MAC);
if(memcmp(calculated_MAC, item.mac, 4) == 0) if (memcmp(calculated_MAC, item.mac, 4) == 0) {
{ for (int i =0 ; i < numbytes_to_recover; i++)
for(i =0 ; i < numbytes_to_recover; i++)
prnlog("=> %d: 0x%02x", bytes_to_recover[i], 0xFF & keytable[bytes_to_recover[i]]); prnlog("=> %d: 0x%02x", bytes_to_recover[i], 0xFF & keytable[bytes_to_recover[i]]);
found = true; found = true;
break; break;
} }
brute++; brute++;
if((brute & 0xFFFF) == 0) if ((brute & 0xFFFF) == 0) {
{
printf("%d",(brute >> 16) & 0xFF); printf("%d",(brute >> 16) & 0xFF);
fflush(stdout); fflush(stdout);
} }
} }
if(! found) if (! found) {
{
prnlog("Failed to recover %d bytes using the following CSN",numbytes_to_recover); prnlog("Failed to recover %d bytes using the following CSN",numbytes_to_recover);
printvar("CSN",item.csn,8); printvar("CSN",item.csn,8);
errors++; errors++;
//Before we exit, reset the 'BEING_CRACKED' to zero //Before we exit, reset the 'BEING_CRACKED' to zero
for(i =0 ; i < numbytes_to_recover; i++) for (int i = 0; i < numbytes_to_recover; i++) {
{
keytable[bytes_to_recover[i]] &= 0xFF; keytable[bytes_to_recover[i]] &= 0xFF;
keytable[bytes_to_recover[i]] |= CRACK_FAILED; keytable[bytes_to_recover[i]] |= CRACK_FAILED;
} }
} else {
}else for (int i = 0 ;i < numbytes_to_recover; i++) {
{
for(i =0 ; i < numbytes_to_recover; i++)
{
keytable[bytes_to_recover[i]] &= 0xFF; keytable[bytes_to_recover[i]] &= 0xFF;
keytable[bytes_to_recover[i]] |= CRACKED; keytable[bytes_to_recover[i]] |= CRACKED;
} }
} }
return errors; return errors;
} }

View file

@ -236,7 +236,7 @@ typedef struct{
#define FLAG_RANDOM_NONCE (1<<5) #define FLAG_RANDOM_NONCE (1<<5)
//Iclass reader flags // iCLASS reader flags
#define FLAG_ICLASS_READER_ONLY_ONCE 0x01 #define FLAG_ICLASS_READER_ONLY_ONCE 0x01
#define FLAG_ICLASS_READER_CC 0x02 #define FLAG_ICLASS_READER_CC 0x02
#define FLAG_ICLASS_READER_CSN 0x04 #define FLAG_ICLASS_READER_CSN 0x04
@ -245,6 +245,14 @@ typedef struct{
#define FLAG_ICLASS_READER_ONE_TRY 0x20 #define FLAG_ICLASS_READER_ONE_TRY 0x20
#define FLAG_ICLASS_READER_CEDITKEY 0x40 #define FLAG_ICLASS_READER_CEDITKEY 0x40
// iCLASS simulation modes
#define ICLASS_SIM_MODE_CSN 0
#define ICLASS_SIM_MODE_CSN_DEFAULT 1
#define ICLASS_SIM_MODE_READER_ATTACK 2
#define ICLASS_SIM_MODE_FULL 3
#define ICLASS_SIM_MODE_READER_ATTACK_KEYROLL 4
#define ICLASS_SIM_MODE_EXIT_AFTER_MAC 5 // note: device internal only
// hw tune args // hw tune args
#define FLAG_TUNE_LF 1 #define FLAG_TUNE_LF 1