SEH stack 结构探索(3)--- __exception_handler4() 探秘1

SEH stack 结构探索(3)--- __exception_handler4() 探秘1


在我的环境里,用户的缺省的 exception handler 是 MSVCR100D!_except_handler4(),它是 VC/C++ 的调试版本运行库里的函数,当然如果是发行版的话是MSVCR100!_except_handler4()

在 WinNT.h 里定义的 exception handler 原型是:

typedef
__drv_sameIRQL
__drv_functionClass(EXCEPTION_ROUTINE)
EXCEPTION_DISPOSITION
NTAPI
EXCEPTION_ROUTINE (
    __inout struct _EXCEPTION_RECORD *ExceptionRecord,
    __in PVOID EstablisherFrame,
    __inout struct _CONTEXT *ContextRecord,
    __in PVOID DispatcherContext
    );

每个 exception handler 有 4 个参数:

  • ExceptionRecord
  • EstablisherFrame
  • ContextRecord
  • DispathContext

ExceptionRecord 是指向 EXCEPTION_RECORD 结构的指针,ContextRecord 是指向 CONTEXT 结构的指针

在 WinDbg 里我们还可以发现:

0:000:x86> x MSVCR100D!_ex*handler4
01149550 MSVCR100D!_except_handler4 (struct _EXCEPTION_RECORD *, struct _EXCEPTION_REGISTRATION_RECORD *, struct _CONTEXT *, void *)

EstablisherFrame 是指向 EXCEPTION_REGISTRATION_RECORD 的指针,指向的这个 EXCEPTION_REGISTRATION_RECORD 并不是前面文章所说的 VC 扩展的 EXCEPTION_REGISTRATION_RECORD 结构,它的结构如下:

0:000:x86> dt _EXCEPTION_REGISTRATION_RECORD
MSVCR100D!_EXCEPTION_REGISTRATION_RECORD
   +0x000 Next             : Ptr32 _EXCEPTION_REGISTRATION_RECORD
   +0x004 Handler          : Ptr32     _EXCEPTION_DISPOSITION

这个结构仅使用了 Nextprev_structure)和 Handler 成员

 

1. _exception_handler4() 函数

下面是从 WinDbg 里得到的 _exception_handler4():

MSVCR100D!_except_handler4:
01149550 8bff            mov     edi,edi
01149552 55              push    ebp
01149553 8bec            mov     ebp,esp
01149555 8b4514          mov     eax,dword ptr [ebp+14h]
01149558 50              push    eax
01149559 8b4d10          mov     ecx,dword ptr [ebp+10h]
0114955c 51              push    ecx
0114955d 8b550c          mov     edx,dword ptr [ebp+0Ch]
01149560 52              push    edx
01149561 8b4508          mov     eax,dword ptr [ebp+8]
01149564 50              push    eax
01149565 6860591401      push    offset MSVCR100D!__security_check_cookie (01145960)
0114956a 68bc041901      push    offset MSVCR100D!__security_cookie (011904bc)
0114956f e8cc8e0000      call    MSVCR100D!_except_handler4_common (01152440)
01149574 83c418          add     esp,18h
01149577 5d              pop     ebp
01149578 c3              ret

这个函数所做的工作就是加了 cookie 直接将工作交给 _exception_handler4_common(),给 _exception_handler4_common() 有 6 个参数:

最后压入的两个是 cookie 指针值。

 

2. _exception_handler4_common() 函数

下面是 _exception_handler4_common() 的代码,这个函数有些长:

