Jamal Darbo// Security PortfolioPortfolio v1
Portfolio Blog
Back to blog

Windows Security

Windows internals for security analysts

Follow a PowerShell process through its handles, access tokens, file operations and memory with NtObjectManager and Sysinternals.

A translucent blue Windows emblem revealing illuminated internal layers and a golden core

A process starts, a file changes, and a connection appears in the logs. During a security investigation, these observations raise further questions: what caused the activity, which security context was involved, and why did Windows allow or deny the operation?

In my earlier article on using Process Monitor, I explored how to observe activity beneath the application interface. Here, I want to connect those observations to the Windows mechanisms behind them.

I also want to credit James Forshaw, author of Windows Security Internals and creator of the NtObjectManager module. His book explores Windows authentication, authorization and auditing through practical PowerShell examples. The module makes many of these mechanisms directly accessible for inspection and experimentation.

We will follow a single PowerShell process through several perspectives: its handles, access token, file operations and memory allocations. Using NtObjectManager alongside Process Explorer, Process Monitor and VMMap, we can connect what Windows is doing to the evidence available during an investigation.

The NtObjectManager commands below were checked with version 2.0.1. They assume the module is already installed. The examples use a Windows lab environment with the relevant Sysinternals tools available.

A process and its resources

A process provides the resources that code needs to run. Its virtual address space, open handles and security context belong to that execution environment. Threads perform the actual execution and receive CPU time from the scheduler. Several threads within one process can share its memory and resources.

Open PowerShell and display its process ID:

$PID
PowerShell displaying the current process ID, 11740

In this case it is 11740. Now find that PID in Process Explorer (as administrator). Depending on the PowerShell edition, the executable will usually be powershell.exe or pwsh.exe.

Process Explorer showing powershell.exe with PID 11740
PowerShell.exe with PID 11740.

Look at the process properties, its command line and its location in the process tree. Then enable the lower pane. In handle mode, it shows references the process holds to Windows objects:

Process Explorer handle view for powershell.exe
Some powershell.exe handles.

In DLL mode, it shows loaded DLLs and memory-mapped files:

Process Explorer DLL view for powershell.exe
Some powershell.exe DLLs.

This already gives us several different views of the same process. Its name identifies the executable. Its command line helps explain how it started. Its handles show resources it currently references. Its loaded modules help explain which code is available inside its address space.

Each observation answers a different question.

User mode and kernel mode

The process also operates across a distinction that is easy to confuse with administrator privileges: user mode and kernel mode.

PowerShell executes application code in user mode. When it needs an operating system service, an API call can eventually reach a native interface in ntdll.dll and cross into kernel mode through a system call (syscall). Windows then processes the request using the relevant validation and access checks.

Running PowerShell as administrator changes its security context. It does not turn the application into arbitrary kernel-mode code. Similarly, kernel32.dll is a user-mode library despite its name. Core Windows kernel and executive functionality resides in ntoskrnl.exe.

This is important when assessing a security claim. Administrator access, High integrity and kernel execution describe different properties. We need to establish which one is actually involved.

Diagram showing a file request crossing from user mode into kernel mode
Simplified path for a file request that reaches the kernel. Elevation changes the security context. Application code still executes in user mode.

Handles and granted access

Handles make resource access concrete. Windows represents many resources as objects, including processes, threads, files, registry keys and access tokens. A handle gives a process a reference through which it can perform permitted operations on an object.

When opening an object, the caller requests particular access rights. Windows evaluates the request, and a successful handle carries the granted access. A handle that permits querying a process does not automatically permit terminating it or writing to its memory.

Diagram showing process-local handles with different granted access to the same object
Handle values are local to a process. These illustrative handles use the same numeric value but grant different operations on the same target object.

We can inspect the types of handles held by our PowerShell process with NtObjectManager:

Import-Module NtObjectManager

Use-NtObject ($process = Get-NtProcess -Current -Access QueryInformation) {
    Get-NtHandle -Process $process -NoQuery |
        Group-Object ObjectType |
        Sort-Object Count -Descending |
        Select-Object Name, Count
}
NtObjectManager output grouping PowerShell handles by object type
The types of handles held by our PowerShell process.

This opens our current process for information queries and groups its existing handles by object type. You may see types such as File, Key, Event and Thread. The exact counts will vary. An Event here is a synchronization object that threads can wait on, rather than a Windows Event Log record.

The counts represent handles. Multiple handles can reference the same underlying object.

The -NoQuery parameter skips additional queries for information such as object names. That is sufficient for this example, where we only want to count handle types.

Use-NtObject also deserves attention. It disposes of the process object opened for this query when the block finishes. It leaves the existing handles we enumerated alone. Managing these references is part of using the module correctly.

We now know that a process holds references to resources. The next question is what allows it to obtain those references.

Access tokens and elevation

