gyp is a Go library for parsing YARA rules. It uses the same grammar and lexer files as the original libyara to ensure that lexing and parsing work exactly like YARA. This library produces an Abstract Syntax Tree (AST) for the parsed YARA rules. Additionally, the AST can be serialized as a Protocol Buffer, which facilitate its manipulation in other programming languages.
The example below illustrates the usage of gyp, this a simple program that reads a YARA source file from the standard input, creates the corresponding AST, and writes the rules back to the standard output. The resulting output won't be exactly like the input, during the parsing and re-generation of the rules the text is reformatted and comments are lost.
package main
import (
"log"
"os"
"github.com/VirusTotal/gyp"
)
func main() {
ruleset, err := gyp.Parse(os.Stdin)
if err != nil {
log.Fatalf(`Error parsing rules: %v`, err)
}
if err = ruleset.WriteSource(os.Stdout); err != nil {
log.Fatalf(`Error writing rules: %v`, err)
}
}
apt is assumed in the following example): apt update && apt install \
automake \
bison \
help2man \
m4 \
texinfo \
texlive
go get golang.org/x/tools/cmd/goyaccgo get github.com/pebbe/flexgo/...FLEXGO, pointing out to the flexgo folder in your Go workspace (e.g., $HOME/go/src/github.com/pebbe/flexgo).cd ${FLEXGO} && ./configure && cd -make -C ${FLEXGO} && make -C ${FLEXGO} installThe Makefile includes targets for quickly building the parser and lexer and the data protocol buffer, as well as the y2j and j2y command-line tools:
make grammarmake hexgrammarmake protoy2j tool: make y2jj2y tool: make j2yThis project uses code from yara-parser by Northern-Lights, which is available under the MIT license (see LICENSES_THIRD_PARTIES).