Search This Blog

Showing posts with label .process. Show all posts
Showing posts with label .process. Show all posts

Thursday, 18 September 2014

WinDbg : An Introduction To Windows Heaps

WinDbg : An Introduction To Windows Heaps 


  1. dt
  2. dd
  3. !process
  4. .process


Heaps are used by applications which need to allocate and release memory dynamically. Even though the heap is the most common facility to accommodate dynamic memory allocations, it is not the only way. Several other methods are available:

  • Memory can be requested through the C run time (CRT).
  • The virtual memory manager or some other private memory manager.
Even though these methods are different, they are tightly coupled internally at the memory manager's level.

When a process starts, the heap manager automatically creates a new heap called the 'default process heap'. Although some processes are known to use the default heap, a very very large number rely on the CRT heap (using malloc/free family of APIs, or the default new/delete family of APIs).

Some processes however also create additional heaps (via HeapCreate(...) ). this method has advantage of isolating different components running in the process. Thus memory leaks can be easier to detect and debug, if we know exactly which heap is leaking. It is indeed good programming practice. Kernel programmers are used to using different tags for allocating memory for different objects, this method described above is the usermode solution to the problem.

The Windows heap manager has two sub components. 

  • Front End Allocator.
  • Back End Allocator.
The Front End Allocator is an optimization layer for the actual Back End Allocator. Thus, by choosing different front end allocators applications with differnt memory requirements can function appropriately. Example, some programs might expect small bursts of allocations, and thus might prefer to use a low fragmentation front end allocator. 

Windows supplies two different front end allocators:

  • Low fragmentation (LF) Front End Allocator.
  • Look aside List (LAL) Front End Allocator.
Vista and above use the LF allocator by default, where as all older generations use the LAL allocator. The differences between these allocators re beyond the scope of the current discussion.

If the Front End Allocator is unable to satisfy the allocation requests, then the Back End Allocator takes over. It is made up of free lists. Each list has blocks of specific sizes. Such free blocks are sorted in ascending order of sizes. Again, a more elaborate discussion on how this is managed, or of it's algorithms is beyond the scope of this current article.

Enough theory, lets get into some practicals.

The first step for us is to determine which all heaps are active for a process.


kd> $ Lets use the lsass process as an example.
kd> $ First we need to find it's EPROCESS

kd> !process 0 0 lsass.exe
PROCESS 84cdc860  SessionId: 0  Cid: 0208    Peb: 7ffda000  ParentCid: 018c
    DirBase: 1eed30e0  ObjectTable: 95f00d18  HandleCount: 513.
    Image: lsass.exe

kd> $ Next we need to switch contexts to this process (this is because this session is running from Kernel mode)

kd> .process /p /r 84cdc860  
Implicit process is now 84cdc860
Loading User Symbols
............................................................

kd> $ The Process execution block (_PEB) helps us with finding the active heaps, so lets use dt to find it

