Más contenido relacionado
Similar a 110824 knoss-windows系统机制浅析 (20)
110824 knoss-windows系统机制浅析
- 2. • Windows Components
• 中断与异常
• KiFastCallEntry vs KiIntSystemCall
• IRQ vs IRQL
• FastMutex,Event 同步对象
• Lookaside 内存管理
• 对象管理器
- 6. 系统调用例子:
• 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
- 7. 中断
• 中断:指当出现需要时, CPU 暂时停止当
前程序的执行转而执行处理新情况的程序
和执行过程。即在程序运行过程中,系统
出现了一个必须由 CPU 立即处理的情况,
此时, CPU 暂时中止程序的执行转而处理
这个新的情况的过程就叫做中断。
- 8. 异常
• 异常:程序在运行过程中发生
由于硬件设备问 题、软件设计
错误等导致的程序异常事件 。
• 通过 try…catch 语句进行捕获
异常。
• 相关函数:
SetUnhandledExceptionFilter
• 异常处理结构在:线程控制块
(TEB)0 Byte 偏移处指针标识
,即 FS : [0] 处地址。
- 9. 中断异常管理结构
• 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;
•
- 10. 异常
• l 0 号异常为除法运算出错,特别是除数为 0 。
• l 3 号异常用于通过自陷指令“ INT 3” 实现的程
序断点。
• l 6 号异常为遇到非法指令。
• l 9 号异常为浮点指令异常。
• l 13 号异常为总保护 (General Protection) ,例
如企图在用户空间执行特权指令等等。
• l 14 号异常为存储页面异常。
• l 16 号异常为浮点运算异常。
- 12. 中断
• 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
- 13. 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
- 15. IRQ
• IRQ 英文全称 Interrupt Request ,中文翻译为中
断请求线。计算机中有许多设备(例如声卡、硬
盘等)他们都能在没有 CPU 介入的情况下完成一
定的工作。但是这些设备还是需要定期中断
CPU ,让 CPU 为其做一些特定的工作。如果这
些设备要中断 CPU 的运行,就必需在中断请求线
上把 CPU 中断的信号发给 CPU 。所以每个设备
只能使用自己独立的中断请求线。一般来说在
80286 以上计算机中,共有 16 个中断请求线与各
种需要用中断的不同外设相连接,(每个中断线
有一个标号也就是中断号)。
- 16. IRQL
• IRQL 是 Interrupt Request Level ,中断请求级
别。一个由 windows 虚拟出来的概念,划分在
windows 下中断的优先级,这里中断包括了硬中
断和软中断,硬中断是由硬件产生,而软中断则
是完全虚拟出来的。 处理器在一个 IRQL 上
执行线程代码。 IRQL 是帮助决定线程如何被中
断的。在同一处理器上,线程只能被更高级别
IRQL 的线程能中断。每个处理器都有自己的中
断 IRQL 。
- 17. 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 蓝屏错误码。
- 18. 操作 IRQL 函数
• KeGetCurrentIrql
• KeRaiseIrql : the caller specifies NewIrql
>= CurrentIrql
• KeLowerIrql : it specifies NewIrql <=
CurrentIrql
- 19. 函数调用相关的 IRQL 例子:
• PsCreateSystemThread IRQL =
PASSIVE_LEVEL.
• PsGetCurrentProcessId running at any
IRQL.
• KeAcquireSpinLock IRQL <=
DISPATCH_LEVEL
- 20. 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
- 21. FastMutex 互斥同步对象
• ExInitializeFastMutex
• ExAcquireFastMutex
• ExReleaseFastMutex
• 内核管理结构是: nt!_FAST_MUTEX
• 注记 :IRQL <= APC_LEVEL
• Ring3 层的方法 :
• InitializeCriticalSection
• EnterCriticalSection
• LeaveCriticalSection
- 22. Event 同步对象
• KeInitializeEvent
• KeSetEvent
• KeReadStateEvent
• KeClearEvent
• 内核管理结构是 nt!_KEVENT 。
• Ring3 层的方法 :
• CreateEvent
• OpenEvent
• SetEvent
- 23. Lookaside 固定大小内存管理
• ExInitializePagedLookasideList
• ExAllocateFromPagedLookasideList
• ExFreeToPagedLookasideList
• 注记: IRQL <= APC_LEVEL 。
• ExInitializeNPagedLookasideList
• ExAllocateFromNPagedLookasideList
• ExFreeToNPagedLookasideList
• 注记: IRQL <= DISPATCH_LEVEL 。
- 24. 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
- 26. 对象定义浅说
• 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
- 27. 对象管理器 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 // 字节
• );
•
•