From 49c6806b1b35b7ffcc8d8a4e45ea2ce8a0a03e9f Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Fri, 17 Apr 2020 19:29:33 +1000 Subject: [PATCH 01/47] device debug and tweaks --- client/src/preferences.c | 383 ++++++++++++++++++++++++++++++++------- client/src/preferences.h | 1 + client/src/proxmark3.c | 51 +++++- client/src/ui.h | 5 +- 4 files changed, 368 insertions(+), 72 deletions(-) diff --git a/client/src/preferences.c b/client/src/preferences.c index a1de6e4e2..194399ec3 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -23,11 +23,9 @@ #include #include "cmdparser.h" #include -// #include +#include #include -//#include "proxgui.h" -//extern void SetWindowsPosition (void); static int CmdHelp(const char *Cmd); static int setCmdHelp(const char *Cmd); @@ -54,7 +52,8 @@ int preferences_load(void) { PrintAndLogEx(INFO, "Looking for preferences..."); // Set all defaults - session.client_debug_level = OFF; + session.client_debug_level = cdbOFF; + session.device_debug_level = ddbOFF; session.window_changed = false; session.window_plot_xpos = 10; session.window_plot_ypos = 30; @@ -69,6 +68,25 @@ int preferences_load(void) { session.supports_colors = false; + // default save path + if (get_my_user_directory() != NULL) { + if (session.default_savepath != NULL) + free(session.default_savepath); + session.default_savepath = (char *)calloc(strlen(get_my_user_directory()) + 1, sizeof(uint8_t)); + strcpy(session.default_savepath, get_my_user_directory()); + //if (session.file_savepath == NULL) { + + } else { + session.default_savepath = (char *)calloc(2, sizeof(uint8_t)); + strcpy(session.default_savepath, "."); + } +/* + if (get_my_user_directory ()!= NULL) { + strncpy (session.file_savepath,get_my_user_directory(),sizeof(session.file_savepath)-1); + } else { + strncpy (session.file_savepath,".",sizeof(session.file_savepath)-1); + } +*/ // loadFileJson wants these, so pass in place holder values, though not used // in settings load; uint8_t dummyData = 0x00; @@ -87,10 +105,15 @@ int preferences_load(void) { int preferences_save(void) { // Note sure if backup has value ? - char backupFilename[FILENAME_MAX + sizeof(preferencesFilename) + 10] = {0}; + char *backupFilename = NULL;// [FILENAME_MAX+sizeof(preferencesFilename)+10] = {0}; + int fnLen = 0; PrintAndLogEx(INFO, "Saving preferences ..."); - snprintf(backupFilename, sizeof(backupFilename) - 1, "%s.bak", prefGetFilename()); + + fnLen = strlen(prefGetFilename()) + 5; // .bak\0 + backupFilename = (char *)calloc(fnLen, sizeof(uint8_t)); +// snprintf (backupFilename,sizeof(backupFilename)-1,"%s.bak",prefGetFilename()); + snprintf(backupFilename, fnLen, "%s.bak", prefGetFilename()); if (fileExists(backupFilename)) { if (remove(backupFilename) != 0) { @@ -112,6 +135,9 @@ int preferences_save(void) { if (saveFileJSON(prefGetFilename(), jsfSettings, &dummyData, dummyDL) != PM3_SUCCESS) PrintAndLogEx(ERR, "Error saving preferences to \"%s\"", prefGetFilename()); + if (backupFilename != NULL) + free(backupFilename); + return PM3_SUCCESS; } @@ -119,40 +145,13 @@ void preferences_save_callback(json_t *root) { JsonSaveStr(root, "FileType", "settings"); - // Log level, convert to text - switch (session.client_debug_level) { - case OFF: - JsonSaveStr(root, "client.debug.level", "off"); - break; - case SIMPLE: - JsonSaveStr(root, "client.debug.level", "simple"); - break; - case FULL: - JsonSaveStr(root, "client.debug.level", "full"); - break; - default: - JsonSaveStr(root, "logging.level", "NORMAL"); - } - - // Plot window - JsonSaveInt(root, "window.plot.xpos", session.window_plot_xpos); - JsonSaveInt(root, "window.plot.ypos", session.window_plot_ypos); - JsonSaveInt(root, "window.plot.hsize", session.window_plot_hsize); - JsonSaveInt(root, "window.plot.wsize", session.window_plot_wsize); - - // Overlay/Slider window - JsonSaveInt(root, "window.overlay.xpos", session.window_overlay_xpos); - JsonSaveInt(root, "window.overlay.ypos", session.window_overlay_ypos); - JsonSaveInt(root, "window.overlay.hsize", session.window_overlay_hsize); - JsonSaveInt(root, "window.overlay.wsize", session.window_overlay_wsize); - // Emoji switch (session.emoji_mode) { case ALIAS: JsonSaveStr(root, "show.emoji", "alias"); break; case EMOJI: - JsonSaveStr(root, "show.emoji", "emoji"); + JsonSaveStr(root,"show.emoji","emoji"); break; case ALTTEXT: JsonSaveStr(root, "show.emoji", "alttext"); @@ -167,6 +166,56 @@ void preferences_save_callback(json_t *root) { JsonSaveBoolean(root, "show.hints", session.show_hints); JsonSaveBoolean(root, "os.supports.colors", session.supports_colors); + + JsonSaveStr(root, "file.default.savepath", session.default_savepath); + + // Plot window + JsonSaveInt(root, "window.plot.xpos", session.window_plot_xpos); + JsonSaveInt(root, "window.plot.ypos", session.window_plot_ypos); + JsonSaveInt(root, "window.plot.hsize", session.window_plot_hsize); + JsonSaveInt(root, "window.plot.wsize", session.window_plot_wsize); + + // Overlay/Slider window + JsonSaveInt(root, "window.overlay.xpos", session.window_overlay_xpos); + JsonSaveInt(root, "window.overlay.ypos", session.window_overlay_ypos); + JsonSaveInt(root, "window.overlay.hsize", session.window_overlay_hsize); + JsonSaveInt(root, "window.overlay.wsize", session.window_overlay_wsize); + + // Log level, convert to text + switch (session.client_debug_level) { + case cdbOFF: + JsonSaveStr(root, "client.debug.level", "off"); + break; + case cdbSIMPLE: + JsonSaveStr(root, "client.debug.level", "simple"); + break; + case cdbFULL: + JsonSaveStr(root, "client.debug.level", "full"); + break; + default: + JsonSaveStr(root, "logging.level", "NORMAL"); + } + + switch (session.device_debug_level) { + case ddbOFF: + JsonSaveStr(root, "device.debug.level", "off"); + break; + case ddbERROR: + JsonSaveStr(root, "device.debug.level", "error"); + break; + case ddbINFO: + JsonSaveStr(root, "device.debug.level", "info"); + break; + case ddbDEBUG: + JsonSaveStr(root, "device.debug.level", "debug"); + break; + case ddbEXTENDED: + JsonSaveStr(root, "device.debug.level", "extended"); + break; + default: + JsonSaveStr(root, "logging.level", "NORMAL"); + } + } void preferences_load_callback(json_t *root) { @@ -180,11 +229,19 @@ void preferences_load_callback(json_t *root) { if (json_unpack_ex(root, &up_error, 0, "{s:s}", "client.debug.level", &s1) == 0) { strncpy(tempStr, s1, sizeof(tempStr) - 1); str_lower(tempStr); - if (strncmp(tempStr, "off", 3) == 0) session.client_debug_level = OFF; - if (strncmp(tempStr, "simple", 6) == 0) session.client_debug_level = SIMPLE; - if (strncmp(tempStr, "full", 4) == 0) session.client_debug_level = FULL; + if (strncmp(tempStr, "off", 3) == 0) session.client_debug_level = cdbOFF; + if (strncmp(tempStr, "simple", 6) == 0) session.client_debug_level = cdbSIMPLE; + if (strncmp(tempStr, "full", 4) == 0) session.client_debug_level = cdbFULL; } + // default save path + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.savepath", &s1) == 0) { + if (session.default_savepath != NULL) + free(session.default_savepath); + session.default_savepath = (char *)calloc(strlen(s1) + 1, sizeof(uint8_t)); + strcpy(session.default_savepath,s1); + } + // window plot if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.xpos", &i1) == 0) session.window_plot_xpos = i1; @@ -221,6 +278,17 @@ void preferences_load_callback(json_t *root) { if (json_unpack_ex(root, &up_error, 0, "{s:b}", "os.supports.colors", &b1) == 0) session.supports_colors = b1; + // Logging Level + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "device.debug.level", &s1) == 0) { + strncpy(tempStr, s1, sizeof(tempStr) - 1); + str_lower(tempStr); + if (strncmp(tempStr, "off", 3) == 0) session.device_debug_level = ddbOFF; + if (strncmp(tempStr, "error", 5) == 0) session.device_debug_level = ddbERROR; + if (strncmp(tempStr, "info", 4) == 0) session.device_debug_level = ddbINFO; + if (strncmp(tempStr, "debug", 5) == 0) session.device_debug_level = ddbDEBUG; + if (strncmp(tempStr, "extended", 8) == 0) session.device_debug_level = ddbEXTENDED; + } + } // Help Functions @@ -248,7 +316,7 @@ static int usage_set_color() { } static int usage_set_debug() { - PrintAndLogEx(NORMAL, "Usage: pref set debug "); + PrintAndLogEx(NORMAL, "Usage: pref set clientdebug "); PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); PrintAndLogEx(NORMAL, " "_GREEN_("off")" - no debug messages"); @@ -257,6 +325,20 @@ static int usage_set_debug() { return PM3_SUCCESS; } + +static int usage_set_devicedebug() { + PrintAndLogEx(NORMAL, "Usage: pref set devicedebug "); + PrintAndLogEx(NORMAL, "Options:"); + PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); + PrintAndLogEx(NORMAL, " "_GREEN_("off")" - no debug messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("error")" - error messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("info")" - info messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("debug")" - debug messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("extended")" - extended debug messages"); + + return PM3_SUCCESS; +} + static int usage_set_hints() { PrintAndLogEx(NORMAL, "Usage: pref set hints "); PrintAndLogEx(NORMAL, "Options:"); @@ -267,16 +349,26 @@ static int usage_set_hints() { return PM3_SUCCESS; } -// Preference Processing Functions -typedef enum preferenceId {prefNONE, prefHELP, prefEMOJI, prefCOLOR, prefPLOT, prefOVERLAY, prefHINTS, prefCLIENTDEBUG} preferenceId_t; -typedef enum prefShowOpt {prefShowNone, prefShowOLD, prefShowNEW} prefShowOpt_t; +static int usage_set_savePath() { + PrintAndLogEx(NORMAL, "Usage: pref set defaultsavepath "); + PrintAndLogEx(NORMAL, "Options:"); + PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); + PrintAndLogEx(NORMAL, " "_GREEN_("")" - default save path"); -const char *prefShowMsg(prefShowOpt_t Opt) { + return PM3_SUCCESS; +} + +// Preference Processing Functions +// typedef enum preferenceId {prefNONE,prefHELP,prefEMOJI,prefCOLOR,prefPLOT,prefOVERLAY,prefHINTS,prefCLIENTDEBUG} preferenceId_t; +typedef enum prefShowOpt {prefShowNone, prefShowOLD, prefShowNEW} prefShowOpt_t; + +const char *prefShowMsg(prefShowOpt_t Opt) +{ switch (Opt) { case prefShowOLD: - return _YELLOW_("[old]"); //strncpy(Msg,"Before ",sizeof(Msg)-1); break; + return _YELLOW_("[old]"); case prefShowNEW: - return _GREEN_("[new]"); // strncpy(Msg,"After ",sizeof(Msg)-1); break; + return _GREEN_("[new]"); case prefShowNone: return ""; } @@ -309,19 +401,19 @@ void showColorState(prefShowOpt_t Opt) { if (session.supports_colors) PrintAndLogEx(NORMAL, " %s color.................. "_GREEN_("ansi"), prefShowMsg(Opt)); else - PrintAndLogEx(NORMAL, " %s color.................. "_GREEN_("off"), prefShowMsg(Opt)); + PrintAndLogEx(NORMAL, " %s color.................. "_WHITE_("off"), prefShowMsg(Opt)); } void showClientDebugState(prefShowOpt_t Opt) { switch (session.client_debug_level) { - case OFF: - PrintAndLogEx(NORMAL, " %s client debug........... "_GREEN_("off"), prefShowMsg(Opt)); + case cdbOFF: + PrintAndLogEx(NORMAL, " %s client debug........... "_WHITE_("off"), prefShowMsg(Opt)); break; - case SIMPLE: + case cdbSIMPLE: PrintAndLogEx(NORMAL, " %s client debug........... "_GREEN_("simple"), prefShowMsg(Opt)); break; - case FULL: + case cdbFULL: PrintAndLogEx(NORMAL, " %s client debug........... "_GREEN_("full"), prefShowMsg(Opt)); break; default: @@ -329,7 +421,33 @@ void showClientDebugState(prefShowOpt_t Opt) { } } -void showPlotPosState(void) { +void showDeviceDebugState(prefShowOpt_t Opt) { + switch (session.device_debug_level) { + case ddbOFF: + PrintAndLogEx(NORMAL, " %s device debug........... "_WHITE_("off"), prefShowMsg(Opt)); + break; + case ddbERROR: + PrintAndLogEx(NORMAL, " %s device debug........... "_GREEN_("error"), prefShowMsg(Opt)); + break; + case ddbINFO: + PrintAndLogEx(NORMAL, " %s device debug........... "_GREEN_("info"), prefShowMsg(Opt)); + break; + case ddbDEBUG: + PrintAndLogEx(NORMAL, " %s device debug........... "_GREEN_("debug"), prefShowMsg(Opt)); + break; + case ddbEXTENDED: + PrintAndLogEx(NORMAL, " %s device debug........... "_GREEN_("extended"), prefShowMsg(Opt)); + break; + default: + PrintAndLogEx(NORMAL, " %s device debug........... "_RED_("unknown"), prefShowMsg(Opt)); + } +} + +void showSavePathState(prefShowOpt_t Opt) { + PrintAndLogEx(NORMAL, " %s default save path...... "_GREEN_("%s"), prefShowMsg(Opt), session.default_savepath); +} + +void showPlotPosState(void){ PrintAndLogEx(NORMAL, " Plot window............ X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), session.window_plot_xpos, session.window_plot_ypos, session.window_plot_hsize, session.window_plot_wsize); } @@ -341,9 +459,9 @@ void showOverlayPosState(void) { void showHintsState(prefShowOpt_t Opt) { if (session.show_hints) - PrintAndLogEx(NORMAL, " %s Hints.................. "_GREEN_("on"), prefShowMsg(Opt)); + PrintAndLogEx(NORMAL, " %s hints.................. "_GREEN_("on"), prefShowMsg(Opt)); else - PrintAndLogEx(NORMAL, " %s Hints.................. "_GREEN_("off"), prefShowMsg(Opt)); + PrintAndLogEx(NORMAL, " %s hints.................. "_WHITE_("off"), prefShowMsg(Opt)); } static int setCmdEmoji(const char *Cmd) { @@ -465,15 +583,15 @@ static int setCmdDebug(const char *Cmd) { return usage_set_debug(); if (strncmp(strOpt, "off", 3) == 0) { validValue = true; - newValue = OFF; + newValue = cdbOFF; } if (strncmp(strOpt, "simple", 6) == 0) { validValue = true; - newValue = SIMPLE; + newValue = cdbSIMPLE; } if (strncmp(strOpt, "full", 4) == 0) { validValue = true; - newValue = FULL; + newValue = cdbFULL; } if (validValue) { @@ -497,7 +615,74 @@ static int setCmdDebug(const char *Cmd) { return PM3_SUCCESS; } -static int setCmdHint(const char *Cmd) { +static int setCmdDeviceDebug (const char *Cmd) +{ + uint8_t cmdp = 0; + bool errors = false; + bool validValue = false; + char strOpt[50]; + devicedebugLevel_t newValue = session.device_debug_level; + + if (param_getchar(Cmd, cmdp) == 0x00) + return usage_set_devicedebug (); + + while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { + + if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { + str_lower(strOpt); // convert to lowercase + + if (strncmp (strOpt,"help",4) == 0) + return usage_set_devicedebug(); + if (strncmp (strOpt,"off",3) == 0) { + validValue = true; + newValue = ddbOFF; + } + if (strncmp (strOpt,"error",5) == 0) { + validValue = true; + newValue = ddbERROR; + } + if (strncmp (strOpt,"info",4) == 0) { + validValue = true; + newValue = ddbINFO; + } + if (strncmp (strOpt,"debug",5) == 0) { + validValue = true; + newValue = ddbDEBUG; + } + if (strncmp (strOpt,"extended",8) == 0) { + validValue = true; + newValue = ddbEXTENDED; + } + + if (validValue) { + if (session.device_debug_level != newValue) {// changed + showDeviceDebugState (prefShowOLD); + session.device_debug_level = newValue; + showDeviceDebugState (prefShowNEW); + preferences_save (); + } else { + PrintAndLogEx(INFO,"nothing changed"); + showDeviceDebugState (prefShowNone); + } + if (session.pm3_present) { + PrintAndLogEx (INFO,"setting device debug loglevel"); + SendCommandNG(CMD_SET_DBGMODE, &session.device_debug_level, 1); + PacketResponseNG resp; + if (WaitForResponseTimeout(CMD_SET_DBGMODE, &resp, 2000) == false) + PrintAndLogEx (INFO,"failed to set device debug loglevel"); + } + } else { + PrintAndLogEx(ERR,"invalid option"); + return usage_set_devicedebug(); + } + } + } + + return PM3_SUCCESS; +} + +static int setCmdHint (const char *Cmd) +{ uint8_t cmdp = 0; bool errors = false; bool validValue = false; @@ -543,12 +728,74 @@ static int setCmdHint(const char *Cmd) { return PM3_SUCCESS; } +static int setCmdSavePath (const char *Cmd) { + uint8_t cmdp = 0; + bool errors = false; + // bool validValue = false; + char *strOpt = NULL; + int optLen = 0; + char *newValue = NULL; + + if (param_getchar(Cmd, cmdp) == 0x00) + return usage_set_savePath(); + + while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { + if (strOpt != NULL) + free (strOpt); + + optLen = param_getlength(Cmd, cmdp)+1; + strOpt = (char *)calloc(optLen+1, sizeof(uint8_t)); + if (param_getstr(Cmd, cmdp++, strOpt, optLen) != 0) { + str_lower(strOpt); // convert to lowercase + + if (strncmp (strOpt,"help",4) == 0) + return usage_set_savePath(); + else { + // not help, must be the path.... + + // param_getstr(Cmd, cmdp++, newValue, sizeof(newValue)); + if (newValue == NULL) + free(newValue); + newValue = (char *)calloc(strlen(strOpt)+1, sizeof(uint8_t)); + strcpy (newValue,strOpt); + + if (strcmp (newValue,session.default_savepath) != 0) { + + // check if new path exists + if (fileExists (newValue)) { + showSavePathState (prefShowOLD); + if (session.default_savepath != NULL) + free (session.default_savepath); + session.default_savepath = (char *)calloc(strlen(newValue)+1, sizeof(uint8_t)); + strcpy (session.default_savepath,newValue); + showSavePathState (prefShowNEW); + preferences_save (); + } else { + PrintAndLogEx (ERR,"path does not exist"); + } + } else { + PrintAndLogEx(INFO,"nothing changed"); + showSavePathState (prefShowNone); + } + } + } + } + + // clean up + if (strOpt != NULL) free (strOpt); + if (newValue != NULL) free (newValue); + + return PM3_SUCCESS; +} + static command_t setCommandTable[] = { {"help", setCmdHelp, AlwaysAvailable, "This help"}, {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, - {"color", setCmdColor, AlwaysAvailable, "Set color support"}, - {"debug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, + {"color", setCmdColor, AlwaysAvailable, "Set color support"}, + {"defaultsavepath", setCmdSavePath, AlwaysAvailable, "Set default save path"}, + {"clientdebug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, + {"devicedebug", setCmdDeviceDebug, AlwaysAvailable, "Set device debug level"}, {NULL, NULL, NULL, NULL} }; @@ -569,21 +816,22 @@ int CmdPrefSet(const char *Cmd) { static int CmdPrefShow(const char *Cmd) { PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, _BLUE_("Preferences")); - - if (!session. preferences_loaded) { - PrintAndLogEx(ERR, "Preferneces not loaded"); + PrintAndLogEx(NORMAL, _CYAN_("Preferences")); + + if (!session.preferences_loaded) { + PrintAndLogEx (ERR, "Preferneces not loaded"); return PM3_ESOFT; } PrintAndLogEx(NORMAL, " preference file........ "_GREEN_("%s"), prefGetFilename()); showEmojiState(prefShowNone); - showColorState(prefShowNone); - // showPlotPosState (); - // showOverlayPosState (); - showClientDebugState(prefShowNone); showHintsState(prefShowNone); - + showColorState(prefShowNone); + // showPlotPosState (); + // showOverlayPosState (); + showSavePathState(prefShowNone); + showClientDebugState(prefShowNone); + showDeviceDebugState(prefShowNone); PrintAndLogEx(NORMAL, ""); return PM3_SUCCESS; @@ -599,7 +847,6 @@ static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, {"set", CmdPrefSet, AlwaysAvailable, "Set a preference"}, {"show", CmdPrefShow, AlwaysAvailable, "Show preferences"}, -// {"save", CmdPrefSave, AlwaysAvailable, "Save preferences now"}, {NULL, NULL, NULL, NULL} }; diff --git a/client/src/preferences.h b/client/src/preferences.h index 38e2272cb..0d7705027 100644 --- a/client/src/preferences.h +++ b/client/src/preferences.h @@ -9,6 +9,7 @@ #define PREFERENCES_H_ #include "fileutils.h" +#include // Current working directory will be prepended. #define preferencesFilename "preferences.json" diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 6bc88435a..84768f613 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -38,7 +38,7 @@ #endif // Used to enable/disable use of preferences json file -// #define USE_PREFERENCE_FILE +#define USE_PREFERENCE_FILE #ifdef _WIN32 @@ -414,13 +414,14 @@ static void set_my_executable_path(void) { } static const char *my_user_directory = NULL; -static char _cwd_Buffer [FILENAME_MAX] = {0}; +// static char _cwd_Buffer [FILENAME_MAX] = {0}; const char *get_my_user_directory(void) { return my_user_directory; } + static void set_my_user_directory(void) { - my_user_directory = getenv("HOME"); +/* my_user_directory = getenv("HOME"); // if not found, default to current directory if (my_user_directory == NULL) { @@ -430,6 +431,38 @@ static void set_my_user_directory(void) { if (_cwd_Buffer[i] == '\\') _cwd_Buffer[i] = '/'; // my_user_directory = "."; } +*/ + my_user_directory = getenv("HOME"); + + // if not found, default to current directory + if (my_user_directory == NULL) { + + char *cwd_Buffer = NULL; + uint16_t pathLen = FILENAME_MAX; // should be a good starting point + bool error = false; + + cwd_Buffer = (char *)calloc(pathLen, sizeof(uint8_t)); + + while (!error && (GetCurrentDir (cwd_Buffer,pathLen) == NULL)) { + if (errno == ERANGE) { // Need bigger buffer + pathLen += 10; // if buffer was too small add 10 characters and try again + cwd_Buffer = realloc(cwd_Buffer, pathLen); + } else { + error = true; + free (cwd_Buffer); + cwd_Buffer = NULL; + } + printf ("Len... %d\n",pathLen); + } + + if (!error) { + + for (int i = 0; i < strlen(cwd_Buffer); i++) + if (cwd_Buffer[i] == '\\') cwd_Buffer[i] = '/'; + + my_user_directory = cwd_Buffer; + } + } } static void show_help(bool showFullHelp, char *exec_name) { @@ -949,7 +982,19 @@ int main(int argc, char *argv[]) { if (!session.preferences_loaded) { preferences_save(); // Save defaults session.preferences_loaded = true; + } else { + // Set device debug level + PrintAndLogEx(INFO,"setting device debug loglevel"); + if (session.pm3_present) { + SendCommandNG(CMD_SET_DBGMODE, &session.device_debug_level, 1); + PacketResponseNG resp; + if (WaitForResponseTimeout(CMD_SET_DBGMODE, &resp, 2000) == false) + PrintAndLogEx (INFO,"failed to set device debug loglevel"); + } + else + PrintAndLogEx(WARNING,"Proxmark3 not ready to set debug level"); } + #endif #ifdef HAVE_GUI diff --git a/client/src/ui.h b/client/src/ui.h index 24c4b7201..620769faa 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -19,7 +19,8 @@ typedef enum logLevel {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG, INPLACE, HINT} logLevel_t; typedef enum emojiMode {ALIAS, EMOJI, ALTTEXT, ERASE} emojiMode_t; -typedef enum clientdebugLevel {OFF, SIMPLE, FULL} clientdebugLevel_t; +typedef enum clientdebugLevel {cdbOFF,cdbSIMPLE,cdbFULL} clientdebugLevel_t; +typedef enum devicedebugLevel {ddbOFF,ddbERROR,ddbINFO,ddbDEBUG,ddbEXTENDED} devicedebugLevel_t; typedef struct { bool preferences_loaded; @@ -31,6 +32,7 @@ typedef struct { bool help_dump_mode; bool show_hints; bool window_changed; // track if plot/overlay pos/size changed to save on exit + char *default_savepath;// [FILENAME_MAX]; int window_plot_xpos; int window_plot_ypos; int window_plot_hsize; @@ -40,6 +42,7 @@ typedef struct { int window_overlay_hsize; int window_overlay_wsize; clientdebugLevel_t client_debug_level; + uint8_t device_debug_level; } session_arg_t; extern session_arg_t session; From b593d2c319ec833d9bd4655fbc8c899a3ae44f2e Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Fri, 17 Apr 2020 19:50:12 +1000 Subject: [PATCH 02/47] clean up --- client/src/preferences.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/client/src/preferences.c b/client/src/preferences.c index 194399ec3..c53615dca 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -69,24 +69,14 @@ int preferences_load(void) { // default save path - if (get_my_user_directory() != NULL) { - if (session.default_savepath != NULL) - free(session.default_savepath); - session.default_savepath = (char *)calloc(strlen(get_my_user_directory()) + 1, sizeof(uint8_t)); + if (get_my_user_directory() != NULL) { // should return path to .proxmark3 folder + session.default_savepath = (char *)realloc(session.default_savepath, strlen(get_my_user_directory()) + 1); //, sizeof(uint8_t)); strcpy(session.default_savepath, get_my_user_directory()); - //if (session.file_savepath == NULL) { - } else { - session.default_savepath = (char *)calloc(2, sizeof(uint8_t)); + session.default_savepath = (char *)realloc(session.default_savepath,2); // 2, sizeof(uint8_t)); strcpy(session.default_savepath, "."); } -/* - if (get_my_user_directory ()!= NULL) { - strncpy (session.file_savepath,get_my_user_directory(),sizeof(session.file_savepath)-1); - } else { - strncpy (session.file_savepath,".",sizeof(session.file_savepath)-1); - } -*/ + // loadFileJson wants these, so pass in place holder values, though not used // in settings load; uint8_t dummyData = 0x00; @@ -112,7 +102,6 @@ int preferences_save(void) { fnLen = strlen(prefGetFilename()) + 5; // .bak\0 backupFilename = (char *)calloc(fnLen, sizeof(uint8_t)); -// snprintf (backupFilename,sizeof(backupFilename)-1,"%s.bak",prefGetFilename()); snprintf(backupFilename, fnLen, "%s.bak", prefGetFilename()); if (fileExists(backupFilename)) { From e2c56fe26cb4b8adb05a65f1cd6d02a7f3d23d4c Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sat, 18 Apr 2020 18:04:08 +1000 Subject: [PATCH 03/47] updates Added dump and trace path and clean a little for cleaner use and expansion. --- client/src/fileutils.c | 60 ++++++++++ client/src/fileutils.h | 1 + client/src/preferences.c | 235 ++++++++++++++++++++++++++------------- client/src/proxguiqt.cpp | 22 ++-- client/src/ui.h | 15 +-- 5 files changed, 237 insertions(+), 96 deletions(-) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index b32d8ec88..d98b3d7ff 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -49,6 +49,7 @@ #include "util.h" #ifdef _WIN32 #include "scandir.h" +#include #endif #define PATH_MAX_LENGTH 200 @@ -130,6 +131,65 @@ static bool is_directory(const char *filename) { return S_ISDIR(st.st_mode) != 0; } +/** + * @brief create a new directory. + * @param dirname + * @return + */ +#ifdef _WIN32 +#define make_dir(a) _mkdir(a) +#else +#define make_dir(a) mkdir(a,0x755) +#endif +bool create_path(const char *dirname) { + + if (dirname == NULL) // nothing to do + return false; + + if ((strlen(dirname) == 1) && (dirname[0] == '/')) + return true; + + if ((strlen(dirname) == 2) && (dirname[1] == ':')) + return true; + + if (fileExists(dirname) == 0) { + + char *bs = strrchr(dirname, '\\'); + char *fs = strrchr(dirname, '/'); + + if ((bs == NULL) && (fs != NULL)) { + *fs = 0x00; + create_path (dirname); + *fs = '/'; + } + + if ((bs != NULL) && (fs == NULL)) { + *bs = 0x00; + create_path (dirname); + *bs = '\\'; + } + + if ((bs != NULL) && (fs != NULL)) { + if (strlen (bs) > strlen (fs)) { + *fs = 0x00; // No slash + create_path (dirname); + *fs = '/'; + } else { + *bs = 0x00; + create_path (dirname); + *bs = '\\'; + } + + } + + if (make_dir(dirname) != 0) { + PrintAndLogEx(ERR, "could not create directory.... "_RED_("%s"),dirname); + return false; + } + } + return true; +} + static char *filenamemcopy(const char *preferredName, const char *suffix) { if (preferredName == NULL) return NULL; diff --git a/client/src/fileutils.h b/client/src/fileutils.h index ff43b3f7e..bd7886aa3 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -74,6 +74,7 @@ typedef enum { } DumpFileType_t; int fileExists(const char *filename); +bool create_path(const char *dirname); /** * @brief Utility function to save data to a binary file. This method takes a preferred name, but if that diff --git a/client/src/preferences.c b/client/src/preferences.c index c53615dca..eb58389d2 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -55,14 +55,14 @@ int preferences_load(void) { session.client_debug_level = cdbOFF; session.device_debug_level = ddbOFF; session.window_changed = false; - session.window_plot_xpos = 10; - session.window_plot_ypos = 30; - session.window_plot_hsize = 400; - session.window_plot_wsize = 800; - session.window_overlay_xpos = session.window_plot_xpos; - session.window_overlay_ypos = 60 + session.window_plot_ypos + session.window_plot_hsize; - session.window_overlay_hsize = 200; - session.window_overlay_wsize = session.window_plot_wsize; + session.plot.x = 10; + session.plot.y = 30; + session.plot.h = 400; + session.plot.w = 800; + session.overlay.x = session.plot.x; + session.overlay.y = 60 + session.plot.y + session.plot.h; + session.overlay.h = 200; + session.overlay.w = session.plot.w; session.emoji_mode = ALIAS; session.show_hints = false; session.supports_colors = false; @@ -70,11 +70,27 @@ int preferences_load(void) { // default save path if (get_my_user_directory() != NULL) { // should return path to .proxmark3 folder - session.default_savepath = (char *)realloc(session.default_savepath, strlen(get_my_user_directory()) + 1); //, sizeof(uint8_t)); - strcpy(session.default_savepath, get_my_user_directory()); + session.defaultPaths[spDefault] = (char *)realloc(session.defaultPaths[spDefault], strlen(get_my_user_directory()) + 1); //, sizeof(uint8_t)); + strcpy(session.defaultPaths[spDefault], get_my_user_directory()); } else { - session.default_savepath = (char *)realloc(session.default_savepath,2); // 2, sizeof(uint8_t)); - strcpy(session.default_savepath, "."); + session.defaultPaths[spDefault] = (char *)realloc(session.defaultPaths[spDefault], 2); // 2, sizeof(uint8_t)); + strcpy(session.defaultPaths[spDefault], "."); + } + // default dump path + if (get_my_user_directory() != NULL) { // should return path to .proxmark3 folder + session.defaultPaths[spDump] = (char *)realloc(session.defaultPaths[spDump], strlen(get_my_user_directory()) + 1); //, sizeof(uint8_t)); + strcpy(session.defaultPaths[spDump], get_my_user_directory()); + } else { + session.defaultPaths[spDump] = (char *)realloc(session.defaultPaths[spDump], 2); // 2, sizeof(uint8_t)); + strcpy(session.defaultPaths[spDump], "."); + } + // default save path + if (get_my_user_directory() != NULL) { // should return path to .proxmark3 folder + session.defaultPaths[spTrace] = (char *)realloc(session.defaultPaths[spTrace], strlen(get_my_user_directory()) + 1); //, sizeof(uint8_t)); + strcpy(session.defaultPaths[spTrace], get_my_user_directory()); + } else { + session.defaultPaths[spTrace] = (char *)realloc(session.defaultPaths[spTrace], 2); // 2, sizeof(uint8_t)); + strcpy(session.defaultPaths[spTrace], "."); } // loadFileJson wants these, so pass in place holder values, though not used @@ -156,19 +172,21 @@ void preferences_save_callback(json_t *root) { JsonSaveBoolean(root, "os.supports.colors", session.supports_colors); - JsonSaveStr(root, "file.default.savepath", session.default_savepath); + JsonSaveStr(root, "file.default.savepath", session.defaultPaths[spDefault]); + JsonSaveStr(root, "file.default.dumppath", session.defaultPaths[spDump]); + JsonSaveStr(root, "file.default.tracepath", session.defaultPaths[spTrace]); // Plot window - JsonSaveInt(root, "window.plot.xpos", session.window_plot_xpos); - JsonSaveInt(root, "window.plot.ypos", session.window_plot_ypos); - JsonSaveInt(root, "window.plot.hsize", session.window_plot_hsize); - JsonSaveInt(root, "window.plot.wsize", session.window_plot_wsize); + JsonSaveInt(root, "window.plot.xpos", session.plot.x); + JsonSaveInt(root, "window.plot.ypos", session.plot.y); + JsonSaveInt(root, "window.plot.hsize", session.plot.h); + JsonSaveInt(root, "window.plot.wsize", session.plot.w); // Overlay/Slider window - JsonSaveInt(root, "window.overlay.xpos", session.window_overlay_xpos); - JsonSaveInt(root, "window.overlay.ypos", session.window_overlay_ypos); - JsonSaveInt(root, "window.overlay.hsize", session.window_overlay_hsize); - JsonSaveInt(root, "window.overlay.wsize", session.window_overlay_wsize); + JsonSaveInt(root, "window.overlay.xpos", session.overlay.x); + JsonSaveInt(root, "window.overlay.ypos", session.overlay.y); + JsonSaveInt(root, "window.overlay.hsize", session.overlay.h); + JsonSaveInt(root, "window.overlay.wsize", session.overlay.w); // Log level, convert to text switch (session.client_debug_level) { @@ -225,31 +243,39 @@ void preferences_load_callback(json_t *root) { // default save path if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.savepath", &s1) == 0) { - if (session.default_savepath != NULL) - free(session.default_savepath); - session.default_savepath = (char *)calloc(strlen(s1) + 1, sizeof(uint8_t)); - strcpy(session.default_savepath,s1); + session.defaultPaths[spDefault] = realloc(session.defaultPaths[spDefault], strlen(s1) + 1); + strcpy(session.defaultPaths[spDefault], s1); + } + // default dump path + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.dumppath", &s1) == 0) { + session.defaultPaths[spDump] = realloc(session.defaultPaths[spDump],strlen(s1) + 1); + strcpy(session.defaultPaths[spDump],s1); + } + // default trace path + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.tracepath", &s1) == 0) { + session.defaultPaths[spTrace] = realloc(session.defaultPaths[spTrace],strlen(s1) + 1); + strcpy(session.defaultPaths[spTrace],s1); } // window plot if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.xpos", &i1) == 0) - session.window_plot_xpos = i1; + session.plot.x = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.ypos", &i1) == 0) - session.window_plot_ypos = i1; + session.plot.y = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.hsize", &i1) == 0) - session.window_plot_hsize = i1; + session.plot.h = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.wsize", &i1) == 0) - session.window_plot_wsize = i1; + session.plot.w = i1; // overlay/slider plot if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.overlay.xpos", &i1) == 0) - session.window_overlay_xpos = i1; + session.overlay.x = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.overlay.ypos", &i1) == 0) - session.window_overlay_ypos = i1; + session.overlay.y = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.overlay.hsize", &i1) == 0) - session.window_overlay_hsize = i1; + session.overlay.h = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.overlay.wsize", &i1) == 0) - session.window_overlay_wsize = i1; + session.overlay.w = i1; // show options if (json_unpack_ex(root, &up_error, 0, "{s:s}", "show.emoji", &s1) == 0) { @@ -338,11 +364,14 @@ static int usage_set_hints() { return PM3_SUCCESS; } -static int usage_set_savePath() { - PrintAndLogEx(NORMAL, "Usage: pref set defaultsavepath "); +static int usage_set_savePaths() { + PrintAndLogEx(NORMAL, "Usage: pref set savepaths [help] [create] [default ] [dump ] [trace ]"); PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); - PrintAndLogEx(NORMAL, " "_GREEN_("")" - default save path"); + PrintAndLogEx(NORMAL, " "_GREEN_("create")" - Create directory if it does not exist"); + PrintAndLogEx(NORMAL, " "_GREEN_("default")" - Deafult path"); + PrintAndLogEx(NORMAL, " "_GREEN_("dump")" - Dump path"); + PrintAndLogEx(NORMAL, " "_GREEN_("trace")" - Trace help"); return PM3_SUCCESS; } @@ -433,17 +462,23 @@ void showDeviceDebugState(prefShowOpt_t Opt) { } void showSavePathState(prefShowOpt_t Opt) { - PrintAndLogEx(NORMAL, " %s default save path...... "_GREEN_("%s"), prefShowMsg(Opt), session.default_savepath); + PrintAndLogEx(NORMAL, " %s default save path...... "_GREEN_("%s"), prefShowMsg(Opt), session.defaultPaths[spDefault]); +} +void showDumpPathState(prefShowOpt_t Opt) { + PrintAndLogEx(NORMAL, " %s dump save path......... "_GREEN_("%s"), prefShowMsg(Opt), session.defaultPaths[spDump]); +} +void showTracePathState(prefShowOpt_t Opt) { + PrintAndLogEx(NORMAL, " %s trace save path........ "_GREEN_("%s"), prefShowMsg(Opt), session.defaultPaths[spTrace]); } void showPlotPosState(void){ PrintAndLogEx(NORMAL, " Plot window............ X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), - session.window_plot_xpos, session.window_plot_ypos, session.window_plot_hsize, session.window_plot_wsize); + session.plot.x, session.plot.y, session.plot.h, session.plot.w); } void showOverlayPosState(void) { PrintAndLogEx(NORMAL, " Slider/Overlay window.. X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), - session.window_overlay_xpos, session.window_overlay_ypos, session.window_overlay_hsize, session.window_overlay_wsize); + session.overlay.x, session.overlay.y, session.overlay.h, session.overlay.w); } void showHintsState(prefShowOpt_t Opt) { @@ -717,54 +752,100 @@ static int setCmdHint (const char *Cmd) return PM3_SUCCESS; } -static int setCmdSavePath (const char *Cmd) { +static int setCmdSavePaths (const char *Cmd) { uint8_t cmdp = 0; bool errors = false; // bool validValue = false; char *strOpt = NULL; int optLen = 0; char *newValue = NULL; + bool createDir = false; if (param_getchar(Cmd, cmdp) == 0x00) - return usage_set_savePath(); + return usage_set_savePaths(); while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { - if (strOpt != NULL) - free (strOpt); - + optLen = param_getlength(Cmd, cmdp)+1; - strOpt = (char *)calloc(optLen+1, sizeof(uint8_t)); + strOpt = (char *)realloc(strOpt,optLen+1);//, sizeof(uint8_t)); + if (param_getstr(Cmd, cmdp++, strOpt, optLen) != 0) { str_lower(strOpt); // convert to lowercase - if (strncmp (strOpt,"help",4) == 0) - return usage_set_savePath(); - else { - // not help, must be the path.... - - // param_getstr(Cmd, cmdp++, newValue, sizeof(newValue)); - if (newValue == NULL) - free(newValue); - newValue = (char *)calloc(strlen(strOpt)+1, sizeof(uint8_t)); - strcpy (newValue,strOpt); - - if (strcmp (newValue,session.default_savepath) != 0) { + if (strncmp(strOpt, "help", 4) == 0) + return usage_set_savePaths(); - // check if new path exists - if (fileExists (newValue)) { - showSavePathState (prefShowOLD); - if (session.default_savepath != NULL) - free (session.default_savepath); - session.default_savepath = (char *)calloc(strlen(newValue)+1, sizeof(uint8_t)); - strcpy (session.default_savepath,newValue); - showSavePathState (prefShowNEW); - preferences_save (); + if (strncmp(strOpt, "create", 6) == 0) { + // check if 2 more options. + if (param_getlength(Cmd, cmdp+1) == 0) // should have min 2 more options + return usage_set_savePaths(); + createDir = true; + } else { + if ((strncmp(strOpt, "default", 7) == 0) || + (strncmp(strOpt, "dump", 4) == 0) || + (strncmp(strOpt, "trace", 7) == 0)) { + + // Get Path + optLen = param_getlength(Cmd, cmdp) + 1; + newValue = (char *)realloc(newValue, optLen+1); + if (param_getstr(Cmd, cmdp++, newValue, optLen) == 0) { + PrintAndLogEx(INFO, "missing %s path",strOpt); + return usage_set_savePaths(); + } + // remove trailing slash. + if ((newValue[strlen(newValue)-1] == '/') || (newValue[strlen(newValue)-1] == '\\')) + newValue[strlen(newValue)-1] = 0x00; + + // Check path + if (!fileExists(newValue) && !createDir) { + PrintAndLogEx(ERR,"path does not exist... "_RED_("%s"),newValue); } else { - PrintAndLogEx (ERR,"path does not exist"); + // do we need to create it + if (!fileExists(newValue)) + create_path (newValue); //mkdir (newValue,0x777); + + // Default + if (strncmp(strOpt, "default", 7) == 0) { + if (strcmp(newValue, session.defaultPaths[spDefault]) != 0) { + showSavePathState(prefShowOLD); + session.defaultPaths[spDefault] = (char *)realloc(session.defaultPaths[spDefault], strlen(newValue) + 1); + strcpy (session.defaultPaths[spDefault],newValue); + showSavePathState(prefShowNEW); + preferences_save(); + } else { + PrintAndLogEx(INFO, "nothing changed"); + showSavePathState(prefShowNone); + } + } + // Dump + if (strncmp(strOpt, "dump", 4) == 0) { + if (strcmp(newValue, session.defaultPaths[spDump]) != 0) { + showDumpPathState(prefShowOLD); + session.defaultPaths[spDump] = (char *)realloc(session.defaultPaths[spDump], strlen(newValue) + 1); + strcpy (session.defaultPaths[spDump],newValue); + showDumpPathState(prefShowNEW); + preferences_save(); + } else { + PrintAndLogEx(INFO, "nothing changed"); + showDumpPathState(prefShowNone); + } + } + // Trace + if (strncmp(strOpt, "trace", 7) == 0) { + if (strcmp(newValue, session.defaultPaths[spTrace]) != 0) { + showTracePathState(prefShowOLD); + session.defaultPaths[spTrace] = (char *)realloc(session.defaultPaths[spTrace], strlen(newValue) + 1); + strcpy (session.defaultPaths[spTrace],newValue); + showTracePathState(prefShowNEW); + preferences_save(); + } else { + PrintAndLogEx(INFO, "nothing changed"); + showTracePathState(prefShowNone); + } + } } } else { - PrintAndLogEx(INFO,"nothing changed"); - showSavePathState (prefShowNone); + return usage_set_savePaths(); } } } @@ -778,13 +859,13 @@ static int setCmdSavePath (const char *Cmd) { } static command_t setCommandTable[] = { - {"help", setCmdHelp, AlwaysAvailable, "This help"}, - {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, - {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, - {"color", setCmdColor, AlwaysAvailable, "Set color support"}, - {"defaultsavepath", setCmdSavePath, AlwaysAvailable, "Set default save path"}, - {"clientdebug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, - {"devicedebug", setCmdDeviceDebug, AlwaysAvailable, "Set device debug level"}, + {"help", setCmdHelp, AlwaysAvailable, "This help"}, + {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, + {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, + {"color", setCmdColor, AlwaysAvailable, "Set color support"}, + {"defaultsavepaths", setCmdSavePaths, AlwaysAvailable, "Set default save paths"}, + {"clientdebug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, + {"devicedebug", setCmdDeviceDebug, AlwaysAvailable, "Set device debug level"}, {NULL, NULL, NULL, NULL} }; @@ -819,6 +900,8 @@ static int CmdPrefShow(const char *Cmd) { // showPlotPosState (); // showOverlayPosState (); showSavePathState(prefShowNone); + showDumpPathState(prefShowNone); + showTracePathState(prefShowNone); showClientDebugState(prefShowNone); showDeviceDebugState(prefShowNone); PrintAndLogEx(NORMAL, ""); diff --git a/client/src/proxguiqt.cpp b/client/src/proxguiqt.cpp index 945a1922f..f2fb5986a 100644 --- a/client/src/proxguiqt.cpp +++ b/client/src/proxguiqt.cpp @@ -139,21 +139,21 @@ ProxGuiQT::~ProxGuiQT(void) { SliderWidget::SliderWidget() { // Set the initail postion and size from settings if (session.preferences_loaded) - setGeometry(session.window_overlay_xpos, session.window_overlay_ypos, session.window_overlay_wsize, session.window_overlay_hsize); + setGeometry(session.overlay.x, session.overlay.y, session.overlay.w, session.overlay.h); else resize(800, 400); } void SliderWidget::resizeEvent(QResizeEvent *event) { - session.window_overlay_hsize = event->size().height(); - session.window_overlay_wsize = event->size().width(); + session.overlay.h = event->size().height(); + session.overlay.w = event->size().width(); session.window_changed = true; } void SliderWidget::moveEvent(QMoveEvent *event) { - session.window_overlay_xpos = event->pos().x(); - session.window_overlay_ypos = event->pos().y(); + session.overlay.x = event->pos().x(); + session.overlay.y = event->pos().y(); session.window_changed = true; } @@ -199,7 +199,7 @@ ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) { this->master = master; // Set the initail postion and size from settings if (session.preferences_loaded) - setGeometry(session.window_plot_xpos, session.window_plot_ypos, session.window_plot_wsize, session.window_plot_hsize); + setGeometry(session.plot.x, session.plot.y, session.plot.w, session.plot.h); else resize(800, 400); @@ -224,7 +224,7 @@ ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) { QObject::connect(opsController->horizontalSlider_dirthr_down, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int))); QObject::connect(opsController->horizontalSlider_askedge, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int))); - controlWidget->setGeometry(session.window_overlay_xpos, session.window_overlay_ypos, session.window_overlay_wsize, session.window_overlay_hsize); + controlWidget->setGeometry(session.overlay.x, session.overlay.y, session.overlay.w, session.overlay.h); // Set up the plot widget, which does the actual plotting plot = new Plot(this); @@ -292,13 +292,13 @@ void ProxWidget::showEvent(QShowEvent *event) { plot->show(); } void ProxWidget::moveEvent(QMoveEvent *event) { - session.window_plot_xpos = event->pos().x(); - session.window_plot_ypos = event->pos().y(); + session.plot.x = event->pos().x(); + session.plot.y = event->pos().y(); session.window_changed = true; } void ProxWidget::resizeEvent(QResizeEvent *event) { - session.window_plot_hsize = event->size().height(); - session.window_plot_wsize = event->size().width(); + session.plot.h = event->size().height(); + session.plot.w = event->size().width(); session.window_changed = true; } diff --git a/client/src/ui.h b/client/src/ui.h index 620769faa..1cfd9db21 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -21,6 +21,9 @@ typedef enum logLevel {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG, INPLA typedef enum emojiMode {ALIAS, EMOJI, ALTTEXT, ERASE} emojiMode_t; typedef enum clientdebugLevel {cdbOFF,cdbSIMPLE,cdbFULL} clientdebugLevel_t; typedef enum devicedebugLevel {ddbOFF,ddbERROR,ddbINFO,ddbDEBUG,ddbEXTENDED} devicedebugLevel_t; +#define savePathCount 3 +typedef enum savePaths {spDefault, spDump, spTrace} savePaths_t; +typedef struct {int x; int y; int h; int w;} qtWindow_t; typedef struct { bool preferences_loaded; @@ -32,15 +35,9 @@ typedef struct { bool help_dump_mode; bool show_hints; bool window_changed; // track if plot/overlay pos/size changed to save on exit - char *default_savepath;// [FILENAME_MAX]; - int window_plot_xpos; - int window_plot_ypos; - int window_plot_hsize; - int window_plot_wsize; - int window_overlay_xpos; - int window_overlay_ypos; - int window_overlay_hsize; - int window_overlay_wsize; + qtWindow_t plot; + qtWindow_t overlay; + char *defaultPaths[savePathCount]; // Array should allow loop searching for files clientdebugLevel_t client_debug_level; uint8_t device_debug_level; } session_arg_t; From 5060a52b3a13c1e126e50603fff9a479faa572cc Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sat, 18 Apr 2020 18:47:30 +1000 Subject: [PATCH 04/47] Update fileutils.c Linux file permission fix --- client/src/fileutils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index d98b3d7ff..ad68d43e8 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -139,7 +139,7 @@ static bool is_directory(const char *filename) { #ifdef _WIN32 #define make_dir(a) _mkdir(a) #else -#define make_dir(a) mkdir(a,0x755) +#define make_dir(a) mkdir(a,0755) #endif bool create_path(const char *dirname) { From 600e5d7d6f98e6317413b9763459a964fa8bb527 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sun, 19 Apr 2020 10:49:09 +1000 Subject: [PATCH 05/47] streamline works --- client/src/fileutils.c | 20 +++++- client/src/fileutils.h | 2 + client/src/preferences.c | 149 +++++++++++++++++++++++---------------- client/src/ui.h | 5 +- 4 files changed, 112 insertions(+), 64 deletions(-) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index ad68d43e8..54df5a17f 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -139,7 +139,7 @@ static bool is_directory(const char *filename) { #ifdef _WIN32 #define make_dir(a) _mkdir(a) #else -#define make_dir(a) mkdir(a,0755) +#define make_dir(a) mkdir(a,0755) //note 0755 MUST have leading 0 for octal linux file permissions #endif bool create_path(const char *dirname) { @@ -190,6 +190,24 @@ bool create_path(const char *dirname) { return true; } +bool setDefaultPath (savePaths_t pathIndex,const char *Path) { + + if (pathIndex < spItemCount) { + if ((Path == NULL) && (session.defaultPaths[pathIndex] != NULL)) { + free (session.defaultPaths[pathIndex]); + session.defaultPaths[pathIndex] = NULL; + } + + if (Path != NULL) { + session.defaultPaths[pathIndex] = (char *)realloc(session.defaultPaths[pathIndex], strlen(Path) + 1); + strcpy(session.defaultPaths[pathIndex], Path); + } + } else { + return false; + } + + return true; +} static char *filenamemcopy(const char *preferredName, const char *suffix) { if (preferredName == NULL) return NULL; diff --git a/client/src/fileutils.h b/client/src/fileutils.h index bd7886aa3..6d83fbcfe 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -75,6 +75,8 @@ typedef enum { int fileExists(const char *filename); bool create_path(const char *dirname); +bool setDefaultPath (savePaths_t pathIndex,const char *Path); // set a path in the path list session.defaultPaths + /** * @brief Utility function to save data to a binary file. This method takes a preferred name, but if that diff --git a/client/src/preferences.c b/client/src/preferences.c index eb58389d2..b649a6f13 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -67,31 +67,23 @@ int preferences_load(void) { session.show_hints = false; session.supports_colors = false; + // default save path + if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder + setDefaultPath (spDefault, get_my_user_directory()); + else + setDefaultPath (spDefault, "."); - // default save path - if (get_my_user_directory() != NULL) { // should return path to .proxmark3 folder - session.defaultPaths[spDefault] = (char *)realloc(session.defaultPaths[spDefault], strlen(get_my_user_directory()) + 1); //, sizeof(uint8_t)); - strcpy(session.defaultPaths[spDefault], get_my_user_directory()); - } else { - session.defaultPaths[spDefault] = (char *)realloc(session.defaultPaths[spDefault], 2); // 2, sizeof(uint8_t)); - strcpy(session.defaultPaths[spDefault], "."); - } // default dump path - if (get_my_user_directory() != NULL) { // should return path to .proxmark3 folder - session.defaultPaths[spDump] = (char *)realloc(session.defaultPaths[spDump], strlen(get_my_user_directory()) + 1); //, sizeof(uint8_t)); - strcpy(session.defaultPaths[spDump], get_my_user_directory()); - } else { - session.defaultPaths[spDump] = (char *)realloc(session.defaultPaths[spDump], 2); // 2, sizeof(uint8_t)); - strcpy(session.defaultPaths[spDump], "."); - } - // default save path - if (get_my_user_directory() != NULL) { // should return path to .proxmark3 folder - session.defaultPaths[spTrace] = (char *)realloc(session.defaultPaths[spTrace], strlen(get_my_user_directory()) + 1); //, sizeof(uint8_t)); - strcpy(session.defaultPaths[spTrace], get_my_user_directory()); - } else { - session.defaultPaths[spTrace] = (char *)realloc(session.defaultPaths[spTrace], 2); // 2, sizeof(uint8_t)); - strcpy(session.defaultPaths[spTrace], "."); - } + if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder + setDefaultPath (spDump, get_my_user_directory()); + else + setDefaultPath (spDump, "."); + + // default dump path + if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder + setDefaultPath (spTrace, get_my_user_directory()); + else + setDefaultPath (spTrace, "."); // loadFileJson wants these, so pass in place holder values, though not used // in settings load; @@ -242,21 +234,17 @@ void preferences_load_callback(json_t *root) { } // default save path - if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.savepath", &s1) == 0) { - session.defaultPaths[spDefault] = realloc(session.defaultPaths[spDefault], strlen(s1) + 1); - strcpy(session.defaultPaths[spDefault], s1); - } + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.savepath", &s1) == 0) + setDefaultPath (spDefault,s1); + // default dump path - if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.dumppath", &s1) == 0) { - session.defaultPaths[spDump] = realloc(session.defaultPaths[spDump],strlen(s1) + 1); - strcpy(session.defaultPaths[spDump],s1); - } + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.dumppath", &s1) == 0) + setDefaultPath (spDump,s1); + // default trace path - if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.tracepath", &s1) == 0) { - session.defaultPaths[spTrace] = realloc(session.defaultPaths[spTrace],strlen(s1) + 1); - strcpy(session.defaultPaths[spTrace],s1); - } - + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.tracepath", &s1) == 0) + setDefaultPath (spTrace,s1); + // window plot if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.xpos", &i1) == 0) session.plot.x = i1; @@ -461,16 +449,33 @@ void showDeviceDebugState(prefShowOpt_t Opt) { } } -void showSavePathState(prefShowOpt_t Opt) { - PrintAndLogEx(NORMAL, " %s default save path...... "_GREEN_("%s"), prefShowMsg(Opt), session.defaultPaths[spDefault]); +void showSavePathState(savePaths_t pathIndex, prefShowOpt_t Opt) { + + char tempStr[50]; + + switch (pathIndex) { + case spDefault: + strcpy (tempStr,"default save path......"); + break; + case spDump: + strcpy (tempStr,"dump save path........."); + break; + case spTrace: + strcpy (tempStr,"trace save path........"); + break; + default: + strcpy (tempStr,_RED_("unknown")" save path......"); + } + PrintAndLogEx(NORMAL, " %s %s "_GREEN_("%s"), prefShowMsg(Opt), tempStr, session.defaultPaths[pathIndex]); } +/* void showDumpPathState(prefShowOpt_t Opt) { PrintAndLogEx(NORMAL, " %s dump save path......... "_GREEN_("%s"), prefShowMsg(Opt), session.defaultPaths[spDump]); } void showTracePathState(prefShowOpt_t Opt) { PrintAndLogEx(NORMAL, " %s trace save path........ "_GREEN_("%s"), prefShowMsg(Opt), session.defaultPaths[spTrace]); } - +*/ void showPlotPosState(void){ PrintAndLogEx(NORMAL, " Plot window............ X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), session.plot.x, session.plot.y, session.plot.h, session.plot.w); @@ -760,6 +765,8 @@ static int setCmdSavePaths (const char *Cmd) { int optLen = 0; char *newValue = NULL; bool createDir = false; + savePaths_t pathItem = spItemCount; + if (param_getchar(Cmd, cmdp) == 0x00) return usage_set_savePaths(); @@ -804,45 +811,67 @@ static int setCmdSavePaths (const char *Cmd) { if (!fileExists(newValue)) create_path (newValue); //mkdir (newValue,0x777); - // Default - if (strncmp(strOpt, "default", 7) == 0) { - if (strcmp(newValue, session.defaultPaths[spDefault]) != 0) { - showSavePathState(prefShowOLD); - session.defaultPaths[spDefault] = (char *)realloc(session.defaultPaths[spDefault], strlen(newValue) + 1); - strcpy (session.defaultPaths[spDefault],newValue); - showSavePathState(prefShowNEW); + pathItem = spItemCount; + if (strncmp(strOpt, "default", 7) == 0) pathItem = spDefault; + if (strncmp(strOpt, "dump", 4) == 0) pathItem = spDump; + if (strncmp(strOpt, "trace", 7) == 0) pathItem = spTrace; + + if (pathItem < spItemCount) { + if (strcmp(newValue, session.defaultPaths[pathItem]) != 0) { + showSavePathState(pathItem, prefShowOLD); + setDefaultPath (pathItem, newValue); + showSavePathState(pathItem, prefShowNEW); preferences_save(); } else { PrintAndLogEx(INFO, "nothing changed"); - showSavePathState(prefShowNone); + showSavePathState(pathItem, prefShowNone); + } + } + + /* + // Default + if (strncmp(strOpt, "default", 7) == 0) { + if (strcmp(newValue, session.defaultPaths[spDefault]) != 0) { + showSavePathState(spDefault, prefShowOLD); + setDefaultPath (spDefault, newValue); +// session.defaultPaths[spDefault] = (char *)realloc(session.defaultPaths[spDefault], strlen(newValue) + 1); +// strcpy (session.defaultPaths[spDefault],newValue); + showSavePathState(spDefault, prefShowNEW); + preferences_save(); + } else { + PrintAndLogEx(INFO, "nothing changed"); + showSavePathState(spDefault, prefShowNone); } } // Dump if (strncmp(strOpt, "dump", 4) == 0) { if (strcmp(newValue, session.defaultPaths[spDump]) != 0) { - showDumpPathState(prefShowOLD); - session.defaultPaths[spDump] = (char *)realloc(session.defaultPaths[spDump], strlen(newValue) + 1); - strcpy (session.defaultPaths[spDump],newValue); - showDumpPathState(prefShowNEW); + showSavePathState(spDump, prefShowOLD); + setDefaultPath (spDump, newValue); +// session.defaultPaths[spDump] = (char *)realloc(session.defaultPaths[spDump], strlen(newValue) + 1); +// strcpy (session.defaultPaths[spDump],newValue); + showSavePathState(spDump, prefShowNEW); preferences_save(); } else { PrintAndLogEx(INFO, "nothing changed"); - showDumpPathState(prefShowNone); + showSavePathState(spDump, prefShowNone); } } // Trace if (strncmp(strOpt, "trace", 7) == 0) { if (strcmp(newValue, session.defaultPaths[spTrace]) != 0) { - showTracePathState(prefShowOLD); - session.defaultPaths[spTrace] = (char *)realloc(session.defaultPaths[spTrace], strlen(newValue) + 1); - strcpy (session.defaultPaths[spTrace],newValue); - showTracePathState(prefShowNEW); + showSavePathState(spTrace, prefShowOLD); + setDefaultPath (spTrace, newValue); +// session.defaultPaths[spTrace] = (char *)realloc(session.defaultPaths[spTrace], strlen(newValue) + 1); +// strcpy (session.defaultPaths[spTrace],newValue); + showSavePathState(spTrace,prefShowNEW); preferences_save(); } else { PrintAndLogEx(INFO, "nothing changed"); - showTracePathState(prefShowNone); + showSavePathState(spTrace,prefShowNone); } } + */ } } else { return usage_set_savePaths(); @@ -899,9 +928,9 @@ static int CmdPrefShow(const char *Cmd) { showColorState(prefShowNone); // showPlotPosState (); // showOverlayPosState (); - showSavePathState(prefShowNone); - showDumpPathState(prefShowNone); - showTracePathState(prefShowNone); + showSavePathState(spDefault, prefShowNone); + showSavePathState(spDump, prefShowNone); + showSavePathState(spTrace, prefShowNone); showClientDebugState(prefShowNone); showDeviceDebugState(prefShowNone); PrintAndLogEx(NORMAL, ""); diff --git a/client/src/ui.h b/client/src/ui.h index 1cfd9db21..80d1794df 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -21,8 +21,7 @@ typedef enum logLevel {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG, INPLA typedef enum emojiMode {ALIAS, EMOJI, ALTTEXT, ERASE} emojiMode_t; typedef enum clientdebugLevel {cdbOFF,cdbSIMPLE,cdbFULL} clientdebugLevel_t; typedef enum devicedebugLevel {ddbOFF,ddbERROR,ddbINFO,ddbDEBUG,ddbEXTENDED} devicedebugLevel_t; -#define savePathCount 3 -typedef enum savePaths {spDefault, spDump, spTrace} savePaths_t; +typedef enum savePaths {spDefault, spDump, spTrace, spItemCount} savePaths_t; // last item spItemCount used to auto map to number of files typedef struct {int x; int y; int h; int w;} qtWindow_t; typedef struct { @@ -37,7 +36,7 @@ typedef struct { bool window_changed; // track if plot/overlay pos/size changed to save on exit qtWindow_t plot; qtWindow_t overlay; - char *defaultPaths[savePathCount]; // Array should allow loop searching for files + char *defaultPaths[spItemCount]; // Array should allow loop searching for files clientdebugLevel_t client_debug_level; uint8_t device_debug_level; } session_arg_t; From dd09d4074c19d94278528177691a5b30d9605377 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Tue, 21 Apr 2020 08:50:04 +1000 Subject: [PATCH 06/47] tuning File List optimised. --- client/src/preferences.c | 45 ---------------------------------------- client/src/ui.h | 4 ++-- 2 files changed, 2 insertions(+), 47 deletions(-) diff --git a/client/src/preferences.c b/client/src/preferences.c index b649a6f13..73cd60293 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -827,51 +827,6 @@ static int setCmdSavePaths (const char *Cmd) { showSavePathState(pathItem, prefShowNone); } } - - /* - // Default - if (strncmp(strOpt, "default", 7) == 0) { - if (strcmp(newValue, session.defaultPaths[spDefault]) != 0) { - showSavePathState(spDefault, prefShowOLD); - setDefaultPath (spDefault, newValue); -// session.defaultPaths[spDefault] = (char *)realloc(session.defaultPaths[spDefault], strlen(newValue) + 1); -// strcpy (session.defaultPaths[spDefault],newValue); - showSavePathState(spDefault, prefShowNEW); - preferences_save(); - } else { - PrintAndLogEx(INFO, "nothing changed"); - showSavePathState(spDefault, prefShowNone); - } - } - // Dump - if (strncmp(strOpt, "dump", 4) == 0) { - if (strcmp(newValue, session.defaultPaths[spDump]) != 0) { - showSavePathState(spDump, prefShowOLD); - setDefaultPath (spDump, newValue); -// session.defaultPaths[spDump] = (char *)realloc(session.defaultPaths[spDump], strlen(newValue) + 1); -// strcpy (session.defaultPaths[spDump],newValue); - showSavePathState(spDump, prefShowNEW); - preferences_save(); - } else { - PrintAndLogEx(INFO, "nothing changed"); - showSavePathState(spDump, prefShowNone); - } - } - // Trace - if (strncmp(strOpt, "trace", 7) == 0) { - if (strcmp(newValue, session.defaultPaths[spTrace]) != 0) { - showSavePathState(spTrace, prefShowOLD); - setDefaultPath (spTrace, newValue); -// session.defaultPaths[spTrace] = (char *)realloc(session.defaultPaths[spTrace], strlen(newValue) + 1); -// strcpy (session.defaultPaths[spTrace],newValue); - showSavePathState(spTrace,prefShowNEW); - preferences_save(); - } else { - PrintAndLogEx(INFO, "nothing changed"); - showSavePathState(spTrace,prefShowNone); - } - } - */ } } else { return usage_set_savePaths(); diff --git a/client/src/ui.h b/client/src/ui.h index 80d1794df..f691de450 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -19,8 +19,8 @@ typedef enum logLevel {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG, INPLACE, HINT} logLevel_t; typedef enum emojiMode {ALIAS, EMOJI, ALTTEXT, ERASE} emojiMode_t; -typedef enum clientdebugLevel {cdbOFF,cdbSIMPLE,cdbFULL} clientdebugLevel_t; -typedef enum devicedebugLevel {ddbOFF,ddbERROR,ddbINFO,ddbDEBUG,ddbEXTENDED} devicedebugLevel_t; +typedef enum clientdebugLevel {cdbOFF, cdbSIMPLE, cdbFULL} clientdebugLevel_t; +typedef enum devicedebugLevel {ddbOFF, ddbERROR, ddbINFO, ddbDEBUG, ddbEXTENDED} devicedebugLevel_t; typedef enum savePaths {spDefault, spDump, spTrace, spItemCount} savePaths_t; // last item spItemCount used to auto map to number of files typedef struct {int x; int y; int h; int w;} qtWindow_t; From 3aee4fc5e9f72da2cd3eb5a21f06d05466ce8614 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Tue, 21 Apr 2020 12:35:13 +1000 Subject: [PATCH 07/47] Update preferences.c --- client/src/preferences.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/preferences.c b/client/src/preferences.c index 73cd60293..cc8fd7b6f 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -790,7 +790,7 @@ static int setCmdSavePaths (const char *Cmd) { } else { if ((strncmp(strOpt, "default", 7) == 0) || (strncmp(strOpt, "dump", 4) == 0) || - (strncmp(strOpt, "trace", 7) == 0)) { + (strncmp(strOpt, "trace", 5) == 0)) { // Get Path optLen = param_getlength(Cmd, cmdp) + 1; @@ -814,7 +814,7 @@ static int setCmdSavePaths (const char *Cmd) { pathItem = spItemCount; if (strncmp(strOpt, "default", 7) == 0) pathItem = spDefault; if (strncmp(strOpt, "dump", 4) == 0) pathItem = spDump; - if (strncmp(strOpt, "trace", 7) == 0) pathItem = spTrace; + if (strncmp(strOpt, "trace", 5) == 0) pathItem = spTrace; if (pathItem < spItemCount) { if (strcmp(newValue, session.defaultPaths[pathItem]) != 0) { From 7fdaa51678645530d744b78bdf12616ef5ae1870 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Fri, 24 Apr 2020 16:56:35 +1000 Subject: [PATCH 08/47] Pre Filepath cleanup --- client/src/fileutils.c | 4 ++- client/src/fileutils.h | 2 +- client/src/preferences.c | 73 +++++++++++++++++++++------------------- client/src/proxmark3.c | 9 ++--- client/src/ui.h | 4 +-- 5 files changed, 48 insertions(+), 44 deletions(-) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index 54df5a17f..cf8079c19 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -136,6 +136,8 @@ static bool is_directory(const char *filename) { * @param dirname * @return */ +// Not used... +/* #ifdef _WIN32 #define make_dir(a) _mkdir(a) #else @@ -189,7 +191,7 @@ bool create_path(const char *dirname) { } return true; } - +*/ bool setDefaultPath (savePaths_t pathIndex,const char *Path) { if (pathIndex < spItemCount) { diff --git a/client/src/fileutils.h b/client/src/fileutils.h index 6d83fbcfe..39e7886ab 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -74,7 +74,7 @@ typedef enum { } DumpFileType_t; int fileExists(const char *filename); -bool create_path(const char *dirname); +//bool create_path(const char *dirname); bool setDefaultPath (savePaths_t pathIndex,const char *Path); // set a path in the path list session.defaultPaths diff --git a/client/src/preferences.c b/client/src/preferences.c index cc8fd7b6f..d3c462e0f 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -49,11 +49,9 @@ static char *prefGetFilename(void) { int preferences_load(void) { - PrintAndLogEx(INFO, "Looking for preferences..."); - // Set all defaults session.client_debug_level = cdbOFF; - session.device_debug_level = ddbOFF; + // session.device_debug_level = ddbOFF; session.window_changed = false; session.plot.x = 10; session.plot.y = 30; @@ -67,6 +65,11 @@ int preferences_load(void) { session.show_hints = false; session.supports_colors = false; + setDefaultPath (spDefault, ""); + setDefaultPath (spDump, ""); + setDefaultPath (spTrace, ""); + +/* // default save path if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder setDefaultPath (spDefault, get_my_user_directory()); @@ -84,14 +87,19 @@ int preferences_load(void) { setDefaultPath (spTrace, get_my_user_directory()); else setDefaultPath (spTrace, "."); +*/ // loadFileJson wants these, so pass in place holder values, though not used // in settings load; uint8_t dummyData = 0x00; size_t dummyDL = 0x00; - if (loadFileJSON(prefGetFilename(), &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { - session.preferences_loaded = true; + // to better control json cant find file error msg. + if (fileExists(prefGetFilename())) { + PrintAndLogEx(INFO, "Loading Preferences %s",prefGetFilename()); + if (loadFileJSON(prefGetFilename(), &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { + session.preferences_loaded = true; + } } // Note, if session.settings_loaded == false then the settings_save // will be called in main () to save settings as set in defaults and main() checks. @@ -194,7 +202,7 @@ void preferences_save_callback(json_t *root) { default: JsonSaveStr(root, "logging.level", "NORMAL"); } - +/* switch (session.device_debug_level) { case ddbOFF: JsonSaveStr(root, "device.debug.level", "off"); @@ -214,7 +222,7 @@ void preferences_save_callback(json_t *root) { default: JsonSaveStr(root, "logging.level", "NORMAL"); } - +*/ } void preferences_load_callback(json_t *root) { @@ -280,7 +288,7 @@ void preferences_load_callback(json_t *root) { if (json_unpack_ex(root, &up_error, 0, "{s:b}", "os.supports.colors", &b1) == 0) session.supports_colors = b1; - +/* // Logging Level if (json_unpack_ex(root, &up_error, 0, "{s:s}", "device.debug.level", &s1) == 0) { strncpy(tempStr, s1, sizeof(tempStr) - 1); @@ -291,7 +299,7 @@ void preferences_load_callback(json_t *root) { if (strncmp(tempStr, "debug", 5) == 0) session.device_debug_level = ddbDEBUG; if (strncmp(tempStr, "extended", 8) == 0) session.device_debug_level = ddbEXTENDED; } - +*/ } // Help Functions @@ -328,7 +336,7 @@ static int usage_set_debug() { return PM3_SUCCESS; } - +/* static int usage_set_devicedebug() { PrintAndLogEx(NORMAL, "Usage: pref set devicedebug "); PrintAndLogEx(NORMAL, "Options:"); @@ -341,7 +349,7 @@ static int usage_set_devicedebug() { return PM3_SUCCESS; } - +*/ static int usage_set_hints() { PrintAndLogEx(NORMAL, "Usage: pref set hints "); PrintAndLogEx(NORMAL, "Options:"); @@ -426,7 +434,7 @@ void showClientDebugState(prefShowOpt_t Opt) { PrintAndLogEx(NORMAL, " %s client debug........... "_RED_("unknown"), prefShowMsg(Opt)); } } - +/* void showDeviceDebugState(prefShowOpt_t Opt) { switch (session.device_debug_level) { case ddbOFF: @@ -448,7 +456,7 @@ void showDeviceDebugState(prefShowOpt_t Opt) { PrintAndLogEx(NORMAL, " %s device debug........... "_RED_("unknown"), prefShowMsg(Opt)); } } - +*/ void showSavePathState(savePaths_t pathIndex, prefShowOpt_t Opt) { char tempStr[50]; @@ -466,16 +474,12 @@ void showSavePathState(savePaths_t pathIndex, prefShowOpt_t Opt) { default: strcpy (tempStr,_RED_("unknown")" save path......"); } - PrintAndLogEx(NORMAL, " %s %s "_GREEN_("%s"), prefShowMsg(Opt), tempStr, session.defaultPaths[pathIndex]); + if ((session.defaultPaths[pathIndex] == NULL) || (strcmp(session.defaultPaths[pathIndex],"") == 0)) + PrintAndLogEx(NORMAL, " %s %s "_WHITE_("not set"), prefShowMsg(Opt),tempStr); + else + PrintAndLogEx(NORMAL, " %s %s "_GREEN_("%s"), prefShowMsg(Opt), tempStr, session.defaultPaths[pathIndex]); } -/* -void showDumpPathState(prefShowOpt_t Opt) { - PrintAndLogEx(NORMAL, " %s dump save path......... "_GREEN_("%s"), prefShowMsg(Opt), session.defaultPaths[spDump]); -} -void showTracePathState(prefShowOpt_t Opt) { - PrintAndLogEx(NORMAL, " %s trace save path........ "_GREEN_("%s"), prefShowMsg(Opt), session.defaultPaths[spTrace]); -} -*/ + void showPlotPosState(void){ PrintAndLogEx(NORMAL, " Plot window............ X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), session.plot.x, session.plot.y, session.plot.h, session.plot.w); @@ -643,7 +647,7 @@ static int setCmdDebug(const char *Cmd) { return PM3_SUCCESS; } - +/* static int setCmdDeviceDebug (const char *Cmd) { uint8_t cmdp = 0; @@ -709,7 +713,7 @@ static int setCmdDeviceDebug (const char *Cmd) return PM3_SUCCESS; } - +*/ static int setCmdHint (const char *Cmd) { uint8_t cmdp = 0; @@ -808,8 +812,8 @@ static int setCmdSavePaths (const char *Cmd) { PrintAndLogEx(ERR,"path does not exist... "_RED_("%s"),newValue); } else { // do we need to create it - if (!fileExists(newValue)) - create_path (newValue); //mkdir (newValue,0x777); + // if (!fileExists(newValue)) + // create_path (newValue); //mkdir (newValue,0x777); pathItem = spItemCount; if (strncmp(strOpt, "default", 7) == 0) pathItem = spDefault; @@ -847,9 +851,9 @@ static command_t setCommandTable[] = { {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, {"color", setCmdColor, AlwaysAvailable, "Set color support"}, - {"defaultsavepaths", setCmdSavePaths, AlwaysAvailable, "Set default save paths"}, + {"defaultsavepaths", setCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, {"clientdebug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, - {"devicedebug", setCmdDeviceDebug, AlwaysAvailable, "Set device debug level"}, + // {"devicedebug", setCmdDeviceDebug, AlwaysAvailable, "Set device debug level"}, {NULL, NULL, NULL, NULL} }; @@ -870,24 +874,25 @@ int CmdPrefSet(const char *Cmd) { static int CmdPrefShow(const char *Cmd) { PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, _CYAN_("Preferences")); + PrintAndLogEx(NORMAL, _CYAN_("Preferences loaded from %s"),prefGetFilename()); if (!session.preferences_loaded) { PrintAndLogEx (ERR, "Preferneces not loaded"); return PM3_ESOFT; } - PrintAndLogEx(NORMAL, " preference file........ "_GREEN_("%s"), prefGetFilename()); + // PrintAndLogEx(NORMAL, " preference file........ "_GREEN_("%s"), prefGetFilename()); showEmojiState(prefShowNone); showHintsState(prefShowNone); showColorState(prefShowNone); // showPlotPosState (); // showOverlayPosState (); - showSavePathState(spDefault, prefShowNone); - showSavePathState(spDump, prefShowNone); - showSavePathState(spTrace, prefShowNone); + // showSavePathState(spDefault, prefShowNone); + // showSavePathState(spDump, prefShowNone); + // showSavePathState(spTrace, prefShowNone); + showClientDebugState(prefShowNone); - showDeviceDebugState(prefShowNone); +// showDeviceDebugState(prefShowNone); PrintAndLogEx(NORMAL, ""); return PM3_SUCCESS; diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 84768f613..730504714 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -703,10 +703,6 @@ int main(int argc, char *argv[]) { // Load Settings and assign // This will allow the command line to override the settings.json values preferences_load(); - // Change height/width (Rows,Cols) - Testing - // printf ("\e[8;50;100t"); - // printf ("\e[3;50;50t"); // x,y - //printf ("Path : %s \n",my_user_directory); // quick patch for debug level g_debugMode = session.client_debug_level; // settings_save (); @@ -980,9 +976,10 @@ int main(int argc, char *argv[]) { // Doing this here will ensure other checks and updates are saved to over rule default // e.g. Linux color use check if (!session.preferences_loaded) { + PrintAndLogEx (INFO,"Creating initial preferences file"); // json save reports file name, so just info msg here preferences_save(); // Save defaults session.preferences_loaded = true; - } else { + } /* else { // Set device debug level PrintAndLogEx(INFO,"setting device debug loglevel"); if (session.pm3_present) { @@ -994,7 +991,7 @@ int main(int argc, char *argv[]) { else PrintAndLogEx(WARNING,"Proxmark3 not ready to set debug level"); } - + */ #endif #ifdef HAVE_GUI diff --git a/client/src/ui.h b/client/src/ui.h index f691de450..2e5a3fd06 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -20,7 +20,7 @@ typedef enum logLevel {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG, INPLACE, HINT} logLevel_t; typedef enum emojiMode {ALIAS, EMOJI, ALTTEXT, ERASE} emojiMode_t; typedef enum clientdebugLevel {cdbOFF, cdbSIMPLE, cdbFULL} clientdebugLevel_t; -typedef enum devicedebugLevel {ddbOFF, ddbERROR, ddbINFO, ddbDEBUG, ddbEXTENDED} devicedebugLevel_t; +// typedef enum devicedebugLevel {ddbOFF, ddbERROR, ddbINFO, ddbDEBUG, ddbEXTENDED} devicedebugLevel_t; typedef enum savePaths {spDefault, spDump, spTrace, spItemCount} savePaths_t; // last item spItemCount used to auto map to number of files typedef struct {int x; int y; int h; int w;} qtWindow_t; @@ -38,7 +38,7 @@ typedef struct { qtWindow_t overlay; char *defaultPaths[spItemCount]; // Array should allow loop searching for files clientdebugLevel_t client_debug_level; - uint8_t device_debug_level; +// uint8_t device_debug_level; } session_arg_t; extern session_arg_t session; From a8ff7b4815b598bf99c5bc7fff990a9755d0ed57 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sat, 25 Apr 2020 18:37:23 +1000 Subject: [PATCH 09/47] Comment out file path code --- client/src/fileutils.c | 3 ++- client/src/fileutils.h | 2 +- client/src/preferences.c | 29 +++++++++++++++-------------- client/src/ui.h | 4 ++-- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index cf8079c19..936bf0aef 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -192,6 +192,7 @@ bool create_path(const char *dirname) { return true; } */ +/* bool setDefaultPath (savePaths_t pathIndex,const char *Path) { if (pathIndex < spItemCount) { @@ -210,7 +211,7 @@ bool setDefaultPath (savePaths_t pathIndex,const char *Path) { return true; } - +*/ static char *filenamemcopy(const char *preferredName, const char *suffix) { if (preferredName == NULL) return NULL; if (suffix == NULL) return NULL; diff --git a/client/src/fileutils.h b/client/src/fileutils.h index 39e7886ab..2f55a3c1a 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -75,7 +75,7 @@ typedef enum { int fileExists(const char *filename); //bool create_path(const char *dirname); -bool setDefaultPath (savePaths_t pathIndex,const char *Path); // set a path in the path list session.defaultPaths +//bool setDefaultPath (savePaths_t pathIndex,const char *Path); // set a path in the path list session.defaultPaths /** diff --git a/client/src/preferences.c b/client/src/preferences.c index d3c462e0f..3bc7158b4 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -65,9 +65,9 @@ int preferences_load(void) { session.show_hints = false; session.supports_colors = false; - setDefaultPath (spDefault, ""); - setDefaultPath (spDump, ""); - setDefaultPath (spTrace, ""); +// setDefaultPath (spDefault, ""); +// setDefaultPath (spDump, ""); +// setDefaultPath (spTrace, ""); /* // default save path @@ -172,9 +172,9 @@ void preferences_save_callback(json_t *root) { JsonSaveBoolean(root, "os.supports.colors", session.supports_colors); - JsonSaveStr(root, "file.default.savepath", session.defaultPaths[spDefault]); - JsonSaveStr(root, "file.default.dumppath", session.defaultPaths[spDump]); - JsonSaveStr(root, "file.default.tracepath", session.defaultPaths[spTrace]); + // JsonSaveStr(root, "file.default.savepath", session.defaultPaths[spDefault]); + // JsonSaveStr(root, "file.default.dumppath", session.defaultPaths[spDump]); + // JsonSaveStr(root, "file.default.tracepath", session.defaultPaths[spTrace]); // Plot window JsonSaveInt(root, "window.plot.xpos", session.plot.x); @@ -240,7 +240,7 @@ void preferences_load_callback(json_t *root) { if (strncmp(tempStr, "simple", 6) == 0) session.client_debug_level = cdbSIMPLE; if (strncmp(tempStr, "full", 4) == 0) session.client_debug_level = cdbFULL; } - +/* // default save path if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.savepath", &s1) == 0) setDefaultPath (spDefault,s1); @@ -252,7 +252,7 @@ void preferences_load_callback(json_t *root) { // default trace path if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.tracepath", &s1) == 0) setDefaultPath (spTrace,s1); - +*/ // window plot if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.xpos", &i1) == 0) session.plot.x = i1; @@ -359,7 +359,7 @@ static int usage_set_hints() { return PM3_SUCCESS; } - +/* static int usage_set_savePaths() { PrintAndLogEx(NORMAL, "Usage: pref set savepaths [help] [create] [default ] [dump ] [trace ]"); PrintAndLogEx(NORMAL, "Options:"); @@ -371,7 +371,7 @@ static int usage_set_savePaths() { return PM3_SUCCESS; } - +*/ // Preference Processing Functions // typedef enum preferenceId {prefNONE,prefHELP,prefEMOJI,prefCOLOR,prefPLOT,prefOVERLAY,prefHINTS,prefCLIENTDEBUG} preferenceId_t; typedef enum prefShowOpt {prefShowNone, prefShowOLD, prefShowNEW} prefShowOpt_t; @@ -457,6 +457,7 @@ void showDeviceDebugState(prefShowOpt_t Opt) { } } */ +/* void showSavePathState(savePaths_t pathIndex, prefShowOpt_t Opt) { char tempStr[50]; @@ -479,7 +480,7 @@ void showSavePathState(savePaths_t pathIndex, prefShowOpt_t Opt) { else PrintAndLogEx(NORMAL, " %s %s "_GREEN_("%s"), prefShowMsg(Opt), tempStr, session.defaultPaths[pathIndex]); } - +*/ void showPlotPosState(void){ PrintAndLogEx(NORMAL, " Plot window............ X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), session.plot.x, session.plot.y, session.plot.h, session.plot.w); @@ -760,7 +761,7 @@ static int setCmdHint (const char *Cmd) return PM3_SUCCESS; } - +/* static int setCmdSavePaths (const char *Cmd) { uint8_t cmdp = 0; bool errors = false; @@ -845,13 +846,13 @@ static int setCmdSavePaths (const char *Cmd) { return PM3_SUCCESS; } - +*/ static command_t setCommandTable[] = { {"help", setCmdHelp, AlwaysAvailable, "This help"}, {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, {"color", setCmdColor, AlwaysAvailable, "Set color support"}, - {"defaultsavepaths", setCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, + // {"defaultsavepaths", setCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, {"clientdebug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, // {"devicedebug", setCmdDeviceDebug, AlwaysAvailable, "Set device debug level"}, {NULL, NULL, NULL, NULL} diff --git a/client/src/ui.h b/client/src/ui.h index 2e5a3fd06..e47031d70 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -21,7 +21,7 @@ typedef enum logLevel {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG, INPLA typedef enum emojiMode {ALIAS, EMOJI, ALTTEXT, ERASE} emojiMode_t; typedef enum clientdebugLevel {cdbOFF, cdbSIMPLE, cdbFULL} clientdebugLevel_t; // typedef enum devicedebugLevel {ddbOFF, ddbERROR, ddbINFO, ddbDEBUG, ddbEXTENDED} devicedebugLevel_t; -typedef enum savePaths {spDefault, spDump, spTrace, spItemCount} savePaths_t; // last item spItemCount used to auto map to number of files +//typedef enum savePaths {spDefault, spDump, spTrace, spItemCount} savePaths_t; // last item spItemCount used to auto map to number of files typedef struct {int x; int y; int h; int w;} qtWindow_t; typedef struct { @@ -36,7 +36,7 @@ typedef struct { bool window_changed; // track if plot/overlay pos/size changed to save on exit qtWindow_t plot; qtWindow_t overlay; - char *defaultPaths[spItemCount]; // Array should allow loop searching for files +// char *defaultPaths[spItemCount]; // Array should allow loop searching for files clientdebugLevel_t client_debug_level; // uint8_t device_debug_level; } session_arg_t; From a15d14e038d7493e82444c8c42fd3db45ea9ff40 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 25 Apr 2020 15:10:34 +0200 Subject: [PATCH 10/47] Make sure colors and emoji are disabled when not on TTY --- client/src/fileutils.c | 2 +- client/src/proxmark3.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index 936bf0aef..4d1b8355d 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -986,10 +986,10 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_ } *datalen = sptr; } + PrintAndLogEx(SUCCESS, "loaded from JSON file " _YELLOW_("%s"), fileName); if (!strcmp(ctype, "settings")) { preferences_load_callback(root); } - PrintAndLogEx(SUCCESS, "loaded from JSON file " _YELLOW_("%s"), fileName); out: json_decref(root); free(fileName); diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 730504714..0cd545389 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -908,6 +908,12 @@ int main(int argc, char *argv[]) { session.supports_colors = true; session.emoji_mode = EMOJI; } + } else { + // even if prefs, we disable colors if stdin or stdout is not a TTY + if ((! session.stdinOnTTY) || (! session.stdoutOnTTY)) { + session.supports_colors = false; + session.emoji_mode = ALTTEXT; + } } #else if (session.stdinOnTTY && session.stdoutOnTTY) { From ae21a3148f5ca54ea2a5c75a8f54832700f84db5 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 25 Apr 2020 15:17:11 +0200 Subject: [PATCH 11/47] Lighter msg for loading prefs, json will anyway always tell the filename --- client/src/preferences.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/preferences.c b/client/src/preferences.c index 3bc7158b4..f93305b4d 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -96,7 +96,7 @@ int preferences_load(void) { // to better control json cant find file error msg. if (fileExists(prefGetFilename())) { - PrintAndLogEx(INFO, "Loading Preferences %s",prefGetFilename()); + PrintAndLogEx(INFO, "Loading Preferences..."); if (loadFileJSON(prefGetFilename(), &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { session.preferences_loaded = true; } @@ -114,7 +114,7 @@ int preferences_save(void) { char *backupFilename = NULL;// [FILENAME_MAX+sizeof(preferencesFilename)+10] = {0}; int fnLen = 0; - PrintAndLogEx(INFO, "Saving preferences ..."); + PrintAndLogEx(INFO, "Saving Preferences..."); fnLen = strlen(prefGetFilename()) + 5; // .bak\0 backupFilename = (char *)calloc(fnLen, sizeof(uint8_t)); From ec5d777580d0e87d80ee9484372637ac18783a12 Mon Sep 17 00:00:00 2001 From: Iceman Date: Sat, 25 Apr 2020 21:15:39 +0200 Subject: [PATCH 12/47] Update Troubleshooting.md added section about sudo --- doc/md/Installation_Instructions/Troubleshooting.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/md/Installation_Instructions/Troubleshooting.md b/doc/md/Installation_Instructions/Troubleshooting.md index 3fba71e1c..d2c062dcb 100644 --- a/doc/md/Installation_Instructions/Troubleshooting.md +++ b/doc/md/Installation_Instructions/Troubleshooting.md @@ -19,6 +19,7 @@ Always use the latest repository commits from *master* branch. There are always * [Pixmap / pixbuf warnings](#pixmap--pixbuf-warnings) * [Usb cable](#usb-cable) * [WSL 2 explorer.exe . doesnt work](#WSL-2) + * [Troubles with running the Proxmark3 client](#troubles-with-running-the-proxmark3-client) ## `pm3` or `pm3-flash*` doesn't see my Proxmark @@ -173,3 +174,7 @@ Trying to access the dump files created in WSL, you will need to run ```explore [](www.icedev.se/proxmark3/rdv40/wsl2_p9np.png) +## Troubles with running the Proxmark3 client +Some reports has stated that they needed to execute the Proxmark3 as root on their *nix system. +Try running it with + `sudo ./pm3` From 9bd5f27414a05dc5a5caaa65789a27d4fc5eaa94 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 26 Apr 2020 01:23:49 +0200 Subject: [PATCH 13/47] Rename PKG_CONFIG_PATH so it can be overruled by env --- client/Makefile | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/client/Makefile b/client/Makefile index a7bf93383..d3461a302 100644 --- a/client/Makefile +++ b/client/Makefile @@ -83,7 +83,7 @@ else LDLIBS := -L/usr/local/opt/readline/lib $(LDLIBS) LIBS := -I/usr/local/opt/readline/include $(LIBS) # cf brew info qt: qt not symlinked anymore - PKG_CONFIG_PATH := PKG_CONFIG_PATH=/usr/local/opt/qt/lib/pkgconfig + PKG_CONFIG_ENV := PKG_CONFIG_ENV=/usr/local/opt/qt/lib/pkgconfig else LUALIB += -ldl LUAPLATFORM = linux @@ -92,16 +92,16 @@ endif ifneq ($(SKIPQT),1) # Check for correctly configured Qt5 - QTINCLUDES = $(shell $(PKG_CONFIG_PATH) pkg-config --cflags Qt5Core Qt5Widgets 2>/dev/null) - QTLDLIBS = $(shell $(PKG_CONFIG_PATH) pkg-config --libs Qt5Core Qt5Widgets 2>/dev/null) - MOC = $(shell $(PKG_CONFIG_PATH) pkg-config --variable=host_bins Qt5Core)/moc - UIC = $(shell $(PKG_CONFIG_PATH) pkg-config --variable=host_bins Qt5Core)/uic + QTINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags Qt5Core Qt5Widgets 2>/dev/null) + QTLDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs Qt5Core Qt5Widgets 2>/dev/null) + MOC = $(shell $(PKG_CONFIG_ENV) pkg-config --variable=host_bins Qt5Core)/moc + UIC = $(shell $(PKG_CONFIG_ENV) pkg-config --variable=host_bins Qt5Core)/uic ifeq ($(QTINCLUDES), ) # if Qt5 not found check for correctly configured Qt4 - QTINCLUDES = $(shell $(PKG_CONFIG_PATH) pkg-config --cflags QtCore QtGui 2>/dev/null) - QTLDLIBS = $(shell $(PKG_CONFIG_PATH) pkg-config --libs QtCore QtGui 2>/dev/null) - MOC = $(shell $(PKG_CONFIG_PATH) pkg-config --variable=moc_location QtCore) - UIC = $(shell $(PKG_CONFIG_PATH) pkg-config --variable=uic_location QtCore) + QTINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags QtCore QtGui 2>/dev/null) + QTLDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs QtCore QtGui 2>/dev/null) + MOC = $(shell $(PKG_CONFIG_ENV) pkg-config --variable=moc_location QtCore) + UIC = $(shell $(PKG_CONFIG_ENV) pkg-config --variable=uic_location QtCore) else # On OSX Qt5 is claiming for a C++11 compiler (gnu++14 works too, but if nothing it fails) PM3CXXFLAGS += -fPIC -std=c++11 From ba0b28a7dea6c69b196c647ccbf0312758efd963 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sun, 26 Apr 2020 10:18:00 +1000 Subject: [PATCH 14/47] Get/display a single preference --- client/src/preferences.c | 54 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/client/src/preferences.c b/client/src/preferences.c index 3bc7158b4..eb6634531 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -847,6 +847,50 @@ static int setCmdSavePaths (const char *Cmd) { return PM3_SUCCESS; } */ + +int getCmdHelp (const char *Cmd) { + + return PM3_SUCCESS; +} + +int getCmdEmoji (const char *Cmd) { + + showEmojiState(prefShowNone); + + return PM3_SUCCESS; +} + +int getCmdHint (const char *Cmd) { + + showHintsState(prefShowNone); + + return PM3_SUCCESS; +} + +int getCmdColor (const char *Cmd) { + + showColorState(prefShowNone); + + return PM3_SUCCESS; +} + +int getCmdDebug (const char *Cmd) { + + showClientDebugState(prefShowNone); + return PM3_SUCCESS; +} + +static command_t getCommandTable[] = { +// {"help", getCmdHelp, AlwaysAvailable, "This help"}, + {"emoji", getCmdEmoji, AlwaysAvailable, "Get emoji display preference"}, + {"hints", getCmdHint, AlwaysAvailable, "Get hint display preference"}, + {"color", getCmdColor, AlwaysAvailable, "Get color support preference"}, + // {"defaultsavepaths", getCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, + {"clientdebug", getCmdDebug, AlwaysAvailable, "Get client debug level preference"}, + // {"devicedebug", getCmdDeviceDebug, AlwaysAvailable, "Get device debug level"}, + {NULL, NULL, NULL, NULL} +}; + static command_t setCommandTable[] = { {"help", setCmdHelp, AlwaysAvailable, "This help"}, {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, @@ -858,7 +902,6 @@ static command_t setCommandTable[] = { {NULL, NULL, NULL, NULL} }; - static int setCmdHelp(const char *Cmd) { (void)Cmd; // Cmd is not used so far CmdsHelp(setCommandTable); @@ -866,6 +909,12 @@ static int setCmdHelp(const char *Cmd) { return PM3_SUCCESS; } +int CmdPrefGet(const char *Cmd) { + clearCommandBuffer(); + + return CmdsParse(getCommandTable, Cmd); +} + int CmdPrefSet(const char *Cmd) { clearCommandBuffer(); @@ -907,8 +956,9 @@ static int CmdPrefSave (const char *Cmd) { */ static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, + {"get", CmdPrefGet, AlwaysAvailable, "Get a preference"}, {"set", CmdPrefSet, AlwaysAvailable, "Set a preference"}, - {"show", CmdPrefShow, AlwaysAvailable, "Show preferences"}, + {"show", CmdPrefShow, AlwaysAvailable, "Show all preferences"}, {NULL, NULL, NULL, NULL} }; From eea4e5b147c141932027d6d2eda26e89ea6d2a58 Mon Sep 17 00:00:00 2001 From: netvader <5840601+netvader@users.noreply.github.com> Date: Sun, 26 Apr 2020 09:08:43 +0200 Subject: [PATCH 15/47] Update aid_desfire.json --- client/resources/aid_desfire.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/client/resources/aid_desfire.json b/client/resources/aid_desfire.json index e1f679845..206e003a0 100644 --- a/client/resources/aid_desfire.json +++ b/client/resources/aid_desfire.json @@ -313,5 +313,12 @@ FFFFFF General Issuer Information (FIDs 00: MAD Version; 01: Card Holder; 02: Ca "Description": "Campus Card", "Type": "student" }, -} + { + "AID": "C26001", + "Vendor": "CAR2GO", + "Country": "DE", + "Name": "MemberCard", + "Description": "CAR2GO - Member Card", + "Type": "carsharing" + } ] From 7cd509c77c30058d8a6e180c4b30a3f237996755 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 26 Apr 2020 11:18:04 +0200 Subject: [PATCH 16/47] make style --- client/src/fileutils.c | 2 +- client/src/preferences.c | 202 +++++++++++++++++++-------------------- client/src/proxmark3.c | 32 +++---- 3 files changed, 117 insertions(+), 119 deletions(-) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index 4d1b8355d..b0b8d9772 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -149,7 +149,7 @@ bool create_path(const char *dirname) { return false; if ((strlen(dirname) == 1) && (dirname[0] == '/')) - return true; + return true; if ((strlen(dirname) == 2) && (dirname[1] == ':')) return true; diff --git a/client/src/preferences.c b/client/src/preferences.c index f0c3a0f50..2f3fe8156 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -51,7 +51,7 @@ int preferences_load(void) { // Set all defaults session.client_debug_level = cdbOFF; - // session.device_debug_level = ddbOFF; + // session.device_debug_level = ddbOFF; session.window_changed = false; session.plot.x = 10; session.plot.y = 30; @@ -69,25 +69,25 @@ int preferences_load(void) { // setDefaultPath (spDump, ""); // setDefaultPath (spTrace, ""); -/* - // default save path - if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder - setDefaultPath (spDefault, get_my_user_directory()); - else - setDefaultPath (spDefault, "."); + /* + // default save path + if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder + setDefaultPath (spDefault, get_my_user_directory()); + else + setDefaultPath (spDefault, "."); - // default dump path - if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder - setDefaultPath (spDump, get_my_user_directory()); - else - setDefaultPath (spDump, "."); + // default dump path + if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder + setDefaultPath (spDump, get_my_user_directory()); + else + setDefaultPath (spDump, "."); - // default dump path - if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder - setDefaultPath (spTrace, get_my_user_directory()); - else - setDefaultPath (spTrace, "."); -*/ + // default dump path + if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder + setDefaultPath (spTrace, get_my_user_directory()); + else + setDefaultPath (spTrace, "."); + */ // loadFileJson wants these, so pass in place holder values, though not used // in settings load; @@ -156,7 +156,7 @@ void preferences_save_callback(json_t *root) { JsonSaveStr(root, "show.emoji", "alias"); break; case EMOJI: - JsonSaveStr(root,"show.emoji","emoji"); + JsonSaveStr(root, "show.emoji", "emoji"); break; case ALTTEXT: JsonSaveStr(root, "show.emoji", "alttext"); @@ -172,9 +172,9 @@ void preferences_save_callback(json_t *root) { JsonSaveBoolean(root, "os.supports.colors", session.supports_colors); - // JsonSaveStr(root, "file.default.savepath", session.defaultPaths[spDefault]); - // JsonSaveStr(root, "file.default.dumppath", session.defaultPaths[spDump]); - // JsonSaveStr(root, "file.default.tracepath", session.defaultPaths[spTrace]); +// JsonSaveStr(root, "file.default.savepath", session.defaultPaths[spDefault]); +// JsonSaveStr(root, "file.default.dumppath", session.defaultPaths[spDump]); +// JsonSaveStr(root, "file.default.tracepath", session.defaultPaths[spTrace]); // Plot window JsonSaveInt(root, "window.plot.xpos", session.plot.x); @@ -202,27 +202,27 @@ void preferences_save_callback(json_t *root) { default: JsonSaveStr(root, "logging.level", "NORMAL"); } -/* - switch (session.device_debug_level) { - case ddbOFF: - JsonSaveStr(root, "device.debug.level", "off"); - break; - case ddbERROR: - JsonSaveStr(root, "device.debug.level", "error"); - break; - case ddbINFO: - JsonSaveStr(root, "device.debug.level", "info"); - break; - case ddbDEBUG: - JsonSaveStr(root, "device.debug.level", "debug"); - break; - case ddbEXTENDED: - JsonSaveStr(root, "device.debug.level", "extended"); - break; - default: - JsonSaveStr(root, "logging.level", "NORMAL"); - } -*/ + /* + switch (session.device_debug_level) { + case ddbOFF: + JsonSaveStr(root, "device.debug.level", "off"); + break; + case ddbERROR: + JsonSaveStr(root, "device.debug.level", "error"); + break; + case ddbINFO: + JsonSaveStr(root, "device.debug.level", "info"); + break; + case ddbDEBUG: + JsonSaveStr(root, "device.debug.level", "debug"); + break; + case ddbEXTENDED: + JsonSaveStr(root, "device.debug.level", "extended"); + break; + default: + JsonSaveStr(root, "logging.level", "NORMAL"); + } + */ } void preferences_load_callback(json_t *root) { @@ -240,19 +240,19 @@ void preferences_load_callback(json_t *root) { if (strncmp(tempStr, "simple", 6) == 0) session.client_debug_level = cdbSIMPLE; if (strncmp(tempStr, "full", 4) == 0) session.client_debug_level = cdbFULL; } -/* - // default save path - if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.savepath", &s1) == 0) - setDefaultPath (spDefault,s1); + /* + // default save path + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.savepath", &s1) == 0) + setDefaultPath (spDefault,s1); - // default dump path - if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.dumppath", &s1) == 0) - setDefaultPath (spDump,s1); + // default dump path + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.dumppath", &s1) == 0) + setDefaultPath (spDump,s1); - // default trace path - if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.tracepath", &s1) == 0) - setDefaultPath (spTrace,s1); -*/ + // default trace path + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.tracepath", &s1) == 0) + setDefaultPath (spTrace,s1); + */ // window plot if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.xpos", &i1) == 0) session.plot.x = i1; @@ -288,18 +288,18 @@ void preferences_load_callback(json_t *root) { if (json_unpack_ex(root, &up_error, 0, "{s:b}", "os.supports.colors", &b1) == 0) session.supports_colors = b1; -/* - // Logging Level - if (json_unpack_ex(root, &up_error, 0, "{s:s}", "device.debug.level", &s1) == 0) { - strncpy(tempStr, s1, sizeof(tempStr) - 1); - str_lower(tempStr); - if (strncmp(tempStr, "off", 3) == 0) session.device_debug_level = ddbOFF; - if (strncmp(tempStr, "error", 5) == 0) session.device_debug_level = ddbERROR; - if (strncmp(tempStr, "info", 4) == 0) session.device_debug_level = ddbINFO; - if (strncmp(tempStr, "debug", 5) == 0) session.device_debug_level = ddbDEBUG; - if (strncmp(tempStr, "extended", 8) == 0) session.device_debug_level = ddbEXTENDED; - } -*/ + /* + // Logging Level + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "device.debug.level", &s1) == 0) { + strncpy(tempStr, s1, sizeof(tempStr) - 1); + str_lower(tempStr); + if (strncmp(tempStr, "off", 3) == 0) session.device_debug_level = ddbOFF; + if (strncmp(tempStr, "error", 5) == 0) session.device_debug_level = ddbERROR; + if (strncmp(tempStr, "info", 4) == 0) session.device_debug_level = ddbINFO; + if (strncmp(tempStr, "debug", 5) == 0) session.device_debug_level = ddbDEBUG; + if (strncmp(tempStr, "extended", 8) == 0) session.device_debug_level = ddbEXTENDED; + } + */ } // Help Functions @@ -376,13 +376,12 @@ static int usage_set_savePaths() { // typedef enum preferenceId {prefNONE,prefHELP,prefEMOJI,prefCOLOR,prefPLOT,prefOVERLAY,prefHINTS,prefCLIENTDEBUG} preferenceId_t; typedef enum prefShowOpt {prefShowNone, prefShowOLD, prefShowNEW} prefShowOpt_t; -const char *prefShowMsg(prefShowOpt_t Opt) -{ +const char *prefShowMsg(prefShowOpt_t Opt) { switch (Opt) { case prefShowOLD: - return _YELLOW_("[old]"); + return _YELLOW_("[old]"); case prefShowNEW: - return _GREEN_("[new]"); + return _GREEN_("[new]"); case prefShowNone: return ""; } @@ -463,7 +462,7 @@ void showSavePathState(savePaths_t pathIndex, prefShowOpt_t Opt) { char tempStr[50]; switch (pathIndex) { - case spDefault: + case spDefault: strcpy (tempStr,"default save path......"); break; case spDump: @@ -481,7 +480,7 @@ void showSavePathState(savePaths_t pathIndex, prefShowOpt_t Opt) { PrintAndLogEx(NORMAL, " %s %s "_GREEN_("%s"), prefShowMsg(Opt), tempStr, session.defaultPaths[pathIndex]); } */ -void showPlotPosState(void){ +void showPlotPosState(void) { PrintAndLogEx(NORMAL, " Plot window............ X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), session.plot.x, session.plot.y, session.plot.h, session.plot.w); } @@ -657,7 +656,7 @@ static int setCmdDeviceDebug (const char *Cmd) char strOpt[50]; devicedebugLevel_t newValue = session.device_debug_level; - if (param_getchar(Cmd, cmdp) == 0x00) + if (param_getchar(Cmd, cmdp) == 0x00) return usage_set_devicedebug (); while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { @@ -665,12 +664,12 @@ static int setCmdDeviceDebug (const char *Cmd) if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { str_lower(strOpt); // convert to lowercase - if (strncmp (strOpt,"help",4) == 0) + if (strncmp (strOpt,"help",4) == 0) return usage_set_devicedebug(); if (strncmp (strOpt,"off",3) == 0) { validValue = true; newValue = ddbOFF; - } + } if (strncmp (strOpt,"error",5) == 0) { validValue = true; newValue = ddbERROR; @@ -689,7 +688,7 @@ static int setCmdDeviceDebug (const char *Cmd) } if (validValue) { - if (session.device_debug_level != newValue) {// changed + if (session.device_debug_level != newValue) {// changed showDeviceDebugState (prefShowOLD); session.device_debug_level = newValue; showDeviceDebugState (prefShowNEW); @@ -715,8 +714,7 @@ static int setCmdDeviceDebug (const char *Cmd) return PM3_SUCCESS; } */ -static int setCmdHint (const char *Cmd) -{ +static int setCmdHint(const char *Cmd) { uint8_t cmdp = 0; bool errors = false; bool validValue = false; @@ -771,9 +769,9 @@ static int setCmdSavePaths (const char *Cmd) { char *newValue = NULL; bool createDir = false; savePaths_t pathItem = spItemCount; - - - if (param_getchar(Cmd, cmdp) == 0x00) + + + if (param_getchar(Cmd, cmdp) == 0x00) return usage_set_savePaths(); while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { @@ -784,12 +782,12 @@ static int setCmdSavePaths (const char *Cmd) { if (param_getstr(Cmd, cmdp++, strOpt, optLen) != 0) { str_lower(strOpt); // convert to lowercase - if (strncmp(strOpt, "help", 4) == 0) + if (strncmp(strOpt, "help", 4) == 0) return usage_set_savePaths(); if (strncmp(strOpt, "create", 6) == 0) { // check if 2 more options. - if (param_getlength(Cmd, cmdp+1) == 0) // should have min 2 more options + if (param_getlength(Cmd, cmdp+1) == 0) // should have min 2 more options return usage_set_savePaths(); createDir = true; } else { @@ -820,7 +818,7 @@ static int setCmdSavePaths (const char *Cmd) { if (strncmp(strOpt, "default", 7) == 0) pathItem = spDefault; if (strncmp(strOpt, "dump", 4) == 0) pathItem = spDump; if (strncmp(strOpt, "trace", 5) == 0) pathItem = spTrace; - + if (pathItem < spItemCount) { if (strcmp(newValue, session.defaultPaths[pathItem]) != 0) { showSavePathState(pathItem, prefShowOLD); @@ -848,33 +846,33 @@ static int setCmdSavePaths (const char *Cmd) { } */ -int getCmdHelp (const char *Cmd) { - +int getCmdHelp(const char *Cmd) { + return PM3_SUCCESS; } -int getCmdEmoji (const char *Cmd) { +int getCmdEmoji(const char *Cmd) { showEmojiState(prefShowNone); return PM3_SUCCESS; } -int getCmdHint (const char *Cmd) { +int getCmdHint(const char *Cmd) { showHintsState(prefShowNone); return PM3_SUCCESS; } -int getCmdColor (const char *Cmd) { +int getCmdColor(const char *Cmd) { showColorState(prefShowNone); return PM3_SUCCESS; } -int getCmdDebug (const char *Cmd) { +int getCmdDebug(const char *Cmd) { showClientDebugState(prefShowNone); return PM3_SUCCESS; @@ -885,9 +883,9 @@ static command_t getCommandTable[] = { {"emoji", getCmdEmoji, AlwaysAvailable, "Get emoji display preference"}, {"hints", getCmdHint, AlwaysAvailable, "Get hint display preference"}, {"color", getCmdColor, AlwaysAvailable, "Get color support preference"}, - // {"defaultsavepaths", getCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, + // {"defaultsavepaths", getCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, {"clientdebug", getCmdDebug, AlwaysAvailable, "Get client debug level preference"}, - // {"devicedebug", getCmdDeviceDebug, AlwaysAvailable, "Get device debug level"}, + // {"devicedebug", getCmdDeviceDebug, AlwaysAvailable, "Get device debug level"}, {NULL, NULL, NULL, NULL} }; @@ -896,9 +894,9 @@ static command_t setCommandTable[] = { {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, {"color", setCmdColor, AlwaysAvailable, "Set color support"}, - // {"defaultsavepaths", setCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, + // {"defaultsavepaths", setCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, {"clientdebug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, - // {"devicedebug", setCmdDeviceDebug, AlwaysAvailable, "Set device debug level"}, + // {"devicedebug", setCmdDeviceDebug, AlwaysAvailable, "Set device debug level"}, {NULL, NULL, NULL, NULL} }; @@ -924,10 +922,10 @@ int CmdPrefSet(const char *Cmd) { static int CmdPrefShow(const char *Cmd) { PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, _CYAN_("Preferences loaded from %s"),prefGetFilename()); - + PrintAndLogEx(NORMAL, _CYAN_("Preferences loaded from %s"), prefGetFilename()); + if (!session.preferences_loaded) { - PrintAndLogEx (ERR, "Preferneces not loaded"); + PrintAndLogEx(ERR, "Preferneces not loaded"); return PM3_ESOFT; } @@ -935,11 +933,11 @@ static int CmdPrefShow(const char *Cmd) { showEmojiState(prefShowNone); showHintsState(prefShowNone); showColorState(prefShowNone); - // showPlotPosState (); - // showOverlayPosState (); - // showSavePathState(spDefault, prefShowNone); - // showSavePathState(spDump, prefShowNone); - // showSavePathState(spTrace, prefShowNone); + // showPlotPosState (); + // showOverlayPosState (); + // showSavePathState(spDefault, prefShowNone); + // showSavePathState(spDump, prefShowNone); + // showSavePathState(spTrace, prefShowNone); showClientDebugState(prefShowNone); // showDeviceDebugState(prefShowNone); diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 62c438a80..6010fb981 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -419,17 +419,17 @@ const char *get_my_user_directory(void) { } static void set_my_user_directory(void) { -/* my_user_directory = getenv("HOME"); + /* my_user_directory = getenv("HOME"); - // if not found, default to current directory - if (my_user_directory == NULL) { - my_user_directory = GetCurrentDir(_cwd_Buffer, sizeof(_cwd_Buffer)); - // change all slashs to / (windows should not care... - for (int i = 0; i < strlen(_cwd_Buffer); i++) - if (_cwd_Buffer[i] == '\\') _cwd_Buffer[i] = '/'; - // my_user_directory = "."; - } -*/ + // if not found, default to current directory + if (my_user_directory == NULL) { + my_user_directory = GetCurrentDir(_cwd_Buffer, sizeof(_cwd_Buffer)); + // change all slashs to / (windows should not care... + for (int i = 0; i < strlen(_cwd_Buffer); i++) + if (_cwd_Buffer[i] == '\\') _cwd_Buffer[i] = '/'; + // my_user_directory = "."; + } + */ my_user_directory = getenv("HOME"); // if not found, default to current directory @@ -438,23 +438,23 @@ static void set_my_user_directory(void) { char *cwd_Buffer = NULL; uint16_t pathLen = FILENAME_MAX; // should be a good starting point bool error = false; - + cwd_Buffer = (char *)calloc(pathLen, sizeof(uint8_t)); - while (!error && (GetCurrentDir (cwd_Buffer,pathLen) == NULL)) { + while (!error && (GetCurrentDir(cwd_Buffer, pathLen) == NULL)) { if (errno == ERANGE) { // Need bigger buffer pathLen += 10; // if buffer was too small add 10 characters and try again cwd_Buffer = realloc(cwd_Buffer, pathLen); } else { error = true; - free (cwd_Buffer); + free(cwd_Buffer); cwd_Buffer = NULL; } - printf ("Len... %d\n",pathLen); + printf("Len... %d\n", pathLen); } if (!error) { - + for (int i = 0; i < strlen(cwd_Buffer); i++) if (cwd_Buffer[i] == '\\') cwd_Buffer[i] = '/'; @@ -980,7 +980,7 @@ int main(int argc, char *argv[]) { // Doing this here will ensure other checks and updates are saved to over rule default // e.g. Linux color use check if (!session.preferences_loaded) { - PrintAndLogEx (INFO,"Creating initial preferences file"); // json save reports file name, so just info msg here + PrintAndLogEx(INFO, "Creating initial preferences file"); // json save reports file name, so just info msg here preferences_save(); // Save defaults session.preferences_loaded = true; } /* else { From f416fa9b4f933b22cbe15f904975b487fa6cd864 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 12:56:19 +0200 Subject: [PATCH 17/47] cppchecker fixes --- armsrc/Standalone/hf_msdsal.c | 12 ++++++------ armsrc/hitag2crack.c | 4 +--- armsrc/lfsampling.h | 2 +- client/deps/amiitool/amiitool.c | 4 ++-- client/deps/jansson/pack_unpack.c | 2 +- client/src/cmdhf14a.c | 4 ++-- 6 files changed, 13 insertions(+), 15 deletions(-) diff --git a/armsrc/Standalone/hf_msdsal.c b/armsrc/Standalone/hf_msdsal.c index 38416307c..9e3175ea8 100644 --- a/armsrc/Standalone/hf_msdsal.c +++ b/armsrc/Standalone/hf_msdsal.c @@ -371,9 +371,8 @@ void RunMod() { Dbhexdump(len, receivedCmd, false); if (receivedCmd[0] == 0x02 || receivedCmd[0] == 0x03) { //Emulate a Visa MSD(Magnetic stripe data) card - uint8_t ppsea[39] = {0x6F, 0x23, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31, 0xA5, 0x11, 0xBF, 0x0C, 0x0E, 0x61, 0x0C, 0x4F, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x87, 0x01, 0x01, 0x90, 0x00}; - uint8_t processing[10] = {0x80, 0x06, 0x00, 0x80, 0x08, 0x01, 0x01, 0x00, 0x90, 0x00}; - uint8_t visauid[34] = {0x6F, 0x1E, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0xA5, 0x13, 0x50, 0x0B, 0x56, 0x49, 0x53, 0x41, 0x20, 0x43, 0x52, 0x45, 0x44, 0x49, 0x54, 0x9F, 0x38, 0x03, 0x9F, 0x66, 0x02, 0x90, 0x00}; + uint8_t processing_long[10] = {0x80, 0x06, 0x00, 0x80, 0x08, 0x01, 0x01, 0x00, 0x90, 0x00}; + uint8_t visauid_long[34] = {0x6F, 0x1E, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0xA5, 0x13, 0x50, 0x0B, 0x56, 0x49, 0x53, 0x41, 0x20, 0x43, 0x52, 0x45, 0x44, 0x49, 0x54, 0x9F, 0x38, 0x03, 0x9F, 0x66, 0x02, 0x90, 0x00}; uint8_t last[4] = {0x70, 0x15, 0x57, 0x13}; uint8_t card[25]; @@ -389,16 +388,17 @@ void RunMod() { //Depending on card reader commands, the Proxmark will answer to fool the reader if (receivedCmd[2] == 0xA4 && receivedCmd[6] == 0x32 && prevCmd == 0) { //Respond with PPSE + uint8_t ppsea[39] = {0x6F, 0x23, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31, 0xA5, 0x11, 0xBF, 0x0C, 0x0E, 0x61, 0x0C, 0x4F, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x87, 0x01, 0x01, 0x90, 0x00}; memcpy(&dynamic_response_info.response[1], ppsea, sizeof(ppsea)); dynamic_response_info.response_n = sizeof(ppsea) + 1; prevCmd++; } else if (receivedCmd[2] == 0xA4 && receivedCmd[10] == 0x03 && receivedCmd[11] == 0x10 && prevCmd == 1) { //Respond Visa AID - memcpy(&dynamic_response_info.response[1], visauid, sizeof(visauid)); + memcpy(&dynamic_response_info.response[1], visauid_long, sizeof(visauid)); dynamic_response_info.response_n = sizeof(visauid) + 1; prevCmd++; } else if (receivedCmd[1] == 0x80 && receivedCmd[2] == 0xA8 && receivedCmd[6] == 0x83 && prevCmd == 2) { //GET PROCESSING - memcpy(&dynamic_response_info.response[1], processing, sizeof(processing)); - dynamic_response_info.response_n = sizeof(processing) + 1; + memcpy(&dynamic_response_info.response[1], processing_long, sizeof(processing)); + dynamic_response_info.response_n = sizeof(processing_long) + 1; prevCmd++; } else if (receivedCmd[1] == 0x00 && receivedCmd[2] == 0xB2 && prevCmd == 3) { //SFI memcpy(&dynamic_response_info.response[1], card, sizeof(card)); diff --git a/armsrc/hitag2crack.c b/armsrc/hitag2crack.c index 34d35ada6..faf530897 100644 --- a/armsrc/hitag2crack.c +++ b/armsrc/hitag2crack.c @@ -32,7 +32,7 @@ bool hitag2_crack(uint8_t *response, uint8_t *nrarhex) { uint8_t uid[32]; uint8_t nrar[64]; uint8_t e_firstcmd[10]; - uint8_t e_page0cmd[10]; +// uint8_t e_page0cmd[10]; uint8_t keybits[42]; uint8_t pagehex[9]; uint8_t temp[20]; @@ -298,7 +298,6 @@ bool hitag2crack_read_page(uint8_t *responsestr, uint8_t pagenum, uint8_t *nrar, uint8_t e_responsestr[9]; uint8_t e_response[32]; uint8_t response[32]; - int i; if (pagenum > 7) { UserMessage("hitag2crack_read_page:\r\n invalid pagenum\r\n"); @@ -354,7 +353,6 @@ bool hitag2crack_send_e_cmd(uint8_t *responsestr, uint8_t *nrar, uint8_t *cmd, i // uint8_t tmp[37]; uint8_t uid[9]; uint8_t e_page3str[9]; - int ret = 0; // get the UID if (!hitag2_get_uid(uid)) { diff --git a/armsrc/lfsampling.h b/armsrc/lfsampling.h index 570dc20fe..bad90d62c 100644 --- a/armsrc/lfsampling.h +++ b/armsrc/lfsampling.h @@ -82,7 +82,7 @@ uint32_t getSampleCounter(); * 0 or 95 ==> 125 kHz * **/ -void LFSetupFPGAForADC(int divisor, bool lf_field); +void LFSetupFPGAForADC(int divisor, bool reader_field); /** * Called from the USB-handler to set the sampling configuration diff --git a/client/deps/amiitool/amiitool.c b/client/deps/amiitool/amiitool.c index 2c5c4ddc2..b63222d50 100644 --- a/client/deps/amiitool/amiitool.c +++ b/client/deps/amiitool/amiitool.c @@ -86,7 +86,7 @@ int main(int argc, char **argv) { return 1; } - nfc3d_amiibo_keys amiiboKeys; + nfc3d_amiibo_keys amiiboKeys = {0}; if (! LoadAmiikey(amiiboKeys, keyfile)) return 5; @@ -136,7 +136,7 @@ int main(int argc, char **argv) { return 3; } } - size_t readPages = fread(original, 4, NTAG215_SIZE / 4, f); + readPages = fread(original, 4, NTAG215_SIZE / 4, f); if (readPages < NFC3D_AMIIBO_SIZE / 4) { fprintf(stderr, "Could not read from save\n"); fclose(f); diff --git a/client/deps/jansson/pack_unpack.c b/client/deps/jansson/pack_unpack.c index 651e1c56b..279499ebd 100644 --- a/client/deps/jansson/pack_unpack.c +++ b/client/deps/jansson/pack_unpack.c @@ -185,7 +185,7 @@ static char *read_string(scanner_t *s, va_list *ap, length = va_arg(*ap, size_t); } else { prev_token(s); - length = s->has_error ? 0 : strlen(str); + length = s->has_error == 1 ? 0 : strlen(str); } if (!s->has_error && strbuffer_append_bytes(&strbuff, str, length) == -1) { diff --git a/client/src/cmdhf14a.c b/client/src/cmdhf14a.c index c53bc08b6..262e879f4 100644 --- a/client/src/cmdhf14a.c +++ b/client/src/cmdhf14a.c @@ -1024,7 +1024,7 @@ static int CmdHF14ACmdRaw(const char *Cmd) { active_select = true; break; case 'b': - sscanf(Cmd + i + 2, "%d", &temp); + sscanf(Cmd + i + 2, "%u", &temp); numbits = temp & 0xFFFF; i += 3; while (Cmd[i] != ' ' && Cmd[i] != '\0') { i++; } @@ -1032,7 +1032,7 @@ static int CmdHF14ACmdRaw(const char *Cmd) { break; case 't': bTimeout = true; - sscanf(Cmd + i + 2, "%d", &temp); + sscanf(Cmd + i + 2, "%u", &temp); timeout = temp; i += 3; while (Cmd[i] != ' ' && Cmd[i] != '\0') { i++; } From dd5679a53a3454a5af080554aa2194662fba46ea Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 13:49:06 +0200 Subject: [PATCH 18/47] cppchecker fixes --- client/src/cmdhfmfdes.c | 114 ++++++++++++++---------- client/src/cmdhfmfu.c | 19 ++-- client/src/proxguiqt.cpp | 18 ++-- client/src/proxguiqt.h | 4 +- tools/hitag2crack/crack5/ht2crack5.c | 4 +- tools/hitag2crack/crack5gpu/ht2crack5.c | 2 +- 6 files changed, 93 insertions(+), 68 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 4b091343b..ea466c0dc 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -1478,7 +1478,7 @@ static int handler_desfire_writedata(mfdes_data_t *data, MFDES_FILE_TYPE_T type) if (data->fileno > 0x1F) return PM3_EINVARG; int datatowrite = le24toh(data->length); int offset = le24toh(data->offset); - int datasize = 0; + int datasize; int pos = 0; int recvlen = 0; int res = PM3_SUCCESS; @@ -1491,8 +1491,10 @@ static int handler_desfire_writedata(mfdes_data_t *data, MFDES_FILE_TYPE_T type) if (type == MFDES_RECORD_FILE) apdu.INS = MFDES_WRITE_RECORD; while (datatowrite > 0) { - if (datatowrite > 52) datasize = 52; - else datasize = datatowrite; + if (datatowrite > 52) + datasize = 52; + else + datasize = datatowrite; tmp[1] = offset & 0xFF; tmp[2] = (offset >> 8) & 0xFF; @@ -1690,7 +1692,7 @@ int getKeySettings(uint8_t *aid) { } // Authentication tests - int res = test_desfire_authenticate(); + res = test_desfire_authenticate(); if (res == PM3_ETIMEOUT) return res; PrintAndLogEx(SUCCESS, " [0x0A] Authenticate : %s", (res == PM3_SUCCESS) ? _YELLOW_("YES") : "NO"); @@ -1838,29 +1840,29 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { swap16(fid); if (aidlength != 3) { - PrintAndLogEx(ERR, "AID must have 3 bytes length."); + PrintAndLogEx(ERR, "AID must have 3 bytes length"); return PM3_EINVARG; } if (fidlength != 2) { - PrintAndLogEx(ERR, "FID must have 2 bytes length."); - return PM3_EINVARG; - } - bool usefid = true; - if (fidlength == 0) usefid = false; - - if (keylen1 != 1) { - PrintAndLogEx(ERR, "Keysetting1 must have 1 byte length."); + PrintAndLogEx(ERR, "FID must have 2 bytes length"); return PM3_EINVARG; } + bool usefid = (fidlength != 0); + if (keylen1 != 1) { - PrintAndLogEx(ERR, "Keysetting2 must have 1 byte length."); + PrintAndLogEx(ERR, "Keysetting1 must have 1 byte length"); + return PM3_EINVARG; + } + + if (keylen2 != 1) { + PrintAndLogEx(ERR, "Keysetting2 must have 1 byte length"); return PM3_EINVARG; } if (namelen > 16) { - PrintAndLogEx(ERR, "Name has a max. of 16 bytes length."); + PrintAndLogEx(ERR, "Name has a max. of 16 bytes length"); return PM3_EINVARG; } bool usename = true; @@ -1874,7 +1876,7 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { uint8_t keysetting2=0xEE;*/ if (memcmp(aid, "\x00\x00\x00", 3) == 0) { - PrintAndLogEx(WARNING, _RED_(" Creating root aid 000000 is forbidden.")); + PrintAndLogEx(WARNING, _RED_(" Creating root aid 000000 is forbidden")); return PM3_ESOFT; } @@ -1882,12 +1884,17 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { memcpy(aidhdr.aid, aid, sizeof(aid)); aidhdr.keysetting1 = keysetting1; aidhdr.keysetting2 = keysetting2; + if (usefid) memcpy(aidhdr.fid, fid, sizeof(fid)); + if (usename) memcpy(aidhdr.name, name, sizeof(name)); uint8_t rootaid[3] = {0x00, 0x00, 0x00}; int res = handler_desfire_select_application(rootaid); - if (res != PM3_SUCCESS) { DropField(); return res; } + if (res != PM3_SUCCESS) { + DropField(); + return res; + } res = handler_desfire_createapp(&aidhdr, usename, usefid); DropField(); @@ -2392,6 +2399,13 @@ static int CmdHF14ADesChangeValue(const char *Cmd) { static int CmdHF14ADesWriteData(const char *Cmd) { + + uint8_t *data = (uint8_t *)calloc(0xFFFF, sizeof(uint8_t)); + if (data == NULL) { + PrintAndLogEx(ERR, "failed to allocate memory"); + return PM3_EMALLOC; + } + CLIParserInit("hf mfdes writedata", "Write data to File", "Usage:" @@ -2403,56 +2417,61 @@ static int CmdHF14ADesWriteData(const char *Cmd) { arg_strx0("aA", "aid", "", "AID for file (3 hex bytes, big endian)"), arg_strx0("nN", "fileno", "", "File Number (1 hex byte, 0x00 - 0x1F)"), arg_strx0("oO", "offset", "", "File Offset (3 hex bytes, big endian), optional"), - arg_strx0("dD", "data", "", "Data to write (hex bytes, 0xFFFFFF bytes max.)"), + arg_strx0("dD", "data", "", "Data to write (hex bytes, 0xFFFF bytes max.)"), arg_int0("type", "type", "", "File Type (0=Standard/Backup, 1=Record)"), arg_param_end }; + CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; + int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + + uint8_t fileno; int filenolen = 0; CLIGetHexWithReturn(2, &fileno, &filenolen); + int offsetlength = 0; uint8_t offset[3] = {0}; CLIParamHexToBuf(arg_get_str(3), offset, 3, &offsetlength); - int dlength = 0xFFFFFF; - uint8_t *data = (uint8_t *)malloc(0xFFFFFF); - memset(data, 0x0, 0xFFFFFF); - CLIParamHexToBuf(arg_get_str(4), data, 0xFFFFFF, &dlength); + + int dlength = 0xFFFF; + CLIParamHexToBuf(arg_get_str(4), data, 0xFFFF, &dlength); + int type = arg_get_int(5); + CLIParserFree(); swap24(aid); swap24(offset); - if (type > 1) { - PrintAndLogEx(ERR, "Unknown type (0=Standard/Backup, 1=Record)."); + if (type < 0 || type > 1) { + PrintAndLogEx(ERR, "Unknown type (0=Standard/Backup, 1=Record)"); if (data) free(data); return PM3_EINVARG; } if (dlength == 0) { - PrintAndLogEx(ERR, "Data needs some hex bytes to write."); + PrintAndLogEx(ERR, "Data needs some hex bytes to write"); if (data) free(data); return PM3_EINVARG; } if (offsetlength != 3 && offsetlength != 0) { - PrintAndLogEx(ERR, "Offset needs 3 hex bytes."); + PrintAndLogEx(ERR, "Offset needs 3 hex bytes"); if (data) free(data); return PM3_EINVARG; } if (filenolen != 1) { - PrintAndLogEx(ERR, "File number is missing."); + PrintAndLogEx(ERR, "File number is missing"); if (data) free(data); return PM3_EINVARG; } if (fileno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); + PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); if (data) free(data); return PM3_EINVARG; } @@ -2466,21 +2485,23 @@ static int CmdHF14ADesWriteData(const char *Cmd) { int res = handler_desfire_select_application(aid); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Couldn't select aid."); + PrintAndLogEx(ERR, "Couldn't select aid"); DropField(); if (data) free(data); return res; } mfdes_data_t ft; + memcpy(ft.offset, offset, 3); htole24(dlength, ft.length); ft.fileno = fileno; + if (data != NULL) { ft.data = data; res = handler_desfire_writedata(&ft, type); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "Successfully wrote data."); + PrintAndLogEx(SUCCESS, "Successfully wrote data"); } else { PrintAndLogEx(ERR, "Couldn't read data. Error %d", res); } @@ -2751,7 +2772,7 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { CLIParserFree(); if ((keylen < 8) || (keylen > 8)) { - PrintAndLogEx(ERR, "Specified key must have 8 bytes length."); + PrintAndLogEx(ERR, "Specified key must have 8 bytes length"); return PM3_EINVARG; } @@ -2766,6 +2787,7 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { payload.mode = MFDES_AUTH_PICC; payload.algo = MFDES_ALGO_DES; payload.keyno = 0; + SendCommandNG(CMD_HF_DESFIRE_AUTH1, (uint8_t *)&payload, sizeof(payload)); PacketResponseNG resp; @@ -2781,11 +2803,11 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { uint8_t flags; uint8_t datalen; uint8_t datain[FRAME_PAYLOAD_SIZE]; - } PACKED payload; - payload.datain[0] = 0xFC; - payload.flags = NONE; - payload.datalen = 1; - SendCommandNG(CMD_HF_DESFIRE_COMMAND, (uint8_t *)&payload, sizeof(payload)); + } PACKED payload_raw; + payload_raw.datain[0] = 0xFC; + payload_raw.flags = NONE; + payload_raw.datalen = 1; + SendCommandNG(CMD_HF_DESFIRE_COMMAND, (uint8_t *)&payload_raw, sizeof(payload_raw)); if (!WaitForResponseTimeout(CMD_HF_DESFIRE_COMMAND, &resp, 3000)) { PrintAndLogEx(WARNING, "Client reset command execute timeout"); DropField(); @@ -2801,7 +2823,7 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { return PM3_SUCCESS; } } else { - PrintAndLogEx(WARNING, _RED_("Auth command failed.")); + PrintAndLogEx(WARNING, _RED_("Auth command failed")); } DropField(); return PM3_SUCCESS; @@ -3124,8 +3146,10 @@ static int CmdHF14ADesDump(const char *Cmd) { uint8_t filesettings[20] = {0}; int fileset_len = 0; - int res = handler_desfire_filesettings(file_ids[j], filesettings, &fileset_len); + + res = handler_desfire_filesettings(file_ids[j], filesettings, &fileset_len); int maclen = 0; // To be implemented + if (res == PM3_SUCCESS) { //if (DecodeFileSettings(filesettings, fileset_len, maclen) != PM3_SUCCESS) { if (fileset_len == 1 + 1 + 2 + 3 + maclen) { @@ -3143,8 +3167,8 @@ static int CmdHF14ADesDump(const char *Cmd) { PrintAndLogEx(NORMAL, "\nOffset | Data | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); int len = le24toh(fdata.length); - for (int i = 0; i < len; i += 16) { - PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", i, i, sprint_hex(&fdata.data[i], len > 16 ? 16 : len), sprint_ascii(&fdata.data[i], len > 16 ? 16 : len)); + for (int n = 0; n < len; n += 16) { + PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", n, n, sprint_hex(&fdata.data[n], len > 16 ? 16 : len), sprint_ascii(&fdata.data[n], len > 16 ? 16 : len)); } free(data); } else { @@ -3161,8 +3185,8 @@ static int CmdHF14ADesDump(const char *Cmd) { if (res == PM3_SUCCESS) { PrintAndLogEx(NORMAL, "\nOffset | Value | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); - for (int i = 0; i < len; i += 16) { - PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", i, i, sprint_hex(&value.value[i], len > 16 ? 16 : len), sprint_ascii(&value.value[i], len > 16 ? 16 : len)); + for (int n = 0; n < len; n += 16) { + PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", n, n, sprint_hex(&value.value[n], len > 16 ? 16 : len), sprint_ascii(&value.value[n], len > 16 ? 16 : len)); } } else { PrintAndLogEx(ERR, "Couldn't read value. Error %d", res); @@ -3189,8 +3213,8 @@ static int CmdHF14ADesDump(const char *Cmd) { PrintAndLogEx(NORMAL, "\nOffset | Data | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); int len = le24toh(fdata.length); - for (int i = 0; i < len; i += 16) { - PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", i, i, sprint_hex(&fdata.data[i], len > 16 ? 16 : len), sprint_ascii(&fdata.data[i], len > 16 ? 16 : len)); + for (int n = 0; n < len; n += 16) { + PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", n, n, sprint_hex(&fdata.data[n], len > 16 ? 16 : len), sprint_ascii(&fdata.data[n], len > 16 ? 16 : len)); } } else { res = handler_desfire_select_application(aid); diff --git a/client/src/cmdhfmfu.c b/client/src/cmdhfmfu.c index 9bdd9b814..f70c3887e 100644 --- a/client/src/cmdhfmfu.c +++ b/client/src/cmdhfmfu.c @@ -2705,7 +2705,8 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { return usage_hf_mfu_otp_tearoff(); case 'b': blockNoUint = param_get8(Cmd, cmdp + 1); - if (blockNoUint < 0) { + //iceman, which blocks can be targeted? UID blocks? + if (blockNoUint < 2) { PrintAndLogEx(WARNING, "Wrong block number"); errors = true; } @@ -2713,7 +2714,7 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { break; case 'i': interval = param_get32ex(Cmd, cmdp + 1, interval, 10); - if (interval <= 0) { + if (interval == 0) { PrintAndLogEx(WARNING, "Wrong interval number"); errors = true; } @@ -2758,13 +2759,14 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { if (errors) return usage_hf_mfu_otp_tearoff(); + PrintAndLogEx(INFO, "Starting TearOff test - Selected Block no: %u", blockNoUint); + + uint32_t actualTime = startTime; - printf("\nStarting TearOff test - Selected Block no: %d ...\n", blockNoUint); while (actualTime <= (timeLimit - interval)) { - printf("\nTrying attack at: %d us\n", actualTime); - printf("\n.....\n"); - printf("\nReading block before attack: \n"); + PrintAndLogEx(INFO, "Using tear-off at: %" PRIu32 " us", actualTime); + PrintAndLogEx(INFO, "Reading block BEFORE attack"); clearCommandBuffer(); SendCommandOLD(CMD_HF_MIFAREU_READBL, blockNoUint, 0, 0, NULL, 0); @@ -2780,7 +2782,7 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { } } - printf("\n.....\n"); + PrintAndLogEx(INFO, "....."); clearCommandBuffer(); SendCommandOLD(CMD_HF_MFU_OTP_TEAROFF, blockNoUint, actualTime, 0, teardata, 8); @@ -2789,8 +2791,7 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { return PM3_ESOFT; } - - printf("\nReading block after attack: \n"); + PrintAndLogEx(INFO, "Reading block AFTER attack"); clearCommandBuffer(); SendCommandOLD(CMD_HF_MIFAREU_READBL, blockNoUint, 0, 0, NULL, 0); diff --git a/client/src/proxguiqt.cpp b/client/src/proxguiqt.cpp index f2fb5986a..855b41c87 100644 --- a/client/src/proxguiqt.cpp +++ b/client/src/proxguiqt.cpp @@ -349,11 +349,11 @@ void Plot::setMaxAndStart(int *buffer, size_t len, QRect plotRect) { GraphStart = startMax; } if (GraphStart > len) return; - int vMin = INT_MAX, vMax = INT_MIN, v = 0; + int vMin = INT_MAX, vMax = INT_MIN; uint32_t sample_index = GraphStart ; for (; sample_index < len && xCoordOf(sample_index, plotRect) < plotRect.right() ; sample_index++) { - v = buffer[sample_index]; + int v = buffer[sample_index]; if (v < vMin) vMin = v; if (v > vMax) vMax = v; } @@ -381,7 +381,7 @@ void Plot::PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotati first_delta_x += BitStart * PlotGridX; if (BitStart > (int)len) return; int delta_x = 0; - int v = 0; +// int v = 0; //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart); painter->setPen(getColor(graphNum)); @@ -393,9 +393,9 @@ void Plot::PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotati delta_x = 0; int clk = first_delta_x; for (int i = BitStart; i < (int)len && xCoordOf(delta_x + DemodStart, plotRect) < plotRect.right(); i++) { - for (int ii = 0; ii < (clk) && i < (int)len && xCoordOf(DemodStart + delta_x + ii, plotRect) < plotRect.right() ; ii++) { - x = xCoordOf(DemodStart + delta_x + ii, plotRect); - v = buffer[i] * 200 - 100; + for (int j = 0; j < (clk) && i < (int)len && xCoordOf(DemodStart + delta_x + j, plotRect) < plotRect.right() ; j++) { + x = xCoordOf(DemodStart + delta_x + j, plotRect); + int v = buffer[i] * 200 - 100; y = yCoordOf(v, plotRect, absVMax); @@ -405,7 +405,7 @@ void Plot::PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotati QRect f(QPoint(x - 3, y - 3), QPoint(x + 3, y + 3)); painter->fillRect(f, QColor(100, 255, 100)); } - if (ii == (int)clk / 2) { + if (j == (int)clk / 2) { //print label sprintf(str, "%u", buffer[i]); painter->drawText(x - 8, y + ((buffer[i] > 0) ? 18 : -6), str); @@ -482,7 +482,7 @@ void Plot::PlotGraph(int *buffer, size_t len, QRect plotRect, QRect annotationRe //Graph annotations painter->drawPath(penPath); char str[200]; - sprintf(str, "max=%d min=%d mean=%d n=%d/%zu CursorAVal=[%d] CursorBVal=[%d]", + sprintf(str, "max=%d min=%d mean=%d n=%u/%zu CursorAVal=[%d] CursorBVal=[%d]", vMax, vMin, vMean, i, len, buffer[CursorAPos], buffer[CursorBPos]); painter->drawText(20, annotationRect.bottom() - 23 - 20 * graphNum, str); @@ -595,7 +595,7 @@ void Plot::paintEvent(QPaintEvent *event) { //Draw annotations char str[200]; - sprintf(str, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d", + sprintf(str, "@%u dt=%u [%2.2f] zoom=%2.2f CursorAPos=%u CursorBPos=%u GridX=%d GridY=%d (%s) GridXoffset=%d", GraphStart, CursorBPos - CursorAPos, ((int32_t)(CursorBPos - CursorAPos)) / CursorScaleFactor, diff --git a/client/src/proxguiqt.h b/client/src/proxguiqt.h index 70a9cc6c1..7f9a82126 100644 --- a/client/src/proxguiqt.h +++ b/client/src/proxguiqt.h @@ -35,8 +35,8 @@ class Plot: public QWidget { double GraphPixelsPerPoint; // How many visual pixels are between each sample point (x axis) uint32_t CursorAPos; uint32_t CursorBPos; - void PlotGraph(int *buffer, size_t len, QRect r, QRect r2, QPainter *painter, int graphNum); - void PlotDemod(uint8_t *buffer, size_t len, QRect r, QRect r2, QPainter *painter, int graphNum, uint32_t plotOffset); + void PlotGraph(int *buffer, size_t len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum); + void PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum, uint32_t plotOffset); void plotGridLines(QPainter *painter, QRect r); int xCoordOf(int i, QRect r); int yCoordOf(int v, QRect r, int maxVal); diff --git a/tools/hitag2crack/crack5/ht2crack5.c b/tools/hitag2crack/crack5/ht2crack5.c index 42afbde88..33145d99a 100644 --- a/tools/hitag2crack/crack5/ht2crack5.c +++ b/tools/hitag2crack/crack5/ht2crack5.c @@ -162,7 +162,7 @@ int main(int argc, char *argv[]) { for (size_t i0 = 0; i0 < 1 << 20; i0++) { uint64_t state0 = expand(0x5806b4a2d16c, i0); - if (f(state0) == target >> 31) { + if (f(state0) == (target >> 31)) { candidates[layer_0_found++] = state0; } } @@ -185,7 +185,7 @@ void *find_state(void *thread_d) { for (size_t index = thread; index < layer_0_found; index += thread_count) { if (((index / thread_count) & 0xFF) == 0) - printf("Thread %lu slice %lu/%lu\n", thread, index / thread_count / 256 + 1, layer_0_found / thread_count / 256); + printf("Thread %zu slice %zu/%zu\n", thread, index / thread_count / 256 + 1, layer_0_found / thread_count / 256); uint64_t state0 = candidates[index]; bitslice(state0 >> 2, &state[0], 46, false); for (size_t bit = 0; bit < 8; bit++) { diff --git a/tools/hitag2crack/crack5gpu/ht2crack5.c b/tools/hitag2crack/crack5gpu/ht2crack5.c index 37df9df65..8e6c29c76 100644 --- a/tools/hitag2crack/crack5gpu/ht2crack5.c +++ b/tools/hitag2crack/crack5gpu/ht2crack5.c @@ -231,7 +231,7 @@ int main(int argc, char *argv[]) { for (size_t i0 = 0; i0 < 1 << 20; i0++) { uint64_t state0 = expand(0x5806b4a2d16c, i0); - if (f(state0) == target >> 31) { + if (f(state0) == (target >> 31)) { // cf kernel, state is now split in 3 shorts >> 2 candidates[(layer_0_found * 3) + 0] = (uint16_t)((state0 >> (32 + 2)) & 0xffff); candidates[(layer_0_found * 3) + 1] = (uint16_t)((state0 >> (16 + 2)) & 0xffff); From 2e2bfc17a04e654e4b32329eb64ebb3918c6c163 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 13:54:55 +0200 Subject: [PATCH 19/47] coverity fix: 286678 --- client/src/uart/uart_posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/uart/uart_posix.c b/client/src/uart/uart_posix.c index 0e05f8389..4ec3fd507 100644 --- a/client/src/uart/uart_posix.c +++ b/client/src/uart/uart_posix.c @@ -194,12 +194,12 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { } if (connect(localsocket, (struct sockaddr *) &remote, len) == -1) { + free(localsocket); free(sp); return INVALID_SERIAL_PORT; } sp->fd = localsocket; - return sp; } From 5e51a72caef105e69b0aa716be93ff7a1ccef70c Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 14:13:12 +0200 Subject: [PATCH 20/47] coverity fix 286678 - resource leak --- client/src/uart/uart_posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/uart/uart_posix.c b/client/src/uart/uart_posix.c index 4ec3fd507..a2b324b49 100644 --- a/client/src/uart/uart_posix.c +++ b/client/src/uart/uart_posix.c @@ -194,7 +194,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { } if (connect(localsocket, (struct sockaddr *) &remote, len) == -1) { - free(localsocket); + close(localsocket); free(sp); return INVALID_SERIAL_PORT; } From 00cac87fb2b3fd411fa4543f01850db3021199e5 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 14:15:59 +0200 Subject: [PATCH 21/47] coverity fix - resource leaks --- client/src/preferences.c | 71 ++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/client/src/preferences.c b/client/src/preferences.c index 2f3fe8156..1e0b59297 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -39,10 +39,10 @@ static int setCmdHelp(const char *Cmd); #endif static char *prefGetFilename(void) { - char *Path; + char *path; - if (searchHomeFilePath(&Path, preferencesFilename, false) == PM3_SUCCESS) - return Path; + if (searchHomeFilePath(&path, preferencesFilename, false) == PM3_SUCCESS) + return path; else return preferencesFilename; } @@ -95,12 +95,14 @@ int preferences_load(void) { size_t dummyDL = 0x00; // to better control json cant find file error msg. - if (fileExists(prefGetFilename())) { + char* fn = prefGetFilename(); + if (fileExists(fn)) { PrintAndLogEx(INFO, "Loading Preferences..."); - if (loadFileJSON(prefGetFilename(), &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { + if (loadFileJSON(fn, &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { session.preferences_loaded = true; } } + free(fn); // Note, if session.settings_loaded == false then the settings_save // will be called in main () to save settings as set in defaults and main() checks. @@ -111,25 +113,33 @@ int preferences_load(void) { int preferences_save(void) { // Note sure if backup has value ? - char *backupFilename = NULL;// [FILENAME_MAX+sizeof(preferencesFilename)+10] = {0}; - int fnLen = 0; - PrintAndLogEx(INFO, "Saving Preferences..."); - fnLen = strlen(prefGetFilename()) + 5; // .bak\0 - backupFilename = (char *)calloc(fnLen, sizeof(uint8_t)); - snprintf(backupFilename, fnLen, "%s.bak", prefGetFilename()); + char* fn = prefGetFilename(); + int fnLen = strlen(fn) + 5; // .bak\0 + + // [FILENAME_MAX+sizeof(preferencesFilename)+10] + char* backupFilename = (char *)calloc(fnLen, sizeof(uint8_t)); + if (backupFilename == NULL) { + PrintAndLogEx(ERR, "failed to allocate memory"); + return PM3_EMALLOC; + } + snprintf(backupFilename, fnLen, "%s.bak", fn); if (fileExists(backupFilename)) { if (remove(backupFilename) != 0) { PrintAndLogEx(FAILED, "Error - could not delete old settings backup file \"%s\"", backupFilename); + free(fn); + free(backupFilename); return PM3_ESOFT; } } - if (fileExists(prefGetFilename())) { - if (rename(prefGetFilename(), backupFilename) != 0) { + if (fileExists(fn)) { + if (rename(fn, backupFilename) != 0) { PrintAndLogEx(FAILED, "Error - could not backup settings file \"%s\" to \"%s\"", prefGetFilename(), backupFilename); + free(fn); + free(backupFilename); return PM3_ESOFT; } } @@ -137,12 +147,11 @@ int preferences_save(void) { uint8_t dummyData = 0x00; size_t dummyDL = 0x00; - if (saveFileJSON(prefGetFilename(), jsfSettings, &dummyData, dummyDL) != PM3_SUCCESS) - PrintAndLogEx(ERR, "Error saving preferences to \"%s\"", prefGetFilename()); - - if (backupFilename != NULL) - free(backupFilename); + if (saveFileJSON(fn, jsfSettings, &dummyData, dummyDL) != PM3_SUCCESS) + PrintAndLogEx(ERR, "Error saving preferences to \"%s\"", fn); + free(fn); + free(backupFilename); return PM3_SUCCESS; } @@ -312,7 +321,6 @@ static int usage_set_emoji() { PrintAndLogEx(NORMAL, " "_GREEN_("emoji")" - Show amoji"); PrintAndLogEx(NORMAL, " "_GREEN_("alttext")" - Show alt text for emoji"); PrintAndLogEx(NORMAL, " "_GREEN_("erase")" - Dont show emoji or text"); - return PM3_SUCCESS; } @@ -322,7 +330,6 @@ static int usage_set_color() { PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); PrintAndLogEx(NORMAL, " "_GREEN_("off")" - Dont use colors"); PrintAndLogEx(NORMAL, " "_GREEN_("ansi")" - Use ANSI colors"); - return PM3_SUCCESS; } @@ -333,7 +340,6 @@ static int usage_set_debug() { PrintAndLogEx(NORMAL, " "_GREEN_("off")" - no debug messages"); PrintAndLogEx(NORMAL, " "_GREEN_("simple")" - simple debug messages"); PrintAndLogEx(NORMAL, " "_GREEN_("full")" - full debug messages"); - return PM3_SUCCESS; } /* @@ -356,7 +362,6 @@ static int usage_set_hints() { PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); PrintAndLogEx(NORMAL, " "_GREEN_("off")" - Dont display hints"); PrintAndLogEx(NORMAL, " "_GREEN_("on")" - Display hints"); - return PM3_SUCCESS; } /* @@ -847,33 +852,25 @@ static int setCmdSavePaths (const char *Cmd) { */ int getCmdHelp(const char *Cmd) { - return PM3_SUCCESS; } int getCmdEmoji(const char *Cmd) { - showEmojiState(prefShowNone); - return PM3_SUCCESS; } int getCmdHint(const char *Cmd) { - showHintsState(prefShowNone); - return PM3_SUCCESS; } int getCmdColor(const char *Cmd) { - showColorState(prefShowNone); - return PM3_SUCCESS; } int getCmdDebug(const char *Cmd) { - showClientDebugState(prefShowNone); return PM3_SUCCESS; } @@ -903,29 +900,29 @@ static command_t setCommandTable[] = { static int setCmdHelp(const char *Cmd) { (void)Cmd; // Cmd is not used so far CmdsHelp(setCommandTable); - return PM3_SUCCESS; } int CmdPrefGet(const char *Cmd) { clearCommandBuffer(); - return CmdsParse(getCommandTable, Cmd); } int CmdPrefSet(const char *Cmd) { clearCommandBuffer(); - return CmdsParse(setCommandTable, Cmd); } static int CmdPrefShow(const char *Cmd) { + char* fn = prefGetFilename(); PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, _CYAN_("Preferences loaded from %s"), prefGetFilename()); + PrintAndLogEx(NORMAL, _CYAN_("Preferences loaded from %s"), fn); + + free(fn); if (!session.preferences_loaded) { - PrintAndLogEx(ERR, "Preferneces not loaded"); + PrintAndLogEx(ERR, "Preferences not loaded"); return PM3_ESOFT; } @@ -942,13 +939,11 @@ static int CmdPrefShow(const char *Cmd) { showClientDebugState(prefShowNone); // showDeviceDebugState(prefShowNone); PrintAndLogEx(NORMAL, ""); - return PM3_SUCCESS; } /* static int CmdPrefSave (const char *Cmd) { preferences_save(); - return PM3_SUCCESS; } */ @@ -963,12 +958,10 @@ static command_t CommandTable[] = { static int CmdHelp(const char *Cmd) { (void)Cmd; // Cmd is not used so far CmdsHelp(CommandTable); - return PM3_SUCCESS; } int CmdPreferences(const char *Cmd) { clearCommandBuffer(); - return CmdsParse(CommandTable, Cmd); } From a3a7dfa5a11eea75cd758d0e972835889428131a Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 14:27:44 +0200 Subject: [PATCH 22/47] coverity / cppchecker fixes : dont like that code style --- client/src/cmdhfmfdes.c | 140 ++++++++++++++++++++++++++-------------- 1 file changed, 91 insertions(+), 49 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index ea466c0dc..5623bf1f3 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -1952,12 +1952,14 @@ static int CmdHF14ADesClearRecordFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int fidlength = 0; uint8_t fid[2] = {0}; CLIParamHexToBuf(arg_get_str(3), fid, 2, &fidlength); @@ -1968,7 +1970,7 @@ static int CmdHF14ADesClearRecordFile(const char *Cmd) { return PM3_EINVARG; } - if (fileno > 0x1F) { + if (fileno[0] > 0x1F) { PrintAndLogEx(ERR, "Fileno must be lower 0x1F."); return PM3_EINVARG; } @@ -1986,10 +1988,12 @@ static int CmdHF14ADesClearRecordFile(const char *Cmd) { return res; } - res = handler_desfire_clearrecordfile(fileno); + res = handler_desfire_clearrecordfile(fileno[0]); if (res == PM3_SUCCESS) { PrintAndLogEx(SUCCESS, "Successfully cleared record file."); - } else PrintAndLogEx(ERR, "Error on deleting file : %d", res); + } else { + PrintAndLogEx(ERR, "Error on deleting file : %d", res); + } DropField(); return res; } @@ -2008,12 +2012,14 @@ static int CmdHF14ADesDeleteFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int fidlength = 0; uint8_t fid[2] = {0}; CLIParamHexToBuf(arg_get_str(3), fid, 2, &fidlength); @@ -2024,7 +2030,7 @@ static int CmdHF14ADesDeleteFile(const char *Cmd) { return PM3_EINVARG; } - if (fileno > 0x1F) { + if (fileno[0] > 0x1F) { PrintAndLogEx(ERR, "Fileno must be lower 0x1F."); return PM3_EINVARG; } @@ -2042,10 +2048,12 @@ static int CmdHF14ADesDeleteFile(const char *Cmd) { return res; } - res = handler_desfire_deletefile(fileno); + res = handler_desfire_deletefile(fileno[0]); if (res == PM3_SUCCESS) { PrintAndLogEx(SUCCESS, "Successfully deleted file.."); - } else PrintAndLogEx(ERR, "Error on deleting file : %d", res); + } else { + PrintAndLogEx(ERR, "Error on deleting file : %d", res); + } DropField(); return res; } @@ -2069,22 +2077,27 @@ static int CmdHF14ADesCreateFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int fidlength = 0; uint8_t fid[2] = {0}; CLIParamHexToBuf(arg_get_str(3), fid, 2, &fidlength); + uint8_t comset = arg_get_int(4); int arlength = 0; uint8_t ar[2] = {0}; CLIGetHexWithReturn(5, ar, &arlength); + int fsizelen = 0; uint8_t filesize[3] = {0}; CLIGetHexWithReturn(6, filesize, &fsizelen); + bool isbackup = arg_get_lit(7); CLIParserFree(); @@ -2097,7 +2110,7 @@ static int CmdHF14ADesCreateFile(const char *Cmd) { return PM3_EINVARG; } - if (fileno > 0x1F) { + if (fileno[0] > 0x1F) { PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); return PM3_EINVARG; } @@ -2137,7 +2150,7 @@ static int CmdHF14ADesCreateFile(const char *Cmd) { mfdes_file_t ft; memcpy(ft.fid, fid, 2); memcpy(ft.filesize, filesize, 3); - ft.fileno = fileno; + ft.fileno = fileno[0]; ft.comset = comset; memcpy(ft.access_rights, ar, 2); @@ -2167,21 +2180,22 @@ static int CmdHF14ADesGetValueData(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); CLIParserFree(); if (filenolen != 1) { - PrintAndLogEx(ERR, "File number is missing."); + PrintAndLogEx(ERR, "File number is missing"); return PM3_EINVARG; } - if (fileno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); + if (fileno[0] > 0x1F) { + PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); return PM3_EINVARG; } @@ -2200,7 +2214,8 @@ static int CmdHF14ADesGetValueData(const char *Cmd) { return res; } mfdes_value_t value; - value.fileno = fileno; + value.fileno = fileno[0]; + int len = 0; res = handler_desfire_getvalue(&value, &len); if (res == PM3_SUCCESS) { @@ -2235,38 +2250,42 @@ static int CmdHF14ADesReadData(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int offsetlength = 0; uint8_t offset[3] = {0}; CLIParamHexToBuf(arg_get_str(3), offset, 3, &offsetlength); + int flength = 0; uint8_t filesize[3] = {0}; CLIParamHexToBuf(arg_get_str(4), filesize, 3, &flength); + int type = arg_get_int(5); CLIParserFree(); if (type > 1) { - PrintAndLogEx(ERR, "Invalid file type (0=Standard/Backup, 1=Record)."); + PrintAndLogEx(ERR, "Invalid file type (0=Standard/Backup, 1=Record)"); return PM3_EINVARG; } if (offsetlength != 3 && offsetlength != 0) { - PrintAndLogEx(ERR, "Offset needs 3 hex bytes."); + PrintAndLogEx(ERR, "Offset needs 3 hex bytes"); return PM3_EINVARG; } if (filenolen != 1) { - PrintAndLogEx(ERR, "File number is missing."); + PrintAndLogEx(ERR, "File number is missing"); return PM3_EINVARG; } - if (fileno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); + if (fileno[0] > 0x1F) { + PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); return PM3_EINVARG; } @@ -2290,15 +2309,18 @@ static int CmdHF14ADesReadData(const char *Cmd) { mfdes_data_t ft; memcpy(ft.offset, offset, 3); memcpy(ft.length, filesize, 3); - ft.fileno = fileno; + ft.fileno = fileno[0]; + int bytestoread = le24toh(filesize); + if (bytestoread == 0) bytestoread = 0xFFFFFF; - uint8_t *data = (uint8_t *)malloc(bytestoread); + + uint8_t *data = (uint8_t *)calloc(bytestoread, sizeof(uint8_t)); if (data != NULL) { ft.data = data; res = handler_desfire_readdata(&ft, type); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "Successfully read data from File %d:", ft.fileno); + PrintAndLogEx(SUCCESS, "Successfully read data from file %d:", ft.fileno); PrintAndLogEx(NORMAL, "\nOffset | Data | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); int len = le24toh(ft.length); @@ -2329,15 +2351,20 @@ static int CmdHF14ADesChangeValue(const char *Cmd) { arg_int0("mM", "mode", "", "Mode (0=Credit, 1=LimitedCredit, 2=Debit)"), arg_param_end }; + mfdes_value_t value; CLIExecWithReturn(Cmd, argtable, false); + int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; CLIGetHexWithReturn(2, &value.fileno, &filenolen); + int vlength = 0x0; CLIParamHexToBuf(arg_get_str(3), value.value, 4, &vlength); + int mode = arg_get_int(4); CLIParserFree(); swap24(aid); @@ -2428,9 +2455,9 @@ static int CmdHF14ADesWriteData(const char *Cmd) { uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); - uint8_t fileno; int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); int offsetlength = 0; uint8_t offset[3] = {0}; @@ -2470,7 +2497,7 @@ static int CmdHF14ADesWriteData(const char *Cmd) { return PM3_EINVARG; } - if (fileno > 0x1F) { + if (fileno[0] > 0x1F) { PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); if (data) free(data); return PM3_EINVARG; @@ -2495,7 +2522,7 @@ static int CmdHF14ADesWriteData(const char *Cmd) { memcpy(ft.offset, offset, 3); htole24(dlength, ft.length); - ft.fileno = fileno; + ft.fileno = fileno[0]; if (data != NULL) { ft.data = data; @@ -2532,25 +2559,31 @@ static int CmdHF14ADesCreateRecordFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int fidlength = 0; uint8_t fid[2] = {0}; CLIParamHexToBuf(arg_get_str(3), fid, 2, &fidlength); + uint8_t comset = arg_get_int(4); int arlength = 0; uint8_t ar[2] = {0}; CLIGetHexWithReturn(5, ar, &arlength); + int rsizelen = 0; uint8_t recordsize[3] = {0}; CLIGetHexWithReturn(6, recordsize, &rsizelen); + int msizelen = 0; uint8_t maxnumrecords[3] = {0}; CLIGetHexWithReturn(7, maxnumrecords, &msizelen); + bool cyclic = arg_get_lit(8); CLIParserFree(); @@ -2574,7 +2607,7 @@ static int CmdHF14ADesCreateRecordFile(const char *Cmd) { return PM3_EINVARG; } - if (fileno > 0x1F) { + if (fileno[0] > 0x1F) { PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); return PM3_EINVARG; } @@ -2613,7 +2646,7 @@ static int CmdHF14ADesCreateRecordFile(const char *Cmd) { } mfdes_linear_t ft; - ft.fileno = fileno; + ft.fileno = fileno[0]; memcpy(ft.fid, fid, 2); ft.comset = comset; memcpy(ft.access_rights, ar, 2); @@ -2651,25 +2684,31 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + uint8_t comset = arg_get_int(3); int arlength = 0; uint8_t ar[2] = {0}; CLIGetHexWithReturn(4, ar, &arlength); + int lllen = 0; uint8_t lowerlimit[4] = {0}; CLIGetHexWithReturn(5, lowerlimit, &lllen); + int ullen = 0; uint8_t upperlimit[4] = {0}; CLIGetHexWithReturn(6, upperlimit, &ullen); + int vllen = 0; uint8_t value[4] = {0}; CLIGetHexWithReturn(7, value, &vllen); + int limitedlen = 0; uint8_t limited = 0; CLIGetHexWithReturn(8, &limited, &limitedlen); @@ -2682,37 +2721,37 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { swap32(value); if (filenolen != 1) { - PrintAndLogEx(ERR, "File number is missing."); + PrintAndLogEx(ERR, "File number is missing"); return PM3_EINVARG; } - if (fileno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); + if (fileno[0] > 0x1F) { + PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); return PM3_EINVARG; } if (comset != 0 && comset != 1 && comset != 3) { - PrintAndLogEx(ERR, "Communication setting must be either 0=Plain, 1=Plain+MAC or 3=Encrypt."); + PrintAndLogEx(ERR, "Communication setting must be either 0=Plain, 1=Plain+MAC or 3=Encrypt"); return PM3_EINVARG; } if (arlength != 2) { - PrintAndLogEx(ERR, "Access rights must have 2 hex bytes length."); + PrintAndLogEx(ERR, "Access rights must have 2 hex bytes length"); return PM3_EINVARG; } if (lllen != 4) { - PrintAndLogEx(ERR, "Lower limit must have 4 hex bytes length."); + PrintAndLogEx(ERR, "Lower limit must have 4 hex bytes length"); return PM3_EINVARG; } if (ullen != 4) { - PrintAndLogEx(ERR, "Upper limit must have 4 hex bytes length."); + PrintAndLogEx(ERR, "Upper limit must have 4 hex bytes length"); return PM3_EINVARG; } if (vllen != 4) { - PrintAndLogEx(ERR, "Value must have 4 hex bytes length."); + PrintAndLogEx(ERR, "Value must have 4 hex bytes length"); return PM3_EINVARG; } @@ -2734,7 +2773,7 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { } mfdes_value_file_t ft; - ft.fileno = fileno; + ft.fileno = fileno[0]; ft.comset = comset; memcpy(ft.access_rights, ar, 2); memcpy(ft.lowerlimit, lowerlimit, 4); @@ -2804,10 +2843,13 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { uint8_t datalen; uint8_t datain[FRAME_PAYLOAD_SIZE]; } PACKED payload_raw; + payload_raw.datain[0] = 0xFC; payload_raw.flags = NONE; payload_raw.datalen = 1; + SendCommandNG(CMD_HF_DESFIRE_COMMAND, (uint8_t *)&payload_raw, sizeof(payload_raw)); + if (!WaitForResponseTimeout(CMD_HF_DESFIRE_COMMAND, &resp, 3000)) { PrintAndLogEx(WARNING, "Client reset command execute timeout"); DropField(); From 0665b3f40adf908f544f6c02f29455f953556fec Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 26 Apr 2020 17:36:15 +0200 Subject: [PATCH 23/47] Makefile: move mingw defines to Makefile.host --- Makefile.host | 6 ++++++ tools/mfkey/Makefile | 7 ------- tools/nonce2key/Makefile | 7 ------- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/Makefile.host b/Makefile.host index 9f5688939..9ef2685d2 100644 --- a/Makefile.host +++ b/Makefile.host @@ -17,6 +17,12 @@ endif CFLAGS ?= -Wall -Werror -O3 CFLAGS += $(MYDEFS) $(MYCFLAGS) $(MYINCLUDES) +ifneq (,$(findstring MINGW,$(platform))) + # Mingw uses by default Microsoft printf, we want the GNU printf (e.g. for %z) + # and setting _ISOC99_SOURCE sets internally __USE_MINGW_ANSI_STDIO=1 + CFLAGS += -D_ISOC99_SOURCE + PM3CFLAGS += -mno-ms-bitfields -fexec-charset=cp850 +endif vpath %.c $(MYSRCPATHS) diff --git a/tools/mfkey/Makefile b/tools/mfkey/Makefile index fd69207a7..df4e3cc54 100644 --- a/tools/mfkey/Makefile +++ b/tools/mfkey/Makefile @@ -9,13 +9,6 @@ INSTALLTOOLS = $(BINS) include ../../Makefile.host -# checking platform can be done only after Makefile.host -ifneq (,$(findstring MINGW,$(platform))) - # Mingw uses by default Microsoft printf, we want the GNU printf (e.g. for %z) - # and setting _ISOC99_SOURCE sets internally __USE_MINGW_ANSI_STDIO=1 - CFLAGS += -D_ISOC99_SOURCE -endif - mfkey32 : $(OBJDIR)/mfkey32.o $(MYOBJS) mfkey32v2 : $(OBJDIR)/mfkey32v2.o $(MYOBJS) mfkey64 : $(OBJDIR)/mfkey64.o $(MYOBJS) diff --git a/tools/nonce2key/Makefile b/tools/nonce2key/Makefile index a4b0bde05..8b59d996d 100644 --- a/tools/nonce2key/Makefile +++ b/tools/nonce2key/Makefile @@ -9,11 +9,4 @@ INSTALLTOOLS = $(BINS) include ../../Makefile.host -# checking platform can be done only after Makefile.host -ifneq (,$(findstring MINGW,$(platform))) - # Mingw uses by default Microsoft printf, we want the GNU printf (e.g. for %z) - # and setting _ISOC99_SOURCE sets internally __USE_MINGW_ANSI_STDIO=1 - CFLAGS += -D_ISOC99_SOURCE -endif - nonce2key : $(OBJDIR)/nonce2key.o $(MYOBJS) From 04a16ddeb5831d3063d040316c62b819dcdad809 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 26 Apr 2020 17:44:45 +0200 Subject: [PATCH 24/47] msdsal fix bug after var de-shadowing, move stuff around --- armsrc/Standalone/hf_msdsal.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/armsrc/Standalone/hf_msdsal.c b/armsrc/Standalone/hf_msdsal.c index 9e3175ea8..c79d01aed 100644 --- a/armsrc/Standalone/hf_msdsal.c +++ b/armsrc/Standalone/hf_msdsal.c @@ -371,19 +371,6 @@ void RunMod() { Dbhexdump(len, receivedCmd, false); if (receivedCmd[0] == 0x02 || receivedCmd[0] == 0x03) { //Emulate a Visa MSD(Magnetic stripe data) card - uint8_t processing_long[10] = {0x80, 0x06, 0x00, 0x80, 0x08, 0x01, 0x01, 0x00, 0x90, 0x00}; - uint8_t visauid_long[34] = {0x6F, 0x1E, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0xA5, 0x13, 0x50, 0x0B, 0x56, 0x49, 0x53, 0x41, 0x20, 0x43, 0x52, 0x45, 0x44, 0x49, 0x54, 0x9F, 0x38, 0x03, 0x9F, 0x66, 0x02, 0x90, 0x00}; - - uint8_t last[4] = {0x70, 0x15, 0x57, 0x13}; - uint8_t card[25]; - uint8_t statusapdu[2] = {0x90, 0x00}; - - uint8_t finished[2] = {0x6f, 0x00}; - - memcpy(&card[0], last, sizeof(last)); - memcpy(&card[4], token, sizeof(token)); - memcpy(&card[23], statusapdu, sizeof(statusapdu)); - dynamic_response_info.response[0] = receivedCmd[0]; //Depending on card reader commands, the Proxmark will answer to fool the reader @@ -393,18 +380,27 @@ void RunMod() { dynamic_response_info.response_n = sizeof(ppsea) + 1; prevCmd++; } else if (receivedCmd[2] == 0xA4 && receivedCmd[10] == 0x03 && receivedCmd[11] == 0x10 && prevCmd == 1) { //Respond Visa AID - memcpy(&dynamic_response_info.response[1], visauid_long, sizeof(visauid)); - dynamic_response_info.response_n = sizeof(visauid) + 1; + uint8_t visauid_long[34] = {0x6F, 0x1E, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0xA5, 0x13, 0x50, 0x0B, 0x56, 0x49, 0x53, 0x41, 0x20, 0x43, 0x52, 0x45, 0x44, 0x49, 0x54, 0x9F, 0x38, 0x03, 0x9F, 0x66, 0x02, 0x90, 0x00}; + memcpy(&dynamic_response_info.response[1], visauid_long, sizeof(visauid_long)); + dynamic_response_info.response_n = sizeof(visauid_long) + 1; prevCmd++; } else if (receivedCmd[1] == 0x80 && receivedCmd[2] == 0xA8 && receivedCmd[6] == 0x83 && prevCmd == 2) { //GET PROCESSING - memcpy(&dynamic_response_info.response[1], processing_long, sizeof(processing)); + uint8_t processing_long[10] = {0x80, 0x06, 0x00, 0x80, 0x08, 0x01, 0x01, 0x00, 0x90, 0x00}; + memcpy(&dynamic_response_info.response[1], processing_long, sizeof(processing_long)); dynamic_response_info.response_n = sizeof(processing_long) + 1; prevCmd++; } else if (receivedCmd[1] == 0x00 && receivedCmd[2] == 0xB2 && prevCmd == 3) { //SFI + uint8_t last[4] = {0x70, 0x15, 0x57, 0x13}; + uint8_t statusapdu[2] = {0x90, 0x00}; + uint8_t card[25]; + memcpy(&card[0], last, sizeof(last)); + memcpy(&card[4], token, sizeof(token)); + memcpy(&card[23], statusapdu, sizeof(statusapdu)); memcpy(&dynamic_response_info.response[1], card, sizeof(card)); dynamic_response_info.response_n = sizeof(card) + 1; prevCmd++; } else { + uint8_t finished[2] = {0x6f, 0x00}; memcpy(&dynamic_response_info.response[1], finished, sizeof(finished)); dynamic_response_info.response_n = sizeof(finished) + 1; if (prevCmd == 5) { From e36a8ca5fb47d16dfd06c65b3c23a1543880f76d Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 19:28:58 +0200 Subject: [PATCH 25/47] coverity fix 286920 --- client/src/preferences.c | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/preferences.c b/client/src/preferences.c index 1e0b59297..9d482a1f5 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -122,6 +122,7 @@ int preferences_save(void) { char* backupFilename = (char *)calloc(fnLen, sizeof(uint8_t)); if (backupFilename == NULL) { PrintAndLogEx(ERR, "failed to allocate memory"); + free(fn); return PM3_EMALLOC; } snprintf(backupFilename, fnLen, "%s.bak", fn); From cf9a5a4850f514995f985bdcb0cbd3bfcb4b8d50 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 19:29:22 +0200 Subject: [PATCH 26/47] coverity fix 286921, 286922, .. --- client/src/cmdhfmfdes.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 5623bf1f3..287b13523 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -2180,6 +2180,7 @@ static int CmdHF14ADesGetValueData(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); + int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); @@ -2219,7 +2220,7 @@ static int CmdHF14ADesGetValueData(const char *Cmd) { int len = 0; res = handler_desfire_getvalue(&value, &len); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "Successfully read value from File %d:", fileno); + PrintAndLogEx(SUCCESS, "Successfully read value from File %u:", fileno); PrintAndLogEx(NORMAL, "\nOffset | Data | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); for (int i = 0; i < len; i += 16) { @@ -2360,7 +2361,9 @@ static int CmdHF14ADesChangeValue(const char *Cmd) { CLIGetHexWithReturn(1, aid, &aidlength); int filenolen = 0; - CLIGetHexWithReturn(2, &value.fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + value.fileno = fileno[0]; int vlength = 0x0; CLIParamHexToBuf(arg_get_str(3), value.value, 4, &vlength); @@ -2427,12 +2430,6 @@ static int CmdHF14ADesChangeValue(const char *Cmd) { static int CmdHF14ADesWriteData(const char *Cmd) { - uint8_t *data = (uint8_t *)calloc(0xFFFF, sizeof(uint8_t)); - if (data == NULL) { - PrintAndLogEx(ERR, "failed to allocate memory"); - return PM3_EMALLOC; - } - CLIParserInit("hf mfdes writedata", "Write data to File", "Usage:" @@ -2464,6 +2461,11 @@ static int CmdHF14ADesWriteData(const char *Cmd) { CLIParamHexToBuf(arg_get_str(3), offset, 3, &offsetlength); int dlength = 0xFFFF; + uint8_t *data = (uint8_t *)calloc(0xFFFF, sizeof(uint8_t)); + if (data == NULL) { + PrintAndLogEx(ERR, "failed to allocate memory"); + return PM3_EMALLOC; + } CLIParamHexToBuf(arg_get_str(4), data, 0xFFFF, &dlength); int type = arg_get_int(5); From bc811f11131c20efde258e164739072f320e05af Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 19:49:54 +0200 Subject: [PATCH 27/47] chg: more lf tests --- pm3test.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pm3test.sh b/pm3test.sh index 202849f60..b1ae8a73b 100755 --- a/pm3test.sh +++ b/pm3test.sh @@ -108,9 +108,15 @@ while true; do printf "\n${C_BLUE}Testing data manipulation:${C_NC}\n" if ! CheckExecute "reveng test" "$PM3BIN -c 'reveng -w 8 -s 01020304e3 010204039d'" "CRC-8/SMBUS"; then break; fi if ! CheckExecute "mfu pwdgen test" "$PM3BIN -c 'hf mfu pwdgen t'" "Selftest OK"; then break; fi - + printf "\n${C_BLUE}Testing LF:${C_NC}\n" - if ! CheckExecute "lf em4x05 test" "$PM3BIN -c 'data load traces/em4x05.pm3;lf search'" "FDX-B ID found"; then break; fi + if ! CheckExecute "lf em4x05 test" "$PM3BIN -c 'data load traces/em4x05.pm3;lf search 1'" "FDX-B ID found"; then break; fi + if ! CheckExecute "lf em410x test" "$PM3BIN -c 'data load traces/EM4102-1.pm3;lf search 1'" "EM410x ID found"; then break; fi + if ! CheckExecute "lf visa2000 test" "$PM3BIN -c 'data load traces/visa2000.pm3;lf search 1'" "Visa2000 ID found"; then break; fi + if ! CheckExecute "lf awid test" "$PM3BIN -c 'data load traces/AWID-15-259.pm3;lf search 1'" "AWID ID found"; then break; fi + if ! CheckExecute "lf securakey test" "$PM3BIN -c 'data load traces/securakey-64169.pm3;lf search 1 '" "Securakey ID found"; then break; fi + if ! CheckExecute "lf keri test" "$PM3BIN -c 'data load traces/keri.pm3;lf search 1'" "Pyramid ID found"; then break; fi + if ! CheckExecute "lf HID LF Prox test" "$PM3BIN -c 'data load traces/hid-proxCardII-05512-11432784-1.pm3;lf search 1'" "HID Prox ID found"; then break; fi printf "\n${C_BLUE}Testing HF:${C_NC}\n" if ! CheckExecute "hf mf offline text" "$PM3BIN -c 'hf mf'" "at_enc"; then break; fi From 20335e175d771c77996ac726d93f0cfe340fe123 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 26 Apr 2020 19:59:45 +0200 Subject: [PATCH 28/47] more lf test --- pm3test.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pm3test.sh b/pm3test.sh index b1ae8a73b..be9331358 100755 --- a/pm3test.sh +++ b/pm3test.sh @@ -116,7 +116,9 @@ while true; do if ! CheckExecute "lf awid test" "$PM3BIN -c 'data load traces/AWID-15-259.pm3;lf search 1'" "AWID ID found"; then break; fi if ! CheckExecute "lf securakey test" "$PM3BIN -c 'data load traces/securakey-64169.pm3;lf search 1 '" "Securakey ID found"; then break; fi if ! CheckExecute "lf keri test" "$PM3BIN -c 'data load traces/keri.pm3;lf search 1'" "Pyramid ID found"; then break; fi - if ! CheckExecute "lf HID LF Prox test" "$PM3BIN -c 'data load traces/hid-proxCardII-05512-11432784-1.pm3;lf search 1'" "HID Prox ID found"; then break; fi + if ! CheckExecute "lf HID Prox test" "$PM3BIN -c 'data load traces/hid-proxCardII-05512-11432784-1.pm3;lf search 1'" "HID Prox ID found"; then break; fi + if ! CheckExecute "lf Paradox test" "$PM3BIN -c 'data load traces/Paradox-96_40426-APJN08.pm3;lf search 1'" "Paradox ID found"; then break; fi + if ! CheckExecute "lf IO Prox test" "$PM3BIN -c 'data load traces/ioprox-XSF-01-3B-44725.pm3;lf search 1'" "IO Prox ID found"; then break; fi printf "\n${C_BLUE}Testing HF:${C_NC}\n" if ! CheckExecute "hf mf offline text" "$PM3BIN -c 'hf mf'" "at_enc"; then break; fi From f7f33f27322ee8247179ce4ca19d0d2cdb853285 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 26 Apr 2020 22:30:41 +0200 Subject: [PATCH 29/47] Revert "Makefile: move mingw defines to Makefile.host" This reverts commit 0665b3f40adf908f544f6c02f29455f953556fec. --- Makefile.host | 6 ------ tools/mfkey/Makefile | 7 +++++++ tools/nonce2key/Makefile | 7 +++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Makefile.host b/Makefile.host index 9ef2685d2..9f5688939 100644 --- a/Makefile.host +++ b/Makefile.host @@ -17,12 +17,6 @@ endif CFLAGS ?= -Wall -Werror -O3 CFLAGS += $(MYDEFS) $(MYCFLAGS) $(MYINCLUDES) -ifneq (,$(findstring MINGW,$(platform))) - # Mingw uses by default Microsoft printf, we want the GNU printf (e.g. for %z) - # and setting _ISOC99_SOURCE sets internally __USE_MINGW_ANSI_STDIO=1 - CFLAGS += -D_ISOC99_SOURCE - PM3CFLAGS += -mno-ms-bitfields -fexec-charset=cp850 -endif vpath %.c $(MYSRCPATHS) diff --git a/tools/mfkey/Makefile b/tools/mfkey/Makefile index df4e3cc54..fd69207a7 100644 --- a/tools/mfkey/Makefile +++ b/tools/mfkey/Makefile @@ -9,6 +9,13 @@ INSTALLTOOLS = $(BINS) include ../../Makefile.host +# checking platform can be done only after Makefile.host +ifneq (,$(findstring MINGW,$(platform))) + # Mingw uses by default Microsoft printf, we want the GNU printf (e.g. for %z) + # and setting _ISOC99_SOURCE sets internally __USE_MINGW_ANSI_STDIO=1 + CFLAGS += -D_ISOC99_SOURCE +endif + mfkey32 : $(OBJDIR)/mfkey32.o $(MYOBJS) mfkey32v2 : $(OBJDIR)/mfkey32v2.o $(MYOBJS) mfkey64 : $(OBJDIR)/mfkey64.o $(MYOBJS) diff --git a/tools/nonce2key/Makefile b/tools/nonce2key/Makefile index 8b59d996d..a4b0bde05 100644 --- a/tools/nonce2key/Makefile +++ b/tools/nonce2key/Makefile @@ -9,4 +9,11 @@ INSTALLTOOLS = $(BINS) include ../../Makefile.host +# checking platform can be done only after Makefile.host +ifneq (,$(findstring MINGW,$(platform))) + # Mingw uses by default Microsoft printf, we want the GNU printf (e.g. for %z) + # and setting _ISOC99_SOURCE sets internally __USE_MINGW_ANSI_STDIO=1 + CFLAGS += -D_ISOC99_SOURCE +endif + nonce2key : $(OBJDIR)/nonce2key.o $(MYOBJS) From b01d1f0aa2dc831155b8c8edf24c9fc7720c3613 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 26 Apr 2020 22:36:02 +0200 Subject: [PATCH 30/47] Avoid printing size_t in ht2crack5 for Mingw --- tools/hitag2crack/crack5/ht2crack5.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/hitag2crack/crack5/ht2crack5.c b/tools/hitag2crack/crack5/ht2crack5.c index 33145d99a..ea1cc5ac8 100644 --- a/tools/hitag2crack/crack5/ht2crack5.c +++ b/tools/hitag2crack/crack5/ht2crack5.c @@ -181,11 +181,11 @@ int main(int argc, char *argv[]) { } void *find_state(void *thread_d) { - size_t thread = (size_t)thread_d; + uint64_t thread = (uint64_t)thread_d; - for (size_t index = thread; index < layer_0_found; index += thread_count) { + for (uint64_t index = thread; index < layer_0_found; index += thread_count) { if (((index / thread_count) & 0xFF) == 0) - printf("Thread %zu slice %zu/%zu\n", thread, index / thread_count / 256 + 1, layer_0_found / thread_count / 256); + printf("Thread %" PRIu64 " slice %" PRIu64 "/%" PRIu64 "\n", thread, index / thread_count / 256 + 1, layer_0_found / thread_count / 256); uint64_t state0 = candidates[index]; bitslice(state0 >> 2, &state[0], 46, false); for (size_t bit = 0; bit < 8; bit++) { From a7c286cc9af944261a26ff196da67a3a8201125a Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 26 Apr 2020 22:41:41 +0200 Subject: [PATCH 31/47] hitag2crack compile flags --- tools/hitag2crack/crack2/Makefile | 2 +- tools/hitag2crack/crack3/Makefile | 2 +- tools/hitag2crack/crack4/Makefile | 2 +- tools/hitag2crack/crack5/Makefile | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/hitag2crack/crack2/Makefile b/tools/hitag2crack/crack2/Makefile index 55c6a5bd9..4794d6ec4 100644 --- a/tools/hitag2crack/crack2/Makefile +++ b/tools/hitag2crack/crack2/Makefile @@ -1,4 +1,4 @@ -CFLAGS?=-Wall +CFLAGS?=-Wall -Werror -O3 # Linux libs LIBS=-pthread -D_GNU_SOURCE # Mac libs diff --git a/tools/hitag2crack/crack3/Makefile b/tools/hitag2crack/crack3/Makefile index 80f1ed863..318b5db78 100644 --- a/tools/hitag2crack/crack3/Makefile +++ b/tools/hitag2crack/crack3/Makefile @@ -1,4 +1,4 @@ -CFLAGS?=-Wall +CFLAGS?=-Wall -Werror -O3 LIBS= VPATH=../common INC=-I ../common diff --git a/tools/hitag2crack/crack4/Makefile b/tools/hitag2crack/crack4/Makefile index 418fb9930..b560577bd 100644 --- a/tools/hitag2crack/crack4/Makefile +++ b/tools/hitag2crack/crack4/Makefile @@ -1,4 +1,4 @@ -CFLAGS?=-Wall +CFLAGS?=-Wall -Werror -O3 LIBS=-lpthread VPATH=../common INC=-I ../common diff --git a/tools/hitag2crack/crack5/Makefile b/tools/hitag2crack/crack5/Makefile index 6ddc0150f..9d19b63ee 100644 --- a/tools/hitag2crack/crack5/Makefile +++ b/tools/hitag2crack/crack5/Makefile @@ -1,4 +1,4 @@ -CFLAGS?=-Wall +CFLAGS?=-Wall -Werror -O3 LIBS=-lpthread VPATH=../common INC=-I ../common From 2280442492547a2d8f5e370e60dfffa68da435a5 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 11:02:19 +0200 Subject: [PATCH 32/47] chg: adjust number of threads according to cpu --- tools/hitag2crack/crack5/ht2crack5.c | 80 ++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/tools/hitag2crack/crack5/ht2crack5.c b/tools/hitag2crack/crack5/ht2crack5.c index ea1cc5ac8..08feae194 100644 --- a/tools/hitag2crack/crack5/ht2crack5.c +++ b/tools/hitag2crack/crack5/ht2crack5.c @@ -104,16 +104,24 @@ void *find_state(void *thread_d); static void try_state(uint64_t s); int main(int argc, char *argv[]) { + + if (argc < 6) { + printf("%s UID {nR1} {aR1} {nR2} {aR2}\n", argv[0]); + exit(1); + } + // set constants memset(bs_ones.bytes, 0xff, VECTOR_SIZE); memset(bs_zeroes.bytes, 0x00, VECTOR_SIZE); uint32_t target = 0; - if (argc < 6) { - printf("%s UID {nR1} {aR1} {nR2} {aR2}\n", argv[0]); - exit(1); - } +#ifndef __WIN32 + thread_count = sysconf(_SC_NPROCESSORS_CONF); + if ( thread_count < 2) + thread_count = 2; +#endif /* _WIN32 */ + if (!strncmp(argv[1], "0x", 2) || !strncmp(argv[1], "0X", 2)) { uid = rev32(hexreversetoulong(argv[1] + 2)); @@ -184,13 +192,17 @@ void *find_state(void *thread_d) { uint64_t thread = (uint64_t)thread_d; for (uint64_t index = thread; index < layer_0_found; index += thread_count) { + if (((index / thread_count) & 0xFF) == 0) printf("Thread %" PRIu64 " slice %" PRIu64 "/%" PRIu64 "\n", thread, index / thread_count / 256 + 1, layer_0_found / thread_count / 256); + uint64_t state0 = candidates[index]; bitslice(state0 >> 2, &state[0], 46, false); + for (size_t bit = 0; bit < 8; bit++) { state[-2 + filter_pos[bit]] = initial_bitslices[bit]; } + for (uint16_t i1 = 0; i1 < (1 << (bits[1] + 1) >> 8); i1++) { state[-2 + 27].value = ((bool)(i1 & 0x1)) ? bs_ones.value : bs_zeroes.value; state[-2 + 30].value = ((bool)(i1 & 0x2)) ? bs_ones.value : bs_zeroes.value; @@ -208,6 +220,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter1 = f_c_bs(filter1_0, filter1_1, filter1_2, filter1_3, filter1_4); bitslice_t results1; results1.value = filter1 ^ keystream[1].value; + if (results1.bytes64[0] == 0 && results1.bytes64[1] == 0 && results1.bytes64[2] == 0 @@ -226,6 +239,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter10_0 = f_a_bs(state[-2 + 12].value, state[-2 + 13].value, state[-2 + 15].value, state[-2 + 16].value); const bitslice_value_t filter11_0 = f_a_bs(state[-2 + 13].value, state[-2 + 14].value, state[-2 + 16].value, state[-2 + 17].value); const bitslice_value_t filter12_0 = f_a_bs(state[-2 + 14].value, state[-2 + 15].value, state[-2 + 17].value, state[-2 + 18].value); + for (uint16_t i2 = 0; i2 < (1 << (bits[2] + 1)); i2++) { state[-2 + 10].value = ((bool)(i2 & 0x1)) ? bs_ones.value : bs_zeroes.value; state[-2 + 19].value = ((bool)(i2 & 0x2)) ? bs_ones.value : bs_zeroes.value; @@ -239,6 +253,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter2 = f_c_bs(filter2_0, filter2_1, filter2_2, filter2_3, filter2_4); bitslice_t results2; results2.value = results1.value & (filter2 ^ keystream[2].value); + if (results2.bytes64[0] == 0 && results2.bytes64[1] == 0 && results2.bytes64[2] == 0 @@ -257,6 +272,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter10_1 = f_b_bs(state[-2 + 18].value, state[-2 + 22].value, state[-2 + 24].value, state[-2 + 25].value); const bitslice_value_t filter10_2 = f_b_bs(state[-2 + 27].value, state[-2 + 31].value, state[-2 + 33].value, state[-2 + 36].value); const bitslice_value_t filter11_1 = f_b_bs(state[-2 + 19].value, state[-2 + 23].value, state[-2 + 25].value, state[-2 + 26].value); + for (uint8_t i3 = 0; i3 < (1 << bits[3]); i3++) { state[-2 + 11].value = ((bool)(i3 & 0x1)) ? bs_ones.value : bs_zeroes.value; state[-2 + 20].value = ((bool)(i3 & 0x2)) ? bs_ones.value : bs_zeroes.value; @@ -268,6 +284,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter3 = f_c_bs(filter3_0, filter3_1, filter3_2, filter3_3, filter3_4); bitslice_t results3; results3.value = results2.value & (filter3 ^ keystream[3].value); + if (results3.bytes64[0] == 0 && results3.bytes64[1] == 0 && results3.bytes64[2] == 0 @@ -275,6 +292,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 51].value = lfsr_bs(3); state[-2 + 52].value = lfsr_bs(4); state[-2 + 53].value = lfsr_bs(5); @@ -291,6 +309,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter9_4 = f_a_bs(state[-2 + 43].value, state[-2 + 52].value, state[-2 + 53].value, state[-2 + 55].value); const bitslice_value_t filter11_2 = f_b_bs(state[-2 + 28].value, state[-2 + 32].value, state[-2 + 34].value, state[-2 + 37].value); const bitslice_value_t filter12_1 = f_b_bs(state[-2 + 20].value, state[-2 + 24].value, state[-2 + 26].value, state[-2 + 27].value); + for (uint8_t i4 = 0; i4 < (1 << bits[4]); i4++) { state[-2 + 38].value = ((bool)(i4 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xff87ffffffff @@ -298,6 +317,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter4 = f_c_bs(filter4_0, filter4_1, filter4_2, filter4_3, filter4_4); bitslice_t results4; results4.value = results3.value & (filter4 ^ keystream[4].value); + if (results4.bytes64[0] == 0 && results4.bytes64[1] == 0 && results4.bytes64[2] == 0 @@ -305,8 +325,10 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter5_3 = f_b_bs(state[-2 + 33].value, state[-2 + 34].value, state[-2 + 36].value, state[-2 + 38].value); const bitslice_value_t filter12_2 = f_b_bs(state[-2 + 29].value, state[-2 + 33].value, state[-2 + 35].value, state[-2 + 38].value); + for (uint8_t i5 = 0; i5 < (1 << bits[5]); i5++) { state[-2 + 39].value = ((bool)(i5 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xffc7ffffffff @@ -314,6 +336,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter5 = f_c_bs(filter5_0, filter5_1, filter5_2, filter5_3, filter5_4); bitslice_t results5; results5.value = results4.value & (filter5 ^ keystream[5].value); + if (results5.bytes64[0] == 0 && results5.bytes64[1] == 0 && results5.bytes64[2] == 0 @@ -321,7 +344,9 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter6_3 = f_b_bs(state[-2 + 34].value, state[-2 + 35].value, state[-2 + 37].value, state[-2 + 39].value); + for (uint8_t i6 = 0; i6 < (1 << bits[6]); i6++) { state[-2 + 40].value = ((bool)(i6 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xffe7ffffffff @@ -329,6 +354,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter6 = f_c_bs(filter6_0, filter6_1, filter6_2, filter6_3, filter6_4); bitslice_t results6; results6.value = results5.value & (filter6 ^ keystream[6].value); + if (results6.bytes64[0] == 0 && results6.bytes64[1] == 0 && results6.bytes64[2] == 0 @@ -336,7 +362,9 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter7_3 = f_b_bs(state[-2 + 35].value, state[-2 + 36].value, state[-2 + 38].value, state[-2 + 40].value); + for (uint8_t i7 = 0; i7 < (1 << bits[7]); i7++) { state[-2 + 41].value = ((bool)(i7 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xfff7ffffffff @@ -351,9 +379,11 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter8_3 = f_b_bs(state[-2 + 36].value, state[-2 + 37].value, state[-2 + 39].value, state[-2 + 41].value); const bitslice_value_t filter10_3 = f_b_bs(state[-2 + 38].value, state[-2 + 39].value, state[-2 + 41].value, state[-2 + 43].value); const bitslice_value_t filter12_3 = f_b_bs(state[-2 + 40].value, state[-2 + 41].value, state[-2 + 43].value, state[-2 + 45].value); + for (uint8_t i8 = 0; i8 < (1 << bits[8]); i8++) { state[-2 + 42].value = ((bool)(i8 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xffffffffffff @@ -363,6 +393,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter8 = f_c_bs(filter8_0, filter8_1, filter8_2, filter8_3, filter8_4); bitslice_t results8; results8.value = results7.value & (filter8 ^ keystream[8].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -370,8 +401,10 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter9 = f_c_bs(filter9_0, filter9_1, filter9_2, filter9_3, filter9_4); results8.value &= (filter9 ^ keystream[9].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -383,6 +416,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter10_4 = f_a_bs(state[-2 + 44].value, state[-2 + 53].value, state[-2 + 54].value, state[-2 + 56].value); const bitslice_value_t filter10 = f_c_bs(filter10_0, filter10_1, filter10_2, filter10_3, filter10_4); results8.value &= (filter10 ^ keystream[10].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -390,10 +424,12 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 57].value = lfsr_bs(9); const bitslice_value_t filter11_4 = f_a_bs(state[-2 + 45].value, state[-2 + 54].value, state[-2 + 55].value, state[-2 + 57].value); const bitslice_value_t filter11 = f_c_bs(filter11_0, filter11_1, filter11_2, filter11_3, filter11_4); results8.value &= (filter11 ^ keystream[11].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -401,10 +437,12 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 58].value = lfsr_bs(10); const bitslice_value_t filter12_4 = f_a_bs(state[-2 + 46].value, state[-2 + 55].value, state[-2 + 56].value, state[-2 + 58].value); const bitslice_value_t filter12 = f_c_bs(filter12_0, filter12_1, filter12_2, filter12_3, filter12_4); results8.value &= (filter12 ^ keystream[12].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -412,6 +450,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 59].value = lfsr_bs(11); const bitslice_value_t filter13_0 = f_a_bs(state[-2 + 15].value, state[-2 + 16].value, state[-2 + 18].value, state[-2 + 19].value); const bitslice_value_t filter13_1 = f_b_bs(state[-2 + 21].value, state[-2 + 25].value, state[-2 + 27].value, state[-2 + 28].value); @@ -420,6 +459,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter13_4 = f_a_bs(state[-2 + 47].value, state[-2 + 56].value, state[-2 + 57].value, state[-2 + 59].value); const bitslice_value_t filter13 = f_c_bs(filter13_0, filter13_1, filter13_2, filter13_3, filter13_4); results8.value &= (filter13 ^ keystream[13].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -427,6 +467,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 60].value = lfsr_bs(12); const bitslice_value_t filter14_0 = f_a_bs(state[-2 + 16].value, state[-2 + 17].value, state[-2 + 19].value, state[-2 + 20].value); const bitslice_value_t filter14_1 = f_b_bs(state[-2 + 22].value, state[-2 + 26].value, state[-2 + 28].value, state[-2 + 29].value); @@ -435,6 +476,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter14_4 = f_a_bs(state[-2 + 48].value, state[-2 + 57].value, state[-2 + 58].value, state[-2 + 60].value); const bitslice_value_t filter14 = f_c_bs(filter14_0, filter14_1, filter14_2, filter14_3, filter14_4); results8.value &= (filter14 ^ keystream[14].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -442,6 +484,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 61].value = lfsr_bs(13); const bitslice_value_t filter15_0 = f_a_bs(state[-2 + 17].value, state[-2 + 18].value, state[-2 + 20].value, state[-2 + 21].value); const bitslice_value_t filter15_1 = f_b_bs(state[-2 + 23].value, state[-2 + 27].value, state[-2 + 29].value, state[-2 + 30].value); @@ -450,6 +493,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter15_4 = f_a_bs(state[-2 + 49].value, state[-2 + 58].value, state[-2 + 59].value, state[-2 + 61].value); const bitslice_value_t filter15 = f_c_bs(filter15_0, filter15_1, filter15_2, filter15_3, filter15_4); results8.value &= (filter15 ^ keystream[15].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -457,6 +501,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 62].value = lfsr_bs(14); const bitslice_value_t filter16_0 = f_a_bs(state[-2 + 18].value, state[-2 + 19].value, state[-2 + 21].value, state[-2 + 22].value); const bitslice_value_t filter16_1 = f_b_bs(state[-2 + 24].value, state[-2 + 28].value, state[-2 + 30].value, state[-2 + 31].value); @@ -465,6 +510,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter16_4 = f_a_bs(state[-2 + 50].value, state[-2 + 59].value, state[-2 + 60].value, state[-2 + 62].value); const bitslice_value_t filter16 = f_c_bs(filter16_0, filter16_1, filter16_2, filter16_3, filter16_4); results8.value &= (filter16 ^ keystream[16].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -472,6 +518,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 63].value = lfsr_bs(15); const bitslice_value_t filter17_0 = f_a_bs(state[-2 + 19].value, state[-2 + 20].value, state[-2 + 22].value, state[-2 + 23].value); const bitslice_value_t filter17_1 = f_b_bs(state[-2 + 25].value, state[-2 + 29].value, state[-2 + 31].value, state[-2 + 32].value); @@ -480,6 +527,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter17_4 = f_a_bs(state[-2 + 51].value, state[-2 + 60].value, state[-2 + 61].value, state[-2 + 63].value); const bitslice_value_t filter17 = f_c_bs(filter17_0, filter17_1, filter17_2, filter17_3, filter17_4); results8.value &= (filter17 ^ keystream[17].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -487,6 +535,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 64].value = lfsr_bs(16); const bitslice_value_t filter18_0 = f_a_bs(state[-2 + 20].value, state[-2 + 21].value, state[-2 + 23].value, state[-2 + 24].value); const bitslice_value_t filter18_1 = f_b_bs(state[-2 + 26].value, state[-2 + 30].value, state[-2 + 32].value, state[-2 + 33].value); @@ -495,6 +544,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter18_4 = f_a_bs(state[-2 + 52].value, state[-2 + 61].value, state[-2 + 62].value, state[-2 + 64].value); const bitslice_value_t filter18 = f_c_bs(filter18_0, filter18_1, filter18_2, filter18_3, filter18_4); results8.value &= (filter18 ^ keystream[18].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -502,6 +552,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 65].value = lfsr_bs(17); const bitslice_value_t filter19_0 = f_a_bs(state[-2 + 21].value, state[-2 + 22].value, state[-2 + 24].value, state[-2 + 25].value); const bitslice_value_t filter19_1 = f_b_bs(state[-2 + 27].value, state[-2 + 31].value, state[-2 + 33].value, state[-2 + 34].value); @@ -510,6 +561,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter19_4 = f_a_bs(state[-2 + 53].value, state[-2 + 62].value, state[-2 + 63].value, state[-2 + 65].value); const bitslice_value_t filter19 = f_c_bs(filter19_0, filter19_1, filter19_2, filter19_3, filter19_4); results8.value &= (filter19 ^ keystream[19].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -517,6 +569,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 66].value = lfsr_bs(18); const bitslice_value_t filter20_0 = f_a_bs(state[-2 + 22].value, state[-2 + 23].value, state[-2 + 25].value, state[-2 + 26].value); const bitslice_value_t filter20_1 = f_b_bs(state[-2 + 28].value, state[-2 + 32].value, state[-2 + 34].value, state[-2 + 35].value); @@ -525,6 +578,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter20_4 = f_a_bs(state[-2 + 54].value, state[-2 + 63].value, state[-2 + 64].value, state[-2 + 66].value); const bitslice_value_t filter20 = f_c_bs(filter20_0, filter20_1, filter20_2, filter20_3, filter20_4); results8.value &= (filter20 ^ keystream[20].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -532,6 +586,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 67].value = lfsr_bs(19); const bitslice_value_t filter21_0 = f_a_bs(state[-2 + 23].value, state[-2 + 24].value, state[-2 + 26].value, state[-2 + 27].value); const bitslice_value_t filter21_1 = f_b_bs(state[-2 + 29].value, state[-2 + 33].value, state[-2 + 35].value, state[-2 + 36].value); @@ -540,6 +595,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter21_4 = f_a_bs(state[-2 + 55].value, state[-2 + 64].value, state[-2 + 65].value, state[-2 + 67].value); const bitslice_value_t filter21 = f_c_bs(filter21_0, filter21_1, filter21_2, filter21_3, filter21_4); results8.value &= (filter21 ^ keystream[21].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -547,6 +603,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 68].value = lfsr_bs(20); const bitslice_value_t filter22_0 = f_a_bs(state[-2 + 24].value, state[-2 + 25].value, state[-2 + 27].value, state[-2 + 28].value); const bitslice_value_t filter22_1 = f_b_bs(state[-2 + 30].value, state[-2 + 34].value, state[-2 + 36].value, state[-2 + 37].value); @@ -555,6 +612,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter22_4 = f_a_bs(state[-2 + 56].value, state[-2 + 65].value, state[-2 + 66].value, state[-2 + 68].value); const bitslice_value_t filter22 = f_c_bs(filter22_0, filter22_1, filter22_2, filter22_3, filter22_4); results8.value &= (filter22 ^ keystream[22].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -562,6 +620,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 69].value = lfsr_bs(21); const bitslice_value_t filter23_0 = f_a_bs(state[-2 + 25].value, state[-2 + 26].value, state[-2 + 28].value, state[-2 + 29].value); const bitslice_value_t filter23_1 = f_b_bs(state[-2 + 31].value, state[-2 + 35].value, state[-2 + 37].value, state[-2 + 38].value); @@ -600,6 +659,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter25_4 = f_a_bs(state[-2 + 59].value, state[-2 + 68].value, state[-2 + 69].value, state[-2 + 71].value); const bitslice_value_t filter25 = f_c_bs(filter25_0, filter25_1, filter25_2, filter25_3, filter25_4); results8.value &= (filter25 ^ keystream[25].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -607,6 +667,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 72].value = lfsr_bs(24); const bitslice_value_t filter26_0 = f_a_bs(state[-2 + 28].value, state[-2 + 29].value, state[-2 + 31].value, state[-2 + 32].value); const bitslice_value_t filter26_1 = f_b_bs(state[-2 + 34].value, state[-2 + 38].value, state[-2 + 40].value, state[-2 + 41].value); @@ -615,6 +676,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter26_4 = f_a_bs(state[-2 + 60].value, state[-2 + 69].value, state[-2 + 70].value, state[-2 + 72].value); const bitslice_value_t filter26 = f_c_bs(filter26_0, filter26_1, filter26_2, filter26_3, filter26_4); results8.value &= (filter26 ^ keystream[26].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -622,6 +684,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 73].value = lfsr_bs(25); const bitslice_value_t filter27_0 = f_a_bs(state[-2 + 29].value, state[-2 + 30].value, state[-2 + 32].value, state[-2 + 33].value); const bitslice_value_t filter27_1 = f_b_bs(state[-2 + 35].value, state[-2 + 39].value, state[-2 + 41].value, state[-2 + 42].value); @@ -630,6 +693,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter27_4 = f_a_bs(state[-2 + 61].value, state[-2 + 70].value, state[-2 + 71].value, state[-2 + 73].value); const bitslice_value_t filter27 = f_c_bs(filter27_0, filter27_1, filter27_2, filter27_3, filter27_4); results8.value &= (filter27 ^ keystream[27].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -637,6 +701,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 74].value = lfsr_bs(26); const bitslice_value_t filter28_0 = f_a_bs(state[-2 + 30].value, state[-2 + 31].value, state[-2 + 33].value, state[-2 + 34].value); const bitslice_value_t filter28_1 = f_b_bs(state[-2 + 36].value, state[-2 + 40].value, state[-2 + 42].value, state[-2 + 43].value); @@ -645,6 +710,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter28_4 = f_a_bs(state[-2 + 62].value, state[-2 + 71].value, state[-2 + 72].value, state[-2 + 74].value); const bitslice_value_t filter28 = f_c_bs(filter28_0, filter28_1, filter28_2, filter28_3, filter28_4); results8.value &= (filter28 ^ keystream[28].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -652,6 +718,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 75].value = lfsr_bs(27); const bitslice_value_t filter29_0 = f_a_bs(state[-2 + 31].value, state[-2 + 32].value, state[-2 + 34].value, state[-2 + 35].value); const bitslice_value_t filter29_1 = f_b_bs(state[-2 + 37].value, state[-2 + 41].value, state[-2 + 43].value, state[-2 + 44].value); @@ -660,6 +727,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter29_4 = f_a_bs(state[-2 + 63].value, state[-2 + 72].value, state[-2 + 73].value, state[-2 + 75].value); const bitslice_value_t filter29 = f_c_bs(filter29_0, filter29_1, filter29_2, filter29_3, filter29_4); results8.value &= (filter29 ^ keystream[29].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -667,6 +735,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 76].value = lfsr_bs(28); const bitslice_value_t filter30_0 = f_a_bs(state[-2 + 32].value, state[-2 + 33].value, state[-2 + 35].value, state[-2 + 36].value); const bitslice_value_t filter30_1 = f_b_bs(state[-2 + 38].value, state[-2 + 42].value, state[-2 + 44].value, state[-2 + 45].value); @@ -675,6 +744,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter30_4 = f_a_bs(state[-2 + 64].value, state[-2 + 73].value, state[-2 + 74].value, state[-2 + 76].value); const bitslice_value_t filter30 = f_c_bs(filter30_0, filter30_1, filter30_2, filter30_3, filter30_4); results8.value &= (filter30 ^ keystream[30].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -682,6 +752,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 77].value = lfsr_bs(29); const bitslice_value_t filter31_0 = f_a_bs(state[-2 + 33].value, state[-2 + 34].value, state[-2 + 36].value, state[-2 + 37].value); const bitslice_value_t filter31_1 = f_b_bs(state[-2 + 39].value, state[-2 + 43].value, state[-2 + 45].value, state[-2 + 46].value); @@ -690,6 +761,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter31_4 = f_a_bs(state[-2 + 65].value, state[-2 + 74].value, state[-2 + 75].value, state[-2 + 77].value); const bitslice_value_t filter31 = f_c_bs(filter31_0, filter31_1, filter31_2, filter31_3, filter31_4); results8.value &= (filter31 ^ keystream[31].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 From d98f8e96f45bba6a0513ab72261db37f1e4f818f Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 13:28:29 +0200 Subject: [PATCH 33/47] chg: hf iclass loclass - use defines, cppchecker happy --- client/src/loclass/cipher.c | 53 ++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/client/src/loclass/cipher.c b/client/src/loclass/cipher.c index 13f686b51..af5d4f3a3 100644 --- a/client/src/loclass/cipher.c +++ b/client/src/loclass/cipher.c @@ -68,6 +68,7 @@ typedef struct { * T (x 0 x 1 . . . . . . x 15 ) = x 0 ⊕ x 1 ⊕ x 5 ⊕ x 7 ⊕ x 10 ⊕ x 11 ⊕ x 14 ⊕ x 15 . **/ static bool T(State state) { +/* bool x0 = state.t & 0x8000; bool x1 = state.t & 0x4000; bool x5 = state.t & 0x0400; @@ -77,19 +78,33 @@ static bool T(State state) { bool x14 = state.t & 0x0002; bool x15 = state.t & 0x0001; return x0 ^ x1 ^ x5 ^ x7 ^ x10 ^ x11 ^ x14 ^ x15; +*/ +#define _x0 ((state.t & 0x8000) >> 15 ) +#define _x1 ((state.t & 0x4000) >> 14 ) +#define _x5 ((state.t & 0x0400) >> 10 ) +#define _x7 ((state.t & 0x0100) >> 8 ) +#define _x10 ((state.t & 0x0020) >> 5 ) +#define _x11 ((state.t & 0x0010) >> 4 ) +#define _x14 ((state.t & 0x0002) >> 1 ) +#define _x15 (state.t & 0x0001) + return (_x0) ^ (_x1) ^ (_x5) ^ (_x7) ^ (_x10) ^ (_x11) ^ (_x14) ^ (_x15); } /** * Similarly, the feedback function for the bottom register B : F 8/2 → F 2 is defined as * B(x 0 x 1 . . . x 7 ) = x 1 ⊕ x 2 ⊕ x 3 ⊕ x 7 . **/ -static bool B(State state) { +/*static bool B(State state) { bool x1 = state.b & 0x40; bool x2 = state.b & 0x20; bool x3 = state.b & 0x10; bool x7 = state.b & 0x01; return x1 ^ x2 ^ x3 ^ x7; } +*/ +#define B(x) (((x.b & 0x40) >> 6) ^ ((x.b & 0x20) >> 5) ^ ((x.b & 0x10) >> 4) ^ (x.b & 0x01)) +// 12 3456 +// 0100 0000 /** * Definition 3 (Selection function). The selection function select : F 2 × F 2 × @@ -99,14 +114,28 @@ static bool B(State state) { * z 2 = (r 3 ∧ r 5 ) ⊕ (r 4 ∧ r 6 ) ⊕ r 7 ⊕ x **/ static uint8_t _select(bool x, bool y, uint8_t r) { - bool r0 = r >> 7 & 0x1; - bool r1 = r >> 6 & 0x1; - bool r2 = r >> 5 & 0x1; - bool r3 = r >> 4 & 0x1; - bool r4 = r >> 3 & 0x1; - bool r5 = r >> 2 & 0x1; - bool r6 = r >> 1 & 0x1; - bool r7 = r & 0x1; + #define _r0 ((r >> 7) & 0x01) + #define _r1 ((r >> 6) & 0x01) + #define _r2 ((r >> 5) & 0x01) + #define _r3 ((r >> 4) & 0x01) + #define _r4 ((r >> 3) & 0x01) + #define _r5 ((r >> 2) & 0x01) + #define _r6 ((r >> 1) & 0x01) + #define _r7 (r & 0x01) + + #define _z0 ( (_r0 & _r2) ^ ( _r1 & (!_r3)) ^ (_r2 | _r4) ) + #define _z1 ( (_r0 | _r2) ^ ( _r5 | _r7) ^_r1 ^ _r6 ^ x ^ y ) + #define _z2 ( (_r3 & (!_r5)) ^ (_r4 & _r6) ^ _r7 ^ x ) + +/* + uint8_t r0 = r >> 7 & 0x1; + uint8_t r1 = r >> 6 & 0x1; + uint8_t r2 = r >> 5 & 0x1; + uint8_t r3 = r >> 4 & 0x1; + uint8_t r4 = r >> 3 & 0x1; + uint8_t r5 = r >> 2 & 0x1; + uint8_t r6 = r >> 1 & 0x1; + uint8_t r7 = r & 0x1; bool z0 = (r0 & r2) ^ (r1 & (!r3)) ^ (r2 | r4); bool z1 = (r0 | r2) ^ (r5 | r7) ^ r1 ^ r6 ^ x ^ y; @@ -115,6 +144,11 @@ static uint8_t _select(bool x, bool y, uint8_t r) { // The three bitz z0.. z1 are packed into a uint8_t: // 00000ZZZ //Return value is a uint8_t + return ((z0 << 2) & 4) | ((z1 << 1) & 2) | (z2 & 1); +*/ + return ((_z0 << 2) & 4) | ((_z1 << 1) & 2) | (_z2 & 1); + +/* uint8_t retval = 0; retval |= (z0 << 2) & 4; retval |= (z1 << 1) & 2; @@ -122,6 +156,7 @@ static uint8_t _select(bool x, bool y, uint8_t r) { // Return value 0 <= retval <= 7 return retval; +*/ } /** From b03f9402c12f76ad7d7e2f09d185d6eb1c2e4271 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 13:29:16 +0200 Subject: [PATCH 34/47] cppchecker fix --- tools/hitag2crack/crack5/ht2crack5.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/hitag2crack/crack5/ht2crack5.c b/tools/hitag2crack/crack5/ht2crack5.c index 08feae194..79557bbfd 100644 --- a/tools/hitag2crack/crack5/ht2crack5.c +++ b/tools/hitag2crack/crack5/ht2crack5.c @@ -99,7 +99,7 @@ uint64_t candidates[(1 << 20)]; bitslice_t initial_bitslices[48]; size_t filter_pos[20] = {4, 7, 9, 13, 16, 18, 22, 24, 27, 30, 32, 35, 45, 47 }; size_t thread_count = 8; -size_t layer_0_found; +uint64_t layer_0_found; void *find_state(void *thread_d); static void try_state(uint64_t s); @@ -170,7 +170,7 @@ int main(int argc, char *argv[]) { for (size_t i0 = 0; i0 < 1 << 20; i0++) { uint64_t state0 = expand(0x5806b4a2d16c, i0); - if (f(state0) == (target >> 31)) { + if (f(state0) == target >> 31) { candidates[layer_0_found++] = state0; } } From 4a282b32c2d34552a9fb235ca8bf30f3b2af392d Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 13:29:19 +0200 Subject: [PATCH 35/47] cppchecker fix --- tools/hitag2crack/crack5gpu/ht2crack5.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/hitag2crack/crack5gpu/ht2crack5.c b/tools/hitag2crack/crack5gpu/ht2crack5.c index 8e6c29c76..adaf1ae82 100644 --- a/tools/hitag2crack/crack5gpu/ht2crack5.c +++ b/tools/hitag2crack/crack5gpu/ht2crack5.c @@ -129,6 +129,11 @@ void runKernel(struct context *ctx, uint32_t cand_base, uint64_t *matches, uint3 // Write our data set into the input array in device memory err = clEnqueueWriteBuffer(ctx->commands, ctx->matches_found, CL_TRUE, 0, sizeof(uint32_t), matches_found, 0, NULL, NULL); + if (err != CL_SUCCESS) { + printf("Error: Failed to enque kernel writebuffer in runKernel! %d\n", err); + exit(1); + } + // Set the arguments to our compute kernel err = clSetKernelArg(ctx->kernel, 0, sizeof(uint32_t), &cand_base); err |= clSetKernelArg(ctx->kernel, 4, sizeof(cl_mem), &ctx->matches_found); @@ -231,7 +236,7 @@ int main(int argc, char *argv[]) { for (size_t i0 = 0; i0 < 1 << 20; i0++) { uint64_t state0 = expand(0x5806b4a2d16c, i0); - if (f(state0) == (target >> 31)) { + if (f(state0) == target >> 31 ) { // cf kernel, state is now split in 3 shorts >> 2 candidates[(layer_0_found * 3) + 0] = (uint16_t)((state0 >> (32 + 2)) & 0xffff); candidates[(layer_0_found * 3) + 1] = (uint16_t)((state0 >> (16 + 2)) & 0xffff); From f1cdb68c52c5d21c745004d3e7e203af883e13d2 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 13:30:43 +0200 Subject: [PATCH 36/47] fix: cppchecker complains on null. Not sure if that should be a valid call. I guess @cjbrigato will let me know if I messed up --- armsrc/frozen.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/armsrc/frozen.c b/armsrc/frozen.c index 41f2179cd..6477e0602 100644 --- a/armsrc/frozen.c +++ b/armsrc/frozen.c @@ -780,8 +780,7 @@ int json_printf_array(struct json_out *out, va_list *ap) { } #ifdef _WIN32 -int cs_win_vsnprintf(char *str, size_t size, const char *format, - va_list ap) WEAK; +int cs_win_vsnprintf(char *str, size_t size, const char *format, va_list ap) WEAK; int cs_win_vsnprintf(char *str, size_t size, const char *format, va_list ap) { int res = _vsnprintf(str, size, format, ap); va_end(ap); @@ -863,14 +862,21 @@ struct json_scanf_info { int json_unescape(const char *src, int slen, char *dst, int dlen) WEAK; int json_unescape(const char *src, int slen, char *dst, int dlen) { - char *send = (char *) src + slen, *dend = dst + dlen, *orig_dst = dst, *p; + if (dst == NULL || dlen == 0) + return JSON_STRING_INVALID; + + char *send = (char *) src + slen; + char *dend = dst + dlen; + char *orig_dst = dst; + char *p; const char *esc1 = "\"\\/bfnrt", *esc2 = "\"\\/\b\f\n\r\t"; while (src < send) { if (*src == '\\') { - if (++src >= send) return JSON_STRING_INCOMPLETE; + if (++src >= send) { return JSON_STRING_INCOMPLETE; } + if (*src == 'u') { - if (send - src < 5) return JSON_STRING_INCOMPLETE; + if (send - src < 5) { return JSON_STRING_INCOMPLETE; } /* Here we go: this is a \u.... escape. Process simple one-byte chars */ if (src[1] == '0' && src[2] == '0') { /* This is \u00xx character from the ASCII range */ @@ -881,12 +887,16 @@ int json_unescape(const char *src, int slen, char *dst, int dlen) { return JSON_STRING_INVALID; } } else if ((p = (char *) strchr(esc1, *src)) != NULL) { - if (dst < dend) *dst = esc2[p - esc1]; + if (dst < dend) { + *dst = esc2[p - esc1]; + } } else { return JSON_STRING_INVALID; } } else { - if (dst < dend) *dst = *src; + if (dst < dend) { + *dst = *src; + } } dst++; src++; From 19a5a70cc89d7f0c5da040f87a3fd2e1fc1445dc Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 13:36:24 +0200 Subject: [PATCH 37/47] cppchecker fix, now use res --- client/src/fido/fidocore.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/src/fido/fidocore.c b/client/src/fido/fidocore.c index 1a680ed88..6ee6557fb 100644 --- a/client/src/fido/fidocore.c +++ b/client/src/fido/fidocore.c @@ -383,7 +383,8 @@ static int FIDO2CheckSignature(json_t *root, uint8_t *publickey, uint8_t *sign, authData, authDataLen, // rpIdHash[32] + flags[1] + signCount[4] clientDataHash, 32, // Hash of the serialized client data. "$.ClientDataHash" from json NULL, 0); - //PrintAndLogEx(NORMAL, "--xbuf(%d)[%d]: %s", res, xbuflen, sprint_hex(xbuf, xbuflen)); + PrintAndLogEx(DEBUG, "--xbuf(%d)[%d]: %s", res, xbuflen, sprint_hex(xbuf, xbuflen)); + res = ecdsa_signature_verify(MBEDTLS_ECP_DP_SECP256R1, publickey, xbuf, xbuflen, sign, signLen, true); if (res) { if (res == MBEDTLS_ERR_ECP_VERIFY_FAILED) { From f1df27e6bbecb0c7728129115463006a315d9e77 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 13:44:25 +0200 Subject: [PATCH 38/47] cppchecker fix - better return value handling --- client/src/crypto/libpcrypto.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/client/src/crypto/libpcrypto.c b/client/src/crypto/libpcrypto.c index 1141818ab..a00918b91 100644 --- a/client/src/crypto/libpcrypto.c +++ b/client/src/crypto/libpcrypto.c @@ -177,8 +177,9 @@ static int ecdsa_init(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id curveid, int ecdsa_key_create(mbedtls_ecp_group_id curveid, uint8_t *key_d, uint8_t *key_xy) { int res; mbedtls_ecdsa_context ctx; - ecdsa_init(&ctx, curveid, NULL, NULL); - + res = ecdsa_init(&ctx, curveid, NULL, NULL); + if (res) + goto exit; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; @@ -276,7 +277,10 @@ int ecdsa_signature_create(mbedtls_ecp_group_id curveid, uint8_t *key_d, uint8_t goto exit; mbedtls_ecdsa_context ctx; - ecdsa_init(&ctx, curveid, key_d, key_xy); + res = ecdsa_init(&ctx, curveid, key_d, key_xy); + if (res) + goto exit; + res = mbedtls_ecdsa_write_signature( &ctx, MBEDTLS_MD_SHA256, @@ -308,7 +312,10 @@ static int ecdsa_signature_create_test(mbedtls_ecp_group_id curveid, const char param_gethex_to_eol(random, 0, fixed_rand_value, sizeof(fixed_rand_value), &rndlen); mbedtls_ecdsa_context ctx; - ecdsa_init_str(&ctx, curveid, key_d, key_x, key_y); + res = ecdsa_init_str(&ctx, curveid, key_d, key_x, key_y); + if (res) + return res; + res = mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256, shahash, sizeof(shahash), signature, signaturelen, fixed_rand, NULL); mbedtls_ecdsa_free(&ctx); @@ -323,7 +330,10 @@ static int ecdsa_signature_verify_keystr(mbedtls_ecp_group_id curveid, const cha return res; mbedtls_ecdsa_context ctx; - ecdsa_init_str(&ctx, curveid, NULL, key_x, key_y); + res = ecdsa_init_str(&ctx, curveid, NULL, key_x, key_y); + if (res) + return res; + res = mbedtls_ecdsa_read_signature( &ctx, hash ? shahash : input, @@ -347,6 +357,9 @@ int ecdsa_signature_verify(mbedtls_ecp_group_id curveid, uint8_t *key_xy, uint8_ mbedtls_ecdsa_context ctx; res = ecdsa_init(&ctx, curveid, NULL, key_xy); + if (res) + return res; + res = mbedtls_ecdsa_read_signature( &ctx, hash ? shahash : input, From 662d3259ca128e0837d6f296b8daaba82e88701f Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 13:46:13 +0200 Subject: [PATCH 39/47] cppchecker fix, shadow var --- client/src/cmdhfmfdes.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 287b13523..a5bab46dd 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -3347,8 +3347,9 @@ static int CmdHF14ADesEnumApplications(const char *Cmd) { uint8_t filesettings[20] = {0}; int fileset_len = 0; - int res = handler_desfire_filesettings(file_ids[j], filesettings, &fileset_len); int maclen = 0; // To be implemented + + res = handler_desfire_filesettings(file_ids[j], filesettings, &fileset_len); if (res == PM3_SUCCESS) { if (DecodeFileSettings(filesettings, fileset_len, maclen) != PM3_SUCCESS) { PrintAndLogEx(INFO, " Settings [%u] %s", fileset_len, sprint_hex(filesettings, fileset_len)); From 6a55330398ef93e5726709dd6e0fdb88178b8ec5 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 13:53:43 +0200 Subject: [PATCH 40/47] enforce PACKED structs --- client/src/cmdhfmfdes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index a5bab46dd..3504c85f2 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -46,12 +46,12 @@ typedef struct { uint8_t keyno; uint8_t keylen; uint8_t key[24]; -} mfdes_authinput_t; +} PACKED mfdes_authinput_t; typedef struct mfdes_auth_res { uint8_t sessionkeylen; uint8_t sessionkey[24]; -} mfdes_auth_res_t; +} PACKED mfdes_auth_res_t; typedef struct mfdes_data { uint8_t fileno; //01 From 25c94177f8ace4c1c4df55302b9a302d51b4d2e7 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 14:31:29 +0200 Subject: [PATCH 41/47] coverity fix 286664 --- client/src/preferences.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/preferences.c b/client/src/preferences.c index 9d482a1f5..b0534a671 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -138,7 +138,7 @@ int preferences_save(void) { if (fileExists(fn)) { if (rename(fn, backupFilename) != 0) { - PrintAndLogEx(FAILED, "Error - could not backup settings file \"%s\" to \"%s\"", prefGetFilename(), backupFilename); + PrintAndLogEx(FAILED, "Error - could not backup settings file \"%s\" to \"%s\"", fn, backupFilename); free(fn); free(backupFilename); return PM3_ESOFT; @@ -927,7 +927,7 @@ static int CmdPrefShow(const char *Cmd) { return PM3_ESOFT; } - // PrintAndLogEx(NORMAL, " preference file........ "_GREEN_("%s"), prefGetFilename()); + // PrintAndLogEx(NORMAL, " preference file........ "_GREEN_("%s"), fn); showEmojiState(prefShowNone); showHintsState(prefShowNone); showColorState(prefShowNone); From 5e925962d3b8487a7fe97840d0fe7ba7046885bb Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 14:32:12 +0200 Subject: [PATCH 42/47] coverity fix 286994 --- client/src/fido/fidocore.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/client/src/fido/fidocore.c b/client/src/fido/fidocore.c index 6ee6557fb..32d7c9ff0 100644 --- a/client/src/fido/fidocore.c +++ b/client/src/fido/fidocore.c @@ -359,10 +359,11 @@ bool CheckrpIdHash(json_t *json, uint8_t *hash) { // check ANSI X9.62 format ECDSA signature (on P-256) static int FIDO2CheckSignature(json_t *root, uint8_t *publickey, uint8_t *sign, size_t signLen, uint8_t *authData, size_t authDataLen, bool verbose) { - int res; + uint8_t rval[300] = {0}; uint8_t sval[300] = {0}; - res = ecdsa_asn1_get_signature(sign, signLen, rval, sval); + + int res = ecdsa_asn1_get_signature(sign, signLen, rval, sval); if (!res) { if (verbose) { PrintAndLogEx(NORMAL, " r: %s", sprint_hex(rval, 32)); @@ -383,7 +384,7 @@ static int FIDO2CheckSignature(json_t *root, uint8_t *publickey, uint8_t *sign, authData, authDataLen, // rpIdHash[32] + flags[1] + signCount[4] clientDataHash, 32, // Hash of the serialized client data. "$.ClientDataHash" from json NULL, 0); - PrintAndLogEx(DEBUG, "--xbuf(%d)[%d]: %s", res, xbuflen, sprint_hex(xbuf, xbuflen)); + PrintAndLogEx(DEBUG, "--xbuf(%d)[%zu]: %s", res, xbuflen, sprint_hex(xbuf, xbuflen)); res = ecdsa_signature_verify(MBEDTLS_ECP_DP_SECP256R1, publickey, xbuf, xbuflen, sign, signLen, true); if (res) { From 0759f886f4b7ab7baf9a42a151ef4806ce4c6cee Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 14:33:45 +0200 Subject: [PATCH 43/47] coverity fix 286923 --- client/src/cmdhfmfdes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 3504c85f2..22735f46b 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -2220,7 +2220,7 @@ static int CmdHF14ADesGetValueData(const char *Cmd) { int len = 0; res = handler_desfire_getvalue(&value, &len); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "Successfully read value from File %u:", fileno); + PrintAndLogEx(SUCCESS, "Successfully read value from File %u:", fileno[0]); PrintAndLogEx(NORMAL, "\nOffset | Data | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); for (int i = 0; i < len; i += 16) { From 0896cee51c3b3a4339898094b90fac16cba87273 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 14:35:00 +0200 Subject: [PATCH 44/47] coverity fix 286680 --- client/src/cmdhfmfdes.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 22735f46b..ea1b1eb72 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -2712,8 +2712,8 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { CLIGetHexWithReturn(7, value, &vllen); int limitedlen = 0; - uint8_t limited = 0; - CLIGetHexWithReturn(8, &limited, &limitedlen); + uint8_t limited[1] = {0}; + CLIGetHexWithReturn(8, limited, &limitedlen); CLIParserFree(); @@ -2781,7 +2781,7 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { memcpy(ft.lowerlimit, lowerlimit, 4); memcpy(ft.upperlimit, upperlimit, 4); memcpy(ft.value, value, 4); - ft.limitedcreditenabled = limited; + ft.limitedcreditenabled = limited[0]; res = handler_desfire_create_value_file(&ft); if (res == PM3_SUCCESS) { From fa98a10b387b29c33e5a914cc86748eb00d92fe2 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 14:38:32 +0200 Subject: [PATCH 45/47] coverity fix 286651 --- client/src/cmdhfmfdes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index ea1b1eb72..65b02b5f8 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -3669,7 +3669,7 @@ static int AuthCheckDesfire(uint8_t *aid, uint8_t deskeyList[MAX_KEYS_LIST_LEN][ DropField(); res = handler_desfire_select_application(aid); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "AID %X does not exist."); + PrintAndLogEx(ERR, "AID 0x%06X does not exist", curaid); return res; } break; From ae142721177f8e3baa803f13bfcc8c72e2c5fc7e Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 14:46:40 +0200 Subject: [PATCH 46/47] coverity fix: 279923, flawed logic. how to signal to device to use the default keys? --- armsrc/mifaredesfire.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/armsrc/mifaredesfire.c b/armsrc/mifaredesfire.c index 1fb77ab0e..ff5babda0 100644 --- a/armsrc/mifaredesfire.c +++ b/armsrc/mifaredesfire.c @@ -270,7 +270,8 @@ void MifareDES_Auth1(uint8_t *datain) { LED_B_OFF(); LED_C_OFF(); - if (payload->key == NULL) { + + if (payload->keylen == 0) { if (payload->algo == MFDES_AUTH_DES) { memcpy(keybytes, PICC_MASTER_KEY8, 8); } else if (payload->algo == MFDES_ALGO_AES || payload->algo == MFDES_ALGO_3DES) { From 7303dd8c5dfafccbb9eaf70687ef1ef7a60c504d Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 28 Apr 2020 14:51:47 +0200 Subject: [PATCH 47/47] coverity fix 279922 , div by zero --- client/src/mifare/desfire_crypto.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/client/src/mifare/desfire_crypto.c b/client/src/mifare/desfire_crypto.c index e4caaae26..a0e066b44 100644 --- a/client/src/mifare/desfire_crypto.c +++ b/client/src/mifare/desfire_crypto.c @@ -281,6 +281,10 @@ void cmac_generate_subkeys(desfirekey_t key) { void cmac(const desfirekey_t key, uint8_t *ivect, const uint8_t *data, size_t len, uint8_t *cmac) { int kbs = key_block_size(key); + if (kbs == 0) { + return; + } + uint8_t *buffer = malloc(padded_data_length(len, kbs)); memcpy(buffer, data, len);