generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a new setup.py related heuristic in the pypi malware analyz…
…er (#932) New heuristic that checks for the presence of a wheel (.whl) file distributed with the pypi package, passing when a wheel file is present, and failing when it is absent. Signed-off-by: Carl Flottmann <[email protected]>
- Loading branch information
Showing
8 changed files
with
336 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/macaron/malware_analyzer/pypi_heuristics/metadata/wheel_absence.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved. | ||
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. | ||
|
||
"""The heuristic analyzer to check .whl file absence.""" | ||
|
||
import logging | ||
|
||
from macaron.errors import HeuristicAnalyzerValueError | ||
from macaron.json_tools import JsonType | ||
from macaron.malware_analyzer.pypi_heuristics.base_analyzer import BaseHeuristicAnalyzer | ||
from macaron.malware_analyzer.pypi_heuristics.heuristics import HeuristicResult, Heuristics | ||
from macaron.slsa_analyzer.package_registry.pypi_registry import PyPIPackageJsonAsset | ||
|
||
logger: logging.Logger = logging.getLogger(__name__) | ||
|
||
|
||
class WheelAbsenceAnalyzer(BaseHeuristicAnalyzer): | ||
""" | ||
Analyze to see if a .whl file is available for the package. | ||
If a package is distributed with a .whl file, this heuristic passes. Otherwise, the | ||
heuristic fails. | ||
""" | ||
|
||
WHEEL: str = "bdist_wheel" | ||
|
||
def __init__(self) -> None: | ||
super().__init__( | ||
name="wheel_absence_analyzer", | ||
heuristic=Heuristics.WHEEL_ABSENCE, | ||
depends_on=None, | ||
) | ||
|
||
def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicResult, dict[str, JsonType]]: | ||
"""Analyze the package. | ||
Parameters | ||
---------- | ||
pypi_package_json: PyPIPackageJsonAsset | ||
The PyPI package JSON asset object. | ||
Returns | ||
------- | ||
tuple[HeuristicResult, dict[str, JsonType]]: | ||
The result and related information collected during the analysis. | ||
Raises | ||
------ | ||
HeuristicAnalyzerValueError | ||
If there is no release information, or has no most recent version (if queried). | ||
""" | ||
releases = pypi_package_json.get_releases() | ||
if releases is None: # no release information | ||
error_msg = "There is no information for any release of this package." | ||
logger.debug(error_msg) | ||
raise HeuristicAnalyzerValueError(error_msg) | ||
|
||
version = pypi_package_json.component.version | ||
if version is None: # check latest release version | ||
version = pypi_package_json.get_latest_version() | ||
|
||
if version is None: | ||
error_msg = "There is no latest version of this package." | ||
logger.debug(error_msg) | ||
raise HeuristicAnalyzerValueError(error_msg) | ||
|
||
release_files: list[JsonType] = [] | ||
wheel_present: bool = False | ||
|
||
try: | ||
for release_metadata in releases[version]: | ||
if release_metadata["packagetype"] == self.WHEEL: | ||
wheel_present = True | ||
|
||
release_files.append(release_metadata["filename"]) | ||
except KeyError as error: | ||
error_msg = f"The version {version} is not available as a release." | ||
logger.debug(error_msg) | ||
raise HeuristicAnalyzerValueError(error_msg) from error | ||
|
||
if wheel_present: | ||
return HeuristicResult.PASS, {version: release_files} | ||
|
||
return HeuristicResult.FAIL, {version: release_files} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
tests/integration/cases/ajax-requester_pypi_malware_analyzer/policy.dl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved. */ | ||
/* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. */ | ||
|
||
#include "prelude.dl" | ||
|
||
Policy("check-ajax-requester", component_id, "Check ajax-requester artifacts") :- | ||
check_passed(component_id, "mcn_detect_malicious_metadata_1"). | ||
|
||
apply_policy_to("check-ajax-requester", component_id) :- | ||
is_component(component_id, purl), | ||
match("pkg:pypi/ajax-requester", purl). |
22 changes: 22 additions & 0 deletions
22
tests/integration/cases/ajax-requester_pypi_malware_analyzer/test.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved. | ||
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. | ||
|
||
description: | | ||
Analyzing the metadata of a project with unreachable links, frequent releases, and the maintainer | ||
released it shortly after account registration with a suspicious setup.py file, but a wheel file | ||
is present. Macaron should report a pass for such a package. | ||
tags: | ||
- macaron-python-package | ||
|
||
steps: | ||
- name: Run macaron analyze against ajax-requester | ||
kind: analyze | ||
options: | ||
command_args: | ||
- -purl | ||
- pkg:pypi/ajax-requester | ||
- name: Run macaron verify-policy to check the results | ||
kind: verify | ||
options: | ||
policy: policy.dl |
Oops, something went wrong.