### Internal Thread Management - Lua Example Source: https://context7.com/syr2004/fatality-cs2-hook-lib/llms.txt Illustrates the internal mechanism for suspending and resuming process threads during hook installation to prevent race conditions. This process involves taking a snapshot of threads, suspending all except the current one, performing hook modifications, and then resuming all threads. It utilizes Windows Toolhelp32 and kernel32.dll APIs. This is not directly exposed to the user but occurs automatically. ```lua -- Internal usage (not directly exposed, but shows the mechanism) -- This happens automatically inside CreateInline and CreateVtable -- Example of what happens internally: -- 1. Snapshot all threads in current process local snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0) -- 2. Enumerate and suspend threads local threads = {} for thread_id in enumerate_threads(snapshot) do if thread_id ~= current_thread_id then local handle = OpenThread(THREAD_SUSPEND_RESUME, 0, thread_id) SuspendThread(handle) table.insert(threads, {id = thread_id, handle = handle}) end end -- 3. Safely modify function bytes or vtable -- ... hook installation code ... -- 4. Resume all threads for _, thread in ipairs(threads) do ResumeThread(thread.handle) CloseHandle(thread.handle) end -- 5. Flush instruction cache FlushInstructionCache(current_process, target_address, 14) ``` -------------------------------- ### Create Inline Function Hook with Lua Source: https://context7.com/syr2004/fatality-cs2-hook-lib/llms.txt Installs an inline hook by replacing a target function's prologue with a jump to a detour function. It suspends threads during installation, backs up original bytes, and uses a 64-bit absolute jump. The hook object allows attaching, detaching, and calling the original function. Requires the `hook_lib` module. ```lua local hook_lib = require("hook lib") -- Hook a function by address with full type signature local target_address = 0x140001000 -- Example function address local my_hook = hook_lib.CreateInline( target_address, function(hook_obj, arg1, arg2) print("Hooked function called with:", arg1, arg2) -- Call original function local result = hook_obj(arg1, arg2) -- Modify return value return result + 100 end, "int(__cdecl*)(int, int)" -- Function signature ) -- Temporarily detach hook my_hook:Detach() -- Reattach hook my_hook:Attach() -- Permanently remove hook and restore original bytes my_hook:Remove() ``` -------------------------------- ### CreateInline - Inline Function Hooking Source: https://context7.com/syr2004/fatality-cs2-hook-lib/llms.txt Creates an inline hook by overwriting a target function's prologue with a jump to a detour function. It handles thread suspension, backup of original bytes, and installation of a 64-bit absolute jump shellcode. ```APIDOC ## POST /api/hook/createInline ### Description Creates an inline hook by overwriting the target function's prologue with a jump instruction to a detour function. The hook suspends all process threads during installation to prevent race conditions, backs up the original 14 bytes of the target function, and installs a 64-bit absolute jump shellcode. The returned hook object provides methods to attach, detach, and call the original function. ### Method POST ### Endpoint /api/hook/createInline ### Parameters #### Request Body - **target_address** (address) - Required - The memory address of the function to hook. - **detour_function** (function) - Required - The Lua function to be called when the target function is invoked. - **signature** (string) - Required - The C-style function signature of the target function. ### Request Example ```json { "target_address": "0x140001000", "detour_function": "function(hook_obj, arg1, arg2) ... end", "signature": "int(__cdecl*)(int, int)" } ``` ### Response #### Success Response (200) - **hook_object** (object) - An object representing the created hook, with methods like `Detach()`, `Attach()`, `Remove()`, and calling the original function. #### Response Example ```json { "hook_object": { "detach": "function()", "attach": "function()", "remove": "function()", "call_original": "function(args...)" } } ``` ``` -------------------------------- ### UnHooks - Remove All Hooks Source: https://context7.com/syr2004/fatality-cs2-hook-lib/llms.txt Removes all active hooks installed by the library and clears the hook registry. This function iterates through all hook objects and calls their Remove method. ```APIDOC ## POST /api/hook/unhooks ### Description Removes all active hooks installed by the library and clears the hook registry. This function iterates through all hook objects and calls their Remove method, restoring original function bytes or vtable entries. It's automatically called via Lua garbage collection when the library is unloaded. ### Method POST ### Endpoint /api/hook/unhooks ### Parameters No parameters required. ### Request Example ```json {} ``` ### Response #### Success Response (200) - **status** (string) - Indicates that all hooks have been successfully removed. #### Response Example ```json { "status": "All hooks removed successfully." } ``` ``` -------------------------------- ### Create Virtual Table Hook with Lua Source: https://context7.com/syr2004/fatality-cs2-hook-lib/llms.txt Installs a vtable hook by replacing a virtual function pointer in an object's vtable with a detour function pointer. This is suitable for C++ virtual methods. It modifies memory protection, swaps the function pointer, and restores protection. The original function can be called via the hook object. Requires the `hook_lib` module and `ffi`. ```lua local hook_lib = require("hook lib") -- Hook a virtual function at index 42 in an interface vtable local interface_ptr = ffi.cast("void*", 0x7FF000000000) -- Example interface pointer local vtable_hook = hook_lib.CreateVtable( interface_ptr, function(hook_obj, this_ptr, width, height) print("Virtual function intercepted") -- Modify parameters before calling original width = width * 2 height = height * 2 -- Call original virtual function return hook_obj(this_ptr, width, height) end, 42, -- Vtable index "int(__thiscall*)(void*, int, int)" -- Virtual function signature ) -- Check if hook is valid if vtable_hook:IsValid() then print("Vtable hook installed successfully") end -- Detach and remove hook vtable_hook:Detach() vtable_hook:Remove() ``` -------------------------------- ### Remove All Hooks with Lua Source: https://context7.com/syr2004/fatality-cs2-hook-lib/llms.txt Cleans up all active hooks installed by the library and clears the hook registry. This function iterates through all hook objects and calls their `Remove` method, effectively restoring original function bytes or vtable entries. It is also automatically called during garbage collection when the library is unloaded. ```lua local hook_lib = require("hook lib") -- Install multiple hooks local hook1 = hook_lib.CreateInline(address1, detour1, "void(__cdecl*)()") local hook2 = hook_lib.CreateInline(address2, detour2, "int(__cdecl*)(int)") local hook3 = hook_lib.CreateVtable(interface, detour3, 10, "void*(__thiscall*)(void*)") -- Remove all hooks at once hook_lib.UnHooks() -- All hooks are now detached and invalidated -- Calling hook1(), hook2(), or hook3() will return nil ``` -------------------------------- ### CreateVtable - Virtual Table Hooking Source: https://context7.com/syr2004/fatality-cs2-hook-lib/llms.txt Creates a vtable hook by replacing a virtual function pointer in an object's vtable with a detour function pointer. This is used for hooking C++ virtual methods without modifying actual function code. ```APIDOC ## POST /api/hook/createVtable ### Description Creates a vtable hook by replacing a virtual function pointer in an object's vtable with a detour function pointer. This method is used for hooking C++ virtual methods without modifying the actual function code. The hook modifies memory protection to allow writing to the vtable, swaps the function pointer, and restores protection. The original function can be invoked through the hook object. ### Method POST ### Endpoint /api/hook/createVtable ### Parameters #### Request Body - **interface_ptr** (pointer) - Required - A pointer to the object whose vtable will be modified. - **detour_function** (function) - Required - The Lua function to be called when the virtual function is invoked. - **vtable_index** (integer) - Required - The index of the virtual function within the vtable. - **signature** (string) - Required - The C-style signature of the virtual function. ### Request Example ```json { "interface_ptr": "0x7FF000000000", "detour_function": "function(hook_obj, this_ptr, width, height) ... end", "vtable_index": 42, "signature": "int(__thiscall*)(void*, int, int)" } ``` ### Response #### Success Response (200) - **hook_object** (object) - An object representing the created vtable hook, with methods like `IsValid()`, `Detach()`, and `Remove()`. #### Response Example ```json { "hook_object": { "is_valid": true, "detach": "function()", "remove": "function()" } } ``` ``` -------------------------------- ### Retrieve Active Hooks - Lua Source: https://context7.com/syr2004/fatality-cs2-hook-lib/llms.txt Retrieves a table of all currently active hook objects. Each hook object contains metadata like validity, attachment status, original function pointer, and backup data. Useful for debugging and inspecting the hooking state. Requires the 'hook_lib' module. ```lua local hook_lib = require("hook lib") -- Install some hooks hook_lib.CreateInline(0x140001000, my_detour1, "void(__cdecl*)()") hook_lib.CreateVtable(interface_ptr, my_detour2, 5, "int(__thiscall*)(void*, int)") -- Get all active hooks local active_hooks = hook_lib.GetHooks() -- Inspect hook states for i, hook in ipairs(active_hooks) do print(string.format("Hook %d: Valid=%s, Attached=%s", i, tostring(hook.bValid), tostring(hook.bAttached or hook.bAttach) )) -- Temporarily disable a specific hook if hook.bAttached then hook:Detach() end end -- Output example: -- Hook 1: Valid=true, Attached=true -- Hook 2: Valid=true, Attached=true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.