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-07-01. Synthesized from 237 of 237 curated resources. Browse all 237 Python resources →

Problem Framing

Python's ubiquity as a scripting language, web framework backend, and data science tool positions it as a prime target for attackers seeking to compromise systems. Its ease of use and extensive library ecosystem, while advantageous for developers, also lower the barrier for malicious actors to introduce vulnerabilities and deploy exploits. The expansive Python Package Index (PyPI) serves as a primary distribution channel, making it susceptible to supply chain attacks where compromised or trojanized packages can infiltrate development pipelines and production environments. This landscape necessitates a deep understanding of Python-specific attack vectors and robust mitigation strategies for application security professionals.

Core Mechanics

The core of many Python vulnerabilities lies in how the language handles dynamic execution, external input, and complex dependency management. Unsafe deserialization, particularly with the pickle module, is a recurring theme, allowing arbitrary code execution by manipulating object __reduce__ methods [1]. Similarly, libraries like PyYAML, jsonpickle, and even specialized frameworks such as LangChain and ChromaDB have exhibited deserialization flaws, enabling attackers to achieve Remote Code Execution (RCE) or server hijacking by crafting malicious serialized data [2][3].

Code injection remains a prevalent threat. This can manifest through insecure use of eval() or exec() on untrusted input, leading to arbitrary code execution [4]. The subprocess module and os.system, when used with shell=True and user-controlled parameters, are direct conduits for command injection [5]. Jinja2's xmlattr filter has been a source of Cross-Site Scripting (XSS) when keys contain spaces, enabling arbitrary HTML attribute injection [6]. Furthermore, Python's startup hooks, specifically .pth files located in site-packages, can be exploited for persistence and credential exfiltration by executing malicious code during interpreter startup [7][8].

The software supply chain presents a significant attack surface. Trojanized packages published to PyPI, often through typo-squatting, account takeovers, or compromised CI/CD pipelines, can distribute malware, steal credentials, or establish backdoors [9][10][11]. Examples include the TeamPCP campaign compromising LiteLLM and DurableTask, and the Shai-Hulud campaign poisoning numerous PyPI packages for credential theft [7][12].

Memory management and low-level vulnerabilities, while less common in pure Python code, can impact CPython itself or libraries with C extensions. Use-after-free vulnerabilities and integer overflows have been identified, leading to issues like arbitrary file seeks or out-of-bounds memory reads [13]. Memory exhaustion via crafted large strings or arrays can also be exploited to cause denial-of-service.

Authentication and authorization mechanisms are also prime targets. HTTP Host header manipulation, as seen in the BadHost vulnerability (CVE-2026-48710), can bypass path-based access controls in frameworks like Starlette and FastAPI, affecting AI agent deployments [14]. API authentication bypass and insecure direct object references (IDOR) remain relevant threats, though type systems can help mitigate the latter [2]. Secrets management is critical; hardcoded API keys, exposed credential files, and sensitive data in environment variables or shell history are common findings [15].

Notable Techniques

import subprocess

# Vulnerable: user_input is not sanitized subprocess.run(f"ls {user_input}", shell=True)

# Vulnerable: executing arbitrary code from user input

user_code = input("Enter Python code: ") eval(user_code)

import pickle

# Vulnerable: unpickling data from an untrusted source data = pickle.loads(untrusted_data)

from jinja2 import Template

# Vulnerable: malicious_key contains a space and script template = Template('Link') print(template.render(malicious_key='href="javascript:alert(1)"'))

Detection & Prevention

A multi-layered defense strategy is essential for securing Python applications.

Tooling

A comprehensive suite of tools is available for securing Python applications:

Recent Developments

The threat landscape for Python applications is constantly evolving. Recent developments highlight a growing sophistication in attack methods and an increasing reliance on AI/ML models within applications, which introduce new vulnerabilities.

Where to Go Deeper

For those seeking to deepen their understanding of Python security, several resources and avenues are recommended:

Sources cited in this guide

  1. Insecure Deserialization in Python | Semgrep — semgrep.dev
  2. Max-severity flaw in ChromaDB for AI apps allows server hijacking — bleepingcomputer.com
  3. CVE-2025-68664: Critical LangChain Flaw Enables Secret Extraction — socradar.io
  4. Code injection in Python: examples and prevention — snyk.io
  5. Command injection in Python: examples and prevention — snyk.io
  6. Understanding and mitigating the Jinja2 XSS vulnerability (CVE-2024-22195) — snyk.io
  7. Three’s a Crowd: TeamPCP trojanizes LiteLLM in Continuation of Campaign — wiz.io
  8. How a Poisoned Security Scanner Became the Key to Backdooring LiteLLM — snyk.io
  9. Hunting Leaked PyPI Tokens: 62 Live, 125 Packages Exposed — blog.gitguardian.com
  10. Ultralytics AI Library Hacked via GitHub for Cryptomining — wiz.io
  11. Multi-Stage Malware Attack on Python Package Index — securitymagazine.com
  12. Microsoft DurableTask Python Client Targeted in TeamPCP Cyberattack — gbhackers.com
  13. Critical severity vulnerability affecting CPython (CVE-2026-6100) — systemtek.co.uk
  14. BadHost - One character and your AI agent switches sides — korben.info
  15. How to store your users' API keys securely in Django ? — photondesigner.com
  16. Linux Kernel Elevation of Privilege Vulnerability — hkcert.org
  17. Taming our Python dependencies at Microsoft with AI — microsoft.com
  18. How To Keep A Secret in Python Apps — talkpython.fm
  19. Bandit Python: Free SAST in 10 Seconds (2026 Review) — appsecsanta.com
  20. GitHub - wapiti-scanner/wapiti: Web vulnerability scanner written in Python3 — github.com
  21. Running Untrusted Python Code — healeycodes.com
  22. GitHub - mkalioby/django-mfa2: A Django app that handles MFA, it supports TOTP, U2F, FIDO2 U2F (Webauthn), Email Token and Trusted Devices — github.com
  23. GitHub - danialhalo/SqliSniper: Advanced Time-based Blind SQL Injection fuzzer for HTTP Headers — github.com
  24. A Python prompt into a running process: debugging with Manhole — pythonspeed.com
  25. A Python prompt into a running process: debugging with Manhole — pythonspeed.com
  26. Usage Scapy 2.4.5. documentation — scapy.readthedocs.io
  27. Malware extraction in Python with Scapy — link.medium.com
  28. fortra/impacket — github.com
  29. GitHub - apify/crawlee-python: Crawlee—A web scraping and browser automation library for Python to build reliable crawlers. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with BeautifulSoup, Playwright, and raw HTTP. Both headful and headless mode. With proxy rotation. — github.com
  30. CVE-2026-33873: Langflow Agentic Assistant RCE Vulnerability — sentinelone.com
  31. How a Poisoned Security Scanner Backdoored LiteLLM — snyk.io
  32. Picklescan Allows RCE via Malicious Pickle File — github.com
  33. CVE-2025-56005: PLY RCE Vulnerability — sentinelone.com
  34. Exposing 4 Critical Vulnerabilities in Python PickleScan | Sonatype — sonatype.com
  35. PyTorch Users at Risk: 3 Zero-Day PickleScan Vulnerabilities | JFrog — jfrog.com
  36. Python Vulnerability Allows Out-of-Bounds Write on Windows Systems — cybersecuritynews.com
  37. Pygoat - Learn Django security the hard way - Speaker Deck — speakerdeck.com
  38. OWASP Pygoat — owasp.org
📚 This guide is synthesized from the full text of resources curated in the Python library, and refreshed as new material is added.