Brief Summary

This article describes how to test an application for OS command injection. We try try to inject an OS command through an HTTP request to the application.

Short Description of the Issue

OS command injection is a technique used via a web interface in order to execute OS commands on a web server.

The user supplies operating system commands through a web interface in order to execute OS commands. Any web interface that is not properly sanitized is subject to this exploit. With the ability to execute OS commands, the user can upload malicious programs or even obtain passwords. OS command injection is preventable when security is emphasized during the design and development of applications.

Black Box testing and example

When viewing a file in a web application, the file name is often shown in the URL. Perl allows piping data from a process into an open statement. The user can simply append the Pipe symbol â|â onto the end of the filename.
Example URL before alteration:

http://sensitive/cgi-bin/userData.pl?doc=user1.txt

Example URL modified:

http://sensitive/cgi-bin/userData.pl?doc=/bin/ls|

This will execute the command â/bin/lsâ.
Appending a semicolon to the end of a URL for a .PHP page followed by an operating system command, will execute the command.
Example:

http://sensitive/something.php?dir=%3Bcat%20/etc/passwd

Example
Consider the case of an application that contains a set of documents that you can browse from the Internet. If you fire up WebScarab, you can obtain a POST HTTP like the following:

In this post request, we notice how the application retrieves the public documentation. Now we can test if it is possible to add an operating system command to inject in the POST HTTP. Try the following:

Gray Box testing

Sanitization
The URL and form data needs to be sanitized for invalid characters. A âblacklistâ of characters is an option but it may be difficult to think of all of the characters to validate against. Also there may be some that were not discovered as of yet. A âwhite listâ containing only allowable characters should be created to validate the user input. Characters that were missed, as well as undiscovered threats, should be eliminated by this list.Permissions
The web application and its components should be running under strict permissions that do not allow operating system command execution. Try to verify all these informations to test from a Gray Box point of view