safeFile*: accept when suffix is already provided

This commit is contained in:
Philippe Teuwen 2019-04-28 20:42:57 +02:00
commit 1dbcb712c3
13 changed files with 77 additions and 74 deletions

View file

@ -351,8 +351,8 @@ static int CmdFlashMemSave(const char *Cmd) {
return 1; return 1;
} }
saveFile(filename, "bin", dump, len); saveFile(filename, ".bin", dump, len);
saveFileEML(filename, "eml", dump, len, 16); saveFileEML(filename, dump, len, 16);
free(dump); free(dump);
return 0; return 0;
} }

View file

@ -985,8 +985,8 @@ static int CmdHF14BDump(const char *Cmd) {
size_t datalen = (blocks + 1) * 4; size_t datalen = (blocks + 1) * 4;
saveFileEML(filename, "eml", data, datalen, 4); saveFileEML(filename, data, datalen, 4);
saveFile(filename, "bin", data, datalen); saveFile(filename, ".bin", data, datalen);
out: out:
return switch_off_field_14b(); return switch_off_field_14b();
} }

View file

@ -842,8 +842,8 @@ static int CmdHF15Dump(const char *Cmd) {
PrintAndLogEx(NORMAL, "\n"); PrintAndLogEx(NORMAL, "\n");
size_t datalen = blocknum * 4; size_t datalen = blocknum * 4;
saveFileEML(filename, "eml", data, datalen, 4); saveFileEML(filename, data, datalen, 4);
saveFile(filename, "bin", data, datalen); saveFile(filename, ".bin", data, datalen);
return 0; return 0;
} }

View file

@ -425,7 +425,7 @@ static int CmdHFiClassSim(const char *Cmd) {
memcpy(dump + i * 24 + 16, resp.d.asBytes + i * 16 + 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);
break; break;
} }
@ -479,7 +479,7 @@ static int CmdHFiClassSim(const char *Cmd) {
// copy NR_MAC (eight bytes from the response) ( 8b csn + 8b epurse == 16) // copy NR_MAC (eight bytes from the response) ( 8b csn + 8b epurse == 16)
memcpy(dump + i * MAC_ITEM_SIZE + 16, resp.d.asBytes + i * 16 + 8, 8); memcpy(dump + i * MAC_ITEM_SIZE + 16, resp.d.asBytes + i * 16 + 8, 8);
} }
saveFile("iclass_mac_attack_keyroll_A", "bin", dump, datalen); saveFile("iclass_mac_attack_keyroll_A", ".bin", dump, datalen);
//KEYROLL 2 //KEYROLL 2
memset(dump, 0, datalen); memset(dump, 0, datalen);
@ -494,7 +494,7 @@ static int CmdHFiClassSim(const char *Cmd) {
memcpy(dump + i * MAC_ITEM_SIZE + 16, resp.d.asBytes + resp_index + 8, 8); memcpy(dump + i * MAC_ITEM_SIZE + 16, resp.d.asBytes + resp_index + 8, 8);
resp_index++; resp_index++;
} }
saveFile("iclass_mac_attack_keyroll_B", "bin", dump, datalen); saveFile("iclass_mac_attack_keyroll_B", ".bin", dump, datalen);
free(dump); free(dump);
break; break;
} }
@ -720,7 +720,7 @@ static int CmdHFiClassDecrypt(const char *Cmd) {
} }
} }
saveFile(outfilename, "bin", decrypted, fsize); saveFile(outfilename, ".bin", decrypted, fsize);
printIclassDumpContents(decrypted, 1, (fsize / 8), fsize); printIclassDumpContents(decrypted, 1, (fsize / 8), fsize);
free(decrypted); free(decrypted);
return 0; return 0;
@ -1087,7 +1087,7 @@ static int CmdHFiClassReader_Dump(const char *Cmd) {
// save the dump to .bin file // save the dump to .bin file
PrintAndLogEx(SUCCESS, "saving dump file - %d blocks read", gotBytes / 8); PrintAndLogEx(SUCCESS, "saving dump file - %d blocks read", gotBytes / 8);
saveFile(filename, "bin", tag_data, gotBytes); saveFile(filename, ".bin", tag_data, gotBytes);
return 1; return 1;
} }

View file

