🌐
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 › secure coding › python › prevent code injection for python
Prevent Code Injection for Python - Semgrep
2 weeks ago - 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. A malicious actor can inject a malicious string to execute arbitrary Python code. Example:
Discussions

A new take on dependency injection in Python
I've noticed that most Python projects I've worked on don't really structure applications in the way I'm used in other OOP languages (e.g. Java), where you encapsulate your application logic in modular classes, typically first described by an unimplemented interface, and then implemented in one or more subclasses. You then "build" your application by selecting appropriate implementations and wiring them together (i.e., passing dependencies as parameters to constructors of other dependencies, and so on). With a good DI framework, you can abstract away the tedious process of constructing your application. I'm curious why this hasn't really caught on in Python. By making your application modular in this way, you can make your code easily extensible and really simplify things like testing (no need for monkey-patching, e.g. -- you can just wire in a test version of a dependency). Any thoughts on this? I know that the dynamic nature of Python allows you to achieve a lot flexibility by manipulating objects at runtime, but this is super messy. OOP encapsulation makes everything so much cleaner and easier to reason about. More on reddit.com
🌐 r/Python
36
15
October 11, 2024
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 86 users
Forked by 22 users
Languages   Python 96.8% | HTML 2.1% | Shell 1.1%
🌐
Wisc
research.cs.wisc.edu › mist › SoftwareSecurityCourse › Chapters › 3_8_3-Code-Injections.pdf pdf
Introduction to Software Security Chapter 3.8.3: Code Injections
Given that eval evaluates the input as a Python expression, it can · also calculate values if you prefer. For example, if the input is 30 * 12 + 5 then it computes the value ... The __import__ function dynamically imports the module named by the string provided, so this invokes · the standard ...
🌐
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.
🌐
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:
🌐
Bright Security
brightsec.com › blog › code-injection-example
Code Injection Example: A Guide to Discovering and Preventing attacks - Bright Security
August 10, 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.
🌐
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: […]
Find elsewhere
🌐
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 ...
🌐
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'))
🌐
Medium
medium.com › @snyksec › code-injection-in-python-examples-and-prevention-680b44f3d9ae
Code injection in Python: examples and prevention | by Snyk | Medium
December 7, 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.
🌐
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.
🌐
Medium
medium.com › swlh › hacking-python-applications-5d4cd541b3f1
Hacking Python Applications. And how attackers exploit common… | by Vickie Li | The Startup | Medium
November 15, 2019 - Dangerous functions in Python like eval(), exec() and input() can be used to achieve authentication bypass and even code injection. The eval() function in Python takes strings and execute them as code. For example, eval(‘1+1’) would return 2.
🌐
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 ...
🌐
Information Security Newspaper
securitynewspaper.com › home › exploiting python code injection in web applications
Exploiting Python Code Injection in Web Applications
December 11, 2016 - A web application vulnerable to Python code injection allows you to send Python code though the application to the Python interpreter on the target server. If you can execute python, you can likely call operating system commands.
🌐
Andrea Fortuna
andreafortuna.org › 2018 › 08 › 20 › pycodeinjector-a-simple-python-code-injection-library
pycodeinjector: a simple python Code Injection library | Andrea Fortuna
August 20, 2018 - In my previous post "Code injection on Windows using Python: a simple example", i've explored the ctype python library and the usage of Windows API in order to perform a code injection on 32bit systems. All tests was performed using shellcodes generated by metasploit or found on some online ...
🌐
Acunetix
acunetix.com › vulnerabilities › web › python-code-injection
Code Evaluation (Python) - Vulnerabilities - Acunetix
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....
🌐
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. ... Want to pursue a career in AI Red Teaming? Check out our AI Red Teaming Masterclass, available On-Demand or as a Live cohort. Earn your AIRTP+ certification. Code injection is a sophisticated hacking technique that takes advantage of ChatGPT's ability to interpret Python code.
🌐
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.