A sandbox/supervisor for python modules.
Project description
secimport
A sandbox/supervisor for python modules.
secimport
can be used to:
- Confine/Restrict specific python modules inside your production environment.
- Open Source, 3rd party from unstrusted sources.
- Audit the flow of your python application at user-space/os/kernel level.
- Run an entire python application under unified configuration
- Like
seccomp
andseccomp-bpf
, without changing your code - Not limited to Linux kernels. Cross platform.
- Like
Requirements
- A python interpreter that was built with
--with-dtrace
.- See INSTALL.md.
Quick Start
For the full list of examples, see EXAMPLES.md.
Shell blocking
# example.py - Executes code upon import;
import os;
os.system('Hello World!');
# production.py - Your production code
from secimport import secure_import
example = secure_import('example', allow_shells=False)
(root) sh-3.2# export PYTHONPATH=$(pwd)/src:$(pwd)/examples:$(pwd):$PYTHONPATH
(root) sh-3.2# python examples/production.py
Successfully compiled dtrace profile: /tmp/.secimport/sandbox_example.d
Killed: 9
- We imported
example
with limited capabilities. - If a syscall like
spawn/exec/fork/forkexec
will be executed- The process will be
kill
ed with-9
signal.
- The process will be
Network blocking
>>> import requests
>>> requests.get('https://google.com')
<Response [200]>
>>> from secimport import secure_import
>>> requests = secure_import('requests', allow_networking=False)
# The next call should kill the process, since networking is not allowed
>>> requests.get('https://google.com')
[1] 86664 killed
Python Shell Interactive Example
Python 3.10.0 (default, May 2 2022, 21:43:20) [Clang 13.0.0 (clang-1300.0.27.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
# Let's import subprocess module, limiting it's syscall access.
>>> import secimport
>>> subprocess = secimport.secure_import("subprocess")
# Let's import os
>>> import os
>>> os.system("ps")
PID TTY TIME CMD
2022 ttys000 0:00.61 /bin/zsh -l
50092 ttys001 0:04.66 /bin/zsh -l
75860 ttys001 0:00.13 python
0
# It worked as expected, returning exit code 0.
# Now, let's try to invoke the same logic using a different module, "subprocess", that was imported using secure_import:
>>> subprocess.check_call('ps')
[1] 75860 killed python
# Damn! That's cool.
- The dtrace profile for the module is saved under:
/tmp/.secimport/sandbox_subprocess.d
:
- The log file for this module is under
/tmp/.secimport/sandbox_subprocess.log
:... (OPENING SHELL using posix_spawn): (pid 75860) (thread 344676) (user 501) (python module: <stdin>) (probe mod=, name=entry, prov=syscall func=posix_spawn) /bin/sh #posix_spawn, (TOUCHING FILESYSTEM): write(140339021606912) from thread 344676 libsystem_kernel.dylib`__fork+0xb _posixsubprocess.cpython-310-darwin.so`do_fork_exec+0x29 _posixsubprocess.cpython-310-darwin.so`subprocess_fork_exec+0x71f python.exe`cfunction_call+0x86 killing... killed.
Useful References
- Examples
- F.A.Q
- Installation
- Mac OS Users - Disabling SIP for dtrace
- Tracing processes
- Using
dtrace
- Tracing the syscalls of a process with pid
12345
dtrace -n 'syscall::: /pid == ($1)/ {@[pid,execname,probefunc]=count()}' 12345
- Tracing the syscalls of a docker container with pid
12345
dtrace -n 'syscall::: /progenyof($1)/ {@[pid,execname,probefunc]=count()}' 12345
- Tracing the syscalls of a process with pid
- Using
strace
- A script to list all your application's syscalls using
strace
.
I contributed it tofirejail
a few years ago: - https://github.com/netblue30/firejail/blob/master/contrib/syscalls.sh
-
wget "https://raw.githubusercontent.com/netblue30/firejail/c5d426b245b24d5bd432893f74baec04cb8b59ed/contrib/syscalls.sh" -O syscalls.sh chmod +x syscalls.sh ./syscalls.sh examples/http_request.py
- A script to list all your application's syscalls using
- Using
- https://www.brendangregg.com/DTrace/DTrace-cheatsheet.pdf
TODO:
- Node support (dtrace hooks)
- Go support (dtrace hooks)
- Allow/Block list configuration
- Create a .yaml configuration per module in the code
- Use secimport to compile that yml
- Create a single dcript policy
- Run an application with that policy using dtrace, without using
secure_import
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
secimport-0.1.0.tar.gz
(11.3 kB
view hashes)
Built Distribution
secimport-0.1.0-py3-none-any.whl
(13.9 kB
view hashes)
Close
Hashes for secimport-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 318e81936465839af27ffafb8f75c545e21cb645ebc474b379044add257c7b2b |
|
MD5 | f72081586348a5d466ad192557880582 |
|
BLAKE2b-256 | d2148cdb3e6771c527888fc2378274e6eb9f6bb21724bc7f5cdcf7022a386aec |