转载: http://bbs.pediy.com/showthread.php?t=89407
在XP系统中, 系统服务在内核的入口是KiFastCallEntry,我们从该入口开始,分析这一段代码都做了什么工作。
由于水平有限和背景知识不够,本人没有完全读懂它们,作为抛砖引玉,我先将我所看懂的和大家分享,希望大家多多指教!
KiFastCallEntry的主要功能有:
背景知识:
1 nt!KiFastCallEntry: 2 3 // 当线程在内核模式下执行时的数据段选择子是23h 4 // 当线程在内核模式下执行时,在FS寄存器中加载的是选择子30, 5 // 用于寻址PCR结构体 6 804de6f0 mov ecx,23h 7 804de6f5 push 30h 8 804de6f7 pop fs 9 10 // 23h->ds;23h->es 11 804de6f9 mov ds,cx 12 804de6fb mov es,cx 13 14 // dword ptr fs:[40h] == dword ptr ds:[0FFDFF040h]{TSS} 15 // dword ptr [ecx+4] 为当前线程的内核模式栈顶指针的值 16 804de6fd mov ecx,dword ptr ds:[0FFDFF040h] 17 804de703 mov esp,dword ptr [ecx+4] 18 19 // ========================================== 20 // 将当前(执行Sysenter前各寄存器的值)上下文压入系统堆栈, 21 22 // _KTRAP_FRAME.HardwareSegSs 23 804de706 push 23h 24 25 // _KTRAP_FRAME.HardwareEsp 26 804de708 push edx 27 28 // _KTRAP_FRAME.EFags 29 804de709 pushfd 30 804de70a push 2 31 804de70c add edx,8 32 33 // EFlags = 2 34 804de70f popfd 35 804de710 or byte ptr [esp+1],2 36 37 // _KTRAP_FRAME.SegCs 38 804de715 push 1Bh 39 40 // ds:[0FFDF0304h] 是SystemCallReturn(_KUser_Shared_Data) 41 // _KTRAP_FRAME.Eip 42 804de717 push dword ptr ds:[0FFDF0304h] 43 44 //_KTRAP_FRAME.ErrorCode 45 804de71d push 0 46 47 //_KTRAP_FRAME.Ebp 48 804de71f push ebp 49 50 //_KTRAP_FRAME.Ebx 51 804de720 push ebx 52 53 //_KTRAP_FRAME.Esi 54 804de721 push esi 55 56 //_KTRAP_FRAME.Edi 57 804de722 push edi 58 59 60 //ebx<-_KPCR.SelfPcr 61 804de723 mov ebx,dword ptr ds:[0FFDFF01Ch] 62 63 // _KTRAP_FRAME.SegFs 64 804de729 push 3Bh 65 66 //esi=_KPCR.PrcbData.CurrentThread 67 804de72b mov esi,dword ptr [ebx+124h] 68 69 //_KTRAP_FRAME.ExceptionList 70 804de731 push dword ptr [ebx] 71 // 初始化链表 72 804de733 mov dword ptr [ebx],0FFFFFFFFh 73 74 //ebp = KPCR.PrcbData.CurrentThread. InitialStack 75 804de739 mov ebp,dword ptr [esi+18h] 76 77 //_KTRAP_FRAME.PreviousPreviousMode = 1 78 804de73c push 1 79 80 // esp->_KTRAP_FRAME 81 804de73e sub esp,48h 82 83 // ebp ->_KTRAP_FRAME 84 804de741 sub ebp,29Ch 85 86 // CurrentThread +124 = 1 87 804de747 mov byte ptr [esi+140h],1 88 89 // 比较 90 804de74e cmp ebp,esp 91 // 不相等,发生了异常 92 804de750 jne nt!KiFastCallEntry2+0x24 (804de6c8) 93 94 // _KTRAP_FRAME.Dr7 = 0 95 804de756 and dword ptr [ebp+2Ch],0 96 97 // [esi+2Ch]是KTHREAD.DebugActive当前线程的调试标志 98 //不等于0xFF表示调试中? 99 804de75a test byte ptr [esi+2Ch],0FFh 100 804de75e mov dword ptr [esi+134h],ebp 101 804de764 jne nt!Dr_FastCallDrSave (804de5b0) 102 103 104 // ebx = _KTRAP_FRAME.Ebp 105 804de76a mov ebx,dword ptr [ebp+60h] 106 107 // edi = _KTRAP_FRAME.EIP 108 804de76d mov edi,dword ptr [ebp+68h] 109 110 // _KTRAP_FRAME.DbgArgPointer 111 804de770 mov dword ptr [ebp+0Ch],edx 112 113 // _KTRAP_FRAME.DbgArgPointer = DbgArgMark 114 804de773 mov dword ptr [ebp+8],0BADB0D00h 115 116 //_KTRAP_FRAME.DbgEbp 117 804de77a mov dword ptr [ebp],ebx 118 119 //_KTRAP_FRAME.DbgEip = _KTRAP_FRAME.EIP 120 804de77d mov dword ptr [ebp+4],edi 121 //================================== 122 123 // 开中断 124 804de780 sti 125 126 //eax为函数序号,但是在KeServiceDescriptorTableShadow时 127 //是函数序号+1000h,所以在KeServiceDescriptorTableShadow 128 //经过下面运算ecx最终为10h,否则为0 129 804de781 mov edi,eax 130 804de783 shr edi,8 131 804de786 and edi,30h 132 804de789 mov ecx,edi 133 134 //KTHREAD.ServiceTable gdi32.dll和user32.dll调用时, 135 //ServiceTable为KeServiceDescriptorTableShadow, 136 //ntdll.dll调用时ServiceTable为KeServiceDescriptorTable 137 //在KeServiceDescriptorTableShadow时+10正好得到win32.sys调用 138 804de78b add edi,dword ptr [esi+0E0h] 139 804de791 mov ebx,eax 140 141 //保留三个字节,得到正真的序号 142 804de793 and eax,0FFFh 143 144 //和ssdt或shadow ssdt总共项数比较,eax大则跳 145 804de798 cmp eax,dword ptr [edi+8] 146 804de79b jae nt!KiBBTUnexpectedRange (804de4e2) 147 148 //判断是shadow ssdt还是ssdt,不跳则是shadow ssdt,跳则是ssdt 149 804de7a1 cmp ecx,10h 150 804de7a4 jne nt!KiFastCallEntry+0xcc (804de7c0) 151 152 // TIB地址送ecx 153 804de7a6 mov ecx,dword ptr ds:[0FFDFF018h] 154 155 // [ecx+0xf70] 是什么东东? 156 804de7ac xor ebx,ebx 157 804de7ae or ebx,dword ptr [ecx+0F70h] 158 804de7b4 je nt!KiFastCallEntry+0xcc (804de7c0) 159 160 804de7b6 push edx 161 804de7b7 push eax 162 804de7b8 call dword ptr [nt!KeGdiFlushUserBatch (8055a164)] 163 804de7be pop eax 164 804de7bf pop edx 165 166 // SSDT 167 // ds:[0FFDFF638h](fs:[638h]) KeSystemCalls 168 // 仅仅用来记录系统调用的次数,无太多用处 169 804de7c0 inc dword ptr ds:[0FFDFF638h] 170 171 // edx中是参数(堆栈顶的指针) 172 804de7c6 mov esi,edx 173 804de7c8 mov ebx,dword ptr [edi+0Ch] 174 804de7cb xor ecx,ecx 175 804de7cd mov cl,byte ptr [eax+ebx] 176 177 //edi=KeServiceDescriptorTable->ServiceTableBase 178 804de7d0 mov edi,dword ptr [edi] 179 180 //计算服务例程地址(edi为KiServiceTable,eax为函数序号) 181 804de7d2 mov ebx,dword ptr [edi+eax*4] 182 804de7d5 sub esp,ecx 183 804de7d7 shr ecx,2 184 804de7da mov edi,esp 185 186 // 检查esi是否指向用户空间 187 804de7dc cmp esi,dword ptr ds:[0EED02CF4h] 188 804de7e2 jae nt!KiSystemCallExit2+0x9f (804de990) 189 190 //把参数复制到内核 191 804de7e8 rep movs dword ptr es:[edi],dword ptr [esi] 192 193 //转入服务例程入口 194 804de7ea call ebx 195 196 // 恢复堆栈 197 804de7ec mov esp,ebp 198 199 // fs:[124h]中储存着当前线程的ETHREAD 200 // ETHREAD的偏移134h处为TrapFrame 201 804de7ee mov ecx,dword ptr ds:[0FFDFF124h] 202 804de7f4 mov edx,dword ptr [ebp+3Ch] 203 804de7f7 mov dword ptr [ecx+134h],edx 204 。。。。 205 206 804de904 sysexit
附相关数据结构 :
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
原文:http://www.cnblogs.com/Acg-Check/p/4268735.html