muhanLuo/wordpress-plugin-semgrep-rules
GitHub: muhanLuo/wordpress-plugin-semgrep-rules
一套用于发现WordPress插件安全漏洞的Semgrep规则集合,帮助自动化静态代码分析。
Stars: 1 | Forks: 0
# WordPress 插件的 Semgrep 规则
本列表包含为发现 WordPress 插件中常见安全漏洞而编写的 Semgrep 规则。
这些规则主要基于以下资源:
- [WordFence - 常见的 WordPress 漏洞及通过安全编码最佳实践进行预防](https://www.wordfence.com/wp-content/uploads/2021/07/Common-WordPress-Vulnerabilities-and-Prevention-Through-Secure-Coding-Best-Practices.pdf)
- [WordFence - Alex Thomas 的文章](https://www.wordfence.com/blog/author/wfalext/)
- [PatchStack Academy](https://patchstack.com/academy/welcome/)
## 示例用法
```
git clone https://github.com/muhanLuo/wordpress-plugin-semgrep-rules.git
semgrep login
semgrep scan --config="p/php" --config ./wordpress-plugin-semgrep-rules/ --metrics=off --timeout 60 --dataflow-traces --sarif --output scan_results.sarif ./plugin-directory/
```
**解释**
- ``--config="p/php"``:配置 Semgrep 使用 [默认 PHP 规则集](https://semgrep.dev/p/php) 进行扫描。
- ``--config ./wordpress-plugin-semgrep-rules/``:配置 Semgrep 同时使用此特定目录中的规则进行扫描。
- ``--metrics=off``:禁止将指标发送至 Semgrep 服务器。
- ``--timeout 60``:单条规则在单个文件上花费的最大时间(秒)。
- ``--dataflow-traces``:使 Semgrep 为执行 [从源到汇](https://www.youtube.com/watch?v=ZaOtY4i5w_U) 分析的规则输出详细数据流,例如 [``wp-php-object-injection-audit.yml``](https://github.com/muhanLuo/wordpress-plugin-semgrep-rules/blob/main/deserialization/wp-php-object-injection-audit.yml)。
- ``--sarif``:以 SARIF 格式输出结果,这是静态分析工具的标准输出格式。
- ``--output scan_results.sarif``:将输出保存到的文件。
- ``./plugin-directory/``:要扫描的目录。
更多 Semgrep CLI 信息可 [在此处](https://semgrep.dev/docs/cli-reference) 找到。
## 充分利用规则的 5 个技巧
### 1. 运行前先测试规则
Semgrep 经常对其引擎进行 [更改](https://semgrep.dev/docs/release-notes),有时这可能会破坏规则。在运行这些规则之前,在 ``wordpress-plugin-semgrep-rules/`` 目录下运行此命令以确保规则正常工作。
```
semgrep scan -v --config . --test .
```
如果这些规则在更新到更新版本后突然停止工作,请考虑降级您的 Semgrep 版本。
### 2. 创建 Semgrep 账户
虽然此仓库中几乎所有规则都可以在没有 Semgrep 账户的情况下运行,但 ``wp-ajax-hook-missing-auth`` 规则无法在不登录的情况下运行,因为它使用了 [join 模式](https://semgrep.dev/docs/writing-rules/experiments/join-mode/overview),这是 Semgrep 的一项实验性功能。
创建账户后,只需在您的 CLI 中运行 ``semgrep login``。
### 3. 将这些规则与 Semgrep 的 PHP 规则集一起运行
当前仓库中的规则仅涵盖某些 WordPress 特定的问题。为了进行更彻底的扫描,请将此仓库中的规则与 [Semgrep 的 PHP 规则集](https://semgrep.dev/p/php) 结合使用。
*附注:Semgrep 也有一个 [WordPress 规则集](https://semgrep.dev/p/wordpress),但截至 2026 年 4 月撰写时,我认为那些规则并不太好。*
### 4. 设置更长的 ``--timeout``
在 Semgrep 中运行这些规则时,您可能希望使用以下参数设置更长的超时时间 ``--timeout``。其中一些规则确实需要更长的时间来运行,并且有时可能在较大的文件上超时。
### 5. 使用 SARIF Explorer
Trail of Bits 的 VSCode 扩展 [SARIF Explorer](https://github.com/trailofbits/vscode-sarif-explorer) 对于审查扫描输出非常有用。该扩展提供了许多强大功能,例如:
- 轻松浏览和导航发现的问题。
- 将发现标记为误报、已审查或真正漏洞的能力。
- 针对 Semgrep 输出的搜索和筛选功能。

## 规则说明
### 反序列化
| 规则(s) | 描述 | |
| --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| ``wp-php-object-injection-audit`` | 识别从用户输入到 ``unserialize()`` 或 ``maybe_unserialize()`` 的数据流。
更多信息,请参阅 Patchstack 的这篇关于 [PHP 对象注入](https://patchstack.com/academy/wordpress/vulnerabilities/php-object-injection/) 的文章。 | ### 缺失授权 | 规则(s) | 描述 | | | -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | ``wp-ajax-hook-missing-auth`` | **⚠️您必须登录才能运行此规则,因为 [此规则使用 ``join 模式``](https://semgrep.dev/docs/writing-rules/experiments/join-mode/overview),这是仅对登录用户开放的实验性功能。**
查找其回调函数未执行权限或 nonce 检查的 AJAX 钩子。
更多信息,请阅读 [WordFence 的这篇文章](https://www.wordfence.com/blog/2023/02/authorization-vs-intent-why-you-should-always-verify-both/)。此规则的灵感来自 [Brandon Roldan](https://noob3xploiter.medium.com/automating-csrf-detection-in-wordpress-plugins-with-semgrep-52ece2c212b7)。
*附注:不要一次在 [过多插件](https://semgrep.dev/docs/kb/semgrep-code/scan-engine-kill) 上运行此规则,此规则似乎会消耗大量内存。根据我的经验,在过多插件(几百个)上运行此规则可能会导致您的计算机崩溃。* | | | ``wp-return-true-register-rest-route`` | 查找 ``register_rest_route()`` 的用法,其中 ``'permission_callback' => '__return_true'``。这意味着关联的回调函数不包含任何授权检查。
有关 ``register_rest_route()`` 函数的更多信息,请参阅 [WPKama](https://wp-kama.com/function/register_rest_route)。 | | | ``wp-missing-auth-rest-route-*`` | 查找 ``register_rest_route()`` 的用法,其中 ``'permission_callback'`` 中的回调函数不包含 ``current_user_can()``。这意味着此 REST 路由可能不执行授权检查。
有关 ``register_rest_route()`` 函数的更多信息,请参阅 [WPKama](https://wp-kama.com/function/register_rest_route)。 | | | ``wp-missing-direct-access-check`` | 查找未阻止用户通过 URL 直接导航到文件的 PHP 文件。标记所有未调用 ``defined( 'ABSPATH' )`` 的 PHP 文件。
进一步阅读请参阅 [Notes on Tech](https://notesontech.com/preventing-direct-access-to-php-files-in-wordpress/) | | ### 跨站脚本 (XSS) | 规则(s) | 描述 | | | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | ``wp-reflected-xss`` | 识别从 ``$_GET`` 或 ``$_REQUEST`` 超全局变量到用于打印字符串的函数(如 ``echo()``、``print()``、``printf()`` 等)的数据流。排除所有数据流经过清理函数(如 ``esc_attr()`` 或 ``wp_kses()``)的发现。
更多信息,请参阅 WordFence 的这篇关于 [如何在 WordPress 插件中查找 XSS 漏洞](https://www.wordfence.com/blog/2024/09/how-to-find-xss-cross-site-scripting-vulnerabilities-in-wordpress-plugins-and-themes/) 的文章。 | ### 杂项 | 规则(s) | 描述 | | | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | ``wp-open-redirect-audit`` | 识别从 ``$_GET`` 或 ``$_REQUEST`` 超全局变量到 ``wp_redirect()`` 或 ``header()`` 的数据流。
要了解更多,请阅读 Patchstack 的 [这篇文章](https://patchstack.com/academy/wordpress/vulnerabilities/open-redirect/)。 |
更多信息,请参阅 Patchstack 的这篇关于 [PHP 对象注入](https://patchstack.com/academy/wordpress/vulnerabilities/php-object-injection/) 的文章。 | ### 缺失授权 | 规则(s) | 描述 | | | -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | ``wp-ajax-hook-missing-auth`` | **⚠️您必须登录才能运行此规则,因为 [此规则使用 ``join 模式``](https://semgrep.dev/docs/writing-rules/experiments/join-mode/overview),这是仅对登录用户开放的实验性功能。**
查找其回调函数未执行权限或 nonce 检查的 AJAX 钩子。
更多信息,请阅读 [WordFence 的这篇文章](https://www.wordfence.com/blog/2023/02/authorization-vs-intent-why-you-should-always-verify-both/)。此规则的灵感来自 [Brandon Roldan](https://noob3xploiter.medium.com/automating-csrf-detection-in-wordpress-plugins-with-semgrep-52ece2c212b7)。
*附注:不要一次在 [过多插件](https://semgrep.dev/docs/kb/semgrep-code/scan-engine-kill) 上运行此规则,此规则似乎会消耗大量内存。根据我的经验,在过多插件(几百个)上运行此规则可能会导致您的计算机崩溃。* | | | ``wp-return-true-register-rest-route`` | 查找 ``register_rest_route()`` 的用法,其中 ``'permission_callback' => '__return_true'``。这意味着关联的回调函数不包含任何授权检查。
有关 ``register_rest_route()`` 函数的更多信息,请参阅 [WPKama](https://wp-kama.com/function/register_rest_route)。 | | | ``wp-missing-auth-rest-route-*`` | 查找 ``register_rest_route()`` 的用法,其中 ``'permission_callback'`` 中的回调函数不包含 ``current_user_can()``。这意味着此 REST 路由可能不执行授权检查。
有关 ``register_rest_route()`` 函数的更多信息,请参阅 [WPKama](https://wp-kama.com/function/register_rest_route)。 | | | ``wp-missing-direct-access-check`` | 查找未阻止用户通过 URL 直接导航到文件的 PHP 文件。标记所有未调用 ``defined( 'ABSPATH' )`` 的 PHP 文件。
进一步阅读请参阅 [Notes on Tech](https://notesontech.com/preventing-direct-access-to-php-files-in-wordpress/) | | ### 跨站脚本 (XSS) | 规则(s) | 描述 | | | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | ``wp-reflected-xss`` | 识别从 ``$_GET`` 或 ``$_REQUEST`` 超全局变量到用于打印字符串的函数(如 ``echo()``、``print()``、``printf()`` 等)的数据流。排除所有数据流经过清理函数(如 ``esc_attr()`` 或 ``wp_kses()``)的发现。
更多信息,请参阅 WordFence 的这篇关于 [如何在 WordPress 插件中查找 XSS 漏洞](https://www.wordfence.com/blog/2024/09/how-to-find-xss-cross-site-scripting-vulnerabilities-in-wordpress-plugins-and-themes/) 的文章。 | ### 杂项 | 规则(s) | 描述 | | | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | ``wp-open-redirect-audit`` | 识别从 ``$_GET`` 或 ``$_REQUEST`` 超全局变量到 ``wp_redirect()`` 或 ``header()`` 的数据流。
要了解更多,请阅读 Patchstack 的 [这篇文章](https://patchstack.com/academy/wordpress/vulnerabilities/open-redirect/)。 |
标签:ffuf, PHP安全, Semgrep, Web安全, WordPress安全, WordPress安全扫描, 安全最佳实践, 插件安全, 操作系统监控, 源码分析, 漏洞预防, 网络安全, 蓝队分析, 错误基检测, 隐私保护, 静态代码分析