The access token describes the security context. It contains information such as the user SID, group SIDs and privileges. A process has a primary token, while a thread can use an impersonation token when acting on behalf of another identity. That means the process owner alone may not explain the effective context of a particular operation.

NtObjectManager lets us inspect the primary token of our current PowerShell process (this PowerShell session is not elevated):

Use-NtObject ($token = Get-NtToken -Primary -Access Query) {
    Format-NtToken -Token $token -Basic
}
Annotated non-elevated PowerShell token with deny-only administrator groups and Medium integrity

-Primary selects the process token. -Access Query requests the right to read token information. The remaining commands display its basic properties. This invocation inspects the existing token without enabling privileges or changing the process identity.

Try the same block in a normal shell and an elevated shell.

For an administrator account using Admin Approval Mode, the normal shell typically uses a filtered token, while the elevated shell uses the full administrator token. You can therefore see different group attributes, privileges and integrity levels while the username remains the same. A standard user supplying another administrator’s credentials is a different situation.

Pay particular attention to the difference between a privilege being present and being enabled. Also look for groups marked deny-only. The group list carries attributes that affect how Windows uses those SIDs during access checks.

Annotated elevated PowerShell token with enabled administrator groups, additional privileges and High integrity

This helps turn a vague description such as “the process is running as an administrator” into a more precise question: which token was effective, and which of its properties mattered for this operation?

Exploring tokens with Token Viewer

We can explore these token differences interactively. NtObjectManager includes Token Viewer, which provides a graphical view of accessible processes and their tokens:

Show-NtToken -All

Find the PowerShell process using its PID and open its token details. Compare the user, group attributes, privileges and integrity level with the console output above. Here, -All opens the viewer’s overview; it does not print every property of the current token.

To open the current PowerShell process directly, use:

Show-NtToken -ProcessId $PID
Annotated Token Viewer showing the primary token of PowerShell process 11740
The PowerShell token viewed with Token Viewer.

The viewer’s permissions determine which tokens it can inspect. Show-NtToken -All -RunAsAdmin starts the viewer elevated when broader access is needed. This does not elevate the processes being inspected.

Asking focused questions with Get-NtToken

Get-NtToken lets us ask more focused questions from PowerShell. For example, we can inspect the token’s elevation properties, list only deny-only groups, and display the state of its privileges:

Use-NtObject ($token = Get-NtToken -Primary -Access Query) {
    $token | Format-List User, TokenType, IntegrityLevel,
        Elevated, ElevationType

    Get-NtTokenGroup -Token $token -Attributes UseForDenyOnly |
        Format-Table Name, Attributes

    Get-NtTokenPrivilege -Token $token |
        Format-Table Name, Enabled
}
PowerShell output showing token elevation, deny-only groups and privilege states
Listing only deny-only groups with the state of its privileges.

ElevationType adds context to the comparison. Under UAC, Limited identifies a filtered token, while Full identifies its elevated counterpart. Default means the token has no linked UAC counterpart; it does not, by itself, tell us whether the account has administrative rights.

The group query isolates SIDs that can participate in deny checks but cannot grant access through allow entries. The privilege query shows which privileges are present and whether they are currently enabled. An elevated token can still contain disabled privileges.

To inspect another accessible process, add -ProcessId to the token query, using that process’s current PID:

$targetProcessId = 11668  # Replace with the current PID of the process to inspect

Use-NtObject (
    $token = Get-NtToken -Primary -ProcessId $targetProcessId -Access Query
) {
    Format-NtToken -Token $token -Basic
}

The process’s primary token is not always the effective token of a thread. A thread can impersonate another identity, for example when a service handles a client request. We can query the current thread’s effective context with:

Use-NtObject ($token = Get-NtToken -Effective -Access Query) {
    $token | Format-List User, TokenType, IntegrityLevel
}
The current PowerShell thread effective token is Primary with Medium integrity
The effective token of the current PowerShell thread.

-Effective first looks for a thread impersonation token. If none exists, it returns the owning process’s primary token. Seeing TokenType: Primary in an ordinary PowerShell session is therefore expected.

Compare that with the primary token of process 11668, which has High integrity.

The primary token of process 11668 has High integrity
The primary token of process 11668.

These queries describe the context at the time of inspection.

How access decisions fit together

The target resource has its own security information. Its discretionary access control list, or DACL, contains access control entries that allow or deny particular rights.

Suppose a process requests both read and write access to a file. An applicable deny entry for the requested write access can cause that combined request to fail, even when reading alone would be permitted. The requested access mask therefore matters alongside the identity.

Windows also applies Mandatory Integrity Control. Under the usual no-write-up policy, a Low-integrity process cannot write to a Medium-integrity object even when the DACL would otherwise allow it. Integrity policy adds another part to the decision.

