This commit is contained in:
6arelyFuture 2022-03-22 23:41:36 +00:00
parent 1dd227baa5
commit 8a2df0a179
No known key found for this signature in database
GPG Key ID: E883E2BC9657D955
42 changed files with 2495 additions and 3388 deletions

12
.clang-format Normal file
View File

@ -0,0 +1,12 @@

---
Language: Cpp
BasedOnStyle: LLVM
DerivePointerAlignment: false
PointerAlignment: Left
SortIncludes: false
# Regroup causes unnecessary noise due to clang-format bug.
IncludeBlocks: Preserve
---

55
.github/workflows/build.yml vendored Normal file
View File

@ -0,0 +1,55 @@
name: Build
on:
push:
branches:
- "*"
pull_request:
branches:
- "*"
types: [opened, synchronize, reopened]
jobs:
build:
name: Build binaries
runs-on: windows-2022
strategy:
matrix:
configuration:
- Debug
- Release
steps:
- name: Wait for previous workflows
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
uses: softprops/turnstyle@v1
with:
poll-interval-seconds: 10
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Check out files
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0
# NOTE - If LFS ever starts getting used during builds, switch this to true!
lfs: false
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1
- name: Generate project files
run: tools/premake5 vs2022
- name: Set up problem matching
uses: ammaraskar/msvc-problem-matcher@master
- name: Build ${{matrix.configuration}} binaries
run: msbuild /m /v:minimal /p:Configuration=${{matrix.configuration}} /p:Platform=Win32 build/mw3-server-freezer.sln
- name: Upload ${{matrix.configuration}} binaries
uses: actions/upload-artifact@v2
with:
name: ${{matrix.configuration}} binaries
path: |
build/bin/Win32/${{matrix.configuration}}/mw3-server-freezer.dll
build/bin/Win32/${{matrix.configuration}}/mw3-server-freezer.pdb

View File

@ -0,0 +1,20 @@
name: clang-format Check
on:
push:
branches:
- "*"
pull_request:
branches:
- "*"
types: [opened, synchronize, reopened]
jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run clang-format style check for C/C++/Protobuf programs.
uses: jidicula/clang-format-action@v4.6.2
with:
clang-format-version: '14'
check-path: 'src'

View File

@ -14,8 +14,7 @@ If you think your server is vulnerable you should seek help in the appropriate d
## Compile from source ## Compile from source
- Clone the Git repo. Do NOT download it as ZIP, that won't work. - Clone the Git repo. Do NOT download it as ZIP, that won't work.
- Update the submodules and run `premake5 vs2019` or simply use the delivered `generate.bat`. - Update the submodules and run `premake5 vs2022` or simply use the delivered `generate.bat`.
- Visual Studio Properties -> Linker -> Input -> Module Definition File -> add module.def located in src folder
- Build via solution file in `build\mw3-server-freezer.sln`. - Build via solution file in `build\mw3-server-freezer.sln`.
## Commands ## Commands

View File

@ -1,4 +1,4 @@
@echo off @echo off
echo Updating submodules... echo Updating submodules...
call git submodule update --init --recursive call git submodule update --init --recursive
call tools\premake5 %* vs2019 call tools\premake5 %* vs2022

View File

@ -36,36 +36,39 @@ workspace "mw3-server-freezer"
targetdir "%{wks.location}/bin/%{cfg.platform}/%{cfg.buildcfg}" targetdir "%{wks.location}/bin/%{cfg.platform}/%{cfg.buildcfg}"
targetname "%{prj.name}" targetname "%{prj.name}"
language "C++"
architecture "x86"
platforms "x86"
buildoptions "/std:c++latest"
systemversion "latest"
flags { "NoIncrementalLink", "NoMinimalRebuild", "MultiProcessorCompile", "No64BitChecks" }
configurations {"Debug", "Release"} configurations {"Debug", "Release"}
language "C++"
cppdialect "C++20"
architecture "x86"
platforms "Win32"
systemversion "latest"
symbols "On" symbols "On"
staticruntime "On" staticruntime "On"
editandcontinue "Off" editandcontinue "Off"
warnings "Extra" warnings "Extra"
characterset "ASCII" characterset "ASCII"
configuration "Release" flags {"NoIncrementalLink", "NoMinimalRebuild", "MultiProcessorCompile", "No64BitChecks"}
filter "platforms:Win*"
defines {"_WINDOWS", "WIN32"}
filter {}
filter "configurations:Release"
optimize "Size" optimize "Size"
defines {"NDEBUG"} defines {"NDEBUG"}
flags {"FatalCompileWarnings"} flags {"FatalCompileWarnings"}
buildoptions {"/GL"} buildoptions {"/GL"}
linkoptions { "/IGNORE:4702", "/LTCG" } linkoptions { "/IGNORE:4702", "/LTCG" }
configuration {} filter {}
configuration "Debug" filter "configurations:Debug"
optimize "Debug" optimize "Debug"
defines {"DEBUG", "_DEBUG"} defines {"DEBUG", "_DEBUG"}
configuration {} filter {}
project "mw3-server-freezer" project "mw3-server-freezer"
kind "SharedLib" kind "SharedLib"

View File

@ -9,12 +9,11 @@
namespace cheats namespace cheats
{ {
game::dvar_t* cl_EnableCheats; game::dvar_t* cl_EnableCheats;
__declspec(naked) void draw_red_box_stub() __declspec(naked) void draw_red_box_stub()
{ {
__asm __asm {
{
push eax push eax
mov eax, cl_EnableCheats mov eax, cl_EnableCheats
cmp byte ptr [eax + 12], 1 cmp byte ptr [eax + 12], 1
@ -30,13 +29,12 @@ namespace cheats
draw: draw:
push 0x43056A push 0x43056A
retn retn
} }
} }
__declspec(naked) void blind_eye_check_stub() __declspec(naked) void blind_eye_check_stub()
{ {
__asm __asm {
{
push eax push eax
mov eax, cl_EnableCheats mov eax, cl_EnableCheats
cmp byte ptr [eax + 12], 1 cmp byte ptr [eax + 12], 1
@ -45,59 +43,71 @@ namespace cheats
je draw je draw
test byte ptr [esi], 0x20 test byte ptr [esi], 0x20
jnz skipBecauseBlindeye jnz skip_because_blindeye
jmp draw jmp draw
skipBecauseBlindeye: skip_because_blindeye:
push 0x5AA5A2 push 0x5AA5A2
retn retn
draw: draw:
push 0x05AA529 push 0x05AA529
retn retn
} }
} }
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_unpack() override void post_unpack() override
{ {
cl_EnableCheats = game::Dvar_RegisterBool("cl_EnableCheats", false, game::DVAR_FLAG_NONE, "Enable FoF wallhack"); cl_EnableCheats = game::Dvar_RegisterBool(
"cl_EnableCheats", false, game::DVAR_NONE, "Enable FoF wallhack");
utils::hook::jump(0x430561, draw_red_box_stub); utils::hook::jump(0x430561, draw_red_box_stub);
utils::hook::nop(0x430566, 2); utils::hook::nop(0x430566, 2);
utils::hook::jump(0x5AA524, blind_eye_check_stub); utils::hook::jump(0x5AA524, blind_eye_check_stub);
add_cheat_commands(); add_cheat_commands();
} }
private: private:
static void add_cheat_commands() static void add_cheat_commands()
{ {
key_catcher::on_key_press("Z", [](const game::LocalClientNum_t&) key_catcher::on_key_press(
{ "Z",
game::Dvar_SetBool(cl_EnableCheats, true); []([[maybe_unused]] const game::LocalClientNum_t& local_client)
}); {
game::Dvar_SetBool(cl_EnableCheats, true);
});
key_catcher::on_key_press("X", [](const game::LocalClientNum_t&) key_catcher::on_key_press(
{ "X",
game::Dvar_SetBool(cl_EnableCheats, false); []([[maybe_unused]] const game::LocalClientNum_t& local_client)
}); {
game::Dvar_SetBool(cl_EnableCheats, false);
});
key_catcher::on_key_press("Y", [](const game::LocalClientNum_t&) key_catcher::on_key_press(
{ "Y",
command::execute(utils::string::va("cmd mr %i 2 allies", *game::serverId), true); []([[maybe_unused]] const game::LocalClientNum_t& local_client)
}); {
command::execute(
utils::string::va("cmd mr %i 2 allies", *game::serverId), true);
});
key_catcher::on_key_press("8", [](const game::LocalClientNum_t&) key_catcher::on_key_press(
{ "8",
command::execute(utils::string::va("cmd mr %i -1 endround", *game::serverId), true); []([[maybe_unused]] const game::LocalClientNum_t& local_client)
}); {
} command::execute(
}; utils::string::va("cmd mr %i -1 endround", *game::serverId),
} true);
});
}
};
} // namespace cheats
REGISTER_COMPONENT(cheats::component) REGISTER_COMPONENT(cheats::component)

View File

@ -6,134 +6,119 @@
#include "command.hpp" #include "command.hpp"
constexpr auto CMD_MAX_NESTING = 8;
namespace command namespace command
{ {
std::unordered_map<std::string, std::function<void(params&)>> handlers; std::unordered_map<std::string, std::function<void(params&)>> handlers;
void main_handler() void main_handler()
{ {
params params = {}; params params = {};
const auto command = utils::string::to_lower(params[0]); const auto command = utils::string::to_lower(params[0]);
if (handlers.find(command) != handlers.end()) if (!handlers.contains(command))
{ {
handlers[command](params); handlers[command](params);
} }
} }
params::params() params::params()
: nesting_(game::cmd_args->nesting) : nesting_(game::cmd_args->nesting)
{ {
} assert(game::cmd_args->nesting < CMD_MAX_NESTING);
}
int params::size() const int params::size() const
{ {
return game::cmd_args->argc[this->nesting_]; return game::cmd_args->argc[this->nesting_];
} }
const char* params::get(const int index) const const char* params::get(const int index) const
{ {
if (index >= this->size()) if (index >= this->size())
{ {
return ""; return "";
} }
return game::cmd_args->argv[this->nesting_][index]; return game::cmd_args->argv[this->nesting_][index];
} }
std::string params::join(const int index) const std::string params::join(const int index) const
{ {
std::string result = {}; std::string result = {};
for (auto i = index; i < this->size(); i++) for (auto i = index; i < this->size(); i++)
{ {
if (i > index) result.append(" "); if (i > index) result.append(" ");
result.append(this->get(i)); result.append(this->get(i));
} }
return result;
}
void add_raw(const char* name, void (*callback)()) return result;
{ }
game::Cmd_AddCommandInternal(name, callback, utils::memory::get_allocator()->allocate<game::cmd_function_t>());
}
void add(const char* name, const std::function<void(const params&)>& callback) void add_raw(const char* name, void (*callback)())
{ {
const auto command = utils::string::to_lower(name); game::Cmd_AddCommandInternal(
name,
callback,
utils::memory::get_allocator()->allocate<game::cmd_function_t>());
}
if (handlers.find(command) == handlers.end()) void add(const char* name, const std::function<void(const params&)>& callback)
{ {
add_raw(name, main_handler); const auto command = utils::string::to_lower(name);
}
handlers[command] = callback; if (!handlers.contains(command))
} {
add_raw(name, main_handler);
}
std::vector<std::string> script_commands; handlers[command] = callback;
utils::memory::allocator allocator; }
void add_script_command(const std::string& name, const std::function<void(const params&)>& callback) void add(const char* name, const std::function<void()>& callback)
{ {
script_commands.push_back(name); add(name,
const auto _name = allocator.duplicate_string(name); [callback](const params&)
add(_name, callback); {
} callback();
});
}
void clear_script_commands() void execute(std::string command, const bool sync)
{ {
for (const auto& name : script_commands) command += "\n";
{
handlers.erase(name);
game::Cmd_RemoveCommand(name.data());
}
allocator.clear(); if (sync)
script_commands.clear(); {
} game::Cmd_ExecuteSingleCommand(
game::LocalClientNum_t::LOCAL_CLIENT_0, 0, command.data());
}
else
{
game::Cbuf_AddText(game::LocalClientNum_t::LOCAL_CLIENT_0,
command.data());
}
}
void execute(std::string command, const bool sync) class component final : public component_interface
{ {
command += "\n"; public:
void post_unpack() override
{
add_commands_generic();
}
if (sync) private:
{ static void add_commands_generic()
game::Cmd_ExecuteSingleCommand(game::LocalClientNum_t::LOCAL_CLIENT_0, 0, command.data()); {
} // Will cause blue screen
else add("quit_meme", utils::nt::raise_hard_exception);
{
game::Cbuf_AddText(game::LocalClientNum_t::LOCAL_CLIENT_0, command.data());
}
}
class component final : public component_interface add("quit", game::Com_Quit_f);
{ }
public: };
void post_unpack() override } // namespace command
{
add_commands_generic();
}
void pre_destroy() override
{
clear_script_commands();
}
private:
static void add_commands_generic()
{
add("quit_meme", [](const params&)
{
// Will cause blue screen
utils::nt::raise_hard_exception();
});
add("dia_quit", [](const params&)
{
game::Com_Quit_f();
});
}
};
}
REGISTER_COMPONENT(command::component) REGISTER_COMPONENT(command::component)

View File

@ -2,29 +2,28 @@
namespace command namespace command
{ {
class params class params
{ {
public: public:
params(); params();
int size() const; [[nodiscard]] int size() const;
const char* get(int index) const; [[nodiscard]] const char* get(int index) const;
std::string join(int index) const; [[nodiscard]] std::string join(int index) const;
const char* operator[](const int index) const const char* operator[](const int index) const
{ {
return this->get(index); return this->get(index);
} }
private: private:
int nesting_; int nesting_;
}; };
void add_raw(const char* name, void (*callback)()); void add_raw(const char* name, void (*callback)());
void add(const char* name, const std::function<void(const params&)>& callback); void add(const char* name,
const std::function<void(const params&)>& callback);
void add(const char* name, const std::function<void()>& callback);
void add_script_command(const std::string& name, const std::function<void(const params&)>& callback); void execute(std::string command, bool sync = false);
void clear_script_commands(); } // namespace command
void execute(std::string command, bool sync = false);
}

View File

@ -4,49 +4,52 @@
namespace console namespace console
{ {
namespace namespace
{ {
std::thread thread; std::thread thread;
std::thread::id async_thread_id; std::thread::id async_thread_id;
LRESULT __stdcall sys_start_console(HWND, UINT, WPARAM, LPARAM) LRESULT __stdcall sys_start_console(HWND, UINT, WPARAM, LPARAM)
{ {
game::Sys_ShowConsole(); game::Sys_ShowConsole();
return 0; return 0;
} }
void console_unlock() void console_unlock()
{ {
const auto callBack = SetWindowLongA(*game::g_wv_hWnd, const auto callback =
GWL_WNDPROC, reinterpret_cast<LONG>(sys_start_console)); SetWindowLongA(*game::g_wv_hWnd,
GWL_WNDPROC,
reinterpret_cast<LONG>(sys_start_console));
SendMessage(*game::g_wv_hWnd, WM_QUIT, 0, 0); SendMessageA(*game::g_wv_hWnd, WM_QUIT, 0, 0);
SetWindowLongA(*game::g_wv_hWnd, GWL_WNDPROC, callBack); SetWindowLongA(*game::g_wv_hWnd, GWL_WNDPROC, callback);
} }
void show_console() void show_console()
{ {
if (*game::s_wcd_hWnd) if (*game::s_wcd_hWnd)
{ {
ShowWindow(*game::s_wcd_hWnd, SW_SHOW); ShowWindow(*game::s_wcd_hWnd, SW_SHOW);
} }
} }
} } // namespace
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_unpack() override void post_unpack() override
{ {
thread = std::thread([]() thread = std::thread(
{ []()
console_unlock(); {
show_console(); console_unlock();
}); show_console();
});
async_thread_id = thread.get_id(); async_thread_id = thread.get_id();
} }
}; };
} } // namespace console
REGISTER_COMPONENT(console::component) REGISTER_COMPONENT(console::component)

