Skip to content

Commit

Permalink
Deduplicate procmems by sha256 hash
Browse files Browse the repository at this point in the history
  • Loading branch information
msm-code committed Apr 4, 2024
1 parent 7a801b8 commit d7407af
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions malduck/procmem/binmem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from abc import ABCMeta, abstractmethod
from typing import Iterator, List, Optional, Type, TypeVar
from hashlib import sha256

from .procmem import ProcessMemory, ProcessMemoryBuffer
from .region import Region
Expand Down Expand Up @@ -78,10 +79,18 @@ def load_binaries_from_memory(cls: Type[T], procmem: ProcessMemory) -> Iterator[
In previous versions it was done by extractor, so it was working only
if memory-aligned version was also "valid".
"""
seen_hashes = set()
if cls.__magic__ is None:
raise NotImplementedError()
for binary_va in procmem.findv(cls.__magic__):
binary_procmem_dmp = cls.from_memory(procmem, base=binary_va)

# Deduplicate procmems by the content hash
next_hash = sha256(binary_procmem_dmp.m).digest()
if next_hash in seen_hashes:
continue
seen_hashes.add(next_hash)

if binary_procmem_dmp.is_valid():
yield binary_procmem_dmp
binary_procmem_img = binary_procmem_dmp.image
Expand Down

0 comments on commit d7407af

Please sign in to comment.