分享
 
 
 

对抗hook-defeating_hooks

王朝other·作者佚名  2006-04-28
窄屏简体版  字體: |||超大  

----------------------------------------------------------------------------------

(1) Defeating IAT or export table modification AND/OR In-line function hooking [1]

----------------------------------------------------------------------------------

- recall that Import/Export table modification does :

modify the IAT or export address table of the targeted process and all

the modules (DLLs) that process/app uses

each process and module has its own IAT that contains the function

addresses of functions that process/module uses

so we replace the addresses in that IAT with addresses to our hooking fxns

but this method has to take into consideration that some functions will

be dynamically loaded, e.g. thatdll = LoadLibrary( somedll.dll ) then addy =

GetProcAddress( thatdll, SomeFunction )

- so to fully hook would require hooking GetProcAddress so that it

returns the hooked function address rather than the actual function addy

- could also modify the export address table of the desired DLLs; this

this will cause GetProcAddress(...) to return the addys we desire

So: GetProcAddress( thatdll, SomeFunction ) looks at the EAT of

thatdll for SomeFunction; if we have altered the EAT to have our fxn

address, then our fxn addy will be returned instead of the real fxn addy

- recall that inline function hooking does : overwrite start of the

hooked API function with a jmp instruction that causes execution to be

transferred to our function ('the replacement function')

DEFEATING BOTH OF THESE APPROACHES :

- manually read ntdll.dll and kernel32.dll into memory using CreateFile, ReadFile

- we'll call these manually loaded images "file images"

- the memory images of ntdll.dll and kernel32.dll are what have been modified

- iterate through the EAT of the file images of ntdll.dll and kernel32.dll

-- for each function exported, get that function's length * N

-- then compare the first N bytes of each of that function from the file image

and the memory image

-- restore each hooked function by copying the relevant bytes from the

file image to the memory image

- iterate through the IAT of the memory image of kernel32.dll

-- for each imported function from ntdll.dll, check that the address

of this function hasn't been modified

-- if it has been modified, restore the IAT entry from the file image

- obtain the function address of CreateProcessA using the file image of kernel32.dll

- execute desired EXE (app) using CreateProcessA

NB: relies on fact that only memory image of app and DLLs modified and

that the actual file on disk (e.g. ntdll.dll) is not changed

- also seems to rely on fact that disk image of our executable hasn't

been changed (i.e. had its IAT overwritten on disk)

-------------------------------------------------------------------

(2) Evading userland hooks - problems w/hooking implementations [2]

-------------------------------------------------------------------

(a) incomplete hooking : don't hook BOTH ANSI and Unicode versions of function

- ansi fxn names end in A, e.g. CreateProcessA(...)

- unicode versions end in W, e.g. CreateProcessW(...)

- ansi functions usually just wrappers which call unicode versions

- e.g. hook CreateFileA(...) so then attacker just calls

CreateFileW(...) instead

(b) not hooking deeply enough

- kernel32.dll has a lot of functions that are mostly wrappers for ntdll

functions

- so if only hook kernel32.dll functions and NOT ntdll.dll functions,

hooking can be bypassed via calling the ntdll functions directly

- e.g. GetProcAddress from kernel32.dll and LdrGetProcedureAddress from

ntdll.dll

(c) hooking function f but NOT hooking function g which f calls

- e.g. may hook WinExec(...)

- but this is what WinExec(...) call path looks like :

WinExec(...) --> CreateProcessA(...) --> CreateProcessInternalA(...)

- if just hook WinExec(...) and CreateProcessA(...) and NOT

CreateProcessInternalA(...) then attacker can just call that last

fxn and evade detection

- CreateProcessInternalA(...) is exported by kernel32.dll so attacker

could look for its addy via loading kernel32.dll and iterating

through that module's EAT

- also this refers to problem mentioned in (1) whereby say some function

f lives in DLL d but f itself calls function g which lives in DLL d'

- so we need to recursively be hooking not only our target functions

but ensuring that any functions THOSE TARGET FUNCTIONS call are

likewise hooked

(d) detecting installation of in-line function hooks

- most win32 api functions begin with a 5-byte preamble

XP : pre-SP2

55; push ebp

