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

Compute SHA-256 in bundled_binary #471

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Changes from 3 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
34 changes: 26 additions & 8 deletions guarddog/analyzer/metadata/bundled_binary.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from guarddog.analyzer.metadata.detector import Detector
from abc import abstractmethod
from typing import Optional
import os
from functools import reduce
import hashlib
import logging
import os
from typing import Optional

from guarddog.analyzer.metadata.detector import Detector

log = logging.getLogger("guarddog")

Expand All @@ -28,21 +29,38 @@ def detect(
name: Optional[str] = None,
version: Optional[str] = None,
) -> tuple[bool, str]:
def format_file(file: str, kind: str) -> str:
return f"{file} ({kind})"

def sha256(file: str) -> str:
with open(file, "rb") as f:
hasher = hashlib.sha256()
while (chunk := f.read(4096)):
hasher.update(chunk)
return hasher.hexdigest()

log.debug(
f"Running bundled binary heuristic on package {name} version {version}"
)
if not path:
raise ValueError("path is needed to run heuristic " + self.get_name())

bin_files = []
bin_files = {}
for root, _, files in os.walk(path):
for f in files:
kind = self.is_binary(os.path.join(root, f))
path = os.path.join(root, f)
kind = self.is_binary(path)
if kind:
bin_files.append(f"{f} type {kind}")
digest = sha256(path)
if digest not in bin_files:
bin_files[digest] = [format_file(f, kind)]
else:
bin_files[digest].append(format_file(f, kind))
if bin_files:
return True, "Binary file/s detected in package: " + reduce(lambda x, y: f"{x}, {y}", bin_files)
output_lines = '\n'.join(
ikretz marked this conversation as resolved.
Show resolved Hide resolved
f"{digest}: {', '.join(files)}" for digest, files in bin_files.items()
)
return True, f"Binary file/s detected in package:\n{output_lines}"
return False, ""

def is_binary(self, path: str) -> Optional[str]:
Expand Down