28
Windows 系统机制浅析 2011-8-24 祁伟

110824 knoss-windows系统机制浅析

Embed Size (px)

DESCRIPTION

金山卫士开源计划 {v11.08.8.0}开源卫士社区 http://code.ijinshan.com/

Citation preview

Page 1: 110824 knoss-windows系统机制浅析

Windows系统机制浅析

2011-8-24 祁伟

Page 2: 110824 knoss-windows系统机制浅析

• Windows Components

• 中断与异常• KiFastCallEntry vs KiIntSystemCall

• IRQ vs IRQL

• FastMutex,Event同步对象• Lookaside内存管理• 对象管理器

Page 3: 110824 knoss-windows系统机制浅析

Windows 2000

Page 4: 110824 knoss-windows系统机制浅析

Windows Driver Kit

Page 5: 110824 knoss-windows系统机制浅析

深入解析Windows操作系统 .第 5版

Page 6: 110824 knoss-windows系统机制浅析

系统调用例子:• NtUserFindWindowEx:• mov eax, 118Ch• mov edx, 7FFE0300h• call dword ptr [edx]• retn 14h

• NtCreateProcess:• mov eax, 4Fh• mov edx, 7FFE0300h• call dword ptr [edx]• retn 20h

• KiIntSystemCall• lea edx, [esp+arg_4]• int 2Eh• retn

• KiFastSystemCall:• mov edx, esp• sysenter• retn

Page 7: 110824 knoss-windows系统机制浅析

中断

• 中断:指当出现需要时, CPU暂时停止当前程序的执行转而执行处理新情况的程序和执行过程。即在程序运行过程中,系统出现了一个必须由 CPU立即处理的情况,此时, CPU暂时中止程序的执行转而处理

这个新的情况的过程就叫做中断。

Page 8: 110824 knoss-windows系统机制浅析

• 异常:程序在运行过程中发生 由于硬件设备问 题、软件设计

错误等导致的程序异常事件 。• 通过 try…catch语句进行捕获异常。

• 相关函数:SetUnhandledExceptionFilter

• 异常处理结构在:线程控制块(TEB)0 Byte 偏移处指针标识,即 FS: [0]处地址。

异常

Page 9: 110824 knoss-windows系统机制浅析

中断异常管理结构• kd> dt _ktrap_frame• ntdll!_KTRAP_FRAME• .................• +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• ......................

• typedef struct _CONTEXT {• 。。。。。。。• ULONG Edi;• ULONG Esi;• ULONG Ebx;• ULONG Edx;• ULONG Ecx;• ULONG Eax;• ULONG Ebp;• ULONG Eip;• ULONG SegCs; • ULONG EFlags; • ULONG Esp;• ULONG SegSs;• 。。。。• } CONTEXT;•

Page 10: 110824 knoss-windows系统机制浅析

异常

• l 0号异常为除法运算出错,特别是除数为 0。• l 3 “号异常用于通过自陷指令 INT 3”实现的程序断点。

• l 6号异常为遇到非法指令。• l 9号异常为浮点指令异常。• l 13号异常为总保护 (General Protection),例如企图在用户空间执行特权指令等等。

• l 14号异常为存储页面异常。• l 16号异常为浮点运算异常。

Page 11: 110824 knoss-windows系统机制浅析

中断异常分发函数

•中断分发函数: KiDispatchInterrupt

•异常分发函数:KiDispatchException

Page 12: 110824 knoss-windows系统机制浅析

中断• kd> !idt

• Dumping IDT:

