0xdea/semgrep-rules

GitHub: 0xdea/semgrep-rules

由安全研究员 0xdea 维护的 Semgrep 规则集,专注于 C/C++ 代码的漏洞研究,涵盖缓冲区溢出、内存管理错误、整数问题等多种安全缺陷检测。

Stars: 805 | Forks: 82

# semgrep-rules [![](https://img.shields.io/github/stars/0xdea/semgrep-rules.svg?style=flat&color=yellow)](https://github.com/0xdea/semgrep-rules) [![](https://img.shields.io/github/forks/0xdea/semgrep-rules.svg?style=flat&color=green)](https://github.com/0xdea/semgrep-rules) [![](https://img.shields.io/github/watchers/0xdea/semgrep-rules.svg?style=flat&color=red)](https://github.com/0xdea/semgrep-rules) [![](https://img.shields.io/badge/semgrep-1.157.0-violet)](https://semgrep.dev/) [![](https://img.shields.io/badge/twitter-%400xdea-blue.svg)](https://twitter.com/0xdea) [![](https://img.shields.io/badge/mastodon-%40raptor-purple.svg)](https://infosec.exchange/@raptor) [![test](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/8c47207b65222852.svg)](https://github.com/0xdea/semgrep-rules/actions/workflows/validate.yml) 用于辅助漏洞研究的 Semgrep 规则集合。 [![C 语言编程](https://img.youtube.com/vi/tas0O586t80/maxresdefault.jpg)](https://www.youtube.com/embed/tas0O586t80) ## 博客文章 * * * * ## 另见 * * * ## 设置和使用说明 1. 安装 [Semgrep](https://semgrep.dev/docs/getting-started/)。 2. 克隆此 GitHub repo。 3. 要使用这些规则,请运行: ``` # 高优先级扫描(快速见效) semgrep --severity ERROR --config semgrep-rules/rules /path/to/source # 高和中等优先级扫描(推荐) semgrep --severity ERROR --severity WARNING --config semgrep-rules/rules /path/to/source # 完整扫描(可能包含边缘发现和更多误报) semgrep --config semgrep-rules/rules /path/to/source ``` 为了获得更流畅的体验,我建议将 Semgrep 扫描输出保存为 [SARIF](https://sarifweb.azurewebsites.net/) 格式,并在 [VS code](https://code.visualstudio.com/) 中使用 [SARIF Explorer](https://marketplace.visualstudio.com/items?itemName=trailofbits.sarif-explorer): ``` semgrep --sarif --sarif-output=/path/to/source/SEMGREP.sarif --config semgrep-rules/rules /path/to/source code /path/to/source # then open the SEMGREP.sarif file in VS code with SARIF Explorer ``` 另请参阅包含的 SARIF 输出 [示例](sarif-example)。 ## 兼容性 * 已在 Semgrep CLI 1.157.0 上测试 ## 规则 ### C/C++ #### 缓冲区溢出 * [**insecure-api-gets**](rules/c/insecure-api-gets.yaml). 使用了不安全的 API 函数 `gets`。 * [**insecure-api-strcpy-strcat**](rules/c/insecure-api-strcpy-strcat.yaml). 使用了潜在不安全的 API 函数 `strcpy`, `stpcpy`, `strcat`。 * [**insecure-api-sprintf-vsprintf**](rules/c/insecure-api-sprintf-vsprintf.yaml). 使用了潜在不安全的 API 函数 `sprintf` 和 `vsprintf`。 * [**insecure-api-scanf**](rules/c/insecure-api-scanf.yaml). 使用了 `scanf` 系列中潜在不安全的 API 函数。 * [**incorrect-use-of-strncat**](rules/c/incorrect-use-of-strncat.yaml). 传递给 `strncat` 的 size 参数错误。 * [**use-of-source-size-in-copy**](rules/c/use-of-source-size-in-copy.yaml). 传递给 `strncpy`, `memcpy`, `snprintf` 及其变体的 size 参数错误。 * [**incorrect-use-of-sizeof**](rules/c/incorrect-use-of-sizeof.yaml). 误在指针而非其目标上使用 `sizeof` 运算符。 * [**unterminated-string-strncpy**](rules/c/unterminated-string-strncpy.yaml). 在 `strncpy` 和 `stpncpy` 之后缺少显式的 NUL 终止。 * [**off-by-one**](rules/c/off-by-one.yaml). 潜在的差一错误 (off-by-one)。 * [**unsafe-ret-snprintf-vsnprintf**](rules/c/unsafe-ret-snprintf-vsnprintf.yaml). 潜在不安全地使用了 `snprintf` 和 `vsnprintf` 的返回值。 * [**unsafe-ret-strlcpy-strlcat**](rules/c/unsafe-ret-strlcpy-strlcat.yaml). 潜在不安全地使用了 `strlcpy` 和 `strlcat` 的返回值。 * [**pointer-subtraction**](rules/c/pointer-subtraction.yaml). 潜在的使用指针减法来确定大小。 * [**write-into-stack-buffer**](rules/c/write-into-stack-buffer.yaml). 直接写入分配在栈上的缓冲区。 #### 整数溢出 * [**integer-wraparound**](rules/c/integer-wraparound.yaml). 潜在的整数回绕错误。 * [**unsafe-strlen**](rules/c/unsafe-strlen.yaml). 将 `strlen` 的返回值转换为 short 可能是危险的。 * [**integer-truncation**](rules/c/integer-truncation.yaml). 潜在的整数截断错误。 * [**signed-unsigned-conversion**](rules/c/signed-unsigned-conversion.yaml). 潜在的有符号/无符号转换错误。 * [**incorrect-unsigned-comparison**](rules/c/incorrect-unsigned-comparison.yaml). 检查无符号变量是否为负数。 #### 格式化字符串 * [**format-string-bugs**](rules/c/format-string-bugs.yaml). 潜在的格式化字符串漏洞。 #### 内存管理 * [**insecure-api-alloca**](rules/c/insecure-api-alloca.yaml). 使用了潜在不安全的 API 函数 `alloca`。 * [**use-after-free**](rules/c/use-after-free.yaml). 潜在的释放后使用 (`free` 后)。 * [**double-free**](rules/c/double-free.yaml). 潜在的双重释放 (`free`)。 * [**incorrect-use-of-free**](rules/c/incorrect-use-of-free.yaml). 对非堆内存调用 `free`。 * [**unchecked-ret-malloc**](rules/c/unchecked-ret-malloc.yaml). 未检查 `malloc`, `calloc`, `realloc` 等的返回码。 * [**putenv-stack-var**](rules/c/putenv-stack-var.yaml). 使用栈分配的变量调用 `putenv`。 * [**ret-stack-address**](rules/c/ret-stack-address.yaml). 潜在返回栈分配变量的地址。 * [**mismatched-memory-management**](rules/c/mismatched-memory-management.yaml). 潜在不匹配的 C 内存管理例程。 * [**mismatched-memory-management-cpp**](rules/c/mismatched-memory-management-cpp.yaml). 潜在不匹配的 C++ 内存管理例程。 * [**memory-address-exposure**](rules/c/memory-address-exposure.yaml). 潜在的底层内存地址暴露。 #### 命令注入 * [**command-injection**](rules/c/command-injection.yaml). 通过 `system` 或 `popen` 进行的潜在 OS 命令注入。 #### 竞态条件 * [**insecure-api-access-stat**](rules/c/insecure-api-access-stat.yaml). 使用了不安全的 API 函数 `access`, `stat`, `lstat` 等。 * [**insecure-api-mktemp-tmpnam-tempnam**](rules/c/insecure-api-mktemp-tmpnam-tempnam.yaml). 使用了不安全的 API 函数 `mktemp`, `tmpnam`, `tempnam`。 * [**insecure-api-signal**](rules/c/insecure-api-signal.yaml). 使用了不安全的 API 函数 `signal`。 #### 权限管理 * [**incorrect-order-setuid-setgid**](rules/c/incorrect-order-setuid-setgid.yaml). 以错误的顺序调用权限管理函数。 * [**unchecked-ret-setuid-seteuid**](rules/c/unchecked-ret-setuid-seteuid.yaml). 未检查 `setuid` 和 `seteuid` 的返回码。 #### 拒绝服务 * [**regex-dos**](rules/c/regex-dos.yaml). 可能表现出指数级运行时间并导致 ReDoS 的正则表达式。 #### 杂项 * [**incorrect-use-of-memset**](rules/c/incorrect-use-of-memset.yaml). `memset` 的参数顺序错误。 * [**insecure-api-rand-srand**](rules/c/insecure-api-rand-srand.yaml). 使用了潜在不安全的 API 函数 `rand` 和 `srand`。 * [**overlapping-source-destination**](rules/c/overlapping-source-destination.yaml). 复制函数中的源和目标重叠。 * [**suspicious-assert**](rules/c/suspicious-assert.yaml). 由于使用断言宏可能导致无效的大小检查。 * [**interesting-api-calls**](rules/c/interesting-api-calls.yaml). 调用了有趣且潜在不安全的 API 函数。 * [**unchecked-ret-scanf**](rules/c/unchecked-ret-scanf.yaml). 未检查 `scanf` 系列函数的返回码。 * [**insecure-api-ato**](rules/c/insecure-api-ato.yaml). 使用了潜在不安全的 API 函数 `atoi`, `atol`, `atof`。 * [**high-entropy-assignment**](rules/c/high-entropy-assignment.yaml). 分配了可能是机密的高熵值。 * [**argv-envp-access**](rules/c/argv-envp-access.yaml). 命令行参数或环境变量访问。 * [**missing-default-in-switch**](rules/c/missing-default-in-switch.yaml). switch 语句中缺少 default case。 * [**missing-break-in-switch**](rules/c/missing-break-in-switch.yaml). switch 语句中缺少 break 或等效项。 * [**missing-return**](rules/c/missing-return.yaml). 非 void 函数中缺少 return 语句。 * [**typos**](rules/c/typos.yaml). 具有安全隐患的潜在拼写错误。 ### Generic (通用) #### 杂项 * [**bad-words**](rules/generic/bad-words.yaml). 暗示存在 bug 的关键词和注释。 ## 更新日志 * [CHANGELOG.md](CHANGELOG.md) ## TODO (待办事项) ### 短期行动项 * 处理未解决的 [issues](https://github.com/0xdea/semgrep-rules/issues) 和 [pull requests](https://github.com/0xdea/semgrep-rules/pulls)。 * 向现有规则添加 **新检查**,并在需要时添加新规则。 * 提高整体 **准确性** 并减少误报,同时不遗漏代码中的潜在热点。 * 检查性能(参见 [test_public_repos.py](https://github.com/semgrep/semgrep-rules/blob/main/tests/performance/test_public_repos.py))。 * 针对真实世界代码进行额外的 `--time` **基准测试**,以发现需要优化的慢速规则。 * 将 **嘈杂的规则**(例如 `high-entropy-assignment`)移动到特定文件夹,以简化发布到 Semgrep registry 的流程。 * 在发布 v2.0.0 并发布到官方 **Semgrep registry** 之前进行广泛的 **测试**。 ### 长期行动项 * 添加脚本以清理由 [常见反编译器](https://github.com/0xdea/semgrep-rules/issues/12) 生成的伪代码,以改善 Semgrep 解析。 * 将规则移植到 [Semgrep Pro engine](https://semgrep.dev/docs/semgrep-code/semgrep-pro-engine-intro),它允许进行 **跨文件** 和 **跨函数** 分析。 * 在合适的地方实现 [taint mode](https://semgrep.dev/docs/writing-rules/data-flow/taint-mode/overview),以通过 **数据流分析 (dataflow analysis)** 改进规则。 * 研究 [symbolic propagation](https://semgrep.dev/docs/writing-rules/experiments/symbolic-propagation),这可能有助于减少一些 **误报**。 * 按照 [这项研究](https://parsiya.net/blog/semgrep-fun/) 中的描述实现一个 Semgrep **包装器和后处理器**。 ### 特定领域规则集 * 实现专用的 **内核规则**(Linux, BSD, macOS 等)。 * 实现专用的 [crypto](https://semgrep.dev/r?q=cpp.lang.security.crypto&lang=C%2CC%2B%2B) 和 [rng](https://semgrep.dev/r?q=cpp.lang.security.rng&lang=C%2CC%2B%2B) 规则。 * 实现专用的 [C++ 规则](https://semgrep.dev/r?lang=C%2B%2B),并将它们移动到另一个与 C 规则分开的文件夹中。
标签:C/C++安全, SAST, Semgrep, Subfinder, WordPress安全扫描, 云资产清单, 代码安全, 图数据库, 安全专业人员, 客户端加密, 开源安全工具, 情报收集, 漏洞枚举, 漏洞研究, 盲注攻击, 逆向工程, 逆向工程平台, 错误基检测, 静态代码分析