# 2021q3 Homework1 (quiz1) contributed by < 2011eric > 08/27 簽到 ## 背景知識 - [Linux 核心設計: 不僅是個執行單元的 Process](/7F6fBU6tRzuWM6ZPyoq0xA) - [Linux 核心模組運作原理](/pfRzP9sxRYqYbXS6Tm0Ynw) - [Linux Rootkits Part 1: Introduction and Workflow](https://xcellerator.github.io/posts/linux_rootkits_01/) 這篇文章介紹如何編寫並載入 `kernel module`,這些內容在 lkmpg 的前20頁都有涵蓋到,算是複習。 - [Linux Rootkits Part 2: Ftrace and Function Hooking](https://xcellerator.github.io/posts/linux_rootkits_02/) 介紹如何透過 `ftrace` 來 hook `kernel api` ## 利用Ftrace實作Function Hooking ``` struct ftrace_hook { const char *name; void *function; void *original; unsigned long address; struct ftrace_ops ops; }; ``` 在[Linux Rootkits Part 2: Ftrace and Function Hooking](https://xcellerator.github.io/posts/linux_rootkits_02/)中,筆者利用這個 `struct` 來儲存 `hook` 相關的資訊 `function` 儲存用來 `hook` 的函式, `original` 儲存原函式的 `function pointer` 關於 `ftrace_ops` 結構的定義可參考 [Kernel API](https://www.kernel.org/doc/html/latest/trace/ftrace-uses.html)中的說明 ``` struct ftrace_ops ops = { .func = my_callback_func, .flags = MY_FTRACE_FLAGS .private = any_private_data_structure, }; ``` `ftrace_hook` 中的 address` 代表 `ftrace` 要追蹤的函式,當 `rip` 指向該函式時, 就會呼叫 `ftrace_ops` 裡面定義的 `callback_func` 對 `ftrace_ops` 的設定如下: ``` ops.func = hook_ftrace_thunk; //callback_func ops.flags = FTRACE_OPS_FL_SAVE_REGS |FTRACE_OPS_FL_RECURSION_SAFE |FTRACE_OPS_FL_IPMODIFY; ``` 當中,因為我們會在 `callback_func` 中修改 `rip` 的值, 但 `ftrace` 當中針對遞迴的保護機制需要用到 `rip` ,因此使用 `FTRACE_OPS_FL_IPMODIFY` 來告知我們會修改到 `rip` ,並用 `FTRACE_OPS_FL_RECURSION_SAFE` 來關閉內建的保護,但這也代表我們必須自行檢查程式呼叫是否發生遞迴
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up