• 37: 806ee728 hal!PicSpuriousService37• 3d: 806efb70 hal!HalpApcInterrupt• 41: 806ef9cc hal!HalpDispatchInterrupt• 50: 806ee800 hal!HalpApicRebootService• 62: 81b5a71c atapi!IdePortInterrupt (KINTERRUPT 81b5a6e0)->IDE接口中断程序(支持该接口的硬盘或者光驱设备)• 63: 81965044 NDIS!ndisMIsr (KINTERRUPT 81965008)--->网络 NDIS中断程序• 73: 8192a044 portcls!CKsShellRequestor::`vector deleting destructor'+0x26 (KINTERRUPT 8192a008)• USBPORT!USBPORT_InterruptService (KINTERRUPT 81945008)• 82: 81b5750c atapi!IdePortInterrupt (KINTERRUPT 81b574d0)->IDE接口中断程序• 83: 81b54ccc SCSIPORT!ScsiPortInterrupt (KINTERRUPT 81b54c90)->SCSI接口中断程序• 93: 81926044 i8042prt!I8042KeyboardInterruptService (KINTERRUPT 81926008)->键盘中断程序• a3: 8192c044 i8042prt!I8042MouseInterruptService (KINTERRUPT 8192c008)-->鼠标中断程序• b1: 81bd95c4 ACPI!ACPIInterruptServiceRoutine (KINTERRUPT 81bd9588)• b2: 8194c044 serial!SerialCIsrSw (KINTERRUPT 8194c008)-->串口中断程序• c1: 806ee984 hal!HalpBroadcastCallService• d1: 806edd34 hal!HalpClockInterrupt--->时钟中断• e1: 806eef0c hal!HalpIpiHandler• e3: 806eec70 hal!HalpLocalApicErrorService• fd: 806ef464 hal!HalpProfileInterrupt• fe: 806ef604 hal!HalpPerfInterrupt

Page 13: 110824 knoss-windows系统机制浅析

KINTERRUPT完整结构

• nt!_KINTERRUPT• +0x000 Type : Int2B• +0x002 Size : Int2B• +0x004 InterruptListEntry : _LIST_ENTRY• +0x00c ServiceRoutine : Ptr32 unsigned char • +0x010 ServiceContext : Ptr32 Void• +0x014 SpinLock : Uint4B• +0x018 TickCount : Uint4B• +0x01c ActualLock : Ptr32 Uint4B• +0x020 DispatchAddress : Ptr32 void • +0x024 Vector : Uint4B• +0x028 Irql : UChar• +0x029 SynchronizeIrql : UChar• +0x02a FloatingSave : UChar• +0x02b Connected : UChar• +0x02c Number : Char• +0x02d ShareVector : UChar• +0x030 Mode : _KINTERRUPT_MODE• +0x034 ServiceCount : Uint4B• +0x038 DispatchCount : Uint4B• +0x03c DispatchCode : [106] Uint4B

Page 14: 110824 knoss-windows系统机制浅析

中断相关函数

• IoConnectInterrupt

• IoDisconnectInterrupt

• KeInitializeInterrupt

• KiInterruptTemplateObject

Page 15: 110824 knoss-windows系统机制浅析

IRQ

• IRQ英文全称 Interrupt Request,中文翻译为中断请求线。计算机中有许多设备(例如声卡、硬盘等)他们都能在没有 CPU介入的情况下完成一定的工作。但是这些设备还是需要定期中断CPU,让 CPU为其做一些特定的工作。如果这些设备要中断 CPU的运行,就必需在中断请求线上把 CPU中断的信号发给 CPU。所以每个设备只能使用自己独立的中断请求线。一般来说在80286以上计算机中,共有 16个中断请求线与各种需要用中断的不同外设相连接,(每个中断线

有一个标号也就是中断号)。

Page 16: 110824 knoss-windows系统机制浅析

IRQL

•   IRQL是 Interrupt Request Level,中断请求级别。一个由windows虚拟出来的概念,划分在windows下中断的优先级,这里中断包括了硬中断和软中断,硬中断是由硬件产生,而软中断则

是完全虚拟出来的。   处理器在一个 IRQL上执行线程代码。 IRQL是帮助决定线程如何被中断的。在同一处理器上,线程只能被更高级别IRQL的线程能中断。每个处理器都有自己的中断 IRQL 。

Page 17: 110824 knoss-windows系统机制浅析

IRQL( Interrupt Request Level)

• #define PASSIVE_LEVEL 0 // Passive release level• #define LOW_LEVEL 0 // Lowest interrupt level• #define APC_LEVEL 1 // APC interrupt level• #define DISPATCH_LEVEL 2 // Dispatcher level• #define CMC_LEVEL 3 // Correctable machine check level• #define DEVICE_LEVEL_BASE 4 // 4 - 11 - Device IRQLs• #define PC_LEVEL 12 // Performance Counter IRQL• #define IPI_LEVEL 14 // IPI IRQL• #define CLOCK_LEVEL 13 // Clock Timer IRQL• #define POWER_LEVEL 15 // Power failure level• #define PROFILE_LEVEL 15 // Profiling level• #define HIGH_LEVEL 15 // Highest interrupt level

• 注记:高 IRQL级别函数调用低 IRQL级别函数,会触发IRQL_NOT_LESS_OR_EQUAL蓝屏错误码。

Page 18: 110824 knoss-windows系统机制浅析

操作 IRQL函数

• KeGetCurrentIrql

• KeRaiseIrql : the caller specifies NewIrql >= CurrentIrql

• KeLowerIrql : it specifies NewIrql <= CurrentIrql

Page 19: 110824 knoss-windows系统机制浅析

函数调用相关的 IRQL例子:

• PsCreateSystemThread IRQL = PASSIVE_LEVEL.

• PsGetCurrentProcessId running at any IRQL.

• KeAcquireSpinLock IRQL <= DISPATCH_LEVEL

Page 20: 110824 knoss-windows系统机制浅析

KiFastCallEntry• .text:00413C8A ; ULONG_PTR __stdcall KiLoadFastSyscallMachineSpecificRegisters(ULONG_PTR)• .text:00413C8A _KiLoadFastSyscallMachineSpecificRegisters@4 proc near• .text:00413C8A ; CODE XREF: KiConfigureDynamicProcessor()+61p• .text:00413C8A ; DATA XREF: KiRestoreFastSyscallReturnState()+43o• .text:00413C8A cmp _KiFastSystemCallIsIA32, 0• .text:00413C91 push esi• .text:00413C92 jz short loc_413CCC• .text:00413C94 mov esi, large fs:20h• .text:00413C9B push 0• .text:00413C9D push 8• .text:00413C9F push 174h• .text:00413CA4 call _WRMSR@12 ; WRMSR(x,x,x)• .text:00413CA9 push 0• .text:00413CAB push offset _KiFastCallEntry• .text:00413CB0 push 176h• .text:00413CB5 call _WRMSR@12 ; WRMSR(x,x,x)• .text:00413CBA push 0• .text:00413CBC push dword ptr [esi+1908h]• .text:00413CC2 push 175h• .text:00413CC7 call _WRMSR@12 ; WRMSR(x,x,x)• .text:00413CCC• .text:00413CCC loc_413CCC: ; CODE XREF: KiLoadFastSyscallMachineSpecificRegisters(x)+8j• .text:00413CCC pop esi• .text:00413CCD retn 4

Page 21: 110824 knoss-windows系统机制浅析

FastMutex互斥同步对象

• ExInitializeFastMutex • ExAcquireFastMutex• ExReleaseFastMutex• 内核管理结构是: nt!_FAST_MUTEX• 注记 :IRQL <= APC_LEVEL

• Ring3层的方法 :• InitializeCriticalSection• EnterCriticalSection• LeaveCriticalSection

Page 22: 110824 knoss-windows系统机制浅析

Event同步对象

• KeInitializeEvent• KeSetEvent• KeReadStateEvent• KeClearEvent• 内核管理结构是 nt!_KEVENT。

• Ring3层的方法 :• CreateEvent• OpenEvent• SetEvent

Page 23: 110824 knoss-windows系统机制浅析

Lookaside固定大小内存管理

• ExInitializePagedLookasideList• ExAllocateFromPagedLookasideList• ExFreeToPagedLookasideList• 注记: IRQL <= APC_LEVEL。

• ExInitializeNPagedLookasideList• ExAllocateFromNPagedLookasideList • ExFreeToNPagedLookasideList• 注记: IRQL <= DISPATCH_LEVEL。

Page 24: 110824 knoss-windows系统机制浅析

Lookaside管理结构:• ntdll!_GENERAL_LOOKASIDE• +0x000 ListHead : _SLIST_HEADER• +0x008 Depth : Uint2B• +0x00a MaximumDepth : Uint2B• +0x00c TotalAllocates : Uint4B• +0x010 AllocateMisses : Uint4B• +0x010 AllocateHits : Uint4B• +0x014 TotalFrees : Uint4B• +0x018 FreeMisses : Uint4B• +0x018 FreeHits : Uint4B• +0x01c Type : _POOL_TYPE• +0x020 Tag : Uint4B• +0x024 Size : Uint4B• +0x028 Allocate : Ptr32 void* • +0x02c Free : Ptr32 void • +0x030 ListEntry : _LIST_ENTRY• +0x038 LastTotalAllocates : Uint4B• +0x03c LastAllocateMisses : Uint4B• +0x03c LastAllocateHits : Uint4B• +0x040 Future : [2] Uint4B

Page 25: 110824 knoss-windows系统机制浅析

对象管理器

Page 26: 110824 knoss-windows系统机制浅析

对象定义浅说

• Directory---->nt!_OBJECT_DIRECTORY• SymbolicLink --->nt!_OBJECT_SYMBOLIC_LINK• Event--->nt!_KEVENT• Mutant--->nt!_KMUTANT• Semaphore-->nt!_KSEMAPHORE• Timer-->nt!_KTIMER• Adapter-->_ADAPTER_OBJECT• Device ---->nt!_DEVICE_OBJECT• Driver----->nt!_DRIVER_OBJECT• Key--->nt!_CM_KEY_BODY• Port--->_LPCP_PORT_OBJECT• Section--->nt!_SECTION_OBJECT• Process-->nt!_EPROCESS• Thread--->nt!_ETHREAD• Token--->nt!_TOKEN

Page 27: 110824 knoss-windows系统机制浅析

对象管理器 API• NTSYSAPI NTSTATUS NTAPI• NtCreateDirectoryObject(• OUT PHANDLE DirectoryHandle,• IN ACCESS_MASK DesiredAccess,• IN POBJECT_ATTRIBUTES ObjectAttributes• );• • NTSYSAPI NTSTATUS NTAPI• NtOpenDirectoryObject(• OUT PHANDLE DirectoryHandle,• IN ACCESS_MASK DesiredAccess,• IN POBJECT_ATTRIBUTES ObjectAttributes• );

• NTSYSAPI NTSTATUS NTAPI• NtCreateSymbolicLinkObject(• OUT PHANDLE ObjectHandle,• IN ACCESS_MASK DesiredAccess,• IN POBJECT_ATTRIBUTES ObjectAttributes,• IN PUNICODE_STRING SubstituteString• );• • NTSYSAPI NTSTATUS NTAPI• NtOpenSymbolicLinkObject(• OUT PHANDLE ObjectHandle,• IN ACCESS_MASK DesiredAccess,• IN POBJECT_ATTRIBUTES ObjectAttributes• );• • typedef struct _OBJECT_NAME_INFORMATION {• UNICODE_STRING Name;• } OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION;• • NTSYSAPI NTSTATUS NTAPI• NtQuerySymbolicLinkObject(• IN HANDLE ObjectHandle,• OUT POBJECT_NAME_INFORMATION SubstituteString,• OUT PULONG SubstituteStringLength //字节• );

Page 28: 110824 knoss-windows系统机制浅析

• 完