MSVCR100D!_except_handler4_common:
01152440 8bff            mov     edi,edi
01152442 55              push    ebp
01152443 8bec            mov     ebp,esp
01152445 83ec30          sub     esp,30h
01152448 c645df00        mov     byte ptr [ebp-21h],0
0115244c c745f401000000  mov     dword ptr [ebp-0Ch],1
01152453 8b4514          mov     eax,dword ptr [ebp+14h]
01152456 83e808          sub     eax,8
01152459 8945e0          mov     dword ptr [ebp-20h],eax
0115245c 8b4de0          mov     ecx,dword ptr [ebp-20h]
0115245f 83c118          add     ecx,18h
01152462 894de8          mov     dword ptr [ebp-18h],ecx
01152465 8b55e0          mov     edx,dword ptr [ebp-20h]
01152468 8b4508          mov     eax,dword ptr [ebp+8]
0115246b 8b4a10          mov     ecx,dword ptr [edx+10h]
0115246e 3308            xor     ecx,dword ptr [eax]
01152470 894dd4          mov     dword ptr [ebp-2Ch],ecx
01152473 8b55e8          mov     edx,dword ptr [ebp-18h]
01152476 52              push    edx
01152477 8b45d4          mov     eax,dword ptr [ebp-2Ch]
0115247a 50              push    eax
0115247b 8b4d0c          mov     ecx,dword ptr [ebp+0Ch]
0115247e 51              push    ecx
0115247f e88c010000      call    MSVCR100D!ValidateLocalCookies (01152610)
01152484 83c40c          add     esp,0Ch
01152487 8b5510          mov     edx,dword ptr [ebp+10h]
0115248a 8b4204          mov     eax,dword ptr [edx+4]
0115248d 83e066          and     eax,66h
01152490 0f8523010000    jne     MSVCR100D!_except_handler4_common+0x179 (011525b9)

MSVCR100D!_except_handler4_common+0x56:
01152496 8b4d10          mov     ecx,dword ptr [ebp+10h]
01152499 894df8          mov     dword ptr [ebp-8],ecx
0115249c 8b5518          mov     edx,dword ptr [ebp+18h]
0115249f 8955fc          mov     dword ptr [ebp-4],edx
011524a2 8b45e0          mov     eax,dword ptr [ebp-20h]
011524a5 8d4df8          lea     ecx,[ebp-8]
011524a8 894804          mov     dword ptr [eax+4],ecx
011524ab 8b55e0          mov     edx,dword ptr [ebp-20h]
011524ae 8b4214          mov     eax,dword ptr [edx+14h]
011524b1 8945d8          mov     dword ptr [ebp-28h],eax
011524b4 eb06            jmp     MSVCR100D!_except_handler4_common+0x7c (011524bc)

MSVCR100D!_except_handler4_common+0x76:
011524b6 8b4dec          mov     ecx,dword ptr [ebp-14h]
011524b9 894dd8          mov     dword ptr [ebp-28h],ecx

MSVCR100D!_except_handler4_common+0x7c:
011524bc 837dd8fe        cmp     dword ptr [ebp-28h],0FFFFFFFEh
011524c0 0f84f1000000    je      MSVCR100D!_except_handler4_common+0x177 (011525b7)

MSVCR100D!_except_handler4_common+0x86:
011524c6 8b55d8          mov     edx,dword ptr [ebp-28h]
011524c9 6bd20c          imul    edx,edx,0Ch
011524cc 8b45d4          mov     eax,dword ptr [ebp-2Ch]
011524cf 8d4c1010        lea     ecx,[eax+edx+10h]
011524d3 894de4          mov     dword ptr [ebp-1Ch],ecx
011524d6 8b55e4          mov     edx,dword ptr [ebp-1Ch]
011524d9 8b4204          mov     eax,dword ptr [edx+4]
011524dc 8945d0          mov     dword ptr [ebp-30h],eax
011524df 8b4de4          mov     ecx,dword ptr [ebp-1Ch]
011524e2 8b11            mov     edx,dword ptr [ecx]
011524e4 8955ec          mov     dword ptr [ebp-14h],edx
011524e7 837dd000        cmp     dword ptr [ebp-30h],0
011524eb 0f84c1000000    je      MSVCR100D!_except_handler4_common+0x172 (011525b2)

MSVCR100D!_except_handler4_common+0xb1:
011524f1 8b55e8          mov     edx,dword ptr [ebp-18h]
011524f4 8b4dd0          mov     ecx,dword ptr [ebp-30h]
011524f7 e806b4ffff      call    MSVCR100D!_EH4_CallFilterFunc (0114d902)
011524fc 8945f0          mov     dword ptr [ebp-10h],eax
011524ff c645df01        mov     byte ptr [ebp-21h],1
01152503 837df000        cmp     dword ptr [ebp-10h],0
01152507 7d11            jge     MSVCR100D!_except_handler4_common+0xda (0115251a)

