首页
社区
课程
招聘
SetUnhandledExceptionFilter以后try-catch为什么还会有效?
发表于: 2010-3-13 14:55 7100

SetUnhandledExceptionFilter以后try-catch为什么还会有效?

2010-3-13 14:55
7100
SetUnhandledExceptionFilter以后try-catch为什么还会有效?
我用VC写了一段测试代码,连续两次调用SetUnhandledExceptionFilter,参数分别是ExceptionFilter和ExceptionFilter2,然后写一个__asm int 3,非调试状态运行,回调的是最后调用的ExceptionFilter2。
在SetUnhandledExceptionFilter之后,我写了一段try-catch,里面写*(int*)NULL=0,可是catch还是被触发了。应该是我调用SetUnhandledExceptionFilter覆盖了默认的C++异常回调函数了,为什么try-catch还会有效?try里面又没有重新SetUnhandledExceptionFilter。

[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (7)
雪    币: 359
活跃值: (41)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
2
现在还遇到一个更奇怪的问题,我发现有时候发生异常时刻的线程各寄存器CONTEXT与ExceptionFilter里面的参数内容竟然也会不同,这又可能是什么原因造成的呢?
-----------------------
这个我是在调试别人的程序时发现的,我用CreateRemoteThread将DLL注入被调试进程,在DllMain的DLL_PROCESS_ATTACH里面SetUnhandledExceptionFilter,某些时候出现异常后,会发现两者内容不一致,只有Eip是相同的。会是他再次调用了SetUnhandledExceptionFilter覆盖了我设定的回调的缘故吗?我发现即便是在异常前重新设置一次我设定的回调,结果还是这样。
2010-3-13 18:02
0
雪    币: 359
活跃值: (41)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
3
这个似乎是那段代码有try,所以先跑到try里面处理了,结果修改了参数内容。那么如果想让程序出异常后总是先调用自己设定的ExceptionFilter里面是否是不可能完成的任务?
2010-3-14 00:43
0
雪    币: 65
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
好好看一看UnhandledExceptionHandler 这个函数的说明:
当发生异常时,比如内存访问违例时,CPU硬件会发现此问题,并产生一个异常(你可以把它理解为中断)
然后CPU会把代码流程切换到异常处理服务例程。操作系统异常处理服务例程会查看当前进程是否处于调试状态
如果时,则通知调试器发生了异常,如果不是则操作系统会查看当前线程是否安装了的异常帧链(FS[0]),如果安装了SEH(try.... catch....),则调用SEH,并根据返回结果决定是否全局展开活局部展开。如果异常链中所有的SEH都没有处理此异常,而且此进程还处于调试状态,则操作系统会再次通知调试器发生异常(二次异常)。如果还没人处理,则调用操作系统的默认异常处理代码UnhandledExceptionHandler,不过操作系统允许你Hook这个函数,就是通过SetUnhandledExceptionFilter函数来设置。大部分异常通过此种方法都能捕获,不过栈溢出、覆盖的有可能捕获不到。
2010-3-14 10:11
0
雪    币: 359
活跃值: (41)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
5
明白了,这么说try安装的SEH优先级永远是最高的,而想让程序出异常后总是先调用自己设定的ExceptionFilter则是不可能完成的任务,是这样吗?
2010-3-14 11:25
0
雪    币: 2105
活跃值: (424)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
6
Unhandled 肯定是没有处理过的 都被try处理了。。。
2010-3-14 14:42
0
雪    币: 202
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
try内当然无效,这只是没有try的情况下才走的
2010-3-14 15:05
0
雪    币: 11
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
第一个问题,看函数名字,它对应的是没有handled,已经catch的当然轮不到
第二个问题,看MSDN说明,是会由后设置的顶替的
Enables an application to supersede the top-level exception handler of each thread of a process.

After calling this function, if an exception occurs in a process that is not being debugged, and the exception makes it to the unhandled exception
filter, that filter will call the exception filter function specified by the lpTopLevelExceptionFilter parameter.

最后,不同的context当然参数也不同
Remarks

Issuing SetUnhandledExceptionFilter replaces the existing top-level exception filter for all existing and all future threads in the calling process.

The exception handler specified by lpTopLevelExceptionFilter is executed in the context of the thread that caused the fault. This can affect the
exception handler's ability to recover from certain exceptions, such as an invalid stack.
2010-3-14 20:01
0
游客
登录 | 注册 方可回帖
返回
//