Download - Windows internals Essentials

Transcript
Page 1: Windows internals Essentials

Windows Internals (introduction)Windows Internals (introduction)John OmbagiJohn Ombagi

Page 2: Windows internals Essentials

IntroductionIntroduction

Page 3: Windows internals Essentials

● Basic Operating System Concepts.● How the Windows OS is implemented.● Simplified inner working of this OS.● Not a detail guide.

● Basic Operating System Concepts.● How the Windows OS is implemented.● Simplified inner working of this OS.● Not a detail guide.

OverviewOverview

Page 4: Windows internals Essentials

Expectation & Literature● Knowledge gained can be applied in:

– Malware analysis & reverse engineering

– Exploit development & software development.

● Books Required:– Windows Internals 6 (part one and two).

– The recent Windows Internals 7 (part one).

● Knowledge gained can be applied in:– Malware analysis & reverse engineering

– Exploit development & software development.

● Books Required:– Windows Internals 6 (part one and two).

– The recent Windows Internals 7 (part one).

Page 5: Windows internals Essentials

User mode & Kernel modeUser mode & Kernel mode

Page 6: Windows internals Essentials

● Allows access to non-operating system code and data only

● No access to the hardware● Protect user application form crashing the

system

● Allows access to non-operating system code and data only

● No access to the hardware● Protect user application form crashing the

system

User modeUser mode

Page 7: Windows internals Essentials

● Privileged mode for use by the kernel and device drivers only.

● Allows access to all system resources● Can potentially crash system (BoD?)

● Privileged mode for use by the kernel and device drivers only.

● Allows access to all system resources● Can potentially crash system (BoD?)

Kernel modeKernel mode

Page 8: Windows internals Essentials

Lab Setup● Windows 7/8/10 (I’ll be using 32 bit).● Windbg, OllyDbg, Immunituy, x64dbg● IDA Pro ver. 6 or 7● Windows System internals Suite● Python 2.7● Provided Workshop Tools

● Windows 7/8/10 (I’ll be using 32 bit).● Windbg, OllyDbg, Immunituy, x64dbg● IDA Pro ver. 6 or 7● Windows System internals Suite● Python 2.7● Provided Workshop Tools

Page 9: Windows internals Essentials

ProcessesProcesses

Page 10: Windows internals Essentials

● It’s a set of resources to execute a program.● It’s a set of resources to execute a program.

What’s a Process?What’s a Process?

Page 11: Windows internals Essentials

● A private Virtual Address space.● An executable program● A private table of handles to various kernel

objects● A security context (access token)● One or more threads that execute code.

● A private Virtual Address space.● An executable program● A private table of handles to various kernel

objects● A security context (access token)● One or more threads that execute code.

What constitute a process?What constitute a process?

Page 12: Windows internals Essentials

ThreadsThreads

Page 13: Windows internals Essentials

● This is an entity that is scheduled by the kernel to execute code.

● This is an entity that is scheduled by the kernel to execute code.

What’s a Thread?What’s a Thread?

Page 14: Windows internals Essentials

● The state of CPU registers● Current access mode● Two stacks, (userspace and kernel space)● Thread Local Storage (TLS)● Optional Security Token● Optional message queue

● The state of CPU registers● Current access mode● Two stacks, (userspace and kernel space)● Thread Local Storage (TLS)● Optional Security Token● Optional message queue

What constitute a thread?What constitute a thread?

Page 15: Windows internals Essentials

● There are several states but the most important ones are as follows:– Running– Ready– Waiting

● There are several states but the most important ones are as follows:– Running– Ready– Waiting

Thread StateThread State

Page 16: Windows internals Essentials

Demo

Page 17: Windows internals Essentials

Virtual MemoryVirtual Memory

Page 18: Windows internals Essentials

● Each process "sees" a flat linear memory.● Virtual memory may be mapped to physical

memory, but may also be stored on disk. ● Processes access memory regardless of

where it actually resides.● The memory manager handles mapping of

virtual to physical page.

● Each process "sees" a flat linear memory.● Virtual memory may be mapped to physical

memory, but may also be stored on disk. ● Processes access memory regardless of

where it actually resides.● The memory manager handles mapping of

virtual to physical page.

OverviewOverview

Page 19: Windows internals Essentials
Page 20: Windows internals Essentials

● Windows Internals, Sixth Edition, Part 1 (page 15)– Virtual Memory Mapping – Virtual Memory Layout

● Windows Internals, Sixth Edition, Part 1 (page 15)– Virtual Memory Mapping – Virtual Memory Layout

Further ReadingFurther Reading

Page 21: Windows internals Essentials

Demo.