MSVCR100D!_except_handler4_common+0xc9:
01152509 c745f400000000  mov     dword ptr [ebp-0Ch],0
01152510 e9a2000000      jmp     MSVCR100D!_except_handler4_common+0x177 (011525b7)

MSVCR100D!_except_handler4_common+0xda:
0115251a 837df000        cmp     dword ptr [ebp-10h],0
0115251e 0f8e8e000000    jle     MSVCR100D!_except_handler4_common+0x172 (011525b2)

MSVCR100D!_except_handler4_common+0xe4:
01152524 8b4510          mov     eax,dword ptr [ebp+10h]
01152527 813863736de0    cmp     dword ptr [eax],0E06D7363h
0115252d 7529            jne     MSVCR100D!_except_handler4_common+0x118 (01152558)

MSVCR100D!_except_handler4_common+0xef:
0115252f 833d308e040100  cmp     dword ptr [MSVCR100D!_pDestructExceptionObject (01048e30)],0
01152536 7420            je      MSVCR100D!_except_handler4_common+0x118 (01152558)

MSVCR100D!_except_handler4_common+0xf8:
01152538 68308e0401      push    offset MSVCR100D!_pDestructExceptionObject (01048e30)
0115253d e83ea90000      call    MSVCR100D!_IsNonwritableInCurrentImage (0115ce80)
01152542 83c404          add     esp,4
01152545 85c0            test    eax,eax
01152547 740f            je      MSVCR100D!_except_handler4_common+0x118 (01152558)

MSVCR100D!_except_handler4_common+0x109:
01152549 6a01            push    1
0115254b 8b4d10          mov     ecx,dword ptr [ebp+10h]
0115254e 51              push    ecx
0115254f ff15308e0401    call    dword ptr [MSVCR100D!_pDestructExceptionObject (01048e30)]
01152555 83c408          add     esp,8

MSVCR100D!_except_handler4_common+0x118:
01152558 8b4de0          mov     ecx,dword ptr [ebp-20h]
0115255b 83c108          add     ecx,8
0115255e 8b5510          mov     edx,dword ptr [ebp+10h]
01152561 e8ccb3ffff      call    MSVCR100D!_EH4_GlobalUnwind2 (0114d932)
01152566 8b55e0          mov     edx,dword ptr [ebp-20h]
01152569 8b4214          mov     eax,dword ptr [edx+14h]
0115256c 3b45d8          cmp     eax,dword ptr [ebp-28h]
0115256f 7416            je      MSVCR100D!_except_handler4_common+0x147 (01152587)

MSVCR100D!_except_handler4_common+0x131:
01152571 8b4d08          mov     ecx,dword ptr [ebp+8]
01152574 51              push    ecx
01152575 8b55e8          mov     edx,dword ptr [ebp-18h]
01152578 52              push    edx
01152579 8b4de0          mov     ecx,dword ptr [ebp-20h]
0115257c 83c108          add     ecx,8
0115257f 8b55d8          mov     edx,dword ptr [ebp-28h]
01152582 e8c4b3ffff      call    MSVCR100D!_EH4_LocalUnwind (0114d94b)

MSVCR100D!_except_handler4_common+0x147:
01152587 8b45e0          mov     eax,dword ptr [ebp-20h]
0115258a 8b4dec          mov     ecx,dword ptr [ebp-14h]
0115258d 894814          mov     dword ptr [eax+14h],ecx
01152590 8b55e8          mov     edx,dword ptr [ebp-18h]
01152593 52              push    edx
01152594 8b45d4          mov     eax,dword ptr [ebp-2Ch]
01152597 50              push    eax
01152598 8b4d0c          mov     ecx,dword ptr [ebp+0Ch]
0115259b 51              push    ecx
0115259c e86f000000      call    MSVCR100D!ValidateLocalCookies (01152610)
011525a1 83c40c          add     esp,0Ch
011525a4 8b55e8          mov     edx,dword ptr [ebp-18h]
011525a7 8b45e4          mov     eax,dword ptr [ebp-1Ch]
011525aa 8b4808          mov     ecx,dword ptr [eax+8]
011525ad e867b3ffff      call    MSVCR100D!_EH4_TransferToHandler (0114d919)

