SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
Windows 系统机制浅析

   2011-8-24 祁伟
•   Windows Components
•   中断与异常
•   KiFastCallEntry vs KiIntSystemCall
•   IRQ vs IRQL
•   FastMutex,Event 同步对象
•   Lookaside 内存管理
•   对象管理器
Windows 2000
Windows Driver Kit
深入解析 Windows 操作系统 . 第 5
          版
系统调用例子:
•   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
中断
• 中断:指当出现需要时, CPU 暂时停止当
  前程序的执行转而执行处理新情况的程序
  和执行过程。即在程序运行过程中,系统
  出现了一个必须由 CPU 立即处理的情况,
  此时, CPU 暂时中止程序的执行转而处理
  这个新的情况的过程就叫做中断。
异常
• 异常:程序在运行过程中发生
  由于硬件设备问 题、软件设计
  错误等导致的程序异常事件 。
• 通过 try…catch 语句进行捕获
  异常。
•  相关函数:
  SetUnhandledExceptionFilter
• 异常处理结构在:线程控制块
  (TEB)0 Byte 偏移处指针标识
  ,即 FS : [0] 处地址。
中断异常管理结构
•   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;
•
异常
•  l 0 号异常为除法运算出错,特别是除数为 0 。
•  l 3 号异常用于通过自陷指令“ INT 3” 实现的程
  序断点。
•  l 6 号异常为遇到非法指令。
•  l 9 号异常为浮点指令异常。
•  l 13 号异常为总保护 (General Protection) ,例
  如企图在用户空间执行特权指令等等。
•  l 14 号异常为存储页面异常。
•  l 16 号异常为浮点运算异常。
中断异常分发函数

•中断分发函数: KiDispatchInterrupt

•异常分发函数:
 KiDispatchException
中断
•   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
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
中断相关函数
• IoConnectInterrupt

• IoDisconnectInterrupt

• KeInitializeInterrupt

• KiInterruptTemplateObject
IRQ

• IRQ 英文全称 Interrupt Request ,中文翻译为中
  断请求线。计算机中有许多设备(例如声卡、硬
  盘等)他们都能在没有 CPU 介入的情况下完成一
  定的工作。但是这些设备还是需要定期中断
  CPU ,让 CPU 为其做一些特定的工作。如果这
  些设备要中断 CPU 的运行,就必需在中断请求线
  上把 CPU 中断的信号发给 CPU 。所以每个设备
  只能使用自己独立的中断请求线。一般来说在
  80286 以上计算机中,共有 16 个中断请求线与各
  种需要用中断的不同外设相连接,(每个中断线
  有一个标号也就是中断号)。
IRQL
•   IRQL 是 Interrupt Request Level ,中断请求级
  别。一个由 windows 虚拟出来的概念,划分在
  windows 下中断的优先级,这里中断包括了硬中
  断和软中断,硬中断是由硬件产生,而软中断则
  是完全虚拟出来的。   处理器在一个 IRQL 上
  执行线程代码。 IRQL 是帮助决定线程如何被中
  断的。在同一处理器上,线程只能被更高级别
  IRQL 的线程能中断。每个处理器都有自己的中
  断 IRQL 。
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 蓝屏错误码。
操作 IRQL 函数
• KeGetCurrentIrql

• KeRaiseIrql : the caller specifies NewIrql
  >= CurrentIrql

• KeLowerIrql : it specifies NewIrql <=
  CurrentIrql
函数调用相关的 IRQL 例子:
• PsCreateSystemThread IRQL =
  PASSIVE_LEVEL.
• PsGetCurrentProcessId running at any
  IRQL.
• KeAcquireSpinLock IRQL <=
  DISPATCH_LEVEL
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
FastMutex 互斥同步对象
•   ExInitializeFastMutex
•   ExAcquireFastMutex
•   ExReleaseFastMutex
•   内核管理结构是: nt!_FAST_MUTEX
•   注记 :IRQL <= APC_LEVEL

