bad-antics/YARAJulia.jl
GitHub: bad-antics/YARAJulia.jl
纯 Julia 实现的类 YARA 模式匹配引擎,支持完整的十六进制、文本和正则模式,用于恶意软件分析和威胁检测。
Stars: 0 | Forks: 0
# YARAJulia.jl
[]()
一个用于恶意软件分析、威胁检测和二进制扫描的纯 Julia 类 YARA 模式匹配引擎。无需外部依赖。
## 功能特性
- **十六进制模式** — 纯字节、通配符 (`??`)、跳转 (`[N-M]`)、备选项 (`(XX|YY)`)
- **文本模式** — 纯文本、不区分大小写 (`nocase`)、UTF-16LE (`wide`)、单词边界 (`fullword`)
- **正则表达式模式** — 完整的正则表达式支持,可选不区分大小写标志
- **规则条件** — `all of them`、`any of them`、`N of them`、自定义函数
- **RuleSet** — 分组并使用多个规则进行扫描
- **YARA 语法解析器** — 解析标准 YARA 规则文件
- **零依赖** — 纯 Julia 实现
## 安装
```
using Pkg
Pkg.add("YARAJulia")
```
## 快速开始
```
using YARAJulia
# 检测 PE executables
rule = yara_rule("detect_pe",
strings = [
yara_hex("mz_header", "4d 5a 90 00"),
yara_hex("pe_sig", "50 45 00 00"),
],
condition = :all_of_them,
tags = ["pe", "windows"]
)
result = scan(rule, read("suspicious.exe"))
if result.matched
println("PE file detected! $(match_count(result)) matches")
for m in result.matches
println(" $(m.string_id) at offset $(m.offset)")
end
end
```
## 模式类型
### 十六进制模式
```
# 纯十六进制字节
yara_hex("magic", "7f 45 4c 46") # ELF magic
# 通配符 (任意字节)
yara_hex("sig", "4d 5a ?? 00")
# 跳转 (变长间隙)
yara_hex("spaced", "4d 5a [2-8] 50 45")
# 替代匹配
yara_hex("variant", "4d (5a | 5b) 90 00")
```
### 文本模式
```
yara_text("cmd", "CreateRemoteThread")
yara_text("cmd_ci", "createremotethread"; nocase=true)
yara_text("wide_str", "kernel32"; wide=true)
yara_text("word", "malware"; fullword=true)
```
### 正则表达式模式
```
yara_regex("url", raw"https?://[\w.-]+/[\w./-]+")
yara_regex("email", raw"[\w.]+@[\w.]+\.\w+"; nocase=true)
```
## 规则条件
```
# 所有 patterns 必须匹配
yara_rule("strict", strings=patterns, condition=:all_of_them)
# 任意 pattern 必须匹配
yara_rule("loose", strings=patterns, condition=:any_of_them)
# 至少 N 个 patterns 必须匹配
yara_rule("threshold", strings=patterns, condition=(:n_of_them, 3))
# 自定义 condition 函数
yara_rule("custom", strings=patterns,
condition=(matched, matches, data) -> length(matches) >= 5)
```
## RuleSet (规则集)
```
rs = RuleSet("malware_detection")
add_rule!(rs, rule1)
add_rule!(rs, rule2)
# 扫描所有 rules,仅返回匹配结果
results = scan_with_ruleset(rs, data)
# 或从 vector 编译
rs = compile_rules([rule1, rule2]; name="compiled")
```
## YARA 语法解析器
```
source = raw"""
rule detect_elf : linux {
meta:
author = "analyst"
severity = 8
strings:
$magic = { 7F 45 4C 46 }
$suspicious = "eval("
condition:
all of them
}
"""
rules = parse_yara(source)
result = scan(rules[1], file_data)
```
## 文件扫描
```
result = scan_file(rule, "/path/to/file")
```
## API 参考
| Function | Description |
|----------|-------------|
| `yara_hex(id, pattern)` | 创建十六进制字节模式 |
| `yara_text(id, text; nocase, wide, fullword)` | 创建文本模式 |
| `yara_regex(id, pattern; nocase)` | 创建正则表达式模式 |
| `yara_rule(name; strings, condition, tags, metadata)` | 创建规则 |
| `scan(rule, data)` | 根据规则扫描数据 |
| `scan_file(rule, path)` | 根据规则扫描文件 |
| `match_count(result)` | 总匹配计数 |
| `match_count(result, id)` | 指定字符串 ID 的匹配计数 |
| `compile_rules(rules)` | 将规则编译为 RuleSet |
| `scan_with_ruleset(rs, data)` | 使用多个规则进行扫描 |
| `parse_yara(source)` | 将 YARA 语法解析为规则 |
## 许可证
MIT
标签:AMSI绕过, DAST, DNS通配符检测, Julia, PE文件检测, YARA, 二进制扫描, 云安全监控, 云计算, 云资产可视化, 威胁检测, 库, 应急响应, 恶意软件分析, 数字取证, 无依赖, 模式匹配, 结果导出, 网络安全, 自动化脚本, 自动化资产收集, 规则引擎, 速率限制, 隐私保护, 静态分析, 默认DNS解析器