8bec; mov ebp, esp

...

XP : post-SP2

8bff; mov edi, edi (?) -- pg. 75 of Hoglund Rootkits book

55; push ebp

8bec; mov ebp, esp

...

- after in-line function hooking, a function preamble will look like :

e8 xx xx xx xx ; call xx xx xx xx

54 ; push esp

53 ; push ebx

56 ; push esi

57 ; push edi

or instead of call will have jmp (e9)

- so before program P (which wants to bypass hooking) calls function f,

P looks at the code at f; if it looks like a normal preamble, call f

else if the first instruction is a call or a jmp to some address, then

likely in-line function hooking is at work so don't call f or do

something else... but in any event the hooking has been detected

--> putting this into detours terminology, basically what the attacker

would do after probabilistically detecting the hook (above) is

search for the table which contains the original 5-byte preamble

--> recall that for detours we have a target function T, a detour

function (replacement function) D, and a trampoline function TR

where when T is called, instead D is called then TR is called

which calls T

--> so attacker would attempt to call TR then T -- bypassing D

since TR and T must live in the app memory somewhere

--> or instead attacker can have its own preamble (keep track of the

original preamble) then call the target function + 5 bytes, so we

jump over the "jmp xx xx xx xx" or "call xx xx xx xx" function

which is our inline function hook (hook hopping)

==> won't work if another function in the call path is also hooked,

e.g. if do hook hopping on WinExec but then WinExec calls

CreateProcess and haven't done hook hopping on CreateProcess,

then evasion fails

- so would need to call CreateProcess using stored preamble

and do hook hopping on functions called by WinExec

(e) implementations which overwrite preambles must change the write bits

on that code ... so that the preamble can be overwritten with the jmp or

call instruction; but some such implementations never reset the writable

bit on these code preambles ... so attacker can overwrite overwritten

preamble with original preamble!

(f) IAT patching

- attacker overwrites IAT before hooking mechanism does detection

- then hooking mechanism will hook desired functions and make those

functions jump to replacement functions then to the "original API

function"

-- but "the original api function" isn't really, it's the function

address already overwritten by the attacker

- so the attacker acts as first mover

- alters the api addy to X

- then when hooker comes in, it will alter api addy to Y where the

code at Y does whatever then calls X

- in particular the systems being attacked here call various ntdll.dll

functions in order to obtain memory page information so the idea is to

replace those core functions used by the attackee with functions

replaced by the attacker

- context is that attackee is doing buffer overflow protection

- and is using ntdll.dll functions for this purpose (rather than

supplying its own versions of these functions)

(g) write code which replaces functionality of functions exported by ntdll.dll

- have already discussed how most ntdll.dll functions basically :

push args

move the syscall number to eax

move pointer to userland stack to edx

int 2e

- well attacker can write code which essentially replicates the

functionality of those ntdll.dll functions

- so if hook was of all ntdll.dll functions, then attacker can bypass

that detection

+ of course this doesn't work against kernel-land hooking ... where the

actual hooking doesn't come into play until *after* int 2e is executed

* requires knowing the syscall numbers of desired kernel syscalls as well as

the method signature for each such kernel syscall

(type and number and order of arguments to that kernel syscall so as

to conform to it)

----------------------------

(3) Evading kernel hooks [3]

----------------------------

- basically kernel-level hooks (in NT/2k/XP/2003) rely on modifying the SSDT

- so this basically restores the SSDT from the disk image of ntoskrnl.exe

- the methods for doing this restoration mirror closely those which are

used to alter the SSDT in the first place

obviously assumes that kernel hooking done via overwriting memory image

of ntoskrnl.exe data structures (e.g. KiServiceTable) and that disk image

of ntoskrnl.exe is legit.

- requires some address conversion from original addys on disk image to RVAs

++++++++++++

References :

++++++++++++

(1) Anti API hooking

http://www.security.org.sg/code/antihookexec.html

(2) Evading userland hooks and Evading kernel hooks

http://www.phrack.org/phrack/62/p62-0x05_Bypassing_Win_BufferOverflow_Protection.txt

(3) Defeating native API hookers

http://www.security.org.sg/code/SIG2_DefeatingNativeAPIHookers.pdf

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有