@ -1212,8 +1212,8 @@ static int CmdLegicESave(const char *Cmd) {
else else
sprintf(fnameptr + fileNlen, ".bin"); sprintf(fnameptr + fileNlen, ".bin");
saveFileEML(filename, "eml", data, numofbytes, 8); saveFileEML(filename, data, numofbytes, 8);
saveFile(filename, "bin", data, numofbytes); saveFile(filename, ".bin", data, numofbytes);
return 0; return 0;
} }

View file

@ -917,9 +917,9 @@ static int CmdHF14AMfDump(const char *Cmd) {
uint16_t bytes = 16 * (FirstBlockOfSector(numSectors - 1) + NumBlocksPerSector(numSectors - 1)); uint16_t bytes = 16 * (FirstBlockOfSector(numSectors - 1) + NumBlocksPerSector(numSectors - 1));
saveFile(dataFilename, "bin", (uint8_t *)carddata, bytes); saveFile(dataFilename, ".bin", (uint8_t *)carddata, bytes);
saveFileEML(dataFilename, "eml", (uint8_t *)carddata, bytes, MFBLOCK_SIZE); saveFileEML(dataFilename, (uint8_t *)carddata, bytes, MFBLOCK_SIZE);
saveFileJSON(dataFilename, "json", jsfCardMemory, (uint8_t *)carddata, bytes); saveFileJSON(dataFilename, jsfCardMemory, (uint8_t *)carddata, bytes);
return 0; return 0;
} }
@ -2685,9 +2685,9 @@ static int CmdHF14AMfESave(const char *Cmd) {
FillFileNameByUID(fnameptr, dump, "-dump", 4); FillFileNameByUID(fnameptr, dump, "-dump", 4);
} }
saveFile(filename, "bin", dump, bytes); saveFile(filename, ".bin", dump, bytes);
saveFileEML(filename, "eml", dump, bytes, MFBLOCK_SIZE); saveFileEML(filename, dump, bytes, MFBLOCK_SIZE);
saveFileJSON(filename, "json", jsfCardMemory, dump, bytes); saveFileJSON(filename, jsfCardMemory, dump, bytes);
free(dump); free(dump);
return 0; return 0;
} }
@ -3119,9 +3119,9 @@ static int CmdHF14AMfCSave(const char *Cmd) {
PrintAndLogEx(SUCCESS, "uploaded %d bytes to emulator memory", bytes); PrintAndLogEx(SUCCESS, "uploaded %d bytes to emulator memory", bytes);
} }
saveFile(filename, "bin", dump, bytes); saveFile(filename, ".bin", dump, bytes);
saveFileEML(filename, "eml", dump, bytes, MFBLOCK_SIZE); saveFileEML(filename, dump, bytes, MFBLOCK_SIZE);
saveFileJSON(filename, "json", jsfCardMemory, dump, bytes); saveFileJSON(filename, jsfCardMemory, dump, bytes);
free(dump); free(dump);
return 0; return 0;
} }

View file

@ -1974,8 +1974,8 @@ static int CmdHF14AMfUDump(const char *Cmd) {
FillFileNameByUID(fptr, card.uid, "-dump", card.uidlen); FillFileNameByUID(fptr, card.uid, "-dump", card.uidlen);
} }
uint16_t datalen = pages * 4 + MFU_DUMP_PREFIX_LENGTH; uint16_t datalen = pages * 4 + MFU_DUMP_PREFIX_LENGTH;
saveFile(filename, "bin", (uint8_t *)&dump_file_data, datalen); saveFile(filename, ".bin", (uint8_t *)&dump_file_data, datalen);
saveFileJSON(filename, "json", jsfMfuMemory, (uint8_t *)&dump_file_data, datalen); saveFileJSON(filename, jsfMfuMemory, (uint8_t *)&dump_file_data, datalen);
if (is_partial) if (is_partial)
PrintAndLogEx(WARNING, "Partial dump created. (%d of %d blocks)", pages, card_mem_size); PrintAndLogEx(WARNING, "Partial dump created. (%d of %d blocks)", pages, card_mem_size);

View file

