2026.10.4 学习笔记
Windows Internals 学习笔记:DLL、ApiSet、Window Station、Desktop 与 Session
今天主要学习了 Windows DLL 的导入导出、ApiSet、GUI 子系统中的 Window Station / Desktop,以及 Windows Session 的隔离机制。
使用的主要工具为 NtObjectManager。
首先导入模块:
Import-Module NtObjectManager也可以先查看相关命令:
Get-Command *ModuleExport*
Get-Command *ModuleImport*
Get-Command Get-NtApiSet1. 查看 DLL 导出表
查看一个 DLL 对外导出的全部函数:
Get-Win32ModuleExport -Path "C:\Windows\System32\kernel32.dll"例如可以看到:
CreateProcessW
GetProcAddress
LoadLibraryW
...这里本质上是在解析 PE 文件的 Export Table(导出表)。
也就是说:
PE
└── Export Directory
├── Function1
├── Function2
└── Function3需要注意,NtObjectManager 中这里使用的是:
Get-Win32ModuleExport而不是:
Get-NtModuleExport2. 查看 DLL 某个导出函数的地址
例如查询 kernel32.dll 中 GetProcAddress 的地址:
Get-Win32ModuleExport `
-Path "C:\Windows\System32\kernel32.dll" `
-ProcAddress "GetProcAddress"也可以先加载 DLL:
$module = Import-Win32Module -Path "C:\Windows\System32\kernel32.dll"再查询:
Get-Win32ModuleExport `
-Module $module `
-ProcAddress "GetProcAddress"这里要特别注意:
-Path接收的是 DLL 路径,而:
-Module接收的是已经加载后的模块对象,不能直接写:
Get-Win32ModuleExport -Module kernel32.dll这一点和 Windows API 中:
HMODULE hModule = LoadLibraryW(L"kernel32.dll");
FARPROC addr = GetProcAddress(
hModule,
"GetProcAddress"
);的思想很像。
3. 查看 DLL 的导入表
查看某个 DLL 从其他 DLL 导入了哪些内容:
Get-Win32ModuleImport -Path "C:\Windows\System32\example.dll"例如输出可能类似:
DllName FunctionCount
------- -------------
KERNEL32.dll 15
USER32.dll 6
ADVAPI32.dll 8这里对应 PE 文件中的:
Import Directory也就是这个模块运行时依赖哪些其他模块。
4. 查看从指定 DLL 导入了哪些函数
假设:
example.dll导入了:
kernel32.dll
user32.dll
advapi32.dll如果只想查看它从 kernel32.dll 导入了什么函数:
Get-Win32ModuleImport `
-Path "C:\Path\example.dll" `
-DllName "kernel32.dll"可能得到:
CreateFileW
CloseHandle
GetCurrentProcess
VirtualAlloc
...也就是:
example.dll
|
+-- kernel32.dll
|
+-- CreateFileW
+-- CloseHandle
+-- VirtualAlloc5. ApiSet 与实际 DLL 映射
现代 Windows 中经常可以看到这种 DLL 名字:
api-ms-win-core-file-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-0.dll
api-ms-win-core-memory-l1-1-0.dll这些名称很多并不是传统意义上真正承载实现代码的 DLL,而属于 API Set。
应用程序导入的是一个逻辑 API 集:
api-ms-win-xxxWindows Loader 再根据当前系统的 ApiSet Schema,把它映射到真正的实现模块。
可以使用:
Get-NtApiSet -Name "api-ms-win-core-file-l1-1-0"查看实际映射。
例如逻辑上:
api-ms-win-core-xxx
|
v
kernelbase.dll / kernel32.dll / 其他实现模块因此逆向时,如果 PE 导入表里面看到:
api-ms-win-xxx不能简单地认为系统中一定存在一个同名、包含实际代码实现的 DLL。
也可以直接让 NtObjectManager 在查看导入表时解析 ApiSet:
Get-Win32ModuleImport `
-Path "C:\Path\example.dll" `
-ResolveApiSet如果还想筛选某个 DLL:
Get-Win32ModuleImport `
-Path "C:\Path\example.dll" `
-DllName "kernel32.dll" `
-ResolveApiSet6. Windows GUI 的用户态与内核态
Win32 GUI 并不是完全在用户态实现的。
经典结构可以粗略理解为:
用户程序
|
v
USER32.dll
GDI32.dll
|
v
Win32 GUI Kernel Subsystem
|
v
显示 / 输入 / 窗口管理等其中:
USER32.dll主要提供窗口、消息、输入等 Win32 USER API。
GDI32.dll主要提供传统 GDI 图形相关 API。
传统 Windows 架构资料中经常把内核侧 GUI 子系统称为:
win32k.sys不过现代 Windows 中 GUI / 图形子系统已经比早期 Windows 复杂得多,因此更准确的理解应该是:
USER32/GDI32属于用户态 Win32 GUI 接口层,而大量底层窗口管理和图形相关功能最终进入 Win32k/图形内核子系统。
不要简单理解成:
GUI = user32.dll + gdi32.dll + 一个 win32k.sys现代 Windows 中还会看到:
win32u.dll
gdi32full.dll
dxgkrnl.sys
...等组件。
7. Window Station、Desktop、Window 的层级
Windows GUI 中存在一个很重要的层级:
Session
|
+-- Window Station
|
+-- Desktop
|
+-- Window
+-- Window
+-- WindowWindow Station
Window Station 是一个可安全控制的对象,其中包含:
Clipboard
Atom Table
Desktop
Desktop
...Window Station 主要是进程级关联。
一个进程会连接到某个 Window Station。
例如交互式 Window Station 通常叫:
WinSta0WinSta0 是能够和键盘、鼠标以及显示设备进行交互的 Window Station。
因此可以粗略理解:
Process
|
v
Window StationDesktop
Desktop 是 Window Station 内部的对象。
Desktop 包含:
Window
Menu
Hook
GUI Objects
...Desktop 主要是线程级关联。
也就是:
Process
|
+-- Window Station
|
+-- Thread 1 -> Desktop A
|
+-- Thread 2 -> Desktop A
|
+-- Thread 3 -> Desktop B因此更准确的关系是:
Window Station -> Process
Desktop -> Thread
Window -> 创建该窗口的 GUI Thread需要注意,这里说的是对象关联层级,并不是说创建进程的瞬间永远不能改变 Window Station,也不是说线程永远不能切换 Desktop。
Windows 提供了:
SetProcessWindowStation
SetThreadDesktop这样的 API。
8. 查看 Window Station
查看当前 Session 中的 Window Station:
Get-NtWindowStationName查看当前进程正在使用的 Window Station:
Get-NtWindowStationName -Current普通交互式进程通常可以看到:
WinSta0可以把它理解成:
Process
|
v
WinSta0Window Station 本身也是一个安全对象,所以存在自己的:
Security Descriptor
DACL
Access Mask因此不同进程能不能打开某个 Window Station,需要经过访问检查。
9. 查看 Desktop
查看当前 Window Station 中的 Desktop:
Get-NtDesktopName查看当前线程所在的 Desktop:
Get-NtDesktopName -Current还可以根据线程 ID 查看某个线程所在的 Desktop:
Get-NtDesktopName -ThreadId 1234常见 Desktop 包括:
Default
Winlogon
Screen-saver普通用户正常使用的 Explorer、浏览器、终端等 GUI 程序,一般运行在:
WinSta0\Default登录、锁屏、Ctrl+Alt+Delete 等安全界面则会涉及受保护的 Desktop,例如:
WinSta0\Winlogon这里有一点需要修正:
不能简单说:
只有管理员才能看到 Winlogon Desktop。
更准确的说法是:
Window Station 和 Desktop 都是安全对象,能否枚举、打开以及获得什么访问权限取决于其 Security Descriptor 和当前 Token 的访问权限。
管理员或 SYSTEM 通常能够获得更多权限,但“管理员”并不是判断 Desktop 是否可见或可访问的唯一条件。
10. SendMessage 和 PostMessage
Windows GUI 是典型的消息驱动架构。
窗口一般拥有对应的 Window Procedure:
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam
);常见的消息发送方式有:
SendMessage();
PostMessage();SendMessage
例如:
SendMessage(
hwnd,
WM_USER + 1,
0,
0
);SendMessage 是同步语义。
如果目标窗口属于当前线程,Windows 可以直接调用对应 Window Procedure。
如果目标窗口属于另一个线程,则发送线程通常要等待目标线程处理完这条消息之后才能继续。
逻辑上:
Thread A
|
| SendMessage
v
Thread B / WindowProc
|
| 处理消息
v
返回
|
v
Thread A继续执行因此:
SendMessage = 同步PostMessage
PostMessage(
hwnd,
WM_USER + 1,
0,
0
);PostMessage 不等待窗口处理函数执行完成。
它会把消息投递到目标线程的消息队列,然后返回。
逻辑上:
Thread A
|
| PostMessage
v
Target Thread Message Queue
|
+----------------------+
| Thread A直接继续运行 |
+----------------------+
之后:
Target Thread
|
v
GetMessage / PeekMessage
|
v
DispatchMessage
|
v
WindowProc因此:
SendMessage = 同步
PostMessage = 异步投递这里还需要注意:
SendMessage 发送的消息不能简单理解成“也先进入普通 PostMessage 消息队列,只不过发送者等着”。
特别是跨线程 SendMessage,它属于 Windows 的 sent message 机制,与普通 queued/post message 的处理方式存在区别。
11. Windows Session
这里最容易把几个概念混在一起:
Terminal Services Session
LSA Logon Session
Window Station
Desktop
User它们并不是同一个东西。
Terminal Services Session
我们平时在:
Get-NtProcess里面看到的:
SessionId主要指 Windows Terminal Services / Remote Desktop Services 意义上的 Session。
可以根据 SessionId 对进程分组:
Get-NtProcess | Group-Object SessionId例如可能出现:
Session 0
Session 1
Session 2现代 Windows 中:
Session 0主要用于系统服务。
正常交互式登录用户通常运行在:
Session 1
Session 2
...中。
也可以查看当前系统 Session:
Get-NtConsoleSession快速用户切换并不是在同一个 Session 内换用户
这里之前的理解需要修正。
假设用户 A 已经登录:
Session 1
User A此时使用“切换用户”,用户 B 登录后通常会进入另一个 Session:
Session 1 -> User A
Session 2 -> User B用户 A 的:
进程
窗口
桌面
程序状态
网络连接仍然保留在 Session 1 中。
然后物理控制台切换到 Session 2。
可以粗略理解为:
Physical Console
|
当前连接 Session 2
|
+------------+------------+
| |
Session 1 Session 2
User A User B
| |
WinSta0 WinSta0
| |
Default Default
| |
A的窗口 B的窗口因此快速用户切换的本质并不是:
同一个 Session
User A -> User B而更接近:
Session 1 -> User A
Session 2 -> User B
Console 在不同 Session 之间切换RDP 与 Session
RDP 也是建立在 Session 机制之上的。
RDP 登录通常拥有自己的 Session。
例如:
Console
Session 1
RDP User
Session 2
Another RDP User
Session 3但是 RDP 也可以重新连接到一个已经存在、处于 Disconnect 状态的 Session。
因此:
RDP连接并不必然代表:
每次都创建全新的用户环境它也可能只是重新连接已有 Session。
每个 Session 都存在自己的 Window Station
每个交互式 Session 都可以拥有自己的:
WinSta0因此可以出现:
Session 1
|
+-- WinSta0
|
+-- Default
+-- Winlogon
Session 2
|
+-- WinSta0
|
+-- Default
+-- Winlogon所以虽然名字都叫:
WinSta0但是它们属于不同 Session。
每个 Session 还有自己的 Named Object Namespace
Windows 对很多命名内核对象进行了 Session 隔离,例如:
Event
Mutex
Semaphore
Section / File Mapping
Job
...可以把它粗略理解成:
\Sessions\1\BaseNamedObjects
\Sessions\2\BaseNamedObjects
\Sessions\3\BaseNamedObjects例如 Session 1 中创建:
MyMutex和 Session 2 中创建:
MyMutex默认情况下可以是两个不同的对象。
这也是 Session 隔离的一部分。
可以通过 NtObjectManager 查看:
Get-ChildItem NtObject:\Sessions继续查看某个 Session:
Get-ChildItem NtObject:\Sessions\1再查看命名对象:
Get-ChildItem NtObject:\Sessions\1\BaseNamedObjectsSession 0 不等于 Global Namespace
这里也是一个容易混淆的地方。
之前容易理解成:
Session 0 = Global其实并不准确。
Windows 存在专门的:
Global Namespace用于跨 Session 使用命名对象。
Win32 API 中可以通过:
Global\前缀指定。
例如:
CreateEventW(
NULL,
FALSE,
FALSE,
L"Global\\MyEvent"
);这样创建的对象就可以被其他 Session 中的进程打开,前提是 Security Descriptor 允许。
因此应该区分:
Session 0和:
Global NamespaceSession 0 是一个 Session。
Global namespace 是跨 Session 的命名对象命名空间。
它们不是同一个概念。
Terminal Session 和 Logon Session 也不是一个东西
Windows 还有一个:
Logon Session这是 LSA / Authentication 层面的概念。
通常由一个:
LUID也就是 Authentication ID 标识。
可以使用:
Get-NtLogonSession查看。
因此:
Get-NtProcess | Group-Object SessionId看到的是:
Terminal Services Session而:
Get-NtLogonSession看到的是:
LSA Logon Session这两个概念不要混在一起。
一个 Terminal Session 中完全可能存在多个不同的安全 Token 和 Logon Session。
总结
今天主要把 Windows 中几个容易混淆的层级关系串了起来:
Windows
|
+-- Session
|
+-- Window Station
| |
| +-- Desktop
| |
| +-- Window
|
+-- Process
| |
| +-- Thread
|
+-- Session Named Object Namespace
|
+-- Event
+-- Mutex
+-- Section
+-- Semaphore从 GUI 的角度:
Session
↓
Window Station
↓
Desktop
↓
Window从对象绑定关系来看:
Process -> Window Station
Thread -> Desktop
Window -> GUI Thread从 Session 隔离来看:
Session 1
├── Processes
├── WinSta0
├── Desktop
└── BaseNamedObjects
Session 2
├── Processes
├── WinSta0
├── Desktop
└── BaseNamedObjects而跨 Session 的命名对象则可以使用:
Global\命名空间。
最后还要特别区分:
Terminal Services Session ID
!=
LSA Logon Session / Authentication ID前者主要解决多用户交互环境、RDP、GUI 和 Session 资源隔离问题,后者属于 Windows 身份认证和安全 Token 体系。