Code injection is the malicious injection or introduction of code into an application. The code introduced or injected is capable of compromising database integrity and/or compromising privacy properties, security and even data correctness. It can also steal data and/or bypass access and authentication control. Code injection attacks can plague applications that depend on user input for execution.
Code Injection differs from Command Injection. Here an attacker is only limited by the functionality of the injected language itself. For example, if an attacker is able to inject PHP code into an application and have it executed, he is only limited by what PHP is capable of.
Code injection vulnerabilities range from easy to difficult-to-find ones. Many solutions have been developed for thwarting these types of code injection attacks, for both application and architecture domain. Some examples include input validation, parameterization, privilege setting for different actions, addition of extra layer of protection and others.
Example:
When a developer uses the PHP eval() function and passes it untrusted data that an attacker can modify, code injection could be possible.
The example below shows a dangerous way to use the eval() function:
// A dangerous way to use the eval() function // in PHP $myvar = "varname"; $x = $_GET['arg']; eval("\$myvar = \$x;"); |
As there is no input validation, the code above is vulnerable to a Code Injection attack.
For example:
/index.php?arg=1; phpinfo()
Above will show all the info of php.
While exploiting bugs like these, an attacker may want to execute system commands. In this case, a code injection bug can also be used for command injection, for example:
/index.php?arg=1; system('id')
This will tell the ids of the process.
uid=33(www-data) gid=33(www-data) groups=33(www-data)
Mitigation
Ideally, a developer should use existing API for their language. For example (Java): Rather than use Runtime.exec() to issue a ‘mail’ command, use the available Java API located at javax.mail.*
If no such available API exists, the developer should scrub all input for malicious characters. Implementing a positive security model would be most efficient. Typically, it is much easier to define the legal characters than the illegal characters.
Reference
https://en.wikipedia.org/wiki/Code_injection
https://www.cse.unr.edu/~mgunes/cpe401/cpe401sp11/student/CodeInjection.pptx
This article is contributed by Akash Sharan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- Basic SQL Injection and Mitigation with Example
- Command Injection Vulnerability and Mitigation
- Mitigation of SQL Injection Attack using Prepared Statements (Parameterized Queries)
- How to use SQLMAP to test a website for SQL Injection vulnerability
- CRLF Injection Attack
- Packaging and Publishing Python code
- 7 Tips To Write Clean And Better Code in 2020
- Top 10 Python IDE and Code Editors in 2020
- Top 5 Open Source Source and Free Static Code Analysis Tools in 2020
- Code Smell - A General Introduction and it's Type
- Making a QR code for a website
- Top 7 Code Sharing Website For Developers
- Google Summer of Code Preparation
- Cracking Google Summer of Code 101
- Hackathon - Think, Code, Create
- Lex code to replace a word with another word in a file
- How to run Python code on Google Colaboratory
- 5 Tips On Learning How to Code - General Advice For Programmers
- How to Prepare For GSoC (Google Summer of Code) - A Complete Guide
- Code Reverse Engineering - How To Reverse Engineer Your Expired Software

