首页
社区
课程
招聘
[讨论]怎么获得用户领空?请高手进来!
2010-5-3 14:40 4128

[讨论]怎么获得用户领空?请高手进来!

2010-5-3 14:40
4128
BOOL CShowCurrentProcessDlg::UpdateThreadContext(DWORD dwProcessID,DWORD dwThreadID)
{
    HANDLE                                        hThreadSnap = NULL;
    BOOL                                        bRet = FALSE;
        THREAD_INFORMATION_EX        tie;
        DWORD                                        dwThisThread = ::GetCurrentThreadId();        // used for not killing ourself
    char Tmp[20];
        int IntVar = 0;
        int ThreadID;
        CString mBody,mTmp;
    // Take a snapshot of all threads currently in the system.

    hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwProcessID);

    if(hThreadSnap == INVALID_HANDLE_VALUE)
        return FALSE;

    // Fill in the size of the structure before using it.
    tie.te32.dwSize = sizeof(THREADENTRY32);

    // Walk the thread snapshot to find all threads of the process.
    // If the thread belongs to the process, add its information
    // to the display list.

    if(Thread32First(hThreadSnap, &tie.te32))
    {
        do
        {
                        //
                        // if the thread belongs to the given process...
                        //
                         if (tie.te32.th32OwnerProcessID == dwProcessID)
            {
           
                                if(dwThreadID == tie.te32.th32ThreadID)
                                {
                                        //
                                        // get some more information about this thread
                                        //
                                        HANDLE hThread = ::OpenThread(THREAD_GET_CONTEXT|THREAD_QUERY_INFORMATION, FALSE, tie.te32.th32ThreadID);

                                        if(hThread != INVALID_HANDLE_VALUE)
                                        {
                                                ::SuspendThread(hThread);        // otherwise we dont get the context
                                                {
                                                        tie.ctx.ContextFlags = CONTEXT_FULL;

                                                        ::GetThreadContext(hThread, &tie.ctx);
                             
                                                        if( tie.ctx.Eip > 0x400000 && tie.ctx.Eip<0x500000)
                                                        {
                                                       
                                                                //CS DS SS ES
                                                                mTmp.Format("CS:0x%x    ", tie.ctx.SegCs);
                                                                  
                                                                mBody += mTmp;
                                                                //DS
                                                                mTmp.Format("DS:0x%x    ", tie.ctx.SegDs);

                                                                mBody += mTmp;
                                                                //SS
                                                                mTmp.Format("SS:0x%x    ", tie.ctx.SegSs);

                                                                mBody += mTmp;

                                                                //ES
                                                                mTmp.Format("ES:0x%x    ", tie.ctx.SegSs);

                                                                mBody += mTmp;

                                                                mBody += "\r\n";

                                                                //EIP
                                                                mTmp.Format("EIP:0x%-15x", tie.ctx.Eip);

                                                                mBody += mTmp;

                                                                        //ESP
                                                                mTmp.Format("ESP:0x%-15x", tie.ctx.Esp);

                                                                mBody += mTmp;

                                                                mBody += "\r\n";

                                                                  
                                                                //EAX
                                                                mTmp.Format("EAX:0x%-15x", tie.ctx.Eax);

                                                                mBody += mTmp;

                                                                        //ESI
                                                                mTmp.Format("ESI:0x%-15x", tie.ctx.Esi);

                                                                mBody += mTmp;

                                                                mBody += "\r\n";

                                                                        //EDI
                                                                mTmp.Format("EDI:0x%-15x", tie.ctx.Edi);

                                                                mBody += mTmp;

                                                                        //EBX
                                                                mTmp.Format("EBX:0x%-15x", tie.ctx.Ebx);

                                                                mBody += mTmp;

                                                                mBody += "\r\n";

                                                                        //EDX
                                                                mTmp.Format("EDX:0x%-15x",tie.ctx.Edx);

                                                                mBody += mTmp;

                                                                //ECX
                                                                mTmp.Format("ECX:0x%-15x", tie.ctx.Ecx);

                                                                mBody += mTmp;

                                                                mBody += "\r\n";

                                                                        //EBP
                                                                mTmp.Format("EBP:0x%-15x", tie.ctx.Ebp);

                                                                mBody += mTmp;

                                                                 SetDlgItemText(IDC_EDIT_REGISTER,mBody);
                                                  }

                                                }

                                                ::ResumeThread(hThread);

                                                ::CloseHandle(hThread);
                                        }
                               

                                ZeroMemory(&tie, sizeof(THREAD_INFORMATION_EX));

                                tie.te32.dwSize = sizeof(THREADENTRY32);
            }

                        }
               
        }while(Thread32Next(hThreadSnap, &tie.te32));

        bRet = TRUE;
    }
    else
        bRet = FALSE;          // could not walk the list of threads

    // Do not forget to clean up the snapshot object.

    CloseHandle(hThreadSnap);

    return bRet;
}

以上是我的函数,但是Eip 总是很大总是在ntdll之类的领空,不会到400000类的用户领空?请指教?

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

收藏
点赞0
打赏
分享
最新回复 (2)
雪    币: 75
活跃值: (423)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
skypismire 1 2010-5-3 16:36
2
0
SuspendThread最终调用的是内核函数KeSuspendThread
它通过stub函数NtSuspendThread进入内核,stub函数NtSuspendThread所在的领空是ntdll.dll,因此在进入内核之前eip的值是在ntdll.dll领空内.KeSuspendThread实际上是插入了一个apc函数(KiSuspendThread),在KeSuspendThread从内核态返回到用户态时,这个apc函数就得到执行.而这个apc函数所做的操作是  
KeWaitForSingleObject(&KeGetCurrentThread()->SuspendSemaphore,
                          Suspended,
                          KernelMode,
                          FALSE,
                          NULL);
因此被suspend的线程就在此处于等待状态.

GetThreadContext则是通过内核函数NtGetThreadContext获得线程进入内核时压入内核堆栈中的TrapFrame(保存进入内核时用户态的寄存器值).和上面的红色部分对应起来,就知道为什么获得的eip总是在ntdll.dll的领空了

应该是这样吧..说错的话后面的朋友帮忙指正
雪    币: 75
活跃值: (423)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
skypismire 1 2010-5-4 09:05
3
0
额,一部分我说错了,
游客
登录 | 注册 方可回帖
返回