Chinaunix首页 | 论坛 | 博客
  • 博客访问: 333593
  • 博文数量: 107
  • 博客积分: 2825
  • 博客等级: 少校
  • 技术积分: 795
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-19 12:19
文章分类

全部博文(107)

文章存档

2013年(2)

2012年(31)

2011年(18)

2010年(12)

2009年(44)

我的朋友

分类: LINUX

2009-11-27 10:41:17

在XP系统中, 系统服务在内核的入口是KiFastCallEntry,我们从该入口开始,分析这一段代码都做了什么工作。
 
由于水平有限和背景知识不够,本人没有完全读懂它们,作为抛砖引玉,我先将我所看懂的和大家分享,希望大家多多指教! 
 
 
 

KiFastCallEntry的主要功能有:
  • 初始化系统堆栈
  • 将上下文压入系统堆栈,压栈顺序见KTrap_Frame内核堆栈框架
  • 将参数从用户模式堆栈复制到内核堆栈,以上步骤在关中断下进行的,应为要用到KUser_Shared_Data
  • 确定服务例程的入口地址,包括使用那个服务表,是Shadow SSDT还是SSDT
  • 转入服务例程入口
  • 等等,欢迎补充
背景知识:
  • 进入内核后,fs:[0]或者说ds:[ffdff000] 处是一个叫做KPCR 的结构,PCR 即 Processor Control Region ,处理器控制域。这是一个很有用的结构。系统本身就大量使用。
  • 多谢qihoocom和bozer的补充在ds:[ffdf0000]处是一个nt!_KUser_Shared_Data结构
    在Sysenter调用前,将上下文保存到这里,注意:该结构是共享的,要关闭中断才能使用
  • eax中是服务例程ID 
  • edx指向用户堆栈,参数可以通过其得到
  • 在 Ring3 的代码调用了 sysenter 指令之后,CPU 会做出如下的操作:
    1. 将 SYSENTER_CS_MSR 的值装载到 cs 寄存器 
    2. 将 SYSENTER_EIP_MSR 的值装载到 eip 寄存器 
    3. 将 SYSENTER_CS_MSR 的值加 8(Ring0 的堆栈段描述符)装载到 ss 寄存器。
    4. 将 SYSENTER_ESP_MSR 的值装载到 esp 寄存器 
    5. 将特权级切换到 Ring0 
    6. 如果 EFLAGS 寄存器的 VM 标志被置位,则清除该标志
    7. 开始执行指定的 Ring0 代码 
    在 Ring0 代码执行完毕,调用 SY***IT 指令退回 Ring3 时,CPU 会做出如下操作:
    1. 将 SYSENTER_CS_MSR 的值加 16(Ring3 的代码段描述符)装载到 cs 寄存器 
    2. 将寄存器 edx 的值装载到 eip 寄存器 
    3. 将 SYSENTER_CS_MSR 的值加 24(Ring3 的堆栈段描述符)装载到 ss 寄存器 
    4. 将寄存器 ecx 的值装载到 esp 寄存器 
    5. 将特权级切换到 Ring3 
    6. 继续执行 Ring3 的代码 
