Northern-Lights/yara-parser

GitHub: Northern-Lights/yara-parser

一款采用与 YARA 原版相同语法的 Go 语言库,专注于 YARA 规则集的解析、修改和 JSON 序列化转换。

Stars: 85 | Forks: 9

# yara-parser `yara-parser` 是一个用于操作 YARA 规则集的 Go 库。其核心特性在于使用了与原版 libyara 相同的语法和词法分析器文件,以确保词法分析和解析的工作方式与 YARA 完全一致。这些语法和词法分析器文件经过了修改,以便将数据填充到 Go 数据结构中进行规则集操作,而不是编译规则集用于数据匹配。 借助 `yara-parser`,用户将能够读取 YARA 规则集,并通过编程方式修改元数据、规则名称、规则修饰符、标签、字符串等内容。 该库通过 `y2j` 工具提供了将规则集序列化为 JSON 的功能,以便在其他语言中进行规则操作。类似地,`j2y` 提供了 JSON 到 YARA 的转换功能,但请务必查阅下文的__限制__说明。 ## 安装说明 在执行以下 `go get` 命令时,如果遇到任何问题,可能是由于 Go 版本过旧导致的。本项目使用了 Go 1.10 中引入的新特性。更新版本后,安装即可正常进行。 要一次性安装(或更新)所有内容,可以使用以下命令: `go get -u github.com/Northern-Lights/yara-parser/...` ### `y2j`:YARA 转 JSON 使用以下命令安装用于将 YARA 规则集转换为 JSON 的 `y2j` 命令。 `go get -u github.com/Northern-Lights/yara-parser/cmd/y2j` 当然,这会将 `y2j` 安装到 `$GOPATH/bin` 目录下,因此请确保该路径已添加到您的 `$PATH` 中。 语法和词法分析器文件目前已被冻结,因此无需使用 `goyacc` 和 `flexgo` 重新构建它们。 ### `j2y`:JSON 转 YARA 使用以下命令安装用于将 JSON 转换为 YARA 规则集的 `j2y` 命令。 `go get -u github.com/Northern-Lights/yara-parser/cmd/j2y` ### 语法库 使用以下命令安装语法库,用于反序列化 YARA 规则集而无需安装 `y2j`。 `go get -u github.com/Northern-Lights/yara-parser/grammar` ## `y2j` 用法 `y2j` 的命令行用法如下所示: ``` $ y2j --help Usage of y2j: y2j [options] file.yar options: -indent int Set number of indent spaces (default 2) -o string JSON output file ``` 在实际操作中,`y2j` 会将以下规则集: ``` import "pe" import "cuckoo" include "other.yar" global rule demo : tag1 { meta: description = "This is a demo rule" version = 1 production = false description = "because we can" strings: $string = "this is a string" nocase wide $regex = /this is a regex/i ascii fullword $hex = { 01 23 45 67 89 ab cd ef [0-5] ?1 ?2 ?3 } condition: $string or $regex or $hex } ``` 转换为如下 JSON 输出: ``` { "file": "sample.yar", "imports": [ "pe", "cuckoo" ], "includes": [ "other.yar" ], "rules": [ { "modifiers": { "global": true, "private": false }, "identifier": "demo", "tags": [ "tag1" ], "meta": [ { "Key": "description", "Val": "This is a demo rule" }, { "Key": "version", "Val": 1 }, { "Key": "production", "Val": false }, { "Key": "description", "Val": "because we can" } ], "strings": [ { "id": "$string", "type": 0, "text": "this is a string", "modifiers": { "nocase": true, "ascii": false, "wide": true, "fullword": false, "xor": false, "i": false, "s": false } }, { "id": "$regex", "type": 2, "text": "this is a regex", "modifiers": { "nocase": false, "ascii": true, "wide": false, "fullword": true, "xor": false, "i": true, "s": false } }, { "id": "$hex", "type": 1, "text": " 01 23 45 67 89 ab cd ef [0-5] ?1 ?2 ?3 ", "modifiers": { "nocase": false, "ascii": false, "wide": false, "fullword": false, "xor": false, "i": false, "s": false } } ], "condition": "$string or $regex or $hex" } ] } ``` 请注意,字符串类型如下所示: | 字符串类型 `int` 代码 | 指定类型 | | - | - | | 0 | string | | 1 | hex pair bytes | | 2 | regex | ## Go 用法 在 Go 中处理规则集的示例用法如下所示: ``` package main import ( "fmt" "log" "os" "github.com/Northern-Lights/yara-parser/grammar" ) func main() { input, err := os.Open(os.Args[1]) // Single argument: path to your file if err != nil { log.Fatalf("Error: %s\n", err) } ruleset, err := grammar.Parse(input, os.Stdout) if err != nil { log.Fatalf(`Parsing failed: "%s"`, err) } fmt.Printf("Ruleset:\n%v\n", ruleset) // Manipulate the first rule rule := ruleset.Rules[0] rule.Identifier = "new_rule_name" rule.Modifiers.Global = true rule.Modifiers.Private = false } ``` ## 开发说明 包含的 Dockerfile 将构建一个镜像,用于通过 goyacc 和 flexgo 生成解析器和词法分析器。`Makefile` 中提供了一个 `builder` 目标,可帮助您快速上手。运行以下命令以构建 builder 镜像: `make builder` 这将为您提供一个名为 `yara-parser-builder` 的 Docker 镜像。 当您对语法进行修改时,可以运行 `make grammar`。生成的 .go 文件将输出到 `grammar/` 目录中。 ## 限制 目前,本库无法保证修改后的规则在重新序列化后仍是有效的 YARA 规则集。例如,您可以设置 `rule.Identifier = "123"`,但这在 YARA 中是无效的。此外,添加或删除字符串可能会导致条件失效,因为当前条件仅作为纯文本处理。此外,注释也无法被保留。
标签:DAST, DNS解析, EVTX分析, Go语言, j2y, JSON转换, libyara, y2j, YARA, 云安全监控, 云资产可视化, 威胁情报, 开发者工具, 开源项目, 恶意软件分析, 数据序列化, 日志审计, 程序破解, 网络安全, 规则管理, 规则解析, 规则集操作, 词法分析, 语法解析器, 请求拦截, 隐私保护, 静态分析