# YARA

**YARA** 是一种模式匹配语言与工具，用于依据文件及进程内存中的文本或二进制模式来识别和分类恶意软件。它由维克托·曼努埃尔·阿尔瓦雷斯（Victor Manuel Alvarez）创建，于2013年发布在 GitHub 上，其官方标语自称是“恶意软件研究员（以及其他所有人）的模式匹配瑞士军刀”<sup>[1](https://3375217.fs1.hubspotusercontent-na1.net/hubfs/3375217/Presentation%20PDFs/2024-11-14_YARA%20Rules%20101.pdf)</sup>。规则写法类似 C 语言，由扫描引擎对文件、目录和运行中的进程执行<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。2024年，最初的 C 实现开始被 YARA-X 取代，后者是同一作者用 Rust 重写的版本<sup>[3](https://virustotal.github.io/yara-x/blog/yara-is-dead-long-live-yara-x/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 用途 | 通过文件二进制模式进行基于特征码的恶意软件识别与分类，是密码学哈希的一种替代方案<sup>[4](https://arxiv.org/abs/2111.13910)</sup> |
| 规则结构 | `strings` 段（可选）与 `condition` 段（必需），另有可选的 `meta` 段用于存放标识符/值对<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup> |
| 模式类型 | 文本字符串、十六进制字符串和正则表达式，可配合 `nocase`、`wide` 等修饰符<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup> |
| 模块 | `pe`、`elf`、`dotnet`、`hash`、`macho`、`lnk`、`math`、`string`、`time` 等，通过 `import` 语句导入<sup>[5](https://virustotal.github.io/yara-x/docs/)</sup><sup> • </sup><sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup> |
| YARA-X 重写 | 2024年5月17日公布，用 Rust 编写，目标是与 YARA 达到99%的规则级兼容<sup>[3](https://virustotal.github.io/yara-x/blog/yara-is-dead-long-live-yara-x/)</sup> |
| 稳定版发布 | YARA-X 1.0.0 于2025年6月4日发布；原版 YARA 进入维护模式，仅接收关键缺陷修复和安全补丁<sup>[6](https://virustotal.github.io/yara-x/blog/yara-x-is-stable/)</sup> |
| YARA 最新版本 | v4.5.8<sup>[7](https://github.com/VirusTotal/yara/releases)</sup> |

## 规则剖析

一条规则以关键字 `rule` 开头，后接一个遵循 C 词法惯例的标识符，区分大小写，且不得超过128个字符<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。strings 段定义要搜索的模式，每个模式都有一个以 `$` 开头的标识符；condition 段则是关于这些标识符的布尔表达式，这些标识符充当布尔变量，当字符串在扫描的文件或进程内存中被找到时即为真<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。

**三种模式类型。** 文本字符串用双引号括起，默认按 ASCII 编码且区分大小写；`nocase` 修饰符使匹配不区分大小写，`wide` 修饰符则匹配每字符两字节编码的字符串，这在可执行二进制文件中很常见<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。十六进制字符串在花括号中定义原始字节序列，支持通配符（`?`）、跳转、备选和取反运算符<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。正则表达式则定义可读文本的片段<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。

**条件。** 条件使用布尔运算符 `and`、`or`、`not`，以及关系运算符、算术运算符和位运算符；整数始终为64位长，因此 `~0x01` 的求值结果是 `0xFFFFFFFFFFFFFFFE`，而非 `0xFE`<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。字符串的出现次数可用 `#a` 取得，诸如 `1 of them`、`all of ($a*)`、`none of ($b*)` 这样的集合表达式则对字符串组进行操作<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。自 YARA 4.2.0 起，计数和集合可限定在某个整数范围内，例如 `#a in (filesize-500..filesize) == 2`<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。

**未定义值。** 模块常常会让变量处于未定义状态，例如扫描非 PE 文件时的 `pe.entry_point`。未定义的表达式会转换为 `false`，而 `and` 与 `or` 会把未定义的操作数当作 `false`，因此，一条把字符串条件与 `pe.entry_point == 0x1000` 组合起来的规则，即便字符串存在，也不会匹配非 PE 文件<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。

## 模块

模块通过解析文件结构和计算值来扩展该语言，用 `import "pe"` 之类的语句导入，并以模块名作为前缀引用，例如 `pe.entry_point == 0x1000`<sup>[2](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)</sup>。YARA-X 随附的模块包括 `console`、`crx`、`dotnet`、`elf`、`dex`、`hash`、`lnk`、`macho`、`math`、`pe`、`string` 和 `time`<sup>[5](https://virustotal.github.io/yara-x/docs/)</sup>。专为 macOS Mach-O 可执行文件定制的 `macho` 模块，以及可暴露 Windows 链接文件（Link Files）元数据的 `lnk` 模块，在 YARA-X 接任引擎后被加入了 [VirusTotal](https://www.edgepedia.org/zh/virustotal) 的 Livehunt 和 Retrohunt 服务<sup>[8](https://virustotal.github.io/yara-x/blog/virustotal-moves-to-yara-x/)</sup>。

## YARA-X

YARA-X 是用 Rust 对 YARA 的完全重写，由 VirusTotal（谷歌）高级主任软件工程师维克托·曼努埃尔·阿尔瓦雷斯于2024年5月17日公布<sup>[3](https://virustotal.github.io/yara-x/blog/yara-is-dead-long-live-yara-x/)</sup><sup> • </sup><sup>[1](https://3375217.fs1.hubspotusercontent-na1.net/hubfs/3375217/Presentation%20PDFs/2024-11-14_YARA%20Rules%20101.pdf)</sup>。其既定目标是：更好的用户体验、与 YARA 99%的规则级兼容、在使用正则表达式或复杂循环的规则上提升性能、借助 Rust 获得更高的可靠性与安全性，以及为 Python、Golang 和 C 提供官方 API<sup>[3](https://virustotal.github.io/yara-x/blog/yara-is-dead-long-live-yara-x/)</sup>。之所以选择用 Rust 重写，是因为 YARA 是一个中等规模的项目，其相互交织的代码使渐进式迁移不切实际<sup>[3](https://virustotal.github.io/yara-x/blog/yara-is-dead-long-live-yara-x/)</sup>。

**生产环境采用。** 2024年12月4日，YARA-X 成为驱动 VirusTotal Livehunt 和 Retrohunt 服务的引擎。由于 YARA-X 产生的性能警告更少，此前因服务的性能策略而被拒绝的规则变得可以接受，扫描文件因超时受影响的比例从约2%降至0.2%以下<sup>[8](https://virustotal.github.io/yara-x/blog/virustotal-moves-to-yara-x/)</sup>。随后 YARA-X 1.0.0 于2025年6月4日发布，结束了测试阶段；随着该版本的发布，原版 YARA 进入维护模式，仅接收关键缺陷修复和安全补丁，所有新开发都转移到了 YARA-X<sup>[6](https://virustotal.github.io/yara-x/blog/yara-x-is-stable/)</sup>。原版 YARA 仍继续发布缺陷修复版本，最新为 v4.5.8<sup>[7](https://github.com/VirusTotal/yara/releases)</sup>。

2026年2月公布的 YARA-X 语言服务器，通过[语言服务器协议](https://www.edgepedia.org/zh/language-server-protocol)（Language Server Protocol）为 VS Code、Sublime Text 和 Vim 等编辑器提供集成<sup>[9](https://virustotal.github.io/yara-x/blog/introducing-the-yara-language-server/)</sup>。

## 用途与局限

YARA 在各类检测格式中占据一个特定生态位：基于 YAML 的规则格式 Sigma 在其自身文档中被描述为“之于日志文件，正如 Snort 之于网络流量、YARA 之于文件”<sup>[10](https://sigmahq.io/sigma/)</sup>。单条 YARA 规则即可通过匹配独特的字符串或字节序列，在许多端点上检测出某个恶意软件家族，该格式被广泛用于恶意软件语料库、磁盘镜像、邮件网关和沙箱<sup>[11](https://inferensys.com/differences/ai-driven-cybersecurity-operations-soc/ai-driven-dfir-platforms/yara-vs-sigma-rules)</sup>。

**误报与性能。** 过于宽泛的规则（例如匹配常见字符串）会产生误报，而多态或混淆的恶意软件则会产生漏报<sup>[12](https://nccs.gov.in/public/events/NCCS-malware-analysis-presentation-Sep24_%281%29.pdf)</sup>。社区的性能指南建议仅在必要时使用正则表达式，避免 `.*` 之类的贪婪量词，改用 `.{1,30}` 这样的有界量词，并在可能时优先使用带跳转和通配符的十六进制字符串，因为正则表达式的求值更慢、占用内存更多<sup>[13](https://github.com/Neo23x0/YARA-Performance-Guidelines)</sup>。

**规模。** 直接用 YARA 扫描大型语料库开销高昂，围绕它已构建出若干研究系统。YARIX 是一个在 USENIX Security 2021 上展示的搜索引擎，它把 YARA 规则转换为搜索词，对倒排的4-gram索引进行查询；在3200万个恶意软件样本和1404条公开 YARA 规则上评估，它平均比完整的顺序 YARA 搜索快五个数量级，同时返回健全且完整的结果<sup>[14](https://www.usenix.org/system/files/sec21-brengel.pdf)</sup>。检测流水线还把 YARA 规则用作第一阶段过滤器，在应用机器学习模型和动态分析之前快速剔除已知样本<sup>[15](https://www.sciencedirect.com/science/article/pii/S0167404824005704)</sup>。

## References

1. [A Brief History of YARA (YARA Rules 101 presentation, 2024)](https://3375217.fs1.hubspotusercontent-na1.net/hubfs/3375217/Presentation%20PDFs/2024-11-14_YARA%20Rules%20101.pdf)
2. [Writing YARA rules, YARA documentation (VirusTotal/yara, GitHub)](https://github.com/VirusTotal/yara/blob/master/docs/writingrules.rst)
3. [YARA is dead, long live YARA-X, YARA-X blog (May 17, 2024)](https://virustotal.github.io/yara-x/blog/yara-is-dead-long-live-yara-x/)
4. [Adam Lockett (2021). Assessing the Effectiveness of YARA Rules for Signature-Based Malware Detection and Classification. arXiv:2111.13910](https://arxiv.org/abs/2111.13910)
5. [YARA-X Docs](https://virustotal.github.io/yara-x/docs/)
6. [YARA-X is stable!, YARA-X blog (June 4, 2025)](https://virustotal.github.io/yara-x/blog/yara-x-is-stable/)
7. [Releases, VirusTotal/yara, GitHub](https://github.com/VirusTotal/yara/releases)
8. [VirusTotal moves to YARA-X, YARA-X blog (December 4, 2024)](https://virustotal.github.io/yara-x/blog/virustotal-moves-to-yara-x/)
9. [Introducing the YARA language server, YARA-X blog (February 3, 2026)](https://virustotal.github.io/yara-x/blog/introducing-the-yara-language-server/)
10. [Sigma, Generic Signature Format for SIEM Systems](https://sigmahq.io/sigma/)
11. [YARA vs Sigma Rules: Threat Detection Compared, Inference Systems](https://inferensys.com/differences/ai-driven-cybersecurity-operations-soc/ai-driven-dfir-platforms/yara-vs-sigma-rules)
12. [Malware in the Telecom Industry, NCCS presentation](https://nccs.gov.in/public/events/NCCS-malware-analysis-presentation-Sep24_%281%29.pdf)
13. [YARA Performance Guidelines, Neo23x0, GitHub](https://github.com/Neo23x0/YARA-Performance-Guidelines)
14. [Brengel et al. (2021). YARIX: Scalable YARA Search at USENIX Security 2021](https://www.usenix.org/system/files/sec21-brengel.pdf)
15. [SLIFER: Investigating performance and robustness of malware detection pipelines, Computers & Security](https://www.sciencedirect.com/science/article/pii/S0167404824005704)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › Malware and endpoint threats › Anti-malware and malware analysis

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/yara

English original: https://www.edgepedia.org/yara
