首页
社区
课程
招聘
[求助]可以强制让其他线程抛出异常吗?
发表于: 2010-10-15 14:37 3570

[求助]可以强制让其他线程抛出异常吗?

2010-10-15 14:37
3570
已知欲出异常的线程的HANDLE,如果该线程不在ring0里面,可以先SuspendThread这个线程,然后GetThreadContext改变Eip到抛异常的代码入口,再SetThreadContext回去,这样就可以让它抛异常了。但是如果当时该线程在ring0里面(比如在Sleep),这样就行不通了,他会在出了ring0以后才到指定的Eip执行代码。
那么有没有方法强制它直接抛出异常的?

奉上测试代码:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
 
unsigned __stdcall ThreadFunc(void* lpParam)
{
    try
    {
#if 0
        Sleep(3000);
#else
        DWORD dwTick = GetTickCount();
        while (GetTickCount() - dwTick < 3000)
            Sleep(1);
#endif
    }
    catch(...)
    {
        printf("catch!\r\n");
    }
    return 0;
}
 
 
int main(int, char**)
{
    unsigned tid = 0;
    HANDLE hl = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, NULL, 0, &tid);
    if (hl != NULL)
    {
        system("PAUSE");
        if (SuspendThread(hl) != (DWORD)-1)
        {
            CONTEXT ct = {0};
            ct.ContextFlags = CONTEXT_CONTROL;
            if (GetThreadContext(hl, &ct))
            {
                __asm
                {
                    mov eax, label0
                    mov ct.Eip, eax
                    jmp label1
                label0:
                    int 1
                label1:
                }
                SetThreadContext(hl, &ct);
            }
            ResumeThread(hl);
        }
        WaitForSingleObject(hl, INFINITE);
        CloseHandle(hl);
    }
    system("PAUSE");
    return 0;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册