yara-python Documentation
repository·master·Indexed 20 days ago
https://github.com/virustotal/yara-pythonPython 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.
What's inside yara-python
- yara-python is a Python binding for the YARA library. It allows Python programs to utilize all YARA features, including compiling, saving, and loading rules, as well as scanning files, strings, and processes.
Install yara-python via pip
masterThe simplest method to install the library is using
pip.$ pip install yara-pythonBuild yara-python with dynamic linking
masterIf you prefer to link dynamically against a shared
libyaralibrary instead of static linking, use the--dynamic-linkingflag during the build process.Requirement: You must have already built and installed the core
YARAlibrary separately on your system before attempting this build.$ python setup.py build --dynamic-linkingInstall yara-python from source
masterYou can build and install from source. When cloning the repository, you must use the
--recursiveflag to ensure thelibyarasubproject (the core YARA library) is downloaded.By default, these methods link
libyarastatically intoyara-python.$ git clone --recursive https://github.com/VirusTotal/yara-python $ cd yara-python $ python setup.py build $ sudo python setup.py installBasic usage example: Compiling and matching rules
masterThis 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