Skip to content

Commit

Permalink
Add runpy to unsafe operators
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpmorgan committed May 28, 2024
1 parent d346153 commit 4b58fae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/picklescan/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def __str__(self) -> str:
"socket": "*",
"subprocess": "*",
"sys": "*",
"runpy": "*", # Includes runpy._run_code
"operator": "attrgetter", # Ex of code execution: operator.attrgetter("system")(__import__("os"))("echo pwned")
"pickle": "*",
"_pickle": "*",
Expand Down
23 changes: 20 additions & 3 deletions tests/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import importlib
import io
import os
import runpy
import pickle
import pytest
import requests
Expand Down Expand Up @@ -83,6 +84,11 @@ def __reduce__(self):
return pickle.loads, (b"I12345\n.",) # Loads the value 12345


class Malicious14:
def __reduce__(self):
return runpy._run_code, ("print('456')",)


class HTTPResponse:
def __init__(self, status, data=None):
self.status = status
Expand Down Expand Up @@ -336,6 +342,9 @@ def initialize_pickle_files():
initialize_pickle_file(
f"{_root_path}/data/malicious13b.pkl", Malicious13(), 4
) # pickle module serialized as _pickle
initialize_pickle_file(
f"{_root_path}/data/malicious14.pkl", Malicious14(), 4
) # runpy

initialize_zip_file(
f"{_root_path}/data/malicious1.zip",
Expand Down Expand Up @@ -552,6 +561,13 @@ def test_scan_file_path():
scan_file_path(f"{_root_path}/data/bad_pytorch.pt"), bad_pytorch
)

malicious14 = ScanResult(
[Global("runpy", "_run_code", SafetyLevel.Dangerous)], 1, 1, 1
)
compare_scan_results(
scan_file_path(f"{_root_path}/data/malicious14.pkl"), malicious14
)


def test_scan_directory_path():
sr = ScanResult(
Expand All @@ -578,6 +594,7 @@ def test_scan_directory_path():
Global("requests.api", "get", SafetyLevel.Dangerous),
Global("builtins", "eval", SafetyLevel.Dangerous),
Global("builtins", "eval", SafetyLevel.Dangerous),
Global("runpy", "_run_code", SafetyLevel.Dangerous),
Global("socket", "create_connection", SafetyLevel.Dangerous),
Global("collections", "OrderedDict", SafetyLevel.Innocuous),
Global("torch._utils", "_rebuild_tensor_v2", SafetyLevel.Innocuous),
Expand All @@ -594,9 +611,9 @@ def test_scan_directory_path():
Global("_pickle", "loads", SafetyLevel.Dangerous),
Global("_codecs", "encode", SafetyLevel.Suspicious),
],
scanned_files=27,
issues_count=25,
infected_files=22,
scanned_files=28,
issues_count=26,
infected_files=23,
scan_err=True,
)
compare_scan_results(scan_directory_path(f"{_root_path}/data/"), sr)
Expand Down

0 comments on commit 4b58fae

Please sign in to comment.