MSVCR100D!_except_handler4_common+0x172:
011525b2 e9fffeffff      jmp     MSVCR100D!_except_handler4_common+0x76 (011524b6)

MSVCR100D!_except_handler4_common+0x177:
011525b7 eb25            jmp     MSVCR100D!_except_handler4_common+0x19e (011525de)

MSVCR100D!_except_handler4_common+0x179:
011525b9 8b4de0          mov     ecx,dword ptr [ebp-20h]
011525bc 837914fe        cmp     dword ptr [ecx+14h],0FFFFFFFEh
011525c0 741c            je      MSVCR100D!_except_handler4_common+0x19e (011525de)

MSVCR100D!_except_handler4_common+0x182:
011525c2 8b5508          mov     edx,dword ptr [ebp+8]
011525c5 52              push    edx
011525c6 8b45e8          mov     eax,dword ptr [ebp-18h]
011525c9 50              push    eax
011525ca 8b4de0          mov     ecx,dword ptr [ebp-20h]
011525cd 83c108          add     ecx,8
011525d0 bafeffffff      mov     edx,0FFFFFFFEh
011525d5 e871b3ffff      call    MSVCR100D!_EH4_LocalUnwind (0114d94b)
011525da c645df01        mov     byte ptr [ebp-21h],1

MSVCR100D!_except_handler4_common+0x19e:
011525de 0fb64ddf        movzx   ecx,byte ptr [ebp-21h]
011525e2 85c9            test    ecx,ecx
011525e4 7414            je      MSVCR100D!_except_handler4_common+0x1ba (011525fa)

MSVCR100D!_except_handler4_common+0x1a6:
011525e6 8b55e8          mov     edx,dword ptr [ebp-18h]
011525e9 52              push    edx
011525ea 8b45d4          mov     eax,dword ptr [ebp-2Ch]
011525ed 50              push    eax
011525ee 8b4d0c          mov     ecx,dword ptr [ebp+0Ch]
011525f1 51              push    ecx
011525f2 e819000000      call    MSVCR100D!ValidateLocalCookies (01152610)
011525f7 83c40c          add     esp,0Ch

MSVCR100D!_except_handler4_common+0x1ba:
011525fa 8b45f4          mov     eax,dword ptr [ebp-0Ch]
011525fd 8be5            mov     esp,ebp
011525ff 5d              pop     ebp
01152600 c3              ret

在继续分析之前,我们再来看一看上一篇文章所得到的 SEH stack 结构图:

3. 得到上一个 stack frame 值

来看看下面几行:

01152453 8b4514          mov     eax,dword ptr [ebp+14h]          ; EstablisherFrame (pre_structure)
01152456 83e808          sub     eax,8                            ; get esp
01152459 8945e0          mov     dword ptr [ebp-20h],eax          ; save esp 
0115245c 8b4de0          mov     ecx,dword ptr [ebp-20h]
0115245f 83c118          add     ecx,18h                          ; get ebp(SEH ebp)
01152462 894de8          mov     dword ptr [ebp-18h],ecx          ; save ebp

代码中 [ebp+14h] 是给 _exception_handler4_common() 的参数 EstabllisherFrame 值,它是指向上图中的 prev_struct(也就是 Next)位置,它是当前的 FS:[0] 值,这个值指向下一个 EXCEPTION_REGISTERATION_RECORD 结构。

prev_struct 位置减 8 就得到保存 esp 值的位置,然后再 esp 位置加上 18h 这将得到保存上一个 ebp 值的位置,将得到的 esp 和 ebp 都保存在 stack 中

这里将 [ebp-20h] 作为变量 RegistrationNode,[ebp-18h] 作为变量 FramePointer, 于是就有变量 RegistrationNode 用来保存 SEH stack 结构中保存 esp 位置,变量 FramePointer 用来保存 SEH stack 结构中保存 ebp 的位置。

下一篇文章我继续来分析 _exception_handler_common(),下篇将会揭示 SEH stack 结构中的 scopetable 结构。

 

上一页  目录  下一页


版权 mik 所有,转载请注明出处!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值