YARA-X Python Bindings

1.15.0 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates compiling YARA-X rules from a string and then scanning data. It covers both the simple `yara_x.compile()` function and using the `Compiler` object for more advanced scenarios like managing namespaces.

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}")

view raw JSON →