diff --git a/client/Makefile b/client/Makefile index ed2491202..87cf03a19 100644 --- a/client/Makefile +++ b/client/Makefile @@ -252,7 +252,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..968b51d51 100644 --- a/client/emv/emvjson.c +++ b/client/emv/emvjson.c @@ -94,6 +94,10 @@ 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..05a7e8949 100644 --- a/client/fileutils.c +++ b/client/fileutils.c @@ -38,6 +38,7 @@ // this define is needed for scandir/alphasort to work #define _GNU_SOURCE #include "fileutils.h" +#include "settings.h" #include #include @@ -425,6 +426,9 @@ int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, s } } break; + case jsfSettings: + settings_save_callback (root); + break; default: break; } @@ -863,7 +867,9 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_ } *datalen = sptr; } - + if (!strcmp(ctype,"settings")) { + settings_load_callback (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..0438f81ad 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; @@ -581,6 +582,12 @@ int main(int argc, char *argv[]) { set_my_executable_path(); set_my_user_directory(); + // Settings Load and Test + // settings_load (); + // settings_save (); + // 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..976121eed --- /dev/null +++ b/client/settings.c @@ -0,0 +1,184 @@ +/***************************************************************************** + * 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" + +// Load all settings into memory (struct) +int settings_load (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); + + return PM3_SUCCESS; +} + +// Save all settings from memory (struct) to file +int settings_save (void) +{ + // Note sure if backup has value ? + char backupFilename[500]; + + 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 settings_save_callback (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 settings_load_callback (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..799af7caa --- /dev/null +++ b/client/settings.h @@ -0,0 +1,38 @@ +//----------------------------------------------------------------------------- +// 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 +//----------------------------------------------------------------------------- +#ifndef settings_h +#define settings_h + +#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 struct so as to be available to other modules by including settings.h +settings_t mySettings; + +int settings_load (void); +int settings_save (void); + +void settings_save_callback (json_t *root); +void settings_load_callback (json_t *root); + +#endif