跟踪 Ring3 - Ring0 的执行流程

理论知识

SYSENTER 指令是在 Inter Pentium(R) Ⅱ 处理器上作为“快速系统调用”功能的一部分被首次引用的。
SYSENTER 指令进行过专门的优化,能够以最佳性能由 Ring3 层切换到 Ring0 层。
微软首次引用 SYSENTER 指令是在 Windows 2000 的系统上,再次之前微软的系统是通过自陷指令 int 0x2E 进入 Ring0 层的系统空间的。
在 Windows 2000 及以后的系统中,如果想要从 Ring3 层进入 Ring0 层,系统首先会将要调用的系统调用号(SSDT调用号)放入 EAX 中,然后将当前栈指针 ESP 的内容放入 EDX 中后执行SYSENTER 指令;
SYSENTER 被执行后会将控制权传递给特殊模块寄存器 IA32_SYSENTER_EIP 所指向的函数中,完成后续操作。

特殊模块寄存器组(Model-Specific registers,MSRs)是 CPU 中负责执行一些特定的、与计算逻辑相关性不大的、且操作较复杂的功能的一组寄存器。
特殊模块寄存器可通过使用 rdmsr/wrmsr 指令读写其位于 MSRs 指定偏移处的某个寄存器的信息。
MSRs 共包含上百种具有不同功能的寄存器。每个寄存器所在的偏移与所占用的空间都不尽相同,目前 MSRs 的可用偏移范围是 0x00000000 ~ 0xC0000103。

与 SYSENTER 相配合的 MSRs 共有3个,其详细信息如下所示:

名称 偏移 说明
SYSENTER_CS_MSR 0x174 切换到 Ring0 层之后的 CS 选择器
SYSENTER_ESP_MSR 0x175 切换到 Ring0 层之后的 ESP
SYSENTER_EIP_MSR 0x176 切换到 Ring0 层之后的 EIP

SYSENTER 被执行时,这 3 个寄存器会执行以下操作配合.
SYSENTER 完成的切换操作:

  1. 装载 SYSENTER_CS_MSR 到 CS 寄存器,设置目标代码段;
  2. 装载 SYSENTER_EIP_MSR 到 EIP 寄存器,设置目标指令;
  3. 将 SYSENTER_CS_MSR + 8 装载到 SS 寄存器,设置栈段;
  4. 装载 SYSENTER_ESP_MSR 到 ESP 寄存器,设置栈帧;
  5. 切换 Ring0;
  6. 清除 EFLAGS 的 VM 标志;(虚拟8086方式标志)
  7. 执行位于 EIP 处的 RING0 例程。

实验开始~

说了这么多理论,现在来实践一下咯:( 以 CreateFileW 函数为例 )
在虚拟机中运行 notepad.exe ,用 windbg 使断下
搜索 notepad.exe 进程:

2: kd> !process 0 0 notepad.exe
PROCESS 87b697c0  SessionId: 1  Cid: 0d10    Peb: 7ffdf000  ParentCid: 0474
    DirBase: 6612b000  ObjectTable: 931868f0  HandleCount:  61.
    Image: notepad.exe

我们得到了 notepad.exe 的进程首地址,现在转到他的进程空间:

2: kd> .process /i /p 87b697c0
You need to continue execution (press 'g' <enter>) for the context
to be switched. When the debugger breaks in again, you will be in
the new process context.

