Skip to content

Commit 70c1c6c

Browse files
authored
1 parent d3273f4 commit 70c1c6c

File tree

12 files changed

+939
-768
lines changed

12 files changed

+939
-768
lines changed

.devcontainer/devcontainer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"name": "Python",
33
"build": {
44
"dockerfile": "Dockerfile",
5-
"context": ".."
5+
"context": ".."
66
},
7+
"postStartCommand": "source /home/nonroot/.venv/bin/activate && python3 -m pip install --disable-pip-version-check -e .",
8+
"postAttachCommand": "source /home/nonroot/.venv/bin/activate",
79
"customizations": {
810
"vscode": {
9-
"extensions": [
10-
"ms-python.python"
11-
],
11+
"extensions": [ "ms-python.python", "github.copilot-chat" ],
1212
"settings": {
1313
"chat.tools.autoApprove": true,
1414
"chat.tools.terminal.autoApprove": { "/.*/": true },

AGENTS.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
This repo contains a security scanner which analyzes Python Pickle files and reports dangerous function calls.
2+
3+
# Code style
4+
5+
After making code changes, lint the code using:
6+
```
7+
black src tests --line-length 140
8+
flake8 src tests --count --show-source
9+
```
10+
11+
# Bug fixes
12+
13+
## Update block-list
14+
15+
The scanner relies on an allow-list called `_safe_globals` and a block-list called `_unsafe_globals` in `src/picklescan/scanner.py`. Those lists need to be updated when bugs or security advisories report detection issues.
16+
17+
To update the block-list, create a sample Pickle file reproing the issue, add a test for it, verify that the test fails, update the block-list, and verify the test passes.
18+
19+
In more details:
20+
21+
Step 1: Update and run `tests/init_data_files.py` to create the sample Pickle file.
22+
23+
First create a `reduce_xxx()` function calling the function to add to the block list. For instance:
24+
```python
25+
def reduce_GHSA_4whj_rm5r_c2v8():
26+
import torch.utils.bottleneck.__main__ as bottleneck_main
27+
28+
return bottleneck_main.run_autograd_prof, (_payload, {})
29+
```
30+
31+
The `reduce_xxx()` function must be self-contained: include `import` statements directly in the function and not at the top of the file (i.e. do not follow the typical Python convention). If a package needs to be intalled, run `python3 -m pip install <package>==<version>` to install it in the current virtual environment, and add `<package>==<version>` in `requirements_extras.txt` for future reference.
32+
33+
In `initialize_pickle_files()`, serialize the `reduce_xxx()` function to a file:
34+
```python
35+
initialize_pickle_file_from_reduce("GHSA-4whj-rm5r-c2v8.pkl", reduce_GHSA_4whj_rm5r_c2v8)
36+
```
37+
38+
Finally run `python3 tests/init_data_files.py` to create the sample file.
39+
40+
Step 2: add code validating the output of the scanner for the sample file. In `tests/test_scanner.py`, add an assert in `test_scan_file_path()`. For instance:
41+
```python
42+
assert_scan("GHSA-4whj-rm5r-c2v8.pkl", [Global("torch.utils.bottleneck.__main__", "run_autograd_prof", SafetyLevel.Dangerous)])
43+
```
44+
45+
Run the test and verify it fails:
46+
```bash
47+
pytest tests -k test_scan_file_path -vv
48+
```
49+
50+
Step 3: add a new entry in dictionary `_unsafe_globals` of `src/picklescan/scanner.py` and rerun the test to verify it passes.

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ twine==4.0.1
22
flake8==5.0.4
33
pytest==7.1.3
44
pytest-cov==3.0.0
5-
requests==2.31.0
6-
aiohttp==3.9.1
5+
requests==2.32.5
6+
aiohttp==3.13.2
77
black==22.8.0
88
numpy>1.24.0,<2.0.0
99
py7zr==0.22.0

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = picklescan
3-
version = 0.0.32
3+
version = 0.0.33
44
author = Matthieu Maitre
55
author_email = [email protected]
66
description = Security scanner detecting Python Pickle files performing suspicious actions

src/picklescan/scanner.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,18 @@ def __str__(self) -> str:
120120
"asyncio": "*",
121121
"bdb": "*",
122122
"commands": "*", # Python 2 precursor to subprocess
123+
"ctypes": "*", # Foreign function interface, can load DLLs, call C functions, manipulate raw memory
123124
"functools": "partial", # functools.partial(os.system, "echo pwned")
124125
"httplib": "*", # Includes http.client.HTTPSConnection()
126+
"numpy.f2py": "*", # Multiple unsafe functions (e.g., getlincoef, _eval_length) that call eval on arbitrary strings
125127
"numpy.testing._private.utils": "*", # runstring() in this module is a synonym for exec()
126128
"nt": "*", # Alias for 'os' on Windows. Includes os.system()
127129
"posix": "*", # Alias for 'os' on Linux. Includes os.system()
128-
"operator": "attrgetter", # Ex of code execution: operator.attrgetter("system")(__import__("os"))("echo pwned")
130+
"operator": {
131+
"attrgetter", # Ex of code execution: operator.attrgetter("system")(__import__("os"))("echo pwned")
132+
"itemgetter",
133+
"methodcaller",
134+
},
129135
"os": "*",
130136
"requests.api": "*",
131137
"runpy": "*", # Includes runpy._run_code
@@ -136,6 +142,7 @@ def __str__(self) -> str:
136142
"sys": "*",
137143
"code": {"InteractiveInterpreter.runcode"},
138144
"cProfile": {"runctx", "run"},
145+
"distutils.file_util": "*", # arbitrary file write via distutils.file_util.write_file()
139146
"doctest": {"debug_script"},
140147
"ensurepip": {"_run_pip"},
141148
"idlelib.autocomplete": {"AutoComplete.get_entity", "AutoComplete.fetch_completions"},
@@ -149,8 +156,9 @@ def __str__(self) -> str:
149156
"pickle": "*",
150157
"_pickle": "*",
151158
"pip": "*",
159+
"pty": "*", # pty.spawn() allows executing arbitrary commands
152160
"profile": {"Profile.run", "Profile.runctx"},
153-
"pydoc": "pipepager", # pydoc.pipepager('help','echo pwned')
161+
"pydoc": "*", # pydoc.locate can import arbitrary modules, pydoc.pipepager allows command execution
154162
"timeit": "*",
155163
"torch._dynamo.guards": {"GuardBuilder.get"},
156164
"torch._inductor.codecache": "compile_file", # compile_file('', '', ['sh', '-c','$(echo pwned)'])
@@ -350,9 +358,14 @@ def _build_scan_result_from_raw_globals(
350358
safe_filter = _safe_globals.get(g.module)
351359
unsafe_filter = _unsafe_globals.get(g.module)
352360

353-
# If the module as a whole is marked as dangerous, submodules are also dangerous
354-
if unsafe_filter is None and "." in g.module and _unsafe_globals.get(g.module.split(".")[0]) == "*":
355-
unsafe_filter = "*"
361+
# If any parent module is marked as dangerous with "*", submodules are also dangerous
362+
if unsafe_filter is None and "." in g.module:
363+
module_parts = g.module.split(".")
364+
for i in range(1, len(module_parts)):
365+
parent_module = ".".join(module_parts[:i])
366+
if _unsafe_globals.get(parent_module) == "*":
367+
unsafe_filter = "*"
368+
break
356369

357370
if "unknown" in g.module or "unknown" in g.name:
358371
g.safety = SafetyLevel.Dangerous
35 Bytes
Binary file not shown.
98 Bytes
Binary file not shown.
95 Bytes
Binary file not shown.
95 Bytes
Binary file not shown.
62 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)