Metadata-Version: 2.5
Name: yaratool
Version: 1.0.0
Summary: Python library to normalize Yara signatures
Project-URL: Homepage, https://github.com/chrislee35/yaratool
Project-URL: Documentation, https://chrislee35.github.io/yaratool/
Author-email: Chris Lee & Friends <github@chrislee.dhs.org>
License-Expression: MIT
License-File: LICENSE
Keywords: malware,yara
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Utilities
Requires-Python: >=3.8
Provides-Extra: dev
Requires-Dist: black; extra == 'dev'
Requires-Dist: flake8; extra == 'dev'
Requires-Dist: pylint; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: yara-python; extra == 'dev'
Provides-Extra: docs
Requires-Dist: furo; extra == 'docs'
Requires-Dist: sphinx; extra == 'docs'
Description-Content-Type: text/markdown

# Overview

YaraTool was created to normalize yara signatures to format the signatures nicely, detect duplicates, and express a specific signature by hash (similar to how we express malware).  The hashing method in this tool is the same as the Ruby Yara-Normalize module.

# Normalizing a signature

The following snippet takes a signature, normalizes it, prints out the pieces of the rule, and provides the "Yara Normalized" hash.  The YNHash is designed to identify yara signatures.

```
import yaratool

if __name__ == "__main__":
    ruletext = """rule DebuggerCheck__API : AntiDebug   DebuggerCheck   {
    meta:
        author="Some dude or dudette" 
            weight =   1
    strings:
        $ ="IsDebuggerPresent"
    condition:
        any of them
}"""
    yr = yaratool.YaraRule(ruletext)
    print(yr.normalize())
    print("Name: %s, Tags: %s, Author: %s" % (yr.name, "&".join(yr.tags), yr.metas['author']))
    print("Strings: ")
    for string in yr.strings:
        print("  %s" % (string))
    print("Condition: ")
    for condition in yr.conditions:
        print("  %s" % (condition))
    print(yr.hash())
```

## Outputs

```
    rule DebuggerCheck__API : AntiDebug DebuggerCheck {
      meta:
        author = "Some dude or dudette"
        weight = 1
      strings:
        $ = "IsDebuggerPresent"
      condition:
        any of them
    }
    Name: DebuggerCheck__API, Tags: AntiDebug&DebuggerCheck, Author: "Some dude or dudette"
    Strings: 
      $ = "IsDebuggerPresent"
    Condition: 
      any of them
    yn01:d28d649e24c37244:d936fceffe
```

# Hashes: yn02 and yn01

`YaraRule.hash()` returns a `yn02:` hash by default. It is computed from a *semantic canonical form* of the rule (see `YaraRule.canonical()`), so rules that detect the same things hash the same however they are written:

* string identifiers do not matter, but which string a condition refers to does; unused strings are ignored;
* modifier order, the implicit `ascii`, `/x/i` vs `nocase`, hex-string case, spacing, wildcard/jump spelling (`[3]` vs `?? ?? ??`), and a plain text string vs the same bytes in hex are all normalized;
* conditions are parsed: `and`/`or` operands are sorted, `any of them`, `1 of them`, `$a or $b` and `for any of them : ($)` are the same, `1KB` is `1024`, `#a > 0` is `$a`, comparisons are oriented, loop variables are renamed.

Names, tags, meta, comments and the `private` modifier of a string do not affect the hash; the `private`/`global` modifiers of the rule do. If a condition uses syntax the parser does not understand, a conservative token-level form is used, which never merges rules that differ.

The legacy `yn01` hash is still available as `rule.hash(version=1)` (CLI: `--hash-version 1`). It cannot tell `$a at 0 and $b` from `$b at 0 and $a`, and treats many equivalent spellings as different.

# Detecting Duplicate Rules

The following code iterates through all the files specified on the command line and counts the number of rules and duplicate rules.  It will display the normalized versions of any duplicate rules.

```
import yaratool
import sys

if __name__ == "__main__":
    count = 0
    duplicates = 0
    drf = yaratool.DuplicateDetector()
    for filename in sys.argv[1:]:
        fh = open(filename, 'r')
        sigrules = fh.read()
        fh.close()
        rules = yaratool.YaraRule.split(sigrules)
        for rule in rules:
            ynhash = rule.hash()
            res = drf.check(rule)
            if res:
                duplicates += 1
                for r in res:
                    print(r.normalize())
                    pass
                print(rule.normalize())
                print()
        count += len(rules)
    print("Count: %d, Duplicates: %d" % (count, duplicates))
```

# Command line

Installing the package provides two console scripts (also reachable via `python -m yaratool`):

```
yaranormalize [-i in_file]... [-o out_file] [-h | -H] [--hash-version 1|2]    # python -m yaratool normalize ...
yara_uniq     [-i in_file]... [-o out_file] [-u | -d] [--hash-version 1|2]    # python -m yaratool uniq ...
```

# Development


```
python -m venv .venv && . .venv/bin/activate
pip install -e '.[dev,docs]'
pytest
ruff check . && black --check . && flake8 && pylint yaratool
sphinx-build -W docs docs/_build/html
```
# License

MIT License; see [LICENSE](LICENSE).
