Portable pyenv Setup for Python Vulnerability Research
This guide describes a repeatable, cross‑Linux workflow for setting up isolated Python environments with pyenv for security and CVE research. It assumes basic terminal familiarity and a non‑privileged user account.
Overview
This guide has three goals:
Make environments reproducible across machines and distros.
Keep each target’s tooling isolated from others.
Provide a standard pattern you can reuse for every Python security research project.
The workflow:
Install system build dependencies (per Linux family).
Install pyenv and pyenv‑virtualenv.
Use a standard “research environment template” for each target project.
Prerequisites
A Linux host (VM or physical), using one of:
Debian / Ubuntu
Fedora / RHEL / CentOS
Arch / Manjaro
A regular non‑root user account with
sudoaccess.Basic familiarity with a shell (
bashorzsh).
For security work, using a dedicated research VM or container is strongly recommended.
Step 1: Install OS Build Dependencies
Install the libraries required to build Python from source. Run the block that matches your distribution family.
Debian / Ubuntu
Fedora / RHEL / CentOS
For Fedora or newer RHEL/CentOS that use dnf:
For older RHEL/CentOS that use yum, replace dnf with yum in the commands above.
Arch / Manjaro
If a package name is missing on your specific distro/version, install the closest *-dev or *-devel equivalent for:
OpenSSL
zlib
bz2
readline
sqlite
ncurses
tk
libffi
lzma (xz)
Step 2: Install pyenv (Any Linux)
This section is distro‑agnostic.
Install pyenv with the official installer:
Add pyenv to your shell startup file.
For Bash (
~/.bashrc):For Zsh (
~/.zshrc):Reload your shell:
Confirm installation:
If this prints a version, pyenv is installed correctly.
Step 3: Standard Research Environment Template
Use the same pattern for each Python target you analyze. Only update a few variables per project.
Template Variables
Pick values per target:
TARGET_NAMEshould match the project or repo.PY_VERSIONis the Python version you want to test (you can repeat this for multiple versions).ENV_NAMEcombines both for clarity.WORK_ROOTis a single directory where all security work lives.
Environment Creation Script
Run this block in a shell after setting the variables:
At this point:
Entering
"$WORK_ROOT/$TARGET_NAME"will automatically select"$ENV_NAME"(due topyenv local).The environment contains a consistent set of security and dev tools.
You can now clone and install the target project into this environment.
Step 4: Using the Environment for a Target Project
Once the environment is ready:
Go to the workspace:
Clone the target repository into this directory (example):
Ensure the correct environment is active:
Install project dependencies (examples):
(Adjust filenames according to the project.)
From here, run tests, static analysis tools, proof‑of‑concept scripts, and exploit code in this environment without affecting other projects.
Step 5: Rebuilding an Environment Later
On a new machine or VM:
Repeat Step 1 and Step 2 for the new system.
Recreate the environment with the same variables:
Rebuild the environment:
If you keep tools-baseline.txt, project-requirements.txt, and the scripts in version control (for example, a private “security-envs” or “research-environments” repo), you can reconstruct setups with high fidelity.
Python Security tooling
Alongside pyenv and virtualenv, a base Python tooling set that you can install into each project’s env is:
Static analysis / SAST:
bandit,pip-audit, and optionallysafetyfor dependency checks, plusmypyfor type checking.Testing and coverage:
pytest,coverage, andhypothesisfor property‑based testing and fuzz‑like input generation.General dev utilities:
black,isort,flake8orruffto keep your own scripts consistent and easier to maintain across projects.
Good Practices for Security Research
Use dedicated VMs or containers per project or per group of related projects.
Avoid using the system Python for any research tasks.
Snapshot your VM after completing this setup so future projects can start from a clean baseline.
Prefer non‑privileged users and minimal permissions, especially when running proof‑of‑concept exploits.
Last updated