Validates yara rules and tries to repair the broken ones.
- Python 2.7+ or 3.3+
- yara and yara-python (PR VirusTotal/yara-python#58 and VirusTotal/yara#727 are recommended because they support include_callback, allowing use without requiring disk write access)
sudo python3 setup.py installsudo python setup.py installimport yara_validator
validator = yara_validator.YaraValidator(auto_clear=False)
validator.add_rule_source(u'rule FirstRule{condition: true}', 'namespace_1','first.yara')
validator.add_rule_source(u'include "first.yara" rule SecondRule{condition: true}')
validator.add_rule_file('/path/to/third.yara','namespace_1')
valid, broken, repaired = validator.check_all()
print(===== VALID RULES =====)
for rule in valid:
print(u'{}'.format(rule.source))
print(===== BROKEN RULES =====)
for rule in broken:
print(u'{}'.format(rule.source))
print(===== REPAIRED RULES =====)
for rule in repaired:
print(u'{}'.format(rule.source))
validator.clear_tmp()Optional parameters for YaraValidator.__init__():
disk_buffering: if set to True, allows the tool to use a temporary directory to copy sources and files before validation (requires write access to that directory). If set to False, nothing will be written to disk (requires a yara version supporting include_callback). If not set, will default to False if your yara version supports it, True otherwise.tmp_dir: ifdisk_bufferingis activated, forces the location of the temporary directory. Defaults to OS's temp.auto_clear: ifdisk_bufferingis activated, deletes the temporary directory once theYaraValidatorobject is destroyed. Defaults to False. Manual deletion can be done with clear_tmp().
check_all() can take one optional boolean parameter. If set to True, the suggested repairs will be automatically accepted: the repaired sources will be used instead of the original ones if any other rules includes them. Setting this parameter to True may lead to rules not behaving as expected..
This function returns three lists: the valid rules, the broken rules and the repaired rules.
Rules in the list are instances of YaraRule with the following properties:
source: source codenamespace: rules namespaceinclude_name: name usable in Yaraincludedirectivesstatus:YaraRule.STATUS_UNKNOWN,YaraRule.STATUS_VALID,YaraRule.STATUS_BROKENorYaraRule.STATUS_REPAIREDerror_data: ifSTATUS_BROKENorSTATUS_REPAIRED, contains the error messagerepaired_source: ifSTATUS_REPAIRED, contains a YaraRule with the repairedsourceandSTATUS_VALID