Page 22: Windows internals Essentials

Object and HandlesObject and Handles

Page 23: Windows internals Essentials

● Windows is an object based system.● Objects are run time instance of static

structures & reside in system memory space.● Kernel code can obtain direct pointer to an

object. In user mode, code can only obtain a handle to an object.

● Objects are reference counted.

● Windows is an object based system.● Objects are run time instance of static

structures & reside in system memory space.● Kernel code can obtain direct pointer to an

object. In user mode, code can only obtain a handle to an object.

● Objects are reference counted.

DetailsDetails

Page 24: Windows internals Essentials

Windows Design GoalWindows Design Goal

Page 25: Windows internals Essentials

● Separate address space per process.● Protected Kernel.● Pre-emptive multitasking & multithreading.● Internationalization support using Unicode.● Security throughout the System.● Integrated Networking.

● Separate address space per process.● Protected Kernel.● Pre-emptive multitasking & multithreading.● Internationalization support using Unicode.● Security throughout the System.● Integrated Networking.

OverviewOverview

Page 26: Windows internals Essentials

● Powerful File System (NTFS).● Run most 16 bit Windows and DOS.

applications.● Run POSIX 1003.1 and OS/2 applications.● Portable across processors and platforms.● Be great client as well as server platform.

● Powerful File System (NTFS).● Run most 16 bit Windows and DOS.

applications.● Run POSIX 1003.1 and OS/2 applications.● Portable across processors and platforms.● Be great client as well as server platform.

Cont.Cont.

Page 27: Windows internals Essentials

Core System FilesCore System Files

Page 28: Windows internals Essentials

● On 64 bit Systems, we have Ntoskrnl.exe● On 32 bit Systems, we have NtKrnlPa.exe

– Physical Address Extensions (PAE) kernel● PAE uses paging tables to map the memory

greater than 4 GB.

● On 64 bit Systems, we have Ntoskrnl.exe● On 32 bit Systems, we have NtKrnlPa.exe

– Physical Address Extensions (PAE) kernel● PAE uses paging tables to map the memory

greater than 4 GB.

Executive and KernelExecutive and Kernel

Page 29: Windows internals Essentials

● Stands for Hardware Abstraction Layer.● In Windows there are several Hal and the

best is chosen during installation.● It's a layer that insulate the kernel and the

drivers from the actual hardware.

● Stands for Hardware Abstraction Layer.● In Windows there are several Hal and the

best is chosen during installation.● It's a layer that insulate the kernel and the

drivers from the actual hardware.

Hal.dllHal.dll

Page 30: Windows internals Essentials

● It's a Kernel mode component of the Windows subsystem.

● It handles Windowing and GDI (Graphics).

● It's a Kernel mode component of the Windows subsystem.

● It handles Windowing and GDI (Graphics).

Win32k.sysWin32k.sys

Page 31: Windows internals Essentials

● It's the lower layer of the user mode.● It has System support routines and Native

API dispatcher to execute services.● It provides functions to jump into Kernel

mode.● Provides other simple functions that are

similar to the C runtime library.

● It's the lower layer of the user mode.● It has System support routines and Native

API dispatcher to execute services.● It provides functions to jump into Kernel

mode.● Provides other simple functions that are

similar to the C runtime library.

NtDll.dllNtDll.dll

Page 32: Windows internals Essentials

● Applications don't call the Kernel or Ntdll.dll directly, NativeAPI is undocumented.

● The way to do it, is by using the official, documented Subsystem DLLs.

● They transition into Ntdll.dll if needed or bypass that for GDI/Windowing calls.

● Applications don't call the Kernel or Ntdll.dll directly, NativeAPI is undocumented.

● The way to do it, is by using the official, documented Subsystem DLLs.

● They transition into Ntdll.dll if needed or bypass that for GDI/Windowing calls.

Kernel32.dll, user32.dll, gdi32.dll, advapi32.dll etc...Kernel32.dll, user32.dll, gdi32.dll, advapi32.dll etc...

Page 33: Windows internals Essentials

CSRSS.exe CSRSS.exe ● Client Server Runtime Subsystem● This is the process that manages the

Windows Subsystem.● It's very important and always running● Killing this process will result into a blue

screen of death.

● Client Server Runtime Subsystem● This is the process that manages the

Windows Subsystem.● It's very important and always running● Killing this process will result into a blue

screen of death.

Page 34: Windows internals Essentials

Symmetric MultiprocessingSymmetric Multiprocessing

Page 35: Windows internals Essentials

● All CPUs are the same and share main memory and have equal access to peripheral devices.

● No Master or Slave CPUs.● Basic architecture supports up to 32/64

CPUs.

● All CPUs are the same and share main memory and have equal access to peripheral devices.

● No Master or Slave CPUs.● Basic architecture supports up to 32/64

CPUs.

SMPSMP

Page 36: Windows internals Essentials

● It used bitmask, which was the size of the machine WORD.

● However starting on Windows 7 64 bit and 2008 R2 support up to 256 cores.

A Processor Group can contain up to 64 Processors, there are four possible processor groups in a 256 core system.

● It used bitmask, which was the size of the machine WORD.

● However starting on Windows 7 64 bit and 2008 R2 support up to 256 cores.

A Processor Group can contain up to 64 Processors, there are four possible processor groups in a 256 core system.

SMP cont.SMP cont.

Page 37: Windows internals Essentials

SubsystemsSubsystems

Page 38: Windows internals Essentials

● It exposes service via subsystem DLLS.● These subsystem DLLs expose the API of that

subsystem in a way that is fitting to that subsystem.

● The Windows subsystem must always be running.

● It’s loaded automatically during boots up

● It exposes service via subsystem DLLS.● These subsystem DLLs expose the API of that

subsystem in a way that is fitting to that subsystem.

● The Windows subsystem must always be running.

● It’s loaded automatically during boots up

OverviewOverview

Page 39: Windows internals Essentials

Subsytems cont..Subsytems cont..● Information about Subsystems are stored in

the following registry key:– HKLM\System\CCS\Control\Session

Manger\Subsystems● Subsystems DLLs are the ones exposing a

particular API.

● Information about Subsystems are stored in the following registry key:– HKLM\System\CCS\Control\Session

Manger\Subsystems● Subsystems DLLs are the ones exposing a

particular API.

Page 40: Windows internals Essentials

Cont.Cont.● An image of a certain subsystem calls API

functions exposed through the subsystem DLLs.

● Some processes start up before the Windows Subsystem is up.

● Most dispatcher to kernel services are using Windows API "wrappers".

● An image of a certain subsystem calls API functions exposed through the subsystem DLLs.

● Some processes start up before the Windows Subsystem is up.

● Most dispatcher to kernel services are using Windows API "wrappers".

Page 41: Windows internals Essentials

System ProcessesSystem Processes

Page 42: Windows internals Essentials

Idle Process.Idle Process.● it has an id a PID of 0 which is not a real

process.● It's just one thread per CPU (core).● It's a count for Idle time.

● it has an id a PID of 0 which is not a real process.

● It's just one thread per CPU (core).● It's a count for Idle time.

Page 43: Windows internals Essentials

System ProcessSystem Process● It's a real process and has a fixed PID of 4.● It represent stuff going on in the Kernel● It execute code in system space only.● Created by PsCreateSystemThread kernel API● The threads don't need a particular process

to maintain running (on as System is alive)

● It's a real process and has a fixed PID of 4.● It represent stuff going on in the Kernel● It execute code in system space only.● Created by PsCreateSystemThread kernel API● The threads don't need a particular process

to maintain running (on as System is alive)

Page 44: Windows internals Essentials

Session manger (Smss.exe)Session manger (Smss.exe)● It runs the \windows\system32\smss.exe● it’s the first user mode process created by

System (the kernel, part of the boot process)● Because at this time the windows Subsystem

is not loaded yet, it uses the Native API provided by the NtDll.dll.

● It runs the \windows\system32\smss.exe● it’s the first user mode process created by

System (the kernel, part of the boot process)● Because at this time the windows Subsystem

is not loaded yet, it uses the Native API provided by the NtDll.dll.

Page 45: Windows internals Essentials

Session manger contd...Session manger contd...– Creating system environment variables.– launching the subsystem processes– It also launches itself in other sessions– Waits for csrss.exe instances to terminate. – Waits for subsystem creation request.– Waits for terminal services session creation

requests.

– Creating system environment variables.– launching the subsystem processes– It also launches itself in other sessions– Waits for csrss.exe instances to terminate. – Waits for subsystem creation request.– Waits for terminal services session creation

requests.

Page 46: Windows internals Essentials

Windows Subsystem (Csrss.exe)Windows Subsystem (Csrss.exe)● It runs \windows\system32\csrss.exe● Provides the user mode side of the Win32

subsystem. ● CSRSS is mainly responsible for Win32

console handling and GUI shutdown.● It is critical to system operation

● It runs \windows\system32\csrss.exe● Provides the user mode side of the Win32

subsystem. ● CSRSS is mainly responsible for Win32

console handling and GUI shutdown.● It is critical to system operation

Page 47: Windows internals Essentials

