All Projects → kin4stat → kthook

kin4stat / kthook

Licence: MIT license
No description or website provided.

Programming Languages

C++
36643 projects - #6 most used programming language
c
50402 projects - #5 most used programming language
CMake
9771 projects

Labels

Projects that are alternatives of or similar to kthook

react-use-downloader
Creates a download handler function and gives progress information
Stars: ✭ 65 (+18.18%)
Mutual labels:  hook
subhook.nim
subhook wrapper for Nim https://github.com/Zeex/subhook
Stars: ✭ 15 (-72.73%)
Mutual labels:  hook
react-hook-videojs
Easy React integration of Video.js using hooks.
Stars: ✭ 37 (-32.73%)
Mutual labels:  hook
iosHookViewId
A solution for ios hook view id(给iOS应用自动生成控件id)
Stars: ✭ 44 (-20%)
Mutual labels:  hook
objects-hooks-remover
Package to remove WordPress hook callbacks that uses object methods or closures.
Stars: ✭ 44 (-20%)
Mutual labels:  hook
svg-to-swiftui-core
Headless package for converting SVG to SwiftUI
Stars: ✭ 25 (-54.55%)
Mutual labels:  hook
web
React hooks done right, for browser and SSR.
Stars: ✭ 940 (+1609.09%)
Mutual labels:  hook
AndroidSec
记录一些我自己在学习Android逆向过程中的有意思的东西
Stars: ✭ 565 (+927.27%)
Mutual labels:  hook
KeyBoardTool
Keyboard key detection software realized by Qt(Qt实现的键盘按键检测软件)
Stars: ✭ 35 (-36.36%)
Mutual labels:  hook
BaiDuYunCrack
iOS百度云盘 破解速度限制、去广告、去更新 无需越狱~
Stars: ✭ 82 (+49.09%)
Mutual labels:  hook
riru MPH
Hook android system prop function to add properties
Stars: ✭ 23 (-58.18%)
Mutual labels:  hook
usehooks-ts
React hook library, ready to use, written in Typescript.
Stars: ✭ 2,873 (+5123.64%)
Mutual labels:  hook
Uatu
Android方法调用跟踪 ; 方法耗时统计 ; 方法调用参数以及返回值跟踪 ; 方法调用替换;方法hook
Stars: ✭ 93 (+69.09%)
Mutual labels:  hook
resize-observer-hook
⚛️ A React Hook to monitor changes in the size of an element using native ResizeObserver API 🔍
Stars: ✭ 45 (-18.18%)
Mutual labels:  hook
SwiftLoadHook
Use a hack way to achieve similar functions as Load() or initialize() in OC
Stars: ✭ 21 (-61.82%)
Mutual labels:  hook
messier
Messier is an app for tracing objective-c methods in an iOS app.
Stars: ✭ 72 (+30.91%)
Mutual labels:  hook
nxdk-rdt
Remote Dev Tool is a tool to remote control an Xbox using memory access and RPC
Stars: ✭ 23 (-58.18%)
Mutual labels:  hook
PEDetour
modify binary Portable Executable to hook its export functions
Stars: ✭ 59 (+7.27%)
Mutual labels:  hook
storken
🦩 Storken is a React State Manager. Simple as `useState`.
Stars: ✭ 22 (-60%)
Mutual labels:  hook
klyva
A state management library that follows the React component model
Stars: ✭ 53 (-3.64%)
Mutual labels:  hook

kthook

X86 hooking library with functor callbacks support, so you can use lambdas with state, std::bind values etc...

Windows Linux-x64 Linux-X86 Format check

Usage

Clone repository and simply include kthook.hpp. C++17 compatible compiler required

Examples

Callbacks backend is ktsignal

All hooks are automatically removed in the kthook destructor

All examples are shown based on this function

int FASTCALL func1(float a, float b) {
    print_info(a, b);
    a = 50; b = 100;
    return 5;
}

Basics

Creating hook and binding callback
All callbacks are given a reference to kthook as the first argument. You can get return address and trampoline pointer from hook object.
Hooks are installed after construction by default(last parameter can be set to false to prevent this)

int main() {
    // func_ptr is pointer to function
    auto func_ptr = &func1;

    // func_type is int(CFASTCALL*)(float, float)
    using func_type = decltype(&func1);

    // Creating simple hook object with function type is template parameter and function pointer in constructor
    kthook::kthook_simple<func_type> hook{ func_ptr };

    // Connecting lambda callback that receiving function arguments by references
    hook.before += [](const auto& hook, float& a, float& b) {
        print_info(a, b);
        return std::nullopt;
    };

    /*
    [operator () at 31]: a = 30; b = 20
    [func1 at 16 ]: a = 30; b = 20
    */
    func1(30.f, 20.f);
}

