From 146320ddc6ba23420a3540fd073881c56fc7e9e3 Mon Sep 17 00:00:00 2001 From: Vic P Date: Sat, 11 Nov 2023 17:24:12 +0700 Subject: [PATCH] Initial --- README.md | 134 ++++++++++++++++++++-------------------- cpp-hooking/invokable.h | 2 + main.cpp | 5 ++ 3 files changed, 74 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 7dc0361..c161c80 100644 --- a/README.md +++ b/README.md @@ -1,67 +1,67 @@ -# Cpp Hooking (C++17 or later required) - -With this library, you can set up function hooking easily and write less code. - -It supports both `Inline hooking` & `IAT hooking` on both 32-bit & 64-bit. - -*Note: This library uses the [Vutils INL/IAT Hooking](https://github.com/vic4key/Vutils/tree/54c7da0d9e1933932d357d0802a8e691b005388a) backend.* - -## Demo & Example - -Eg. To hook/un-hook a function with the `Inline Hooking` technique, you only need to write codes as the following - -```cpp -#include "cpp-hooking/hooking.h" - -// Define the hooking function -int WINAPI hkMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) -{ - lpText = L"INL Hooked"; - return INLHookingManager::instance().invoke(MessageBoxW, hWnd, lpText, lpCaption, uType); -} - -// Perform hooking -INLHookingManager::instance().hook(MessageBoxW, hkMessageBoxW); - -// Perform un-hooking -INLHookingManager::instance().unhook(MessageBoxW); -``` - -Eg. To hook/un-hook a function with the `IAT Hooking` technique, you only need to write codes as the following - -```cpp -#include "cpp-hooking/hooking.h" - -// Define the hooking entry -#define Entry_MessageBoxW { "cpp-hooking.exe"s, "user32.dll"s, "MessageBoxW"s } - -// Define the hooking function -int WINAPI hkMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) -{ - lpText = L"IAT Hooked"; - return IATHookingManager::instance().invoke(Entry_MessageBoxW, hWnd, lpText, lpCaption, uType); -} - -// Perform hooking -IATHookingManager::instance().hook(Entry_MessageBoxW, hkMessageBoxW); - -// Perform un-hooking -IATHookingManager::instance().unhook(Entry_MessageBoxW); -``` - -## Documentation - -See doc comments in the code files inside the [cpp-hooking](cpp-hooking) folder. - -## Installation - -1. Installed [Vutils (revision 54c7da0)](https://github.com/vic4key/Vutils/tree/54c7da0d9e1933932d357d0802a8e691b005388a) library -2. Copy the [cpp-hooking](cpp-hooking) folder to your solution then `#include "cpp-hooking\hooking.h"` -3. Enjoy - -## ToDo - -- [ ] Merge to [Vutils](https://github.com/vic4key/Vutils.git) library - -## Contact -Feel free to contact via [Twitter](https://twitter.com/vic4key) / [Gmail](mailto:vic4key@gmail.com) / [Blog](https://blog.vic.onl/) / [Website](https://vic.onl/) +# Cpp Hooking (C++17 or later required) + +With this library, you can set up function hooking easily and write less code. + +It supports both `Inline hooking` & `IAT hooking` on both 32-bit & 64-bit. + +*Note: This library uses the [Vutils INL/IAT Hooking](https://github.com/vic4key/Vutils/tree/54c7da0d9e1933932d357d0802a8e691b005388a) backend.* + +## Demo & Example + +Eg. To hook/un-hook a function with the `Inline Hooking` technique, you only need to write codes as the following + +```cpp +#include "cpp-hooking/hooking.h" + +// Define the hooking function +int WINAPI hkMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) +{ + lpText = L"INL Hooked"; + return INLHookingManager::instance().invoke(MessageBoxW, hWnd, lpText, lpCaption, uType); +} + +// Perform hooking +INLHookingManager::instance().hook(MessageBoxW, hkMessageBoxW); + +// Perform un-hooking +INLHookingManager::instance().unhook(MessageBoxW); +``` + +Eg. To hook/un-hook a function with the `IAT Hooking` technique, you only need to write codes as the following + +```cpp +#include "cpp-hooking/hooking.h" + +// Define the hooking entry +#define Entry_MessageBoxW { "cpp-hooking.exe"s, "user32.dll"s, "MessageBoxW"s } + +// Define the hooking function +int WINAPI hkMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) +{ + lpText = L"IAT Hooked"; + return IATHookingManager::instance().invoke(Entry_MessageBoxW, hWnd, lpText, lpCaption, uType); +} + +// Perform hooking +IATHookingManager::instance().hook(Entry_MessageBoxW, hkMessageBoxW); + +// Perform un-hooking +IATHookingManager::instance().unhook(Entry_MessageBoxW); +``` + +## Documentation + +See doc comments in the code files inside the [cpp-hooking](cpp-hooking) folder. + +## Installation + +1. Installed [Vutils](https://github.com/vic4key/Vutils.git) library +2. Copy the [cpp-hooking](cpp-hooking) folder to your solution then `#include "cpp-hooking\hooking.h"` +3. Enjoy + +## ToDo + +- [x] Merge to [Vutils](https://github.com/vic4key/Vutils.git) library + +## Contact +Feel free to contact via [Twitter](https://twitter.com/vic4key) / [Gmail](mailto:vic4key@gmail.com) / [Blog](https://blog.vic.onl/) / [Website](https://vic.onl/) diff --git a/cpp-hooking/invokable.h b/cpp-hooking/invokable.h index 80493cb..b7a1056 100644 --- a/cpp-hooking/invokable.h +++ b/cpp-hooking/invokable.h @@ -6,8 +6,10 @@ #pragma once +#ifndef _ANY_ #include #include +#endif // _ANY_ /** * @brief The function-container that can hold any function prototype. diff --git a/main.cpp b/main.cpp index 18a478f..29654cb 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,12 @@ // main.cpp : This file contains the 'main' function. Program execution begins and ends there. // +#include +using namespace vu; + +#ifndef VU_HAS_CPP_HOOKING #include "cpp-hooking/hooking.h" +#endif // Inline Hooking