View File

@ -1,23 +1,20 @@
#include <stdinc.hpp> #include <stdinc.hpp>
#include "loader/component_loader.hpp"
#include <loader/component_loader.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
namespace dvar_patches namespace dvar_patches
{ {
void dvar_set_from_string_by_name_stub(const char*, const char*) void dvar_set_from_string_by_name_stub(const char*, const char*) {}
{
return;
}
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_unpack() override void post_unpack() override
{ {
utils::hook::call(0x59C0EF, dvar_set_from_string_by_name_stub); utils::hook::call(0x59C0EF, dvar_set_from_string_by_name_stub);
} }
}; };
} } // namespace dvar_patches
REGISTER_COMPONENT(dvar_patches::component) REGISTER_COMPONENT(dvar_patches::component)

View File

@ -8,111 +8,118 @@
namespace exploit namespace exploit
{ {
game::dvar_t* cl_exploit; game::dvar_t* cl_exploit;
/* /*
* void CL_Netchan_Transmit(netchan_t* chan, unsigned char* data, int a3) * void CL_Netchan_Transmit(netchan_t* chan, unsigned char* data, int a3)
* A brief description of data: the first few bytes contain information from clientConnection_t structure * A brief description of data: the first few bytes contain information from
* Offset 0: ServerID Size : 1 * clientConnection_t structure Offset 0: ServerID Size : 1 Offset 1:
* Offset 1: serverMessageSequence Size: 4 * serverMessageSequence Size: 4 Offset 5: serverCommandSequence Size: 4 One
* Offset 5: serverCommandSequence Size: 4 * clean way of sending invalid data to the server is to hook the functions
* One clean way of sending invalid data to the server is to hook the functions that write the info to the packet * that write the info to the packet Credit:
* Credit: https://stackoverflow.com/questions/58981714/how-do-i-change-the-value-of-a-single-byte-in-a-uint32-t-variable * https://stackoverflow.com/questions/58981714/how-do-i-change-the-value-of-a-single-byte-in-a-uint32-t-variable
*/ */
void write_message_sequence(game::msg_t* msg, int data) void write_message_sequence(game::msg_t* msg, int data)
{
if (msg->maxsize - static_cast<unsigned int>(msg->cursize) < sizeof(int))
{ {
if (msg->maxsize - msg->cursize < sizeof(int)) msg->overflowed = TRUE;
{ return;
msg->overflowed = TRUE;
return;
}
if (cl_exploit->current.enabled)
data = (data & 0xFFFFFF00) | 0xAAu;
auto dest = reinterpret_cast<int*>(&msg->data[msg->cursize]);
*dest = data;
msg->cursize += sizeof(int);
} }
void write_command_sequence(game::msg_t* msg, int data) if (cl_exploit->current.enabled) data = (data & 0xFFFFFF00) | 0xAAu;
auto* dest = reinterpret_cast<int*>(&msg->data[msg->cursize]);
*dest = data;
msg->cursize += sizeof(int);
}
void write_command_sequence(game::msg_t* msg, int data)
{
if (msg->maxsize - static_cast<unsigned int>(msg->cursize) < sizeof(int))
{ {
if (msg->maxsize - msg->cursize < sizeof(int)) msg->overflowed = TRUE;
{ return;
msg->overflowed = TRUE;
return;
}
if (cl_exploit->current.enabled)
data = (data & 0x00FFFFFF) | (0x80u << 24);
auto dest = reinterpret_cast<int*>(&msg->data[msg->cursize]);
*dest = data;
msg->cursize += sizeof(int);
} }
class component final : public component_interface if (cl_exploit->current.enabled) data = (data & 0x00FFFFFF) | (0x80u << 24);
auto* dest = reinterpret_cast<int*>(&msg->data[msg->cursize]);
*dest = data;
msg->cursize += sizeof(int);
}
class component final : public component_interface
{
public:
void post_unpack() override
{ {
public: cl_exploit = game::Dvar_RegisterBool(
void post_unpack() override "cl_exploit", false, game::DVAR_NONE, "Enable server freezer");
{
cl_exploit = game::Dvar_RegisterBool("cl_exploit", false, game::DVAR_FLAG_NONE, "Enable server freezer");
add_exploit_commands(); add_exploit_commands();
add_key_hooks(); add_key_hooks();
utils::hook::call(0x420B76, write_message_sequence); utils::hook::call(0x420B76, write_message_sequence);
utils::hook::call(0x420B86, write_command_sequence); utils::hook::call(0x420B86, write_command_sequence);
}
// Increase size; private:
utils::hook::set<DWORD>(0x4639C7, 0x1FFFCu); static void add_key_hooks()
utils::hook::set<DWORD>(0x496237, 0x1FFFCu); {
} key_catcher::on_key_press(
"O",
[]([[maybe_unused]] const game::LocalClientNum_t& local_client)
{
command::execute("exploit");
});
private: key_catcher::on_key_press(
static void add_key_hooks() "L",
{ []([[maybe_unused]] const game::LocalClientNum_t& local_client)
key_catcher::on_key_press("O", [](const game::LocalClientNum_t&) {
{ command::execute("undo_exploit");
command::execute("exploit"); });
});
key_catcher::on_key_press("L", [](const game::LocalClientNum_t&) key_catcher::on_key_press(
{ "K",
command::execute("undo_exploit"); []([[maybe_unused]] const game::LocalClientNum_t& local_client)
}); {
command::execute("disconnect");
});
}
key_catcher::on_key_press("K", [](const game::LocalClientNum_t&) static void add_exploit_commands()
{ {
command::execute("disconnect"); command::add("exploit",
}); []([[maybe_unused]] const command::params& params)
} {
game::Dvar_SetBool(cl_exploit, true);
});
static void add_exploit_commands() command::add("undo_exploit",
{ []([[maybe_unused]] const command::params& params)
command::add("exploit", [](const command::params&) {
{ game::Dvar_SetBool(cl_exploit, false);
game::Dvar_SetBool(cl_exploit, true); });
});
command::add("undo_exploit", [](const command::params&) command::add(
{ "send_command",
game::Dvar_SetBool(cl_exploit, false); []([[maybe_unused]] const command::params& params)
}); {
if (params.size() < 2) return;
command::add("send_command", [](const command::params& params) if (*game::connectionState <= game::connstate_t::CA_CHALLENGING)
{ return;
if (params.size() < 2) return;
if (*game::connectionState <= game::connstate_t::CA_CHALLENGING) const auto cmd = std::format("queryserverinfo ;{}", params.join(1));
return; game::NET_OutOfBandPrint(game::NS_SERVER,
game::localClientConnection->serverAddress,
const auto cmd = std::format("queryserverinfo ;{}", params.join(1)); cmd.data());
game::NET_OutOfBandPrint(game::NS_SERVER, game::localClientConnection->serverAddress, cmd.data()); });
}); }
} };
}; } // namespace exploit
}
REGISTER_COMPONENT(exploit::component) REGISTER_COMPONENT(exploit::component)

View File

@ -7,59 +7,61 @@
namespace key_catcher namespace key_catcher
{ {
utils::hook::detour cl_key_event_hook; utils::hook::detour cl_key_event_hook;
namespace namespace
{ {
std::unordered_map<std::string, callback>& get_key_callbacks() std::unordered_map<std::string, key_catcher::callback>& get_key_callbacks()
{ {
static std::unordered_map<std::string, callback> key_callbacks{}; static std::unordered_map<std::string, key_catcher::callback>
return key_callbacks; key_callbacks{};
} return key_callbacks;
}
void handle_key_event(game::LocalClientNum_t localClient, int keyID) void handle_key_event(game::LocalClientNum_t local_client, int key_id)
{ {
auto result = VkKeyScanA(static_cast<CHAR>(keyID)); const auto result = VkKeyScanA(static_cast<CHAR>(key_id));
auto VkKey = LOBYTE(result); const auto vk_key = LOBYTE(result);
auto& callbacks = get_key_callbacks(); const auto& callbacks = get_key_callbacks();
for (auto const& i : callbacks) for (auto const& [key, value] : callbacks)
{ {
auto gameVkKey = game::Key_StringToKeynum(i.first.data()); const auto game_vk_key = game::Key_StringToKeynum(key.data());
if (static_cast<BYTE>(gameVkKey) == VkKey) if (static_cast<BYTE>(game_vk_key) == vk_key)
{ {
i.second(localClient); value(local_client);
return; return;
} }
} }
} }
} } // namespace
void on_key_press(const std::string& command, const callback& callback) void on_key_press(const std::string& command, const callback& callback)
{ {
get_key_callbacks()[command] = callback; get_key_callbacks()[command] = callback;
} }
void cl_key_event_stub(game::LocalClientNum_t localClient, int keyID, int a3) void cl_key_event_stub(game::LocalClientNum_t local_client, int key_id,
{ int a3)
handle_key_event(localClient, keyID); {
handle_key_event(local_client, key_id);
cl_key_event_hook.invoke<void>(localClient, keyID, a3); cl_key_event_hook.invoke<void>(local_client, key_id, a3);
} }
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_unpack() override void post_unpack() override
{ {
cl_key_event_hook.create(0x4CD840, &cl_key_event_stub); cl_key_event_hook.create(0x4CD840, &cl_key_event_stub);
} }
void pre_destroy() override void pre_destroy() override
{ {
cl_key_event_hook.clear(); cl_key_event_hook.clear();
} }
}; };
} } // namespace key_catcher
REGISTER_COMPONENT(key_catcher::component) REGISTER_COMPONENT(key_catcher::component)

View File

@ -2,7 +2,7 @@
namespace key_catcher namespace key_catcher
{ {
using callback = std::function<void(game::LocalClientNum_t& localClient)>; using callback = std::function<void(game::LocalClientNum_t& local_client)>;
void on_key_press(const std::string& command, const callback& callback); void on_key_press(const std::string& command, const callback& callback);
} } // namespace key_catcher

View File

@ -9,67 +9,75 @@
namespace network namespace network
{ {
namespace namespace
{ {
std::unordered_map<std::string, callback>& get_callbacks() std::unordered_map<std::string, network::callback>& get_callbacks()
{ {
static std::unordered_map<std::string, callback> network_callbacks{}; static std::unordered_map<std::string, network::callback>
return network_callbacks; network_callbacks{};
} return network_callbacks;
}
bool handle_command(game::netadr_s* address, const char* command, game::msg_t* message) bool handle_command(game::netadr_s* address, const char* command,
{ game::msg_t* msg)
const auto cmd_string = utils::string::to_lower(command); {
auto& callbacks = get_callbacks(); const auto cmd_string = utils::string::to_lower(command);
const auto handler = callbacks.find(cmd_string); auto& callbacks = get_callbacks();
const auto handler = callbacks.find(cmd_string);
const auto offset = cmd_string.size() + 5;
if (handler == callbacks.end()) if (static_cast<unsigned int>(msg->cursize) < offset ||
{ handler == callbacks.end())
return false; {
} return false;
}
const auto offset = cmd_string.size() + 5; const std::string_view data(reinterpret_cast<char*>(msg->data) + offset,
const std::string_view data(reinterpret_cast<char*>(message->data) + offset, message->cursize - offset); msg->cursize - offset);
handler->second(*address, data); handler->second(*address, data);
return true; return true;
} }
} } // namespace
int packet_interception_handler(game::netadr_s* from, const char* command, game::msg_t* message) int packet_interception_handler(game::netadr_s* from, const char* command,
{ game::msg_t* message)
if (!handle_command(from, command, message)) {
{ if (!handle_command(from, command, message))
return reinterpret_cast<int (*)(game::netadr_s*, const char*, game::msg_t*)>(0x525730)(from, command, message); {
} return reinterpret_cast<int (*)(
game::netadr_s*, const char*, game::msg_t*)>(0x525730)(
from, command, message);
}
return TRUE; return TRUE;
} }
void on_packet(const std::string& command, const callback& callback) void on_packet(const std::string& command, const callback& callback)
{ {
get_callbacks()[utils::string::to_lower(command)] = callback; get_callbacks()[utils::string::to_lower(command)] = callback;
} }
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_unpack() override void post_unpack() override
{ {
add_network_commands(); add_network_commands();
utils::hook::call(0x5B27E1, packet_interception_handler); utils::hook::call(0x5B27E1, packet_interception_handler);
} }
private: private:
static void add_network_commands() static void add_network_commands()
{ {
on_packet("naughty_reply", [](const game::netadr_s&, const std::string_view&) on_packet("naughty_reply",
{ [](const game::netadr_s&, const std::string_view&)
command::execute("quit_meme"); {
}); command::execute("quit_meme");
} });
}; }
} };
} // namespace network
REGISTER_COMPONENT(network::component) REGISTER_COMPONENT(network::component)

View File

@ -2,7 +2,8 @@
namespace network namespace network
{ {
using callback = std::function<void(const game::netadr_s&, const std::string_view&)>; using callback =
std::function<void(const game::netadr_s&, const std::string_view&)>;
void on_packet(const std::string& command, const callback& callback); void on_packet(const std::string& command, const callback& callback);
} } // namespace network

View File

@ -1,34 +1,34 @@
#include <stdinc.hpp> #include <stdinc.hpp>
#include <loader/component_loader.hpp> #include <loader/component_loader.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
namespace remove_hooks namespace remove_hooks
{ {
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_unpack() override void post_unpack() override
{ {
remove_tekno_hooks(); remove_tekno_hooks();
} }
private: private:
static void remove_tekno_hooks() static void remove_tekno_hooks()
{ {
utils::hook::set<BYTE>(0x4E3D42, 0xE8); utils::hook::set<BYTE>(0x4E3D42, 0xE8);
utils::hook::set<BYTE>(0x4E3D43, 0xA9); utils::hook::set<BYTE>(0x4E3D43, 0xA9);
utils::hook::set<BYTE>(0x4E3D44, 0x25); utils::hook::set<BYTE>(0x4E3D44, 0x25);
utils::hook::set<BYTE>(0x4E3D45, 0xFE); utils::hook::set<BYTE>(0x4E3D45, 0xFE);
utils::hook::set<BYTE>(0x4E3D46, 0xFF); utils::hook::set<BYTE>(0x4E3D46, 0xFF);
utils::hook::set<BYTE>(0x6EA960, 0x55); utils::hook::set<BYTE>(0x6EA960, 0x55);
utils::hook::set<BYTE>(0x6EA961, 0x8B); utils::hook::set<BYTE>(0x6EA961, 0x8B);
utils::hook::set<BYTE>(0x6EA962, 0xEC); utils::hook::set<BYTE>(0x6EA962, 0xEC);
utils::hook::set<BYTE>(0x6EA963, 0x81); utils::hook::set<BYTE>(0x6EA963, 0x81);
utils::hook::set<BYTE>(0x6EA964, 0xEC); utils::hook::set<BYTE>(0x6EA964, 0xEC);
} }
}; };
} } // namespace remove_hooks
REGISTER_COMPONENT(remove_hooks::component) REGISTER_COMPONENT(remove_hooks::component)

View File

