用hook實現dll注入詳解

2021-03-31 08:56:29 字數 1532 閱讀 7168

//下面說明 inject.dll的setinjecthook的具體操作

在全域性定義以下變數

varg_hhook :hhook=0;

g_dwthreadidinject :dword=0;

g_hinjectfrm:hwnd;

function setinjecthook(dwthreadid:dword):boolean;

begin

result := false;

//如果執行緒標誌為0則用於去掉鉤子,否則進行動態庫注入

if dwthreadid<>0 then

begin

assert(g_hhook=0);

//儲存當前執行緒的id到 g_dwthreadidinject

g_dwthreadidinject := getcurrentthreadid;

//下乙個getmessage的鉤子到目標執行緒

//getmsgproc是在下面定義的乙個函式,在第一次呼叫時將自定義的form在目標執行緒中建立出來

//這樣就能通過這個自定義的form對目標執行緒進行程序內控制了

g_hhook := setwindowshookex(wh_getmessage,getmsgproc,hinstance,dwthreadid);

result := g_hhook <> null;

if result then

//發乙個空的資訊以便於立即建立這個自定義form

result := postthreadmessage(dwthreadid, wm_null,0,0);

//等待半秒鐘,以保證呼叫者可以找到這個剛建立的form

sleep(500);

end else

begin

assert(g_hhook<>0);

//去掉鉤子

result := unhookwindowshookex(g_hhook);

g_hhook := 0;

end;

end;

//定義乙個全域性的是否第乙個訊息的標誌

varffirsttime:boolean = true;

//這個函式用於在收到第乙個訊息時建立自定義窗體,以便於遠端控制

function getmsgproc(code: integer; wparam: wparam; lparam: lparam): lresult; stdcall;

begin

//如果是第一次

if ffirsttime then

begin

ffirsttime := false;

//建立窗體

injectfrm := tinjectfrm.create(nil);

//儲存窗體控制代碼

g_hinjectfrm := injectfrm.handle;

end;

//呼叫預設處理,這一句可不能忘記

result := callnexthookex(g_hhook,code,wparam,lparam);

end;

用hook實現dll注入詳解

需要乙個用來注入的dll inject.dll 及乙個呼叫程式 caller.exe 流程 caller.exe procedure testhook var pwnd,hchild,hwndinject hwnd msg tmsg begin 通過視窗標題用findwindow找到要注入的程式的主...

用hook實現dll注入詳解

需要乙個用來注入的dll inject.dll 及乙個呼叫程式 caller.exe 流程 caller.exe procedure testhook var pwnd,hchild,hwndinject hwnd msg tmsg begin 通過視窗標題用findwindow找到要注入的程式的主...

用hook實現dll注入詳解

該日誌由傻貓發表於 2010 07 21 13 00 31 需要乙個用來注入的 dll inject.dll 及乙個呼叫程式 caller.exe 流程 caller.exe procedure testhook var pwnd,hchild,hwndinject hwnd msg tmsg be...