@ -583,9 +583,9 @@ static int CmdLFHitagReader(const char *Cmd) {
fnameptr += sprintf(fnameptr, "lf-hitag-"); fnameptr += sprintf(fnameptr, "lf-hitag-");
FillFileNameByUID(fnameptr, data, "-dump", 4); FillFileNameByUID(fnameptr, data, "-dump", 4);
saveFile(filename, "bin", data, 48); saveFile(filename, ".bin", data, 48);
saveFileEML(filename, "eml", data, 48, 4); saveFileEML(filename, data, 48, 4);
saveFileJSON(filename, "json", jsfHitag, data, 48); saveFileJSON(filename, jsfHitag, data, 48);
// block3, 1 byte // block3, 1 byte
printHitagConfiguration(data[4 * 3]); printHitagConfiguration(data[4 * 3]);

View file

@ -563,7 +563,7 @@ static int CmdTraceSave(const char *Cmd) {
if (strlen(Cmd) < 1 || cmdp == 'h') return usage_trace_save(); if (strlen(Cmd) < 1 || cmdp == 'h') return usage_trace_save();
param_getstr(Cmd, 0, filename, sizeof(filename)); param_getstr(Cmd, 0, filename, sizeof(filename));
saveFile(filename, "bin", trace, traceLen); saveFile(filename, ".bin", trace, traceLen);
return 0; return 0;
} }

View file

