yara-python Documentation

repository·master·Indexed 20 days ago

https://github.com/virustotal/yara-python

Python bindings for the YARA rule engine, allowing developers to perform pattern matching and malware identification. It provides functionality to compile, save, and load rules, as well as scan files, strings, and processes.

Tokens
626
Snippets
4
Records
5
Agent score
23%

What's inside yara-python

  1. Build yara-python with dynamic linking

    master

    If you prefer to link dynamically against a shared libyara library instead of static linking, use the --dynamic-linking flag during the build process.

    Requirement: You must have already built and installed the core YARA library separately on your system before attempting this build.

    $ python setup.py build --dynamic-linking
  2. Install yara-python from source

    master

    You can build and install from source. When cloning the repository, you must use the --recursive flag to ensure the libyara subproject (the core YARA library) is downloaded.

    By default, these methods link libyara statically into yara-python.

    $ git clone --recursive https://github.com/VirusTotal/yara-python
    $ cd yara-python
    $ python setup.py build
    $ sudo python setup.py install
  3. Basic usage example: Compiling and matching rules

    master

    This example demonstrates how to compile a YARA rule from a string and perform a match against a data string, accessing rule metadata and string instance details.

    >>> import yara
    >>> rule = yara.compile(source='rule foo: bar {strings: $a = "lmn" condition: $a}')
    >>> matches = rule.match(data='abcdefgjiklmnoprstuvwxyz')
    >>> print(matches)
    [foo]
    >>> print(matches[0].rule)
    foo
    >>> print(matches[0].tags)
    ['bar']
    >>> print(matches[0].strings)
    [$a]
    >>> print(matches[0].strings[0].identifier)
    $a
    >>> print(matches[0].strings[0].instances)
    [lmn]
    >>> print(matches[0].strings[0].instances[0].offset)
    10
    >>> print(matches[0].strings[0].instances[0].matched_length)
    3