feat: dynamicly hooking x64 functions from lua at runtime with arbitrary signatures. (#3301)

* feat: dynamicly hooking x64 functions from lua at runtime with arbitrary signatures.

* fix missing include

* c/c++ include system is a good system
This commit is contained in:
Quentin
2024-07-08 14:15:18 +02:00
committed by GitHub
parent 495f7c0c27
commit 95969031df
13 changed files with 874 additions and 19 deletions

View File

@ -0,0 +1,31 @@
# Class: value_wrapper
Class for wrapping parameters and return value of functions, used mostly by the dynamic_hook system.
## Functions (2)
### `get()`
Get the value currently contained by the wrapper.
- **Returns:**
- `any`: The current value.
**Example Usage:**
```lua
any = value_wrapper:get()
```
### `set(new_value)`
Set the new value contained by the wrapper.
- **Parameters:**
- `new_value` (any): The new value.
**Example Usage:**
```lua
value_wrapper:set(new_value)
```

View File

@ -2,7 +2,7 @@
Table containing helper functions related to process memory.
## Functions (5)
## Functions (6)
### `scan_pattern(pattern)`
@ -68,4 +68,39 @@ pointer = memory.allocate(size)
memory.free(ptr)
```
### `dynamic_hook(hook_name, return_type, param_types, target_func_ptr, pre_callback, post_callback)`
**Example Usage:**
```lua
local ptr = memory.scan_pattern("some ida sig")
memory.dynamic_hook("test_hook", "float", {"const char*"}, ptr,
function(ret_val, str)
--str:set("replaced str")
ret_val:set(69.69)
log.info("pre callback from lua", ret_val:get(), str:get())
-- false for skipping the original function call
return false
end,
function(ret_val, str)
log.info("post callback from lua 1", ret_val:get(), str:get())
ret_val:set(79.69)
log.info("post callback from lua 2", ret_val:get(), str:get())
end)
```
- **Parameters:**
- `hook_name` (string): The name of the hook.
- `return_type` (string): Type of the return value of the detoured function.
- `param_types` (table<string>): Types of the parameters of the detoured function.
- `target_func_ptr` (memory.pointer): The pointer to the function to detour.
- `pre_callback` (function): The function that will be called before the original function is about to be called. The callback must match the following signature: ( return_value (value_wrapper), arg1 (value_wrapper), arg2 (value_wrapper), ... ) -> Returns true or false (boolean) depending on whether you want the original function to be called.
- `post_callback` (function): The function that will be called after the original function is called (or just after the pre callback is called, if the original function was skipped). The callback must match the following signature: ( return_value (value_wrapper), arg1 (value_wrapper), arg2 (value_wrapper), ... ) -> void
**Example Usage:**
```lua
memory.dynamic_hook(hook_name, return_type, param_types, target_func_ptr, pre_callback, post_callback)
```