其中两个参数的含义:(注:此中文解释来自 http://www.dbgtech.net/windbghelp/,站长翻译了windbg的帮助文档,英语文盲的福音)

/i
(Windows XP 和之后的系统;仅活动调试;非本地内核调试) 指定要侵入(invasively)调试Process 。这种调试意味着目标机上的操作系统实际上将指定的进程激活。 (如果不带该参数,.process 命令改变调试器的输出,但是不会作用于目标机本身。)如果使用了 /i,必须使用g (Go)命令来执行目标。数秒之后,目标会再次中断到调试器中,并且指定的 Process 被激活并用作当前进程上下文。
/p
如果使用了/p 并且Process 非0,在访问之前将该进程所有页表入口(PTE)转换成物理地址。这种转换可能造成速度变慢,因为调试器必须找到该进程使用的所有内存的物理地址。调试器也需要通过调试电缆传输大量数据。 (该行为和.cache forcedecodeuser一样。)
如果包含/p 并且Process为0或者省略,则禁用这样的转换。 (这种行为和.cache noforcedecodeptes一样。)

上面提示我们说要运行一下才能切换上下文,那么 g 一下

2: kd> g
Break instruction exception - code 80000003 (first chance)
nt!RtlpBreakWithStatusInstruction:
8406dd00 cc              int     3

现在我们就断在了 notepad.exe 的进程里
现在重新加载一下符号表

2: kd> .reload /f /user
Loading User Symbols
.
DBGHELP: notepad - public symbols  
        f:\program files\nt_dbg_symbols\notepad.pdb\E325F5195AE94FAEB58D25C9DF8C0CFD2\notepad.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\ntdll.dll\4CE7B96E13c000\ntdll.dll - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\ntdll.dll\4CE7B96E13c000\ntdll.dll.
DBGHELP: ntdll - public symbols  
        f:\program files\nt_dbg_symbols\ntdll.pdb\120028FA453F4CD5A6A404EC37396A582\ntdll.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\kernel32.dll\4CE7B8EFd4000\kernel32.dll - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\kernel32.dll\4CE7B8EFd4000\kernel32.dll.
DBGHELP: kernel32 - public symbols  
        f:\program files\nt_dbg_symbols\kernel32.pdb\D4AC3906D49D487B9F69F9326A7D206A2\kernel32.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\KERNELBASE.dll\4CE7B8F04a000\KERNELBASE.dll - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\KERNELBASE.dll\4CE7B8F04a000\KERNELBASE.dll.
DBGHELP: KERNELBASE - public symbols  
        f:\program files\nt_dbg_symbols\kernelbase.pdb\F50B25D282504188B73063D653B7D27E2\kernelbase.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\ADVAPI32.dll\4CE7B706a0000\ADVAPI32.dll - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\ADVAPI32.dll\4CE7B706a0000\ADVAPI32.dll.
DBGHELP: ADVAPI32 - public symbols  
        f:\program files\nt_dbg_symbols\advapi32.pdb\3F32049F550C42B09CF114A1FB8A97E92\advapi32.pdb
.
DBGHELP: msvcrt - public symbols  
        f:\program files\nt_dbg_symbols\msvcrt.pdb\6EC79267530C45188F2A816AD59DBBF92\msvcrt.pdb
.
DBGHELP: sechost - public symbols  
        f:\program files\nt_dbg_symbols\sechost.pdb\7AF14D02D41E4CD6942745FE0E6372B11\sechost.pdb
.
DBGHELP: RPCRT4 - public symbols  
        f:\program files\nt_dbg_symbols\rpcrt4.pdb\189CC56E2D4A43DA8A269E088721F13D2\rpcrt4.pdb
.
DBGHELP: GDI32 - public symbols  
        f:\program files\nt_dbg_symbols\gdi32.pdb\96948E513A8747C58A699B8A7009DCB22\gdi32.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\USER32.dll\4CE7BA26c9000\USER32.dll - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\USER32.dll\4CE7BA26c9000\USER32.dll.
DBGHELP: USER32 - public symbols  
        f:\program files\nt_dbg_symbols\user32.pdb\DD74D86F12624845A42A6A5BAAB4D7A82\user32.pdb
.
DBGHELP: LPK - public symbols  
        f:\program files\nt_dbg_symbols\lpk.pdb\B99319FE4427418F9EB5432B9F6A13412\lpk.pdb
.
DBGHELP: USP10 - public symbols  
        f:\program files\nt_dbg_symbols\usp10.pdb\F49786E2C7C54EA99E7C37120CDAEB9C1\usp10.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\COMDLG32.dll\4CE7B82D7b000\COMDLG32.dll - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\COMDLG32.dll\4CE7B82D7b000\COMDLG32.dll.
DBGHELP: COMDLG32 - public symbols  
        f:\program files\nt_dbg_symbols\comdlg32.pdb\96BC483CDFF04D1AAFE462F093B954EC2\comdlg32.pdb
.
DBGHELP: SHLWAPI - public symbols  
        f:\program files\nt_dbg_symbols\shlwapi.pdb\E128B1CEE2EB438C8646E6967118F33E2\shlwapi.pdb
.
DBGHELP: COMCTL32 - public symbols  
        f:\program files\nt_dbg_symbols\comctl32.pdb\B4CE90AAB95E4B89A22A7711DFD7E6EF2\comctl32.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\SHELL32.dll\4CE7B9DEc4a000\SHELL32.dll - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\SHELL32.dll\4CE7B9DEc4a000\SHELL32.dll.
DBGHELP: SHELL32 - public symbols  
        f:\program files\nt_dbg_symbols\shell32.pdb\4555A5FB02FA4E49B65A25616CD97A6B2\shell32.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\WINSPOOL.DRV\4CE7BA4B51000\WINSPOOL.DRV - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\WINSPOOL.DRV\4CE7BA4B51000\WINSPOOL.DRV.
DBGHELP: WINSPOOL - public symbols  
        f:\program files\nt_dbg_symbols\winspool.pdb\B165BBE7CD8C4F39BE373C0D9DFCD77B2\winspool.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\ole32.dll\4CE7B96F15c000\ole32.dll - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\ole32.dll\4CE7B96F15c000\ole32.dll.
DBGHELP: ole32 - public symbols  
        f:\program files\nt_dbg_symbols\ole32.pdb\5061F11A9A57433595EA5EA75A156F4B2\ole32.pdb
.
DBGHELP: f:\program files\nt_dbg_symbols\OLEAUT32.dll\4CE7B9728f000\OLEAUT32.dll - OK
DBGENG:  Partial symbol load found image f:\program files\nt_dbg_symbols\OLEAUT32.dll\4CE7B9728f000\OLEAUT32.dll.
DBGHELP: OLEAUT32 - public symbols  
        f:\program files\nt_dbg_symbols\oleaut32.pdb\312BF231E76A450ABF027EBFB52FA2162\oleaut32.pdb
.
DBGHELP: VERSION - public symbols  
        f:\program files\nt_dbg_symbols\version.pdb\52234E5C7EC44646B62D56357B2C94872\version.pdb
.
DBGHELP: IMM32 - public symbols  
        f:\program files\nt_dbg_symbols\imm32.pdb\91A0004474E24AA89F185029E31144892\imm32.pdb
.
DBGHELP: MSCTF - public symbols  
        f:\program files\nt_dbg_symbols\msctf.pdb\173DAEF86B2548DBA6134EB74C4D2F232\msctf.pdb
.
DBGHELP: CRYPTBASE - public symbols  
        f:\program files\nt_dbg_symbols\cryptbase.pdb\E62FEAE559EE4CD995614215B01AC2102\cryptbase.pdb
.
DBGHELP: uxtheme - public symbols  
        f:\program files\nt_dbg_symbols\UxTheme.pdb\5BECAB35E7714835A6BF3DADD891BB3A2\UxTheme.pdb
.
DBGHELP: dwmapi - public symbols  
        f:\program files\nt_dbg_symbols\dwmapi.pdb\D8D91B3F339A4FDC960FC7121D146DF42\dwmapi.pdb

其中参数含义:

/f(立即加载)
Forces the debugger to immediately load the symbols. This parameter overrides lazy symbol loading. For more information, see the following
Remarks section.
/user (只加载用户符号)
Reloads user symbols only. (You can use this option only during kernel-mode debugging.)

我之前下载了一个完整版的,所有基本都是本地加载。
如果没有并且网速又慢。。。可以等到 kernel 等 pdb 下载过后按Ctrl + C终止,不然就要等到天荒地老。。。。。

现在保证了符号已经没问题,就可以开心的继续咯。直到现在我们才真正开始…….
因为我们要观察 CreateFileW ,我们就在这个函数下断点

2: kd> bp kernel32!CreateFileW

为了放心,看一下断点列表:

2: kd> bl
 0 e 759ecca9     0001 (0001) kernel32!CreateFileW

我们现在继续运行:

2: kd> g
Breakpoint 0 hit
kernel32!CreateFileW:
001b:759ecca9 ff25e0199a75    jmp     dword ptr [kernel32!_imp__CreateFileW (759a19e0)]

进程断在了这里。单步一次进入jmp的函数内部:

3: kd> t
KERNELBASE!CreateFileW:
001b:7574a850 8bff            mov     edi,edi

以函数地址开始反汇编100条:


                
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值