YARA-X Python Bindings
YARA-X is a rewrite of YARA in Rust, designed for faster, safer, and more user-friendly pattern matching, primarily for malware research. It aims to replace the original YARA as the default tool. This library provides official Python bindings for YARA-X, supporting Python 3.9+ on Linux, macOS, and Windows. Releases are frequent, with new versions often appearing monthly.
Warnings
- breaking YARA-X enforces stricter regular expression syntax compared to the original YARA. Certain constructs that YARA previously accepted (e.g., invalid escape sequences treated as literals, unescaped special characters inferred from context) will now raise compilation errors in YARA-X.
- gotcha YARA-X is a completely new implementation (in Rust) and is not a drop-in replacement for the `yara-python` library. The Python module is imported as `yara_x`, not `yara`. Users migrating from `yara-python` will need to update import statements and be aware of API differences, though the core compilation and scanning workflow is similar.
- gotcha The behavior of the `strings` field in the `Match` object significantly changed in `yara-python` versions 4.3.0 and later (from an array of tuples to `yara.StringMatch` objects). While this is specific to `yara-python`, users familiar with that library's older API might incorrectly expect similar `Match` object structures or behaviors when adapting to YARA-X. YARA-X's match result structure is different.
Install
-
pip install yara-x
Imports
- yara_x
import yara_x
Quickstart
import yara_x
rules_source = '''
rule example_rule {
strings:
$a = "foobar"
condition:
$a
}
'''
# Compile the rules
rules = yara_x.compile(rules_source)
# Scan data
data_to_scan = b"This is some data containing foobar for testing."
results = rules.scan(data_to_scan)
if results:
print(f"Matches found: {results}")
else:
print("No matches.")
# Example with a Compiler object for more complex scenarios
compiler = yara_x.Compiler()
compiler.add_source(rules_source, origin="my_rules")
compiled_rules_obj = compiler.build()
scan_results_obj = compiled_rules_obj.scan(b"Another foobar string.")
if scan_results_obj:
print(f"Matches found with Compiler: {scan_results_obj}")