🌐
Snyk
snyk.io › blog › code-injection-python-prevention-examples
Code injection in Python: examples and prevention | Snyk
December 6, 2023 - These vulnerabilities often occur when an application mishandles user input. For example, insecure use of functions like eval() in Python without proper validation can lead to code injection.
🌐
Semgrep
semgrep.dev › write rules › secure coding › python › code injection in python
Code Injection in Python | Semgrep
The _xxsubinterpreters.run_string is an internal Python function that interprets the string as Python code. This causes a code injection vulnerability when unverified user data reaches run_string.
Discussions

Python Injection - is there such a thing? - Stack Overflow
SQL injection comes up a lot but ... thing as python injection? Say for example that a web form submitted a value that was entered in a dictionary in some python backend app. Would it be possible that if that input wasn't handled correctly that python code could be injected ... More on stackoverflow.com
🌐 stackoverflow.com
Demo of how input() is security probem in Python 2.7 (4min)
Demo of how writing insecure code is a security problem. More on reddit.com
🌐 r/Python
60
84
March 14, 2018
Injection Attacks Against Flask [blog]

The first point about template injection almost seemed like it was going somewhere, but actually feels like a no brainer to avoid, since every flask/jinja tutorial under the sun is going to tell you to use curly-brace placeholders in your templates, and not python's built-in substitution operators/methods.

When I think vulnerabilities, I think of something inherently flawed with the design and implementation of something that can be easily exploited, even when used perfectly as intended. For example, if there was a way for someone to inject code into the template even when used with all common sense template syntax and loading techniques.

Since Jinja/Flask were designed to handle untrusted input sanitization well, this is more of a "gotcha" than a "vulnerability." If you use the tools available to you appropriately, it's not a problem. If you misuse or don't use the tools available to you, you risk accidentally creating vulnerabilities unnecessarily. That sort of goes without saying.