@ -1,170 +1,186 @@
#include <stdinc.hpp> #include <stdinc.hpp>
#include "loader/component_loader.hpp" #include <loader/component_loader.hpp>
#include "utils/concurrency.hpp" #include <utils/concurrency.hpp>
#include "utils/hook.hpp" #include <utils/hook.hpp>
#include "scheduler.hpp" #include "scheduler.hpp"
namespace scheduler namespace scheduler
{ {
std::thread::id async_thread_id; std::thread::id async_thread_id;
namespace namespace
{ {
struct task struct task
{ {
std::function<bool()> handler{}; std::function<bool()> handler{};
std::chrono::milliseconds interval{}; std::chrono::milliseconds interval{};
std::chrono::high_resolution_clock::time_point last_call{}; std::chrono::high_resolution_clock::time_point last_call{};
}; };
using task_list = std::vector<task>; using task_list = std::vector<task>;
class task_pipeline class task_pipeline
{ {
public: public:
void add(task&& task) void add(task&& task)
{ {
new_callbacks_.access([&task, this](task_list& tasks) new_callbacks_.access(
{ [&task, this](task_list& tasks)
tasks.emplace_back(std::move(task)); {
}); tasks.emplace_back(std::move(task));
} });
}
void clear() void clear()
{ {
callbacks_.access([&](task_list& tasks) callbacks_.access(
{ [&](task_list& tasks)
this->merge_callbacks(); {
tasks.clear(); this->merge_callbacks();
}); tasks.clear();
} });
}
void execute() void execute()
{ {
callbacks_.access([&](task_list& tasks) callbacks_.access(
{ [&](task_list& tasks)
this->merge_callbacks(); {
this->merge_callbacks();
for (auto i = tasks.begin(); i != tasks.end();) for (auto i = tasks.begin(); i != tasks.end();)
{ {
const auto now = std::chrono::high_resolution_clock::now(); const auto now = std::chrono::high_resolution_clock::now();
const auto diff = now - i->last_call; const auto diff = now - i->last_call;
if (diff < i->interval) if (diff < i->interval)
{ {
++i; ++i;
continue; continue;
} }
i->last_call = now; i->last_call = now;
const auto res = i->handler(); const auto res = i->handler();
if (res == cond_end) if (res == cond_end)
{ {
i = tasks.erase(i); i = tasks.erase(i);
} }
else else
{ {
++i; ++i;
} }
} }
}); });
} }
private: private:
utils::concurrency::container<task_list> new_callbacks_; utils::concurrency::container<task_list> new_callbacks_;
utils::concurrency::container<task_list, std::recursive_mutex> callbacks_; utils::concurrency::container<task_list, std::recursive_mutex> callbacks_;
void merge_callbacks() void merge_callbacks()
{ {
callbacks_.access([&](task_list& tasks) callbacks_.access(
{ [&](task_list& tasks)
new_callbacks_.access([&](task_list& new_tasks) {
{ new_callbacks_.access(
tasks.insert(tasks.end(), std::move_iterator<task_list::iterator>(new_tasks.begin()), std::move_iterator<task_list::iterator>(new_tasks.end())); [&](task_list& new_tasks)
new_tasks = {}; {
}); tasks.insert(tasks.end(),
}); std::move_iterator<task_list::iterator>(
} new_tasks.begin()),
}; std::move_iterator<task_list::iterator>(
new_tasks.end()));
new_tasks = {};
});
});
}
};
std::thread thread; std::thread thread;
task_pipeline pipelines[pipeline::count]; task_pipeline pipelines[pipeline::count];
void execute(const pipeline type) void execute(const pipeline type)
{ {
assert(type >= 0 && type < pipeline::count); assert(type >= 0 && type < pipeline::count);
pipelines[type].execute(); pipelines[type].execute();
} }
void cl_frame_stub(game::LocalClientNum_t local) void cl_frame_stub(game::LocalClientNum_t local)
{ {
reinterpret_cast<void (*)(game::LocalClientNum_t)>(0x41C9B0)(local); reinterpret_cast<void (*)(game::LocalClientNum_t)>(0x41C9B0)(local);
execute(pipeline::client); execute(pipeline::client);
} }
} } // namespace
void clear_tasks(const pipeline type) void clear_tasks(const pipeline type)
{ {
return pipelines[type].clear(); return pipelines[type].clear();
} }
void schedule(const std::function<bool()>& callback, const pipeline type, void schedule(const std::function<bool()>& callback, const pipeline type,
const std::chrono::milliseconds delay) const std::chrono::milliseconds delay)
{ {
assert(type >= 0 && type < pipeline::count); assert(type >= 0 && type < pipeline::count);
task task; task task;
task.handler = callback; task.handler = callback;
task.interval = delay; task.interval = delay;
task.last_call = std::chrono::high_resolution_clock::now(); task.last_call = std::chrono::high_resolution_clock::now();
pipelines[type].add(std::move(task)); pipelines[type].add(std::move(task));
} }
void loop(const std::function<void()>& callback, const pipeline type, void loop(const std::function<void()>& callback, const pipeline type,
const std::chrono::milliseconds delay) const std::chrono::milliseconds delay)
{ {
schedule([callback]() schedule(
{ [callback]()
callback(); {
return cond_continue; callback();
}, type, delay); return cond_continue;
} },
type,
delay);
}
void once(const std::function<void()>& callback, const pipeline type, void once(const std::function<void()>& callback, const pipeline type,
const std::chrono::milliseconds delay) const std::chrono::milliseconds delay)
{ {
schedule([callback]() schedule(
{ [callback]()
callback(); {
return cond_end; callback();
}, type, delay); return cond_end;
} },
type,
delay);
}
unsigned int thread_id; unsigned int thread_id;
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_unpack() override void post_unpack() override
{ {
thread = std::thread([]() thread = std::thread(
{ []()
while (true) {
{ while (true)
execute(pipeline::async); {
std::this_thread::sleep_for(10ms); execute(pipeline::async);
} std::this_thread::sleep_for(10ms);
}); }
});
async_thread_id = thread.get_id(); async_thread_id = thread.get_id();
utils::hook::call(0x4E4A0D, cl_frame_stub); utils::hook::call(0x4E4A0D, cl_frame_stub);
} }
}; };
} } // namespace scheduler
REGISTER_COMPONENT(scheduler::component) REGISTER_COMPONENT(scheduler::component)

View File

@ -2,24 +2,27 @@
namespace scheduler namespace scheduler
{ {
extern std::thread::id async_thread_id; extern std::thread::id async_thread_id;
enum pipeline enum pipeline
{ {
client, client,
async, async,
count, count,
}; };
static const bool cond_continue = false; static const bool cond_continue = false;
static const bool cond_end = true; static const bool cond_end = true;
void clear_tasks(const pipeline type); void clear_tasks(const pipeline type);
void schedule(const std::function<bool()>& callback, pipeline type = pipeline::client, void schedule(const std::function<bool()>& callback,
std::chrono::milliseconds delay = 0ms); pipeline type = pipeline::client,
void loop(const std::function<void()>& callback, pipeline type = pipeline::client, std::chrono::milliseconds delay = 0ms);
std::chrono::milliseconds delay = 0ms); void loop(const std::function<void()>& callback,
void once(const std::function<void()>& callback, pipeline type = pipeline::client, pipeline type = pipeline::client,
std::chrono::milliseconds delay = 0ms); std::chrono::milliseconds delay = 0ms);
} void once(const std::function<void()>& callback,
pipeline type = pipeline::client,
std::chrono::milliseconds delay = 0ms);
} // namespace scheduler

View File

@ -1,52 +1,53 @@
#include <stdinc.hpp> #include <stdinc.hpp>
#include <loader/component_loader.hpp> #include <loader/component_loader.hpp>
#include "utils/hook.hpp"
#include "utils/info_string.hpp" #include <utils/hook.hpp>
#include "utils/string.hpp" #include <utils/info_string.hpp>
#include <utils/string.hpp>
#include "scheduler.hpp" #include "scheduler.hpp"
namespace user_info namespace user_info
{ {
namespace namespace
{ {
int a1 = 0; int a1 = 0;
void cl_check_user_info(int _a1, int force) void cl_check_user_info(int _a1, const int force)
{ {
a1 = _a1; a1 = _a1;
if (*game::connectionState <= game::connstate_t::CA_CHALLENGING) if (*game::connectionState <= game::connstate_t::CA_CHALLENGING) return;
return;
if (game::cl_paused->current.enabled && !force) if (game::cl_paused->current.enabled && !force) return;
return;
const std::string infoString = game::Dvar_InfoString(_a1, 0x200); const std::string info_string = game::Dvar_InfoString(_a1, 0x200);
utils::info_string info(infoString); utils::info_string info(info_string);
const auto colorCode = rand() % 10; const auto color_code = std::rand() % 10;
char name[16]; char name[16];
const auto& numbers = std::to_string(rand() % 10000); const auto numbers = std::to_string(std::rand() % 10000);
_snprintf_s(name, sizeof(name), _TRUNCATE, "^%d%s", colorCode, numbers.data()); _snprintf_s(
name, sizeof(name), _TRUNCATE, "^%d%s", color_code, numbers.data());
info.set("name", name); info.set("name", name);
info.set("ec_usingTag", "1"); info.set("ec_usingTag", "1");
info.set("ec_TagText", utils::string::va("^%dGG", colorCode)); info.set("ec_TagText", utils::string::va("^%dGG", color_code));
const auto& bigTitle = std::to_string(rand() % 512); const auto big_title = std::to_string(std::rand() % 512);
info.set("ec_TitleBg", bigTitle); info.set("ec_TitleBg", big_title);
game::CL_AddReliableCommand(_a1, utils::string::va("userinfo \"%s\"", info.build().data())); game::CL_AddReliableCommand(
} _a1, utils::string::va("userinfo \"%s\"", info.build().data()));
}
__declspec(naked) void cl_check_user_info_stub() __declspec(naked) void cl_check_user_info_stub()
{ {
__asm __asm
{ {
pushad pushad
push 0 push 0
@ -56,23 +57,26 @@ namespace user_info
popad popad
ret ret
} }
} }
} } // namespace
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_unpack() override void post_unpack() override
{ {
utils::hook::call(0x41CA53, cl_check_user_info_stub); utils::hook::call(0x41CA53, cl_check_user_info_stub);
scheduler::loop([] scheduler::loop(
{ []
cl_check_user_info(a1, TRUE); {
}, scheduler::pipeline::client, 4s); cl_check_user_info(a1, TRUE);
} },
}; scheduler::pipeline::client,
} 4s);
}
};
} // namespace user_info
REGISTER_COMPONENT(user_info::component) REGISTER_COMPONENT(user_info::component)

View File

@ -1,21 +1,17 @@
#include <stdinc.hpp> #include <stdinc.hpp>
#include "loader/component_loader.hpp" #include "loader/component_loader.hpp"
BOOL APIENTRY DllMain(HMODULE /*hModule*/, BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call,
DWORD ul_reason_for_call,
LPVOID /*lpReserved*/ LPVOID /*lpReserved*/
) ) {
{ if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) std::srand(uint32_t(time(nullptr)));
{ component_loader::post_unpack();
srand(uint32_t(time(nullptr))); }
component_loader::post_unpack();
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH) else if (ul_reason_for_call == DLL_PROCESS_DETACH) {
{ component_loader::pre_destroy();
component_loader::pre_destroy(); }
}
return TRUE; return TRUE;
} }

View File

@ -1,5 +1,3 @@
#include <stdinc.hpp> #include <stdinc.hpp>
namespace game namespace game {}
{
}

View File

@ -1,34 +1,19 @@
#pragma once #pragma once
namespace game namespace game {
{ template <typename T> class symbol {
template <typename T> public:
class symbol symbol(const size_t dedi) : dedi_(reinterpret_cast<T*>(dedi)) {}
{
public:
symbol(const size_t dedi)
: dedi_(reinterpret_cast<T*>(dedi))
{
}
T* get() const T* get() const { return dedi_; }
{
return dedi_;
}
operator T* () const operator T*() const { return this->get(); }
{
return this->get();
}
T* operator->() const T* operator->() const { return this->get(); }
{
return this->get();
}
private: private:
T* dedi_; T* dedi_;
}; };
} } // namespace game
#include "symbols.hpp" #include "symbols.hpp"

File diff suppressed because it is too large Load Diff

View File

