Usage¶
Install¶
$ pip install yaratool
Normalizing a signature¶
import yaratool
ruletext = """rule DebuggerCheck__API : AntiDebug DebuggerCheck {
meta:
author="Some dude or dudette"
weight = 1
strings:
$ ="IsDebuggerPresent"
condition:
any of them
}"""
yr = yaratool.YaraRule(ruletext)
print(yr.normalize())
print(yr.hash()) # yn01:d28d649e24c37244:d936fceffe
Hashes¶
YaraRule.hash() returns a yn02 hash computed from the rule’s semantic canonical form
(see yaratool.YaraRule.canonical() and yaratool.canonical). Rules that detect the
same things hash the same, however they are written:
string identifiers do not matter, but which string a condition refers to does;
modifier order, the implicit
ascii,/x/ivsnocase, hex spelling, wildcard and jump forms, and a plain text string vs the same bytes in hex are normalized;conditions are parsed and rewritten:
and/oroperands are sorted,any of them,1 of themand$a or $bagree,1KBis1024,#a > 0is$a, and loop variables are renamed.
Names, tags, meta and comments are ignored. The legacy yn01 hash remains available as
rule.hash(version=1).
Detecting duplicate rules¶
drf = yaratool.DuplicateDetector()
for rule in yaratool.YaraRule.split(open("rules.yar").read()):
for earlier in drf.check(rule):
print(earlier.normalize())