# 数据执行保护

**数据执行保护（DEP）** 是一种系统级的内存保护功能，自 [Windows XP](https://www.edgepedia.org/zh/windows-xp) 和 [Windows Server 2003](https://www.edgepedia.org/zh/windows-server-2003) 起内置于 [Microsoft Windows](https://www.edgepedia.org/zh/microsoft-windows) 中，它允许操作系统将内存页标记为不可执行，从而使代码无法从堆、栈和内存池等数据区域运行。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup> 它是一种硬件加软件的解决方案：处理器对每个内存页强制执行执行禁用位，当试图在未明确标记为可执行的页上执行时，Windows 会引发访问冲突。<sup>[2](https://www.microsoft.com/en-us/msrc/blog/2009/06/understanding-dep-as-a-mitigation-technology-part-1/)</sup>

| 关键事实 | 详情 |
|---|---|
| 目的 | 防止代码从堆和栈等纯数据内存页运行，使缓冲区溢出利用更加困难<sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup> |
| 首次发布 | Windows XP SP2 和 Windows Server 2003 SP1，它们会检查 CPU 的执行禁用位（在 Intel 平台上称为 NX）<sup>[2](https://www.microsoft.com/en-us/msrc/blog/2009/06/understanding-dep-as-a-mitigation-technology-part-1/)</sup> |
| 违规行为 | 从受保护页执行会引发 STATUS_ACCESS_VIOLATION (0xC0000005)；若未处理，进程将被终止<sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup><sup> • </sup><sup>[2](https://www.microsoft.com/en-us/msrc/blog/2009/06/understanding-dep-as-a-mitigation-technology-part-1/)</sup> |
| 默认策略 | Windows 客户端版本为 OptIn（仅对 Windows 系统组件和服务应用 DEP）；服务器版本为 OptOut（监视所有应用程序和服务）<sup>[3](https://techcommunity.microsoft.com/blog/askperf/app-data-execution-prevention-dep/373695)</sup><sup> • </sup><sup>[4](https://learn.microsoft.com/en-us/troubleshoot/windows-client/performance/determine-hardware-dep-available)</sup> |
| 配置 | 在启动时根据启动配置数据中的不执行策略设置（BCDEdit `nx` 值：AlwaysOn、OptIn、OptOut、AlwaysOff）；在 32 位 Windows 上，硬件强制 DEP 需要 PAE<sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/boot-parameters-to-configure-dep-and-pae)</sup> |
| 每进程控制 | 应用程序可通过 SetProcessDEPPolicy 更改自身的 DEP 设置；一旦通过 PROCESS_DEP_ENABLE 启用，在进程生命周期内无法禁用<sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup><sup> • </sup><sup>[6](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setprocessdeppolicy)</sup> |
| 保护范围 | Microsoft 将其归类为纵深防御性漏洞利用缓解措施：攻击者无法从堆和栈等不可执行内存执行代码<sup>[7](https://www.microsoft.com/en-us/msrc/windows-security-servicing-criteria)</sup> |

## 工作原理

DEP 将物理内存位置标记为用于可执行或不可执行代码。如果某段代码试图从标记为不执行的位置执行，DEP 会通过使应用程序崩溃来阻止它。<sup>[3](https://techcommunity.microsoft.com/blog/askperf/app-data-execution-prevention-dep/373695)</sup> 在支持执行禁用位的处理器上，任何试图从具有 PAGE_READWRITE 等保护的页执行代码的操作都会生成 STATUS_ACCESS_VIOLATION (0xC0000005) 异常。<sup>[2](https://www.microsoft.com/en-us/msrc/blog/2009/06/understanding-dep-as-a-mitigation-technology-part-1/)</sup> 如果应用程序未处理该异常，调用进程将被终止。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup>

确实需要从内存运行代码的应用程序必须使用适当的虚拟内存保护属性来分配内存：PAGE_EXECUTE、PAGE_EXECUTE_READ、PAGE_EXECUTE_READWRITE 或 PAGE_EXECUTE_WRITECOPY。通过 malloc 和 HeapAlloc 进行的堆分配是不可执行的，应用程序无法从默认进程堆或栈运行代码。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup>

DEP 有两种类型。硬件强制 DEP 涵盖内核模式和用户模式进程，需要处理器和操作系统支持。软件强制 DEP 仅涵盖用户模式进程，仅需操作系统支持。在 32 位版本的 Windows 上，硬件强制 DEP 需要物理地址扩展（PAE）；在支持硬件强制 DEP 的处理器上启用 DEP 时，Windows 会自动启用 PAE 并忽略禁用它的启动参数。<sup>[5](https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/boot-parameters-to-configure-dep-and-pae)</sup>

## 历史与硬件支持

Microsoft 于 2004 年在 Windows XP SP2 中添加了 NX 支持，并将该功能称为 DEP。它最初是一项可选加入的功能，因为某些类别的合法软件不区分数据和代码，在启用 DEP 时会崩溃，通常是动态生成代码的软件。<sup>[8](https://www.os2museum.com/wp/how-the-pc-industry-screws-things-up/)</sup> 在 Intel 平台上，底层能力称为 NX（不执行）；Intel 为所有 AMD64 兼容 CPU 以及后期型号的 32 位 Prescott Pentium 4 及之后的 Pentium M 处理器都加入了 NX 支持。<sup>[9](https://download.microsoft.com/download/1/c/3/1c3b5ab0-e064-4350-a31f-48312180d9b5/st_vid10823-st.pdf)</sup><sup> • </sup><sup>[8](https://www.os2museum.com/wp/how-the-pc-industry-screws-things-up/)</sup>

内核中也存在相同的页级执行限制。在 [Windows 7](https://www.edgepedia.org/zh/windows-7) 及更早版本中，从非分页池分配的所有内存都是可执行的；从 [Windows 8](https://www.edgepedia.org/zh/windows-8) 开始，内核模式驱动程序可以从单独的不执行（NX）非分页池进行分配，从该池分配驱动程序数据可防止恶意软件在其中执行指令。x86、x64 和 Arm 架构均支持将页指定为 NX。<sup>[10](https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/no-execute-nonpaged-pool)</sup>

## 配置与策略

启动时，系统通过读取存储在启动配置数据中的不执行页保护策略来建立 DEP，该策略可通过 BCDEdit `/set` 命令的 `nx` 选项进行调整。支持四个值：AlwaysOn（对所有进程启用 DEP）、OptIn（客户端版本的默认值，仅对 Windows 系统组件和服务应用 DEP）、OptOut（对所有进程启用 DEP，带有管理员管理的例外列表）和 AlwaysOff（不对任何进程启用 DEP）。<sup>[4](https://learn.microsoft.com/en-us/troubleshoot/windows-client/performance/determine-hardware-dep-available)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/boot-parameters-to-configure-dep-and-pae)</sup> Microsoft 的驱动程序文档指出，DEP 是一项非常有效的安全功能，除非别无选择，否则不应禁用，默认设置对大多数系统而言是最佳的。<sup>[5](https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/boot-parameters-to-configure-dep-and-pae)</sup>

管理员和应用程序可以通过 WMI 验证当前状态：当硬件强制 DEP 可用时，`wmic OS Get DataExecutionPrevention_Available` 返回 TRUE；`DataExecutionPrevention_SupportPolicy` 分别对 AlwaysOff、AlwaysOn、OptIn 和 OptOut 返回 0 到 3。<sup>[4](https://learn.microsoft.com/en-us/troubleshoot/windows-client/performance/determine-hardware-dep-available)</sup> 应用程序可以使用 GetSystemDEPPolicy 读取当前策略，并根据策略使用 SetProcessDEPPolicy 更改自身设置。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup>

在现代 Windows 漏洞利用防护界面中，DEP 被列为系统和应用级缓解措施之一，且仅可针对 32 位（x86）应用程序进行配置；对于所有其他架构，它永久启用，并带有可选的 ATL thunk 仿真设置。<sup>[11](https://github.com/MicrosoftDocs/defender-docs/blob/public/defender-endpoint/customize-exploit-protection.md)</sup>

## 应用程序兼容性

某些应用程序功能与 DEP 不兼容。执行动态代码生成（如即时编译）且未显式为生成的代码标记执行权限的应用程序，在启用了 DEP 的系统上可能会失败。使用 7.1 或更早版本的 Active Template Library (ATL) 构建的程序可能会尝试在标记为不可执行的页上运行代码；结果是 NX 错误，导致程序终止。少数可执行文件和库还会将小的代码段（通常称为 thunk）放在数据节中；DEP 将映像节标记为不可执行，除非该节带有可执行属性，因此此类代码应移至代码节，或者应为该节标上 IMAGE_SCN_MEM_EXECUTE 属性。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup>

Microsoft 自己的应用程序说明了这种权衡。[Internet Explorer](https://www.edgepedia.org/zh/internet-explorer) 7 和 Office 应用程序默认不选择加入 DEP，因为它们内部运行的第三方扩展和插件，尽管这些应用程序本身在启用 DEP 时可以正常工作。管理员可以通过应用程序兼容性工具包数据库、将系统范围的策略提升为 OptOut 或 AlwaysOn，或者在 [Windows Vista](https://www.edgepedia.org/zh/windows-vista) 和 Server 2008 上通过 ExecuteOptions 注册表设置，为单个应用程序启用 DEP。<sup>[12](https://www.microsoft.com/en-us/msrc/blog/2009/06/understanding-dep-as-a-mitigation-technology-part-2/)</sup>

## 有效性与局限

Microsoft 将 DEP 的安全目标描述为确保攻击者无法从堆和栈等不可执行内存执行代码，并将其归类为纵深防御性漏洞利用缓解措施，而非针对所有漏洞利用的全面防御。<sup>[7](https://www.microsoft.com/en-us/msrc/windows-security-servicing-criteria)</sup><sup> • </sup><sup>[1](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)</sup> 2009 年 Microsoft 的一次演示展示了该缓解措施对真实攻击的有效性：在一台未打补丁的 Windows XP SP2 机器上为 Excel 2007 启用 DEP 后，一个原本能可靠实现代码执行的漏洞利用在试图从 PAGE_READWRITE 页执行时反而因访问冲突而崩溃。<sup>[12](https://www.microsoft.com/en-us/msrc/blog/2009/06/understanding-dep-as-a-mitigation-technology-part-2/)</sup>

DEP 并不能阻止所有漏洞利用。安全研究表明，面向返回的编程（ROP）——复用现有可执行代码中的短小指令片段，将其拼接成攻击——正是为了绕过 DEP 和地址空间布局随机化（ASLR）等缓解措施而开发的。<sup>[13](https://arxiv.org/abs/1008.4099)</sup> 因此，DEP 与 ASLR、任意代码防护和代码完整性防护等其他缓解措施一起，构成了 Windows 漏洞利用防御的分层体系。<sup>[7](https://www.microsoft.com/en-us/msrc/windows-security-servicing-criteria)</sup>

## References

1. [Data Execution Prevention, Microsoft Learn (Win32 apps)](https://learn.microsoft.com/en-us/windows/win32/memory/data-execution-prevention)
2. [Understanding DEP as a mitigation technology part 1, Microsoft Security Research & Defense blog (2009)](https://www.microsoft.com/en-us/msrc/blog/2009/06/understanding-dep-as-a-mitigation-technology-part-1/)
3. [APP: Data Execution Prevention (DEP), Ask The Performance Team, Microsoft Tech Community](https://techcommunity.microsoft.com/blog/askperf/app-data-execution-prevention-dep/373695)
4. [Determine hardware DEP is available, Microsoft Learn (Windows Client troubleshoot)](https://learn.microsoft.com/en-us/troubleshoot/windows-client/performance/determine-hardware-dep-available)
5. [Boot Parameters to Configure DEP and PAE, Microsoft Learn (Windows drivers)](https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/boot-parameters-to-configure-dep-and-pae)
6. [SetProcessDEPPolicy function (winbase.h), Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setprocessdeppolicy)
7. [Microsoft Security Servicing Criteria for Windows, Microsoft Security Response Center](https://www.microsoft.com/en-us/msrc/windows-security-servicing-criteria)
8. [How the PC Industry Screws Things Up, OS/2 Museum](https://www.os2museum.com/wp/how-the-pc-industry-screws-things-up/)
9. [Protection from Implementation Weaknesses, Microsoft Windows Common Criteria security target (2017)](https://download.microsoft.com/download/1/c/3/1c3b5ab0-e064-4350-a31f-48312180d9b5/st_vid10823-st.pdf)
10. [No-Execute (NX) Nonpaged Pool, Microsoft Learn (Windows drivers)](https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/no-execute-nonpaged-pool)
11. [Customize exploit protection, Microsoft Defender documentation (GitHub)](https://github.com/MicrosoftDocs/defender-docs/blob/public/defender-endpoint/customize-exploit-protection.md)
12. [Understanding DEP as a mitigation technology part 2, Microsoft Security Research & Defense blog (2009)](https://www.microsoft.com/en-us/msrc/blog/2009/06/understanding-dep-as-a-mitigation-technology-part-2/)
13. [Security Mitigations for Return-Oriented Programming Attacks, arXiv:1008.4099](https://arxiv.org/abs/1008.4099)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Operating system concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/data-execution-prevention

English original: https://www.edgepedia.org/data-execution-prevention
