Merge branch 'RfidResearchGroup:master' into master

This commit is contained in:
Christine Barron authored and GitHub committed 2022-02-15 10:24:56 -08:00
commit b7c6d07ee1
18 files changed
+581 -317

No files matched your search

+373 -163
View File
@@ -30,7 +30,9 @@
static int CmdHelp(const char *Cmd);
#define MAX_LENGTH 1024
#define LEGIC_PRIME_MIM22 22
#define LEGIC_PRIME_MIM256 256
#define LEGIC_PRIME_MIM1024 1024
static bool legic_xor(uint8_t *data, uint16_t cardsize) {
@@ -42,14 +44,14 @@ static bool legic_xor(uint8_t *data, uint16_t cardsize) {
uint8_t crc = data[4];
uint32_t calc_crc = CRC8Legic(data, 4);
if (crc != calc_crc) {
PrintAndLogEx(INFO, "Crc mismatch, obsfuscation not possible");
PrintAndLogEx(INFO, "CRC mismatch, obsfuscation not possible");
return false;
}
for (uint16_t i = 22; i < cardsize; i++) {
data[i] ^= crc;
}
PrintAndLogEx(SUCCESS, "applying xoring of data done!");
PrintAndLogEx(SUCCESS, "Applying xoring of data done!");
return true;
}
@@ -393,33 +395,30 @@ static int CmdLegicRdbl(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic rdbl",
"Read data from a LEGIC Prime tag",
"hf legic rdbl -o 0 -l 16 -> reads from byte[0] 16 bytes(system header)\n"
"hf legic rdbl -o 0 -l 4 --iv 55 -> reads from byte[0] 4 bytes with IV 0x55\n"
"hf legic rdbl -o 0 -l 256 --iv 55 -> reads from byte[0] 256 bytes with IV 0x55");
"hf legic rdbl -o 0 -l 16 -> read 16 bytes from offset 0 (system header)\n"
"hf legic rdbl -o 0 -l 4 --iv 55 -> read 4 bytes from offset 0\n"
"hf legic rdbl -o 0 -l 256 --iv 55 -> read 256 bytes from offset 0");
void *argtable[] = {
arg_param_begin,
arg_int1("o", "offset", "<dec>", "offset in data array to start download from"),
arg_int1("l", "length", "<dec>", "number of bytes to read"),
arg_int0("o", "offset", "<dec>", "offset in data array to start download from"),
arg_int0("l", "length", "<dec>", "number of bytes to read"),
arg_str0(NULL, "iv", "<hex>", "Initialization vector to use. Must be odd and 7bits max"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
int offset = arg_get_int_def(ctx, 1, 0);
int len = arg_get_int_def(ctx, 2, 0);
int len = arg_get_int_def(ctx, 2, 16);
int iv_len = 0;
uint8_t iv[1] = {0x01}; // formerly uidcrc
uint8_t iv[1] = {0x01};
CLIGetHexWithReturn(ctx, 3, iv, &iv_len);
CLIParserFree(ctx);
// sanity checks
if (len + offset >= MAX_LENGTH) {
PrintAndLogEx(WARNING, "Out-of-bounds, Cardsize = %d, [offset+len = %d ]", MAX_LENGTH, len + offset);
if (len + offset >= LEGIC_PRIME_MIM1024) {
PrintAndLogEx(WARNING, "Out-of-bounds, Cardsize = %d, [offset+len = %d ]", LEGIC_PRIME_MIM1024, len + offset);
return PM3_EOUTOFBOUND;
}
@@ -435,9 +434,9 @@ static int CmdLegicRdbl(const char *Cmd) {
uint16_t datalen = 0;
int status = legic_read_mem(offset, len, iv[0], data, &datalen);
if (status == PM3_SUCCESS) {
PrintAndLogEx(NORMAL, " ## | 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F");
PrintAndLogEx(NORMAL, "-----+------------------------------------------------------------------------------------------------");
print_hex_break(data, datalen, 32);
PrintAndLogEx(INFO, "## | 0 1 2 3 4 5 6 7 8 9 A B C D E F | ascii");
PrintAndLogEx(INFO, "---+-------------------------------------------------+-----------------");
print_hex_break(data, datalen, 16);
}
free(data);
return status;
@@ -446,17 +445,31 @@ static int CmdLegicRdbl(const char *Cmd) {
static int CmdLegicSim(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic sim",
"Simulates a LEGIC Prime tag. MIM22, MIM256, MIM1024 types can be emulated",
"hf legic sim -t 0 -> Simulate Type MIM22\n"
"hf legic sim -t 1 -> Simulate Type MIM256 (default)\n"
"hf legic sim -t 2 -> Simulate Type MIM1024");
"Simulates a LEGIC Prime tag.\n"
"Following types supported (MIM22, MIM256, MIM1024)",
"hf legic sim --22\n"
);
void *argtable[] = {
arg_param_begin,
arg_int0("t", "type", "<dec>", "Tag type to simulate."),
arg_lit0(NULL, "22", "LEGIC Prime MIM22"),
arg_lit0(NULL, "256", "LEGIC Prime MIM256 (def)"),
arg_lit0(NULL, "1024", "LEGIC Prime MIM1024"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
bool m1 = arg_get_lit(ctx, 1);
bool m2 = arg_get_lit(ctx, 2);
bool m3 = arg_get_lit(ctx, 3);
CLIParserFree(ctx);
// validations
if (m1 + m2 + m3 > 1) {
PrintAndLogEx(WARNING, "Only specify one LEGIC Prime Type");
return PM3_EINVARG;
} else if (m1 + m2 + m3 == 0) {
m2 = true;
}
struct {
uint8_t tagtype;
@@ -464,14 +477,12 @@ static int CmdLegicSim(const char *Cmd) {
} PACKED payload;
payload.send_reply = true;
payload.tagtype = arg_get_int_def(ctx, 1, 1);
CLIParserFree(ctx);
if (payload.tagtype > 2) {
PrintAndLogEx(ERR, "Invalid tag type selected.");
return PM3_EINVARG;
}
if (m1)
payload.tagtype = 0;
else if (m2)
payload.tagtype = 1;
else if (m3)
payload.tagtype = 2;
clearCommandBuffer();
SendCommandNG(CMD_HF_LEGIC_SIMULATE, (uint8_t *)&payload, sizeof(payload));
@@ -513,15 +524,13 @@ static int CmdLegicWrbl(const char *Cmd) {
int offset = arg_get_int_def(ctx, 1, 0);
int dlen = 0;
uint8_t data[MAX_LENGTH] = {0};
uint8_t data[LEGIC_PRIME_MIM1024] = {0};
CLIGetHexWithReturn(ctx, 2, data, &dlen);
bool autoconfirm = arg_get_lit(ctx, 3);
CLIParserFree(ctx);
uint32_t IV = 0x55;
// OUT-OF-BOUNDS checks
// UID 4+1 bytes can't be written to.
if (offset < 5) {
@@ -558,27 +567,34 @@ static int CmdLegicWrbl(const char *Cmd) {
}
}
uint32_t IV = 0x55;
legic_chk_iv(&IV);
PrintAndLogEx(SUCCESS, "Writing to tag");
PrintAndLogEx(SUCCESS, "Writing to tag to offset %i", offset);
legic_packet_t *payload = calloc(1, sizeof(legic_packet_t) + dlen);
payload->offset = (offset & 0xFFFF);
payload->iv = (IV & 0x7F);
payload->len = dlen;
memcpy(payload->data, data, dlen);
PacketResponseNG resp;
clearCommandBuffer();
SendCommandOLD(CMD_HF_LEGIC_WRITER, offset, dlen, IV, data, dlen);
SendCommandNG(CMD_HF_LEGIC_WRITER, (uint8_t *)payload, sizeof(legic_packet_t) + dlen);
free(payload);
uint8_t timeout = 0;
while (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
while (WaitForResponseTimeout(CMD_HF_LEGIC_WRITER, &resp, 2000) == false) {
++timeout;
PrintAndLogEx(NORMAL, "." NOLF);
if (timeout > 7) {
if (timeout > 10) {
PrintAndLogEx(WARNING, "\ncommand execution time out");
return PM3_ETIMEOUT;
}
}
PrintAndLogEx(NORMAL, "");
uint8_t isOK = resp.oldarg[0] & 0xFF;
if (!isOK) {
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(WARNING, "Failed writing tag");
return PM3_ERFTRANS;
}
@@ -633,12 +649,17 @@ int legic_read_mem(uint32_t offset, uint32_t len, uint32_t iv, uint8_t *out, uin
legic_chk_iv(&iv);
legic_packet_t *payload = calloc(1, sizeof(legic_packet_t));
payload->offset = (offset & 0xFFFF);
payload->iv = iv;
payload->len = len;
clearCommandBuffer();
SendCommandMIX(CMD_HF_LEGIC_READER, offset, len, iv, NULL, 0);
SendCommandNG(CMD_HF_LEGIC_READER, (uint8_t *)payload, sizeof(legic_packet_t));
PacketResponseNG resp;
uint8_t timeout = 0;
while (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
while (WaitForResponseTimeout(CMD_HF_LEGIC_READER, &resp, 1000) == false) {
++timeout;
PrintAndLogEx(NORMAL, "." NOLF);
if (timeout > 14) {
@@ -648,9 +669,9 @@ int legic_read_mem(uint32_t offset, uint32_t len, uint32_t iv, uint8_t *out, uin
}
PrintAndLogEx(NORMAL, "");
uint8_t isOK = resp.oldarg[0] & 0xFF;
*outlen = resp.oldarg[1];
if (!isOK) {
*outlen = resp.data.asDwords[0];
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(WARNING, "Failed reading tag");
return PM3_ESOFT;
}
@@ -659,7 +680,7 @@ int legic_read_mem(uint32_t offset, uint32_t len, uint32_t iv, uint8_t *out, uin
PrintAndLogEx(WARNING, "Fail, only managed to read %u bytes", *outlen);
// copy data from device
if (!GetFromDevice(BIG_BUF_EML, out, *outlen, 0, NULL, 0, NULL, 2500, false)) {
if (GetFromDevice(BIG_BUF_EML, out, *outlen, 0, NULL, 0, NULL, 2500, false) == false) {
PrintAndLogEx(WARNING, "Fail, transfer from device time-out");
return PM3_ETIMEOUT;
}
@@ -671,11 +692,11 @@ int legic_print_type(uint32_t tagtype, uint8_t spaces) {
spc[10] = 0x00;
char *spacer = spc + (10 - spaces);
if (tagtype == 22)
if (tagtype == LEGIC_PRIME_MIM22)
PrintAndLogEx(SUCCESS, "%sTYPE: " _YELLOW_("MIM%d card (outdated)"), spacer, tagtype);
else if (tagtype == 256)
else if (tagtype == LEGIC_PRIME_MIM256)
PrintAndLogEx(SUCCESS, "%sTYPE: " _YELLOW_("MIM%d card (234 bytes)"), spacer, tagtype);
else if (tagtype == 1024)
else if (tagtype == LEGIC_PRIME_MIM1024)
PrintAndLogEx(SUCCESS, "%sTYPE: " _YELLOW_("MIM%d card (1002 bytes)"), spacer, tagtype);
else
PrintAndLogEx(INFO, "%sTYPE: " _YELLOW_("Unknown %06x"), spacer, tagtype);
@@ -683,21 +704,22 @@ int legic_print_type(uint32_t tagtype, uint8_t spaces) {
}
int legic_get_type(legic_card_select_t *card) {
if (card == NULL) return PM3_EINVARG;
if (card == NULL)
return PM3_EINVARG;
clearCommandBuffer();
SendCommandNG(CMD_HF_LEGIC_INFO, NULL, 0);
PacketResponseNG resp;
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500))
if (WaitForResponseTimeout(CMD_HF_LEGIC_INFO, &resp, 1500) == false)
return PM3_ETIMEOUT;
uint8_t isOK = resp.oldarg[0] & 0xFF;
if (!isOK)
if (resp.status != PM3_SUCCESS)
return PM3_ESOFT;
memcpy(card, resp.data.asBytes, sizeof(legic_card_select_t));
return PM3_SUCCESS;
}
void legic_chk_iv(uint32_t *iv) {
if ((*iv & 0x7F) != *iv) {
*iv &= 0x7F;
@@ -709,20 +731,30 @@ void legic_chk_iv(uint32_t *iv) {
PrintAndLogEx(INFO, "LSB of IV must be SET %u", *iv);
}
}
void legic_seteml(uint8_t *src, uint32_t offset, uint32_t numofbytes) {
// fast push mode
g_conn.block_after_ACK = true;
for (size_t i = offset; i < numofbytes; i += PM3_CMD_DATA_SIZE) {
for (size_t i = offset; i < numofbytes; i += (PM3_CMD_DATA_SIZE - sizeof(legic_packet_t))) {
size_t len = MIN((numofbytes - i), PM3_CMD_DATA_SIZE);
size_t len = MIN((numofbytes - i), (PM3_CMD_DATA_SIZE - sizeof(legic_packet_t)));
if (len == numofbytes - i) {
// Disable fast mode on last packet
g_conn.block_after_ACK = false;
}
legic_packet_t *payload = calloc(1, sizeof(legic_packet_t) + len);
payload->offset = i;
payload->len = len;
memcpy(payload->data, src + i, len);
clearCommandBuffer();
SendCommandOLD(CMD_HF_LEGIC_ESET, i, len, 0, src + i, len);
SendCommandNG(CMD_HF_LEGIC_ESET, (uint8_t *)payload, sizeof(legic_packet_t) + len);
free(payload);
}
}
static int CmdLegicReader(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic reader",
@@ -748,10 +780,10 @@ static int CmdLegicReader(const char *Cmd) {
static int CmdLegicDump(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic dump",
"Read all memory from LEGIC Prime MIM22, MIM256, MIM1024 and saves bin/eml/json dump file\n"
"It autodetects card type.",
"Read all memory from LEGIC Prime tags and saves to (bin/eml/json) dump file\n"
"It autodetects card type (MIM22, MIM256, MIM1024)",
"hf legic dump --> use UID as filename\n"
"hf legic dump -f myfile --> use user specified filename\n"
"hf legic dump -f myfile \n"
"hf legic dump --de --> use UID as filename and deobfuscate data");
void *argtable[] = {
@@ -780,28 +812,32 @@ static int CmdLegicDump(const char *Cmd) {
legic_print_type(dumplen, 0);
PrintAndLogEx(SUCCESS, "Reading tag memory %d b...", dumplen);
legic_packet_t *payload = calloc(1, sizeof(legic_packet_t));
payload->offset = 0;
payload->iv = 0x55;
payload->len = dumplen;
clearCommandBuffer();
SendCommandMIX(CMD_HF_LEGIC_READER, 0x00, dumplen, 0x55, NULL, 0);
SendCommandNG(CMD_HF_LEGIC_READER, (uint8_t *)payload, sizeof(legic_packet_t));
PacketResponseNG resp;
uint8_t timeout = 0;
while (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
while (WaitForResponseTimeout(CMD_HF_LEGIC_READER, &resp, 2000) == false) {
++timeout;
PrintAndLogEx(NORMAL, "." NOLF);
if (timeout > 7) {
if (timeout > 10) {
PrintAndLogEx(WARNING, "\ncommand execution time out");
return PM3_ETIMEOUT;
}
}
PrintAndLogEx(NORMAL, "");
uint8_t isOK = resp.oldarg[0] & 0xFF;
if (!isOK) {
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(WARNING, "Failed dumping tag data");
return PM3_ERFTRANS;
}
uint16_t readlen = resp.oldarg[1];
uint16_t readlen = resp.data.asDwords[0];
uint8_t *data = calloc(readlen, sizeof(uint8_t));
if (!data) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
@@ -812,7 +848,7 @@ static int CmdLegicDump(const char *Cmd) {
PrintAndLogEx(WARNING, "Fail, only managed to read 0x%02X bytes of 0x%02X", readlen, dumplen);
// copy data from device
if (!GetFromDevice(BIG_BUF_EML, data, readlen, 0, NULL, 0, NULL, 2500, false)) {
if (GetFromDevice(BIG_BUF_EML, data, readlen, 0, NULL, 0, NULL, 2500, false) == false) {
PrintAndLogEx(WARNING, "Fail, transfer from device time-out");
free(data);
return PM3_ETIMEOUT;
@@ -846,7 +882,7 @@ static int CmdLegicDump(const char *Cmd) {
static int CmdLegicRestore(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic restore",
"Reads binary file and it autodetects card type and verifies that the file has the same size\n"
"Reads (bin/eml/json) file and it autodetects card type and verifies that the file has the same size\n"
"Then write the data back to card. All bytes except the first 7bytes [UID(4) MCC(1) DCF(2)]",
"hf legic restore -f myfile --> use user specified filename\n"
"hf legic restore -f myfile --ob --> use UID as filename and obfuscate data");
@@ -876,21 +912,43 @@ static int CmdLegicRestore(const char *Cmd) {
legic_print_type(card.cardsize, 0);
// set up buffer
uint8_t *data = calloc(card.cardsize, sizeof(uint8_t));
if (!data) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
uint8_t *data = NULL;
size_t bytes_read = 0;
int res = 0;
DumpFileType_t dftype = getfiletype(filename);
switch (dftype) {
case BIN: {
res = loadFile_safe(filename, ".bin", (void **)&data, &bytes_read);
break;
}
case EML: {
res = loadFileEML_safe(filename, (void **)&data, &bytes_read);
break;
}
case JSON: {
data = calloc(LEGIC_PRIME_MIM1024, sizeof(uint8_t));
if (data == NULL) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
}
res = loadFileJSON(filename, (void *)data, LEGIC_PRIME_MIM1024, &bytes_read, NULL);
break;
}
case DICTIONARY: {
PrintAndLogEx(ERR, "Error: Only BIN/JSON/EML formats allowed");
free(data);
return PM3_EINVARG;
}
}
size_t numofbytes;
if (loadFile_safe(filename, ".bin", (void **)&data, &numofbytes) != PM3_SUCCESS) {
if (res != PM3_SUCCESS) {
free(data);
PrintAndLogEx(WARNING, "Error, reading file");
return PM3_EFILE;
}
if (card.cardsize != numofbytes) {
PrintAndLogEx(WARNING, "Fail, filesize and cardsize is not equal. [%u != %zu]", card.cardsize, numofbytes);
// validation
if (card.cardsize != bytes_read) {
PrintAndLogEx(WARNING, "Fail, filesize and cardsize is not equal. [%u != %zu]", card.cardsize, bytes_read);
free(data);
return PM3_EFILE;
}
@@ -911,21 +969,30 @@ static int CmdLegicRestore(const char *Cmd) {
// transfer to device
PacketResponseNG resp;
for (size_t i = 7; i < numofbytes; i += PM3_CMD_DATA_SIZE) {
// 7 = skip UID bytes and MCC
for (size_t i = 7; i < bytes_read; i += PM3_CMD_DATA_SIZE) {
size_t len = MIN((numofbytes - i), PM3_CMD_DATA_SIZE);
if (len == numofbytes - i) {
size_t len = MIN((bytes_read - i), PM3_CMD_DATA_SIZE);
if (len == bytes_read - i) {
// Disable fast mode on last packet
g_conn.block_after_ACK = false;
}
legic_packet_t *payload = calloc(1, sizeof(legic_packet_t) + len);
payload->offset = i;
payload->iv = 0x55;
payload->len = len;
memcpy(payload->data, data + i, len);
clearCommandBuffer();
SendCommandOLD(CMD_HF_LEGIC_WRITER, i, len, 0x55, data + i, len);
SendCommandNG(CMD_HF_LEGIC_WRITER, (uint8_t *)payload, sizeof(legic_packet_t) + len);
free(payload);
uint8_t timeout = 0;
while (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
while (WaitForResponseTimeout(CMD_HF_LEGIC_WRITER, &resp, 2000) == false) {
++timeout;
PrintAndLogEx(NORMAL, "." NOLF);
if (timeout > 7) {
if (timeout > 10) {
PrintAndLogEx(WARNING, "\ncommand execution time out");
free(data);
return PM3_ETIMEOUT;
@@ -933,9 +1000,8 @@ static int CmdLegicRestore(const char *Cmd) {
}
PrintAndLogEx(NORMAL, "");
uint8_t isOK = resp.oldarg[0] & 0xFF;
if (!isOK) {
PrintAndLogEx(WARNING, "Failed writing tag [msg = %u]", (uint8_t)(resp.oldarg[1] & 0xFF));
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(WARNING, "Failed writing tag");
free(data);
return PM3_ERFTRANS;
}
@@ -950,15 +1016,14 @@ static int CmdLegicRestore(const char *Cmd) {
static int CmdLegicELoad(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic eload",
"Loads a LEGIC binary dump into emulator memory",
"hf legic eload -f myfile -t 0 -> Simulate Type MIM22\n"
"hf legic eload -f myfile -t 1 -> Simulate Type MIM256 (default)\n"
"hf legic eload -f myfile -t 2 -> Simulate Type MIM1024");
"Loads a LEGIC Prime dump file into emulator memory",
"hf legic eload -f myfile\n"
"hf legic eload -f myfile --obfuscate\n"
);
void *argtable[] = {
arg_param_begin,
arg_str1("f", "file", "<fn>", "Filename to restore"),
arg_int0("t", "type", "<dec>", "Tag type to simulate."),
arg_str1("f", "file", "<fn>", "Filename to load"),
arg_lit0(NULL, "obfuscate", "Obfuscate dump data (xor with MCC)"),
arg_param_end
};
@@ -968,67 +1033,81 @@ static int CmdLegicELoad(const char *Cmd) {
char filename[FILE_PATH_SIZE] = {0};
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
size_t numofbytes = 0;
switch (arg_get_int_def(ctx, 2, 1)) {
case 0:
numofbytes = 22;
break;
case 1:
numofbytes = 256;
break;
case 2:
numofbytes = 1024;
break;
default:
PrintAndLogEx(ERR, "Unknown tag type");
CLIParserFree(ctx);
return PM3_EINVARG;
}
bool shall_obsfuscate = arg_get_lit(ctx, 3);
bool shall_obsfuscate = arg_get_lit(ctx, 2);
CLIParserFree(ctx);
// set up buffer
uint8_t *data = calloc(numofbytes, sizeof(uint8_t));
if (!data) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
uint8_t *data = NULL;
size_t bytes_read = 0;
int res = 0;
DumpFileType_t dftype = getfiletype(filename);
switch (dftype) {
case BIN: {
res = loadFile_safe(filename, ".bin", (void **)&data, &bytes_read);
break;
}
case EML: {
res = loadFileEML_safe(filename, (void **)&data, &bytes_read);
break;
}
case JSON: {
data = calloc(LEGIC_PRIME_MIM1024, sizeof(uint8_t));
if (data == NULL) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
}
res = loadFileJSON(filename, (void *)data, LEGIC_PRIME_MIM1024, &bytes_read, NULL);
break;
}
case DICTIONARY: {
PrintAndLogEx(ERR, "Error: Only BIN/JSON/EML formats allowed");
free(data);
return PM3_EINVARG;
}
}
if (loadFile_safe(filename, ".bin", (void **)&data, &numofbytes) != PM3_SUCCESS) {
if (res != PM3_SUCCESS) {
free(data);
return PM3_EFILE;
}
// validation
if (bytes_read != LEGIC_PRIME_MIM22 &&
bytes_read != LEGIC_PRIME_MIM256 &&
bytes_read != LEGIC_PRIME_MIM1024) {
PrintAndLogEx(ERR, "File content error. Read %zu bytes", bytes_read);
free(data);
PrintAndLogEx(WARNING, "Error, reading file");
return PM3_EFILE;
}
if (shall_obsfuscate) {
legic_xor(data, numofbytes);
legic_xor(data, bytes_read);
}
PrintAndLogEx(SUCCESS, "Uploading to emulator memory");
legic_seteml(data, 0, numofbytes);
legic_seteml(data, 0, bytes_read);
free(data);
PrintAndLogEx(SUCCESS, "Done");
PrintAndLogEx(SUCCESS, "Done!");
return PM3_SUCCESS;
}
static int CmdLegicESave(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic esave",
"Saves bin/eml/json dump file of emulator memory",
"hf legic esave --> uses UID as filename\n"
"hf legic esave -f myfile -t 0 --> Type MIM22\n"
"hf legic esave -f myfile -t 1 --> Type MIM256 (default)\n"
"hf legic esave -f myfile -t 2 --> Type MIM1024");
"Saves a (bin/eml/json) dump file of emulator memory",
"hf legic esave --> uses UID as filename\n"
"hf legic esave -f myfile --22\n"
"hf legic esave -f myfile --22 --de\n"
);
void *argtable[] = {
arg_param_begin,
arg_str0("f", "file", "<fn>", "Filename to save"),
arg_int0("t", "type", "<dec>", "Tag type"),
arg_lit0(NULL, "deobfuscate", "De-obfuscate dump data (xor with MCC)"),
arg_lit0(NULL, "22", "LEGIC Prime MIM22"),
arg_lit0(NULL, "256", "LEGIC Prime MIM256 (def)"),
arg_lit0(NULL, "1024", "LEGIC Prime MIM1024"),
arg_lit0(NULL, "de", "De-obfuscate dump data (xor with MCC)"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -1037,38 +1116,38 @@ static int CmdLegicESave(const char *Cmd) {
char filename[FILE_PATH_SIZE] = {0};
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
size_t numofbytes = 0;
bool m1 = arg_get_lit(ctx, 2);
bool m2 = arg_get_lit(ctx, 3);
bool m3 = arg_get_lit(ctx, 4);
bool shall_deobsfuscate = arg_get_lit(ctx, 5);
CLIParserFree(ctx);
switch (arg_get_int_def(ctx, 2, 1)) {
case 0:
numofbytes = 22;
break;
case 1:
numofbytes = 256;
break;
case 2:
numofbytes = 1024;
break;
default:
PrintAndLogEx(ERR, "Unknown tag type");
CLIParserFree(ctx);
return PM3_EINVARG;
// validations
if (m1 + m2 + m3 > 1) {
PrintAndLogEx(WARNING, "Only specify one LEGIC Prime Type");
return PM3_EINVARG;
} else if (m1 + m2 + m3 == 0) {
m2 = true;
}
bool shall_deobsfuscate = arg_get_lit(ctx, 3);
CLIParserFree(ctx);
size_t numofbytes = LEGIC_PRIME_MIM256;
if (m1)
numofbytes = LEGIC_PRIME_MIM22;
else if (m2)
numofbytes = LEGIC_PRIME_MIM256;
else if (m3)
numofbytes = LEGIC_PRIME_MIM1024;
// set up buffer
uint8_t *data = calloc(numofbytes, sizeof(uint8_t));
if (!data) {
if (data == NULL) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
}
// download emulator memory
PrintAndLogEx(SUCCESS, "Reading emulator memory...");
if (!GetFromDevice(BIG_BUF_EML, data, numofbytes, 0, NULL, 0, NULL, 2500, false)) {
if (GetFromDevice(BIG_BUF_EML, data, numofbytes, 0, NULL, 0, NULL, 2500, false) == false) {
PrintAndLogEx(WARNING, "Fail, transfer from device time-out");
free(data);
return PM3_ETIMEOUT;
@@ -1091,6 +1170,65 @@ static int CmdLegicESave(const char *Cmd) {
return PM3_SUCCESS;
}
static int CmdLegicEView(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic eview",
"It displays emulator memory",
"hf legic eview\n"
"hf legic eview --22\n"
);
void *argtable[] = {
arg_param_begin,
arg_lit0(NULL, "22", "LEGIC Prime MIM22"),
arg_lit0(NULL, "256", "LEGIC Prime MIM256 (def)"),
arg_lit0(NULL, "1024", "LEGIC Prime MIM1024"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
bool m1 = arg_get_lit(ctx, 1);
bool m2 = arg_get_lit(ctx, 2);
bool m3 = arg_get_lit(ctx, 3);
CLIParserFree(ctx);
// validations
if (m1 + m2 + m3 > 1) {
PrintAndLogEx(WARNING, "Only specify one LEGIC Prime Type");
return PM3_EINVARG;
} else if (m1 + m2 + m3 == 0) {
m2 = true;
}
size_t bytes = LEGIC_PRIME_MIM256;
if (m1)
bytes = LEGIC_PRIME_MIM22;
else if (m2)
bytes = LEGIC_PRIME_MIM256;
else if (m3)
bytes = LEGIC_PRIME_MIM1024;
uint8_t *dump = calloc(bytes, sizeof(uint8_t));
if (dump == NULL) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
}
PrintAndLogEx(INFO, "downloading emulator memory");
if (GetFromDevice(BIG_BUF_EML, dump, bytes, 0, NULL, 0, NULL, 2500, false) == false) {
PrintAndLogEx(WARNING, "Fail, transfer from device time-out");
free(dump);
return PM3_ETIMEOUT;
}
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "## | 0 1 2 3 4 5 6 7 8 9 A B C D E F | ascii");
PrintAndLogEx(INFO, "---+-------------------------------------------------+-----------------");
print_hex_break(dump, bytes, 16);
free(dump);
return PM3_SUCCESS;
}
static int CmdLegicWipe(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic wipe",
@@ -1136,14 +1274,22 @@ static int CmdLegicWipe(const char *Cmd) {
// Disable fast mode on last packet
g_conn.block_after_ACK = false;
}
legic_packet_t *payload = calloc(1, sizeof(legic_packet_t) + len);
payload->offset = i;
payload->iv = 0x55;
payload->len = len;
memcpy(payload->data, data + i, len);
clearCommandBuffer();
SendCommandOLD(CMD_HF_LEGIC_WRITER, i, len, 0x55, data + i, len);
SendCommandNG(CMD_HF_LEGIC_WRITER, (uint8_t *)payload, sizeof(legic_packet_t) + len);
free(payload);
uint8_t timeout = 0;
while (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
while (WaitForResponseTimeout(CMD_HF_LEGIC_WRITER, &resp, 2000) == false) {
++timeout;
PrintAndLogEx(NORMAL, "." NOLF);
if (timeout > 7) {
if (timeout > 10) {
PrintAndLogEx(WARNING, "\ncommand execution time out");
free(data);
return PM3_ETIMEOUT;
@@ -1151,14 +1297,13 @@ static int CmdLegicWipe(const char *Cmd) {
}
PrintAndLogEx(NORMAL, "");
uint8_t isOK = resp.oldarg[0] & 0xFF;
if (!isOK) {
PrintAndLogEx(WARNING, "Failed writing tag [msg = %u]", (uint8_t)(resp.oldarg[1] & 0xFF));
if (resp.status != PM3_SUCCESS) {
PrintAndLogEx(WARNING, "failed writing tag");
free(data);
return PM3_ERFTRANS;
}
}
PrintAndLogEx(SUCCESS, "ok\n");
PrintAndLogEx(SUCCESS, "Done!\n");
free(data);
return PM3_SUCCESS;
}
@@ -1167,20 +1312,85 @@ static int CmdLegicList(const char *Cmd) {
return CmdTraceListAlias(Cmd, "hf legic", "legic");
}
static int CmdLegicView(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf legic view",
"Print a LEGIC Prime dump file (bin/eml/json)",
"hf legic view -f hf-legic-01020304-dump.bin"
);
void *argtable[] = {
arg_param_begin,
arg_str1("f", "file", "<fn>", "Filename of dump"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
int fnlen = 0;
char filename[FILE_PATH_SIZE];
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
CLIParserFree(ctx);
// reserve memory
uint8_t *dump = NULL;
size_t bytes_read = 0;
int res = 0;
DumpFileType_t dftype = getfiletype(filename);
switch (dftype) {
case BIN: {
res = loadFile_safe(filename, ".bin", (void **)&dump, &bytes_read);
break;
}
case JSON: {
dump = calloc(LEGIC_PRIME_MIM1024, sizeof(uint8_t));
if (dump == NULL) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
}
res = loadFileJSON(filename, (void *)dump, LEGIC_PRIME_MIM1024, &bytes_read, NULL);
break;
}
case EML:
res = loadFileEML_safe(filename, (void **)&dump, &bytes_read);
break;
case DICTIONARY: {
PrintAndLogEx(ERR, "Error: Only BIN/EML/JSON formats allowed");
free(dump);
return PM3_EINVARG;
}
}
if (res != PM3_SUCCESS) {
PrintAndLogEx(FAILED, "File: " _YELLOW_("%s") ": not found or locked.", filename);
free(dump);
return PM3_EFILE;
}
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "## | 0 1 2 3 4 5 6 7 8 9 A B C D E F | ascii");
PrintAndLogEx(INFO, "---+-------------------------------------------------+-----------------");
print_hex_break(dump, bytes_read, 16);
free(dump);
return PM3_SUCCESS;
}
static command_t CommandTable[] = {
{"-----------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("operations") " ---------------------"},
{"help", CmdHelp, AlwaysAvailable, "This help"},
{"list", CmdLegicList, AlwaysAvailable, "List LEGIC history"},
{"reader", CmdLegicReader, IfPm3Legicrf, "LEGIC Prime Reader UID and tag info"},
{"info", CmdLegicInfo, IfPm3Legicrf, "Display deobfuscated and decoded LEGIC Prime tag data"},
{"dump", CmdLegicDump, IfPm3Legicrf, "Dump LEGIC Prime tag to binary file"},
{"restore", CmdLegicRestore, IfPm3Legicrf, "Restore a dump file onto a LEGIC Prime tag"},
{"info", CmdLegicInfo, IfPm3Legicrf, "Display deobfuscated and decoded LEGIC Prime tag data"},
{"list", CmdLegicList, AlwaysAvailable, "List LEGIC history"},
{"rdbl", CmdLegicRdbl, IfPm3Legicrf, "Read bytes from a LEGIC Prime tag"},
{"sim", CmdLegicSim, IfPm3Legicrf, "Start tag simulator"},
{"wrbl", CmdLegicWrbl, IfPm3Legicrf, "Write data to a LEGIC Prime tag"},
{"crc", CmdLegicCalcCrc, AlwaysAvailable, "Calculate Legic CRC over given bytes"},
{"eload", CmdLegicELoad, AlwaysAvailable, "Load binary dump to emulator memory"},
{"esave", CmdLegicESave, AlwaysAvailable, "Save emulator memory to binary file"},
{"reader", CmdLegicReader, IfPm3Legicrf, "LEGIC Prime Reader UID and tag info"},
{"restore", CmdLegicRestore, IfPm3Legicrf, "Restore a dump file onto a LEGIC Prime tag"},
{"wipe", CmdLegicWipe, IfPm3Legicrf, "Wipe a LEGIC Prime tag"},
{"wrbl", CmdLegicWrbl, IfPm3Legicrf, "Write data to a LEGIC Prime tag"},
{"-----------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("simulation") " ---------------------"},
{"sim", CmdLegicSim, IfPm3Legicrf, "Start tag simulator"},
{"eload", CmdLegicELoad, IfPm3Legicrf, "Load binary dump to emulator memory"},
{"esave", CmdLegicESave, IfPm3Legicrf, "Save emulator memory to binary file"},
{"eview", CmdLegicEView, IfPm3Legicrf, "View emulator memory"},
{"-----------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("utils") " ---------------------"},
{"crc", CmdLegicCalcCrc, AlwaysAvailable, "Calculate Legic CRC over given bytes"},
{"view", CmdLegicView, AlwaysAvailable, "Display content from tag dump file"},
{NULL, NULL, NULL, NULL}
};
-1
View File
@@ -4152,7 +4152,6 @@ static int CmdHF14AMfEView(const char *Cmd) {
PrintAndLogEx(WARNING, "Fail, cannot allocate memory");
return PM3_EMALLOC;
}
memset(dump, 0, bytes);
PrintAndLogEx(INFO, "downloading emulator memory");
if (!GetFromDevice(BIG_BUF_EML, dump, bytes, 0, NULL, 0, NULL, 2500, false)) {
+7 -7
View File
@@ -390,18 +390,18 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
return PM3_ESOFT;
}
int16_t *chanR = calloc(width * height, sizeof(int16_t));
int16_t *chanR = calloc(((size_t)width) * height, sizeof(int16_t));
if (chanR == NULL) {
return PM3_EMALLOC;
}
int16_t *chanG = calloc(width * height, sizeof(int16_t));
int16_t *chanG = calloc(((size_t)width) * height, sizeof(int16_t));
if (chanG == NULL) {
free(chanR);
return PM3_EMALLOC;
}
int16_t *chanB = calloc(width * height, sizeof(int16_t));
int16_t *chanB = calloc(((size_t)width) * height, sizeof(int16_t));
if (chanB == NULL) {
free(chanR);
free(chanG);
@@ -423,14 +423,14 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
if ((model_nr == M1in54B) || (model_nr == M2in13B)) {
// for BW+Red screens:
uint8_t *mapBlack = calloc((uint32_t)(width * height), sizeof(uint8_t));
uint8_t *mapBlack = calloc(((size_t)width) * height, sizeof(uint8_t));
if (mapBlack == NULL) {
free(chanR);
free(chanG);
free(chanB);
return PM3_EMALLOC;
}
uint8_t *mapRed = calloc((uint32_t)(width * height), sizeof(uint8_t));
uint8_t *mapRed = calloc(((size_t)width) * height, sizeof(uint8_t));
if (mapRed == NULL) {
free(chanR);
free(chanG);
@@ -490,7 +490,7 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
free(mapRed);
} else {
// for BW-only screens:
int16_t *chanGrey = calloc(width * height, sizeof(int16_t));
int16_t *chanGrey = calloc(((size_t)width) * height, sizeof(int16_t));
if (chanGrey == NULL) {
free(chanR);
free(chanG);
@@ -500,7 +500,7 @@ static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, ui
rgb_to_gray(chanR, chanG, chanB, width, height, chanGrey);
dither_chan_inplace(chanGrey, width, height);
uint8_t *mapBlack = calloc(width * height, sizeof(uint8_t));
uint8_t *mapBlack = calloc(((size_t)width) * height, sizeof(uint8_t));
if (mapBlack == NULL) {
free(chanR);
free(chanG);
+3 -3
View File
@@ -375,8 +375,8 @@ static int CmdDetectReader(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("L", "LF", "detect low frequence 125/134 kHz"),
arg_lit0("H", "HF", "detect high frequence 13.56 MHZ"),
arg_lit0("L", "LF", "detect low frequency 125/134 kHz"),
arg_lit0("H", "HF", "detect high frequency 13.56 MHZ"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -385,7 +385,7 @@ static int CmdDetectReader(const char *Cmd) {
CLIParserFree(ctx);
if ((lf + hf) > 1) {
PrintAndLogEx(INFO, "Can only set one frequence");
PrintAndLogEx(INFO, "Can only set one frequency");
return PM3_EINVARG;
}
+4
View File
@@ -1239,6 +1239,10 @@ int loadFileJSONex(const char *preferredName, void *data, size_t maxdatalen, siz
JsonLoadBufAsHex(root, "$.raw", udata, maxdatalen, datalen);
}
if (!strcmp(ctype, "legic")) {
JsonLoadBufAsHex(root, "$.raw", udata, maxdatalen, datalen);
}
out:
if (callback != NULL) {
+13 -7
View File
@@ -147,9 +147,11 @@ void printarr(const char *name, uint8_t *arr, int len) {
char *output = calloc(outsize, sizeof(char));
cx = snprintf(output, outsize, "uint8_t %s[] = {", name);
for (i = 0; i < len; i++) {
cx += snprintf(output + cx, outsize - cx, "0x%02x,", *(arr + i)); //5 bytes per byte
if (cx < outsize)
cx += snprintf(output + cx, outsize - cx, "0x%02x,", *(arr + i)); //5 bytes per byte
}
snprintf(output + cx, outsize - cx, "};");
if (cx < outsize)
snprintf(output + cx, outsize - cx, "};");
PrintAndLogEx(INFO, output);
free(output);
}
@@ -165,12 +167,16 @@ void printarr_human_readable(const char *title, uint8_t *arr, int len) {
for (i = 0; i < len; i++) {
if (i % 16 == 0) {
if (i == 0)
cx += snprintf(output + cx, outsize - cx, "%02x| ", i);
else
cx += snprintf(output + cx, outsize - cx, "\n%02x| ", i);
if (i == 0) {
if (cx < outsize)
cx += snprintf(output + cx, outsize - cx, "%02x| ", i);
} else {
if (cx < outsize)
cx += snprintf(output + cx, outsize - cx, "\n%02x| ", i);
}
}
cx += snprintf(output + cx, outsize - cx, "%02x ", *(arr + i));
if (cx < outsize)
cx += snprintf(output + cx, outsize - cx, "%02x ", *(arr + i));
}
PrintAndLogEx(INFO, output);
free(output);
+1 -1
View File
@@ -872,7 +872,7 @@ int mfEmlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidt
uint8_t data[];
} PACKED;
size_t size = blocksCount * blockBtWidth;
size_t size = ((size_t) blocksCount) * blockBtWidth;
if (size > (PM3_CMD_DATA_SIZE - sizeof(struct p))) {
return PM3_ESOFT;
}
+11 -9
View File
@@ -262,18 +262,20 @@ const static vocabulory_t vocabulory[] = {
{ 1, "hf iclass permutekey" },
{ 1, "hf iclass view" },
{ 1, "hf legic help" },
{ 1, "hf legic list" },
{ 0, "hf legic reader" },
{ 0, "hf legic info" },
{ 0, "hf legic dump" },
{ 0, "hf legic restore" },
{ 0, "hf legic info" },
{ 1, "hf legic list" },
{ 0, "hf legic rdbl" },
{ 0, "hf legic sim" },
{ 0, "hf legic wrbl" },
{ 1, "hf legic crc" },
{ 1, "hf legic eload" },
{ 1, "hf legic esave" },
{ 0, "hf legic reader" },
{ 0, "hf legic restore" },
{ 0, "hf legic wipe" },
{ 0, "hf legic wrbl" },
{ 0, "hf legic sim" },
{ 0, "hf legic eload" },
{ 0, "hf legic esave" },
{ 0, "hf legic eview" },
{ 1, "hf legic crc" },
{ 1, "hf legic view" },
{ 1, "hf lto help" },
{ 0, "hf lto dump" },
{ 0, "hf lto restore" },