使用IAT表注入模組到程序中 樣例

2021-08-01 02:07:00 字數 2345 閱讀 7227

模組注入到程序的方法有很多,想一一寫例子,學習一下,也練習一下!

首先先從iat表注入開始吧!

如果了解pe檔案的格式的話,原理很簡單。當乙個模組被系統載入起來後,會遍歷模組的匯入表,將靜態匯入的函式位址填充到匯入表中,以便程序執行起來後,呼叫到該模組時,能夠使用匯入函式。要獲取匯入函式位址,首先要將匯出該函式的模組載入起來。

那方法來了,我們是否可以在程序啟動時,在必然被載入的模組(比如exe模組,ntdll.dll,kernel32.dll等)匯入表被系統遍歷之前(注意了,一定是匯入表被遍歷之前。),將匯入表修改一下,新增進我們的模組,並且將相應的iat的資料新增進來。這樣,當系統遍歷到該模組的匯入表時,遍歷到我們自己的模組的匯入表項,它就會將我們的模組載入起來,並且將模組匯出函式的位址填到我們自己組織的匯入表函式項中。

如下是pe檔案中匯入表的結構:

首先按照上述的結構,為我們的dll準備乙個匯入表(我們以修改test.exe的匯入表,注入dll.dll模組為例):        

[cpp]view plain

copy

typedef

struct

_image_import_descriptor ;  

dword

timedatestamp;                  

// 0 if not bound,

// -1 if bound, and real date\time stamp

//     in image_directory_entry_bound_import (new bind)

// o.w. date/time stamp of dll bound to (old bind)

dword

forwarderchain;                 

// -1 if no forwarders

dword

name;  

dword

firstthunk;                     

// rva to iat (if bound this iat has actual addresses)

} image_import_descriptor;  

typedef

image_import_descriptor unaligned *pimage_import_descriptor;  

簡單一些,通過test.exe nt頭的 datadirectory 目錄的 import table對應的位址以及大小,將對應的匯入表資訊(image_import_descriptor結構體組成的陣列)複製出來,並且在最後新增一項。

對image_import_descriptor 結構體,我們需要填充三項,originalfirstthunk 域,name域,firstthunk域。為了簡便,對於dll.dll我們只匯入乙個函式即可。注意,name使用了全路徑,這樣就可以簡化模組路徑的設定,防止模組所在路徑不在path 中,而無法載入模組。

為了構造 originalfirstthunk / firstthunk 偏移所對應的內容,以及name偏移模組全路徑。定義如下的結構體:

[cpp]view plain

copy

// 匯入表定義

typedef

struct

tag_iattablecontent  

iattablecontent, *lpiattablecontent;  

將匯入表內容進行填寫,originalthunk / firstthunk同時指向 importbyname ,即它的偏移值(相對於test.exe起始位址)。注意szfuncname 這個字元陣列用於擴充套件importbyname的name域,以有足夠的空間容納函式名稱。image_import_by_name結構體的 name域只有乙個位元組,需要額外為它新增空間。

將 image_import_descriptor 的name的偏移設定為 szdllname 的偏移。

通過上述的過程,我們就已經完成了乙個新的匯入表構造,剩下的工作就是在模組的匯入表未被系統遍歷之前將我們構造的匯入表賦值給模組nt頭中。(詳細內容可以參考**)

例子使用了一種取巧的方法,呼叫createprocessw函式時,將第六個引數 dwcreateflags 設定為create_suspended,這樣建立程序後,程序馬上被掛起,程式中的第乙個模組test.exe的匯入表還沒有被遍歷。所以在這時我們可以進行我們的「非法」的勾當,達到**的目的。

by  andy  @ 2015-07-30

程式設計實現APC注入dll到程序

執行緒被喚醒時apc中的註冊函式會被執行,因此使用queueuserapc向apc佇列插入函式即可完成注入 include include include bool apcinject char char dword getprocessidbyprocessname char bool getal...

檢視程序是否被注入執行緒(不在系統模組)

首先我們拿到該程序的程序模組,用來對比程序中線程的模組是否在這裡面 把這些模組放入到vector裡面 便利該程序的所有執行緒獲取模組的起始位址和大小,很快就可以得到偽裝執行緒。pragma region 依賴 typedef enum threadinfoclassthreadinfoclass t...

DLL注入 使用登錄檔進行DLL注入

實驗原理myhack2.dll原始碼 include windows.h include tchar.h define def cmd l c program files internet explorer iexplorer.exe define def addr l define def dst...