lunar-sh/cdl86
GitHub: lunar-sh/cdl86
一个用 C 语言编写的微型跨平台函数钩子库,用于在运行时拦截和重定向 x86_64 二进制函数调用。
Stars: 28 | Forks: 5
# cdl86
```
________ __ ___ ____
/ ___/ _ \/ / ( _ )/ __/
/ /__/ // / /__/ _ / _ \
\___/____/____/\___/\___/
```
`cdl86` - `Compact Detours Library 86`
# 摘要
`cdl86` 是一个微型的、单文件的、跨平台的 `detours` 库,用 `C` 语言编写,代码量少于 `1K` 行。
[`https://journal.lunar.sh/2022/linux-detours.html`](https://journal.lunar.sh/2022/linux-detours.html)
它允许在代码注入后,拦截内存中的 `x86` 和 `x86_64` `C/C++` 二进制函数。
目前支持以下平台:
- `Linux (Ubuntu)`
- `MS Windows`
该库目前支持两种类型的函数钩子(hook):
* `JMP` 补丁 - 使用 `JMP` 指令修补原始函数以跳转到 detour
* `INT3` 补丁 - 在原始地址放置软件断点(`SWBP`)
处理到 `detour` 的 `控制流` # API ``` struct cdl_jmp_patch cdl_jmp_attach(void **target, void *detour); struct cdl_swbp_patch cdl_swbp_attach(void **target, void *detour); void cdl_jmp_detach(struct cdl_jmp_patch *jmp_patch); void cdl_swbp_detach(struct cdl_swbp_patch *swbp_patch); void cdl_jmp_dbg(struct cdl_jmp_patch *jmp_patch); void cdl_swbp_dbg(struct cdl_swbp_patch *swbp_patch); ``` `API` 在相应的 `头文件` 和 `源文件` 中有更详细的文档说明。 # 示例用法 假设我们有一个定义如下的 `add()` 函数: ``` int add( __in int x, __in int y ) { printf("Inside original function\n"); return x + y; } ``` 假设我们要用 `cdl86` 钩住这个函数,第一步是定义一个函数指针 `typedef` 并声明我们的 `detour` 函数: ``` typedef int add_t( __in int x, __in int y ); add_t* addo = NULL; ``` ``` // define detour function int add_detour( __in int x, __in int y ) { printf("Inside detour function\n"); return addo(5,5); } ``` 然后在我们的 `main()` 函数中,创建一个 `cdl_jmp_patch` 结构体并为 `addo`(我们指向原始函数的函数指针)赋值: ``` struct cdl_jmp_patch jmp_patch = {}; addo = (add_t*)add; ``` 最后按如下方式调用 `cdl_jmp_attach`: ``` jmp_patch = cdl_jmp_attach((void**)&addo, add_detour); ``` 原始函数 `add` 现在已经被钩住了! 要从 `cdl_jmp_patch` 结构体转储 `debug` 信息,请使用 `cdl_jmp_dbg`。 # 信息 本项目使用的编译器是 [`tcc`](https://github.com/lunar-sh/tcc)。 文件:
* `cdl.c` - `CDL` 的 `C` 源文件 * `cdl.h` - 要包含的 `CDL` 头文件 文件夹: * `/tests` - `CDL` 测试套件 # 签名 ``` +---------------------------------------+ | .-. .-. .-. | | / \ / \ / \ + | | \ / \ / \ / | | "_" "_" "_" | | | | _ _ _ _ _ _ ___ ___ _ _ | | | | | | | | \| | /_\ | _ \ / __| || | | | | |_| |_| | .` |/ _ \| /_\__ \ __ | | | |____\___/|_|\_/_/ \_\_|_(_)___/_||_| | | | | | | Lunar RF Labs | | Email: root@lunar.sh | | | | Research Laboratories | | OpenAlias (BTC, XMR): lunar.sh | | Copyright (C) 2022-2024 | +---------------------------------------+ ```
处理到 `detour` 的 `控制流` # API ``` struct cdl_jmp_patch cdl_jmp_attach(void **target, void *detour); struct cdl_swbp_patch cdl_swbp_attach(void **target, void *detour); void cdl_jmp_detach(struct cdl_jmp_patch *jmp_patch); void cdl_swbp_detach(struct cdl_swbp_patch *swbp_patch); void cdl_jmp_dbg(struct cdl_jmp_patch *jmp_patch); void cdl_swbp_dbg(struct cdl_swbp_patch *swbp_patch); ``` `API` 在相应的 `头文件` 和 `源文件` 中有更详细的文档说明。 # 示例用法 假设我们有一个定义如下的 `add()` 函数: ``` int add( __in int x, __in int y ) { printf("Inside original function\n"); return x + y; } ``` 假设我们要用 `cdl86` 钩住这个函数,第一步是定义一个函数指针 `typedef` 并声明我们的 `detour` 函数: ``` typedef int add_t( __in int x, __in int y ); add_t* addo = NULL; ``` ``` // define detour function int add_detour( __in int x, __in int y ) { printf("Inside detour function\n"); return addo(5,5); } ``` 然后在我们的 `main()` 函数中,创建一个 `cdl_jmp_patch` 结构体并为 `addo`(我们指向原始函数的函数指针)赋值: ``` struct cdl_jmp_patch jmp_patch = {}; addo = (add_t*)add; ``` 最后按如下方式调用 `cdl_jmp_attach`: ``` jmp_patch = cdl_jmp_attach((void**)&addo, add_detour); ``` 原始函数 `add` 现在已经被钩住了! 要从 `cdl_jmp_patch` 结构体转储 `debug` 信息,请使用 `cdl_jmp_dbg`。 # 信息 本项目使用的编译器是 [`tcc`](https://github.com/lunar-sh/tcc)。 文件:
* `cdl.c` - `CDL` 的 `C` 源文件 * `cdl.h` - 要包含的 `CDL` 头文件 文件夹: * `/tests` - `CDL` 测试套件 # 签名 ``` +---------------------------------------+ | .-. .-. .-. | | / \ / \ / \ + | | \ / \ / \ / | | "_" "_" "_" | | | | _ _ _ _ _ _ ___ ___ _ _ | | | | | | | | \| | /_\ | _ \ / __| || | | | | |_| |_| | .` |/ _ \| /_\__ \ __ | | | |____\___/|_|\_/_/ \_\_|_(_)___/_||_| | | | | | | Lunar RF Labs | | Email: root@lunar.sh | | | | Research Laboratories | | OpenAlias (BTC, XMR): lunar.sh | | Copyright (C) 2022-2024 | +---------------------------------------+ ```
标签:Detours库, EDR绕过, Hook库, INT3断点, JMP补丁, x86_64, XML 请求, 中高交互蜜罐, 二进制拦截, 云资产清单, 内存操作, 内联Hook, 函数挂钩, 动态修补, 安全开发, 客户端加密, 恶意软件开发, 控制流劫持, 系统编程, 轻量级库, 逆向工程, 高交互蜜罐