SafeSEH 对异常处理的保护原理
在 Windows XP sp2 以及之后的版本中,微软引入了 S.E.H 校验机制 SafeSEH。SafeSEH 需要 OS 和 Compiler 的双重支持,二者缺一都会降低保护能力。通过启用 /SafeSEH 链接选项可心使编译好的程序具备 SafeSEH 功能(VS2003 及后续版本默认启用)。该选项会将所有异常处理函数地址提取出来,编入 SEH 表中,并将这张表放到程序的映像里。异常调用时,就与这张预先存好的表中的地址进行校验。
VS 的 Visual Studio 200* Command Prompt 中,使用 dumpbin /loadconfig *.exe 命令可以查看 SEH 表:
1 Microsoft (R) COFF/PE Dumper Version 9.00.30729.01 2 Copyright (C) Microsoft Corporation. All rights reserved. 3 4 Dump of file gs.exe 5 6 File Type: EXECUTABLE IMAGE 7 8 Section contains the following load config: 9 10 00000048 size 11 0 time date stamp 12 0.00 Version 13 0 GlobalFlags Clear 14 0 GlobalFlags Set 15 0 Critical Section Default Timeout 16 0 Decommit Free Block Threshold 17 0 Decommit Total Free Threshold 18 00000000 Lock Prefix Table 19 0 Maximum Allocation Size 20 0 Virtual Memory Threshold 21 0 Process Heap Flags 22 0 Process Affinity Mask 23 0 CSD Version 24 0000 Reserved 25 00000000 Edit list 26 00403000 Security Cookie 27 004021C0 Safe Exception Handler Table 28 1 Safe Exception Handler Count 29 30 Safe Exception Handler Table 31 32 Address 33 -------- 34 004017F5 __except_handler4 35 36 Summary 37 38 1000 .data 39 1000 .rdata 40 1000 .reloc 41 1000 .rsrc 42 1000 .text