Structured Exception Handling,即SEH,是windows的异常处理机制.官方已不建议使用SEH,而是用ISO标准的c++异常处理.但是因为要向前兼容,所以在windows下SEH还是可以使用的,除了系统的代码大量使用以外,自己在一些特殊情况下,也是非常好用的.
SEH结构
SEH其实说穿了就是几个结构体,然后window根据结构体的信息来进行相应处理.
即定义SEH的结构体ntdll!_EXCEPTION_REGISTRATION_RECORD,与SEH处理异常接受异常信息的结构体VCRUNTIME140!_EXCEPTION_RECORD
大致既是:
_EXCEPTION_REGISTRATION_RECORD->handle->_except_handler4->_except_handler4_common()->_EXCEPTION_RECORD
1 | //winseh.cpp |
SEH异常处理函数
相关处理函数1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
430:000> u 77a25196 //SEH最终处理
ntdll!FinalExceptionHandlerPad22:
77a25196 90 nop
ntdll!FinalExceptionHandlerPad23:
77a25197 90 nop
ntdll!FinalExceptionHandlerPad24:
77a25198 90 nop
...more
77a251c0 e9f2b80500 jmp ntdll!_FinalExceptionHandler (77a80ab7)
0:000> u 010f1385
winseh!_except_handler4 [d:\agent\_work\3\s\src\vctools\crt\vcstartup\src\eh\i386\chandler4gs.c @ 84]:
010f1385 55 push ebp
010f1386 8bec mov ebp,esp
010f1388 ff7514 push dword ptr [ebp+14h]
010f138b ff7510 push dword ptr [ebp+10h]
010f138e ff750c push dword ptr [ebp+0Ch]
010f1391 ff7508 push dword ptr [ebp+8]
010f1394 68a8130f01 push offset winseh!__security_check_cookie (010f13a8)
010f1399 6804300f01 push offset winseh!__security_cookie (010f3004)
010f139e e8370b0000 call winseh!except_handler4_common (010f1eda) <----
010f13a3 83c418 add esp,18h
010f13a6 5d pop ebp
010f13a7 c3 ret
0:000> x vcruntime140!_except_handler4_common
74fe4480 VCRUNTIME140!_except_handler4_common (
unsigned int *, //0x0 __security_cookie
<function> *, //0x4 __security_check_cookie
struct _EXCEPTION_RECORD *, //0x8 <----
struct _EXCEPTION_REGISTRATION_RECORD *, //0xc
struct _CONTEXT *,//0x10
void *//0x14 (DispatcherContent)
)
0:000> dt _EXCEPTION_RECORD
winseh!_EXCEPTION_RECORD
+0x000 ExceptionCode : Uint4B //Exception Code 错误代码
+0x004 ExceptionFlags : Uint4B //EstablisherFlags
+0x008 ExceptionRecord : Ptr32 _EXCEPTION_RECORD //*ExceptionRecord
+0x00c ExceptionAddress : Ptr32 Void //ExceptionAddress 出错地址
+0x010 NumberParameters : Uint4B //# os Parameters
+0x014 ExceptionInformation : [15] Uint4B