Code Injection
Code injection can be used by an attacker to introduce malicious code into a vulnerable computer program and change the course of execution.
All software takes some sort of input -- a secure program should treat all input from an external source as "untrusted" until proven otherwise. Code injection vulnerabilities exist when an attacker can submit executable input to a program and trick the software into running that input. This gives the attacker a channel by which they can circumvent any security restrictions put in place by the program's author.
How a Code Injection Attack Works
Imagine a web calculator, written in Python, that adds up the numbers a user types in. The author
takes a shortcut and hands the input straight to eval(), which runs any string it is given as
Python code:
total = eval(request.args["sum"])A user who submits 2+2 gets back 4, as intended. An attacker who submits
open("/etc/passwd").read() gets back the server's list of user accounts, since eval() makes no
distinction between arithmetic and any other Python expression. From there, the attacker can import
the os module and run shell commands with the same permissions as the calculator. (The calculator
still adds up numbers perfectly well, which is small consolation.)
The same pattern turns up wherever a program builds code out of text that an attacker controls. Some common types of code injection are:
- SQL Injection. Unsafe treatment of HTTP parameters when constructing queries on a website can permit an attacker to run arbitrary SQL statements on a vulnerable application.
- Cross site scripting. Unsafe treatment of HTTP parameters can allow injection of malicious JavaScript into a web application.
- Command execution. Unsafe treatment of HTTP parameters can allow an attacker to run arbitrary shell commands on the web server.
Code Injection in the Wild
In 2017, attackers broke into the credit bureau Equifax through a code injection flaw in Apache
Struts, a popular Java web framework. Struts evaluated an expression hidden in the Content-Type
header of an HTTP request, so a single crafted request could run commands on the server. A patch had
been available for two months before the attack began. The breach exposed the personal data of about
147 million people.
Preventing Code Injection
The consequences of a successful code injection are generally disastrous for the application author. The risks can be mitigated by:
- Never evaluating untrusted input as code. Keep user input away from functions like
eval()andexec()in Python, oreval()andnew Function()in JavaScript. To read a number, use a parser such asint()orfloat(). - Using parameterized statements for database queries, so input is never read as SQL.
- Escaping output in HTML templates, so input is never read as JavaScript.
- Keeping frameworks and libraries patched, since your code is only as safe as the code it depends on.
- Limiting the privileges of the running application, as part of defense in depth, so a successful injection can do less damage.