linux核心hook技術之指令覆蓋與注入

2021-10-20 19:16:34 字數 2582 閱讀 6074

說到hook,傳統意義上,大家都會覺得跟注入和劫持掛鉤。在linux核心中,也可以通過指令覆蓋和注入的方式進行hook,來完成自己的業務邏輯,實現自己的功能需求。

一部分人喜歡稱這種hook技術為inline hook。

具體hook細節在以下編寫的驅動例子程式中給出了,例子中標註了詳細的注釋,大家可對照著**檢視。

例子程式在centos 6系統上編譯並測試通過了,如果換成其他系統,部分**可能需要進行微調,想要嘗試的朋友,自己寫個簡單的makefile檔案編譯即可。

例子程式中以check_kill_permission核心函式為例進行了hook,裝載時需要給驅動傳遞kallsyms_lookup_name函式位址作為引數。

eg: insmod **.ko kallsyms_lookup_name=0x*******,具體位址0x*****可通過指令 cat /proc/kallsyms | grep kallsyms_lookup_name獲取。

#include #include #include #include #include #include #include #include /*使用check_kill_permission函式來進行hook測試*/

#define hook0_sys_open_name "check_kill_permission"

#define hook0_insn_init_name "insn_init"

#define hook0_insn_get_length_name "insn_get_length"

typedef unsigned long (* kallsyms_lookup_name_t)(const char *name);

typedef void (* insn_get_length_t)(struct insn *insn);

typedef void (* insn_init_t)(struct insn *insn,

const void *kaddr,

int x86_64);

typedef int (* hook0_stub_t) (int sig,

struct siginfo *info,

struct task_struct *t);

static ulong kallsyms_lookup_name_address;

static void *origin_function_head;

static void *origin_function_next;

static kallsyms_lookup_name_t hook0_lookup_name;

static insn_init_t hook0_insn_init;

static insn_get_length_t hook0_insn_get_length;

static hook0_stub_t p_hook0_stub;

static uint hook0_insn_len = 0;

static ulong g_wpbit_val = 0;

/* *佔位函式

*函式體裡面的**用來佔位,無其他意義

*do_stub稍後會被其他指令覆蓋,用來跳轉會原函式

* */

static int do_stub(int sig,

struct siginfo *info,

struct task_struct *t)

/* *鉤子函式

* 通俗點說就是想為所欲為的地方

* */

static int hook0_new_function(int sig,

struct siginfo *info,

struct task_struct *t)

/* *寫保護關閉函式

* */

static void hook0_clear_wpbit(void)

/* * 寫保護位恢復函式

* */

static void hook0_recover_wpbit(void)

/* *獲取指令長度

* */

static int hook0_get_length(char *pc_addr)

/* *鉤子函式注入的地方

*此處主要通過注入jmp命令來實現的

* */

static int hook0_do_hijack(void *arg)

static int hook0_recover_hijack(void *arg)

static int hook0_init(void)

static void hook0_exit(void)

module_init(hook0_init);

module_exit(hook0_exit);

module_param(kallsyms_lookup_name_address, ulong, 0644);

module_license("gpl");

使用指令注入等hook技術時候,有幾點可能需要注意下:

對這類注入的檢測手段主要是在函式入口點進行jmp和call等跳轉指令檢測,從而判斷函式是不是已經被汙染了。

linux核心hook技術之跳轉指令偏移值

在另一篇博文中提到了指令覆蓋和指令注入的hook方式,使用覆蓋和注入方式完成核心函式hook,需要有很多的注意事項,而且容易被檢測工具檢測。這篇博文則聊一下如何通過替換跳轉指令偏移值來完成核心函式的hook,這種hook技術也可以稱為inline hook。事先做個準備工作,手頭正好有centos ...

linux核心hook技術之函式位址替換

函式位址替換是一種更為簡單 常見的hook方式,比如對security ops sys call table等結構中的函式進行替換,來完成自己的安全許可權控制。其中security ops是lsm框架中所使用的,sys call table是系統呼叫表結構。當然了,這些結構目前在核心中都已經是唯讀資...

Hook 核心函式技術細節

剛開始看到通過 ssdt 來 hook zw 核心函式的方法時不是很了解,等把 zw 反彙編後才發現技術細節.原來也沒什麼新鮮的,就是找到目標函式在 ssdt 中的位置 偏移量 位置 4 然後儲存並替換偏移量處的值為自己新的函式位址就行了。這種技術現在已是老掉牙了,不過在實際的軟體開發中也比較常用,...