fix: loaddictionary_save, now with correct mem clear.

This commit is contained in:
iceman1001 2019-08-28 21:15:32 +02:00
commit ad3fe38b45
2 changed files with 44 additions and 31 deletions

View file

@ -283,9 +283,9 @@ int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, s
} }
case jsfIclass: { case jsfIclass: {
JsonSaveStr(root, "FileType", "iclass"); JsonSaveStr(root, "FileType", "iclass");
uint8_t uid[8] = {0}; uint8_t csn[8] = {0};
memcpy(uid, data, 8); memcpy(csn, data, 8);
JsonSaveBufAsHexCompact(root, "$.Card.UID", uid, sizeof(uid)); JsonSaveBufAsHexCompact(root, "$.Card.CSN", csn, sizeof(csn));
for (size_t i = 0; i < (datalen / 8); i++) { for (size_t i = 0; i < (datalen / 8); i++) {
char path[PATH_MAX_LENGTH] = {0}; char path[PATH_MAX_LENGTH] = {0};
@ -316,13 +316,13 @@ int createMfcKeyDump(uint8_t sectorsCnt, sector_t *e_sector, char *fptr) {
int i; int i;
if (fptr == NULL) { if (fptr == NULL) {
return 1; return PM3_EINVARG;
} }
FILE *fkeys = fopen(fptr, "wb"); FILE *fkeys = fopen(fptr, "wb");
if (fkeys == NULL) { if (fkeys == NULL) {
PrintAndLogEx(WARNING, "Could not create file " _YELLOW_("%s"), fptr); PrintAndLogEx(WARNING, "Could not create file " _YELLOW_("%s"), fptr);
return 1; return PM3_EFILE;
} }
PrintAndLogEx(SUCCESS, "Printing keys to binary file " _YELLOW_("%s")"...", fptr); PrintAndLogEx(SUCCESS, "Printing keys to binary file " _YELLOW_("%s")"...", fptr);
@ -338,14 +338,14 @@ int createMfcKeyDump(uint8_t sectorsCnt, sector_t *e_sector, char *fptr) {
fclose(fkeys); fclose(fkeys);
PrintAndLogEx(SUCCESS, "Found keys have been dumped to " _YELLOW_("%s")" --> 0xffffffffffff has been inserted for unknown keys.", fptr); PrintAndLogEx(SUCCESS, "Found keys have been dumped to " _YELLOW_("%s")" --> 0xffffffffffff has been inserted for unknown keys.", fptr);
return 0; return PM3_SUCCESS;
} }
int loadFile(const char *preferredName, const char *suffix, void *data, size_t maxdatalen, size_t *datalen) { int loadFile(const char *preferredName, const char *suffix, void *data, size_t maxdatalen, size_t *datalen) {
if (data == NULL) return 1; if (data == NULL) return 1;
char *fileName = filenamemcopy(preferredName, suffix); char *fileName = filenamemcopy(preferredName, suffix);
if (fileName == NULL) return 1; if (fileName == NULL) return PM3_EINVARG;
int retval = PM3_SUCCESS; int retval = PM3_SUCCESS;
@ -363,23 +363,24 @@ int loadFile(const char *preferredName, const char *suffix, void *data, size_t m
if (fsize <= 0) { if (fsize <= 0) {
PrintAndLogEx(FAILED, "error, when getting filesize"); PrintAndLogEx(FAILED, "error, when getting filesize");
retval = 1; retval = PM3_EFILE;
goto out; goto out;
} }
uint8_t *dump = calloc(fsize, sizeof(uint8_t)); uint8_t *dump = calloc(fsize, sizeof(uint8_t));
if (!dump) { if (!dump) {
PrintAndLogEx(FAILED, "error, cannot allocate memory"); PrintAndLogEx(FAILED, "error, cannot allocate memory");
retval = 2; retval = PM3_EMALLOC;
goto out; goto out;
} }
size_t bytes_read = fread(dump, 1, fsize, f); size_t bytes_read = fread(dump, 1, fsize, f);
fclose(f);
if (bytes_read != fsize) { if (bytes_read != fsize) {
PrintAndLogEx(FAILED, "error, bytes read mismatch file size"); PrintAndLogEx(FAILED, "error, bytes read mismatch file size");
free(dump); free(dump);
retval = 3; retval = PM3_EFILE;
goto out; goto out;
} }
@ -396,9 +397,7 @@ int loadFile(const char *preferredName, const char *suffix, void *data, size_t m
*datalen = bytes_read; *datalen = bytes_read;
out: out:
fclose(f);
free(fileName); free(fileName);
return retval; return retval;
} }
@ -432,7 +431,7 @@ int loadFileEML(const char *preferredName, void *data, size_t *datalen) {
break; break;
fclose(f); fclose(f);
PrintAndLogEx(FAILED, "File reading error."); PrintAndLogEx(FAILED, "File reading error.");
retval = 2; retval = PM3_EFILE;
goto out; goto out;
} }
@ -471,13 +470,13 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_
root = json_load_file(fileName, 0, &error); root = json_load_file(fileName, 0, &error);
if (!root) { if (!root) {
PrintAndLogEx(ERR, "ERROR: json " _YELLOW_("%s") " error on line %d: %s", fileName, error.line, error.text); PrintAndLogEx(ERR, "ERROR: json " _YELLOW_("%s") " error on line %d: %s", fileName, error.line, error.text);
retval = 2; retval = PM3_ESOFT;
goto out; goto out;
} }
if (!json_is_object(root)) { if (!json_is_object(root)) {
PrintAndLogEx(ERR, "ERROR: Invalid json " _YELLOW_("%s") " format. root must be an object.", fileName); PrintAndLogEx(ERR, "ERROR: Invalid json " _YELLOW_("%s") " format. root must be an object.", fileName);
retval = 3; retval = PM3_ESOFT;
goto out; goto out;
} }
@ -493,7 +492,7 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_
size_t sptr = 0; size_t sptr = 0;
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
if (sptr + 16 > maxdatalen) { if (sptr + 16 > maxdatalen) {
retval = 5; retval = PM3_EMALLOC;
goto out; goto out;
} }
@ -515,7 +514,7 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_
size_t sptr = 0; size_t sptr = 0;
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
if (sptr + 4 > maxdatalen) { if (sptr + 4 > maxdatalen) {
retval = 5; retval = PM3_EMALLOC;
goto out; goto out;
} }
@ -537,7 +536,7 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_
size_t sptr = 0; size_t sptr = 0;
for (size_t i = 0; i < (maxdatalen / 4); i++) { for (size_t i = 0; i < (maxdatalen / 4); i++) {
if (sptr + 4 > maxdatalen) { if (sptr + 4 > maxdatalen) {
retval = 5; retval = PM3_EMALLOC;
goto out; goto out;
} }
@ -559,7 +558,7 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_
size_t sptr = 0; size_t sptr = 0;
for (size_t i = 0; i < (maxdatalen / 8); i++) { for (size_t i = 0; i < (maxdatalen / 8); i++) {
if (sptr + 8 > maxdatalen) { if (sptr + 8 > maxdatalen) {
retval = 5; retval = PM3_EMALLOC;
goto out; goto out;
} }
@ -651,10 +650,8 @@ out:
int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t keylen, uint16_t *keycnt) { int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t keylen, uint16_t *keycnt) {
int block_size = 512;
int allocation_size = block_size;
size_t counter = 0;
int retval = PM3_SUCCESS; int retval = PM3_SUCCESS;
char *path; char *path;
if (searchFile(&path, DICTIONARIES_SUBDIR, preferredName, ".dic") != PM3_SUCCESS) if (searchFile(&path, DICTIONARIES_SUBDIR, preferredName, ".dic") != PM3_SUCCESS)
return PM3_EFILE; return PM3_EFILE;
@ -667,14 +664,20 @@ int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t key
keylen = 6; keylen = 6;
} }
size_t mem_size;
size_t block_size = 10 * keylen;
// double up since its chars // double up since its chars
keylen <<= 1; keylen <<= 1;
char line[255]; char line[255];
// allocate some space for the dictionary // allocate some space for the dictionary
*pdata = calloc(keylen * allocation_size, sizeof(uint8_t)); *pdata = calloc( block_size , sizeof(uint8_t));
if (*pdata == NULL) return PM3_EFILE; if (*pdata == NULL)
return PM3_EFILE;
mem_size = block_size;
FILE *f = fopen(path, "r"); FILE *f = fopen(path, "r");
if (!f) { if (!f) {
@ -684,15 +687,17 @@ int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t key
// read file // read file
while (fgets(line, sizeof(line), f)) { while (fgets(line, sizeof(line), f)) {
// check if we have enough space (if not allocate more) // check if we have enough space (if not allocate more)
if ((*keycnt) >= allocation_size) { if ( (*keycnt * (keylen >> 1) ) >= mem_size ) {
allocation_size += block_size;
*pdata = realloc(*pdata, keylen * allocation_size * sizeof(uint8_t)); mem_size += block_size;
*pdata = realloc(*pdata, mem_size);
if (*pdata == NULL) { if (*pdata == NULL) {
return PM3_EFILE; return PM3_EFILE;
} else { } else {
// zero the new memory (safety first) memset(*pdata + (mem_size - block_size), 0, block_size);
memset(*pdata + allocation_size - block_size, 0, block_size);
} }
} }
@ -714,10 +719,11 @@ int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t key
uint64_t key = strtoull(line, NULL, 16); uint64_t key = strtoull(line, NULL, 16);
num_to_bytes(key, keylen >> 1, *pdata + counter); num_to_bytes(key, keylen >> 1, *pdata + (*keycnt * (keylen >> 1)) );
(*keycnt)++; (*keycnt)++;
memset(line, 0, sizeof(line)); memset(line, 0, sizeof(line));
counter += (keylen >> 1);
} }
fclose(f); fclose(f);
PrintAndLogEx(SUCCESS, "loaded " _GREEN_("%2d") "keys from dictionary file " _YELLOW_("%s"), *keycnt, path); PrintAndLogEx(SUCCESS, "loaded " _GREEN_("%2d") "keys from dictionary file " _YELLOW_("%s"), *keycnt, path);

View file

@ -62,6 +62,13 @@ typedef enum {
// jsfT55xx, // jsfT55xx,
} JSONFileType; } JSONFileType;
typedef enum {
BIN = 0,
EML,
JSON,
DICTIONARY,
} DumpFileType_t;
int fileExists(const char *filename); int fileExists(const char *filename);
/** /**