@ -2,66 +2,78 @@
#define WEAK __declspec(selectany) #define WEAK __declspec(selectany)
namespace game namespace game {
{ // Functions
// Functions WEAK symbol<void()> Sys_ShowConsole{0x515CD0};
WEAK symbol<void()> Sys_ShowConsole{0x515CD0}; WEAK symbol<void(HINSTANCE__*)> Sys_CreateConsole{0x51B770};
WEAK symbol<void(HINSTANCE__*)> Sys_CreateConsole{0x51B770}; WEAK symbol<void(const char* fmt, ...)> Sys_Error{0x434000};
WEAK symbol<void(const char* fmt, ...)> Sys_Error{0x434000}; WEAK symbol<void(const char* text)> Conbuf_AppendText{0x4F7300};
WEAK symbol<void(const char* text)> Conbuf_AppendText{0x4F7300}; WEAK symbol<void(errorParm_t, const char* fmt, ...)> Com_Error{0x4A6660};
WEAK symbol<void(errorParm_t, const char* fmt, ...)> Com_Error{0x4A6660}; WEAK symbol<const char*(int index)> ConcatArgs{0x539060};
WEAK symbol<const char*(int index)> ConcatArgs{0x539060}; WEAK symbol<void(LocalClientNum_t, const char* text)> Cbuf_AddText{0x4C1030};
WEAK symbol<void(LocalClientNum_t, const char* text)> Cbuf_AddText{0x4C1030}; WEAK symbol<void(LocalClientNum_t, const char* text)> Cbuf_InsertText{0x429920};
WEAK symbol<void(LocalClientNum_t, const char* text)> Cbuf_InsertText{0x429920}; WEAK symbol<void(const char* cmdName, void(), cmd_function_t* cmd)>
WEAK symbol<void(const char* cmdName, void(), cmd_function_t* cmd)> Cmd_AddCommandInternal{0x537E70}; Cmd_AddCommandInternal{0x537E70};
WEAK symbol<void(LocalClientNum_t, int, const char* text)> Cmd_ExecuteSingleCommand{0x4EB8F0}; WEAK symbol<void(LocalClientNum_t, int, const char* text)>
WEAK symbol<void(const char* cmdName)> Cmd_RemoveCommand{0x4EAF30}; Cmd_ExecuteSingleCommand{0x4EB8F0};
WEAK symbol<const char*(int index)> Cmd_Argv{0x5580E0}; WEAK symbol<void(const char* cmdName)> Cmd_RemoveCommand{0x4EAF30};
WEAK symbol<const char*(int index)> Cmd_Argv{0x5580E0};
WEAK symbol<dvar_t*(const char*)> Dvar_FindVar{0x4EBB50}; WEAK symbol<dvar_t*(const char*)> Dvar_FindVar{0x4EBB50};
WEAK symbol<dvar_t*(const char* dvarName, int value, int min, int max, unsigned __int16 flags, const char* desc)> WEAK symbol<dvar_t*(const char* dvarName, int value, int min, int max,
Dvar_RegisterInt{0x50C760}; unsigned __int16 flags, const char* desc)>
WEAK symbol<dvar_t*(const char* dvarName, bool value, unsigned __int16 flags, const char* description)> Dvar_RegisterInt{0x50C760};
Dvar_RegisterBool{0x4A3300}; WEAK symbol<dvar_t*(const char* dvarName, bool value, unsigned __int16 flags,
WEAK symbol<dvar_t*(const char* dvarName, const char* value, unsigned __int16 flags, const char* description)> const char* description)>
Dvar_RegisterString{0x4157E0}; Dvar_RegisterBool{0x4A3300};
WEAK symbol<dvar_t*(const char* dvarName, float value, float min, float max, unsigned __int16 flags, const char* description)> WEAK symbol<dvar_t*(const char* dvarName, const char* value,
Dvar_RegisterFloat{0x4A5CF0}; unsigned __int16 flags, const char* description)>
WEAK symbol<void(dvar_t* var, bool value)> Dvar_SetBool{0x46DD70}; Dvar_RegisterString{0x4157E0};
WEAK symbol<void(const char* dvarName, bool value)> Dvar_SetBoolByName{0x48C7D0}; WEAK symbol<dvar_t*(const char* dvarName, float value, float min, float max,
WEAK symbol<const char*(int, int)> Dvar_InfoString{0x4028C0}; unsigned __int16 flags, const char* description)>
Dvar_RegisterFloat{0x4A5CF0};
WEAK symbol<void(dvar_t* var, bool value)> Dvar_SetBool{0x46DD70};
WEAK symbol<void(const char* dvarName, bool value)> Dvar_SetBoolByName{
0x48C7D0};
WEAK symbol<const char*(int, int)> Dvar_InfoString{0x4028C0};
WEAK symbol<int(const char* cmd)> Key_GetBindingForCmd{0x47D300}; WEAK symbol<int(const char* cmd)> Key_GetBindingForCmd{0x47D300};
WEAK symbol<int(const char* keyAsText)> Key_StringToKeynum{0x50A710}; // Virtual-Key Code WEAK symbol<int(const char* keyAsText)> Key_StringToKeynum{
WEAK symbol<void(LocalClientNum_t, int, int)> Key_SetBinding{0x50B770}; 0x50A710}; // Virtual-Key Code
WEAK symbol<void(LocalClientNum_t, int, int)> Key_SetBinding{0x50B770};
WEAK symbol<void(int arg, char* buffer, int bufferLength)> SV_Cmd_ArgvBuffer{0x4F6B00}; WEAK symbol<void(int arg, char* buffer, int bufferLength)> SV_Cmd_ArgvBuffer{
0x4F6B00};
WEAK symbol<bool(netsrc_t, netadr_s dest, const char* message)> NET_OutOfBandPrint{0x496230}; WEAK symbol<bool(netsrc_t, netadr_s dest, const char* message)>
WEAK symbol<bool(netsrc_t, netadr_s dest, unsigned char* data, int size)> NET_OutOfBandData{0x4639C0}; NET_OutOfBandPrint{0x496230};
WEAK symbol<int(unsigned int, void*, netadr_s)> dwSendTo{0x673B20}; WEAK symbol<bool(netsrc_t, netadr_s dest, unsigned char* data, int size)>
WEAK symbol<void(netadr_s*, sockaddr*)> NetadrToSockadr{0x48B460}; NET_OutOfBandData{0x4639C0};
WEAK symbol<int(const char* serverName, netadr_s serverRemote)> NET_StringToAdr{0x4E09A0}; WEAK symbol<int(unsigned int, void*, netadr_s)> dwSendTo{0x673B20};
WEAK symbol<SOCKET> query_socket{0x5A861EC}; WEAK symbol<void(netadr_s*, sockaddr*)> NetadrToSockadr{0x48B460};
WEAK symbol<void()> Com_Quit_f{0x556060}; WEAK symbol<int(const char* serverName, netadr_s serverRemote)> NET_StringToAdr{
0x4E09A0};
WEAK symbol<SOCKET> query_socket{0x5A861EC};
WEAK symbol<void()> Com_Quit_f{0x556060};
WEAK symbol<void(const msg_t*, unsigned char*, int)> MSG_Init{0x40E030}; WEAK symbol<void(const msg_t*, unsigned char*, int)> MSG_Init{0x40E030};
WEAK symbol<void(const msg_t*, const char*)> MSG_WriteString{0x42A560}; WEAK symbol<void(const msg_t*, const char*)> MSG_WriteString{0x42A560};
WEAK symbol<void(const msg_t*, unsigned __int64)> MSG_WriteInt64{0x4906B0}; WEAK symbol<void(const msg_t*, unsigned __int64)> MSG_WriteInt64{0x4906B0};
WEAK symbol<void(const msg_t*, int)> MSG_WriteShort{0x4ACD80}; WEAK symbol<void(const msg_t*, int)> MSG_WriteShort{0x4ACD80};
WEAK symbol<void(const msg_t*, const void*, int)> MSG_WriteData{0x4F8C20}; WEAK symbol<void(const msg_t*, const void*, int)> MSG_WriteData{0x4F8C20};
WEAK symbol<void(int, const char*)> CL_AddReliableCommand{0x4EE3A0}; WEAK symbol<void(int, const char*)> CL_AddReliableCommand{0x4EE3A0};
WEAK symbol<unsigned __int64()> LiveSteam_GetUid{0x4A4050}; WEAK symbol<unsigned __int64()> LiveSteam_GetUid{0x4A4050};
WEAK symbol<int(unsigned __int64, const void*, unsigned int)> LiveSteam_Client_ConnectToSteamServer{0x4D6980}; WEAK symbol<int(unsigned __int64, const void*, unsigned int)>
LiveSteam_Client_ConnectToSteamServer{0x4D6980};
// Variables // Variables
WEAK symbol<CmdArgs> cmd_args{0x1C96850}; WEAK symbol<CmdArgs> cmd_args{0x1C96850};
WEAK symbol<PlayerKeyState> playerKeys{0xB3A38C}; WEAK symbol<PlayerKeyState> playerKeys{0xB3A38C};
WEAK symbol<clientConnection_t> localClientConnection{0xB3D360}; WEAK symbol<clientConnection_t> localClientConnection{0xB3D360};
WEAK symbol<HWND> g_wv_hWnd{0x5A86AF0}; WEAK symbol<HWND> g_wv_hWnd{0x5A86AF0};
WEAK symbol<HWND> s_wcd_hWnd{0x5A86330}; WEAK symbol<HWND> s_wcd_hWnd{0x5A86330};
WEAK symbol<int> serverId{0xFF5058}; WEAK symbol<int> serverId{0xFF5058};
WEAK symbol<connstate_t> connectionState{0x1060214}; WEAK symbol<connstate_t> connectionState{0x1060214};
WEAK symbol<dvar_t> cl_paused{0x1CE6190}; WEAK symbol<dvar_t> cl_paused{0x1CE6190};
} } // namespace game

View File

@ -1,35 +1,21 @@
#pragma once #pragma once
class component_interface class component_interface {
{
public: public:
virtual ~component_interface() virtual ~component_interface() {}
{
}
virtual void post_start() virtual void post_start() {}
{
}
virtual void post_load() virtual void post_load() {}
{
}
virtual void pre_destroy() virtual void pre_destroy() {}
{
}
virtual void post_unpack() virtual void post_unpack() {}
{
}
virtual void* load_import([[maybe_unused]] const std::string& library, [[maybe_unused]] const std::string& function) virtual void* load_import([[maybe_unused]] const std::string& library,
{ [[maybe_unused]] const std::string& function) {
return nullptr; return nullptr;
} }
virtual bool is_supported() virtual bool is_supported() { return true; }
{
return true;
}
}; };

View File

@ -1,127 +1,111 @@
#include <stdinc.hpp> #include <stdinc.hpp>
#include "component_loader.hpp" #include "component_loader.hpp"
void component_loader::register_component(std::unique_ptr<component_interface>&& component_) void component_loader::register_component(
{ std::unique_ptr<component_interface>&& component_) {
get_components().push_back(std::move(component_)); get_components().push_back(std::move(component_));
} }
bool component_loader::post_start() bool component_loader::post_start() {
{ static auto handled = false;
static auto handled = false; if (handled)
if (handled) return true; return true;
handled = true; handled = true;
try try {
{ for (const auto& component_ : get_components()) {
for (const auto& component_ : get_components()) component_->post_start();
{ }
component_->post_start(); } catch (premature_shutdown_trigger&) {
} return false;
} }
catch (premature_shutdown_trigger&)
{
return false;
}
return true; return true;
} }
bool component_loader::post_load() bool component_loader::post_load() {
{ static auto handled = false;
static auto handled = false; if (handled)
if (handled) return true; return true;
handled = true; handled = true;
clean(); clean();
try try {
{ for (const auto& component_ : get_components()) {
for (const auto& component_ : get_components()) component_->post_load();
{ }
component_->post_load(); } catch (premature_shutdown_trigger&) {
} return false;
} }
catch (premature_shutdown_trigger&)
{
return false;
}
return true; return true;
} }
void component_loader::post_unpack() void component_loader::post_unpack() {
{ static auto handled = false;
static auto handled = false; if (handled)
if (handled) return; return;
handled = true; handled = true;
for (const auto& component_ : get_components()) for (const auto& component_ : get_components()) {
{ component_->post_unpack();
component_->post_unpack(); }
}
} }
void component_loader::pre_destroy() void component_loader::pre_destroy() {
{ static auto handled = false;
static auto handled = false; if (handled)
if (handled) return; return;
handled = true; handled = true;
for (const auto& component_ : get_components()) for (const auto& component_ : get_components()) {
{ component_->pre_destroy();
component_->pre_destroy(); }
}
} }
void component_loader::clean() void component_loader::clean() {
{ auto& components = get_components();
auto& components = get_components(); for (auto i = components.begin(); i != components.end();) {
for (auto i = components.begin(); i != components.end();) if (!(*i)->is_supported()) {
{ (*i)->pre_destroy();
if (!(*i)->is_supported()) i = components.erase(i);
{ } else {
(*i)->pre_destroy(); ++i;
i = components.erase(i); }
} }
else
{
++i;
}
}
} }
void* component_loader::load_import(const std::string& library, const std::string& function) void* component_loader::load_import(const std::string& library,
{ const std::string& function) {
void* function_ptr = nullptr; void* function_ptr = nullptr;
for (const auto& component_ : get_components()) for (const auto& component_ : get_components()) {
{ auto* const component_function_ptr =
auto* const component_function_ptr = component_->load_import(library, function); component_->load_import(library, function);
if (component_function_ptr) if (component_function_ptr) {
{ function_ptr = component_function_ptr;
function_ptr = component_function_ptr; }
} }
}
return function_ptr; return function_ptr;
} }
void component_loader::trigger_premature_shutdown() void component_loader::trigger_premature_shutdown() {
{ throw premature_shutdown_trigger();
throw premature_shutdown_trigger();
} }
std::vector<std::unique_ptr<component_interface>>& component_loader::get_components() std::vector<std::unique_ptr<component_interface>>&
{ component_loader::get_components() {
using component_vector = std::vector<std::unique_ptr<component_interface>>; using component_vector = std::vector<std::unique_ptr<component_interface>>;
using component_vector_container = std::unique_ptr<component_vector, std::function<void(component_vector*)>>; using component_vector_container =
std::unique_ptr<component_vector, std::function<void(component_vector*)>>;
static component_vector_container components(new component_vector, [](component_vector* component_vector) static component_vector_container components(
{ new component_vector, [](component_vector* component_vector) {
pre_destroy(); pre_destroy();
delete component_vector; delete component_vector;
}); });
return *components; return *components;
} }

View File

@ -1,61 +1,51 @@
#pragma once #pragma once
#include "component_interface.hpp" #include "component_interface.hpp"
class component_loader final class component_loader final {
{
public: public:
class premature_shutdown_trigger final : public std::exception class premature_shutdown_trigger final : public std::exception {
{ [[nodiscard]] const char* what() const noexcept override {
[[nodiscard]] const char* what() const noexcept override return "Premature shutdown requested";
{ }
return "Premature shutdown requested"; };
}
};
template <typename T> template <typename T> class installer final {
class installer final static_assert(std::is_base_of<component_interface, T>::value,
{ "component has invalid base class");
static_assert(std::is_base_of<component_interface, T>::value, "component has invalid base class");
public: public:
installer() installer() { register_component(std::make_unique<T>()); }
{ };
register_component(std::make_unique<T>());
}
};
template <typename T> template <typename T> static T* get() {
static T* get() for (const auto& component_ : get_components()) {
{ if (typeid(*component_.get()) == typeid(T)) {
for (const auto& component_ : get_components()) return reinterpret_cast<T*>(component_.get());
{ }
if (typeid(*component_.get()) == typeid(T)) }
{
return reinterpret_cast<T*>(component_.get());
}
}
return nullptr; return nullptr;
} }
static void register_component(std::unique_ptr<component_interface>&& component); static void
register_component(std::unique_ptr<component_interface>&& component);
static bool post_start(); static bool post_start();
static bool post_load(); static bool post_load();
static void post_unpack(); static void post_unpack();
static void pre_destroy(); static void pre_destroy();
static void clean(); static void clean();
static void* load_import(const std::string& library, const std::string& function); static void* load_import(const std::string& library,
const std::string& function);
static void trigger_premature_shutdown(); static void trigger_premature_shutdown();
private: private:
static std::vector<std::unique_ptr<component_interface>>& get_components(); static std::vector<std::unique_ptr<component_interface>>& get_components();
}; };
#define REGISTER_COMPONENT(name) \ #define REGISTER_COMPONENT(name) \
namespace \ namespace { \
{ \ static component_loader::installer<name> __component; \
static component_loader::installer<name> __component; \ }
}

View File

@ -4,23 +4,21 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <WinSock2.h> #include <WinSock2.h>
#include <windows.h>
#include <vector> #include <algorithm>
#include <cassert> #include <cassert>
#include <functional>
#include <iostream>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <iostream>
#include <algorithm>
#include <functional>
#include <unordered_set>
#include <map>
#include <vector>
#pragma comment(lib, "ntdll.lib") #pragma comment(lib, "ntdll.lib")
using namespace std::literals; using namespace std::literals;
// clang-format off
#include "game/structs.hpp" #include "game/structs.hpp"
#include "game/game.hpp" #include "game/game.hpp"
// clang-format on

View File

@ -2,45 +2,35 @@
#include <mutex> #include <mutex>
namespace utils::concurrency namespace utils::concurrency {
{ template <typename T, typename MutexType = std::mutex> class container {
template <typename T, typename MutexType = std::mutex> public:
class container template <typename R = void, typename F> R access(F&& accessor) const {
{ std::lock_guard<MutexType> _{mutex_};
public: return accessor(object_);
template <typename R = void, typename F> }
R access(F&& accessor) const
{
std::lock_guard<MutexType> _{mutex_};
return accessor(object_);
}
template <typename R = void, typename F> template <typename R = void, typename F> R access(F&& accessor) {
R access(F&& accessor) std::lock_guard<MutexType> _{mutex_};
{ return accessor(object_);
std::lock_guard<MutexType> _{mutex_}; }
return accessor(object_);
}
template <typename R = void, typename F> template <typename R = void, typename F>
R access_with_lock(F&& accessor) const R access_with_lock(F&& accessor) const {
{ std::unique_lock<MutexType> lock{mutex_};
std::unique_lock<MutexType> lock{mutex_}; return accessor(object_, lock);
return accessor(object_, lock); }
}
template <typename R = void, typename F> template <typename R = void, typename F> R access_with_lock(F&& accessor) {
R access_with_lock(F&& accessor) std::unique_lock<MutexType> lock{mutex_};
{ return accessor(object_, lock);
std::unique_lock<MutexType> lock{mutex_}; }
return accessor(object_, lock);
}
T& get_raw() { return object_; } T& get_raw() { return object_; }
const T& get_raw() const { return object_; } const T& get_raw() const { return object_; }
private: private:
mutable MutexType mutex_{}; mutable MutexType mutex_{};
T object_{}; T object_{};
}; };
} } // namespace utils::concurrency

View File

