The smartest and easiest way to improve your software security. Secure your code, from the start.

Coders Conquer Security: Share & Learn Series - Code Injection

16th May 2019

Code injection attacks are among the most common, and also the most dangerous, that many websites and applications will encounter. They run the gamut both in terms of sophistication and in the danger that they pose, but nearly any site or app that accepts user input could be vulnerable. In fact, this kind of attack is one that almost every cybersecurity defender will need to deal with at some point in their career, and will probably grapple with it multiple times.

A code injection attack can occur whenever an application or website accepts input from users. This can be as simple as providing a search function or asking a user to enter their identification information. The attack happens when a malicious user enters code into the open field instead of normal text input. Their goal is to have the server mistake the input for valid code, and then execute whatever functions the attacker desires.

While code injection attacks are extremely common, so are the available defenses that can be used to stop them. In this episode, we will learn:

  • How they work
  • Why they are so dangerous
  • How you can put defenses in place to stop them.

How do Attackers Use Code Injection?

Although the specific details about code injection attacks change depending on the programming language used, any app or website can be vulnerable so long as it allows a user to input data. Code injection attacks have been triggered for SQL, HTML, XML, CSS and every other common programming language.

First, an attacker must locate vulnerable code within an application, normally at a point where users are allowed to enter their own input. For example, this code takes the PHP eval() function and passes it along to a user to modify, without any sort of validation of the return string.

$myvar = "varname";
$x = $_GET['arg'];
eval("\$myvar = \$x;");

A clever attacker could easily add their own string to the eval function, even executing system commands if they choose.

/index.php?arg=1; system('id')

It’s important to note that while code injection attacks can involve sending system commands, they are not restricted to just doing that. In fact, with code injection attacks, hackers are only limited by the functionality of the language itself. In our example, an attacker could program the targeted system to do almost anything allowed by the PHP framework.

Why are Code Injection Attacks so Dangerous?

Code injection attacks are potentially extremely dangerous depending on the skill of the attacker. They can do anything that the programming language allows, which puts them on the same footing as the app’s programmers. An attacker could practically write their own app and have it executed within the target environment.

Even less skilled attackers can be dangerous. Instead of writing their own application or code strings, they can simply order the targeted system to accept and install pre-programmed malware. This could lead to site defacement, ransomware attacks or even become the basis for a phishing campaign leveled against the site’s users.

Most of the time, code injection attacks are used to steal things like user lists and passwords, or to gain valuable reconnaissance into a system targeted for further compromise. But be warned, a skilled coder can do almost anything with a code injection attack, which is why it’s critical that every potential instance of them are discovered and removed from your environment.

Trust No-One! (Or At Least, Not Users)

When eliminating code injection attack vulnerabilities, the first place to look is anywhere that asks for, or allows, user input. Anything inputted by a user is not to be trusted under any circumstances. If you allow user input without filtering or examination, you are basically inviting attackers to take a free shot at compromising your system or even your network.

Although it is not always possible, the best way to thwart code injection attacks is to prevent functions from executing or interpreting user input directly. Perhaps users can be given a choice of static options instead of free reign to enter their own queries, with the application programmed to only accept those limited choices as valid. It may not always be appropriate to do that, but using it where possible can eliminate code injections before they start.

For areas where users need to enter their own input, tight controls must be placed on that data. Assuming everything is a potential attack is a good place to start. Enforcing least privilege policies such as read-only user access, on both the client and server side, can prevent most code from executing.

The other good defense is to implement application-wide filters and sanitization on everything any user inputs. Developers have been aware of code injection attacks for years, and libraries of proven filters exist for every framework and language. When applying those filters, be sure to do so not just on the obvious user input areas or against common parameters like Get and Post commands, but also against cookies and HTTP headers.

Applying a Correction for Code Injection

Removing unnecessary user input areas from your environment, enforcing least privilege principles and using the latest filtering and sanitization tools to inspect and detect potential attacks can shut the door on this dangerous vulnerability. Having the mindset of never trusting user input will also serve you well moving forward. Do all that, and you can stay one step ahead of this dangerous type of attack.

For further reading, you can take a look at the OWASP write-up on code injection. You can also put your newfound defensive knowledge to the test with the free demo of the Secure Code Warrior platform, which trains cybersecurity teams to become the ultimate cyber warriors. To learn more about defeating this vulnerability, and a rogues’ gallery of other threats, visit the Secure Code Warrior blog.

Face code injection head-on, right now. Take the challenge on our gamified training platform:

"coder"​ && "hacker"​ && "product_designer"

View Comments