Logon Process (Winlogon.exe)Logon Process (Winlogon.exe)● It runs \windows\system32\winlogon.exe

– This handles interactive logons and logoffs. If terminated, logs off the user session

– It's also responsible for capturing of Secure Attention Sequence (SAS) - Ctrl + Alt + Del

– Presents username / password dialog (through LogonUI.exe).

● It runs \windows\system32\winlogon.exe– This handles interactive logons and logoffs. If

terminated, logs off the user session– It's also responsible for capturing of Secure

Attention Sequence (SAS) - Ctrl + Alt + Del– Presents username / password dialog (through

LogonUI.exe).

Page 48: Windows internals Essentials

Local Sec. Auth. Server (Lsass.exe)Local Sec. Auth. Server (Lsass.exe)● Running the \windows\system32\lsass.exe● It calls the appropriate authentication

packages.● Upon successful authentication, creates a

token representing the user's security profile.

● Returns information to Winlogon.

● Running the \windows\system32\lsass.exe● It calls the appropriate authentication

packages.● Upon successful authentication, creates a

token representing the user's security profile.

● Returns information to Winlogon.

Page 49: Windows internals Essentials

Service Control Manager (SCM)Service Control Manager (SCM)● Running \windows\system32\services.exe

– Responsible for starting, stopping and interacting with service processes.

– SERVICE : Similar to 'Unix' daemon processes; Not running in Kernel mode in any way; Normal Windows executable that interact with SCM; Can run under "special" accounts (LocalSystem, NetworkService, LocalService).

● Running \windows\system32\services.exe– Responsible for starting, stopping and

interacting with service processes.– SERVICE : Similar to 'Unix' daemon processes;

Not running in Kernel mode in any way; Normal Windows executable that interact with SCM; Can run under "special" accounts (LocalSystem, NetworkService, LocalService).

Page 50: Windows internals Essentials

Local Session Manager (Lsm.exe)Local Session Manager (Lsm.exe)– A helper Introduced in Windows Vista that does

some managing of the local session and provides information to smss.exe if needed.

– Running image \windows\system32\lsm.exe– In windows 8, turned into a service.– Manages terminal sessions on the local machine

– A helper Introduced in Windows Vista that does some managing of the local session and provides information to smss.exe if needed.

– Running image \windows\system32\lsm.exe– In windows 8, turned into a service.– Manages terminal sessions on the local machine

Page 51: Windows internals Essentials

Wow64 (Windows in Windows 64)Wow64 (Windows in Windows 64)Wow64 allows execution of Win32 (32 bit exes) binaries on 64-bit Windows.

– The isWow64Process function can tell whether a process is running under Wow64.

– Filesystem:● \windows\system32 contains 64 bit images● \windows\syswow64 containd 32 bit images

Wow64 allows execution of Win32 (32 bit exes) binaries on 64-bit Windows.

– The isWow64Process function can tell whether a process is running under Wow64.

– Filesystem:● \windows\system32 contains 64 bit images● \windows\syswow64 containd 32 bit images

Page 52: Windows internals Essentials

Wow64-ArchWow64-Arch● Wow64 Restrictions:

– A 64 bit process cannot load a 32 bit DLLs and vice versa except resource-only DLLs.

● Filesystem Redirection:– \Windows\System32 maps to

\Windows\Syswow64.– \Program Files (x86) & \Program Files

● Wow64 Restrictions:– A 64 bit process cannot load a 32 bit DLLs and

vice versa except resource-only DLLs.● Filesystem Redirection:

– \Windows\System32 maps to \Windows\Syswow64.

– \Program Files (x86) & \Program Files

Page 53: Windows internals Essentials

● Registry Redirection:● COM components trying to register as 32 bit and 64

bit will crush. ● 32 bit components are redirected to the Wow64

registry node (Wow6432Node)– HKYE_LOCAL_MACHINES\Software– HKEY_CLASSES_ROOT– HKEY_CURRENT_USER\Software\Classes

● New flags for Registry APIs allow access to the 64 bit or 32 bit nodes

– KEY_WOW64_64KEY & KEY_WOW64_32KEY

● Registry Redirection:● COM components trying to register as 32 bit and 64

bit will crush. ● 32 bit components are redirected to the Wow64

registry node (Wow6432Node)– HKYE_LOCAL_MACHINES\Software– HKEY_CLASSES_ROOT– HKEY_CURRENT_USER\Software\Classes

● New flags for Registry APIs allow access to the 64 bit or 32 bit nodes

– KEY_WOW64_64KEY & KEY_WOW64_32KEY

Page 54: Windows internals Essentials

End of Part One.John Ombagi

[email protected]

End of Part One.John Ombagi

[email protected]