appsec.fyi

Python — A Practical Guide

A curated AppSec resource library covering XSS, SQLi, SSRF, IDOR, RCE, XXE, OSINT, and more.

Python: A Practical Guide

Curated and synthesized by . Last updated 2026-08-16. Synthesized from 216 of 216 curated resources. Browse all 216 Python resources →

Problem Framing

Python's widespread adoption across diverse domains, from web development and data science to automation and AI, presents a broad attack surface for security vulnerabilities. Its dynamic nature, rich ecosystem of third-party packages, and ease of use, while beneficial for productivity, also introduce unique security challenges. Understanding these challenges is crucial for application security professionals.

The Python ecosystem faces threats ranging from direct code injection and insecure deserialization to sophisticated supply chain attacks that compromise the integrity of software distribution channels. Vulnerabilities can stem from fundamental language features, insecure standard library usage, or misconfigurations within popular frameworks and libraries. The interconnectedness of packages means a vulnerability in one can have cascading effects across an entire application dependency graph.

Core Mechanics of Python Vulnerabilities

Python's security landscape is shaped by its runtime characteristics and how developers interact with its features and ecosystem. Key areas of concern include its handling of dynamic execution, object serialization, system interactions, and dependency management.

Dynamic code execution is a double-edged sword. Functions like eval() and exec() allow for powerful runtime code generation and execution, but they are prime targets for code injection if they process untrusted input [1][2]. Similarly, the subprocess module and os.system can be exploited for OS command injection if user-supplied data is not properly sanitized before being passed to these functions [1][2][3][4].

Insecure deserialization is a pervasive threat. Python's pickle module, for instance, can execute arbitrary code during deserialization if the input data is crafted maliciously. This is often exploited by leveraging the __reduce__ method, which allows for the execution of arbitrary functions [5][1][6][7][8][9][10][11][12]. Other serialization formats and libraries can also be vulnerable if they don't adequately validate input.

The Python Package Index (PyPI) and the broader supply chain present significant risks. Malicious actors can publish compromised packages through various means, including typosquatting, name confusion, or by directly hijacking legitimate package maintainer accounts [13][14][15][16][17]. These malicious packages can then serve as delivery mechanisms for malware, credential stealers, or backdoors, often exploiting Python's startup hooks (like .pth files) for stealthy persistence [14][15]. Credential theft is a common objective, with packages designed to harvest sensitive information from the environment or application code [18][14][15][16][19][17][20].

Notable Techniques and Vulnerabilities

The Python ecosystem has seen numerous impactful vulnerabilities and attack techniques that application security practitioners should be aware of.

Supply Chain Attacks and Malicious Packages

Supply chain attacks remain a dominant threat vector. Compromised CI/CD pipelines have been used to distribute malware [13][14][15][16][17]. The Ultralytics AI Library, for instance, was targeted via a GitHub Actions compromise, leading to the distribution of a cryptominer [13]. Similarly, the LiteLLM package on PyPI was compromised, with malicious versions distributing credential-harvesting malware [14][15]. The durabletask package was also compromised, delivering an infostealer and worm [16][19]. The JDownloader website experienced a supply chain attack where installers were replaced with Python RAT malware [17]. Even security tooling can be targeted; vulnerabilities in picklescan have been found, allowing for bypasses and RCE [21][22][23].

Attackers leverage various tactics for package compromise:

Insecure Deserialization Leading to RCE

Deserialization vulnerabilities are a constant concern, especially with Python's pickle module.

Command and Code Injection

Improper input validation and insecure use of dynamic execution features are common causes of injection vulnerabilities.

Web Application Vulnerabilities

Common web application security flaws are also prevalent in Python web frameworks.

Secret Management and Data Exfiltration

The improper handling of secrets and sensitive data is a recurring issue.

Detection and Prevention Strategies

Mitigating the risks associated with Python development requires a multi-layered approach, combining secure coding practices, robust tooling, and continuous vigilance.

Secure Coding Practices

Runtime Security and Monitoring

Tooling for Application Security

A robust set of tools can significantly enhance an application security team's ability to identify, assess, and mitigate risks in Python applications.

Static Analysis (SAST)

Static Application Security Testing (SAST) tools analyze source code without executing it, identifying potential vulnerabilities.

Dependency Scanning and Software Composition Analysis (SCA)

Dynamic Analysis (DAST) and Runtime Tools

Secret Scanning

Cryptography Libraries

Recent Developments and Emerging Threats

The Python security landscape is constantly evolving, with new attack vectors and vulnerabilities emerging regularly.

AI/ML Framework Vulnerabilities

The increasing use of AI and machine learning in Python applications introduces new security concerns. Serialization vulnerabilities in AI/ML frameworks are a significant area of risk [6][4]. Malicious model files, such as GGUF files used with SGLang, can exploit Jinja2 SSTI for RCE [4]. Vulnerabilities in libraries like NeMo, Uni2TS, and FlexTok have been found that allow RCE via malicious model metadata, often involving Hydra's instantiate() function [25]. Tools like Prisma AIRS are emerging to identify models leveraging these AI/ML library vulnerabilities [Prisma AIRS source citation needed - assuming this is from a provided summary but not a direct numbered source].

Post-Quantum Cryptography

Research and implementation of post-quantum cryptography (PQC) are becoming more relevant as classical cryptographic algorithms face threats from quantum computing [49]. While not a direct vulnerability, understanding and adopting PQC algorithms is a forward-looking security measure.

Evolving Supply Chain Threats

Attackers are continually refining their methods for compromising the software supply chain. This includes more sophisticated techniques for evading detection, such as embedding executable code within audio files or employing multi-stage malware delivery chains. The discovery of numerous live PyPI tokens highlights the ongoing risks in this area [18].

