Skip to main content

Parse YARA rules.

Project description

Build Status Documentation Status Code Health Test Coverage PyPi Version

Parse YARA rules into a dictionary representation.

Plyara is a script and library that lexes and parses a file consisting of one more YARA rules into a python dictionary representation. The goal of this tool is to make it easier to perform bulk operations or transformations of large sets of YARA rules, such as extracting indicators, updating attributes, and analyzing a corpus. Other applications include linters and dependency checkers.

Plyara leverages the Python module PLY for lexing YARA rules.

This is a community-maintained fork of the original plyara by 8u1a. The “plyara” trademark is used with permission.

Installation

Install with pip:

pip install plyara

Usage

Use the plyara Python library in your own applications:

>>> import plyara
>>> parser = plyara.Plyara()
>>> mylist = parser.parse_string('rule MyRule { strings: $a="1" \n condition: false }')
>>>
>>> import pprint
>>> pprint.pprint(mylist)
[{'condition_terms': ['false'],
  'raw_condition': 'condition: false',
  'raw_strings': 'strings: $a="1" \n',
  'rule_name': 'MyRule',
  'start_line': 1,
  'stop_line': 2,
  'strings': [{'name': '$a', 'value': '"1"'}]}]
>>>

Or, use the included plyara script from the command line:

$ plyara -h
usage: plyara.py [-h] [--log] FILE

Parse YARA rules into a dictionary representation.

positional arguments:
  FILE        File containing YARA rules to parse.

optional arguments:
  -h, --help  show this help message and exit
  --log       Enable debug logging to the console.

The command-line tool will print valid JSON output when parsing rules:

$ cat example.yar
rule silent_banker : banker
{
    meta:
        description = "This is just an example"
        thread_level = 3
        in_the_wild = true
    strings:
        $a = {6A 40 68 00 30 00 00 6A 14 8D 91}
        $b = {8D 4D B0 2B C1 83 C0 27 99 6A 4E 59 F7 F9}
        $c = "UVODFRYSIHLNWPEJXQZAKCBGMT"
    condition:
        $a or $b or $c
}

$ plyara example.yar
[
    {
        "condition_terms": [
            "$a",
            "or",
            "$b",
            "or",
            "$c"
        ],
        "metadata": {
            "description": "This is just an example",
            "in_the_wild": "true",
            "thread_level": "3"
        },
        "raw_condition": "condition:\n        $a or $b or $c\n",
        "raw_meta": "meta:\n        description = \"This is just an example\"\n        thread_level = 3\n        in_the_wild = true\n    ",
        "raw_strings": "strings:\n        $a = {6A 40 68 00 30 00 00 6A 14 8D 91}\n        $b = {8D 4D B0 2B C1 83 C0 27 99 6A 4E 59 F7 F9}\n        $c = \"UVODFRYSIHLNWPEJXQZAKCBGMT\"\n    ",
        "rule_name": "silent_banker",
        "start_line": 1,
        "stop_line": 13,
        "strings": [
            {
                "name": "$a",
                "value": "{6A 40 68 00 30 00 00 6A 14 8D 91}"
            },
            {
                "name": "$b",
                "value": "{8D 4D B0 2B C1 83 C0 27 99 6A 4E 59 F7 F9}"
            },
            {
                "name": "$c",
                "value": "\"UVODFRYSIHLNWPEJXQZAKCBGMT\""
            }
        ],
        "tags": [
            "banker"
        ]
    }
]

Migration

If you used an older version of plyara, and want to migrate to this version, there will be some changes required. Most importantly, the parser object instantiation has changed. It was:

# Old style - don't do this!
import plyara.interp as interp
rules_list = interp.parseString(open('myfile.yar').read())

But is now:

# New style - do this instead!
import plyara
parser = plyara.Plyara()
rules_list = parser.parse_string(open('myfile.yar').read())

The existing parsed keys have stayed the same, and new ones have been added.

When reusing a parser for multiple rules and/or files, be aware that imports are now shared across all rules - if one rule has an import, that import will be added to all rules in your parser object.

Contributing

  • If you find a bug, or would like to see a new feature, Pull Requests and Issues are always welcome.

  • By submitting changes, you agree to release those changes under the terms of the LICENSE.

  • Writing passing unit tests for your changes, while not required, is highly encouraged and appreciated.

Discussion

  • You may join our IRC channel on irc.freenode.net #plyara

  • Additionally, project developers can join our slack http://plyara.slack.com (If you need an invite, please ask in the IRC channel.)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plyara-1.2.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

plyara-1.2.2-py2.py3-none-any.whl (15.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file plyara-1.2.2.tar.gz.

File metadata

  • Download URL: plyara-1.2.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.22.0 CPython/2.7.14

File hashes

Hashes for plyara-1.2.2.tar.gz
Algorithm Hash digest
SHA256 965b957c189fda16e9c7ca4d38cd7f34b9a6e8d4f8f474928cf00245c6f1ee97
MD5 eb80bd3ee9e4e2da768da55cbff7a05b
BLAKE2b-256 81fa81af3840d554b3d2a8c1567da504e9bc3dead6719e18f8332ea5a6fad1f5

See more details on using hashes here.

File details

Details for the file plyara-1.2.2-py2.py3-none-any.whl.

File metadata

  • Download URL: plyara-1.2.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/38.5.1 requests-toolbelt/0.8.0 tqdm/4.22.0 CPython/2.7.14

File hashes

Hashes for plyara-1.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bc61f1dfff7e29416a8e701ac7c233f3227143b72f9a90c67d44933a5e5dca8b
MD5 f34c73fe677f12a03eb695db1f0d05b9
BLAKE2b-256 7559c7512c6269ba479f5325a23b124702b6063484ce15ffa1d74836566d625c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page