@ -5,191 +5,154 @@
#include <MinHook.h> #include <MinHook.h>
namespace utils::hook namespace utils::hook {
{ namespace {
namespace [[maybe_unused]] class _ {
{ public:
[[maybe_unused]] class _ _() {
{ if (MH_Initialize() != MH_OK) {
public: throw std::runtime_error("Failed to initialize MinHook");
_() }
{ }
if (MH_Initialize() != MH_OK)
{
throw std::runtime_error("Failed to initialize MinHook");
}
}
~_() ~_() { MH_Uninitialize(); }
{ } __;
MH_Uninitialize(); } // namespace
}
} __;
}
detour::detour(const size_t place, void* target) : detour(reinterpret_cast<void*>(place), target) detour::detour(const size_t place, void* target)
{ : detour(reinterpret_cast<void*>(place), target) {}
}
detour::detour(void* place, void* target) detour::detour(void* place, void* target) { this->create(place, target); }
{
this->create(place, target);
}
detour::~detour() detour::~detour() { this->clear(); }
{
this->clear();
}
void detour::enable() const void detour::enable() const { MH_EnableHook(this->place_); }
{
MH_EnableHook(this->place_);
}
void detour::disable() const void detour::disable() const { MH_DisableHook(this->place_); }
{
MH_DisableHook(this->place_);
}
void detour::create(void* place, void* target) void detour::create(void* place, void* target) {
{ this->clear();
this->clear(); this->place_ = place;
this->place_ = place;
if (MH_CreateHook(this->place_, target, &this->original_) != MH_OK) if (MH_CreateHook(this->place_, target, &this->original_) != MH_OK) {
{ throw std::runtime_error(
throw std::runtime_error(string::va("Unable to create hook at location: %p", this->place_)); string::va("Unable to create hook at location: %p", this->place_));
} }
this->enable(); this->enable();
}
void detour::create(const size_t place, void* target)
{
this->create(reinterpret_cast<void*>(place), target);
}
void detour::clear()
{
if (this->place_)
{
MH_RemoveHook(this->place_);
}
this->place_ = nullptr;
this->original_ = nullptr;
}
void* detour::get_original() const
{
return this->original_;
}
void nop(void* place, const size_t length)
{
DWORD old_protect{};
VirtualProtect(place, length, PAGE_EXECUTE_READWRITE, &old_protect);
std::memset(place, 0x90, length);
VirtualProtect(place, length, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), place, length);
}
void nop(const size_t place, const size_t length)
{
nop(reinterpret_cast<void*>(place), length);
}
void copy(void* place, const void* data, const size_t length)
{
DWORD old_protect{};
VirtualProtect(place, length, PAGE_EXECUTE_READWRITE, &old_protect);
std::memmove(place, data, length);
VirtualProtect(place, length, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), place, length);
}
void copy(const size_t place, const void* data, const size_t length)
{
copy(reinterpret_cast<void*>(place), data, length);
}
bool is_relatively_far(const void* pointer, const void* data, const int offset)
{
const int64_t diff = size_t(data) - (size_t(pointer) + offset);
const auto small_diff = int32_t(diff);
return diff != int64_t(small_diff);
}
void call(void* pointer, void* data)
{
if (is_relatively_far(pointer, data))
{
throw std::runtime_error("Too far away to create 32bit relative branch");
}
auto* patch_pointer = PBYTE(pointer);
set<uint8_t>(patch_pointer, 0xE8);
set<int32_t>(patch_pointer + 1, int32_t(size_t(data) - (size_t(pointer) + 5)));
}
void call(const size_t pointer, void* data)
{
return call(reinterpret_cast<void*>(pointer), data);
}
void call(const size_t pointer, const size_t data)
{
return call(pointer, reinterpret_cast<void*>(data));
}
void set(std::uintptr_t address, std::vector<std::uint8_t>&& bytes)
{
DWORD oldProtect = 0;
auto* place = reinterpret_cast<void*>(address);
VirtualProtect(place, bytes.size(), PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy(place, bytes.data(), bytes.size());
VirtualProtect(place, bytes.size(), oldProtect, &oldProtect);
FlushInstructionCache(GetCurrentProcess(), place, bytes.size());
}
void set(std::uintptr_t address, void* buffer, size_t size)
{
DWORD oldProtect = 0;
auto* place = reinterpret_cast<void*>(address);
VirtualProtect(place, size, PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy(place, buffer, size);
VirtualProtect(place, size, oldProtect, &oldProtect);
FlushInstructionCache(GetCurrentProcess(), place, size);
}
void jump(std::uintptr_t address, void* destination)
{
if (!address) return;
std::uint8_t* bytes = new std::uint8_t[5];
*bytes = 0xE9;
*reinterpret_cast<std::uint32_t*>(bytes + 1) = CalculateRelativeJMPAddress(address, destination);
set(address, bytes, 5);
delete[] bytes;
}
void redirect_jump(void* pointer, void* data)
{
char* operand_ptr = static_cast<char*>(pointer) + 2;
int new_operand = reinterpret_cast<int>(data) - (reinterpret_cast<int>(pointer) + 6);
set<int>(operand_ptr, new_operand);
}
void redirect_jump(size_t pointer, void* data)
{
redirect_jump(reinterpret_cast<void*>(pointer), data);
}
} }
void detour::create(const size_t place, void* target) {
this->create(reinterpret_cast<void*>(place), target);
}
void detour::clear() {
if (this->place_) {
MH_RemoveHook(this->place_);
}
this->place_ = nullptr;
this->original_ = nullptr;
}
void* detour::get_original() const { return this->original_; }
void nop(void* place, const size_t length) {
DWORD old_protect{};
VirtualProtect(place, length, PAGE_EXECUTE_READWRITE, &old_protect);
std::memset(place, 0x90, length);
VirtualProtect(place, length, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), place, length);
}
void nop(const size_t place, const size_t length) {
nop(reinterpret_cast<void*>(place), length);
}
void copy(void* place, const void* data, const size_t length) {
DWORD old_protect{};
VirtualProtect(place, length, PAGE_EXECUTE_READWRITE, &old_protect);
std::memmove(place, data, length);
VirtualProtect(place, length, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), place, length);
}
void copy(const size_t place, const void* data, const size_t length) {
copy(reinterpret_cast<void*>(place), data, length);
}
bool is_relatively_far(const void* pointer, const void* data,
const int offset) {
const int64_t diff = size_t(data) - (size_t(pointer) + offset);
const auto small_diff = int32_t(diff);
return diff != int64_t(small_diff);
}
void call(void* pointer, void* data) {
if (is_relatively_far(pointer, data)) {
throw std::runtime_error("Too far away to create 32bit relative branch");
}
auto* patch_pointer = PBYTE(pointer);
set<uint8_t>(patch_pointer, 0xE8);
set<int32_t>(patch_pointer + 1,
int32_t(size_t(data) - (size_t(pointer) + 5)));
}
void call(const size_t pointer, void* data) {
return call(reinterpret_cast<void*>(pointer), data);
}
void call(const size_t pointer, const size_t data) {
return call(pointer, reinterpret_cast<void*>(data));
}
void set(std::uintptr_t address, std::vector<std::uint8_t>&& bytes) {
DWORD oldProtect = 0;
auto* place = reinterpret_cast<void*>(address);
VirtualProtect(place, bytes.size(), PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy(place, bytes.data(), bytes.size());
VirtualProtect(place, bytes.size(), oldProtect, &oldProtect);
FlushInstructionCache(GetCurrentProcess(), place, bytes.size());
}
void set(std::uintptr_t address, void* buffer, size_t size) {
DWORD oldProtect = 0;
auto* place = reinterpret_cast<void*>(address);
VirtualProtect(place, size, PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy(place, buffer, size);
VirtualProtect(place, size, oldProtect, &oldProtect);
FlushInstructionCache(GetCurrentProcess(), place, size);
}
void jump(std::uintptr_t address, void* destination) {
if (!address)
return;
std::uint8_t* bytes = new std::uint8_t[5];
*bytes = 0xE9;
*reinterpret_cast<std::uint32_t*>(bytes + 1) =
CalculateRelativeJMPAddress(address, destination);
set(address, bytes, 5);
delete[] bytes;
}
void redirect_jump(void* pointer, void* data) {
char* operand_ptr = static_cast<char*>(pointer) + 2;
int new_operand =
reinterpret_cast<int>(data) - (reinterpret_cast<int>(pointer) + 6);
set<int>(operand_ptr, new_operand);
}
void redirect_jump(size_t pointer, void* data) {
redirect_jump(reinterpret_cast<void*>(pointer), data);
}
} // namespace utils::hook

View File

@ -1,120 +1,102 @@
#pragma once #pragma once
#include "signature.hpp" #include "signature.hpp"
#define CalculateRelativeJMPAddress(X, Y) (((std::uintptr_t)Y - (std::uintptr_t)X) - 5) #define CalculateRelativeJMPAddress(X, Y) \
(((std::uintptr_t)Y - (std::uintptr_t)X) - 5)
namespace utils::hook namespace utils::hook {
{ class detour {
class detour public:
{ detour() = default;
public: detour(void* place, void* target);
detour() = default; detour(size_t place, void* target);
detour(void* place, void* target); ~detour();
detour(size_t place, void* target);
~detour();
detour(detour&& other) noexcept detour(detour&& other) noexcept { this->operator=(std::move(other)); }
{
this->operator=(std::move(other));
}
detour& operator= (detour&& other) noexcept detour& operator=(detour&& other) noexcept {
{ if (this != &other) {
if (this != &other) this->~detour();
{
this->~detour();
this->place_ = other.place_; this->place_ = other.place_;
this->original_ = other.original_; this->original_ = other.original_;
other.place_ = nullptr; other.place_ = nullptr;
other.original_ = nullptr; other.original_ = nullptr;
} }
return *this; return *this;
} }
detour(const detour&) = delete; detour(const detour&) = delete;
detour& operator= (const detour&) = delete; detour& operator=(const detour&) = delete;
void enable() const; void enable() const;
void disable() const; void disable() const;
void create(void* place, void* target); void create(void* place, void* target);
void create(size_t place, void* target); void create(size_t place, void* target);
void clear(); void clear();
template <typename T> template <typename T> T* get() const {
T* get() const return static_cast<T*>(this->get_original());
{ }
return static_cast<T*>(this->get_original());
}
template <typename T, typename... Args> template <typename T, typename... Args> T invoke(Args... args) {
T invoke(Args... args) return static_cast<T (*)(Args...)>(this->get_original())(args...);
{ }
return static_cast<T(*)(Args ...)>(this->get_original())(args...);
}
[[nodiscard]] void* get_original() const; [[nodiscard]] void* get_original() const;
private: private:
void* place_{}; void* place_{};
void* original_{}; void* original_{};
}; };
void nop(void* place, size_t length); void nop(void* place, size_t length);
void nop(size_t place, size_t length); void nop(size_t place, size_t length);
void copy(void* place, const void* data, size_t length); void copy(void* place, const void* data, size_t length);
void copy(size_t place, const void* data, size_t length); void copy(size_t place, const void* data, size_t length);
bool is_relatively_far(const void* pointer, const void* data, int offset = 5); bool is_relatively_far(const void* pointer, const void* data, int offset = 5);
void call(void* pointer, void* data); void call(void* pointer, void* data);
void call(size_t pointer, void* data); void call(size_t pointer, void* data);
void call(size_t pointer, size_t data); void call(size_t pointer, size_t data);
void jump(std::uintptr_t address, void* destination); void jump(std::uintptr_t address, void* destination);
void redirect_jump(void* pointer, void* data); void redirect_jump(void* pointer, void* data);
void redirect_jump(size_t pointer, void* data); void redirect_jump(size_t pointer, void* data);
template <typename T> template <typename T> T extract(void* address) {
T extract(void* address) const auto data = static_cast<uint8_t*>(address);
{ const auto offset = *reinterpret_cast<int32_t*>(data);
const auto data = static_cast<uint8_t*>(address); return reinterpret_cast<T>(data + offset + 4);
const auto offset = *reinterpret_cast<int32_t*>(data);
return reinterpret_cast<T>(data + offset + 4);
}
template <typename T>
static void set(void* place, T value)
{
DWORD old_protect;
VirtualProtect(place, sizeof(T), PAGE_EXECUTE_READWRITE, &old_protect);
*static_cast<T*>(place) = value;
VirtualProtect(place, sizeof(T), old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), place, sizeof(T));
}
template <typename T>
static void set(const size_t place, T value)
{
return set<T>(reinterpret_cast<void*>(place), value);
}
template <typename T, typename... Args>
static T invoke(size_t func, Args ... args)
{
return reinterpret_cast<T(*)(Args ...)>(func)(args...);
}
template <typename T, typename... Args>
static T invoke(void* func, Args ... args)
{
return static_cast<T(*)(Args ...)>(func)(args...);
}
} }
template <typename T> static void set(void* place, T value) {
DWORD old_protect;
VirtualProtect(place, sizeof(T), PAGE_EXECUTE_READWRITE, &old_protect);
*static_cast<T*>(place) = value;
VirtualProtect(place, sizeof(T), old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), place, sizeof(T));
}
template <typename T> static void set(const size_t place, T value) {
return set<T>(reinterpret_cast<void*>(place), value);
}
template <typename T, typename... Args>
static T invoke(size_t func, Args... args) {
return reinterpret_cast<T (*)(Args...)>(func)(args...);
}
template <typename T, typename... Args>
static T invoke(void* func, Args... args) {
return static_cast<T (*)(Args...)>(func)(args...);
}
} // namespace utils::hook

View File

@ -3,64 +3,50 @@
#include "info_string.hpp" #include "info_string.hpp"
#include "string.hpp" #include "string.hpp"
namespace utils namespace utils {
{ info_string::info_string(const std::string& buffer) { this->parse(buffer); }
info_string::info_string(const std::string& buffer)
{
this->parse(buffer);
}
info_string::info_string(const std::string_view& buffer) info_string::info_string(const std::string_view& buffer)
: info_string(std::string{buffer}) : info_string(std::string{buffer}) {}
{
}
void info_string::set(const std::string& key, const std::string& value) void info_string::set(const std::string& key, const std::string& value) {
{ this->key_value_pairs_[key] = value;
this->key_value_pairs_[key] = value;
}
std::string info_string::get(const std::string& key) const
{
const auto value = this->key_value_pairs_.find(key);
if (value != this->key_value_pairs_.end())
{
return value->second;
}
return "";
}
void info_string::parse(std::string buffer)
{
if (buffer[0] == '\\')
{
buffer = buffer.substr(1);
}
auto key_values = string::split(buffer, '\\');
for (size_t i = 0; !key_values.empty() && i < (key_values.size() - 1); i += 2)
{
const auto& key = key_values[i];
const auto& value = key_values[i + 1];
this->key_value_pairs_[key] = value;
}
}
std::string info_string::build() const
{
//auto first = true;
std::string info_string;
for (auto i = this->key_value_pairs_.begin(); i != this->key_value_pairs_.end(); ++i)
{
//if (first) first = false;
/*else*/ info_string.append("\\");
info_string.append(i->first); // Key
info_string.append("\\");
info_string.append(i->second); // Value
}
return info_string;
}
} }
std::string info_string::get(const std::string& key) const {
const auto value = this->key_value_pairs_.find(key);
if (value != this->key_value_pairs_.end()) {
return value->second;
}
return {};
}
void info_string::parse(std::string buffer) {
if (buffer[0] == '\\') {
buffer = buffer.substr(1);
}
const auto key_values = string::split(buffer, '\\');
for (size_t i = 0; !key_values.empty() && i < (key_values.size() - 1);
i += 2) {
const auto& key = key_values[i];
const auto& value = key_values[i + 1];
this->key_value_pairs_[key] = value;
}
}
std::string info_string::build() const {
std::string info_string;
for (auto i = this->key_value_pairs_.begin();
i != this->key_value_pairs_.end(); ++i) {
info_string.append("\\");
info_string.append(i->first); // Key
info_string.append("\\");
info_string.append(i->second); // Value
}
return info_string;
}
} // namespace utils

View File

@ -3,22 +3,20 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
namespace utils namespace utils {
{ class info_string {
class info_string public:
{ info_string() = default;
public: explicit info_string(const std::string& buffer);
info_string() = default; explicit info_string(const std::string_view& buffer);
info_string(const std::string& buffer);
info_string(const std::string_view& buffer);
void set(const std::string& key, const std::string& value); void set(const std::string& key, const std::string& value);
std::string get(const std::string& key) const; [[nodiscard]] std::string get(const std::string& key) const;
std::string build() const; [[nodiscard]] std::string build() const;
private: private:
std::unordered_map<std::string, std::string> key_value_pairs_{}; std::unordered_map<std::string, std::string> key_value_pairs_;
void parse(std::string buffer); void parse(std::string buffer);
}; };
} } // namespace utils

View File

