C/C++劫持技术(函数劫持、dll注入、动态库注入、HOOK)
目錄
劫持
detours
實(shí)現(xiàn)劫持
步驟:
1. 安裝Detours
2. 編譯Detours工程
3. 把靜態(tài)庫(kù)和頭文件引入工程
4. 函數(shù)指針與函數(shù)的定義
5.攔截
劫持QQ
實(shí)現(xiàn)劫持system函數(shù)。
1. 設(shè)置項(xiàng)目生成dll
2. 源文件(注意:需要保存為.c文件,或者加上extern C,因?yàn)閐etours是使用C語(yǔ)言實(shí)現(xiàn)的,表示代碼使用C的規(guī)則進(jìn)行編譯)?
3. 生成"劫持1.dll"文件
4. 把dll注入到QQ.exe
5. 攔截QQ執(zhí)行system函數(shù)
參考
劫持
- 劫持的原理就是把目標(biāo)函數(shù)的指針的指向修改為自定義函數(shù)的地址。
- 函數(shù)是放在內(nèi)存中的代碼區(qū),所以劫持與代碼區(qū)密切相關(guān)。
- 實(shí)現(xiàn)劫持需要使用detours。
detours
- detours是微軟亞洲研究院出口的信息安全產(chǎn)品,主要用于劫持。這個(gè)工具使用C語(yǔ)言實(shí)現(xiàn),所以是跨平臺(tái)的。
- detours根據(jù)函數(shù)指針改變函數(shù)的行為,可以攔截任何函數(shù),即使操作系統(tǒng)函數(shù)。
detours下載地址:
下載地址1: http://research.microsoft.com/en-us/downloads/d36340fb-4d3c-4ddd-bf5b-1db25d03713d/default.aspx
下載地址2: http://pan.baidu.com/s/1eQEijtS
實(shí)現(xiàn)劫持
開(kāi)發(fā)環(huán)境說(shuō)明:win7、vs2012
步驟:
1. 安裝Detours
2. 編譯Detours工程
在安裝目錄C:\Program Files\Microsoft Research\Detours Express 3.0\src目錄下的是工程的源文件。
????????(1) 打開(kāi)VS2012命令行工具,進(jìn)入src目錄。
????????(2) 使用nmake(linux下是make)命令編譯生成靜態(tài)庫(kù)。
?????????(3) 在lib.x86目錄下的.lib文件是win32平臺(tái)下的靜態(tài)庫(kù)文件
?????????(4) 在include目錄下的是Detours工程的頭文件
3. 把靜態(tài)庫(kù)和頭文件引入工程
// 引入detours頭文件 #include "detours.h"// 引入detours.lib靜態(tài)庫(kù) #pragma comment(lib,"detours.lib")4. 函數(shù)指針與函數(shù)的定義
????????(1) 定義一個(gè)函數(shù)指針指向目標(biāo)函數(shù),這里目標(biāo)函數(shù)是system
????????例如:
????????detour在realse模式生效(因?yàn)閂S在Debug模式下已經(jīng)把程序中的函數(shù)劫持了)
static int ( *oldsystem)(const char * _Command) = system;//定義一個(gè)函數(shù)指針指向目標(biāo)函數(shù)????????(2) 定義與目標(biāo)函數(shù)原型相同的函數(shù)替代目標(biāo)函數(shù)
????????例如:
//3.定義新的函數(shù)替代目標(biāo)函數(shù),需要與目標(biāo)函數(shù)的原型相同 int newsystem(const char * _Command){int result = MessageBoxA(0,"是否允許該程序調(diào)用system命令","提示",1);//printf("result = %d", result);if (result == 1){oldsystem(_Command); //調(diào)用舊的函數(shù)}else{MessageBoxA(0,"終止調(diào)用system命令","提示",0);}return 0; }5.攔截
//開(kāi)始攔截 void Hook() {DetourRestoreAfterWith();//恢復(fù)原來(lái)狀態(tài)(重置)DetourTransactionBegin();//攔截開(kāi)始DetourUpdateThread(GetCurrentThread());//刷新當(dāng)前線程(刷新生效)//這里可以連續(xù)多次調(diào)用DetourAttach,表明HOOK多個(gè)函數(shù)DetourAttach((void **)&oldsystem, newsystem);//實(shí)現(xiàn)函數(shù)攔截DetourTransactionCommit();//攔截生效 } //取消攔截 void UnHook() {DetourTransactionBegin();//攔截開(kāi)始DetourUpdateThread(GetCurrentThread());//刷新當(dāng)前線程//這里可以連續(xù)多次調(diào)用DetourDetach,表明撤銷(xiāo)多個(gè)函數(shù)HOOKDetourDetach((void **)&oldsystem, newsystem); //撤銷(xiāo)攔截函數(shù)DetourTransactionCommit();//攔截生效 }劫持QQ
實(shí)現(xiàn)劫持system函數(shù)。
1. 設(shè)置項(xiàng)目生成dll
2. 源文件(注意:需要保存為.c文件,或者加上extern C,因?yàn)閐etours是使用C語(yǔ)言實(shí)現(xiàn)的,表示代碼使用C的規(guī)則進(jìn)行編譯)?
#include #include #include // 引入detours頭文件 #include "detours.h"//1.引入detours.lib靜態(tài)庫(kù) #pragma comment(lib,"detours.lib")//2.定義函數(shù)指針 static int ( *oldsystem)(const char * _Command) = system;//定義一個(gè)函數(shù)指針指向目標(biāo)函數(shù)//3.定義新的函數(shù)替代目標(biāo)函數(shù),需要與目標(biāo)函數(shù)的原型相同 int newsystem(const char * _Command){char cmd[100] = {0};int result = 0;sprintf_s(cmd,100, "是否允許該程序執(zhí)行%s指令", _Command);result = MessageBoxA(0,cmd,"提示",1);//printf("result = %d", result);if (result == 1) // 允許調(diào)用{oldsystem(_Command); //調(diào)用舊的函數(shù)}else{// 不允許調(diào)用}return 0; }// 4.攔截 //開(kāi)始攔截 _declspec(dllexport) void Hook() // _declspec(dllexport)表示外部可調(diào)用,需要加上該關(guān)鍵字其它進(jìn)程才能成功調(diào)用該函數(shù) {DetourRestoreAfterWith();//恢復(fù)原來(lái)狀態(tài)(重置)DetourTransactionBegin();//攔截開(kāi)始DetourUpdateThread(GetCurrentThread());//刷新當(dāng)前線程(刷新生效)//這里可以連續(xù)多次調(diào)用DetourAttach,表明HOOK多個(gè)函數(shù)DetourAttach((void **)&oldsystem, newsystem);//實(shí)現(xiàn)函數(shù)攔截DetourTransactionCommit();//攔截生效 }//取消攔截 _declspec(dllexport) void UnHook() {DetourTransactionBegin();//攔截開(kāi)始DetourUpdateThread(GetCurrentThread());//刷新當(dāng)前線程//這里可以連續(xù)多次調(diào)用DetourDetach,表明撤銷(xiāo)多個(gè)函數(shù)HOOKDetourDetach((void **)&oldsystem, newsystem); //撤銷(xiāo)攔截函數(shù)DetourTransactionCommit();//攔截生效 }// 劫持別人的程序:通過(guò)DLL注入,并調(diào)用Hook函數(shù)實(shí)現(xiàn)劫持。 // 劫持系統(tǒng):通過(guò)DLL注入系統(tǒng)程序(如winlogon.exe)實(shí)現(xiàn)劫持系統(tǒng)函數(shù)。_declspec(dllexport) void main(){Hook(); // 攔截system("tasklist"); //彈出提示框UnHook(); // 解除攔截system("ipconfig"); //成功執(zhí)行system("pause"); // 成功執(zhí)行 }3. 生成"劫持1.dll"文件
4. 把dll注入到QQ.exe
DLL注入工具下載: https://coding.net/u/linchaolong/p/DllInjector/git/raw/master/Xenos.exe
????????(1) 打開(kāi)dll注入工具,點(diǎn)擊add,選擇"劫持1.dll"
????????(2) 在Process中選擇QQ.exe,點(diǎn)擊Inject進(jìn)行注入。
????????(3) 點(diǎn)擊菜單欄Tools,選擇Eject modules顯示當(dāng)前QQ.exe進(jìn)程中加載的所有模塊,如果有"劫持1.dll"表示注入成功。
5. 攔截QQ執(zhí)行system函數(shù)
????????(1) 點(diǎn)擊Advanced,在Init routine中填寫(xiě)動(dòng)態(tài)庫(kù)(dll)中的函數(shù)的名稱(chēng),如Hook,然后點(diǎn)擊Inject進(jìn)行調(diào)用。此時(shí),我們已經(jīng)把system函數(shù)劫持了。
????????(2) 點(diǎn)擊Advanced,在Init routine中填寫(xiě)main,執(zhí)行動(dòng)態(tài)庫(kù)中的main函數(shù)。
?????????此時(shí),彈出一個(gè)對(duì)話框,問(wèn)是否允許執(zhí)行tasklist指令,表示成功把system函數(shù)攔截下來(lái)了。
參考
DLL注入工具源碼地址: https://coding.net/u/linchaolong/p/DllInjector/git
說(shuō)明:
該工具來(lái)自以下兩個(gè)項(xiàng)目
Xenos: https://github.com/DarthTon/Xenos.git
Blackbone: https://github.com/DarthTon/Blackbone
總結(jié)
以上是生活随笔為你收集整理的C/C++劫持技术(函数劫持、dll注入、动态库注入、HOOK)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: shell练习03 安装mysql
- 下一篇: PAT-B1032