kd> dt _PEB @$peb
ntdll!_PEB
   +0x000 InheritedAddressSpace : 0 ''
   +0x001 ReadImageFileExecOptions : 0 ''
   +0x002 BeingDebugged    : 0 ''
   +0x003 BitField         : 0x8 ''
   +0x003 ImageUsesLargePages : 0y0
   +0x003 IsProtectedProcess : 0y0
   +0x003 IsLegacyProcess  : 0y0
   +0x003 IsImageDynamicallyRelocated : 0y1
   +0x003 SkipPatchingUser32Forwarders : 0y0
   +0x003 SpareBits        : 0y000
   +0x004 Mutant           : 0xffffffff Void
   +0x008 ImageBaseAddress : 0x00d90000 Void
   +0x00c Ldr              : 0x77c77880 _PEB_LDR_DATA
   +0x010 ProcessParameters : 0x00330f18 _RTL_USER_PROCESS_PARAMETERS
   +0x014 SubSystemData    : (null) 
   +0x018 ProcessHeap      : 0x00330000 Void
   +0x01c FastPebLock      : 0x77c77380 _RTL_CRITICAL_SECTION
   +0x020 AtlThunkSListPtr : (null) 
   +0x024 IFEOKey          : (null) 
   +0x028 CrossProcessFlags : 0
   +0x028 ProcessInJob     : 0y0
   +0x028 ProcessInitializing : 0y0
   +0x028 ProcessUsingVEH  : 0y0
   +0x028 ProcessUsingVCH  : 0y0
   +0x028 ProcessUsingFTH  : 0y0
   +0x028 ReservedBits0    : 0y000000000000000000000000000 (0)
   +0x02c KernelCallbackTable : 0x7790d568 Void
   +0x02c UserSharedInfoPtr : 0x7790d568 Void
   +0x030 SystemReserved   : [1] 0
   +0x034 AtlThunkSListPtr32 : 0
   +0x038 ApiSetMap        : 0x77de0000 Void
   +0x03c TlsExpansionCounter : 0
   +0x040 TlsBitmap        : 0x77c77260 Void
   +0x044 TlsBitmapBits    : [2] 0x1fffff
   +0x04c ReadOnlySharedMemoryBase : 0x7f6f0000 Void
   +0x050 HotpatchInformation : (null) 
   +0x054 ReadOnlyStaticServerData : 0x7f6f0590  -> (null) 
   +0x058 AnsiCodePageData : 0x7ffb0000 Void
   +0x05c OemCodePageData  : 0x7ffc0224 Void
   +0x060 UnicodeCaseTableData : 0x7ffd0648 Void
   +0x064 NumberOfProcessors : 1
   +0x068 NtGlobalFlag     : 0
   +0x070 CriticalSectionTimeout : _LARGE_INTEGER 0xffffe86d`079b8000
   +0x078 HeapSegmentReserve : 0x100000
   +0x07c HeapSegmentCommit : 0x2000
   +0x080 HeapDeCommitTotalFreeThreshold : 0x10000
   +0x084 HeapDeCommitFreeBlockThreshold : 0x1000
   +0x088 NumberOfHeaps    : 4
   +0x08c MaximumNumberOfHeaps : 0x10
   +0x090 ProcessHeaps     : 0x77c77500  -> 0x00330000 Void
   +0x094 GdiSharedHandleTable : 0x004d0000 Void
   +0x098 ProcessStarterHelper : (null) 
   +0x09c GdiDCAttributeList : 0x14
   +0x0a0 LoaderLock       : 0x77c77340 _RTL_CRITICAL_SECTION
   +0x0a4 OSMajorVersion   : 6
   +0x0a8 OSMinorVersion   : 1
   +0x0ac OSBuildNumber    : 0x1db1
   +0x0ae OSCSDVersion     : 0x100
   +0x0b0 OSPlatformId     : 2
   +0x0b4 ImageSubsystem   : 2
   +0x0b8 ImageSubsystemMajorVersion : 6
   +0x0bc ImageSubsystemMinorVersion : 1
   +0x0c0 ActiveProcessAffinityMask : 1
   +0x0c4 GdiHandleBuffer  : [34] 0
   +0x14c PostProcessInitRoutine : (null) 
   +0x150 TlsExpansionBitmap : 0x77c77268 Void
   +0x154 TlsExpansionBitmapBits : [32] 1
   +0x1d4 SessionId        : 0
   +0x1d8 AppCompatFlags   : _ULARGE_INTEGER 0x0
   +0x1e0 AppCompatFlagsUser : _ULARGE_INTEGER 0x0
   +0x1e8 pShimData        : (null) 
   +0x1ec AppCompatInfo    : (null) 
   +0x1f0 CSDVersion       : _UNICODE_STRING "Service Pack 1"
   +0x1f8 ActivationContextData : 0x00040000 _ACTIVATION_CONTEXT_DATA
   +0x1fc ProcessAssemblyStorageMap : (null) 
   +0x200 SystemDefaultActivationContextData : 0x00030000 _ACTIVATION_CONTEXT_DATA
   +0x204 SystemAssemblyStorageMap : (null) 
   +0x208 MinimumStackCommit : 0
   +0x20c FlsCallback      : 0x0033daa0 _FLS_CALLBACK_INFO
   +0x210 FlsListHead      : _LIST_ENTRY [ 0x347828 - 0x3cb680 ]
   +0x218 FlsBitmap        : 0x77c77270 Void
   +0x21c FlsBitmapBits    : [4] 7
   +0x22c FlsHighIndex     : 2
   +0x230 WerRegistrationData : (null) 
   +0x234 WerShipAssertPtr : (null) 
   +0x238 pContextData     : 0x00050000 Void
   +0x23c pImageHeaderHash : (null) 
   +0x240 TracingFlags     : 0
   +0x240 HeapTracingEnabled : 0y0
   +0x240 CritSecTracingEnabled : 0y0
   +0x240 SpareTracingBits : 0y000000000000000000000000000000 (0)

kd> $ Now lets use dd to see what this address contains


kd> dd 0x77c77500  
77c77500  00330000 00010000 00250000 00d10000
77c77510  00000000 00000000 00000000 00000000
77c77520  00000000 00000000 00000000 00000000
77c77530  00000000 00000000 00000000 00000000
77c77540  00000000 77c77340 77c7ab08 77c77220
77c77550  00000000 00000004 00000000 00000000
77c77560  77c77220 003e3198 00000000 00000000
77c77570  00000000 00000000 00000000 00000000

The Default Process heap pointer is always the first one in this list. Since most applications work with the default heap, we will focus our attention on that.

The _HEAP structure in Windows is used to maintain a heap. So lets typecast this address to it.

kd> dt _HEAP 0x00330000
ntdll!_HEAP
   +0x000 Entry            : _HEAP_ENTRY
   +0x008 SegmentSignature : 0xffeeffee
   +0x00c SegmentFlags     : 0
   +0x010 SegmentListEntry : _LIST_ENTRY [ 0x3300a8 - 0x3300a8 ]
   +0x018 Heap             : 0x00330000 _HEAP
   +0x01c BaseAddress      : 0x00330000 Void
   +0x020 NumberOfPages    : 0x100
   +0x024 FirstEntry       : 0x00330588 _HEAP_ENTRY
   +0x028 LastValidEntry   : 0x00430000 _HEAP_ENTRY
   +0x02c NumberOfUnCommittedPages : 0x47
   +0x030 NumberOfUnCommittedRanges : 1
   +0x034 SegmentAllocatorBackTraceIndex : 0
   +0x036 Reserved         : 0
   +0x038 UCRSegmentList   : _LIST_ENTRY [ 0x3e8ff0 - 0x3e8ff0 ]
   +0x040 Flags            : 2
   +0x044 ForceFlags       : 0
   +0x048 CompatibilityFlags : 0
   +0x04c EncodeFlagMask   : 0x100000
   +0x050 Encoding         : _HEAP_ENTRY
   +0x058 PointerKey       : 0x37d910ba
   +0x05c Interceptor      : 0
   +0x060 VirtualMemoryThreshold : 0xfe00
   +0x064 Signature        : 0xeeffeeff
   +0x068 SegmentReserve   : 0x100000
   +0x06c SegmentCommit    : 0x2000
   +0x070 DeCommitFreeBlockThreshold : 0x800
   +0x074 DeCommitTotalFreeThreshold : 0x2000
   +0x078 TotalFreeSize    : 0xac0
   +0x07c MaximumAllocationSize : 0x7ffdefff
   +0x080 ProcessHeapsListIndex : 1
   +0x082 HeaderValidateLength : 0x138
   +0x084 HeaderValidateCopy : (null) 
   +0x088 NextAvailableTagIndex : 0
   +0x08a MaximumTagIndex  : 0
   +0x08c TagEntries       : (null) 
   +0x090 UCRList          : _LIST_ENTRY [ 0x3e8fe8 - 0x3e8fe8 ]
   +0x098 AlignRound       : 0xf
   +0x09c AlignMask        : 0xfffffff8
   +0x0a0 VirtualAllocdBlocks : _LIST_ENTRY [ 0x3300a0 - 0x3300a0 ]
   +0x0a8 SegmentList      : _LIST_ENTRY [ 0x330010 - 0x330010 ]
   +0x0b0 AllocatorBackTraceIndex : 0
   +0x0b4 NonDedicatedListLength : 0
   +0x0b8 BlocksIndex      : 0x00330150 Void
   +0x0bc UCRIndex         : 0x00330590 Void
   +0x0c0 PseudoTagEntries : (null) 
   +0x0c4 FreeLists        : _LIST_ENTRY [ 0x3db6a8 - 0x3de378 ]
   +0x0cc LockVariable     : 0x00330138 _HEAP_LOCK
   +0x0d0 CommitRoutine    : 0x37d910ba     long  +37d910ba
   +0x0d4 FrontEndHeap     : 0x00336548 Void
   +0x0d8 FrontHeapLockCount : 0
   +0x0da FrontEndHeapType : 0x2 ''
   +0x0dc Counters         : _HEAP_COUNTERS
   +0x130 TuningParameters : _HEAP_TUNING_PARAMETERS

Note: The _HEAP structure might be different for different versions of Windows.

A point to note here is that inside the _PEB there are two fields:

kd> dt _PEB ProcessH*
ntdll!_PEB
   +0x018 ProcessHeap : Ptr32 Void
   +0x090 ProcessHeaps : Ptr32 Ptr32 Void

The ProcessHeaps is an array of pointers to the various heaps for the process. The ProcessHeap on the other hand will always store the address of the default heap of the process.

To browse all heaps in this process, we can use the WinDbg .for command, which is described here.

kd> .for (r $t0 = 0; @$t0 < 0x4; r $t0 = @$t0 + 1){dt _HEAP poi(0x77c77500 + ((@$t0)*4)) - y SegmentSignature}
ntdll!_HEAP
   +0x008 SegmentSignature : 0xffeeffee
ntdll!_HEAP
   +0x008 SegmentSignature : 0xffeeffee
ntdll!_HEAP
   +0x008 SegmentSignature : 0xffeeffee
ntdll!_HEAP
   +0x008 SegmentSignature : 0xffeeffee

We will revisit heaps again for further elaborate discussions and even walking the heaps when we explore heap corruption scenarios.

Wednesday, 17 September 2014

WinDbg : !handle Command

WinDbg : !handle Command extension 


Handles are used everywhere in Windows. A handle in Windows is an opaque pointer. Almost every Windows API uses a handle as a reference to the internal object. WinDbg has the !handle extension command to help us find more information regarding handles.

Here is the output of !handle when run on a user mode crash dump. There are differences in the output of !handle when executed via user mode and via kernel mode, we will soon see these differences.


0:000> !handle


Handle 0000000000000004
  Type         Directory
Handle 0000000000000008
  Type         File
Handle 000000000000000c
  Type         Key
Handle 0000000000000010
  Type         Event
Handle 0000000000000014
  Type         ALPC Port
Handle 0000000000000024
  Type         Key
Handle 0000000000000030
  Type         WaitCompletionPacket
Handle 0000000000000034
  Type         IoCompletion
Handle 0000000000000038
  Type         TpWorkerFactory
Handle 000000000000003c
  Type         IRTimer
Handle 0000000000000040
  Type         WaitCompletionPacket
Handle 0000000000000044
  Type         IRTimer
Handle 0000000000000048
  Type         WaitCompletionPacket
Handle 000000000000004c
  Type        
<Output Snipped due to space constraints>
Handle 0000000000000bf8
  Type         Thread
377 Handles
Type                     Count
None                     26
Event                   74
Section                 5
File                     7
Directory               2
Mutant                   115
Semaphore               17
Key                     104
Thread                   11
IoCompletion             2
TpWorkerFactory         1
ALPC Port               9
WaitCompletionPacket     4


The handle command takes a few flags. Here is the output for handle 10 for each of the flags, 1, 2, 4, 8 and f(all flags enabled)

0:000> !handle 10 1
Handle 0000000000000010
  Type          Event

0:000> !handle 10 2
Handle 0000000000000010
  Attributes   0
  GrantedAccess 0x1f0003:
         Delete,ReadControl,WriteDac,WriteOwner,Synch
         Delete,ReadControl,WriteDac,WriteOwner,Synch
  HandleCount   2
  PointerCount 65537

0:000> !handle 10 4
Handle 0000000000000010
  Name         <none>

0:000> !handle 10 8
Handle 0000000000000010
  No object specific information available

0:000> !handle 10 f
Handle 0000000000000010
  Type         Event
  Attributes   0
  GrantedAccess 0x1f0003:
         Delete,ReadControl,WriteDac,WriteOwner,Synch
         QueryState,ModifyState
  HandleCount   2
  PointerCount 65537
  Name         <none>
  Object specific information
    Event Type Auto Reset
    Event is Waiting

To get all information (0xf) about a particular type of handle (say for event objects) for all such events in the process (handle number 0 is all), we can use:

0:000> !handle 0 f event
Handle 0000000000000010
  Type         Event
  Attributes   0
  GrantedAccess 0x1f0003:
         Delete,ReadControl,WriteDac,WriteOwner,Synch
         QueryState,ModifyState
  HandleCount   2
  PointerCount 65537
  Name         <none>
  Object specific information
    Event Type Auto Reset
    Event is Waiting
Handle 000000000000002c
  Type         Event
  Attributes   0
  GrantedAccess 0x1f0003:
         Delete,ReadControl,WriteDac,WriteOwner,Synch
         QueryState,ModifyState
  HandleCount   2
  PointerCount 65538
  Name         <none>
  Object specific information
    Event Type Auto Reset
    Event is Waiting

<Output Snipped to save space>

Handle 0000000000000afc
  Type         Event
  Attributes   0
  GrantedAccess 0x100003:
         Synch
         QueryState,ModifyState
  HandleCount   2
  PointerCount 65512
  Name         <none>
  Object specific information
    Event Type Auto Reset
    Event is Waiting
74 handles of type Event


The output and capabilities of the !handle in kernel mode is different. It is more powerful since the debugger actually has access to the Kernel objects required to fetch such information.

kd> !handle

PROCESS 851d1348  SessionId: 1  Cid: 08b4    Peb: 7ffd4000  ParentCid: 0138
    DirBase: 1eed3420  ObjectTable: 93d06750  HandleCount:  71.
    Image: NotMyfault.exe

Handle table at 93d06750 with 71 entries in use

0004: Object: 8b265108  GrantedAccess: 00000003 Entry: 93d26008
Object: 8b265108  Type: (839b7e90) Directory
    ObjectHeader: 8b2650f0 (new version)
        HandleCount: 29  PointerCount: 67
        Directory Object: 874010e8  Name: KnownDlls

        Hash Address  Type          Name
        ---- -------  ----          ----
         00  875dd678 Section       gdi32.dll
             8b2569f0 Section       kernelbase.dll
             87439a48 Section       IMAGEHLP.dll
         02  875fec88 Section       NORMALIZ.dll
         03  8d82ec58 Section       ole32.dll
             8847b3b8 Section       URLMON.dll
         04  874ff820 Section       USP10.dll
         05  8b252458 Section       DEVOBJ.dll
         06  8ca6d948 Section       SHELL32.dll
             8b250af0 Section       CFGMGR32.dll
             875c71a8 Section       WLDAP32.dll
         09  874e84d0 Section       user32.dll
         14  875de3f8 Section       MSASN1.dll
         16  875edf98 SymbolicLink  KnownDllPath
             8b21ed58 Section       COMCTL32.dll
         17  87580268 Section       CRYPT32.dll
             8b24bba8 Section       PSAPI.DLL
         18  885ffb68 Section       advapi32.dll
             87537178 Section       OLEAUT32.dll
         19  8b3f8478 Section       SHLWAPI.dll
             875d3b50 Section       IERTUTIL.dll
             8755ba30 Section       ntdll.dll
         20  8755b780 Section       WS2_32.dll
         21  8ca6d758 Section       LPK.dll
         22  874d3850 Section       sechost.dll
         23  8745c768 Section       COMDLG32.dll
         24  8ca63f10 Section       difxapi.dll
         25  8758b888 Section       Setupapi.dll
         26  874cc3c8 Section       MSCTF.dll
             8b3e3338 Section       WININET.dll
         27  875d8b48 Section       WINTRUST.dll
             875cdd30 Section       IMM32.dll
         28  8ca533c8 Section       MSVCRT.dll
         31  874d33c8 Section       rpcrt4.dll
             875358b0 Section       clbcatq.dll
         32  8ca593e0 Section       kernel32.dll
         35  875fedf0 Section       NSI.dll

0008: Object: 851d11c0  GrantedAccess: 00100020 Entry: 93d26010
Object: 851d11c0  Type: (83a287a8) File
    ObjectHeader: 851d11a8 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \Users\Win7SP1x86-Debug\Desktop\Tools\Notmyfault\exe\Release {HarddiskVolume2}

000c: Object: 850bd518  GrantedAccess: 00100020 Entry: 93d26018
Object: 850bd518  Type: (83a287a8) File
    ObjectHeader: 850bd500 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.17514_none_41e6975e2bd6f2b2 {HarddiskVolume2}

0010: Object: 93cbdd48  GrantedAccess: 00020019 Entry: 93d26020
Object: 93cbdd48  Type: (83a2d388) Key
    ObjectHeader: 93cbdd30 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \REGISTRY\MACHINE\SYSTEM\CONTROLSET001\CONTROL\NLS\SORTING\VERSIONS

0014: Object: 851d19e8  GrantedAccess: 001f0001 Entry: 93d26028
Object: 851d19e8  Type: (83a298f0) ALPC Port
    ObjectHeader: 851d19d0 (new version)
        HandleCount: 1  PointerCount: 3

0018: Object: 93cf3ec8  GrantedAccess: 00000001 Entry: 93d26030
Object: 93cf3ec8  Type: (83a2d388) Key
    ObjectHeader: 93cf3eb0 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \REGISTRY\MACHINE\SYSTEM\CONTROLSET001\CONTROL\SESSION MANAGER

001c: Object: 850bd620  GrantedAccess: 00000804 Entry: 93d26038
Object: 850bd620  Type: (83a43f78) EtwRegistration
    ObjectHeader: 850bd608 (new version)
        HandleCount: 1  PointerCount: 1

0020: Object: 851cec08  GrantedAccess: 001f0003 (Protected) Entry: 93d26040
Object: 851cec08  Type: (83a33420) Event
    ObjectHeader: 851cebf0 (new version)
        HandleCount: 1  PointerCount: 2

0024: Object: 84ccffa8  GrantedAccess: 000f037f Entry: 93d26048
Object: 84ccffa8  Type: (83a28de8) WindowStation
    ObjectHeader: 84ccff90 (new version)
        HandleCount: 13  PointerCount: 24
        Directory Object: 95e31b38  Name: WinSta0

0028: Object: 84cd1d18  GrantedAccess: 000f01ff Entry: 93d26050
Object: 84cd1d18  Type: (83a28d20) Desktop
    ObjectHeader: 84cd1d00 (new version)
        HandleCount: 8  PointerCount: 476
        Directory Object: 00000000  Name: Default

002c: Object: 84ccffa8  GrantedAccess: 000f037f Entry: 93d26058
Object: 84ccffa8  Type: (83a28de8) WindowStation
    ObjectHeader: 84ccff90 (new version)
        HandleCount: 13  PointerCount: 24
        Directory Object: 95e31b38  Name: WinSta0

0030: Object: 90dbab80  GrantedAccess: 000f003f Entry: 93d26060
Object: 90dbab80  Type: (83a2d388) Key
    ObjectHeader: 90dbab68 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \REGISTRY\MACHINE

0034: Object: 851c79f8  GrantedAccess: 00000804 Entry: 93d26068
Object: 851c79f8  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851c79e0 (new version)
        HandleCount: 1  PointerCount: 1

0038: Object: 851c7990  GrantedAccess: 00000804 Entry: 93d26070
Object: 851c7990  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851c7978 (new version)
        HandleCount: 1  PointerCount: 1

003c: Object: 851c7928  GrantedAccess: 00000804 Entry: 93d26078
Object: 851c7928  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851c7910 (new version)
        HandleCount: 1  PointerCount: 1

0040: Object: 851d3cd8  GrantedAccess: 00000804 Entry: 93d26080
Object: 851d3cd8  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851d3cc0 (new version)
        HandleCount: 1  PointerCount: 1

0044: Object: 851cf428  GrantedAccess: 00000804 Entry: 93d26088
Object: 851cf428  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cf410 (new version)
        HandleCount: 1  PointerCount: 1

0048: Object: 851cf490  GrantedAccess: 00000804 Entry: 93d26090
Object: 851cf490  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cf478 (new version)
        HandleCount: 1  PointerCount: 1

004c: Object: 851cf3c0  GrantedAccess: 00000804 Entry: 93d26098
Object: 851cf3c0  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cf3a8 (new version)
        HandleCount: 1  PointerCount: 1

0050: Object: 851cf358  GrantedAccess: 00000804 Entry: 93d260a0
Object: 851cf358  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cf340 (new version)
        HandleCount: 1  PointerCount: 1

0054: Object: 851cf2f0  GrantedAccess: 00000804 Entry: 93d260a8
Object: 851cf2f0  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cf2d8 (new version)
        HandleCount: 1  PointerCount: 1

0058: Object: 851cf288  GrantedAccess: 00000804 Entry: 93d260b0
Object: 851cf288  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cf270 (new version)
        HandleCount: 1  PointerCount: 1

005c: Object: 851cffd0  GrantedAccess: 00000804 Entry: 93d260b8
Object: 851cffd0  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cffb8 (new version)
        HandleCount: 1  PointerCount: 1

0060: Object: 851cff68  GrantedAccess: 00000804 Entry: 93d260c0
Object: 851cff68  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cff50 (new version)
        HandleCount: 1  PointerCount: 1

0064: Object: 851cff00  GrantedAccess: 00000804 Entry: 93d260c8
Object: 851cff00  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cfee8 (new version)
        HandleCount: 1  PointerCount: 1

0068: Object: 851cfea8  GrantedAccess: 001f0001 Entry: 93d260d0
Object: 851cfea8  Type: (83a25418) Mutant
    ObjectHeader: 851cfe90 (new version)
        HandleCount: 1  PointerCount: 1

006c: Object: 851d3b90  GrantedAccess: 001f0003 Entry: 93d260d8
Object: 851d3b90  Type: (83a33420) Event
    ObjectHeader: 851d3b78 (new version)
        HandleCount: 1  PointerCount: 1

0070: Object: 93d14c28  GrantedAccess: 00020019 Entry: 93d260e0
Object: 93d14c28  Type: (83a2d388) Key
    ObjectHeader: 93d14c10 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \REGISTRY\MACHINE\SYSTEM\CONTROLSET001\CONTROL\NLS\LOCALE

0074: Object: 93c85638  GrantedAccess: 00020019 Entry: 93d260e8
Object: 93c85638  Type: (83a2d388) Key
    ObjectHeader: 93c85620 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \REGISTRY\MACHINE\SYSTEM\CONTROLSET001\CONTROL\NLS\LOCALE\ALTERNATE SORTS

0078: Object: 8bbc5ab8  GrantedAccess: 00020019 Entry: 93d260f0
Object: 8bbc5ab8  Type: (83a2d388) Key
    ObjectHeader: 8bbc5aa0 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \REGISTRY\MACHINE\SYSTEM\CONTROLSET001\CONTROL\NLS\LANGUAGE GROUPS

007c: Object: 851cfe48  GrantedAccess: 00000804 Entry: 93d260f8
Object: 851cfe48  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851cfe30 (new version)
        HandleCount: 1  PointerCount: 1

0080: Object: 850be990  GrantedAccess: 00000804 Entry: 93d26100
Object: 850be990  Type: (83a43f78) EtwRegistration
    ObjectHeader: 850be978 (new version)
        HandleCount: 1  PointerCount: 1

0084: Object: 851d3038  GrantedAccess: 001f0001 Entry: 93d26108
Object: 851d3038  Type: (83a298f0) ALPC Port
    ObjectHeader: 851d3020 (new version)
        HandleCount: 1  PointerCount: 1

0088: Object: 8bb01590  GrantedAccess: 00000004 Entry: 93d26110
Object: 8bb01590  Type: (83a31b50) Section
    ObjectHeader: 8bb01578 (new version)
        HandleCount: 6  PointerCount: 6

008c: Object: 851d31b8  GrantedAccess: 00000804 Entry: 93d26118
Object: 851d31b8  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851d31a0 (new version)
        HandleCount: 1  PointerCount: 1

0090: Object: 851d3220  GrantedAccess: 00000804 Entry: 93d26120
Object: 851d3220  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851d3208 (new version)
        HandleCount: 1  PointerCount: 1

0094: Object: 849c05a8  GrantedAccess: 00120089 Entry: 93d26128
Object: 849c05a8  Type: (83a287a8) File
    ObjectHeader: 849c0590 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \Windows\System32\en-US\user32.dll.mui {HarddiskVolume2}

0098: Object: 851d9098  GrantedAccess: 001f0003 Entry: 93d26130
Object: 851d9098  Type: (83a33420) Event
    ObjectHeader: 851d9080 (new version)
        HandleCount: 1  PointerCount: 1

009c: Object: 95e2ed60  GrantedAccess: 0000000f Entry: 93d26138
Object: 95e2ed60  Type: (839b7e90) Directory
    ObjectHeader: 95e2ed48 (new version)
        HandleCount: 8  PointerCount: 43
        Directory Object: 95e2d298  Name: BaseNamedObjects

        Hash Address  Type          Name
        ---- -------  ----          ----
         00  95e2fc68 SymbolicLink  Local
             8506b5d8 Mutant        ZonesCacheCounterMutex
         01  850d3ab8 Mutant        ZonesLockedCacheCounterMutex
             8515d8b0 Mutant        AccessibilitySoundAgentRunning
             84f40ff0 Event         ThemesStartEvent
         02  95e2c428 Directory     Restricted
         03  84cd0298 Event         ScNetDrvMsg
         04  8e5479f0 Section       windows_shell_global_counters
         07  850f9580 Event         ShellDesktopSwitchEvent
         09  8513eb80 Event         MSCTF.AsmCacheReady.Default1
             84f53ea8 Event         ThemeLoadedEvent
         10  8513ec20 Event         MSCTF.CtfActivated.Default1
             90db9c98 Section       C:*ProgramData*Microsoft*Windows*Caches*{7CD55808-3D38-4DD5-90C9-62F0E6EE60D4}.2.ver0x0000000000000001.db
             90d58340 Section       C:*ProgramData*Microsoft*Windows*Caches*{6AF0698E-D558-4F6E-9B3C-3716689AF493}.2.ver0x0000000000000007.db
         12  85145948 Mutant        MSCTF.CtfMonitorInstMutexDefault1
         13  84f96470 Event         ShellReadyEvent
             90d5eaa0 Section       C:*ProgramData*Microsoft*Windows*Caches*cversions.2.ro
         14  90d59c58 Section       C:*ProgramData*Microsoft*Windows*Caches*{DDF571F2-BE98-426D-8288-1A9A39C3FDA2}.2.ver0x0000000000000002.db
         16  95e2d158 SymbolicLink  Global
         19  84d0e4d8 Event         WinSta0_DesktopSwitch
         20  848c5b18 Mutant        ZoneAttributeCacheCounterMutex
             84ccd208 Event         EventShutDownCSRSS
         21  8ba99b88 Section       windows_ie_global_counters
         22  84d79e10 Mutant        ALTTAB_RUNNING_MUTEX
         26  85100380 Event         MSCTF.CtfMonitorInitialized.Default1
         28  851450a8 Mutant        CicLoadWinStaWinSta0
             85182080 Mutant        _SHuassist.mtx
         29  849544e8 Mutant        ZonesCounterMutex
             85144700 Mutant        MSCTF.Asm.MutexDefault1
         30  95e1f100 SymbolicLink  Session
         31  8e551830 Section       UrlZonesSM_Win7SP1x86-Debug
         33  85141528 ALPC Port     Dwm-49D1-ApiPort-1BF9
             8e5ae7c8 Section       CTF.AsmListCache.FMPDefault1
         35  8513ebd0 Event         MSCTF.CtfDeactivated.Default1

00a0: Object: 851d1b90  GrantedAccess: 001f0003 Entry: 93d26140
Object: 851d1b90  Type: (83a33420) Event
    ObjectHeader: 851d1b78 (new version)
        HandleCount: 1  PointerCount: 1

00a4: Object: 851db930  GrantedAccess: 001f0003 Entry: 93d26148
Object: 851db930  Type: (83a33420) Event
    ObjectHeader: 851db918 (new version)
        HandleCount: 1  PointerCount: 1

00a8: Object: 851d2530  GrantedAccess: 00000804 Entry: 93d26150
Object: 851d2530  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851d2518 (new version)
        HandleCount: 1  PointerCount: 1

00ac: Object: 851d97d8  GrantedAccess: 00000804 Entry: 93d26158
Object: 851d97d8  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851d97c0 (new version)
        HandleCount: 1  PointerCount: 1

00b0: Object: 851c7df8  GrantedAccess: 001f0003 Entry: 93d26160
Object: 851c7df8  Type: (83a33420) Event
    ObjectHeader: 851c7de0 (new version)
        HandleCount: 1  PointerCount: 1

00b4: Object: 851d8d48  GrantedAccess: 001fffff Entry: 93d26168
Object: 851d8d48  Type: (839b78b0) Thread
    ObjectHeader: 851d8d30 (new version)
        HandleCount: 2  PointerCount: 4

00b8: Object: 851d96a0  GrantedAccess: 001f0001 Entry: 93d26170
Object: 851d96a0  Type: (83a298f0) ALPC Port
    ObjectHeader: 851d9688 (new version)
        HandleCount: 1  PointerCount: 1

00bc: Object: 852540f0  GrantedAccess: 0012019f Entry: 93d26178
Object: 852540f0  Type: (83a287a8) File
    ObjectHeader: 852540d8 (new version)
        HandleCount: 1  PointerCount: 3

00c0: Object: 8507f098  GrantedAccess: 001f0003 Entry: 93d26180
Object: 8507f098  Type: (83a28870) IoCompletion
    ObjectHeader: 8507f080 (new version)
        HandleCount: 1  PointerCount: 2

00c4: Object: 85255ee0  GrantedAccess: 000f00ff Entry: 93d26188
Object: 85255ee0  Type: (83a28c58) TpWorkerFactory
    ObjectHeader: 85255ec8 (new version)
        HandleCount: 1  PointerCount: 1

00c8: Object: 94078d00  GrantedAccess: 000f0003 Entry: 93d26190
Object: 94078d00  Type: (83a28eb0) KeyedEvent
    ObjectHeader: 94078ce8 (new version)
        HandleCount: 1  PointerCount: 1

00cc: Object: 850bfe38  GrantedAccess: 00100002 Entry: 93d26198
Object: 850bfe38  Type: (83a26350) Timer
    ObjectHeader: 850bfe20 (new version)
        HandleCount: 1  PointerCount: 2

00d0: Object: 850bfd70  GrantedAccess: 001f0003 Entry: 93d261a0
Object: 850bfd70  Type: (83a26350) Timer
    ObjectHeader: 850bfd58 (new version)
        HandleCount: 1  PointerCount: 2

00d4: Object: 850bfa88  GrantedAccess: 001fffff Entry: 93d261a8
Object: 850bfa88  Type: (839b78b0) Thread
    ObjectHeader: 850bfa70 (new version)
        HandleCount: 2  PointerCount: 3

00d8: Object: 850bfa88  GrantedAccess: 001fffff Entry: 93d261b0
Object: 850bfa88  Type: (839b78b0) Thread
    ObjectHeader: 850bfa70 (new version)
        HandleCount: 2  PointerCount: 3

00dc: Object: 851b1240  GrantedAccess: 001f0003 Entry: 93d261b8
Object: 851b1240  Type: (83a28870) IoCompletion
    ObjectHeader: 851b1228 (new version)
        HandleCount: 1  PointerCount: 2

00e0: Object: 850bf9e0  GrantedAccess: 000f00ff Entry: 93d261c0
Object: 850bf9e0  Type: (83a28c58) TpWorkerFactory
    ObjectHeader: 850bf9c8 (new version)
        HandleCount: 1  PointerCount: 1

00e4: Object: 850bf918  GrantedAccess: 00100002 Entry: 93d261c8
Object: 850bf918  Type: (83a26350) Timer
    ObjectHeader: 850bf900 (new version)
        HandleCount: 1  PointerCount: 2

00e8: Object: 850bf868  GrantedAccess: 00120089 Entry: 93d261d0
Object: 850bf868  Type: (83a287a8) File
    ObjectHeader: 850bf850 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \Windows\Fonts\StaticCache.dat {HarddiskVolume2}

00ec: Object: 90d6b3b0  GrantedAccess: 000f0005 Entry: 93d261d8
Object: 90d6b3b0  Type: (83a31b50) Section
    ObjectHeader: 90d6b398 (new version)
        HandleCount: 1  PointerCount: 1

00f0: Object: 84d4c968  GrantedAccess: 001f0003 Entry: 93d261e0
Object: 84d4c968  Type: (83a33420) Event
    ObjectHeader: 84d4c950 (new version)
        HandleCount: 1  PointerCount: 1

00f4: Object: 851c8e88  GrantedAccess: 00000804 Entry: 93d261e8
Object: 851c8e88  Type: (83a43f78) EtwRegistration
    ObjectHeader: 851c8e70 (new version)
        HandleCount: 1  PointerCount: 1

00f8: Object: 84d50c78  GrantedAccess: 001f0003 Entry: 93d261f0
Object: 84d50c78  Type: (83a33420) Event
    ObjectHeader: 84d50c60 (new version)
        HandleCount: 1  PointerCount: 1

00fc: Object: 849fa258  GrantedAccess: 001f0003 Entry: 93d261f8
Object: 849fa258  Type: (83a33420) Event
    ObjectHeader: 849fa240 (new version)
        HandleCount: 1  PointerCount: 1

0100: Object: 84d70360  GrantedAccess: 001f0003 Entry: 93d26200
Object: 84d70360  Type: (83a33420) Event
    ObjectHeader: 84d70348 (new version)
        HandleCount: 1  PointerCount: 1

0104: Object: 851cc628  GrantedAccess: 001f0003 Entry: 93d26208
Object: 851cc628  Type: (83a33420) Event
    ObjectHeader: 851cc610 (new version)
        HandleCount: 1  PointerCount: 1

0108: Object: 8523bd38  GrantedAccess: 001f0003 Entry: 93d26210
Object: 8523bd38  Type: (83a33420) Event
    ObjectHeader: 8523bd20 (new version)
        HandleCount: 1  PointerCount: 1

010c: Object: 83a4c0f8  GrantedAccess: 001f0003 Entry: 93d26218
Object: 83a4c0f8  Type: (83a33420) Event
    ObjectHeader: 83a4c0e0 (new version)
        HandleCount: 1  PointerCount: 1

0110: Object: 851cf6d0  GrantedAccess: 001f0003 Entry: 93d26220
Object: 851cf6d0  Type: (83a33420) Event
    ObjectHeader: 851cf6b8 (new version)
        HandleCount: 1  PointerCount: 1

0114: Object: 85255318  GrantedAccess: 00100001 Entry: 93d26228
Object: 85255318  Type: (83a287a8) File
    ObjectHeader: 85255300 (new version)
        HandleCount: 1  PointerCount: 1

0118: Object: 93d12608  GrantedAccess: 000f003f Entry: 93d26230
Object: 93d12608  Type: (83a2d388) Key
    ObjectHeader: 93d125f0 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \REGISTRY\USER\S-1-5-21-4213196723-1351097745-788781942-1001

011c: Object: 8e5479f0  GrantedAccess: 00000006 Entry: 93d26238
Object: 8e5479f0  Type: (83a31b50) Section
    ObjectHeader: 8e5479d8 (new version)
        HandleCount: 3  PointerCount: 4
        Directory Object: 95e2ed60  Name: windows_shell_global_counters

As we see the object information and name is given out with a horde of other useful stuff. 

Thus we see that to debug handle leaks etc, it is perhaps more useful to have a full kernel dump instead of just a process dump.

For example, assuming that we want to find out all open file handles for the process 'system'. We can do that with these steps below:

kd> !process 0 0 system
PROCESS 839afbf8  SessionId: none  Cid: 0004    Peb: 00000000  ParentCid: 0000
    DirBase: 00185000  ObjectTable: 87401ca0  HandleCount: 463.
    Image: System

kd> .process /p /r 839afbf8
Implicit process is now 839afbf8
Loading User Symbols

kd> !handle 0 7 839afbf8 File

Searching for handles of type File

PROCESS 839afbf8  SessionId: none  Cid: 0004    Peb: 00000000  ParentCid: 0000
    DirBase: 00185000  ObjectTable: 87401ca0  HandleCount: 463.
    Image: System

Kernel handle table at 87401ca0 with 463 entries in use

0054: Object: 849133f0  GrantedAccess: 00120116 Entry: 874030a8
Object: 849133f0  Type: (83a287a8) File
    ObjectHeader: 849133d8 (new version)
        HandleCount: 1  PointerCount: 1

0060: Object: 84871940  GrantedAccess: 00100001 Entry: 874030c0
Object: 84871940  Type: (83a287a8) File
    ObjectHeader: 84871928 (new version)
        HandleCount: 1  PointerCount: 1

0064: Object: 84dc24d0  GrantedAccess: 0012008b Entry: 874030c8
Object: 84dc24d0  Type: (83a287a8) File
    ObjectHeader: 84dc24b8 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \Windows\System32\wdi\LogFiles\WdiContextLog.etl.003 {HarddiskVolume2}

0070: Object: 84c0ef80  GrantedAccess: 0012019f Entry: 874030e0
Object: 84c0ef80  Type: (83a287a8) File
    ObjectHeader: 84c0ef68 (new version)
        HandleCount: 1  PointerCount: 2

0088: Object: 84c42788  GrantedAccess: 0012019f Entry: 87403110
Object: 84c42788  Type: (83a287a8) File
    ObjectHeader: 84c42770 (new version)
        HandleCount: 1  PointerCount: 2
        Directory Object: 00000000  Name: \$Extend\$RmMetadata\$TxfLog\$TxfLog.blf {HarddiskVolume1}

008c: Object: 84c30268  GrantedAccess: 0012019f (Inherit) Entry: 87403118
Object: 84c30268  Type: (83a287a8) File
    ObjectHeader: 84c30250 (new version)
        HandleCount: 1  PointerCount: 2
        Directory Object: 00000000  Name: TxfLog {clfs}

0090: Object: 84c30310  GrantedAccess: 0012019f Entry: 87403120
Object: 84c30310  Type: (83a287a8) File
    ObjectHeader: 84c302f8 (new version)
        HandleCount: 1  PointerCount: 2
        Directory Object: 00000000  Name: \$Extend\$RmMetadata\$TxfLog\$TxfLogContainer00000000000000000001 {HarddiskVolume1}

<Output snipped>


0140: Object: 847e7e98  GrantedAccess: 00020003 (Protected) Entry: 87403280
Object: 847e7e98  Type: (83a287a8) File
    ObjectHeader: 847e7e80 (new version)
        HandleCount: 1  PointerCount: 1
        Directory Object: 00000000  Name: \Windows\System32\config\SOFTWARE {HarddiskVolume2}


0728: free handle, Entry address 87403e50, Next Entry 00000744
0734: free handle, Entry address 87403e68, Next Entry 0000083c
0ffc: free handle, Entry address 91427ff8, Next Entry 00000000


<Output snipped>

I have deliberately snipped the output since it is very long. As we see, we have open handles for regular files, registry, transaction logs and what not.

We will revisit this topic again in a future post to see how this information can be extended to actually find which device is an IO bound to.


We can use the !object command to get even more useful information of the handle in question. Here is a link to a blog post which describes how the !object command works.

Tuesday, 9 September 2014

WinDbg : Trying To Find The Entry Point Of A Binary

WinDbg : Trying To Find The Entry Point Of A Binary


Alright. I did a lot of talking about the WinDbg commands so far. It is time to walk the talk, and put these commands to some practical usage. Lets start with trying to find the entry point of a binary in memory loaded in memory. The list of commands which would be used in this article are:


  1. !process
  2. ,process
  3. !dh
  4. u
In this example, we are connected to the debugee as a kernel debugger. The commands might slightly vary if run through user mode. I will try to give such specific examples as and where necessary.

For this example we will use lsass.exe, you can use any other process you like.


kd> !process 0 0 lsass.exe

PROCESS 84cdc860  SessionId: 0  Cid: 0208    Peb: 7ffda000  ParentCid: 018c
    DirBase: 1eed30e0  ObjectTable: 95f00d18  HandleCount: 513.
    Image: lsass.exe

Now lets shift the debugger context to the process lsass.exe
kd> .process /p /r 84cdc860  
Implicit process is now 84cdc860
Loading User Symbols
............................................................

Lets run the !dh command on lsass.exe to see what all it contains.
kd> !dh lsass.exe

File Type: EXECUTABLE IMAGE
FILE HEADER VALUES
     14C machine (i386)
       4 number of sections
4A5BBF3E time date stamp Tue Jul 14 04:41:58 2009

       0 file pointer to symbol table
       0 number of symbols
      E0 size of optional header
     122 characteristics
            Executable
            App can handle >2gb addresses
            32 bit word machine

OPTIONAL HEADER VALUES
     10B magic #
    9.00 linker version
    4200 size of code
    1000 size of initialized data
       0 size of uninitialized data
    2F3D address of entry point
    1000 base of code
         ----- new -----
00d90000 image base
    1000 section alignment
     200 file alignment
       2 subsystem (Windows GUI)
    6.01 operating system version
    6.01 image version
    6.01 subsystem version
    9000 size of image
     600 size of headers
    6193 checksum
00040000 size of stack reserve
00006000 size of stack commit
00100000 size of heap reserve
00001000 size of heap commit
    8140  DLL characteristics
            Dynamic base
            NX compatible
            Terminal server aware
    1EC4 [      6B] address [size] of Export Directory
     4478 [     154] address [size] of Import Directory
     7000 [     700] address [size] of Resource Directory
          0 [         0] address [size] of Exception Directory
          0 [         0] address [size] of Security Directory
     8000 [     368] address [size] of Base Relocation Directory
     5080 [       38] address [size] of Debug Directory
          0 [         0] address [size] of Description Directory
          0 [         0] address [size] of Special Directory
          0 [         0] address [size] of Thread Storage Directory
    3F90 [        40] address [size] of Load Configuration Directory
      280 [      294] address [size] of Bound Import Directory
    1000 [     1AC] address [size] of Import Address Table Directory
         0 [          0] address [size] of Delay Import Directory
         0 [          0] address [size] of COR20 Header Directory
         0 [          0] address [size] of Reserved Directory


SECTION HEADER #1
   .text name
    40DE virtual size
    1000 virtual address
    4200 size of raw data
     600 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
60000020 flags
         Code
         (no align specified)
         Execute Read


Debug Directories(2)
Type       Size     Address  Pointer
cv           22        50bc     46bc Format: RSDS, guid, 2, lsass.pdb
(    10)       4        50b8     46b8

SECTION HEADER #2
   .data name
     3CC virtual size
    6000 virtual address
     400 size of raw data
    4800 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
C0000040 flags
         Initialized Data
         (no align specified)
         Read Write

SECTION HEADER #3
   .rsrc name
     700 virtual size
    7000 virtual address
     800 size of raw data
    4C00 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
40000040 flags
         Initialized Data
         (no align specified)
         Read Only

SECTION HEADER #4
  .reloc name
     368 virtual size
    8000 virtual address
     400 size of raw data
    5400 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
42000040 flags
         Initialized Data
         Discardable
         (no align specified)
         Read Only

The output shows us, among other useful information, the relative virtual address offset of the entry point to the binary (in bold above). For this binary instance, it is at hex 0x2F3D.So if we disassemble this offset from the starting of the image, we should be able to see the code for the Main function. Lets try that:

kd> u 2F3D+lsass.exe
lsass!mainCRTStartup:
00d92f3d e80dffffff      call    lsass!__security_init_cookie (00d92e4f)
00d92f42 6a10            push    10h
00d92f44 683030d900      push    offset lsass!SspiSrvClientCallback+0xc (00d93030)
00d92f49 e84e030000      call    lsass!_SEH_prolog4 (00d9329c)
00d92f4e 33db            xor     ebx,ebx
00d92f50 895dfc          mov     dword ptr [ebp-4],ebx
00d92f53 64a118000000    mov     eax,dword ptr fs:[00000018h]
00d92f59 8b7004          mov     esi,dword ptr [eax+4]

As we see, it is indeed Main, or MainCRTStartup, which is the true entry point to the binary. Also worth noting, is the ImageBase field (again in bold above), that is the virtual address where lsass.exe is actually loaded. So in the unassemble command we could have given that address instead of the string lsass.exe to get the same output.

kd> u 2F3D+00d90000 
lsass!mainCRTStartup:
00d92f3d e80dffffff      call    lsass!__security_init_cookie (00d92e4f)
00d92f42 6a10            push    10h
00d92f44 683030d900      push    offset lsass!SspiSrvClientCallback+0xc (00d93030)
00d92f49 e84e030000      call    lsass!_SEH_prolog4 (00d9329c)
00d92f4e 33db            xor     ebx,ebx
00d92f50 895dfc          mov     dword ptr [ebp-4],ebx
00d92f53 64a118000000    mov     eax,dword ptr fs:[00000018h]
00d92f59 8b7004          mov     esi,dword ptr [eax+4]

While doing this exercise, it comes to ones mind, lsass is an executable, what if we run the commands for a DLL, would it give us similar outputs? Lets find out. For this example, I am going to use ntdll.dll which is usually mapped to every win32 proecss. You can, use the same commands for other DLLs as well.

kd> !dh ntdll.dll

File Type: DLL
FILE HEADER VALUES
     14C machine (i386)
       5 number of sections
4CE7B96E time date stamp Sat Nov 20 17:35:02 2010

       0 file pointer to symbol table
       0 number of symbols
      E0 size of optional header
    2102 characteristics
            Executable
            32 bit word machine
            DLL

OPTIONAL HEADER VALUES
     10B magic #
    9.00 linker version
   D5000 size of code
   63200 size of initialized data
       0 size of uninitialized data
       0 address of entry point
    1000 base of code
         ----- new -----
77ba0000 image base
    1000 section alignment
     200 file alignment
       3 subsystem (Windows CUI)
    6.01 operating system version
    6.01 image version
    6.01 subsystem version
  13C000 size of image
     400 size of headers
  1490D9 checksum
00040000 size of stack reserve
00001000 size of stack commit
00100000 size of heap reserve
00001000 size of heap commit
     140  DLL characteristics
            Dynamic base
            NX compatible
   36190 [    F018] address [size] of Export Directory
       0 [       0] address [size] of Import Directory
   E0000 [   560D8] address [size] of Resource Directory
       0 [       0] address [size] of Exception Directory
  137000 [    3928] address [size] of Security Directory
  137000 [    4C3C] address [size] of Base Relocation Directory
   D5D08 [      38] address [size] of Debug Directory
       0 [       0] address [size] of Description Directory
       0 [       0] address [size] of Special Directory
       0 [       0] address [size] of Thread Storage Directory
   1E080 [      40] address [size] of Load Configuration Directory
       0 [       0] address [size] of Bound Import Directory
       0 [       0] address [size] of Import Address Table Directory
       0 [       0] address [size] of Delay Import Directory
       0 [       0] address [size] of COR20 Header Directory
       0 [       0] address [size] of Reserved Directory


SECTION HEADER #1
   .text name
   D4D66 virtual size
    1000 virtual address
   D4E00 size of raw data
     400 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
60000020 flags
         Code
         (no align specified)
         Execute Read


Debug Directories(2)
Type       Size     Address  Pointer
Can't read debug dir

SECTION HEADER #2
      RT name
     1DC virtual size
   D6000 virtual address
     200 size of raw data
   D5200 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
60000020 flags
         Code
         (no align specified)
         Execute Read

SECTION HEADER #3
   .data name
    8064 virtual size
   D7000 virtual address
    6C00 size of raw data
   D5400 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
C0000040 flags
         Initialized Data
         (no align specified)
         Read Write

SECTION HEADER #4
   .rsrc name
   560D8 virtual size
   E0000 virtual address
   56200 size of raw data
   DC000 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
40000040 flags
         Initialized Data
         (no align specified)
         Read Only

SECTION HEADER #5
  .reloc name
    4C3C virtual size
  137000 virtual address
    4E00 size of raw data
  132200 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
42000040 flags
         Initialized Data
         Discardable
         (no align specified)
         Read Only

Just as we suspected, there is no entry point for a DLL, the address is zero. Also check, the File Type filed for a DLL, it says 'DLL'. where as for the executable it said 'EXECUTABLE IMAGE'.

Lets try this command for a driver (ntfs.sys), to see how that shows up:

1: kd> !dh ntfs.sys

File Type: EXECUTABLE IMAGE
FILE HEADER VALUES
    8664 machine (X64)
       8 number of sections
4A5BC14F time date stamp Tue Jul 14 04:50:47 2009

       0 file pointer to symbol table
       0 number of symbols
      F0 size of optional header
      22 characteristics
            Executable
            App can handle >2gb addresses

OPTIONAL HEADER VALUES
     20B magic #
    9.00 linker version
  157200 size of code
   46A00 size of initialized data
       0 size of uninitialized data
  17E06C address of entry point
    1000 base of code
         ----- new -----
fffff80002213000 image base
    1000 section alignment
     200 file alignment
       1 subsystem (Native)
    6.01 operating system version
    6.01 image version
    6.01 subsystem version
  1A3000 size of image
     400 size of headers
  195F88 checksum
0000000000040000 size of stack reserve
0000000000001000 size of stack commit
0000000000100000 size of heap reserve
0000000000001000 size of heap commit
       0  DLL characteristics
           0 [            0] address [size] of Export Directory
  18013C [          64] address [size] of Import Directory
   185000 [   1CFF0] address [size] of Resource Directory
     67000 [      9600] address [size] of Exception Directory
   193800 [      1C50] address [size] of Security Directory
   1A2000 [        134] address [size] of Base Relocation Directory
     45158 [          38] address [size] of Debug Directory
            0 [            0] address [size] of Description Directory
            0 [            0] address [size] of Special Directory
            0 [            0] address [size] of Thread Storage Directory
            0 [            0] address [size] of Load Configuration Directory
            0 [            0] address [size] of Bound Import Directory
     46000 [        F68] address [size] of Import Address Table Directory
            0 [            0] address [size] of Delay Import Directory
            0 [            0] address [size] of COR20 Header Directory
            0 [            0] address [size] of Reserved Directory


SECTION HEADER #1
   .text name
   441B5 virtual size
    1000 virtual address
   44200 size of raw data
     400 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
68000020 flags
         Code
         Not Paged
         (no align specified)
         Execute Read


Debug Directories(2)
Type       Size     Address  Pointer
cv           21       45194    44594 Format: RSDS, guid, 2, ntfs.pdb
(    10)       4       45190    44590

SECTION HEADER #2
  .rdata name
   14BB4 virtual size
   46000 virtual address
   14C00 size of raw data
   44600 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
48000040 flags
         Initialized Data
         Not Paged
         (no align specified)
         Read Only

SECTION HEADER #3
   .data name
    B4E0 virtual size
   5B000 virtual address
     E00 size of raw data
   59200 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
C8000040 flags
         Initialized Data
         Not Paged
         (no align specified)
         Read Write

SECTION HEADER #4
  .pdata name
    9600 virtual size
   67000 virtual address
    9600 size of raw data
   5A000 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
48000040 flags
         Initialized Data
         Not Paged
         (no align specified)
         Read Only

SECTION HEADER #5
    PAGE name
  10CD8A virtual size
   71000 virtual address
  10CE00 size of raw data
   63600 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
60000020 flags
         Code
         (no align specified)
         Execute Read

SECTION HEADER #6
    INIT name
    603C virtual size
  17E000 virtual address
    6200 size of raw data
  170400 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
E2000020 flags
         Code
         Discardable
         (no align specified)
         Execute Read Write

SECTION HEADER #7
   .rsrc name
   1CFF0 virtual size
  185000 virtual address
   1D000 size of raw data
  176600 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
40000040 flags
         Initialized Data
         (no align specified)
         Read Only

SECTION HEADER #8
  .reloc name
     134 virtual size
  1A2000 virtual address
     200 size of raw data
  193600 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
42000040 flags
         Initialized Data
         Discardable
         (no align specified)

         Read Only

Field Name
Executable (lsass.exe, ntfs.sys)
DLL (ntdll.dll)
File Type
EXECUTABLE_IMAGE
DLL
Address Of Entry Point
A valid Offset inside the image from the Image Base
Zero
Image Base
A valid virtual address
A valid virtual address

Note: The output for the driver section is taken from a x64 OS, hence the extra bytes in the address ranges.