feat: limit hook range for insecure method

This commit is contained in:
Kuriko Moe
2026-04-14 22:15:39 +08:00
parent 65476df9e3
commit e4a5a164ef
9 changed files with 61 additions and 44 deletions
+2
View File
@@ -51,6 +51,8 @@ Checks: >
-modernize-avoid-c-arrays,
-readability-redundant-control-flow,
-google-build-using-namespace,
-misc-use-internal-linkage,
-readability-implicit-bool-conversion,
# -modernize-use-nodiscard: [[nodiscard]] for const func
# -bugprone-easily-swappable-parameters: I cannot avoid this fn(i32, i32)
+1 -1
View File
@@ -34,7 +34,7 @@ uninstall:
copyall: build
cp build/windows/x86/release/kpatch.dll "{{TARGET}}"
cp build/windows/x86/release/left4dead2_fix.exe "{{TARGET}}"
cp kpatch.ini "{{TARGET}}"
# cp kpatch.ini "{{TARGET}}"
copy: build
cp build/windows/x86/release/kpatch.dll "{{TARGET}}"
+1 -1
View File
@@ -1,6 +1,6 @@
[System]
; 日志级别, trace debug info warn error critical off
log_level = info
debug = false
[Redirect]
-8
View File
@@ -14,18 +14,10 @@
namespace fs = std::filesystem;
// const wchar_t* game_name = L"left4dead2.exe";
bool debug = false;
std::wstring game_name = L"left4dead2.exe";
void init_cfg() {
// std::ifstream is("kpatch.ini");
// inipp::Ini<char> ini;
// ini.parse(is);
// inipp::extract(ini.sections["System"]["debug"], debug);
// std::string tmp_name;
// inipp::extract(ini.sections["System"]["target"], tmp_name);
LoadIni();
if (cfg::Redirect::enable) {
game_name = cfg::Redirect::target;
+28 -22
View File
@@ -35,7 +35,7 @@ void InitConsole() {
}
void Logging() {
if (cfg::System::log_level == "debug") {
if (cfg::System::debug) {
InitConsole();
}
// Get this module path
@@ -58,8 +58,8 @@ void Logging() {
auto start_time = std::chrono::system_clock::now();
if (!cfg::System::log_level.empty()) {
spdlog::set_level(spdlog::level::from_str(cfg::System::log_level));
if (cfg::System::debug) {
spdlog::set_level(spdlog::level::debug);
}
else {
spdlog::set_level(spdlog::level::info);
@@ -105,23 +105,27 @@ DWORD __stdcall Main(void*) {
LoadIni();
Logging();
if (cfg::System::log_level == "debug") {
MessageBoxW(NULL, L"警告:你已开启 debug 输出。", pMsgboxTitle, MB_OK|MB_SYSTEMMODAL|MB_ICONWARNING);
if (cfg::System::debug) {
MessageBoxW(
nullptr,
L"警告:你已开启 debug 输出。",
L"L4D2 Fix",
MB_OK|MB_SYSTEMMODAL|MB_ICONWARNING);
}
if (cfg::Redirect::enable && cfg::Redirect::origin == L"left4dead2.exe") {
auto ret = MessageBoxW(
NULL,
L"警告,你可能开启了 L4D2Fix 伪装为 Left 4 Dead 2 原版 exe。\n"
"该方法配合 -secure -steam 启动项将允许连接 VAC 服务器。\n"
"如果造成 VAC 封禁,请自行承担后果。",
L"该方法配合 -secure -steam 启动项将允许连接 VAC 服务器。\n"
L"如果造成 VAC 封禁,请自行承担后果。",
pMsgboxTitle, MB_OKCANCEL|MB_SYSTEMMODAL|MB_ICONWARNING);
switch (ret) {
case IDOK:
spdlog::warn(L"用户已确认使用伪装方法,补丁将继续加载...");
break;
case IDCANCEL:
spdlog::warn(L"用户已取消使用伪装方法,取消补丁加载...");
default:
spdlog::warn(L"用户取消补丁加载...");
ExitProcess(0);
return TRUE;
}
@@ -150,7 +154,10 @@ DWORD __stdcall Main(void*) {
std::wcout << absPath << std::endl;
spdlog::info(L"Loading Dll from {}", absPath.wstring());
HMODULE hDll = LoadLibraryExW(absPath.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
HMODULE hDll = LoadLibraryExW(
absPath.c_str(),
nullptr,
LOAD_WITH_ALTERED_SEARCH_PATH);
if (hDll == nullptr) {
auto errCode = GetLastError();
@@ -161,13 +168,17 @@ DWORD __stdcall Main(void*) {
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
std::wcout << errCode << std::endl;
std::wcout << buf << std::endl;
MessageBoxW(NULL, std::format(L"Failed to load {}!", dllName).c_str(), pMsgboxTitle, MB_OK | MB_SYSTEMMODAL | MB_ICONSTOP);
MessageBoxW(
NULL,
std::format(L"Failed to load {}!", dllName).c_str(),
pMsgboxTitle,
MB_OK | MB_SYSTEMMODAL | MB_ICONSTOP);
exit(-1);
}
// Fix exe name
initExeNameHook();
LPSTR buf = new char[255];
auto* buf = new char[255];
GetModuleFileNameA(NULL, buf, 255); // Trigger the hook once
// Not known
@@ -181,22 +192,16 @@ DWORD __stdcall Main(void*) {
// ret += VertexBuffer::hooks_vertexbuffer(hDll, dllName);
if (ret != 0) {
unsigned short int msgID = MessageBoxW(NULL,
unsigned short int msgID = MessageBoxW(
NULL,
L"未能正常应用补丁,patch 未生效,请联系开发者!\n"
"这只是一个警告,可能不影响游戏运行"
"是否前往Github issue进行反馈?",
L"这只是一个警告,可能不影响游戏运行",
pMsgboxTitle, MB_YESNO | MB_ICONWARNING);
if(msgID == IDYES) {
HINSTANCE result = ShellExecuteW(0, 0, L"https://github.com/kurikomoe/L4D2Fix/issues/new", 0, 0 , SW_SHOW);
if ((INT_PTR)result <= 32) {
spdlog::error(L"Cannot open link to Github issue: {}", (INT_PTR)result);
}
}
}
return TRUE;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID /*lpReserved*/) {
if (DetourIsHelperProcess()) {
return TRUE;
}
@@ -213,6 +218,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
// CloseHandle(mainHandle);
// }
break;
default: break;
}
return TRUE;
}
+18 -1
View File
@@ -21,16 +21,33 @@ DWORD __stdcall hGetModuleFileNameA(
auto ret = oGetModuleFileNameAFn(hModule, lpFilename, nSize);
if (!lpFilename) return ret;
void* callerAddress = _ReturnAddress();
HMODULE callerModule = NULL;
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)callerAddress, &callerModule);
char moduleName[MAX_PATH];
auto retCaller = oGetModuleFileNameAFn(callerModule, moduleName, MAX_PATH);
const std::vector<std::string> moduleNameList = {
"launcher.dll",
};
spdlog::info("GetModuleFileNameA called by module: {}", moduleName);
for (auto& name : moduleNameList) {
if (!strstr(moduleName, name.c_str())) {
return ret;
}
}
fs::path filePath(lpFilename);
fs::path newFilePath(lpFilename);
std::wstring exeName = filePath.filename().wstring();
if (cfg::Redirect::enable && exeName == cfg::Redirect::target) {
spdlog::info("GetModuleFileNameA called by module: {}", moduleName);
std::string newExeName = std::string(
cfg::Redirect::origin.begin(), cfg::Redirect::origin.end());
newFilePath = newFilePath.replace_filename(newExeName);
std::string newFilePathStr = newFilePath.string();
memcpy_s(lpFilename, nSize, newFilePath.string().c_str(), newFilePathStr.size() + 1);
spdlog::info("GetModuleFileNameA hooked: \n{} => {}", filePath.string(), newFilePath.string());
}
return ret;
}
+3 -3
View File
@@ -15,7 +15,7 @@ HMODULE thisModule; // Fix DLL
// Version
std::string sFixName = "L4D2Fix";
std::string sFixVer = "1.4.0-debug";
std::string sFixVer = "1.4.0";
std::string sLogFile = sFixName + ".log";
// Strings
@@ -33,7 +33,7 @@ inipp::Ini<char> ini;
// Global config vars
namespace cfg {
namespace System {
std::string log_level;
bool debug;
}
namespace Redirect {
@@ -82,7 +82,7 @@ void LoadIni() {
{
using namespace System;
inipp::extract(ini.sections["System"]["log_level"], log_level);
inipp::extract(ini.sections["System"]["debug"], debug);
}
{
Submodule third/source-engine updated: 29985681a1...ed8209cc35
+7 -7
View File
@@ -3,26 +3,26 @@
version = "1.0"
},
["mingw|x86"] = {
["minhook#31fecfc4"] = {
["minhook#f56260b5"] = {
repo = {
branch = "master",
commit = "3d19345df6ab7745c5c779ee9b20166f96bc559a",
commit = "ad68fb6965284bfdece605f3c6d737f13d58a18e",
url = "https://github.com/xmake-io/xmake-repo.git"
},
version = "v1.3.4"
},
["spdlog#31fecfc4"] = {
["spdlog#f56260b5"] = {
repo = {
branch = "master",
commit = "4333aa942c37880a4830485b0d2043d02d02391d",
commit = "ad68fb6965284bfdece605f3c6d737f13d58a18e",
url = "https://github.com/xmake-io/xmake-repo.git"
},
version = "v1.15.3"
version = "v1.17.0"
},
["vcpkg::detours#31fecfc4"] = {
["vcpkg::detours#f56260b5"] = {
version = "latest"
},
["vcpkg::inipp#31fecfc4"] = {
["vcpkg::inipp#f56260b5"] = {
version = "latest"
}
},