代码:
nt!KiFastCallEntry:   // 当线程在内核模式下执行时的数据段选择子是23h // 当线程在内核模式下执行时,在FS寄存器中加载的是选择子30, // 用于寻址PCR结构体 804de6f0      mov ecx,23h 804de6f5      push 30h 804de6f7      pop fs   // 23h->ds;23h->es 804de6f9      mov ds,cx 804de6fb      mov es,cx   // dword ptr fs:[40h] == dword ptr ds:[0FFDFF040h]{TSS} // dword ptr [ecx+4] 为当前线程的内核模式栈顶指针的值 804de6fd      mov ecx,dword ptr ds:[0FFDFF040h] 804de703      mov esp,dword ptr [ecx+4]   // ========================================== // 将当前(执行Sysenter前各寄存器的值)上下文压入系统堆栈,   // _KTRAP_FRAME.HardwareSegSs 804de706      push 23h   // _KTRAP_FRAME.HardwareEsp 804de708      push edx   // _KTRAP_FRAME.EFags 804de709      pushfd 804de70a      push 2 804de70c      add edx,8   // EFlags = 2 804de70f      popfd 804de710      or byte ptr [esp+1],2   // _KTRAP_FRAME.SegCs 804de715      push 1Bh   // ds:[0FFDF0304h] 是SystemCallReturn(_KUser_Shared_Data) // _KTRAP_FRAME.Eip 804de717      push dword ptr ds:[0FFDF0304h]   //_KTRAP_FRAME.ErrorCode 804de71d      push 0      //_KTRAP_FRAME.Ebp                                        804de71f      push ebp        //_KTRAP_FRAME.Ebx 804de720      push ebx   //_KTRAP_FRAME.Esi 804de721      push esi                                           //_KTRAP_FRAME.Edi 804de722      push edi      //ebx<-_KPCR.SelfPcr 804de723      mov ebx,dword ptr ds:[0FFDFF01Ch]     // _KTRAP_FRAME.SegFs 804de729      push 3Bh   //esi=_KPCR.PrcbData.CurrentThread 804de72b      mov esi,dword ptr [ebx+124h]      //_KTRAP_FRAME.ExceptionList 804de731      push dword ptr [ebx] // 初始化链表 804de733      mov dword ptr [ebx],0FFFFFFFFh   //ebp = KPCR.PrcbData.CurrentThread. InitialStack 804de739      mov ebp,dword ptr [esi+18h]   //_KTRAP_FRAME.PreviousPreviousMode = 1 804de73c      push 1   // esp->_KTRAP_FRAME 804de73e      sub esp,48h      // ebp ->_KTRAP_FRAME 804de741      sub ebp,29Ch   // CurrentThread +124 = 1 804de747      mov byte ptr [esi+140h],1   // 比较 804de74e      cmp ebp,esp // 不相等,发生了异常 804de750      jne nt!KiFastCallEntry2+0x24 (804de6c8)   // _KTRAP_FRAME.Dr7 = 0 804de756      and dword ptr [ebp+2Ch],0   // [esi+2Ch]是KTHREAD.DebugActive当前线程的调试标志 //不等于0xFF表示调试中? 804de75a      test byte ptr [esi+2Ch],0FFh 804de75e      mov dword ptr [esi+134h],ebp 804de764      jne nt!Dr_FastCallDrSave (804de5b0)     // ebx = _KTRAP_FRAME.Ebp 804de76a      mov ebx,dword ptr [ebp+60h]   // edi = _KTRAP_FRAME.EIP 804de76d      mov edi,dword ptr [ebp+68h]   // _KTRAP_FRAME.DbgArgPointer 804de770      mov dword ptr [ebp+0Ch],edx   // _KTRAP_FRAME.DbgArgPointer = DbgArgMark 804de773      mov dword ptr [ebp+8],0BADB0D00h   //_KTRAP_FRAME.DbgEbp  804de77a      mov dword ptr [ebp],ebx   //_KTRAP_FRAME.DbgEip = _KTRAP_FRAME.EIP 804de77d      mov dword ptr [ebp+4],edi //==================================   // 开中断 804de780      sti   //eax为函数序号,但是在KeServiceDescriptorTableShadow时 //是函数序号+1000h,所以在KeServiceDescriptorTableShadow //经过下面运算ecx最终为10h,否则为0 804de781      mov edi,eax 804de783      shr edi,8 804de786      and edi,30h 804de789      mov ecx,edi   //KTHREAD.ServiceTable gdi32.dll和user32.dll调用时, //ServiceTable为KeServiceDescriptorTableShadow, //ntdll.dll调用时ServiceTable为KeServiceDescriptorTable //在KeServiceDescriptorTableShadow时+10正好得到win32.sys调用 804de78b      add edi,dword ptr [esi+0E0h] 804de791      mov ebx,eax   //保留三个字节,得到正真的序号 804de793      and eax,0FFFh   //和ssdt或shadow ssdt总共项数比较,eax大则跳 804de798      cmp eax,dword ptr [edi+8] 804de79b      jae nt!KiBBTUnexpectedRange (804de4e2)   //判断是shadow ssdt还是ssdt,不跳则是shadow ssdt,跳则是ssdt 804de7a1      cmp ecx,10h 804de7a4      jne nt!KiFastCallEntry+0xcc (804de7c0)   // TIB地址送ecx 804de7a6      mov ecx,dword ptr ds:[0FFDFF018h]   // [ecx+0xf70] 是什么东东? 804de7ac      xor ebx,ebx 804de7ae      or ebx,dword ptr [ecx+0F70h] 804de7b4      je nt!KiFastCallEntry+0xcc (804de7c0)   804de7b6      push edx 804de7b7      push eax 804de7b8      call dword ptr [nt!KeGdiFlushUserBatch (8055a164)] 804de7be      pop eax 804de7bf      pop edx   // SSDT // ds:[0FFDFF638h](fs:[638h]) KeSystemCalls  // 仅仅用来记录系统调用的次数,无太多用处 804de7c0      inc dword ptr ds:[0FFDFF638h]   // edx中是参数(堆栈顶的指针) 804de7c6      mov esi,edx 804de7c8      mov ebx,dword ptr [edi+0Ch] 804de7cb      xor ecx,ecx 804de7cd      mov cl,byte ptr [eax+ebx]   //edi=KeServiceDescriptorTable->ServiceTableBase 804de7d0      mov edi,dword ptr [edi]   //计算服务例程地址(edi为KiServiceTable,eax为函数序号) 804de7d2      mov ebx,dword ptr [edi+eax*4] 804de7d5      sub esp,ecx 804de7d7      shr ecx,2 804de7da      mov edi,esp   // 检查esi是否指向用户空间 804de7dc      cmp esi,dword ptr ds:[0EED02CF4h] 804de7e2      jae nt!KiSystemCallExit2+0x9f (804de990)   //把参数复制到内核 804de7e8      rep movs dword ptr es:[edi],dword ptr [esi]   //转入服务例程入口 804de7ea      call ebx   // 恢复堆栈 804de7ec      mov esp,ebp   // fs:[124h]中储存着当前线程的ETHREAD // ETHREAD的偏移134h处为TrapFrame 804de7ee      mov ecx,dword ptr ds:[0FFDFF124h] 804de7f4      mov edx,dword ptr [ebp+3Ch] 804de7f7      mov dword ptr [ecx+134h],edx  。。。。   804de904      sy***it     //附相关数据结构 : lkd> dt nt!_KPCR +0x000 NtTib : _NT_TIB +0x01c SelfPcr : Ptr32 _KPCR +0x020 Prcb : Ptr32 _KPRCB +0x024 Irql : UChar +0x028 IRR : Uint4B +0x02c IrrActive : Uint4B +0x030 IDR : Uint4B +0x034 KdVersionBlock : Ptr32 Void +0x038 IDT : Ptr32 _KIDTENTRY +0x03c GDT : Ptr32 _KGDTENTRY +0x040 TSS : Ptr32 _KTSS +0x044 MajorVersion : Uint2B +0x046 MinorVersion : Uint2B +0x048 SetMember : Uint4B +0x04c StallScaleFactor : Uint4B +0x050 DebugActive : UChar +0x051 Number : UChar +0x052 Spare0 : UChar +0x053 SecondLevelCacheAssociativity : UChar +0x054 VdmAlert : Uint4B +0x058 KernelReserved : [14] Uint4B +0x090 SecondLevelCacheSize : Uint4B +0x094 HalReserved : [16] Uint4B +0x0d4 InterruptMode : Uint4B +0x0d8 Spare1 : UChar +0x0dc KernelReserved2 : [17] Uint4B +0x120 PrcbData : _KPRCB     lkd> dt nt!_NT_TIB +0x000 ExceptionList : Ptr32 _EXCEPTION_REGISTRATION_RECORD +0x004 StackBase : Ptr32 Void +0x008 StackLimit : Ptr32 Void +0x00c SubSystemTib : Ptr32 Void +0x010 FiberData : Ptr32 Void +0x010 Version : Uint4B +0x014 ArbitraryUserPointer : Ptr32 Void +0x018 Self : Ptr32 _NT_TIB     lkd> dt nt!_KPRCB +0x000 MinorVersion : Uint2B +0x002 MajorVersion : Uint2B +0x004 CurrentThread : Ptr32 _KTHREAD +0x008 NextThread : Ptr32 _KTHREAD +0x00c IdleThread : Ptr32 _KTHREAD +0x010 Number : Char +0x011 Reserved : Char +0x012 BuildType : Uint2B +0x014 SetMember : Uint4B +0x018 CpuType : Char +0x019 CpuID : Char +0x01a CpuStep : Uint2B +0x01c ProcessorState : _KPROCESSOR_STATE +0x33c KernelReserved : [16] Uint4B +0x37c HalReserved : [16] Uint4B +0x3bc PrcbPad0 : [92] UChar +0x418 LockQueue : [16] _KSPIN_LOCK_QUEUE +0x498 PrcbPad1 : [8] UChar +0x4a0 NpxThread : Ptr32 _KTHREAD +0x4a4 InterruptCount : Uint4B +0x4a8 KernelTime : Uint4B +0x4ac UserTime : Uint4B +0x4b0 DpcTime : Uint4B +0x4b4 DebugDpcTime : Uint4B +0x4b8 InterruptTime : Uint4B +0x4bc AdjustDpcThreshold : Uint4B +0x4c0 PageColor : Uint4B +0x4c4 SkipTick : Uint4B +0x4c8 MultiThreadSetBusy : UChar +0x4c9 Spare2 : [3] UChar +0x4cc ParentNode : Ptr32 _KNODE +0x4d0 MultiThreadProcessorSet : Uint4B +0x4d4 MultiThreadSetMaster : Ptr32 _KPRCB +0x4d8 ThreadStartCount : [2] Uint4B +0x4e0 CcFastReadNoWait : Uint4B +0x4e4 CcFastReadWait : Uint4B +0x4e8 CcFastReadNotPossible : Uint4B +0x4ec CcCopyReadNoWait : Uint4B +0x4f0 CcCopyReadWait : Uint4B +0x4f4 CcCopyReadNoWaitMiss : Uint4B +0x4f8 KeAlignmentFixupCount : Uint4B +0x4fc KeContextSwitches : Uint4B +0x500 KeDcacheFlushCount : Uint4B +0x504 KeExceptionDispatchCount : Uint4B +0x508 KeFirstLevelTbFills : Uint4B +0x50c KeFloatingEmulationCount : Uint4B +0x510 KeIcacheFlushCount : Uint4B +0x514 KeSecondLevelTbFills : Uint4B +0x518 KeSystemCalls : Uint4B +0x51c SpareCounter0 : [1] Uint4B +0x520 PPLookasideList : [16] _PP_LOOKASIDE_LIST +0x5a0 PPNPagedLookasideList : [32] _PP_LOOKASIDE_LIST +0x6a0 PPPagedLookasideList : [32] _PP_LOOKASIDE_LIST +0x7a0 PacketBarrier : Uint4B +0x7a4 ReverseStall : Uint4B +0x7a8 IpiFrame : Ptr32 Void +0x7ac PrcbPad2 : [52] UChar +0x7e0 CurrentPacket : [3] Ptr32 Void +0x7ec TargetSet : Uint4B +0x7f0 WorkerRoutine : Ptr32 void +0x7f4 IpiFrozen : Uint4B +0x7f8 PrcbPad3 : [40] UChar +0x820 RequestSummary : Uint4B +0x824 SignalDone : Ptr32 _KPRCB +0x828 PrcbPad4 : [56] UChar +0x860 DpcListHead : _LIST_ENTRY +0x868 DpcStack : Ptr32 Void +0x86c DpcCount : Uint4B +0x870 DpcQueueDepth : Uint4B +0x874 DpcRoutineActive : Uint4B +0x878 DpcInterruptRequested : Uint4B +0x87c DpcLastCount : Uint4B +0x880 DpcRequestRate : Uint4B +0x884 MaximumDpcQueueDepth : Uint4B +0x888 MinimumDpcRate : Uint4B +0x88c QuantumEnd : Uint4B +0x890 PrcbPad5 : [16] UChar +0x8a0 DpcLock : Uint4B +0x8a4 PrcbPad6 : [28] UChar +0x8c0 CallDpc : _KDPC +0x8e0 ChainedInterruptList : Ptr32 Void +0x8e4 LookasideIrpFloat : Int4B +0x8e8 SpareFields0 : [6] Uint4B +0x900 VendorString : [13] UChar +0x90d InitialApicId : UChar +0x90e LogicalProcessorsPerPhysicalProcessor : UChar +0x910 MHz : Uint4B +0x914 FeatureBits : Uint4B +0x918 UpdateSignature : _LARGE_INTEGER +0x920 NpxSaveArea : _FX_SAVE_AREA +0xb30 PowerState : _PROCESSOR_POWER_STATE   lkd> dt nt!_KTHREAD +0x000 Header : _DISPATCHER_HEADER +0x010 MutantListHead : _LIST_ENTRY +0x018 InitialStack : Ptr32 Void +0x01c StackLimit : Ptr32 Void +0x020 Teb : Ptr32 Void +0x024 TlsArray : Ptr32 Void +0x028 KernelStack : Ptr32 Void +0x02c DebugActive : UChar +0x02d State : UChar +0x02e Alerted : [2] UChar +0x030 Iopl : UChar +0x031 NpxState : UChar +0x032 Saturation : Char +0x033 Priority : Char +0x034 ApcState : _KAPC_STATE +0x04c ContextSwitches : Uint4B +0x050 IdleSwapBlock : UChar +0x051 Spare0 : [3] UChar +0x054 WaitStatus : Int4B +0x058 WaitIrql : UChar +0x059 WaitMode : Char +0x05a WaitNext : UChar +0x05b WaitReason : UChar +0x05c WaitBlockList : Ptr32 _KWAIT_BLOCK +0x060 WaitListEntry : _LIST_ENTRY +0x060 SwapListEntry : _SINGLE_LIST_ENTRY +0x068 WaitTime : Uint4B +0x06c BasePriority : Char +0x06d DecrementCount : UChar +0x06e PriorityDecrement : Char +0x06f Quantum : Char +0x070 WaitBlock : [4] _KWAIT_BLOCK +0x0d0 LegoData : Ptr32 Void +0x0d4 KernelApcDisable : Uint4B +0x0d8 UserAffinity : Uint4B +0x0dc SystemAffinityActive : UChar +0x0dd PowerState : UChar +0x0de NpxIrql : UChar +0x0df InitialNode : UChar +0x0e0 ServiceTable : Ptr32 Void +0x0e4 Queue : Ptr32 _KQUEUE +0x0e8 ApcQueueLock : Uint4B +0x0f0 Timer : _KTIMER +0x118 QueueListEntry : _LIST_ENTRY +0x120 SoftAffinity : Uint4B +0x124 Affinity : Uint4B +0x128 Preempted : UChar +0x129 ProcessReadyQueue : UChar +0x12a KernelStackResident : UChar +0x12b NextProcessor : UChar +0x12c CallbackStack : Ptr32 Void +0x130 Win32Thread : Ptr32 Void +0x134 TrapFrame : Ptr32 _KTRAP_FRAME +0x138 ApcStatePointer : [2] Ptr32 _KAPC_STATE +0x140 PreviousMode : Char +0x141 EnableStackSwap : UChar +0x142 LargeStack : UChar +0x143 ResourceIndex : UChar +0x144 KernelTime : Uint4B +0x148 UserTime : Uint4B +0x14c SavedApcState : _KAPC_STATE +0x164 Alertable : UChar +0x165 ApcStateIndex : UChar +0x166 ApcQueueable : UChar +0x167 AutoAlignment : UChar +0x168 StackBase : Ptr32 Void +0x16c SuspendApc : _KAPC +0x19c SuspendSemaphore : _KSEMAPHORE +0x1b0 ThreadListEntry : _LIST_ENTRY +0x1b8 FreezeCount : Char +0x1b9 SuspendCount : Char +0x1ba IdealProcessor : UChar +0x1bb DisableBoost : UChar   lkd> dt nt!_KUSER_SHARED_DATA +0x000 TickCountLow     : Uint4B +0x004 TickCountMultiplier : Uint4B +0x008 InterruptTime    : _KSYSTEM_TIME +0x014 SystemTime       : _KSYSTEM_TIME +0x020 TimeZoneBias     : _KSYSTEM_TIME +0x02c ImageNumberLow   : Uint2B +0x02e ImageNumberHigh  : Uint2B +0x030 NtSystemRoot     : [260] Uint2B +0x238 MaxStackTraceDepth : Uint4B +0x23c CryptoExponent   : Uint4B +0x240 TimeZoneId       : Uint4B +0x244 Reserved2        : [8] Uint4B +0x264 NtProductType    : _NT_PRODUCT_TYPE +0x268 ProductTypeIsValid : UChar +0x26c NtMajorVersion   : Uint4B +0x270 NtMinorVersion   : Uint4B +0x274 ProcessorFeatures : [64] UChar +0x2b4 Reserved1        : Uint4B +0x2b8 Reserved3        : Uint4B +0x2bc TimeSlip         : Uint4B +0x2c0 AlternativeArchitecture : _ALTERNATIVE_ARCHITECTURE_TYPE +0x2c8 SystemExpirationDate : _LARGE_INTEGER +0x2d0 SuiteMask        : Uint4B +0x2d4 KdDebuggerEnabled : UChar +0x2d5 NXSupportPolicy  : UChar +0x2d8 ActiveConsoleId  : Uint4B +0x2dc DismountCount    : Uint4B +0x2e0 ComPlusPackage   : Uint4B +0x2e4 LastSystemRITEventTickCount : Uint4B +0x2e8 NumberOfPhysicalPages : Uint4B +0x2ec SafeBootMode     : UChar +0x2f0 TraceLogging     : Uint4B +0x2f8 TestRetInstruction : Uint8B +0x300 SystemCall       : Uint4B +0x304 SystemCallReturn : Uint4B +0x308 SystemCallPad    : [3] Uint8B +0x320 TickCount        : _KSYSTEM_TIME +0x320 TickCountQuad    : Uint8B +0x330 Cookie           : Uint4B   lkd> dt _KTRAP_FRAME nt!_KTRAP_FRAME    +0x000 DbgEbp           : Uint4B    +0x004 DbgEip           : Uint4B    +0x008 DbgArgMark       : Uint4B    +0x00c DbgArgPointer    : Uint4B    +0x010 TempSegCs        : Uint4B    +0x014 TempEsp          : Uint4B    +0x018 Dr0              : Uint4B    +0x01c Dr1              : Uint4B    +0x020 Dr2              : Uint4B    +0x024 Dr3              : Uint4B    +0x028 Dr6              : Uint4B    +0x02c Dr7              : Uint4B    +0x030 SegGs            : Uint4B    +0x034 SegEs            : Uint4B    +0x038 SegDs            : Uint4B    +0x03c Edx              : Uint4B    +0x040 Ecx              : Uint4B    +0x044 Eax              : Uint4B    +0x048 PreviousPreviousMode : Uint4B    +0x04c ExceptionList    : Ptr32 _EXCEPTION_REGISTRATION_RECORD    +0x050 SegFs            : Uint4B    +0x054 Edi              : Uint4B    +0x058 Esi              : Uint4B    +0x05c Ebx              : Uint4B    +0x060 Ebp              : Uint4B    +0x064 ErrCode          : Uint4B    +0x068 Eip              : Uint4B    +0x06c SegCs            : Uint4B    +0x070 EFlags           : Uint4B    +0x074 HardwareEsp      : Uint4B    +0x078 HardwareSegSs    : Uint4B    +0x07c V86Es            : Uint4B    +0x080 V86Ds            : Uint4B    +0x084 V86Fs            : Uint4B    +0x088 V86Gs            : Uint4B


好了,现在算是搞清楚了,系统调用的大致过程是:
 
参数压栈
将服务号保存到eax
将堆栈指针保存到edx
关中断
sysenter
初始化系统栈指针
将各个寄存器的值压入堆栈(在系统栈上保存现场)
将参数从用户栈压入系统堆栈
开中断
确定SSDT表格
调用系统服务例程
。。。(返回准备,回复现场)
sy***it
 
 
有不对的地方欢迎指教!

阅读(2230) | 评论(0) | 转发(0) |
0

上一篇:浅析本机API

下一篇:堆和栈的区别

给主人留下些什么吧!~~