avilum/secimport
GitHub: avilum/secimport
一个基于eBPF的Python模块级沙箱工具,可在Linux和macOS上为每个Python模块强制执行系统调用限制。
Stars: 238 | Forks: 18
# secimport


[](https://github.com/avilum/secimport/actions/workflows/python-publish.yml)
## Python 应用程序的模块级沙箱化
secimport 是一个基于 eBPF 的安全工具包,可对每个 Python 模块强制执行系统调用限制,为您的应用程序安全配置文件提供细粒度控制。将其视为 Linux 上的 seccomp-bpf,但在 Python 模块级别运行。
[
](https://star-history.com/#avilum/secimport&Date)
## 主要功能
- **模块级安全**:为每个 Python 模块定义并强制执行系统调用限制
- **自动分析**:跟踪您的应用程序以创建定制安全配置文件
- **多种执行模式**:在策略违规时记录、停止或终止进程
- **生产就绪**:借助 eBPF 实现极低的性能影响
- **供应链保护**:降低来自易受攻击依赖项的风险
## 快速开始
### 使用 Docker(推荐)
```
git clone https://github.com/avilum/secimport.git
cd secimport/docker
./build.sh && ./run.sh
```
命令行:
```
secimport --help
Usage: python -m secimport.cli [OPTIONS] COMMAND [ARGS]...
secimport is a comprehensive toolkit designed to enable the tracing, construction, and execution of secure Python runtimes. It leverages USDT probes and eBPF/DTrace technologies to enhance overall security measures.
WORKFLOW: 1. secimport trace / secimport shell 2. secimport build 3. secimport run
QUICKSTART: $ secimport interactive
For more details, please see https://github.com/avilum/secimport/wiki/Command-Line-Usage
╭─ Options ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --install-completion Install completion for the current shell. │
│ --show-completion Show completion for the current shell, to copy it or customize the installation. │
│ --help Show this message and exit. │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ trace Trace a Python process using an entrypoint or interactive shell. │
│ shell Alias for 'trace'. │
│ trace-pid Trace a running process by PID. │
│ build Build a sandbox profile from a trace log. │
│ run Run a Python process inside the sandbox. │
│ interactive Run secimport in interactive mode. │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
```
## 创建您的第一个沙箱
```
secimport interactive
# 在打开的 Python shell 中:
>>> secimport trace # Start tracing
>>> import requests # Perform actions you want to profile
>>> # Press CTRL+D to stop tracing
>>> secimport build # Build sandbox from trace
>>> secimport run # Run with enforcement
```
## 高级用法
### 命令行选项
```
secimport trace # Trace a new Python process
secimport trace_pid # Trace an existing process
secimport build # Build sandbox from trace
secimport run [options] # Run with enforcement
```
### 执行模式
```
# 违规时停止
secimport run --stop_on_violation=true
# 违规时终止
secimport run --kill_on_violation=true
```
### Python API
```
import secimport
# 将标准导入替换为安全导入
requests = secimport.secure_import('requests', allowed_syscalls=['open', 'read', ...])
```
## 手动安装
1. 安装带有 USDT 探针的 Python:
# 使用 --enable-dtrace 配置 Python
# 详细说明请参阅我们的 wiki
2. 安装支持的后端(eBPF 或 DTrace)
# Ubuntu/Debian
apt-get install bpftrace
# 对于其他平台,请参阅我们的安装 wiki
3. 安装 secimport
pip install secimport
## 使用 nsjail 的 seccomp-bpf 支持
除了 secimport 构建的沙箱之外,
`secimport build` 命令会为您的跟踪代码创建一个带有 seccomp 配置文件的 nsjail 沙箱。
`nsjail` 可以在 linux 上启用带有 seccomp 的命名空间沙箱化 `secimport` 会自动生成可执行的 bash 脚本,用于生成与 `nsjail` 配合使用的 seccomp 配置文件。 它可用于限制整个 python 进程的系统调用,作为另一层防御。 ## 文档 - [安装指南](https://github.com/avilum/secimport/wiki/Installation) - [命令行用法](https://github.com/avilum/secimport/wiki/Command-Line-Usage) - [API 参考](https://github.com/avilum/secimport/wiki/Python-API) - [示例沙箱](https://github.com/avilum/secimport/wiki/Sandbox-Examples) ## 了解更多 ### 技术资源 - https://www.oligo.security/ - [演讲:PyCon 上的 secimport](https://www.youtube.com/watch?v=6DJNQtBJvLA) - [演讲:BSides 上的 secimport](https://youtu.be/nRV0ulYMsxU?t=1257) - 博客文章: - [secimport + DTrace](https://infosecwriteups.com/sandboxing-python-modules-in-your-code-1e590d71fc26?source=friends_link&sk=5e9a2fa4d4921af0ec94f175f7ee49f9) - [secimport + eBPF + PyTorch](https://infosecwriteups.com/securing-pytorch-models-with-ebpf-7f75732b842d?source=friends_link&sk=14d8db403aaf66724a8a69b4dea24e12) - [secimport + eBPF + FastAPI](https://avi-lumelsky.medium.com/secure-fastapi-with-ebpf-724d4aef8d9e?source=friends_link&sk=b01a6b97ef09003b53cd52c479017b03) ## 贡献 我们欢迎贡献!请参阅我们的[贡献指南](https://github.com/avilum/secimport/blob/master/docs/CONTRIBUTING.md)了解详情。 ## 许可证 本项目根据 MIT 许可证授权 - 有关详细信息,请参阅 [LICENSE](LICENSE) 文件。
`secimport build` 命令会为您的跟踪代码创建一个带有 seccomp 配置文件的 nsjail 沙箱。
`nsjail` 可以在 linux 上启用带有 seccomp 的命名空间沙箱化 `secimport` 会自动生成可执行的 bash 脚本,用于生成与 `nsjail` 配合使用的 seccomp 配置文件。 它可用于限制整个 python 进程的系统调用,作为另一层防御。 ## 文档 - [安装指南](https://github.com/avilum/secimport/wiki/Installation) - [命令行用法](https://github.com/avilum/secimport/wiki/Command-Line-Usage) - [API 参考](https://github.com/avilum/secimport/wiki/Python-API) - [示例沙箱](https://github.com/avilum/secimport/wiki/Sandbox-Examples) ## 了解更多 ### 技术资源 - https://www.oligo.security/ - [演讲:PyCon 上的 secimport](https://www.youtube.com/watch?v=6DJNQtBJvLA) - [演讲:BSides 上的 secimport](https://youtu.be/nRV0ulYMsxU?t=1257) - 博客文章: - [secimport + DTrace](https://infosecwriteups.com/sandboxing-python-modules-in-your-code-1e590d71fc26?source=friends_link&sk=5e9a2fa4d4921af0ec94f175f7ee49f9) - [secimport + eBPF + PyTorch](https://infosecwriteups.com/securing-pytorch-models-with-ebpf-7f75732b842d?source=friends_link&sk=14d8db403aaf66724a8a69b4dea24e12) - [secimport + eBPF + FastAPI](https://avi-lumelsky.medium.com/secure-fastapi-with-ebpf-724d4aef8d9e?source=friends_link&sk=b01a6b97ef09003b53cd52c479017b03) ## 贡献 我们欢迎贡献!请参阅我们的[贡献指南](https://github.com/avilum/secimport/blob/master/docs/CONTRIBUTING.md)了解详情。 ## 许可证 本项目根据 MIT 许可证授权 - 有关详细信息,请参阅 [LICENSE](LICENSE) 文件。
标签:dependency-security, Docker镜像, ebpf-sandbox, Hpfeeds, macOS安全, seccomp, syscall-filtering, 性能分析, 模块级控制, 沙箱, 系统调用, 运行时保护, 进程隔离, 逆向工具