From 8fea6cddf722a43701b64a48d5c161c9e13b3af0 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sun, 5 Apr 2020 20:36:39 +1000 Subject: [PATCH 1/3] Concept --- client/Makefile | 3 +- client/emv/emvjson.c | 3 + client/emv/emvjson.h | 1 + client/fileutils.c | 10 ++- client/fileutils.h | 1 + client/proxmark3.c | 8 ++ client/settings.c | 186 +++++++++++++++++++++++++++++++++++++++++++ client/settings.h | 32 ++++++++ 8 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 client/settings.c create mode 100644 client/settings.h diff --git a/client/Makefile b/client/Makefile index a09a033d9..2c5a663c4 100644 --- a/client/Makefile +++ b/client/Makefile @@ -251,7 +251,8 @@ CMDSRCS = crapto1/crapto1.c \ flash.c \ wiegand_formats.c \ wiegand_formatutils.c \ - cardhelper.c + cardhelper.c \ + settings.c cpu_arch = $(shell uname -m) ifneq ($(findstring 86, $(cpu_arch)), ) diff --git a/client/emv/emvjson.c b/client/emv/emvjson.c index 75b2e87bf..9b86b38ca 100644 --- a/client/emv/emvjson.c +++ b/client/emv/emvjson.c @@ -94,6 +94,9 @@ int JsonSaveStr(json_t *root, const char *path, const char *value) { return JsonSaveJsonObject(root, path, json_string(value)); }; +int JsonSaveBoolean(json_t *root, const char *path, bool value) { + return JsonSaveJsonObject(root, path, json_boolean(value)); +} int JsonSaveBufAsHexCompact(json_t *elm, const char *path, uint8_t *data, size_t datalen) { char *msg = sprint_hex_inrow(data, datalen); if (msg && strlen(msg) && msg[strlen(msg) - 1] == ' ') diff --git a/client/emv/emvjson.h b/client/emv/emvjson.h index 54d97ada8..9b1efb034 100644 --- a/client/emv/emvjson.h +++ b/client/emv/emvjson.h @@ -24,6 +24,7 @@ const char *GetApplicationDataName(tlv_tag_t tag); int JsonSaveJsonObject(json_t *root, const char *path, json_t *value); int JsonSaveStr(json_t *root, const char *path, const char *value); +int JsonSaveBoolean(json_t *root, const char *path, bool value); int JsonSaveInt(json_t *root, const char *path, int value); int JsonSaveBufAsHexCompact(json_t *elm, const char *path, uint8_t *data, size_t datalen); int JsonSaveBufAsHex(json_t *elm, const char *path, uint8_t *data, size_t datalen); diff --git a/client/fileutils.c b/client/fileutils.c index ecf47383f..5ce1f3a6a 100644 --- a/client/fileutils.c +++ b/client/fileutils.c @@ -52,6 +52,9 @@ #define PATH_MAX_LENGTH 200 +extern void JsonLoadSettingsCallback (json_t *root); +extern void JsonSaveSettingsCallback (json_t *root); + struct wave_info_t { char signature[4]; uint32_t filesize; @@ -425,6 +428,9 @@ int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, s } } break; + case jsfSettings: + JsonSaveSettingsCallback (root); + break; default: break; } @@ -863,7 +869,9 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_ } *datalen = sptr; } - + if (!strcmp(ctype,"settings")) { + JsonLoadSettingsCallback (root); + } PrintAndLogEx(SUCCESS, "loaded from JSON file " _YELLOW_("%s"), fileName); out: json_decref(root); diff --git a/client/fileutils.h b/client/fileutils.h index 3747e0850..06d3ff20a 100644 --- a/client/fileutils.h +++ b/client/fileutils.h @@ -62,6 +62,7 @@ typedef enum { jsfT55x7, jsfT5555, jsfMfPlusKeys, + jsfSettings, } JSONFileType; typedef enum { diff --git a/client/proxmark3.c b/client/proxmark3.c index 5a6ae23a9..504ae93e5 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -27,6 +27,7 @@ #include "comms.h" #include "fileutils.h" #include "flash.h" +#include "settings.h" static void showBanner(void) { g_printAndLog = PRINTANDLOG_PRINT; @@ -557,6 +558,7 @@ int main(int argc, char *argv[]) { /* initialize history */ using_history(); + #ifdef RL_STATE_READCMD rl_extend_line_buffer(1024); @@ -581,6 +583,12 @@ int main(int argc, char *argv[]) { set_my_executable_path(); set_my_user_directory(); + // Settings + settingsLoad (); + settingsSave (); + printf ("Ver : %s\n",mySettings.version); + // End Settings + for (int i = 1; i < argc; i++) { if (argv[i][0] != '-') { diff --git a/client/settings.c b/client/settings.c new file mode 100644 index 000000000..a3571c566 --- /dev/null +++ b/client/settings.c @@ -0,0 +1,186 @@ +/***************************************************************************** + * WARNING + * + * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY. + * + * USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL + * PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL, + * AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES. + * + * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS. + * + ***************************************************************************** + * + * This file is part of loclass. It is a reconstructon of the cipher engine + * used in iClass, and RFID techology. + * + * The implementation is based on the work performed by + * Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and + * Milosch Meriac in the paper "Dismantling IClass". + * + * Copyright (C) 2014 Martin Holst Swende + * + * This is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation, or, at your option, any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with loclass. If not, see . + * + * + ****************************************************************************/ + +//----------------------------------------------------------------------------- +// Settings Functions +//----------------------------------------------------------------------------- + +#include "settings.h" +#include "comms.h" +#include "emv/emvjson.h" + +// settings_t mySettings; + +// Load all settings into memory (struct) +void settingsLoad (void) +{ + // loadFileJson wants these, so pass in place holder values, though not used + // in settings load; + uint8_t dummyData = 0x00; + size_t dummyDL = 0x00; + + // clear all settings + memset (&mySettings,0x00,sizeof(mySettings)); + + if (loadFileJSON(settingsFilename, &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { + printf ("==> Settings Loaded\n"); + mySettings.loaded = true; + } + + + // Test results + /* + bool os_windows_usecolor; + bool os_windows_useansicolor; + int window_xpos; + int window_ypos; + int window_hsize; + int window_wsize; + */ + printf (" Settings Version : [%s]\n",mySettings.version); + printf (" os_windows_usecolor (bool) : [%d]\n",mySettings.os_windows_usecolor); + printf (" os_windows_useAnsicolor (bool) : [%d]\n",mySettings.os_windows_useansicolor); + printf (" window_xpos (int) : [%d]\n",mySettings.window_xpos); + printf (" window_ypos (int) : [%d]\n",mySettings.window_ypos); + printf (" window_hsize (int) : [%d]\n",mySettings.window_hsize); + printf (" window_wsize (int) : [%d]\n",mySettings.window_wsize); + +} + +// Save all settings from memory (struct) to file +int settingsSave (void) +{ + // Note sure if backup has value ? + char backupFilename[500]; + // if (mySettings.loaded) + + snprintf (backupFilename,sizeof(backupFilename),"%s.bak",settingsFilename); + + if (fileExists (backupFilename)) { + if (remove (backupFilename) != 0) { + PrintAndLogEx (FAILED, "Error - could not delete old settings backup file \"%s\"",backupFilename); + return PM3_ESOFT; + } + } + + if (fileExists (settingsFilename)) { + if (rename (settingsFilename,backupFilename) != 0) { + PrintAndLogEx (FAILED, "Error - could not backup settings file \"%s\" to \"%s\"",settingsFilename,backupFilename); + return PM3_ESOFT; + } + } + + uint8_t dummyData = 0x00; + size_t dummyDL = 0x00; + + // int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen); + + if (saveFileJSON(settingsFilename, jsfSettings, &dummyData, dummyDL) == PM3_SUCCESS) + PrintAndLogEx (NORMAL, "settings have been saved to \"%s\"",settingsFilename); + + return PM3_SUCCESS; +} + +void JsonSaveSettingsCallback (json_t *root) +{ + // extern settings_t mySettings; + + printf ("==> Save Settings\n"); + //JsonSaveStr(root, "FileType", "settings"); + //JsonSaveStr (root,"Test1.Test2","test settings"); + /* + "version": "1.0 Nov 2019", + "os.windows.usecolor": true, + "os.windows.useAnsiColor": true, + "window.xpos": 10, + "window.ypos": 10, + "window.hsize": 300, + "window.wsize": 600 + */ + JsonSaveStr (root,"FileType","settings"); + JsonSaveStr (root,"version","1.0 Nov 2019");//mySettings.version); + JsonSaveBoolean (root,"os.windows.useColor",mySettings.os_windows_usecolor); + JsonSaveBoolean (root,"os.windows.useAnsiColor",mySettings.os_windows_useansicolor); + JsonSaveInt (root,"window.xpos",mySettings.window_xpos); + JsonSaveInt (root,"window.ypos",mySettings.window_ypos); + JsonSaveInt (root,"window.hsize",mySettings.window_hsize); + JsonSaveInt (root,"window.wsize",mySettings.window_wsize); +} + +void JsonLoadSettingsCallback (json_t *root) +{ +// extern settings_t mySettings; + json_error_t up_error = {0}; + int b1; + int i1; + const char *s1; + + if (json_unpack_ex(root, &up_error , 0, "{s:s}","version",&s1) == 0) + strncpy (mySettings.version,s1,sizeof (mySettings.version) - 1); + else + strncpy (mySettings.version,"unknown",sizeof (mySettings.version) - 1); + + // os.windows... + if (json_unpack_ex(root,&up_error, 0, "{s:b}","os.windows.useColor",&b1) == 0) + mySettings.os_windows_usecolor = b1; + else // default + mySettings.os_windows_useansicolor = false; + + if (json_unpack_ex(root,&up_error, 0, "{s:b}","os.windows.useAnsiColor",&b1) == 0) + mySettings.os_windows_useansicolor = b1; + else // default + mySettings.os_windows_useansicolor = false; + + // window... + if (json_unpack_ex(root,&up_error, 0, "{s:i}","window.xpos",&i1) == 0) + mySettings.window_xpos = i1; + else // default + mySettings.window_xpos = 0; + if (json_unpack_ex(root,&up_error, 0, "{s:i}","window.ypos",&i1) == 0) + mySettings.window_ypos = i1; + else // default + mySettings.window_ypos = 0; + if (json_unpack_ex(root,&up_error, 0, "{s:i}","window.hsize",&i1) == 0) + mySettings.window_hsize = i1; + else // default + mySettings.window_hsize = 0; + if (json_unpack_ex(root,&up_error, 0, "{s:i}","window.wsize",&i1) == 0) + mySettings.window_wsize = i1; + else // default + mySettings.window_wsize = 0; + +} diff --git a/client/settings.h b/client/settings.h new file mode 100644 index 000000000..f0093c207 --- /dev/null +++ b/client/settings.h @@ -0,0 +1,32 @@ +//----------------------------------------------------------------------------- +// Copyright (C) 2009 Michael Gernoth +// Copyright (C) 2010 iZsh +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// Settings Functions +//----------------------------------------------------------------------------- + +#include "fileutils.h" + +#define settingsFilename "settings.json" +typedef struct { + bool loaded; + char version[20]; + bool os_windows_usecolor; + bool os_windows_useansicolor; + int window_xpos; + int window_ypos; + int window_hsize; + int window_wsize; +} settings_t; + +settings_t mySettings; + +void settingsLoad (void); +int settingsSave (void); + +void JsonSaveCallback ( json_t *root); +void JsonLoadCallback ( json_t *root); From 7b14fd7212b2e5f053b1f00dbc4b35d953c84f40 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 6 Apr 2020 16:20:57 +1000 Subject: [PATCH 2/3] Code Clean --- client/emv/emvjson.c | 1 + client/fileutils.c | 9 ++++----- client/proxmark3.c | 7 +++---- client/settings.c | 14 ++++++-------- client/settings.h | 14 ++++++++++---- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/client/emv/emvjson.c b/client/emv/emvjson.c index 9b86b38ca..968b51d51 100644 --- a/client/emv/emvjson.c +++ b/client/emv/emvjson.c @@ -97,6 +97,7 @@ int JsonSaveStr(json_t *root, const char *path, const char *value) { int JsonSaveBoolean(json_t *root, const char *path, bool value) { return JsonSaveJsonObject(root, path, json_boolean(value)); } + int JsonSaveBufAsHexCompact(json_t *elm, const char *path, uint8_t *data, size_t datalen) { char *msg = sprint_hex_inrow(data, datalen); if (msg && strlen(msg) && msg[strlen(msg) - 1] == ' ') diff --git a/client/fileutils.c b/client/fileutils.c index 5ce1f3a6a..50d3ca9d6 100644 --- a/client/fileutils.c +++ b/client/fileutils.c @@ -38,6 +38,8 @@ // this define is needed for scandir/alphasort to work #define _GNU_SOURCE #include "fileutils.h" +#include "settings.h" + #include #include @@ -52,9 +54,6 @@ #define PATH_MAX_LENGTH 200 -extern void JsonLoadSettingsCallback (json_t *root); -extern void JsonSaveSettingsCallback (json_t *root); - struct wave_info_t { char signature[4]; uint32_t filesize; @@ -429,7 +428,7 @@ int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, s } break; case jsfSettings: - JsonSaveSettingsCallback (root); + settings_save_callback (root); break; default: break; @@ -870,7 +869,7 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_ *datalen = sptr; } if (!strcmp(ctype,"settings")) { - JsonLoadSettingsCallback (root); + settings_load_callback (root); } PrintAndLogEx(SUCCESS, "loaded from JSON file " _YELLOW_("%s"), fileName); out: diff --git a/client/proxmark3.c b/client/proxmark3.c index 504ae93e5..168b5b960 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -558,7 +558,6 @@ int main(int argc, char *argv[]) { /* initialize history */ using_history(); - #ifdef RL_STATE_READCMD rl_extend_line_buffer(1024); @@ -583,9 +582,9 @@ int main(int argc, char *argv[]) { set_my_executable_path(); set_my_user_directory(); - // Settings - settingsLoad (); - settingsSave (); + // Settings Load and Test + settings_load (); + settings_save (); printf ("Ver : %s\n",mySettings.version); // End Settings diff --git a/client/settings.c b/client/settings.c index a3571c566..976121eed 100644 --- a/client/settings.c +++ b/client/settings.c @@ -43,10 +43,8 @@ #include "comms.h" #include "emv/emvjson.h" -// settings_t mySettings; - // Load all settings into memory (struct) -void settingsLoad (void) +int settings_load (void) { // loadFileJson wants these, so pass in place holder values, though not used // in settings load; @@ -79,17 +77,17 @@ void settingsLoad (void) printf (" window_hsize (int) : [%d]\n",mySettings.window_hsize); printf (" window_wsize (int) : [%d]\n",mySettings.window_wsize); + return PM3_SUCCESS; } // Save all settings from memory (struct) to file -int settingsSave (void) +int settings_save (void) { // Note sure if backup has value ? char backupFilename[500]; - // if (mySettings.loaded) snprintf (backupFilename,sizeof(backupFilename),"%s.bak",settingsFilename); - + if (fileExists (backupFilename)) { if (remove (backupFilename) != 0) { PrintAndLogEx (FAILED, "Error - could not delete old settings backup file \"%s\"",backupFilename); @@ -115,7 +113,7 @@ int settingsSave (void) return PM3_SUCCESS; } -void JsonSaveSettingsCallback (json_t *root) +void settings_save_callback (json_t *root) { // extern settings_t mySettings; @@ -141,7 +139,7 @@ void JsonSaveSettingsCallback (json_t *root) JsonSaveInt (root,"window.wsize",mySettings.window_wsize); } -void JsonLoadSettingsCallback (json_t *root) +void settings_load_callback (json_t *root) { // extern settings_t mySettings; json_error_t up_error = {0}; diff --git a/client/settings.h b/client/settings.h index f0093c207..799af7caa 100644 --- a/client/settings.h +++ b/client/settings.h @@ -8,10 +8,13 @@ //----------------------------------------------------------------------------- // Settings Functions //----------------------------------------------------------------------------- +#ifndef settings_h +#define settings_h #include "fileutils.h" #define settingsFilename "settings.json" + typedef struct { bool loaded; char version[20]; @@ -23,10 +26,13 @@ typedef struct { int window_wsize; } settings_t; +// Settings struct so as to be available to other modules by including settings.h settings_t mySettings; -void settingsLoad (void); -int settingsSave (void); +int settings_load (void); +int settings_save (void); -void JsonSaveCallback ( json_t *root); -void JsonLoadCallback ( json_t *root); +void settings_save_callback (json_t *root); +void settings_load_callback (json_t *root); + +#endif From f0b0d10ee1a67ae6de2e3d937ba3b85eee656f14 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 6 Apr 2020 16:36:12 +1000 Subject: [PATCH 3/3] Comment out test calls --- client/fileutils.c | 1 - client/proxmark3.c | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/client/fileutils.c b/client/fileutils.c index 50d3ca9d6..05a7e8949 100644 --- a/client/fileutils.c +++ b/client/fileutils.c @@ -40,7 +40,6 @@ #include "fileutils.h" #include "settings.h" - #include #include diff --git a/client/proxmark3.c b/client/proxmark3.c index 168b5b960..0438f81ad 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -583,9 +583,9 @@ int main(int argc, char *argv[]) { set_my_user_directory(); // Settings Load and Test - settings_load (); - settings_save (); - printf ("Ver : %s\n",mySettings.version); + // settings_load (); + // settings_save (); + // printf ("Ver : %s\n",mySettings.version); // End Settings for (int i = 1; i < argc; i++) {