Jump to content
Tuts 4 You

C++ Hooking - Write Less Do More


Vic

Recommended Posts

Posted (edited)

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.


Eg. To hook/un-hook a function with the Inline Hooking technique, you only need to write codes as the following

#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<int>(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

#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<int>(Entry_MessageBoxW, hWnd, lpText, lpCaption, uType);
}

// Perform hooking
IATHookingManager::instance().hook(Entry_MessageBoxW, hkMessageBoxW);

// Perform un-hooking
IATHookingManager::instance().unhook(Entry_MessageBoxW);

The repository @ https://github.com/vic4key/cpp-hooking.git

Follow me on GitHub @ https://github.com/vic4key

Regards,
Vic P.

Edited by Vic
CrLf
  • Like 4
  • Thanks 2
Posted

Very practical source codes... :)

  • Like 2
  • 9 months later...
TRISTAN Pro
Posted (edited)
On 11/7/2023 at 7:15 AM, Vic said:

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.


Eg. To hook/un-hook a function with the Inline Hooking technique, you only need to write codes as the following

#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<int>(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

#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<int>(Entry_MessageBoxW, hWnd, lpText, lpCaption, uType);
}

// Perform hooking
IATHookingManager::instance().hook(Entry_MessageBoxW, hkMessageBoxW);

// Perform un-hooking
IATHookingManager::instance().unhook(Entry_MessageBoxW);

The repository @ https://github.com/vic4key/cpp-hooking.git

Follow me on GitHub @ https://github.com/vic4key

Regards,
Vic P.

Does it possible to restore the api hooked with detour?

 

Thanks 

Edited by TRISTAN Pro
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...