Same thing with common hooks:

int main() {
    // func_ptr is pointer to function
    auto func_ptr = &func1;

    // func_type is int(CFASTCALL*)(float, float)
    using func_type = decltype(&func1);

    auto cb = [](const auto& hook, float a, float b) {
        print_info(a, b);
        return hook.get_trampoline()(a, b);
    };

    // Creating simple hook object with function type is template parameter and function pointer in constructor
    kthook::kthook_simple<func_type> hook{ func_ptr, cb };

    /*
    [operator () at 31]: a = 30; b = 20
    [func1 at 16 ]: a = 30; b = 20
    */
    func1(30.f, 20.f);
}

Also you can bind after original function execution callbacks
If original function return value is non void, return value reference passed at 2 argument

int main() {
    auto func_ptr = &func1;
    using func_type = decltype(&func1);

    kthook::kthook_simple_t<func_type> hook{ func_ptr };

    hook.before.connect([](const auto& hook, float& a, float& b) { 
        print_info(a, b);
        // changing arguments
        a = 50.f, b = 30.f; 
        return std::nullopt;
        });

    // connect after callback
    hook.after.connect([](const auto& hook, int& return_value, float& a, float& b) {
        print_info(a, b);
        print_return_value(return_value);

        // changing return_value
        return_value = 20;
    });

    /*
    [operator () at 31]: a = 30; b = 20
    [func1 at 16 ]: a = 50; b = 30
    [operator () at 34]: a = 50; b = 30
    [operator () at 34]: return_value = 5;
    [main at 20]: return_value = 20;
    */
    auto ret_val = func1(30.f, 20.f);
    print_return_value(ret_val)
}

You can bind multiple before/after callbacks

int main() {
    auto func_ptr = &func1;
    using func_type = decltype(&func1);

    kthook::kthook_simple_t<func_type> hook{ func_ptr };

    hook.before.connect([](const auto& hook, float& a, float& b) { print_info(a, b); return std::nullopt; });
    hook.before.connect([](const auto& hook, float& a, float& b) { a = 20; b = 30; return std::nullopt; });
    hook.after.connect([](const auto& hook, int& ret_val, float& a, float& b) { print_info(a, b); });
    hook.after.connect([](const auto& hook, int& ret_val, float& a, float& b) { print_info(a, b); });
    /*
    [operator () at 31]: a = 0; b = 0
    [func1 at 16]: a = 20; b = 30
    [operator () at 33]: a = 20; b = 30
    [operator () at 34]: a = 20; b = 30
    [main at 20]: return_value = 5;
    */
    auto ret_val = func1(30.f, 20.f);
    print_return_value(ret_val)
}

important notes

  • Function return type must be default-constructible
  • If any before callback wiil return false, and function return type is non void, the original function and after callback are not called. Default constructed value is returned
  • If all before callbacks will return true, original function and after callbacks will be called

Advanced Usage

There is a kthook that allows you to change the return value from a function without calling the original function
For generating true return value, you can use std::make_optional(value) function
For generating false return value, you can use std::nullopt

If function return type is void, then you can just return true/false see notes

int main() {
    auto func_ptr = &func1;
    using func_type = decltype(&func1);

    kthook::kthook_signal<func_type> hook{ func_ptr };
    
    hook.before.connect([](const auto& hook, float& a, float& b) { print_info(a, b); return std::nullopt; });
    hook.after.connect([](const auto& hook, int& ret_val, float& a, float& b) { ret_val = 20; print_info(a, b); });
    /*
    [operator () at 44]: a = 30; b = 20
    [func1 at 16]: a = 30; b = 20
    [operator () at 45]: a = 30; b = 20
    [main at 20]: return_value = 20;
    */
    auto ret_val = func1(30.f, 20.f);
    print_return_value(ret_val)
}

Return false example

int main() {
    auto func_ptr = &func1;
    using func_type = decltype(&func1);

    kthook::kthook_signal<func_type> hook{ func_ptr };
    
    hook.before.connect([](auto& hook, float& a, float& b) { print_info(a, b); return std::make_optional(20); });
    hook.after.connect([](auto& hook, int& ret_val, float& a, float& b) { ret_val = 20; print_info(a, b); });
    /*
    [operator () at 44]: a = 30; b = 20
    [main at 20]: return_value = 20;
    */
    auto ret_val = func1(30.f, 20.f);
    print_return_value(ret_val)
}

More examples can be found here

Credits

xbyak - x86/x86-64 JIT assembler
ktsignal - C++17 signals library

License

kthook is licensed under the MIT License, see LICENSE.txt for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].