Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
AspectUnk committed Jun 28, 2024
1 parent b158f8f commit 1f0550a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 79 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Folders
**/.vs/
**/enc_temp_folder/
**/x64/Debug/
**/x64/Release/

# Prerequisites
*.d

Expand Down
6 changes: 2 additions & 4 deletions cs2_swapping/cs2_swapping.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<LanguageStandard>stdcpplatest</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
Expand All @@ -149,8 +148,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
163 changes: 88 additions & 75 deletions cs2_swapping/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <cstdint>
#include <thread>
#include <chrono>
#include <array>
#include <process.h>
#include <filesystem>
#include <Psapi.h>

#include <fmt/core.h>
#include <MinHook.h>
Expand All @@ -13,113 +15,124 @@

std::uintptr_t find_pattern(std::uintptr_t base, size_t length, const char* pattern, const char* mask)
{
length -= strlen(mask);

for (int i = 0; i <= length; i++)
{
bool found = true;

for (int p = 0; mask[p]; p++)
{
if (mask[p] == 'x' && pattern[p] != reinterpret_cast<char*>(base)[i + p])
{
found = false;
break;
}
}

if (found)
return base + i;
}

return NULL;
}

std::string to_str(std::wstring wstr)
{
return std::string(wstr.begin(), wstr.end());
length -= strlen(mask);

for (int i = 0; i <= length; i++)
{
bool found = true;

for (int p = 0; mask[p]; p++)
{
if (mask[p] == 'x' && pattern[p] != reinterpret_cast<char*>(base)[i + p])
{
found = false;
break;
}
}

if (found)
return base + i;
}

return NULL;
}

void replace_str(const std::string& find_, const std::string& to_, std::string& str)
{
for (size_t npos = 0; (npos = str.find(find_)) != std::string::npos;)
str.replace(npos, find_.size(), to_);
for (size_t npos = 0; (npos = str.find(find_)) != std::string::npos;)
str.replace(npos, find_.size(), to_);
}

std::string normalize_path(std::string path)
{
replace_str("\\\\", "/", path);
replace_str("\\", "/", path);
replace_str("\\\\", "/", path);
replace_str("\\", "/", path);

return path;
return path;
}

typedef __int64 (__fastcall* sub_180010B40_fn)(__int64 a1, const char* a2, __int64 a3, int a4, const char* a5);
sub_180010B40_fn o_sub_180010B40 = nullptr;
typedef __int64(__fastcall* open_fn)(__int64 a1, const char* a2, __int64 a3, int a4, const char* a5);
open_fn o_open = nullptr;

__int64 __fastcall sub_180010B40_hk(__int64 a1, const char* a2, __int64 a3, int a4, const char* a5)
__int64 __fastcall open_hk(__int64 a1, const char* a2, __int64 a3, int a4, const char* a5)
{
std::string path = normalize_path(a2);
const std::string path = normalize_path(a2);

for (const auto& dir : std::filesystem::recursive_directory_iterator("./swapping"))
{
std::string absolute = std::filesystem::absolute(dir.path()).string();
std::string relative = normalize_path(dir.path().string());

for (const auto& dir : std::filesystem::recursive_directory_iterator("./swapping"))
{
std::string absolute = to_str(std::filesystem::absolute(dir.path()));
std::string relative = normalize_path(to_str(dir.path().c_str()));
replace_str("./swapping/", "", relative);

replace_str("./swapping/", "", relative);
if (relative != path)
continue;

if (relative != path)
continue;

fmt::println("file {}", path.c_str());
fmt::println("{:c}{:c} swapped to {}", 192, 196, relative.c_str());
return o_sub_180010B40(a1, absolute.c_str(), a3, a4, "");
}
fmt::println("file {}", path.c_str());
fmt::println("{:c}{:c} swapped to {}", 192, 196, absolute);
return o_open(a1, absolute.c_str(), a3, a4, "");
}

return o_sub_180010B40(a1, a2, a3, a4, a5);
return o_open(a1, a2, a3, a4, a5);
}

void main_thread(void*)
{
std::filesystem::create_directory("./swapping");
std::filesystem::create_directory("./swapping");

fmt::print("waiting filesystem_stdio.dll module... ");

HMODULE module_ = nullptr;
for (; !module_; module_ = GetModuleHandle(L"filesystem_stdio.dll"))
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);
}

fmt::println("loaded.");

MODULEINFO module_info = {};
if (!GetModuleInformation(GetCurrentProcess(), module_, &module_info, sizeof(module_info)))
return fmt::println("failed to retrieve module information: {:08X}", GetLastError());

fmt::print("waiting filesystem_stdio.dll module... ");
constexpr auto signatures = std::to_array<std::pair<const char*, const char*>>({
{ "\x48\x8B\xC4\x44\x89\x48\x20\x48\x89\x50\x10", "xxxxxxxxxxx" },
{ "\x44\x89\x4C\x24\x00\x4C\x89\x44\x24\x00\x48\x89\x54\x24\x00\x55", "xxxx?xxxx?xxxx?x" }
});

std::uintptr_t base = NULL;
for (; !base; base = std::uintptr_t(GetModuleHandle(L"filesystem_stdio.dll")))
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::uintptr_t fn_ptr = NULL;

fmt::println("loaded.");
for (const auto& [pattern, mask] : signatures)
{
fn_ptr = find_pattern(reinterpret_cast<std::uintptr_t>(module_), module_info.SizeOfImage, pattern, mask);
if (fn_ptr)
break;
}

std::uintptr_t fn_ptr = find_pattern(base, 0x6415C, "\x48\x8B\xC4\x44\x89\x48\x20\x48\x89\x50\x10", "xxxxxxxxxxx");
if (!fn_ptr)
return fmt::println("failed to find function");
if (!fn_ptr)
return fmt::println("failed to find function");

if (MH_Initialize() != MH_OK)
return fmt::println("failed to initialize minhook");
if (MH_Initialize() != MH_OK)
return fmt::println("failed to initialize minhook");

if (MH_CreateHook((void*)fn_ptr, &sub_180010B40_hk, (void**)&o_sub_180010B40) != MH_OK ||
MH_EnableHook(MH_ALL_HOOKS) != MH_OK)
return fmt::println("failed to set hook");
if (MH_CreateHook((void*)fn_ptr, &open_hk, (void**)&o_open) != MH_OK || MH_EnableHook(MH_ALL_HOOKS) != MH_OK)
return fmt::println("failed to set hook");
}

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 (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
AllocConsole();
AttachConsole(ATTACH_PARENT_PROCESS);
SetConsoleTitle(L"cs2 swapping");
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
AllocConsole();
AttachConsole(ATTACH_PARENT_PROCESS);
SetConsoleTitle(L"cs2 swapping");

FILE* file = nullptr;
freopen_s(&file, "CONOUT$", "w", stdout);
FILE* file = nullptr;
freopen_s(&file, "CONOUT$", "w", stdout);

_beginthread(&main_thread, NULL, nullptr);
}
_beginthread(&main_thread, NULL, nullptr);
}

return TRUE;
return TRUE;
}

0 comments on commit 1f0550a

Please sign in to comment.