Skip to content

Commit

Permalink
Merge pull request #14 from IoT-Inspector/1-cache-compiled-yara-rules
Browse files Browse the repository at this point in the history
Cache compiled YARA rules
  • Loading branch information
kissgyorgy authored Nov 23, 2021
2 parents b1bff2e + 414105e commit 03d9ec4
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions unblob/finder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from functools import lru_cache
from pathlib import Path
from typing import Dict, Iterable, List
from typing import Dict, List, Tuple

import yara
from structlog import get_logger
Expand All @@ -18,7 +19,8 @@
"""


def _make_yara_rules(handlers: Iterable[Handler]):
@lru_cache
def _make_yara_rules(handlers: Tuple[Handler, ...]):
all_yara_rules = "\n".join(
_YARA_RULE_TEMPLATE.format(NAME=h.NAME, YARA_RULE=h.YARA_RULE.strip())
for h in handlers
Expand All @@ -31,7 +33,7 @@ def _make_yara_rules(handlers: Iterable[Handler]):
def search_chunks(
handlers: Dict[str, Handler], full_path: Path
) -> List[YaraMatchResult]:
yara_rules = _make_yara_rules(handlers.values())
yara_rules = _make_yara_rules(tuple(handlers.values()))
# YARA uses a memory mapped file internally when given a path
yara_matches: List[yara.Match] = yara_rules.match(str(full_path), timeout=60)

Expand Down

0 comments on commit 03d9ec4

Please sign in to comment.