Skip to content

Commit

Permalink
Add more suspicious globals
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaitre314 committed Sep 6, 2022
1 parent 7d67d2f commit 1c86244
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/picklescan/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
_log = logging.getLogger("picklescan")

_suspicious_globals = {
"__builtin__": {"eval", "compile", "getattr", "apply"}, # Pickle versions 0, 1, 2 have those function under '__builtin__'
"builtins": {"eval", "compile", "getattr", "apply"}, # Pickle versions 3, 4 have those function under 'builtins'
"__builtin__": {"eval", "compile", "getattr", "apply", "exec", "open", "breakpoint"}, # Pickle versions 0, 1, 2 have those function under '__builtin__'
"builtins": {"eval", "compile", "getattr", "apply", "exec", "open", "breakpoint"}, # Pickle versions 3, 4 have those function under 'builtins'
"webbrowser": "*", # Includes webbrowser.open()
"httplib": "*", # Includes http.client.HTTPSConnection()
"requests.api": "*",
"aiohttp.client": "*",
"nt": "*", # Alias for 'os' on Windows. Includes os.system()
"posix": "*", # Alias for 'os' on Linux. Includes os.system()
"socket": "*",
"subprocess": "*",
"sys": "*",
}

_pickle_file_extensions = {".pkl", ".pickle", ".joblib"}
Expand Down
Binary file added tests/data/malicious7.pkl
Binary file not shown.
Binary file added tests/data/malicious8.pkl
Binary file not shown.
Binary file added tests/data/malicious9.pkl
Binary file not shown.
30 changes: 27 additions & 3 deletions tests/test_scanner.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import http.client
import io
import os
import pickle
import http.client
import pytest
import socket
import subprocess
import sys
import zipfile
import pytest
import requests
import aiohttp
from picklescan.scanner import _http_get, _list_globals, scan_pickle_bytes, scan_zip_bytes,\
Expand Down Expand Up @@ -40,6 +43,21 @@ def __reduce__(self):
return aiohttp.ClientSession, tuple()


class Malicious6:
def __reduce__(self):
return socket.create_connection, (("github.com", 80),)


class Malicious7:
def __reduce__(self):
return subprocess.run, (["ls", "-l"],)


class Malicious8:
def __reduce__(self):
return sys.exit, (0,)


class HTTPResponse:
def __init__(self, status, data=None):
self.status = status
Expand Down Expand Up @@ -138,6 +156,9 @@ def initialize_pickle_files():
initialize_pickle_file(f"{_root_path}/data/malicious4.pickle", Malicious4(), 4)
initialize_pickle_file(f"{_root_path}/data/malicious5.pickle", Malicious5(), 4)
initialize_data_file(f"{_root_path}/data/malicious6.pkl", pickle.dumps(["a", "b", "c"]) + pickle.dumps(Malicious4()))
initialize_pickle_file(f"{_root_path}/data/malicious7.pkl", Malicious6(), 4)
initialize_pickle_file(f"{_root_path}/data/malicious8.pkl", Malicious7(), 4)
initialize_pickle_file(f"{_root_path}/data/malicious9.pkl", Malicious8(), 4)

initialize_zip_file(f"{_root_path}/data/malicious1.zip", "data.pkl", pickle.dumps(Malicious1(), protocol=4))

Expand Down Expand Up @@ -184,10 +205,13 @@ def test_scan_file_path():
assert scan_file_path(f"{_root_path}/data/malicious4.pickle") == (1, 1)
assert scan_file_path(f"{_root_path}/data/malicious5.pickle") == (1, 1)
assert scan_file_path(f"{_root_path}/data/malicious6.pkl") == (1, 1)
assert scan_file_path(f"{_root_path}/data/malicious7.pkl") == (1, 1)
assert scan_file_path(f"{_root_path}/data/malicious8.pkl") == (1, 1)
assert scan_file_path(f"{_root_path}/data/malicious9.pkl") == (1, 1)


def test_scan_directory_path():
assert scan_directory_path(f"{_root_path}/data/") == (16, 12)
assert scan_directory_path(f"{_root_path}/data/") == (19, 15)


def test_scan_url():
Expand Down

0 comments on commit 1c86244

Please sign in to comment.