Exploitation of Header Manipulation

Vulnerabilities like CVE-2026-48710 (BadHost) demonstrate how subtle manipulation of network-level components, like HTTP headers, can lead to significant security bypasses in web frameworks [30].

Where to Go Deeper

For application security professionals focusing on Python, continuous learning and hands-on exploration are key.

By staying informed about Python's evolving threat landscape, understanding its core security mechanics, and leveraging the right tools and practices, application security professionals can effectively protect Python-based applications.

Sources cited in this guide

  1. Code injection in Python: examples and prevention — snyk.io
  2. Command injection in Python: examples and prevention — snyk.io
  3. wapiti-scanner/wapiti: Web vulnerability scanner written in Python3 — github.com
  4. SGLang CVE-2026-5760 (CVSS 9.8) Enables RCE via Malicious GGUF Model Files — thehackernews.com
  5. Leaking internal headers in Flask Ninja with deserialization — eval.blog
  6. CVE-2025-68664: Critical LangChain Flaw Enables Secret Extraction — socradar.io
  7. CVE-2026-22607: Fickling Python RCE Vulnerability — sentinelone.com
  8. CVE-2026-21226: Azure Core Python Library RCE Vulnerability — sentinelone.com
  9. Escalating Deserialization Attacks in Python — frichetten.com
  10. Exploiting Python Pickles - David Hamann — davidhamann.de
  11. How Python Pickle Deserialization Security Exploit Works — securecodingpractices.com
  12. Insecure Deserialization in Python: Attack Techniques and Secure Coding — redfoxsec.com
  13. Ultralytics AI Library Hacked via GitHub for Cryptomining — wiz.io
  14. Three’s a Crowd: TeamPCP trojanizes LiteLLM in Continuation of Campaign — wiz.io
  15. How a Poisoned Security Scanner Became the Key to Backdooring LiteLLM — snyk.io
  16. durabletask: TeamPCP's Latest PyPi Compromise — wiz.io
  17. JDownloader Website Supply Chain Attack: Installers Replaced with Python RAT Malware (May 2026) — rescana.com
  18. Hunting Leaked PyPI Tokens: 62 Live, 125 Packages Exposed — blog.gitguardian.com
  19. Microsoft's durabletask package on PyPi Compromised. Mini Shai Hulud attacks again... again! — aikido.dev
  20. Script Injection and Data Theft: Python Data Analysis Tool Compromised — heise.de
  21. Picklescan Allows RCE via Malicious Pickle File — github.com
  22. Exposing 4 Critical Vulnerabilities in Python PickleScan | Sonatype — sonatype.com
  23. PyTorch Users at Risk: 3 Zero-Day PickleScan Vulnerabilities | JFrog — jfrog.com
  24. Malicious PyPI Packages Deliver SilentSync RAT — zscaler.com
  25. Compromised LiteLLM PyPI Package Delivers Credential Stealer — sonatype.com
  26. Marimo RCE Flaw CVE-2026-39987 Exploited Within 10 Hours of Disclosure — thehackernews.com
  27. Python Software Foundation - Python 3.11.0a3 to 3.15.0b2 — bishopfox.com
  28. Understanding and mitigating the Jinja2 XSS vulnerability (CVE-2024-22195) — snyk.io
  29. Critical SQL Injection Vulnerability in Django (CVE-2025-64459) — endorlabs.com
  30. BadHost - One character and your AI agent switches sides — korben.info
  31. How To Keep A Secret in Python Apps — talkpython.fm
  32. New Linux 'Copy Fail' Vulnerability Enables Root Access on Major Distributions — thehackernews.com
  33. Insecure Deserialization in Python | Semgrep — semgrep.dev
  34. Mastering Python virtual environments: A complete guide to venv, Docker, and securing your code — snyk.io
  35. How to secure Python Flask applications — snyk.io
  36. 10 Dimensions of Python Static Analysis — snyk.io
  37. Snyk and uv, Better Together — snyk.io
  38. Django Security Best Practices: A Comprehensive Guide for Software Engineers - Corgea - Home — corgea.com
  39. Running Untrusted Python Code — healeycodes.com
  40. A Python prompt into a running process: debugging with Manhole — pythonspeed.com
  41. Bandit Python: Free SAST in 10 Seconds (2026 Review) — appsecsanta.com
  42. Bandit: Python Static Application Security Testing Guide — dev.to
  43. HN Security - My Semgrep C/C++ ruleset is ready for prime time again — hnsecurity.it
  44. Python SAST Tools: Free & Paid Solutions for Secure Code Analysis — bito.ai
  45. GitHub - wapiti-scanner/wapiti: Web vulnerability scanner written in Python3 — github.com
  46. pwviptbl/ProxyHunter: Aplicação Python com interface gráfica que permite configurar regras de interceptação para modificar parâmetros de requisições HTTP. Quando o navegador envia uma requisição para uma rota configurada, o proxy intercepta, modifica apenas os parâmetros especificados e encaminha a requisição mantendo todos os outros parâmetros originais. — github.com
  47. GitHub - xnl-h4ck3r/knoxnl: This is a python wrapper around the amazing KNOXSS API by Brute Logic — github.com
  48. GitHub - danialhalo/SqliSniper: Advanced Time-based Blind SQL Injection fuzzer for HTTP Headers — github.com
  49. Shipping post-quantum cryptography to Python — blog.trailofbits.com
  50. File encryption in Python: An in-depth exploration of symmetric and asymmetric techniques — snyk.io
  51. Symmetric vs. asymmetric encryption: Practical Python examples — snyk.io
  52. LiteLLM PyPI Package Compromised in TeamPCP Supply Chain Attack — bleepingcomputer.com
📚 This guide is synthesized from the full text of resources curated in the Python library, and refreshed as new material is added.