@ -3,165 +3,134 @@
#include "memory.hpp" #include "memory.hpp"
#include "nt.hpp" #include "nt.hpp"
namespace utils namespace utils {
{ memory::allocator memory::mem_allocator_;
memory::allocator memory::mem_allocator_;
memory::allocator::~allocator() memory::allocator::~allocator() { this->clear(); }
{
this->clear();
}
void memory::allocator::clear() void memory::allocator::clear() {
{ std::lock_guard _(this->mutex_);
std::lock_guard _(this->mutex_);
for (auto& data : this->pool_) for (const auto& data : this->pool_) {
{ memory::free(data);
memory::free(data); }
}
this->pool_.clear(); this->pool_.clear();
}
void memory::allocator::free(void* data)
{
std::lock_guard _(this->mutex_);
const auto j = std::find(this->pool_.begin(), this->pool_.end(), data);
if (j != this->pool_.end())
{
memory::free(data);
this->pool_.erase(j);
}
}
void memory::allocator::free(const void* data)
{
this->free(const_cast<void*>(data));
}
void* memory::allocator::allocate(const size_t length)
{
std::lock_guard _(this->mutex_);
const auto data = memory::allocate(length);
this->pool_.push_back(data);
return data;
}
bool memory::allocator::empty() const
{
return this->pool_.empty();
}
char* memory::allocator::duplicate_string(const std::string& string)
{
std::lock_guard _(this->mutex_);
const auto data = memory::duplicate_string(string);
this->pool_.push_back(data);
return data;
}
void* memory::allocate(const size_t length)
{
return calloc(length, 1);
}
char* memory::duplicate_string(const std::string& string)
{
const auto new_string = allocate_array<char>(string.size() + 1);
std::memcpy(new_string, string.data(), string.size());
return new_string;
}
void memory::free(void* data)
{
if (data)
{
::free(data);
}
}
void memory::free(const void* data)
{
free(const_cast<void*>(data));
}
bool memory::is_set(const void* mem, const char chr, const size_t length)
{
const auto mem_arr = static_cast<const char*>(mem);
for (size_t i = 0; i < length; ++i)
{
if (mem_arr[i] != chr)
{
return false;
}
}
return true;
}
bool memory::is_bad_read_ptr(const void* ptr)
{
MEMORY_BASIC_INFORMATION mbi = {};
if (VirtualQuery(ptr, &mbi, sizeof(mbi)))
{
const DWORD mask = (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READ |
PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY);
auto b = !(mbi.Protect & mask);
// check the page is not a guard page
if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) b = true;
return b;
}
return true;
}
bool memory::is_bad_code_ptr(const void* ptr)
{
MEMORY_BASIC_INFORMATION mbi = {};
if (VirtualQuery(ptr, &mbi, sizeof(mbi)))
{
const DWORD mask = (PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY);
auto b = !(mbi.Protect & mask);
// check the page is not a guard page
if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) b = true;
return b;
}
return true;
}
bool memory::is_rdata_ptr(void* pointer)
{
const std::string rdata = ".rdata";
const auto pointer_lib = utils::nt::library::get_by_address(pointer);
for (const auto& section : pointer_lib.get_section_headers())
{
const auto size = sizeof(section->Name);
char name[size + 1];
name[size] = 0;
std::memcpy(name, section->Name, size);
if (name == rdata)
{
const auto target = size_t(pointer);
const size_t source_start = size_t(pointer_lib.get_ptr()) + section->PointerToRawData;
const size_t source_end = source_start + section->SizeOfRawData;
return target >= source_start && target <= source_end;
}
}
return false;
}
memory::allocator* memory::get_allocator()
{
return &memory::mem_allocator_;
}
} }
void memory::allocator::free(void* data) {
std::lock_guard _(this->mutex_);
const auto j = std::find(this->pool_.begin(), this->pool_.end(), data);
if (j != this->pool_.end()) {
memory::free(data);
this->pool_.erase(j);
}
}
void memory::allocator::free(const void* data) {
this->free(const_cast<void*>(data));
}
void* memory::allocator::allocate(const size_t length) {
std::lock_guard _(this->mutex_);
const auto data = memory::allocate(length);
this->pool_.push_back(data);
return data;
}
bool memory::allocator::empty() const { return this->pool_.empty(); }
char* memory::allocator::duplicate_string(const std::string& string) {
std::lock_guard _(this->mutex_);
const auto data = memory::duplicate_string(string);
this->pool_.push_back(data);
return data;
}
void* memory::allocate(const size_t length) { return calloc(length, 1); }
char* memory::duplicate_string(const std::string& string) {
const auto new_string = allocate_array<char>(string.size() + 1);
std::memcpy(new_string, string.data(), string.size());
return new_string;
}
void memory::free(void* data) {
if (data) {
::free(data);
}
}
void memory::free(const void* data) { free(const_cast<void*>(data)); }
bool memory::is_set(const void* mem, const char chr, const size_t length) {
const auto mem_arr = static_cast<const char*>(mem);
for (size_t i = 0; i < length; ++i) {
if (mem_arr[i] != chr) {
return false;
}
}
return true;
}
bool memory::is_bad_read_ptr(const void* ptr) {
MEMORY_BASIC_INFORMATION mbi = {};
if (VirtualQuery(ptr, &mbi, sizeof(mbi))) {
const DWORD mask =
(PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READ |
PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY);
auto b = !(mbi.Protect & mask);
// check the page is not a guard page
if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS))
b = true;
return b;
}
return true;
}
bool memory::is_bad_code_ptr(const void* ptr) {
MEMORY_BASIC_INFORMATION mbi = {};
if (VirtualQuery(ptr, &mbi, sizeof(mbi))) {
const DWORD mask =
(PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY);
auto b = !(mbi.Protect & mask);
// check the page is not a guard page
if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS))
b = true;
return b;
}
return true;
}
bool memory::is_rdata_ptr(void* ptr) {
const std::string rdata = ".rdata";
const auto pointer_lib = utils::nt::library::get_by_address(ptr);
for (const auto& section : pointer_lib.get_section_headers()) {
constexpr auto size = sizeof(section->Name);
char name[size + 1];
name[size] = 0;
std::memcpy(name, section->Name, size);
if (name == rdata) {
const auto target = size_t(ptr);
const size_t source_start =
size_t(pointer_lib.get_ptr()) + section->PointerToRawData;
const size_t source_end = source_start + section->SizeOfRawData;
return target >= source_start && target <= source_end;
}
}
return false;
}
memory::allocator* memory::get_allocator() { return &memory::mem_allocator_; }
} // namespace utils

View File

@ -3,73 +3,58 @@
#include <mutex> #include <mutex>
#include <vector> #include <vector>
namespace utils namespace utils {
{ class memory final {
class memory final public:
{ class allocator final {
public: public:
class allocator final ~allocator();
{
public:
~allocator();
void clear(); void clear();
void free(void* data); void free(void* data);
void free(const void* data); void free(const void* data);
void* allocate(size_t length); void* allocate(size_t length);
template <typename T> template <typename T> T* allocate() { return this->allocate_array<T>(1); }
inline T* allocate()
{
return this->allocate_array<T>(1);
}
template <typename T> template <typename T> T* allocate_array(const size_t count = 1) {
inline T* allocate_array(const size_t count = 1) return static_cast<T*>(this->allocate(count * sizeof(T)));
{ }
return static_cast<T*>(this->allocate(count * sizeof(T)));
}
bool empty() const; bool empty() const;
char* duplicate_string(const std::string& string); char* duplicate_string(const std::string& string);
private: private:
std::mutex mutex_; std::mutex mutex_;
std::vector<void*> pool_; std::vector<void*> pool_;
}; };
static void* allocate(size_t length); static void* allocate(size_t length);
template <typename T> template <typename T> static T* allocate() { return allocate_array<T>(1); }
static inline T* allocate()
{
return allocate_array<T>(1);
}
template <typename T> template <typename T> static T* allocate_array(const size_t count = 1) {
static inline T* allocate_array(const size_t count = 1) return static_cast<T*>(allocate(count * sizeof(T)));
{ }
return static_cast<T*>(allocate(count * sizeof(T)));
}
static char* duplicate_string(const std::string& string); static char* duplicate_string(const std::string& string);
static void free(void* data); static void free(void* data);
static void free(const void* data); static void free(const void* data);
static bool is_set(const void* mem, char chr, size_t length); static bool is_set(const void* mem, char chr, size_t length);
static bool is_bad_read_ptr(const void* ptr); static bool is_bad_read_ptr(const void* ptr);
static bool is_bad_code_ptr(const void* ptr); static bool is_bad_code_ptr(const void* ptr);
static bool is_rdata_ptr(void* ptr); static bool is_rdata_ptr(void* ptr);
static allocator* get_allocator(); static allocator* get_allocator();
private: private:
static allocator mem_allocator_; static allocator mem_allocator_;
}; };
} } // namespace utils

View File

@ -2,255 +2,241 @@
#include "nt.hpp" #include "nt.hpp"
namespace utils::nt namespace utils::nt {
{ library library::load(const std::string& name) {
library library::load(const std::string& name) return library(LoadLibraryA(name.data()));
{
return library(LoadLibraryA(name.data()));
}
library library::load(const std::filesystem::path& path)
{
return library::load(path.generic_string());
}
library library::get_by_address(void* address)
{
HMODULE handle = nullptr;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, static_cast<LPCSTR>(address), &handle);
return library(handle);
}
library::library()
{
this->module_ = GetModuleHandleA(nullptr);
}
library::library(const std::string& name)
{
this->module_ = GetModuleHandleA(name.data());
}
library::library(const HMODULE handle)
{
this->module_ = handle;
}
bool library::operator==(const library& obj) const
{
return this->module_ == obj.module_;
}
library::operator bool() const
{
return this->is_valid();
}
library::operator HMODULE() const
{
return this->get_handle();
}
PIMAGE_NT_HEADERS library::get_nt_headers() const
{
if (!this->is_valid()) return nullptr;
return reinterpret_cast<PIMAGE_NT_HEADERS>(this->get_ptr() + this->get_dos_header()->e_lfanew);
}
PIMAGE_DOS_HEADER library::get_dos_header() const
{
return reinterpret_cast<PIMAGE_DOS_HEADER>(this->get_ptr());
}
PIMAGE_OPTIONAL_HEADER library::get_optional_header() const
{
if (!this->is_valid()) return nullptr;
return &this->get_nt_headers()->OptionalHeader;
}
std::vector<PIMAGE_SECTION_HEADER> library::get_section_headers() const
{
std::vector<PIMAGE_SECTION_HEADER> headers;
auto nt_headers = this->get_nt_headers();
auto section = IMAGE_FIRST_SECTION(nt_headers);
for (uint16_t i = 0; i < nt_headers->FileHeader.NumberOfSections; ++i, ++section)
{
if (section) headers.push_back(section);
else OutputDebugStringA("There was an invalid section :O");
}
return headers;
}
std::uint8_t* library::get_ptr() const
{
return reinterpret_cast<std::uint8_t*>(this->module_);
}
void library::unprotect() const
{
if (!this->is_valid()) return;
DWORD protection;
VirtualProtect(this->get_ptr(), this->get_optional_header()->SizeOfImage, PAGE_EXECUTE_READWRITE,
&protection);
}
size_t library::get_relative_entry_point() const
{
if (!this->is_valid()) return 0;
return this->get_nt_headers()->OptionalHeader.AddressOfEntryPoint;
}
void* library::get_entry_point() const
{
if (!this->is_valid()) return nullptr;
return this->get_ptr() + this->get_relative_entry_point();
}
bool library::is_valid() const
{
return this->module_ != nullptr && this->get_dos_header()->e_magic == IMAGE_DOS_SIGNATURE;
}
std::string library::get_name() const
{
if (!this->is_valid()) return "";
auto path = this->get_path();
const auto pos = path.find_last_of("/\\");
if (pos == std::string::npos) return path;
return path.substr(pos + 1);
}
std::string library::get_path() const
{
if (!this->is_valid()) return "";
char name[MAX_PATH] = {0};
GetModuleFileNameA(this->module_, name, sizeof name);
return name;
}
std::string library::get_folder() const
{
if (!this->is_valid()) return "";
const auto path = std::filesystem::path(this->get_path());
return path.parent_path().generic_string();
}
void library::free()
{
if (this->is_valid())
{
FreeLibrary(this->module_);
this->module_ = nullptr;
}
}
HMODULE library::get_handle() const
{
return this->module_;
}
void** library::get_iat_entry(const std::string& module_name, const std::string& proc_name) const
{
if (!this->is_valid()) return nullptr;
const library other_module(module_name);
if (!other_module.is_valid()) return nullptr;
auto* const target_function = other_module.get_proc<void*>(proc_name);
if (!target_function) return nullptr;
auto* header = this->get_optional_header();
if (!header) return nullptr;
auto* import_descriptor = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(this->get_ptr() + header->DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while (import_descriptor->Name)
{
if (!_stricmp(reinterpret_cast<char*>(this->get_ptr() + import_descriptor->Name), module_name.data()))
{
auto* original_thunk_data = reinterpret_cast<PIMAGE_THUNK_DATA>(import_descriptor->
OriginalFirstThunk + this->get_ptr());
auto* thunk_data = reinterpret_cast<PIMAGE_THUNK_DATA>(import_descriptor->FirstThunk + this->
get_ptr());
while (original_thunk_data->u1.AddressOfData)
{
const size_t ordinal_number = original_thunk_data->u1.AddressOfData & 0xFFFFFFF;
if (ordinal_number > 0xFFFF) continue;
if (GetProcAddress(other_module.module_, reinterpret_cast<char*>(ordinal_number)) ==
target_function)
{
return reinterpret_cast<void**>(&thunk_data->u1.Function);
}
++original_thunk_data;
++thunk_data;
}
//break;
}
++import_descriptor;
}
return nullptr;
}
void raise_hard_exception()
{
int data = false;
const library ntdll("ntdll.dll");
ntdll.invoke_pascal<void>("RtlAdjustPrivilege", 19, true, false, &data);
ntdll.invoke_pascal<void>("NtRaiseHardError", 0xC000007B, 0, nullptr, nullptr, 6, &data);
}
std::string load_resource(const int id)
{
auto* const res = FindResource(library(), MAKEINTRESOURCE(id), RT_RCDATA);
if (!res) return {};
auto* const handle = LoadResource(nullptr, res);
if (!handle) return {};
return std::string(LPSTR(LockResource(handle)), SizeofResource(nullptr, res));
}
void relaunch_self()
{
const utils::nt::library self;
STARTUPINFOA startup_info;
PROCESS_INFORMATION process_info;
ZeroMemory(&startup_info, sizeof(startup_info));
ZeroMemory(&process_info, sizeof(process_info));
startup_info.cb = sizeof(startup_info);
char current_dir[MAX_PATH];
GetCurrentDirectoryA(sizeof(current_dir), current_dir);
auto* const command_line = GetCommandLineA();
CreateProcessA(self.get_path().data(), command_line, nullptr, nullptr, false, NULL, nullptr, current_dir,
&startup_info, &process_info);
if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) CloseHandle(process_info.hThread);
if (process_info.hProcess && process_info.hProcess != INVALID_HANDLE_VALUE) CloseHandle(process_info.hProcess);
}
void terminate(const uint32_t code)
{
TerminateProcess(GetCurrentProcess(), code);
}
} }
library library::load(const std::filesystem::path& path) {
return library::load(path.generic_string());
}
library library::get_by_address(void* address) {
HMODULE handle = nullptr;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
static_cast<LPCSTR>(address), &handle);
return library(handle);
}
library::library() { this->module_ = GetModuleHandleA(nullptr); }
library::library(const std::string& name) {
this->module_ = GetModuleHandleA(name.data());
}
library::library(const HMODULE handle) { this->module_ = handle; }
bool library::operator==(const library& obj) const {
return this->module_ == obj.module_;
}
library::operator bool() const { return this->is_valid(); }
library::operator HMODULE() const { return this->get_handle(); }
PIMAGE_NT_HEADERS library::get_nt_headers() const {
if (!this->is_valid())
return nullptr;
return reinterpret_cast<PIMAGE_NT_HEADERS>(this->get_ptr() +
this->get_dos_header()->e_lfanew);
}
PIMAGE_DOS_HEADER library::get_dos_header() const {
return reinterpret_cast<PIMAGE_DOS_HEADER>(this->get_ptr());
}
PIMAGE_OPTIONAL_HEADER library::get_optional_header() const {
if (!this->is_valid())
return nullptr;
return &this->get_nt_headers()->OptionalHeader;
}
std::vector<PIMAGE_SECTION_HEADER> library::get_section_headers() const {
std::vector<PIMAGE_SECTION_HEADER> headers;
auto nt_headers = this->get_nt_headers();
auto section = IMAGE_FIRST_SECTION(nt_headers);
for (uint16_t i = 0; i < nt_headers->FileHeader.NumberOfSections;
++i, ++section) {
if (section)
headers.push_back(section);
else
OutputDebugStringA("There was an invalid section :O");
}
return headers;
}
std::uint8_t* library::get_ptr() const {
return reinterpret_cast<std::uint8_t*>(this->module_);
}
void library::unprotect() const {
if (!this->is_valid())
return;
DWORD protection;
VirtualProtect(this->get_ptr(), this->get_optional_header()->SizeOfImage,
PAGE_EXECUTE_READWRITE, &protection);
}
size_t library::get_relative_entry_point() const {
if (!this->is_valid())
return 0;
return this->get_nt_headers()->OptionalHeader.AddressOfEntryPoint;
}
void* library::get_entry_point() const {
if (!this->is_valid())
return nullptr;
return this->get_ptr() + this->get_relative_entry_point();
}
bool library::is_valid() const {
return this->module_ != nullptr &&
this->get_dos_header()->e_magic == IMAGE_DOS_SIGNATURE;
}
std::string library::get_name() const {
if (!this->is_valid())
return "";
auto path = this->get_path();
const auto pos = path.find_last_of("/\\");
if (pos == std::string::npos)
return path;
return path.substr(pos + 1);
}
std::string library::get_path() const {
if (!this->is_valid())
return "";
char name[MAX_PATH] = {0};
GetModuleFileNameA(this->module_, name, sizeof name);
return name;
}
std::string library::get_folder() const {
if (!this->is_valid())
return "";
const auto path = std::filesystem::path(this->get_path());
return path.parent_path().generic_string();
}
void library::free() {
if (this->is_valid()) {
FreeLibrary(this->module_);
this->module_ = nullptr;
}
}
HMODULE library::get_handle() const { return this->module_; }
void** library::get_iat_entry(const std::string& module_name,
const std::string& proc_name) const {
if (!this->is_valid())
return nullptr;
const library other_module(module_name);
if (!other_module.is_valid())
return nullptr;
auto* const target_function = other_module.get_proc<void*>(proc_name);
if (!target_function)
return nullptr;
auto* header = this->get_optional_header();
if (!header)
return nullptr;
auto* import_descriptor = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(
this->get_ptr() +
header->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while (import_descriptor->Name) {
if (!_stricmp(
reinterpret_cast<char*>(this->get_ptr() + import_descriptor->Name),
module_name.data())) {
auto* original_thunk_data = reinterpret_cast<PIMAGE_THUNK_DATA>(
import_descriptor->OriginalFirstThunk + this->get_ptr());
auto* thunk_data = reinterpret_cast<PIMAGE_THUNK_DATA>(
import_descriptor->FirstThunk + this->get_ptr());
while (original_thunk_data->u1.AddressOfData) {
const size_t ordinal_number =
original_thunk_data->u1.AddressOfData & 0xFFFFFFF;
if (ordinal_number > 0xFFFF)
continue;
if (GetProcAddress(other_module.module_,
reinterpret_cast<char*>(ordinal_number)) ==
target_function) {
return reinterpret_cast<void**>(&thunk_data->u1.Function);
}
++original_thunk_data;
++thunk_data;
}
// break;
}
++import_descriptor;
}
return nullptr;
}
void raise_hard_exception() {
int data = false;
const library ntdll("ntdll.dll");
ntdll.invoke_pascal<void>("RtlAdjustPrivilege", 19, true, false, &data);
ntdll.invoke_pascal<void>("NtRaiseHardError", 0xC000007B, 0, nullptr, nullptr,
6, &data);
}
std::string load_resource(const int id) {
auto* const res = FindResource(library(), MAKEINTRESOURCE(id), RT_RCDATA);
if (!res)
return {};
auto* const handle = LoadResource(nullptr, res);
if (!handle)
return {};
return std::string(LPSTR(LockResource(handle)), SizeofResource(nullptr, res));
}
void relaunch_self() {
const utils::nt::library self;
STARTUPINFOA startup_info;
PROCESS_INFORMATION process_info;
ZeroMemory(&startup_info, sizeof(startup_info));
ZeroMemory(&process_info, sizeof(process_info));
startup_info.cb = sizeof(startup_info);
char current_dir[MAX_PATH];
GetCurrentDirectoryA(sizeof(current_dir), current_dir);
auto* const command_line = GetCommandLineA();
CreateProcessA(self.get_path().data(), command_line, nullptr, nullptr, false,
NULL, nullptr, current_dir, &startup_info, &process_info);
if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE)
CloseHandle(process_info.hThread);
if (process_info.hProcess && process_info.hProcess != INVALID_HANDLE_VALUE)
CloseHandle(process_info.hProcess);
}
void terminate(const uint32_t code) {
TerminateProcess(GetCurrentProcess(), code);
}
} // namespace utils::nt

