avatar

Malloc 的博客

学海无涯苦作舟

  • 首页
  • 归档
  • 分类
  • 标签
  • 我
  • 友链
主页 2026.10.4 学习笔记
文章

2026.10.4 学习笔记

发表于 2026-10-4 更新于 2026-10- 4
作者 Malloc
39~50 分钟 阅读

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-NtApiSet

1. 查看 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-NtModuleExport

2. 查看 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
           +-- VirtualAlloc

5. 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-xxx

Windows 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" `
    -ResolveApiSet

6. 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
                   +-- Window

Window Station

Window Station 是一个可安全控制的对象,其中包含:

Clipboard
Atom Table
Desktop
Desktop
...

Window Station 主要是进程级关联。

一个进程会连接到某个 Window Station。

例如交互式 Window Station 通常叫:

WinSta0

WinSta0 是能够和键盘、鼠标以及显示设备进行交互的 Window Station。

因此可以粗略理解:

Process
   |
   v
Window Station

Desktop

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
WinSta0

Window 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\BaseNamedObjects

Session 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 Namespace

Session 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 体系。

Windows安全内幕
security windows DLL GUI Win32
许可协议:  CC BY 4.0
分享

相关文章

10月 10, 2026

2026.10.10 学习笔记-注册表隐藏键导致扫描程序缓冲区溢出

Windows 注册表 NULL 隐藏键研究:Win32 与 Native API 字符串解析差异及 DoS 风险 一、Win32 API 与 Native API 的字符串解析差异 Windows 中,Wi

10月 10, 2026

2026.10.9 学习笔记 -- 文件扩展名的安全问题

# Windows 文件关联机制与注册表劫持研究 ## 一、CreateProcess 与 ShellExecute 的区别 Windows 中启动程序通常可以使用 CreateProcess,但该函数主要用于创建可执行映像对应的进程,不能直接将普通 TXT 文本当作可执行程序启动。 例如: Cre

10月 5, 2026

2026.10.5 学习笔记

Windows Win32 与 NT 原生字符串、注册表及路径解析机制 Windows 用户层常见的 Win32 API 与更底层的 Native API 在字符串、注册表路径以及文件路径解析方面存在明显差异。 很多看起来比较奇怪的 Windows 安全问题,本质上就是: 两个不同层级的 API 对

下一篇

2026.10.5 学习笔记

上一篇

section内核对象共享安全问题

最近更新

  • 2026.10.10 学习笔记-注册表隐藏键导致扫描程序缓冲区溢出
  • 2026.10.9 学习笔记 -- 文件扩展名的安全问题
  • 2026.10.5 学习笔记
  • 2026.10.4 学习笔记
  • section内核对象共享安全问题

热门标签

Halo docker Dos ext GUI Registry section DLL windows security

目录

©2026 Malloc 的博客. 保留部分权利。

使用 Halo 主题 Chirpy