Skip to content

Commit

Permalink
models:file - add options to set results of Siegfried and custom matches
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoCampinoti94 committed Oct 31, 2023
1 parent 9ca8b65 commit 45aa54e
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions acacore/models/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
from acacore.utils.functions import file_checksum
from acacore.utils.functions import get_bof
from acacore.utils.functions import get_eof
from acacore.utils.functions import is_binary

from .base import ACABase
from .identification import Identification
from ..utils.functions import is_binary


class ActionConvert(TypedDict):
Expand Down Expand Up @@ -91,25 +91,35 @@ def from_file(cls, path: Path, root: Optional[Path] = None):
root=root,
)

def identify(self, sf: Siegfried) -> SiegfriedFile:
def identify(self, sf: Siegfried, *, set_match: bool = False) -> SiegfriedFile:
"""Identify the file using `siegfried`.
Args:
sf (Siegfried): A Siegfried class object
set_match (bool): Set results of Siegfried match if True
Returns:
SiegfriedFile: A dataclass object containing the results from the identification
"""
return sf.identify(self.get_absolute_path(self.root)).files[0]

def identify_custom(self, custom_sigs: list[CustomSignature]) -> Optional[CustomSignature]:
result = sf.identify(self.get_absolute_path(self.root)).files[0]
match = result.best_match()
if set_match:
self.puid = match.id if match else None
self.signature = match.format if match else None
self.warning = "; ".join(match.warning) if match else None
return result

def identify_custom(
self, custom_sigs: list[CustomSignature], *, set_match: bool = False,
) -> Optional[CustomSignature]:
"""Uses the BOF and EOF to try to determine a ACAUID for the file.
The custom_sigs list should be found on the `reference_files` repo.
If no match can be found, the method does nothing.
Args:
custom_sigs: A list of the custom_signatures that the file should be checked against
set_match (bool): Set results of match if True
"""
bof = get_bof(self.get_absolute_path(self.root)).hex()
eof = get_eof(self.get_absolute_path(self.root)).hex()
Expand Down Expand Up @@ -137,6 +147,11 @@ def identify_custom(self, custom_sigs: list[CustomSignature]) -> Optional[Custom
match_length = (match_eof.end() - match_eof.start()) if match_eof else 0
signature = sig if match_eof and match_length > signature_length else signature

if set_match:
self.puid = signature.puid if signature else None
self.signature = signature.signature if signature else None
self.warning = None

return signature

def get_absolute_path(self, root: Optional[Path] = None) -> Path:
Expand Down

0 comments on commit 45aa54e

Please sign in to comment.