What is Code Injection?
Code Injection, also known as Remote Code Execution (RCE), is a vulnerability in web applications that occurs when an attacker discovers an input validation flaw where they are able to inject malicious code which is then interpreted/executed by the application.
Applications are vulnerable to code injection if they use unvalidated input, with web applications being a prime target for attackers to access the database and corrupt the application entirely.
Code Injection in Real Life
Real-life examples of code injection are hard to come by given that the developers prefer to keep things under wraps. You might also be very surprised to hear that code injection can actually be used for positive reasons.
The most popular example perhaps is Dropbox. They utilize code injection to make some online functionalities available in the offline use of their tools.
Conversely, however, bad coding practices can very easily lead to code injection, as a user might unknowingly use the reserved program keyword due to the developer not sanitizing it correctly or in time.
Code Injection Examples in Programming Languages
Code Injection is a huge safety issue in web applications and most programmers are well aware of this. As previously mentioned, most of the most popular server-side languages can be exploited by code injection, which we will review below.
Code Injection in PHP
As with other programming languages, failing to validate user input is the biggest mistake made by PHP developers, enabling an attacker to inject malicious code through that same input.
A good example of this is when a developer uses eval() function without sanitizing. Below is an example of an unsafe eval function:
$temp = "tempVar";
$a = $_GET['arg'];
eval("$temp = $a;");
In this instance, the code is vulnerable to code injection, an attacker could use something like:
This would give them all the information they need about processes, versions, etc. to wreak havoc in your application.
Code Injection in Java
Even though you might not think that’s the case, Java is indeed vulnerable to code injection. This is because, just like with any other programming language, user input is used and processed by the Java application.
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.
Code Injection in Python
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.
Just like with PHP, you should avoid using the eval command as it may easily lead down code injection paths.
In an ideal world, we would simply avoid having user inputs implemented into the dynamic code. However, more often than not, this is unavoidable. In order to keep your applications safe, you should strictly validate the user input. If possible, a whitelist of accepted inputs would be the best solution, but unfortunately, this isn’t feasible for most web applications that receive user input.
How to Prevent Code Injection
Quite simply, heavy input validation is the best way to prevent these issues. This requires checking all the parameters you can think of, even if it affects the speed of the execution.
The ideal solution would be to create a list of available options for the user input. The issue is, however, that that’s often not possible as we need custom user input a lot of the time.
One method is to take extra caution and safely process the user input, using existing functions whose purpose is mainly to exclude any possibility of injecting malicious code.
This is why it’s recommended that you create a list of forbidden functions in your server configuration that might be used for malicious purposes.
And finally, make sure that you’re regularly scanning your applications with a dynamic webapp security scanner!
Code injection is one of the biggest vulnerabilities in web programming nowadays. What makes it so dangerous is that a small slip-up in user validation could lead to unfixable consequences.
The good thing, however, is that sanitizing the user input will do the job most of the time. Even though it can be a bit of a boring process, it’s a highly valuable trait for web applications to have as it keeps them safe and sound from any possible code injection. Luckily for you, NexPloit offers free scans on your web applications – sign up today!