Windows For Reverse Engineers OS Internals
Windows For Reverse Engineers OS Internals
OS Internals
CS-E4330 - Special Course in Information Security
Reverse Engineering Malware
Course 2017
System Mechanisms
Service Dispatching
Service Dispatching
Memory Management
Memory Manager
• Each process sees a large and contiguous private virtual address space
• This virtual address space is known as virtual memory
• As virtual memory can exceed the available physical memory
• The memory manager has two important tasks
• Mapping the access to virtual memory into physical memory
• Paging contents of the memory to disk as physical memory runs out; and
paging the data back into the memory when needed
Virtual Memory
• Every process has its own virtual address space
• Virtual memory provides a logical view of the memory that might not correspond to
its physical layout
• By default, only the lower half can be used by the process for its own private storage
• The OS takes the upper half for its own protected memory utilization
• The memory mappings of the lower half is changed to match the virtual address
space of the next process to be run at every context switch
Virtual Memory
Virtual address spaces between user mode applications 32-bit Windows virtual address space
Source: https://msdn.microsoft.com/windows/hardware/drivers/gettingstarted/virtual-address-spaces
Virtual Memory
64-bit Windows virtual address space System dynamic memory allocation pools
Source: https://msdn.microsoft.com/windows/hardware/drivers/gettingstarted/virtual-address-spaces
Processes and Threads
Process
• A process is an abstraction of a running program
• A process consists of the following essential components:
• A private virtual address space
• An executable program (“the base image”)
• A private list of open handles to resources allocated by the operating system
• An access token, which uniquely identifies the owner, security groups, and
privileges associated with the process
• A process ID
• One or more threads
• Important structures: EPROCESS (kernel mode) and PEB (user mode)
Thread
• A thread is an entity scheduled for execution on the CPU
• A thread consists of the following essential components:
• The CPU state
• Two stacks, one for kernel mode and one for user mode
• Thread-Local Storage (TLS), a private storage area that can be used by certain
Windows subsystems, run-time libraries, and DLLs
• A thread ID
• An access token, which uniquely identifies the owner, security groups, and
privileges associated with the thread
• Important structures: ETHREAD (kernel mode) and TEB (user mode)
Processes and Threads
Applications on Windows
PE - Portable Executable Format
• Object and executables files follow the PE (Portable
Executable) file format
• Full specification available online
• PECOFF.mspx
• Use a hex editor (HT) or specialized PE viewer (PE
Explorer) to explore it
• File extensions commonly used by executables:
• EXE, DLL and SYS
PE Format: http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx
Native API
• Undocumented user mode interface to OS
functionality
• One layer below the Windows API
• Some low-level functionality is only in the Native
API
• See “Windows NT/2000 Native API Reference” by
Nebbettor for further reference
Windows API
• Windows API is the OS interface for applications
• Exposed by a set of system libraries: kernel32.dll, user32.dll, …
• Windows 7 and above refactored the system libraries (kernelbase.dll)
• Several subcategories
• Administration and management (WMI, …)
• Diagnostics (event logging, …)
• Networking
• Security
• System services (access to processes, threads, registry…)
• MSDN is the best source of information for Windows APIs reference
MSDN: https://msdn.microsoft.com
WOW64
• 32-bit process running on
Windows 64-bit OS
• Win32 API emulation
• Implemented as a set of
user mode DLLs, with kernel
support
WOW64 - File System Redirection
• Folder \Windows\System32 stores native 64-bit file images
• Calls from 32-bit code are redirected to \Windows\SysWOW64
• Some subdirectories are excluded for compatibility
• %windir%\system32\drivers\etc and %windir%\system32\spool
• %windir%\system32\catroot and %windir%\system32\catroot2
• %windir%\system32\logfiles and %windir%\system32\driverstore
• Other common folders are handled via environment variables
• 64-bit: %ProgramFiles% -> ”C:\Program Files”
• 32-bit: %ProgramFiles% -> ”C:\Program Files(x86)”
• Automatic redirections can be controlled per thread by using certain APIs
• APIs: Wow64DisableWow64FsRedirection, Wow64RevertWow64FsRedirection