From a994639caa5c5fda3ee2efb67fa34c84282ca67d Mon Sep 17 00:00:00 2001 From: Xpl0itR Date: Sun, 15 Jan 2023 20:25:09 +0000 Subject: [PATCH] Implement all the things --- VersionShim.sln | 32 ++++++++++ VersionShim/VersionShim.vcxproj | 84 +++++++++++++++++++++++++ VersionShim/VersionShim.vcxproj.filters | 36 +++++++++++ VersionShim/dllmain.c | 66 +++++++++++++++++++ VersionShim/pch.c | 3 + VersionShim/pch.h | 14 +++++ VersionShim/util.c | 78 +++++++++++++++++++++++ VersionShim/util.h | 8 +++ 8 files changed, 321 insertions(+) create mode 100644 VersionShim.sln create mode 100644 VersionShim/VersionShim.vcxproj create mode 100644 VersionShim/VersionShim.vcxproj.filters create mode 100644 VersionShim/dllmain.c create mode 100644 VersionShim/pch.c create mode 100644 VersionShim/pch.h create mode 100644 VersionShim/util.c create mode 100644 VersionShim/util.h diff --git a/VersionShim.sln b/VersionShim.sln new file mode 100644 index 0000000..30935ab --- /dev/null +++ b/VersionShim.sln @@ -0,0 +1,32 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33209.295 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VersionShim", "VersionShim\VersionShim.vcxproj", "{7D2E1375-BF0B-49EF-BBA8-C134A347F1C7}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{39DCA191-5807-42D8-A66A-373FBE7F5EA9}" + ProjectSection(SolutionItems) = preProject + .gitignore = .gitignore + LICENSE = LICENSE + README.md = README.md + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7D2E1375-BF0B-49EF-BBA8-C134A347F1C7}.Debug|x64.ActiveCfg = Debug|x64 + {7D2E1375-BF0B-49EF-BBA8-C134A347F1C7}.Debug|x64.Build.0 = Debug|x64 + {7D2E1375-BF0B-49EF-BBA8-C134A347F1C7}.Release|x64.ActiveCfg = Release|x64 + {7D2E1375-BF0B-49EF-BBA8-C134A347F1C7}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {577CC093-ACE1-4DFE-BC2F-AAD2A3A7FF00} + EndGlobalSection +EndGlobal diff --git a/VersionShim/VersionShim.vcxproj b/VersionShim/VersionShim.vcxproj new file mode 100644 index 0000000..57d34b2 --- /dev/null +++ b/VersionShim/VersionShim.vcxproj @@ -0,0 +1,84 @@ + + + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {7d2e1375-bf0b-49ef-bba8-c134a347f1c7} + VersionShim + 10.0 + VersionShim + + + + DynamicLibrary + true + v143 + Unicode + + + DynamicLibrary + false + v143 + true + Unicode + + + + + + + + + + + Level3 + true + _DEBUG;VERSIONSHIM_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Create + pch.h + + + Windows + true + + + + + Level3 + true + true + true + NDEBUG;VERSIONSHIM_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Create + pch.h + + + Windows + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/VersionShim/VersionShim.vcxproj.filters b/VersionShim/VersionShim.vcxproj.filters new file mode 100644 index 0000000..0db1bb8 --- /dev/null +++ b/VersionShim/VersionShim.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/VersionShim/dllmain.c b/VersionShim/dllmain.c new file mode 100644 index 0000000..aa79719 --- /dev/null +++ b/VersionShim/dllmain.c @@ -0,0 +1,66 @@ +// Copyright © 2023 Xpl0itR +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "pch.h" +#include "util.h" + +#pragma region Forward functions to version.dll +#pragma comment(linker, "/EXPORT:GetFileVersionInfoA=C:\\Windows\\System32\\version.GetFileVersionInfoA,@1") +#pragma comment(linker, "/EXPORT:GetFileVersionInfoByHandle=C:\\Windows\\System32\\version.GetFileVersionInfoByHandle,@2") +#pragma comment(linker, "/EXPORT:GetFileVersionInfoExA=C:\\Windows\\System32\\version.GetFileVersionInfoExA,@3") +#pragma comment(linker, "/EXPORT:GetFileVersionInfoExW=C:\\Windows\\System32\\version.GetFileVersionInfoExW,@4") +#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeA=C:\\Windows\\System32\\version.GetFileVersionInfoSizeA,@5") +#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeExA=C:\\Windows\\System32\\version.GetFileVersionInfoSizeExA,@6") +#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeExW=C:\\Windows\\System32\\version.GetFileVersionInfoSizeExW,@7") +#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeW=C:\\Windows\\System32\\version.GetFileVersionInfoSizeW,@8") +#pragma comment(linker, "/EXPORT:GetFileVersionInfoW=C:\\Windows\\System32\\version.GetFileVersionInfoW,@9") +#pragma comment(linker, "/EXPORT:VerFindFileA=C:\\Windows\\System32\\version.VerFindFileA,@10") +#pragma comment(linker, "/EXPORT:VerFindFileW=C:\\Windows\\System32\\version.VerFindFileW,@11") +#pragma comment(linker, "/EXPORT:VerInstallFileA=C:\\Windows\\System32\\version.VerInstallFileA,@12") +#pragma comment(linker, "/EXPORT:VerInstallFileW=C:\\Windows\\System32\\version.VerInstallFileW,@13") +#pragma comment(linker, "/EXPORT:VerLanguageNameA=C:\\Windows\\System32\\version.VerLanguageNameA,@14") +#pragma comment(linker, "/EXPORT:VerLanguageNameW=C:\\Windows\\System32\\version.VerLanguageNameW,@15") +#pragma comment(linker, "/EXPORT:VerQueryValueA=C:\\Windows\\System32\\version.VerQueryValueA,@16") +#pragma comment(linker, "/EXPORT:VerQueryValueW=C:\\Windows\\System32\\version.VerQueryValueW,@17") +#pragma endregion + +BOOL APIENTRY DllMain(const HMODULE hModule, const DWORD fdwReason, LPCVOID lpReserved) +{ + if (fdwReason != DLL_PROCESS_ATTACH) + return TRUE; + + HANDLE hFile = NULL; + if (!OpenRead("Libraries.txt", &hFile)) + return FALSE; + + LPCH fileBuf = NULL; + DWORD fileLen = 0; + BOOL success = ReadUtf8File(hFile, &fileBuf, &fileLen); + CloseHandle(hFile); + if (!success) + return Error(L"Failed to read Libraries.txt"); + if (fileLen > INT_MAX) + return Error(L"Libraries.txt was too large to read"); + + LPWSTR fileStr = NULL; + success = Utf8ToUtf16(fileBuf, &fileStr, (int)fileLen); + free(fileBuf); + if (!success) + return Error(L"Failed to read Libraries.txt as UTF-16"); + + LPWSTR end = fileStr + fileLen; + for (LPWSTR dllPath = fileStr; dllPath < end;) + { + int numCharInVal = NextLine(dllPath); + + LoadLibrary(dllPath); + + dllPath += numCharInVal + 2; + } + + free(fileStr); + return TRUE; +} \ No newline at end of file diff --git a/VersionShim/pch.c b/VersionShim/pch.c new file mode 100644 index 0000000..4c048fe --- /dev/null +++ b/VersionShim/pch.c @@ -0,0 +1,3 @@ +// pch.c: source file corresponding to the pre-compiled header +// When you are using pre-compiled headers, this source file is necessary for compilation to succeed. +#include "pch.h" \ No newline at end of file diff --git a/VersionShim/pch.h b/VersionShim/pch.h new file mode 100644 index 0000000..8f68251 --- /dev/null +++ b/VersionShim/pch.h @@ -0,0 +1,14 @@ +// pch.h: This is a precompiled header file. +// Files listed below are compiled only once, improving build performance for future builds. +// This also affects IntelliSense performance, including code completion and many code browsing features. +// However, files listed here are ALL re-compiled if any one of them is updated between builds. +// Do not add files here that you will be updating frequently as this negates the performance advantage. + +#ifndef PCH_H +#define PCH_H + +#define WIN32_LEAN_AND_MEAN +#include +#include + +#endif //PCH_H \ No newline at end of file diff --git a/VersionShim/util.c b/VersionShim/util.c new file mode 100644 index 0000000..4fcbc75 --- /dev/null +++ b/VersionShim/util.c @@ -0,0 +1,78 @@ +// Copyright © 2023 Xpl0itR +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "pch.h" + +BOOL Error(const LPWSTR message) +{ + MessageBox(NULL, message, L"VersionShim", MB_ICONHAND | MB_OK); + return FALSE; +} + +BOOL OpenRead(LPCSTR lpFileName, HANDLE* fileHandle) +{ + OFSTRUCT ofStruct = { 0 }; + HFILE hFile = OpenFile(lpFileName, &ofStruct, OF_READ | OF_PROMPT); + + *fileHandle = (HANDLE)hFile; + return hFile != HFILE_ERROR; +} + +BOOL ReadUtf8File(const HANDLE fileHandle, LPCH* fileBuffer, DWORD* fileLength) +{ + const DWORD fileLen = GetFileSize(fileHandle, NULL); + *fileLength = fileLen; + + LPCH buffer = malloc(fileLen + 1); + if (!buffer) + return FALSE; + + if (!ReadFile(fileHandle, buffer, fileLen, NULL, NULL)) + { + free(buffer); + return FALSE; + } + + buffer[fileLen] = '\0'; + *fileBuffer = buffer; + + return TRUE; +} + +BOOL Utf8ToUtf16(LPCCH utf8String, const PZPWSTR utf16String, const int length) +{ + LPWSTR string = malloc(length * 2 + 2); + if (!string) + return FALSE; + + if (!MultiByteToWideChar(CP_UTF8, 0, utf8String, length, string, length)) + { + free(string); + return FALSE; + } + + string[length] = L'\0'; + *utf16String = string; + + return TRUE; +} + +int NextLine(const LPWSTR text) +{ + for (int i = 0; ; i++) + { + if (text[i] == L'\r' && + text[i + 1] == L'\n') + { + text[i] = text[i + 1] = L'\0'; + } + + if (text[i] == L'\0') + { + return i; + } + } +} \ No newline at end of file diff --git a/VersionShim/util.h b/VersionShim/util.h new file mode 100644 index 0000000..61839b5 --- /dev/null +++ b/VersionShim/util.h @@ -0,0 +1,8 @@ +#pragma once +#include "pch.h" + +BOOL Error(LPWSTR message); +BOOL OpenRead(LPCSTR lpFileName, HANDLE* fileHandle); +BOOL ReadUtf8File(HANDLE fileHandle, LPCH* fileBuffer, DWORD* fileLength); +BOOL Utf8ToUtf16(LPCCH utf8String, PZPWSTR utf16String, int length); +int NextLine(LPWSTR text); \ No newline at end of file