需先自行安装 Semgrep CLI,README 测试版本为 1.169.0;默认会遵循 .gitignore 跳过文件,需加 --no-git-ignore 才能全量扫描;noisy 目录下的规则误报较多,不建议直接用于常规扫描
semgrep-rules
0xdeaMIT活跃维护一套面向漏洞研究的 Semgrep 规则集,覆盖 C 等语言的静态代码扫描,帮你在审计时快速定位可疑模式。
A collection of my Semgrep rules to facilitate vulnerability research.
semgrep --severity ERROR --severity WARNING --config "p/0xdea"README 未说明
一套面向 C/C++ 漏洞研究的 Semgrep 规则集,覆盖缓冲区溢出、整数溢出、内存管理等常见缺陷
一套面向漏洞研究的 Semgrep 规则集,覆盖 C 等语言的静态代码扫描,帮你在审计时快速定位可疑模式。
核心能力
- 按缓冲区溢出、整数溢出、格式化字符串等类别组织 C/C++ 规则
- 检测 gets、strcpy、sprintf 等不安全 API 调用
- 识别 use-after-free、double-free 等内存管理问题
- 支持按严重级别过滤扫描,可只跑 ERROR 级快速筛查
- 输出 SARIF 格式,可配合 VS Code 插件查看结果
容易踩的坑
- v2.0.0v2.0.0 - Current stable release最新正式版2026-06-15 · 2026-06-15 07:30 UTC
First release to be published in the official Semgrep registry.
Changed
- Add the deep expression operator in some patterns (
incorrect-order-setuid-setgid,regex-dos). - Optimize memory allocation patterns (
mismatched-memory-management,mismatched-memory-management-cpp,unchecked-ret-malloc). - Remove trailing semicolon from some detection patterns (
incorrect-use-of-memset,mismatched-memory-management,mismatched-memory-management-cpp,putenv-stack-var,unsafe-strlen). - Change language in
bad-wordsfromgenerictoregexto improve performance. - Refactor the directory tree to simplify integration with the official Semgrep registry.
- Add link to the official Semgrep registry to the documentation.
- Generally improve code samples and documentation.
Security
- Pin all action references to hashes and add zizmor GitHub workflow.
- Add the deep expression operator in some patterns (
- v1.2.02026-04-03 · 2026-04-03 16:33 UTC
查看更新说明
Added
- Add C++
thisand function call patterns toincorrect-use-of-sizeof. - Add detections for
confstr,ttyname_r/ptsname_r,strfind/strrspn/strtrns,readlinkat,fgetws/fgetws_l,*cftime,chdir,chroot,copylist,dbminit,fattach,ftw,truncate,umask/ulimit,cdev_init,inet_*,utmp*,OemToCharW,mbs/wcs,*spawn*,CreateProcess*, and missingget*family functions (interesting-api-calls). - Add detections for some Microsoft-specific functions (
interesting-api-calls). - Add detections for missing
str*family functions (insecure-api-strcpy-strcat,interesting-api-calls,overlapping-source-destination,write-into-stack-buffer). - Add detections for
mempcpyandwmempcpy(use-of-source-size-in-copy,interesting-api-calls,overlapping-source-destination,signed-unsigned-conversion,write-into-stack-buffer). - Add detections for
swprintfandvswprintf(signed-insigned-conversion,write-into-stack-buffer). - Add detection for
p2openandwordexp(command-injection,interesting-api-calls). - Add detections for
faccessat,faccessat2, andfstatat(insecure-api-access-stat,interesting-api-calls). - Add detections for
rand48family functions (insecure-api-rand-srand,interesting-api-calls). - Add detections for
sysloganderr/warnfunction families (memory-address-exposure). - Add detections for missing
signalfamily functions (insecure-api-signal,interesting-api-calls). - Add detection for
tmpnam_r(insecure-api-mktemp-tmpnam-tempnam,interesting-api-calls). - Add detection for
VirtualAlloc(integer-wraparound). - Add detection for
syscall(interesting-api-calls). - Add some code samples (
command-injection,format-string-bugs,incorrect-use-of-sizeof,insecure-api-access-stat,insecure-api-gets,insecure-api-scanf,insecure-api-sprintf-vsprintf,interesting-api-calls,off-by-one,unsafe-ret-snprintf-vsnprintf,use-after-free,use-of-source-size-in-copy,unterminated-string-strncpy). - Add required metadata to contribute rules to the Semgrep Registry.
- Add metadata validation in GitHub workflow (see also ToB's CONTRIBUTING document).
- Add
prettier-checkGitHub action to check YAML formatting.
Changed
- Extend
*s*printfpattern matching (interesting-api-calls). - Improve matching of array creation patterns (
incorrect-use-of-free,putenv-stack-var,ret-stack-address,write-into-stack-buffer). - Improve matching of array creation and length calculation (
off-by-one). - Optimize uses of the deep expression operator (
unchecked-ret-malloc,use-after-free). - Exclude string literals as source (
insecure-api-scanf). - Add detections for
snprintffamily functions and improve pattern matching (use-of-source-size-in-copy). - Replace the pattern
= $_;with= ...;everywhere. - Rename
incorrect-order-setuid-setgid-etcintoincorrect-order-setuid-setgid. - Rename
incorrect-use-of-strncpy-memcpy-etcintouse-of-source-size-in-copy. - Rename
insecure-api-scanf-etcintoinsecure-api-scanf. - Rename
unchecked-ret-scanf-etcintounchecked-ret-scanf. - Rename
insecure-api-strcpy-stpcpy-strcatintoinsecure-api-strcpy-strcat. - Rename
unterminated-string-strncpy-stpncpyintounterminated-string-strncpy. - Rename
unchecked-ret-malloc-calloc-reallocintounchecked-ret-malloc. - Rename
insecure-api-access-stat-lstatintoinsecure-api-access-stat. - Rename
insecure-api-atoi-atol-atofintoinsecure-api-ato. - Rename
overlapping-src-dstintooverlapping-source-destination. - Rearrange rule order based on new subcategory/likelihood/impact metrics.
- Improve code samples, documentation, and indent style.
- Update SARIF output example.
- Add C++
- v1.1.02025-12-03 · 2025-12-03 14:47 UTC
查看更新说明
Added
- Add
high-entropy-assignmentandregex-dosrules that use the new metavariable-analysis feature. - Add detections for the
__iso99_*scanffunction family (format-string-bugs,insecure-api-scanf,unchecked-ret-scanf). - Add detections for the
_mb*and*_s/*_lfunction families (insecure-api-strcpy-stpcpy-strcat,interesting-api-calls,unsafe-ret-strlcpy-strlcat,unsafe-strlen,unterminated-string-strncpy-stpncpy,write-into-stack-buffer). - Add some code samples (
incorrect-use-of-strncpy-memcpy-etc,integer-wraparound,overlapping-src-dst). - Add SARIF output example as requested.
- Add GitHub action to automate rule validation and testing.
Changed
- Rename
incorrect-use-of-strncpy-stpncpy-strlcpyintoincorrect-use-of-strncpy-memcpyand add matching ofmemcpyand its main variants. - Rename
incorrect-use-of-sprintf-snprintfintooverlapping-src-dstand add matching of other copy functions. - Exclude length known at compile time from
memcpymatches (interesting-api-calls). - Explicitly exclude
$_ = 0assignments to reduce false positives (integer-truncation,signed-unsigned-conversion). - Refactor some rules to include other variants and improve readability and scalability (
unsafe-ret-snprintf-vsnprintf,unsafe-ret-strlcpy-strlcat,unsafe-strlen). - Implement case-insensitive matching (
interesting-api-calls). - Reduce false positives (
format-string-bugs,putenv-stack-var,ret-stack-address,unsafe-ret-snprintf-vsnprintf,write-into-stack-buffer). - Use the new anonymous metavariables feature where suitable.
- Optimize some patterns (
integer-truncation,integer-wraparound,mismatched-memory-management,mismatched-memory-management-cpp,off-by-one,write-into-stack-buffer,missing-return). - Change some confidence levels (
integer-truncation,integer-wraparound,putenv-stack-var,ret-stack-address). - Move rules into a dedicated folder.
- Improve documentation and code formatting.
Removed
- Remove non-existent API functions (
insecure-api-strcpy-stpcpy-strcat,unterminated-string-strncpy-stpncpy).
- Add
- v1.0.0v1.0.0 - Historical stable release2025-11-10 · 2025-11-10 08:12 UTC
查看更新说明
Historical, battle-tested stable release.
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
semgrep-rules
"The attack surface is the vulnerability. Finding a bug there is just a detail."
-- Mark Dowd
"Some details are more important than others."
-- Fedor G. Pikus
A collection of my Semgrep rules to facilitate vulnerability research.
Articles
- https://hnsecurity.it/blog/semgrep-ruleset-for-c-c-vulnerability-research
- https://hnsecurity.it/blog/automating-binary-vulnerability-discovery-with-ghidra-and-semgrep
- https://hnsecurity.it/blog/big-update-to-my-semgrep-c-cpp-ruleset
- https://hnsecurity.it/blog/streamlining-vulnerability-research-with-the-idalib-rust-bindings-for-ida-9-2/
- https://hnsecurity.it/blog/my-semgrep-c-cpp-ruleset-is-ready-for-prime-time-again/
See also
- https://appsec.guide/docs/static-analysis/semgrep/
- https://semgrep.dev/docs/writing-rules/overview
- https://semgrep.dev/r
Setup and usage instructions
- Install Semgrep.
- To use these rules via the Semgrep registry, run the following from the root folder of your target source code repository:
# high priority scan (quick wins)
semgrep --severity ERROR --config "p/0xdea"
# high and medium priority scan (recommended)
semgrep --severity ERROR --severity WARNING --config "p/0xdea"
# full scan (might include marginal findings and more false positives)
semgrep --config "p/0xdea"
Alternatively, you can clone this GitHub repository locally and run:
# full scan
semgrep --config semgrep-rules/rules /path/to/source
# specific rule scan
semgrep --config semgrep-rules/rules/c/command-injection.yaml /path/to/source
[!TIP] Specify the
--no-git-ignoreswitch to scan files regardless of git tracking status or.gitignorerules.
For a better streamlined experience, I recommend saving the Semgrep scan output in SARIF format and using SARIF Explorer in VS code:
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
See also the included SARIF output example.
Compatibility
Tested with Semgrep CLI 1.169.0.
Rules
C/C++
Battle-tested C (and sometimes C++) ruleset.
buffer overflows
- insecure-api-gets. Use of the insecure API function
gets. - insecure-api-strcpy-strcat. Use of potentially insecure API functions
strcpy,stpcpy,strcat. - insecure-api-sprintf-vsprintf. Use of potentially insecure API functions
sprintfandvsprintf. - insecure-api-scanf. Use of potentially insecure API functions in the
scanffamily. - incorrect-use-of-strncat. Wrong size argument passed to
strncat. - use-of-source-size-in-copy. Wrong size argument passed to
strncpy,memcpy,snprintf, and variants. - incorrect-use-of-sizeof. Accidental use of the
sizeofoperator on a pointer instead of its target. - unterminated-string-strncpy. Lack of explicit NUL-termination after
strncpyandstpncpy. - off-by-one. Potential off-by-one error.
- unsafe-ret-snprintf-vsnprintf. Potentially unsafe use of the return value of
snprintfandvsnprintf. - unsafe-ret-strlcpy-strlcat. Potentially unsafe use of the return value of
strlcpyandstrlcat. - pointer-subtraction. Potential use of pointer subtraction to determine size.
- write-into-stack-buffer. Direct write into buffer allocated on the stack.
integer overflows
- integer-wraparound. Potential integer wraparound errors.
- unsafe-strlen. Casting the return value of
strlento short might be dangerous. - integer-truncation. Potential integer truncation errors.
- signed-unsigned-conversion. Potential signed/unsigned conversion errors.
- incorrect-unsigned-comparison. Checking if an unsigned variable is negative.
format strings
- format-string-bugs. Potential format string bugs.
memory management
- insecure-api-alloca. Use of the potentially insecure API function
alloca. - use-after-free. Potential use after
free. - double-free. Potential double
free. - incorrect-use-of-free. Calling
freeon memory not in the heap. - unchecked-ret-malloc. Unchecked return code of
malloc,calloc,realloc, etc. - putenv-stack-var. Call to
putenvwith a stack-allocated variable. - ret-stack-address. Potential return of the address of a stack-allocated variable.
- mismatched-memory-management. Potentially mismatched C memory management routines.
- mismatched-memory-management-cpp. Potentially mismatched C++ memory management routines.
- memory-address-exposure. Potential exposure of underlying memory addresses.
command injection
- command-injection. Potential OS command injection via
systemorpopen.
race conditions
- insecure-api-access-stat. Use of insecure API functions
access,stat,lstat, etc. - insecure-api-mktemp-tmpnam-tempnam. Use of insecure API functions
mktemp,tmpnam,tempnam. - insecure-api-signal. Use of insecure API function
signal.
privilege management
- incorrect-order-setuid-setgid. Privilege management functions called in the wrong order.
- unchecked-ret-setuid-seteuid. Unchecked return code of
setuidandseteuid.
denial of service
- regex-dos. Regular expression that may exhibit exponential runtime and lead to ReDoS.
miscellaneous
- incorrect-use-of-memset. Wrong order of arguments to
memset. - insecure-api-rand-srand. Use of potentially insecure API functions
randandsrand. - overlapping-source-destination. Source and destination overlap in copy functions.
- suspicious-assert. Potentially invalid size check due to use of assertion macros.
- interesting-api-calls. Calls to interesting and potentially insecure API functions.
- unchecked-ret-scanf. Unchecked return code of functions in the
scanffamily. - insecure-api-ato. Use of potentially insecure API functions
atoi,atol,atof. - high-entropy-assignment. Assignment of a high-entropy value that might be a secret.
- argv-envp-access. Command-line argument or environment variable access.
- missing-default-in-switch. Missing default case in a switch statement.
- missing-break-in-switch. Missing break or equivalent in a switch statement.
- missing-return. Missing return statement in non-void function.
- typos. Potential typos with security implications.
Noisy
These rules are considered potentially too noisy and unsuitable for general use.
generic
- bad-words. Keywords and comments that suggest the presence of bugs.
Changelog
TODO
Short-term action items
- Process open issues and pull requests.
- Add new checks to the existing rules and add new rules where needed.
- Improve overall accuracy and reduce false positives, without missing potential hot spots in code.
- Check performance (see test_public_repos.py).
- Additional
--timebenchmarking against real-world code to spot slow rules in need of optimization.
Long-term action items
- Add scripts to clean up pseudocode generated by common decompilers to improve Semgrep parsing.
- Port the rules to the Semgrep Pro engine, which allows for inter-file and inter-function analysis.
- Implement taint mode where suitable to improve rules with dataflow analysis.
- Investigate symbolic propagation that might be useful to reduce some false positives.
- Implement a Semgrep wrapper and post-processor as described in this research.