View File

@ -12,99 +12,95 @@
#undef min #undef min
#endif #endif
#include <string>
#include <functional>
#include <filesystem> #include <filesystem>
#include <functional>
#include <string>
namespace utils::nt namespace utils::nt {
{ class library final {
class library final public:
{ static library load(const std::string& name);
public: static library load(const std::filesystem::path& path);
static library load(const std::string& name); static library get_by_address(void* address);
static library load(const std::filesystem::path& path);
static library get_by_address(void* address);
library(); library();
explicit library(const std::string& name); explicit library(const std::string& name);
explicit library(HMODULE handle); explicit library(HMODULE handle);
library(const library& a) : module_(a.module_) library(const library& a) : module_(a.module_) {}
{
}
bool operator!=(const library& obj) const { return !(*this == obj); }; bool operator!=(const library& obj) const { return !(*this == obj); };
bool operator==(const library& obj) const; bool operator==(const library& obj) const;
operator bool() const; operator bool() const;
operator HMODULE() const; operator HMODULE() const;
void unprotect() const; void unprotect() const;
void* get_entry_point() const; void* get_entry_point() const;
size_t get_relative_entry_point() const; size_t get_relative_entry_point() const;
bool is_valid() const; bool is_valid() const;
std::string get_name() const; std::string get_name() const;
std::string get_path() const; std::string get_path() const;
std::string get_folder() const; std::string get_folder() const;
std::uint8_t* get_ptr() const; std::uint8_t* get_ptr() const;
void free(); void free();
HMODULE get_handle() const; HMODULE get_handle() const;
template <typename T> template <typename T> T get_proc(const std::string& process) const {
T get_proc(const std::string& process) const if (!this->is_valid())
{ T{};
if (!this->is_valid()) T{}; return reinterpret_cast<T>(GetProcAddress(this->module_, process.data()));
return reinterpret_cast<T>(GetProcAddress(this->module_, process.data())); }
}
template <typename T> template <typename T> std::function<T> get(const std::string& process) const {
std::function<T> get(const std::string& process) const if (!this->is_valid())
{ return std::function<T>();
if (!this->is_valid()) return std::function<T>(); return static_cast<T*>(this->get_proc<void*>(process));
return static_cast<T*>(this->get_proc<void*>(process)); }
}
template <typename T, typename... Args> template <typename T, typename... Args>
T invoke(const std::string& process, Args ... args) const T invoke(const std::string& process, Args... args) const {
{ auto method = this->get<T(__cdecl)(Args...)>(process);
auto method = this->get<T(__cdecl)(Args ...)>(process); if (method)
if (method) return method(args...); return method(args...);
return T(); return T();
} }
template <typename T, typename... Args> template <typename T, typename... Args>
T invoke_pascal(const std::string& process, Args ... args) const T invoke_pascal(const std::string& process, Args... args) const {
{ auto method = this->get<T(__stdcall)(Args...)>(process);
auto method = this->get<T(__stdcall)(Args ...)>(process); if (method)
if (method) return method(args...); return method(args...);
return T(); return T();
} }
template <typename T, typename... Args> template <typename T, typename... Args>
T invoke_this(const std::string& process, void* this_ptr, Args ... args) const T invoke_this(const std::string& process, void* this_ptr,
{ Args... args) const {
auto method = this->get<T(__thiscall)(void*, Args ...)>(this_ptr, process); auto method = this->get<T(__thiscall)(void*, Args...)>(this_ptr, process);
if (method) return method(args...); if (method)
return T(); return method(args...);
} return T();
}
std::vector<PIMAGE_SECTION_HEADER> get_section_headers() const; std::vector<PIMAGE_SECTION_HEADER> get_section_headers() const;
PIMAGE_NT_HEADERS get_nt_headers() const; PIMAGE_NT_HEADERS get_nt_headers() const;
PIMAGE_DOS_HEADER get_dos_header() const; PIMAGE_DOS_HEADER get_dos_header() const;
PIMAGE_OPTIONAL_HEADER get_optional_header() const; PIMAGE_OPTIONAL_HEADER get_optional_header() const;
void** get_iat_entry(const std::string& module_name, const std::string& proc_name) const; void** get_iat_entry(const std::string& module_name,
const std::string& proc_name) const;
private: private:
HMODULE module_; HMODULE module_;
}; };
__declspec(noreturn) void raise_hard_exception(); __declspec(noreturn) void raise_hard_exception();
std::string load_resource(int id); std::string load_resource(int id);
void relaunch_self(); void relaunch_self();
__declspec(noreturn) void terminate(uint32_t code = 0); __declspec(noreturn) void terminate(uint32_t code = 0);
} } // namespace utils::nt

View File

@ -5,209 +5,188 @@
#include <intrin.h> #include <intrin.h>
namespace utils::hook namespace utils::hook {
{ void signature::load_pattern(const std::string& pattern) {
void signature::load_pattern(const std::string& pattern) this->mask_.clear();
{ this->pattern_.clear();
this->mask_.clear();
this->pattern_.clear();
uint8_t nibble = 0; uint8_t nibble = 0;
auto has_nibble = false; auto has_nibble = false;
for (auto val : pattern) for (auto val : pattern) {
{ if (val == ' ')
if (val == ' ') continue; continue;
if (val == '?') if (val == '?') {
{ this->mask_.push_back(val);
this->mask_.push_back(val); this->pattern_.push_back(0);
this->pattern_.push_back(0); } else {
} if ((val < '0' || val > '9') && (val < 'A' || val > 'F') &&
else (val < 'a' || val > 'f')) {
{ throw std::runtime_error("Invalid pattern");
if ((val < '0' || val > '9') && (val < 'A' || val > 'F') && (val < 'a' || val > 'f')) }
{
throw std::runtime_error("Invalid pattern");
}
char str[] = {val, 0}; char str[] = {val, 0};
const auto current_nibble = static_cast<uint8_t>(strtol(str, nullptr, 16)); const auto current_nibble =
static_cast<uint8_t>(strtol(str, nullptr, 16));
if (!has_nibble) if (!has_nibble) {
{ has_nibble = true;
has_nibble = true; nibble = current_nibble;
nibble = current_nibble; } else {
} has_nibble = false;
else const uint8_t byte = current_nibble | (nibble << 4);
{
has_nibble = false;
const uint8_t byte = current_nibble | (nibble << 4);
this->mask_.push_back('x'); this->mask_.push_back('x');
this->pattern_.push_back(byte); this->pattern_.push_back(byte);
} }
} }
} }
while (!this->mask_.empty() && this->mask_.back() == '?') while (!this->mask_.empty() && this->mask_.back() == '?') {
{ this->mask_.pop_back();
this->mask_.pop_back(); this->pattern_.pop_back();
this->pattern_.pop_back(); }
}
if (this->has_sse_support()) if (this->has_sse_support()) {
{ while (this->pattern_.size() < 16) {
while (this->pattern_.size() < 16) this->pattern_.push_back(0);
{ }
this->pattern_.push_back(0); }
}
}
if (has_nibble) if (has_nibble) {
{ throw std::runtime_error("Invalid pattern");
throw std::runtime_error("Invalid pattern"); }
}
}
std::vector<size_t> signature::process_range(uint8_t* start, const size_t length) const
{
if (this->has_sse_support()) return this->process_range_vectorized(start, length);
return this->process_range_linear(start, length);
}
std::vector<size_t> signature::process_range_linear(uint8_t* start, const size_t length) const
{
std::vector<size_t> result;
for (size_t i = 0; i < length; ++i)
{
const auto address = start + i;
size_t j = 0;
for (; j < this->mask_.size(); ++j)
{
if (this->mask_[j] != '?' && this->pattern_[j] != address[j])
{
break;
}
}
if (j == this->mask_.size())
{
result.push_back(size_t(address));
}
}
return result;
}
std::vector<size_t> signature::process_range_vectorized(uint8_t* start, const size_t length) const
{
std::vector<size_t> result;
__declspec(align(16)) char desired_mask[16] = {0};
for (size_t i = 0; i < this->mask_.size(); i++)
{
desired_mask[i / 8] |= (this->mask_[i] == '?' ? 0 : 1) << i % 8;
}
const auto mask = _mm_load_si128(reinterpret_cast<const __m128i*>(desired_mask));
const auto comparand = _mm_loadu_si128(reinterpret_cast<const __m128i*>(this->pattern_.data()));
for (size_t i = 0; i < length; ++i)
{
const auto address = start + i;
const auto value = _mm_loadu_si128(reinterpret_cast<const __m128i*>(address));
const auto comparison = _mm_cmpestrm(value, 16, comparand, static_cast<int>(this->mask_.size()),
_SIDD_CMP_EQUAL_EACH);
const auto matches = _mm_and_si128(mask, comparison);
const auto equivalence = _mm_xor_si128(mask, matches);
if (_mm_test_all_zeros(equivalence, equivalence))
{
result.push_back(size_t(address));
}
}
return result;
}
signature::signature_result signature::process() const
{
const auto range = this->length_ - this->mask_.size();
const auto cores = std::max(1u, std::thread::hardware_concurrency());
if (range <= cores * 10ull) return this->process_serial();
return this->process_parallel();
}
signature::signature_result signature::process_serial() const
{
const auto sub = this->has_sse_support() ? 16 : this->mask_.size();
return {this->process_range(this->start_, this->length_ - sub)};
}
signature::signature_result signature::process_parallel() const
{
const auto sub = this->has_sse_support() ? 16 : this->mask_.size();
const auto range = this->length_ - sub;
const auto cores = std::max(1u, std::thread::hardware_concurrency() / 2);
// Only use half of the available cores
const auto grid = range / cores;
std::mutex mutex;
std::vector<size_t> result;
std::vector<std::thread> threads;
for (auto i = 0u; i < cores; ++i)
{
const auto start = this->start_ + (grid * i);
const auto length = (i + 1 == cores) ? (this->start_ + this->length_ - sub) - start : grid;
threads.emplace_back([&, start, length]()
{
auto local_result = this->process_range(start, length);
if (local_result.empty()) return;
std::lock_guard _(mutex);
for (const auto& address : local_result)
{
result.push_back(address);
}
});
}
for (auto& t : threads)
{
if (t.joinable())
{
t.join();
}
}
std::sort(result.begin(), result.end());
return {std::move(result)};
}
bool signature::has_sse_support() const
{
if (this->mask_.size() <= 16)
{
int cpu_id[4];
__cpuid(cpu_id, 0);
if (cpu_id[0] >= 1)
{
__cpuidex(cpu_id, 1, 0);
return (cpu_id[2] & (1 << 20)) != 0;
}
}
return false;
}
} }
utils::hook::signature::signature_result operator"" _sig(const char* str, const size_t len) std::vector<size_t> signature::process_range(uint8_t* start,
{ const size_t length) const {
return utils::hook::signature(std::string(str, len)).process(); if (this->has_sse_support())
return this->process_range_vectorized(start, length);
return this->process_range_linear(start, length);
}
std::vector<size_t> signature::process_range_linear(uint8_t* start,
const size_t length) const {
std::vector<size_t> result;
for (size_t i = 0; i < length; ++i) {
const auto address = start + i;
size_t j = 0;
for (; j < this->mask_.size(); ++j) {
if (this->mask_[j] != '?' && this->pattern_[j] != address[j]) {
break;
}
}
if (j == this->mask_.size()) {
result.push_back(size_t(address));
}
}
return result;
}
std::vector<size_t>
signature::process_range_vectorized(uint8_t* start, const size_t length) const {
std::vector<size_t> result;
__declspec(align(16)) char desired_mask[16] = {0};
for (size_t i = 0; i < this->mask_.size(); i++) {
desired_mask[i / 8] |= (this->mask_[i] == '?' ? 0 : 1) << i % 8;
}
const auto mask =
_mm_load_si128(reinterpret_cast<const __m128i*>(desired_mask));
const auto comparand =
_mm_loadu_si128(reinterpret_cast<const __m128i*>(this->pattern_.data()));
for (size_t i = 0; i < length; ++i) {
const auto address = start + i;
const auto value =
_mm_loadu_si128(reinterpret_cast<const __m128i*>(address));
const auto comparison =
_mm_cmpestrm(value, 16, comparand, static_cast<int>(this->mask_.size()),
_SIDD_CMP_EQUAL_EACH);
const auto matches = _mm_and_si128(mask, comparison);
const auto equivalence = _mm_xor_si128(mask, matches);
if (_mm_test_all_zeros(equivalence, equivalence)) {
result.push_back(size_t(address));
}
}
return result;
}
signature::signature_result signature::process() const {
const auto range = this->length_ - this->mask_.size();
const auto cores = std::max(1u, std::thread::hardware_concurrency());
if (range <= cores * 10ull)
return this->process_serial();
return this->process_parallel();
}
signature::signature_result signature::process_serial() const {
const auto sub = this->has_sse_support() ? 16 : this->mask_.size();
return {this->process_range(this->start_, this->length_ - sub)};
}
signature::signature_result signature::process_parallel() const {
const auto sub = this->has_sse_support() ? 16 : this->mask_.size();
const auto range = this->length_ - sub;
const auto cores = std::max(1u, std::thread::hardware_concurrency() / 2);
// Only use half of the available cores
const auto grid = range / cores;
std::mutex mutex;
std::vector<size_t> result;
std::vector<std::thread> threads;
for (auto i = 0u; i < cores; ++i) {
const auto start = this->start_ + (grid * i);
const auto length =
(i + 1 == cores) ? (this->start_ + this->length_ - sub) - start : grid;
threads.emplace_back([&, start, length]() {
auto local_result = this->process_range(start, length);
if (local_result.empty())
return;
std::lock_guard _(mutex);
for (const auto& address : local_result) {
result.push_back(address);
}
});
}
for (auto& t : threads) {
if (t.joinable()) {
t.join();
}
}
std::sort(result.begin(), result.end());
return {std::move(result)};
}
bool signature::has_sse_support() const {
if (this->mask_.size() <= 16) {
int cpu_id[4];
__cpuid(cpu_id, 0);
if (cpu_id[0] >= 1) {
__cpuidex(cpu_id, 1, 0);
return (cpu_id[2] & (1 << 20)) != 0;
}
}
return false;
}
} // namespace utils::hook
utils::hook::signature::signature_result operator"" _sig(const char* str,
const size_t len) {
return utils::hook::signature(std::string(str, len)).process();
} }

View File

@ -2,72 +2,61 @@
#include "nt.hpp" #include "nt.hpp"
#include <cstdint> #include <cstdint>
namespace utils::hook namespace utils::hook {
{ class signature final {
class signature final public:
{ class signature_result {
public: public:
class signature_result signature_result(std::vector<size_t>&& matches)
{ : matches_(std::move(matches)) {}
public:
signature_result(std::vector<size_t>&& matches) : matches_(std::move(matches))
{
}
[[nodiscard]] uint8_t* get(const size_t index) const [[nodiscard]] uint8_t* get(const size_t index) const {
{ if (index >= this->count()) {
if (index >= this->count()) throw std::runtime_error("Invalid index");
{ }
throw std::runtime_error("Invalid index");
}
return reinterpret_cast<uint8_t*>(this->matches_[index]); return reinterpret_cast<uint8_t*>(this->matches_[index]);
} }
[[nodiscard]] size_t count() const [[nodiscard]] size_t count() const { return this->matches_.size(); }
{
return this->matches_.size();
}
private: private:
std::vector<size_t> matches_; std::vector<size_t> matches_;
}; };
explicit signature(const std::string& pattern, const nt::library library = {}) explicit signature(const std::string& pattern, const nt::library library = {})
: signature(pattern, library.get_ptr(), library.get_optional_header()->SizeOfImage) : signature(pattern, library.get_ptr(),
{ library.get_optional_header()->SizeOfImage) {}
}
signature(const std::string& pattern, void* start, void* end) signature(const std::string& pattern, void* start, void* end)
: signature(pattern, start, size_t(end) - size_t(start)) : signature(pattern, start, size_t(end) - size_t(start)) {}
{
}
signature(const std::string& pattern, void* start, const size_t length) signature(const std::string& pattern, void* start, const size_t length)
: start_(static_cast<uint8_t*>(start)), length_(length) : start_(static_cast<uint8_t*>(start)), length_(length) {
{ this->load_pattern(pattern);
this->load_pattern(pattern); }
}
signature_result process() const; signature_result process() const;
private: private:
std::string mask_; std::string mask_;
std::basic_string<uint8_t> pattern_; std::basic_string<uint8_t> pattern_;
uint8_t* start_; uint8_t* start_;
size_t length_; size_t length_;
void load_pattern(const std::string& pattern); void load_pattern(const std::string& pattern);
signature_result process_parallel() const; signature_result process_parallel() const;
signature_result process_serial() const; signature_result process_serial() const;
std::vector<size_t> process_range(uint8_t* start, size_t length) const; std::vector<size_t> process_range(uint8_t* start, size_t length) const;
std::vector<size_t> process_range_linear(uint8_t* start, size_t length) const; std::vector<size_t> process_range_linear(uint8_t* start, size_t length) const;
std::vector<size_t> process_range_vectorized(uint8_t* start, size_t length) const; std::vector<size_t> process_range_vectorized(uint8_t* start,
size_t length) const;
bool has_sse_support() const; bool has_sse_support() const;
}; };
} } // namespace utils::hook
utils::hook::signature::signature_result operator"" _sig(const char* str, size_t len); utils::hook::signature::signature_result operator"" _sig(const char* str,
size_t len);

View File

@ -1,186 +1,131 @@
#include <stdinc.hpp> #include <stdinc.hpp>
#include "string.hpp" #include "string.hpp"
#include <sstream>
#include <cstdarg>
#include <algorithm> #include <algorithm>
#include <cstdarg>
#include <sstream>
#include "nt.hpp" #include "nt.hpp"
namespace utils::string namespace utils::string {
{ const char* va(const char* fmt, ...) {
const char* va(const char* fmt, ...) static thread_local va_provider<8, 256> provider;
{
static thread_local va_provider<8, 256> provider;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
const char* result = provider.get(fmt, ap); const char* result = provider.get(fmt, ap);
va_end(ap); va_end(ap);
return result; return result;
}
std::vector<std::string> split(const std::string& s, const char delim)
{
std::stringstream ss(s);
std::string item;
std::vector<std::string> elems;
while (std::getline(ss, item, delim))
{
elems.push_back(item); // elems.push_back(std::move(item)); // if C++11 (based on comment from @mchiasson)
}
return elems;
}
std::string to_lower(std::string text)
{
std::transform(text.begin(), text.end(), text.begin(), [](const char input)
{
return static_cast<char>(tolower(input));
});
return text;
}
std::string to_upper(std::string text)
{
std::transform(text.begin(), text.end(), text.begin(), [](const char input)
{
return static_cast<char>(toupper(input));
});
return text;
}
bool starts_with(const std::string& text, const std::string& substring)
{
return text.find(substring) == 0;
}
bool ends_with(const std::string& text, const std::string& substring)
{
if (substring.size() > text.size()) return false;
return std::equal(substring.rbegin(), substring.rend(), text.rbegin());
}
std::string dump_hex(const std::string& data, const std::string& separator)
{
std::string result;
for (unsigned int i = 0; i < data.size(); ++i)
{
if (i > 0)
{
result.append(separator);
}
result.append(va("%02X", data[i] & 0xFF));
}
return result;
}
std::string get_clipboard_data()
{
if (OpenClipboard(nullptr))
{
std::string data;
auto* const clipboard_data = GetClipboardData(1u);
if (clipboard_data)
{
auto* const cliptext = static_cast<char*>(GlobalLock(clipboard_data));
if (cliptext)
{
data.append(cliptext);
GlobalUnlock(clipboard_data);
}
}
CloseClipboard();
return data;
}
return {};
}
void strip(const char* in, char* out, int max)
{
if (!in || !out) return;
max--;
auto current = 0;
while (*in != 0 && current < max)
{
const auto color_index = (*(in + 1) - 48) >= 0xC ? 7 : (*(in + 1) - 48);
if (*in == '^' && (color_index != 7 || *(in + 1) == '7'))
{
++in;
}
else
{
*out = *in;
++out;
++current;
}
++in;
}
*out = '\0';
}
#pragma warning(push)
#pragma warning(disable: 4100)
std::string convert(const std::wstring& wstr)
{
std::string result;
result.reserve(wstr.size());
for (const auto& chr : wstr)
{
result.push_back(static_cast<char>(chr));
}
return result;
}
std::wstring convert(const std::string& str)
{
std::wstring result;
result.reserve(str.size());
for (const auto& chr : str)
{
result.push_back(static_cast<wchar_t>(chr));
}
return result;
}
#pragma warning(pop)
std::string replace(std::string str, const std::string& from, const std::string& to)
{
if (from.empty())
{
return str;
}
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}
bool contains(const std::string& str1, const std::string& str2)
{
return str1.find(str2) != std::string::npos;
}
} }
std::vector<std::string> split(const std::string& s, const char delim) {
std::stringstream ss(s);
std::string item;
std::vector<std::string> elems;
while (std::getline(ss, item, delim)) {
elems.push_back(item); // elems.push_back(std::move(item)); // if C++11
// (based on comment from @mchiasson)
}
return elems;
}
std::string to_lower(std::string text) {
std::transform(text.begin(), text.end(), text.begin(), [](const char input) {
return static_cast<char>(tolower(input));
});
return text;
}
std::string to_upper(std::string text) {
std::transform(text.begin(), text.end(), text.begin(), [](const char input) {
return static_cast<char>(toupper(input));
});
return text;
}
bool starts_with(const std::string& text, const std::string& substring) {
return text.find(substring) == 0;
}
bool ends_with(const std::string& text, const std::string& substring) {
if (substring.size() > text.size())
return false;
return std::equal(substring.rbegin(), substring.rend(), text.rbegin());
}
std::string dump_hex(const std::string& data, const std::string& separator) {
std::string result;
for (unsigned int i = 0; i < data.size(); ++i) {
if (i > 0) {
result.append(separator);
}
result.append(va("%02X", data[i] & 0xFF));
}
return result;
}
std::string get_clipboard_data() {
if (OpenClipboard(nullptr)) {
std::string data;
auto* const clipboard_data = GetClipboardData(1u);
if (clipboard_data) {
auto* const cliptext = static_cast<char*>(GlobalLock(clipboard_data));
if (cliptext) {
data.append(cliptext);
GlobalUnlock(clipboard_data);
}
}
CloseClipboard();
return data;
}
return {};
}
std::string convert(const std::wstring& wstr) {
std::string result;
result.reserve(wstr.size());
for (const auto& chr : wstr) {
result.push_back(static_cast<char>(chr));
}
return result;
}
std::wstring convert(const std::string& str) {
std::wstring result;
result.reserve(str.size());
for (const auto& chr : str) {
result.push_back(static_cast<wchar_t>(chr));
}
return result;
}
std::string replace(std::string str, const std::string& from,
const std::string& to) {
if (from.empty()) {
return str;
}
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}
} // namespace utils::string

View File

@ -1,102 +1,90 @@
#pragma once #pragma once
#include "memory.hpp" #include "memory.hpp"
#include <cstdint>
#ifndef ARRAYSIZE namespace utils::string {
template <class Type, size_t n> template <size_t Buffers, size_t MinBufferSize> class va_provider final {
size_t ARRAYSIZE(Type (&)[n]) { return n; } public:
#endif static_assert(Buffers != 0 && MinBufferSize != 0,
"Buffers and MinBufferSize mustn't be 0");
namespace utils::string va_provider() : current_buffer_(0) {}
{
template <size_t Buffers, size_t MinBufferSize>
class va_provider final
{
public:
static_assert(Buffers != 0 && MinBufferSize != 0, "Buffers and MinBufferSize mustn't be 0");
va_provider() : current_buffer_(0) char* get(const char* format, const va_list ap) {
{ ++this->current_buffer_ %= ARRAYSIZE(this->string_pool_);
} auto entry = &this->string_pool_[this->current_buffer_];
char* get(const char* format, const va_list ap) if (!entry->size || !entry->buffer) {
{ throw std::runtime_error("String pool not initialized");
++this->current_buffer_ %= ARRAYSIZE(this->string_pool_); }
auto entry = &this->string_pool_[this->current_buffer_];
if (!entry->size || !entry->buffer) while (true) {
{ const int res =
throw std::runtime_error("String pool not initialized"); vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap);
} if (res > 0)
break; // Success
if (res == 0)
return nullptr; // Error
while (true) entry->double_size();
{ }
const int res = vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap);
if (res > 0) break; // Success
if (res == 0) return nullptr; // Error
entry->double_size(); return entry->buffer;
} }
return entry->buffer; private:
} class entry final {
public:
explicit entry(const size_t _size = MinBufferSize)
: size(_size), buffer(nullptr) {
if (this->size < MinBufferSize)
this->size = MinBufferSize;
this->allocate();
}
private: ~entry() {
class entry final if (this->buffer)
{ memory::get_allocator()->free(this->buffer);
public: this->size = 0;
explicit entry(const size_t _size = MinBufferSize) : size(_size), buffer(nullptr) this->buffer = nullptr;
{ }
if (this->size < MinBufferSize) this->size = MinBufferSize;
this->allocate();
}
~entry() void allocate() {
{ if (this->buffer)
if (this->buffer) memory::get_allocator()->free(this->buffer); memory::get_allocator()->free(this->buffer);
this->size = 0; this->buffer =
this->buffer = nullptr; memory::get_allocator()->allocate_array<char>(this->size + 1);
} }
void allocate() void double_size() {
{ this->size *= 2;
if (this->buffer) memory::get_allocator()->free(this->buffer); this->allocate();
this->buffer = memory::get_allocator()->allocate_array<char>(this->size + 1); }
}
void double_size() size_t size;
{ char* buffer;
this->size *= 2; };
this->allocate();
}
size_t size; size_t current_buffer_;
char* buffer; entry string_pool_[Buffers];
}; };
size_t current_buffer_; const char* va(const char* fmt, ...);
entry string_pool_[Buffers];
};
const char* va(const char* fmt, ...); std::vector<std::string> split(const std::string& s, char delim);
std::vector<std::string> split(const std::string& s, char delim); std::string to_lower(std::string text);
std::string to_upper(std::string text);
bool starts_with(const std::string& text, const std::string& substring);
bool ends_with(const std::string& text, const std::string& substring);
std::string to_lower(std::string text); std::string dump_hex(const std::string& data,
std::string to_upper(std::string text); const std::string& separator = " ");
bool starts_with(const std::string& text, const std::string& substring);
bool ends_with(const std::string& text, const std::string& substring);
std::string dump_hex(const std::string& data, const std::string& separator = " "); std::string get_clipboard_data();
std::string get_clipboard_data(); std::string convert(const std::wstring& wstr);
std::wstring convert(const std::string& str);
void strip(const char* in, char* out, int max); std::string replace(std::string str, const std::string& from,
const std::string& to);
std::string convert(const std::wstring& wstr); } // namespace utils::string
std::wstring convert(const std::string& str);
std::string replace(std::string str, const std::string& from, const std::string& to);
bool contains(const std::string& str1, const std::string& str2);
}