fix 'hf iclass sim':

* ignore standard iso15693 INVENTORY commands silently
* make iso15693 command decoder more strict (prevent decoding rubbish)
* re-enable sim 3
This commit is contained in:
pwpiwi 2019-09-25 14:24:36 +02:00
commit e49d31c0e7
3 changed files with 12 additions and 14 deletions

View file

@ -1128,6 +1128,9 @@ int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
// Otherwise, we should answer 8bytes (block) + 2bytes CRC // Otherwise, we should answer 8bytes (block) + 2bytes CRC
} }
} else if (receivedCmd[0] == 0x26 && len == 5) {
// standard ISO15693 INVENTORY command. Ignore.
} else { } else {
// don't know how to handle this command // don't know how to handle this command
char debug_message[250]; // should be enough char debug_message[250]; // should be enough

View file

@ -827,6 +827,7 @@ static int inline __attribute__((always_inline)) Handle15693SampleFromReader(uin
break; break;
case STATE_READER_RECEIVE_DATA_1_OUT_OF_4: case STATE_READER_RECEIVE_DATA_1_OUT_OF_4:
bit = !!bit;
DecodeReader->posCount++; DecodeReader->posCount++;
if (DecodeReader->posCount == 1) { if (DecodeReader->posCount == 1) {
DecodeReader->sum1 = bit; DecodeReader->sum1 = bit;
@ -839,17 +840,14 @@ static int inline __attribute__((always_inline)) Handle15693SampleFromReader(uin
} }
if (DecodeReader->posCount == 8) { if (DecodeReader->posCount == 8) {
DecodeReader->posCount = 0; DecodeReader->posCount = 0;
int corr10 = DecodeReader->sum1 - DecodeReader->sum2; if (DecodeReader->sum1 <= 1 && DecodeReader->sum2 >= 3) { // EOF
int corr01 = DecodeReader->sum2 - DecodeReader->sum1;
int corr11 = (DecodeReader->sum1 + DecodeReader->sum2) / 2;
if (corr01 > corr11 && corr01 > corr10) { // EOF
LED_B_OFF(); // Finished receiving LED_B_OFF(); // Finished receiving
DecodeReaderReset(DecodeReader); DecodeReaderReset(DecodeReader);
if (DecodeReader->byteCount != 0) { if (DecodeReader->byteCount != 0) {
return true; return true;
} }
} }
if (corr10 > corr11) { // detected a 2bit position if (DecodeReader->sum1 >= 3 && DecodeReader->sum2 <= 1) { // detected a 2bit position
DecodeReader->shiftReg >>= 2; DecodeReader->shiftReg >>= 2;
DecodeReader->shiftReg |= (DecodeReader->bitCount << 6); DecodeReader->shiftReg |= (DecodeReader->bitCount << 6);
} }
@ -869,6 +867,7 @@ static int inline __attribute__((always_inline)) Handle15693SampleFromReader(uin
break; break;
case STATE_READER_RECEIVE_DATA_1_OUT_OF_256: case STATE_READER_RECEIVE_DATA_1_OUT_OF_256:
bit = !!bit;
DecodeReader->posCount++; DecodeReader->posCount++;
if (DecodeReader->posCount == 1) { if (DecodeReader->posCount == 1) {
DecodeReader->sum1 = bit; DecodeReader->sum1 = bit;
@ -881,17 +880,14 @@ static int inline __attribute__((always_inline)) Handle15693SampleFromReader(uin
} }
if (DecodeReader->posCount == 8) { if (DecodeReader->posCount == 8) {
DecodeReader->posCount = 0; DecodeReader->posCount = 0;
int corr10 = DecodeReader->sum1 - DecodeReader->sum2; if (DecodeReader->sum1 <= 1 && DecodeReader->sum2 >= 3) { // EOF
int corr01 = DecodeReader->sum2 - DecodeReader->sum1;
int corr11 = (DecodeReader->sum1 + DecodeReader->sum2) / 2;
if (corr01 > corr11 && corr01 > corr10) { // EOF
LED_B_OFF(); // Finished receiving LED_B_OFF(); // Finished receiving
DecodeReaderReset(DecodeReader); DecodeReaderReset(DecodeReader);
if (DecodeReader->byteCount != 0) { if (DecodeReader->byteCount != 0) {
return true; return true;
} }
} }
if (corr10 > corr11) { // detected the bit position if (DecodeReader->sum1 >= 3 && DecodeReader->sum2 <= 1) { // detected the bit position
DecodeReader->shiftReg = DecodeReader->bitCount; DecodeReader->shiftReg = DecodeReader->bitCount;
} }
if (DecodeReader->bitCount == 255) { // we have a full byte if (DecodeReader->bitCount == 255) { // we have a full byte

View file

@ -157,7 +157,6 @@ int CmdHFiClassSim(const char *Cmd) {
PrintAndLog("--simtype:%02x csn:%s", simType, sprint_hex(CSN, 8)); PrintAndLog("--simtype:%02x csn:%s", simType, sprint_hex(CSN, 8));
} }
uint8_t numberOfCSNs = 0;
if (simType == ICLASS_SIM_MODE_READER_ATTACK) { 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};
@ -196,8 +195,8 @@ int CmdHFiClassSim(const char *Cmd) {
saveFile("iclass_mac_attack", "bin", dump,datalen); saveFile("iclass_mac_attack", "bin", dump,datalen);
free(dump); free(dump);
} else if (simType == ICLASS_SIM_MODE_CSN || simType == ICLASS_SIM_MODE_CSN_DEFAULT) { } else if (simType == ICLASS_SIM_MODE_CSN || simType == ICLASS_SIM_MODE_CSN_DEFAULT || simType == ICLASS_SIM_MODE_FULL) {
UsbCommand c = {CMD_SIMULATE_TAG_ICLASS, {simType, numberOfCSNs}}; UsbCommand c = {CMD_SIMULATE_TAG_ICLASS, {simType, 0}};
memcpy(c.d.asBytes, CSN, 8); memcpy(c.d.asBytes, CSN, 8);
SendCommand(&c); SendCommand(&c);