Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate over carved binaries instead of collecting all at once #121

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions malduck/extractor/extract_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import logging
import warnings
from typing import Any, Dict, List, Optional, Type
from typing import Any, Dict, Iterator, List, Optional, Type

from ..procmem import ProcessMemory, ProcessMemoryELF, ProcessMemoryPE
from ..procmem.binmem import ProcessMemoryBinary
Expand Down Expand Up @@ -114,21 +114,19 @@ def match_procmem(self, p: ProcessMemory) -> YaraRulesetMatch:
log.debug("Matched rules: %s", ",".join(list(matches.keys())))
return matches

def carve_procmem(self, p: ProcessMemory) -> List[ProcessMemoryBinary]:
def carve_procmem(self, p: ProcessMemory) -> Iterator[ProcessMemoryBinary]:
"""
Carves binaries from ProcessMemory to try configuration extraction
using every possible address mapping.
"""
binaries = []
for binclass in self.binary_classes:
carved_bins = list(binclass.load_binaries_from_memory(p))
carved_bins = binclass.load_binaries_from_memory(p)
for carved_bin in carved_bins:
log.debug(
f"carve: Found {carved_bin.__class__.__name__} "
f"at offset {carved_bin.regions[0].offset}"
)
binaries += carved_bins
return binaries
yield carved_bin

def push_config(self, config: Config) -> bool:
if not config.get("family"):
Expand Down Expand Up @@ -209,7 +207,7 @@ def push_procmem(
log.debug("No Yara matches.")
return None

binaries = self.carve_procmem(p) if rip_binaries else []
binaries = self.carve_procmem(p) if rip_binaries else iter([])

family = self._extract_procmem(p, matches)
for binary in binaries:
Expand Down
Loading