•   Ring3 层的方法 :
•   InitializeCriticalSection
•   EnterCriticalSection
•   LeaveCriticalSection
Event 同步对象
•   KeInitializeEvent
•   KeSetEvent
•   KeReadStateEvent
•   KeClearEvent
•   内核管理结构是 nt!_KEVENT 。

•   Ring3 层的方法 :
•   CreateEvent
•   OpenEvent
•   SetEvent
Lookaside 固定大小内存管理
•   ExInitializePagedLookasideList
•   ExAllocateFromPagedLookasideList
•   ExFreeToPagedLookasideList
•   注记: IRQL <= APC_LEVEL 。

• ExInitializeNPagedLookasideList
• ExAllocateFromNPagedLookasideList
• ExFreeToNPagedLookasideList
• 注记: IRQL <= DISPATCH_LEVEL 。
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
对象管理器
对象定义浅说
•   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
对象管理器 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 // 字节
•     );


•




•
• 完

Contenu connexe

En vedette

110823 kn-cachemana-wengjun
110823 kn-cachemana-wengjun110823 kn-cachemana-wengjun
110823 kn-cachemana-wengjunZoom Quiet
 
Permutation Oriented Programming: (Re)searching for alternatives!
Permutation Oriented Programming: (Re)searching for alternatives!Permutation Oriented Programming: (Re)searching for alternatives!
Permutation Oriented Programming: (Re)searching for alternatives!Nelson Brito
 
Intro Psat 20102011
Intro Psat 20102011Intro Psat 20102011
Intro Psat 20102011IS Manila
 
Investigate the role of social network marketing
Investigate the role of social network marketingInvestigate the role of social network marketing
Investigate the role of social network marketingCarman Ho
 
Why social media matters - September 2011
Why social media matters - September 2011Why social media matters - September 2011
Why social media matters - September 2011Redd Marketing
 
Permutation Oriented Programming
Permutation Oriented ProgrammingPermutation Oriented Programming
Permutation Oriented ProgrammingNelson Brito
 
Sect 3, Performance diagnostics
Sect 3, Performance diagnosticsSect 3, Performance diagnostics
Sect 3, Performance diagnosticsLars Moller
 
How to prepare_for_campus_placements_
How to prepare_for_campus_placements_How to prepare_for_campus_placements_
How to prepare_for_campus_placements_iTestiWin.com
 
Marketing finance interface
Marketing finance interfaceMarketing finance interface
Marketing finance interfaceAshwin Malshe
 

En vedette (17)

110823 kn-cachemana-wengjun
110823 kn-cachemana-wengjun110823 kn-cachemana-wengjun
110823 kn-cachemana-wengjun
 
Permutation Oriented Programming: (Re)searching for alternatives!
Permutation Oriented Programming: (Re)searching for alternatives!Permutation Oriented Programming: (Re)searching for alternatives!
Permutation Oriented Programming: (Re)searching for alternatives!
 
Intro to unit.
Intro to unit.Intro to unit.
Intro to unit.
 
Cost of-opportunity
Cost of-opportunityCost of-opportunity
Cost of-opportunity
 
Ac&m jul11 stat_eng
Ac&m jul11 stat_engAc&m jul11 stat_eng
Ac&m jul11 stat_eng
 
Sect 2 KPI's
Sect 2 KPI'sSect 2 KPI's
Sect 2 KPI's
 
Intro Psat 20102011
Intro Psat 20102011Intro Psat 20102011
Intro Psat 20102011
 
Investigate the role of social network marketing
Investigate the role of social network marketingInvestigate the role of social network marketing
Investigate the role of social network marketing
 
Why social media matters - September 2011
Why social media matters - September 2011Why social media matters - September 2011
Why social media matters - September 2011
 
Permutation Oriented Programming
Permutation Oriented ProgrammingPermutation Oriented Programming
Permutation Oriented Programming
 
Sect 3, Performance diagnostics
Sect 3, Performance diagnosticsSect 3, Performance diagnostics
Sect 3, Performance diagnostics
 
How to prepare_for_campus_placements_
How to prepare_for_campus_placements_How to prepare_for_campus_placements_
How to prepare_for_campus_placements_
 
Making Decisions... Parents' Advice
Making Decisions... Parents' AdviceMaking Decisions... Parents' Advice
Making Decisions... Parents' Advice
 
Social Media Marketing & The Fallacy of Going Viral
Social Media Marketing & The Fallacy of Going ViralSocial Media Marketing & The Fallacy of Going Viral
Social Media Marketing & The Fallacy of Going Viral
 
Content is not_flat
Content is not_flatContent is not_flat
Content is not_flat
 
Marketing finance interface
Marketing finance interfaceMarketing finance interface
Marketing finance interface
 
SVN to GitHUb
SVN to GitHUbSVN to GitHUb
SVN to GitHUb
 

Similaire à 110824 knoss-windows系统机制浅析

基于Innodb开发的最佳实践
基于Innodb开发的最佳实践基于Innodb开发的最佳实践
基于Innodb开发的最佳实践wubx
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3Angel Boy
 
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUGYingSiang Geng
 
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
COSCUP 2014 : open source compiler 戰國時代的軍備競賽COSCUP 2014 : open source compiler 戰國時代的軍備競賽
COSCUP 2014 : open source compiler 戰國時代的軍備競賽Kito Cheng
 
Mvcc (oracle, innodb, postgres)
Mvcc (oracle, innodb, postgres)Mvcc (oracle, innodb, postgres)
Mvcc (oracle, innodb, postgres)frogd
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeAngel Boy
 
Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)FLASH开发者交流会
 
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)Shanda innovation institute
 
Sigreturn Oriented Programming
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented ProgrammingAngel Boy
 
Avm2虚拟机浅析与as3性能优化
Avm2虚拟机浅析与as3性能优化Avm2虚拟机浅析与as3性能优化
Avm2虚拟机浅析与as3性能优化Harvey Zhang
 
Arduino 底層原始碼解析心得
Arduino 底層原始碼解析心得Arduino 底層原始碼解析心得
Arduino 底層原始碼解析心得roboard
 
快快樂樂SIMD
快快樂樂SIMD快快樂樂SIMD
快快樂樂SIMDWei-Ta Wang
 
Elastic stack day-2
Elastic stack day-2Elastic stack day-2
Elastic stack day-2YI-CHING WU
 
揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharingYi-Jun Zheng
 
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用勇浩 赖
 
藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)
藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)
藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)GoGoPrivateRyan
 
2011 06-12-lamp-mysql-顾春江
2011 06-12-lamp-mysql-顾春江2011 06-12-lamp-mysql-顾春江
2011 06-12-lamp-mysql-顾春江thinkinlamp
 

Similaire à 110824 knoss-windows系统机制浅析 (20)

基于Innodb开发的最佳实践
基于Innodb开发的最佳实践基于Innodb开发的最佳实践
基于Innodb开发的最佳实践
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3
 
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
探索 ISTIO 新型 DATA PLANE 架構 AMBIENT MESH - GOLANG TAIWAN GATHERING #77 X CNTUG
 
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
COSCUP 2014 : open source compiler 戰國時代的軍備競賽COSCUP 2014 : open source compiler 戰國時代的軍備競賽
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
 
Mvcc (oracle, innodb, postgres)
Mvcc (oracle, innodb, postgres)Mvcc (oracle, innodb, postgres)
Mvcc (oracle, innodb, postgres)
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledge
 
Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)
 
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
 
Sigreturn Oriented Programming
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented Programming
 
Win dbg入门
Win dbg入门Win dbg入门
Win dbg入门
 
Windbg入门
Windbg入门Windbg入门
Windbg入门
 
Rootkit 101
Rootkit 101Rootkit 101
Rootkit 101
 
Avm2虚拟机浅析与as3性能优化
Avm2虚拟机浅析与as3性能优化Avm2虚拟机浅析与as3性能优化
Avm2虚拟机浅析与as3性能优化
 
Arduino 底層原始碼解析心得
Arduino 底層原始碼解析心得Arduino 底層原始碼解析心得
Arduino 底層原始碼解析心得
 
快快樂樂SIMD
快快樂樂SIMD快快樂樂SIMD
快快樂樂SIMD
 
Elastic stack day-2
Elastic stack day-2Elastic stack day-2
Elastic stack day-2
 
揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing
 
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用
 
藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)
藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)
藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)
 
2011 06-12-lamp-mysql-顾春江
2011 06-12-lamp-mysql-顾春江2011 06-12-lamp-mysql-顾春江
2011 06-12-lamp-mysql-顾春江
 

Plus de Zoom Quiet

产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变Zoom Quiet
 
01s0401 go,互联网时代的c语言 许式伟
01s0401 go,互联网时代的c语言   许式伟01s0401 go,互联网时代的c语言   许式伟
01s0401 go,互联网时代的c语言 许式伟Zoom Quiet
 
Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿Zoom Quiet
 
金山云查询系统改进之路1
金山云查询系统改进之路1金山云查询系统改进之路1
金山云查询系统改进之路1Zoom Quiet
 
Zh120226techparty zd-story
Zh120226techparty zd-storyZh120226techparty zd-story
Zh120226techparty zd-storyZoom Quiet
 
Zh120226techparty velocity2011-review
Zh120226techparty velocity2011-reviewZh120226techparty velocity2011-review
Zh120226techparty velocity2011-reviewZoom Quiet
 
Zh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolboxZh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolboxZoom Quiet
 
Velocity2011分享
Velocity2011分享Velocity2011分享
Velocity2011分享Zoom Quiet
 
陈正 Introduction to-sae_python
陈正   Introduction to-sae_python陈正   Introduction to-sae_python
陈正 Introduction to-sae_pythonZoom Quiet
 
111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿Zoom Quiet
 
111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析Zoom Quiet
 
111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发Zoom Quiet
 
ImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_yImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_yZoom Quiet
 
Import this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importersImport this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importersZoom Quiet
 
金山卫士界面框架
金山卫士界面框架金山卫士界面框架
金山卫士界面框架Zoom Quiet
 
111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysqlZoom Quiet
 

Plus de Zoom Quiet (20)

42qu thrift1
42qu thrift142qu thrift1
42qu thrift1
 
产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变
 
Go courseday3
Go courseday3Go courseday3
Go courseday3
 
Go courseday2
Go courseday2Go courseday2
Go courseday2
 
Go courseday1
Go courseday1Go courseday1
Go courseday1
 
01s0401 go,互联网时代的c语言 许式伟
01s0401 go,互联网时代的c语言   许式伟01s0401 go,互联网时代的c语言   许式伟
01s0401 go,互联网时代的c语言 许式伟
 
Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿
 
金山云查询系统改进之路1
金山云查询系统改进之路1金山云查询系统改进之路1
金山云查询系统改进之路1
 
Zh120226techparty zd-story
Zh120226techparty zd-storyZh120226techparty zd-story
Zh120226techparty zd-story
 
Zh120226techparty velocity2011-review
Zh120226techparty velocity2011-reviewZh120226techparty velocity2011-review
Zh120226techparty velocity2011-review
 
Zh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolboxZh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolbox
 
Velocity2011分享
Velocity2011分享Velocity2011分享
Velocity2011分享
 
陈正 Introduction to-sae_python
陈正   Introduction to-sae_python陈正   Introduction to-sae_python
陈正 Introduction to-sae_python
 
111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿
 
111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析
 
111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发
 
ImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_yImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_y
 
Import this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importersImport this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importers
 
金山卫士界面框架
金山卫士界面框架金山卫士界面框架
金山卫士界面框架
 
111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql
 

110824 knoss-windows系统机制浅析

  • 1. Windows 系统机制浅析 2011-8-24 祁伟
  • 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
  • 14. 中断相关函数 • IoConnectInterrupt • IoDisconnectInterrupt • KeInitializeInterrupt • KiInterruptTemplateObject
  • 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 // 字节 • ); • •