@ -57,15 +57,49 @@ int fileExists(const char *filename) {
return result == 0; return result == 0;
} }
int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen) { static char *filenamemcopy(const char *preferredName, const char *suffix) {
int size = sizeof(char) * (strlen(preferredName) + strlen(suffix) + 10); if (preferredName == NULL) return NULL;
char *fileName = calloc(size, sizeof(char)); if (suffix == NULL) return NULL;
char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t));
if (fileName == NULL)
return NULL;
strcpy(fileName, preferredName);
if (str_endswith(fileName, suffix))
return fileName;
strcat(fileName, suffix);
return fileName;
}
static char *newfilenamemcopy(const char *preferredName, const char *suffix) {
if (preferredName == NULL) return NULL;
if (suffix == NULL) return NULL;
char *preferredNameTmp = (char *) calloc(strlen(preferredName) + 1, sizeof(uint8_t));
if (preferredNameTmp == NULL)
return NULL;
strcpy(preferredNameTmp, preferredName);
if (str_endswith(preferredNameTmp, suffix))
preferredNameTmp[strlen(preferredNameTmp) - strlen(suffix)] = '\0';
char *fileName = (char *) calloc(strlen(preferredNameTmp) + strlen(suffix) + 1 + 10, sizeof(uint8_t)); // 10: room for filenum to ensure new filename
if (fileName == NULL) {
free(preferredNameTmp);
return NULL;
}
int num = 1; int num = 1;
sprintf(fileName, "%s.%s", preferredName, suffix); sprintf(fileName, "%s%s", preferredNameTmp, suffix);
while (fileExists(fileName)) { while (fileExists(fileName)) {
sprintf(fileName, "%s-%d.%s", preferredName, num, suffix); sprintf(fileName, "%s-%d%s", preferredNameTmp, num, suffix);
num++; num++;
} }
free(preferredNameTmp);
return fileName;
}
int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen) {
if (data == NULL) return 1;
char *fileName = newfilenamemcopy(preferredName, suffix);
if (fileName == NULL) return 1;
/* We should have a valid filename now, e.g. dumpdata-3.bin */ /* We should have a valid filename now, e.g. dumpdata-3.bin */
/*Opening file for writing in binary mode*/ /*Opening file for writing in binary mode*/
@ -83,23 +117,15 @@ int saveFile(const char *preferredName, const char *suffix, const void *data, si
return 0; return 0;
} }
int saveFileEML(const char *preferredName, const char *suffix, uint8_t *data, size_t datalen, size_t blocksize) { int saveFileEML(const char *preferredName, uint8_t *data, size_t datalen, size_t blocksize) {
if (preferredName == NULL) return 1;
if (suffix == NULL) return 1;
if (data == NULL) return 1; if (data == NULL) return 1;
char *fileName = newfilenamemcopy(preferredName, ".eml");
if (fileName == NULL) return 1;
int retval = 0; int retval = 0;
int blocks = datalen / blocksize; int blocks = datalen / blocksize;
uint16_t currblock = 1; uint16_t currblock = 1;
int size = sizeof(char) * (strlen(preferredName) + strlen(suffix) + 10);
char *fileName = calloc(size, sizeof(char));
int num = 1;
sprintf(fileName, "%s.%s", preferredName, suffix);
while (fileExists(fileName)) {
sprintf(fileName, "%s-%d.%s", preferredName, num, suffix);
num++;
}
/* We should have a valid filename now, e.g. dumpdata-3.bin */ /* We should have a valid filename now, e.g. dumpdata-3.bin */
@ -136,20 +162,13 @@ out:
return retval; return retval;
} }
int saveFileJSON(const char *preferredName, const char *suffix, JSONFileType ftype, uint8_t *data, size_t datalen) { int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen) {
if (preferredName == NULL) return 1;
if (suffix == NULL) return 1;
if (data == NULL) return 1; if (data == NULL) return 1;
char *fileName = newfilenamemcopy(preferredName, ".json");
if (fileName == NULL) return 1;
int retval = 0; int retval = 0;
int size = sizeof(char) * (strlen(preferredName) + strlen(suffix) + 10);
char *fileName = calloc(size, sizeof(char));
int num = 1;
sprintf(fileName, "%s.%s", preferredName, suffix);
while (fileExists(fileName)) {
sprintf(fileName, "%s-%d.%s", preferredName, num, suffix);
num++;
}
json_t *root = json_object(); json_t *root = json_object();
JsonSaveStr(root, "Created", "proxmark3"); JsonSaveStr(root, "Created", "proxmark3");

View file

@ -70,7 +70,7 @@ int fileExists(const char *filename);
* E.g. dumpdata-15.txt * E.g. dumpdata-15.txt
* *
* @param preferredName * @param preferredName
* @param suffix the file suffix. Leave out the ".". * @param suffix the file suffix. Including the ".".
* @param data The binary data to write to the file * @param data The binary data to write to the file
* @param datalen the length of the data * @param datalen the length of the data
* @return 0 for ok, 1 for failz * @return 0 for ok, 1 for failz
@ -83,13 +83,12 @@ int saveFile(const char *preferredName, const char *suffix, const void *data, si
* E.g. dumpdata-15.txt * E.g. dumpdata-15.txt
* *
* @param preferredName * @param preferredName
* @param suffix the file suffix. Leave out the ".".
* @param data The binary data to write to the file * @param data The binary data to write to the file
* @param datalen the length of the data * @param datalen the length of the data
* @param blocksize the length of one row * @param blocksize the length of one row
* @return 0 for ok, 1 for failz * @return 0 for ok, 1 for failz
*/ */
int saveFileEML(const char *preferredName, const char *suffix, uint8_t *data, size_t datalen, size_t blocksize); int saveFileEML(const char *preferredName, uint8_t *data, size_t datalen, size_t blocksize);
/** STUB /** STUB
* @brief Utility function to save JSON data to a file. This method takes a preferred name, but if that * @brief Utility function to save JSON data to a file. This method takes a preferred name, but if that
@ -97,13 +96,12 @@ int saveFileEML(const char *preferredName, const char *suffix, uint8_t *data, si
* E.g. dumpdata-15.json * E.g. dumpdata-15.json
* *
* @param preferredName * @param preferredName
* @param suffix the file suffix. Leave out the ".".
* @param ftype type of file. * @param ftype type of file.
* @param data The binary data to write to the file * @param data The binary data to write to the file
* @param datalen the length of the data * @param datalen the length of the data
* @return 0 for ok, 1 for failz * @return 0 for ok, 1 for failz
*/ */
int saveFileJSON(const char *preferredName, const char *suffix, JSONFileType ftype, uint8_t *data, size_t datalen); int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen);
/** STUB /** STUB
* @brief Utility function to load data from a binary file. This method takes a preferred name. * @brief Utility function to load data from a binary file. This method takes a preferred name.

View file

@ -938,16 +938,3 @@ char *strmcopy(const char *buf) {
} }
return str; return str;
} }
char *filenamemcopy(const char *preferredName, const char *suffix) {
if (preferredName == NULL) return NULL;
if (suffix == NULL) return NULL;
char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t));
if (fileName == NULL)
return NULL;
strcpy(fileName, preferredName);
if (str_endswith(fileName, suffix))
return fileName;
strcat(fileName, suffix);
return fileName;
}

View file

@ -266,5 +266,4 @@ void clean_ascii(unsigned char *buf, size_t len);
void strcleanrn(char *buf, size_t len); void strcleanrn(char *buf, size_t len);
void strcreplace(char *buf, size_t len, char from, char to); void strcreplace(char *buf, size_t len, char from, char to);
char *strmcopy(const char *buf); char *strmcopy(const char *buf);
char *filenamemcopy(const char *preferredName, const char *suffix);
#endif #endif