More on reddit.com
🌐 r/flask
4
16
December 8, 2015
Loosely coupled Python code with Dependency Injection
I used to hate DI but Go forced me to do it and honestly I can really see the benefits of the pattern. I don't love when it's done magically though More on reddit.com
🌐 r/Python
7
10
November 10, 2022
🌐
GitHub
github.com › sethsec › PyCodeInjection
GitHub - sethsec/PyCodeInjection: Automated Python Code Injection Tool · GitHub
root@playground:/opt/PyCodeInjection# python PyCodeInjectionShell.py -h Usage: python PyCodeInjectionShell.py -c command -p param -u URL python PyCodeInjectionShell.py -c command -p param -r request.file Options: -h, --help show this help message and exit -c CMD Enter the OS command you want to run at the command line -i Interactivly enter OS commands until finished -u URL Specify the URL. URLs can use * or -p to set injection point -p PARAMETER Specify injection parameter.
Starred by 87 users
Forked by 23 users
Languages   Python 96.8% | HTML 2.1% | Shell 1.1%
🌐
Snyk
snyk.io › blog › command-injection-python-prevention-examples
Command injection in Python: examples and prevention | Snyk
December 21, 2023 - Note that in a real-world application, you’ll likely need stricter validation rules than shown in this simple example. The subprocess module can spawn new processes, connect to their input, output, and error pipes, and obtain their return codes. This means it's essential to use this module securely. Avoid using shell=True with the subprocess module unless necessary, as this can execute commands in a shell, leading to command injection attacks:
🌐
PortSwigger
portswigger.net › kb › issues › 00100f10_python-code-injection
Python code injection - PortSwigger
Server-side code injection vulnerabilities arise when an application incorporates user-controllable data into a string that is dynamically evaluated by a code interpreter. If the user data is not strictly validated, an attacker can use crafted input to modify the code to be executed, and inject ...
🌐
Armur
armur.ai › blogs › posts › code_injection_in_python
Code Injection in Python: Examples and Prevention | Armur
This article focuses on code injection in Python, exploring various types of injections, providing examples, and discussing prevention techniques. Understanding these concepts is crucial for developing secure Python applications. 2. Understanding Code Injection Code injection is a technique ...
🌐
DataCamp
datacamp.com › tutorial › python-dependency-injection
Python Dependency Injection: A Guide for Cleaner Code Design | DataCamp
July 24, 2025 - Bandit: A Static code analyzer for Python focused on security issues. Dependency injection is not just a theoretical pattern; it is used in various production systems to manage complexity, improve flexibility, and streamline testing. In modern web applications, dependency injection plays a crucial role in managing standard services, such as authentication, logging, and database access. Frameworks like FastAPI play an essential role in resolving routes and dependencies. The example below illustrates a centralized authentication implementation in FastAPI.
🌐
StackHawk
stackhawk.com › stackhawk, inc. › vulnerabilities and remediation › preventing command injection in python: a guide to security
Command Injection in Python: Examples and Prevention
January 13, 2025 - So in this post we will get you familiar with command injection via concrete examples—more precisely, command injection in Python. Command injection sends malicious data into an application that can lead to grave damage when dynamically evaluated by the code interpreter.
Find elsewhere
🌐
Learn Prompting
learnprompting.org › home › docs › prompt hacking › offensive measures › code injection
Code Injection: Hacking Tool-Augmented LLMs
We will assume that it takes in a math problem and writes Python code to try to solve the problem. Here is the prompt that the simplified example app uses: ... Interested in prompt hacking and AI safety? Test your skills on HackAPrompt, the largest AI safety hackathon. You can register here. Code injection is a sophisticated hacking technique that takes advantage of ChatGPT's ability to interpret Python code.
🌐
eLearnSecurity
doyler.net › home › exploiting python code injection in web applications
Exploiting Python Code Injection in Web Applications
April 11, 2018 - I was looking into python code injection recently, and ran across SethSec’s blog post. This looked like a great example, and I wanted to run through it myself. First off, I downloaded the PyCodeInjection application and got it running locally. root@kali:~/Documents# ls root@kali:~/Documents# git clone https://github.com/sethsec/PyCodeInjection.git Cloning into 'PyCodeInjection'... remote: Counting objects: 67, done. remote: […]
🌐
Acunetix
acunetix.com › vulnerabilities › web › python-code-injection
Python code injection - Vulnerabilities
This script is vulnerable to Python code injection. The user input appears to be placed into a dynamically evaluated Python code statement, allowing a... Code Evaluation (Python)
🌐
CodeQL
codeql.github.com › codeql-query-help › python › py-code-injection
Code injection — CodeQL query help documentation - GitHub
ID: py/code-injection Kind: ... python-security-and-quality.qls ... Directly evaluating user input (for example, an HTTP request parameter) as code without properly sanitizing the input first allows an attacker arbitrary code execution....
🌐
SecureFlag
knowledge-base.secureflag.com › vulnerabilities › code_injection › os_command_injection_python.html
OS Command Injection in Python | SecureFlag Security Knowledge Base
August 5, 2025 - The following snippet contains a Flask web application written in Python that executes the nslookup command to resolve the host supplied by the user. @app.route("/dns") def page(): hostname = request.values.get(hostname) cmd = 'nslookup ' + hostname return subprocess.check_output(cmd, shell=True) Since the hostname is simply appended to the command and executed on a subshell with shell=True, an attacker could stack another command using ; in the file_path GET parameter to inject additional commands.
🌐
Bright Security
brightsec.com › blog › code-injection-example
Code Injection Example: A Guide to Discovering and Preventing attacks - Bright Security
March 25, 2025 - With countless libraries and addons for Java, it’s very easy to fall into the trap of carelessness which is why you have to be extremely careful in order to avoid code injection in Java alongside the disastrous consequences that it brings. Python code injection appears when user input is processed by Python that allows the attacker to inject malicious Python code into the input field with the language itself processing that same code.
🌐
Wisc
research.cs.wisc.edu › mist › SoftwareSecurityCourse › Chapters › 3_8_3-Code-Injections.pdf pdf
3_8_3-Code-Injections.pdf
https://research.cs.wisc.edu/mist/SoftwareSecurityCourse/Chapters/18-Code-Injections.pdf
🌐
https://probely.com
probely.com › vulnerabilities › python-code-injection
Python code injection - Probely
A Python code injection vulnerability allows the attacker to execute arbitrary Python code into the application. In the worst-case scenario, the attacker will be able to fully administrate the server, which will enable him to extract sensitive data, modify the application contents or delete data.
🌐
Greg Scharf
blog.gregscharf.com › 2023 › 04 › 11 › code-injections
Code Injections :: Greg Scharf — Development & Security
April 11, 2023 - You could use that same syntax in any python application, and it will work just fine, but when you’re tampering with an already running python application, like we are now, then only the double underscore syntax will be successful. In TryHackMe’s Devie room the home page displays inputs for 3 separate mathematical formulas on the home page. The code for this application is given to us via a download link at the bottom of the page.
🌐
Blogger
sethsec.blogspot.com › 2016 › 11 › exploiting-python-code-injection-in-web.html
Exploiting Python Code Injection in Web Applications
November 20, 2016 - In fact, for those of you who are CWE fans like I am, these two CWEs are right on point: CWE-94: Improper Control of Generation of Code ('Code Injection') CWE-95: Improper Neutralization of Directives in Dynamically Evaluated Code ('Eval Injection') If you (or Burp or another tool) finds a python injection with a payload like this: eval(compile('for x in range(1):\n import time\n time.sleep(20)','a','single'))
🌐
Information Security Newspaper
securitynewspaper.com › home › exploiting python code injection in web applications
Exploiting Python Code Injection in Web Applications
December 11, 2016 - Note the sqlmap style * designating the payload placement in the URL. This example also uses interactive mode, which lets you continuously enter new commands until you exit: And here is the same functionality using a request file copy/pasted from burp repeater, with an implanted *, which tells the tool where to inject: