Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/VMT hook example #59

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,32 @@ if(SAFETYHOOK_BUILD_EXAMPLES) # build-examples
safetyhook::safetyhook
)

endif()
# Target: example-vmthook
if(SAFETYHOOK_BUILD_EXAMPLES) # build-examples
set(example-vmthook_SOURCES
"example/vmthook.cpp"
cmake.toml
)

add_executable(example-vmthook)

target_sources(example-vmthook PRIVATE ${example-vmthook_SOURCES})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${example-vmthook_SOURCES})

target_compile_features(example-vmthook PRIVATE
cxx_std_23
)

target_link_libraries(example-vmthook PRIVATE
safetyhook::safetyhook
)

get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT)
if(NOT CMKR_VS_STARTUP_PROJECT)
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT example-vmthook)
endif()

endif()
# Target: test
set(test_SOURCES
Expand Down
4 changes: 4 additions & 0 deletions cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ sources = ["example/threadsafe.cpp"]
type = "example-dll"
sources = ["example/dll.cpp"]

[target.example-vmthook]
type = "example"
sources = ["example/vmthook.cpp"]

[target.test]
type = "executable"
sources = ["test/*.cpp"]
Expand Down
10 changes: 5 additions & 5 deletions example/midhook.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <iostream>
#include <print>

#if __has_include(<Zydis/Zydis.h>)
#include <Zydis/Zydis.h>
Expand All @@ -25,7 +25,7 @@ void hooked_add_42(SafetyHookContext& ctx) {
SafetyHookMid g_hook{};

int main() {
std::cout << add_42(2) << "\n";
std::println("unhooked add_42(2) = {}", add_42(2));

// Let's disassemble add_42 and hook its RET.
ZydisDecoder decoder{};
Expand Down Expand Up @@ -55,11 +55,11 @@ int main() {

g_hook = safetyhook::create_mid(ip, hooked_add_42);

std::cout << add_42(3) << "\n";
std::println("hooked add_42(3) = {}", add_42(3));

g_hook.reset();
g_hook = {};

std::cout << add_42(4) << "\n";
std::println("unhooked add_42(4) = {}", add_42(4));

return 0;
}
8 changes: 4 additions & 4 deletions example/minimal.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <iostream>
#include <print>

#include <safetyhook.hpp>

Expand All @@ -13,16 +13,16 @@ int hook_add(int x, int y) {
}

int main() {
std::cout << "unhooked add(2, 3) = " << add(2, 3) << "\n";
std::println("unhooked add(2, 3) = {}", add(2, 3));

// Create a hook on add.
g_add_hook = safetyhook::create_inline(reinterpret_cast<void*>(add), reinterpret_cast<void*>(hook_add));

std::cout << "hooked add(3, 4) = " << add(3, 4) << "\n";
std::println("hooked add(3, 4) = {}", add(3, 4));

g_add_hook = {};

std::cout << "unhooked add(5, 6) = " << add(5, 6) << "\n";
std::println("unhooked add(5, 6) = {}", add(5, 6));

return 0;
}
4 changes: 2 additions & 2 deletions example/multiple.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <iostream>
#include <print>

#include <safetyhook.hpp>

SafetyHookInline hook0, hook1, hook2, hook3;

__declspec(noinline) void say_hi(const std::string& name) {
std::cout << "hello " << name << "\n";
std::println("hello {}", name);
}

void hook0_fn(const std::string& name) {
Expand Down
4 changes: 2 additions & 2 deletions example/threadsafe.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <iostream>
#include <print>
#include <thread>

#include <safetyhook.hpp>

SafetyHookInline g_hook{};

__declspec(noinline) void SayHello(int times) {
std::cout << "Hello #" << times << std::endl;
std::println("Hello #{}", times);
}

void Hooked_SayHello(int times [[maybe_unused]]) {
Expand Down
40 changes: 40 additions & 0 deletions example/vmthook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <memory>
#include <print>

#include <safetyhook.hpp>

class Interface {
public:
virtual ~Interface() = default;
virtual int add_42(int a) = 0;
};

class Target : public Interface {
public:
int add_42(int a) override { return a + 42; }
};

SafetyHookVmt g_target_hook;
SafetyHookVm g_add_42_hook;

class Hook : public Target {
public:
int hooked_add_42(int a) { return g_add_42_hook.thiscall<int>(this, a) + 1337; }
};

int main() {
auto target = std::make_unique<Target>();

std::println("unhooked target->add_42(1) = {}", target->add_42(1));

g_target_hook = safetyhook::create_vmt(target.get());
g_add_42_hook = safetyhook::create_vm(g_target_hook, 1, &Hook::hooked_add_42);

std::println("hooked target->add_42(2) = {}", target->add_42(1));

g_target_hook = {};

std::println("unhooked target->add_42(3) = {}", target->add_42(1));

return 0;
}
Loading