Diagram connecting requested rights, effective token and object security to an access decision
Conceptual model for opening a securable object. The decision depends on the effective security context, requested rights and rules applicable to the object and operation.

An ACCESS DENIED result could consequently be a starting point for investigation. We still need the requested operation, effective token, target permissions and relevant policy to explain the failure.

Following a file operation in Process Monitor

We can observe the file side of this relationship with a small Procmon exercise.

In PowerShell, prepare a unique temporary path and display the information needed for filtering:

$file = Join-Path $env:TEMP (
    "internals-{0}.txt" -f [guid]::NewGuid()
)
$PID
$file
Process Monitor Include filters for PowerShell PID 11740 and the exact temporary file path
The Procmon filters used.

In Process Monitor, add an Include filter for that PID and another for the exact file path. Clear the existing capture, start recording, and run:

[IO.File]::WriteAllText($file, "Windows internals lab")
[IO.File]::ReadAllText($file)
Remove-Item -LiteralPath $file

The commands create a temporary file, read its contents and remove that same file.

Stop the capture and examine the operations and their results. You should find activity associated with opening, writing, reading and closing the file. Libraries may also perform metadata queries and other supporting operations, so a single high-level command can produce several records.

Process Monitor capture of operations on the temporary file
Procmon results.

Select a CreateFile operation and inspect its details, especially the requested access and disposition.

Annotated CreateFile event properties with Read Attributes access and Open disposition
The Event tab of a CreateFile operation from above.

The operation name alone does not establish that Windows created a new file: the underlying interface supports opening existing files as well.

Annotated process properties for the PowerShell process associated with the CreateFile operation
The process details of a CreateFile operation from above.

Then inspect the stack for an operation. With appropriate symbols, this can help connect the filesystem activity to the components involved in issuing it. Process Monitor supports operation details and thread stacks for precisely this kind of investigation.

Annotated CreateFile stack showing user-mode and kernel-mode frames
The stack details.

The handle view and the trace now complement each other. Process Explorer shows resources currently referenced by a process. Procmon records operations during the capture interval. A file that was opened and closed quickly may appear in the trace without remaining visible in a later handle snapshot.

Understanding memory with VMMap

Memory adds another useful perspective on the same process.

A process uses virtual addresses, which Windows and the processor translate through memory mappings. A large virtual address range does not mean that the process currently occupies the same amount of physical RAM.

These terms help separate the measurements:

TermWhat it describes
Reserved memoryA range of virtual addresses set aside for possible use
Committed memoryMemory for which the system has accounted for the necessary backing
Working setThe process’s pageable memory currently resident in physical RAM
Diagram distinguishing reserved pages, committed pages and resident working-set pages
Simplified view of a pageable private allocation.

Reservation and commitment are separate operations, and committed pages do not all require physical pages immediately.

Open the PowerShell process in VMMap and compare the memory categories and their committed and working-set values.

VMMap captures a moment in time. As PowerShell accesses memory, Windows may need to bring additional pages into its working set. Accessing a page outside that working set triggers a page fault.

A soft fault can be resolved without reading from storage. A hard fault requires a read from a mapped file or the pagefile. Both can occur during normal activity, such as loading a DLL, so interpret fault counts alongside what the process was doing.

Annotated VMMap overview of PowerShell memory categories, committed memory and working set
VMMap overview of the PowerShell process (PID 11740), showing committed memory, private bytes and the working set.

Following a PowerShell process through these tools connects the events we observe to the mechanisms behind them. Handles, tokens, file operations and memory measurements each reveal a different part of its behaviour. Understanding Windows internals gives us a good practical foundation for security investigations on Windows devices.

Sources and further reading

Book and companion material

Tools

  • NtObjectManager and TokenViewer — Source repository for the PowerShell module and token inspection tools used in the examples.
  • Process Explorer — Microsoft Sysinternals documentation and download for inspecting processes, handles and loaded DLLs.
  • Process Monitor — Microsoft Sysinternals documentation and download for tracing file system, Registry and process/thread activity.
  • VMMap — Microsoft Sysinternals documentation and download for examining virtual memory categories and their working sets.

Microsoft technical references

  • About Processes and Threads — Process resources, thread execution and thread security context.
  • User Mode and Kernel Mode — The execution modes used by applications, operating system components and drivers.
  • Object Handles — How handles provide access to system objects and are used in subsequent operations.
  • Access Tokens — Token contents and the distinction between primary and impersonation tokens.
  • How User Account Control Works — Filtered administrator tokens, elevation and the UAC sign-in process.
  • Mandatory Integrity Control — Integrity levels, mandatory labels and the relationship between MIC and discretionary access checks.
  • How AccessCheck Works — How access control entries and the effective security context determine whether requested access is allowed.
  • Working Set — Resident pageable memory, working-set changes, and the difference between soft and hard page faults.
  • CreateFileW — Requested access, creation disposition, sharing modes and file-opening flags.