- PHP Command Injection: Examples and Prevention
- What Is Command Injection?
- How Is Command Injection Performed?
- Examples of Command Injection in PHP
- How Can an Attacker Craft a Command Injection Request?
- Where Does Unsafe Data Come From?
- How to Fix Command Injection Vulnerabilities
- Don’t Use Direct Shell Execution Functions
- Don’t Use Unsafe Data in Combination With Direct Shell Execution Functions
- Protect Your Code from Injections
- More StackHawk
- Web. Решение задач с r0от-мi. Часть 1
- HTML — Source code
- HTTP redirect
- HTTP — User-agent
- HTTP Аутентификация
- PHP — command injection
- Backup file
- HTTP — Directory indexing
PHP Command Injection:
Examples and Prevention
Let’s see what command injection is, how it works in PHP, and understand how we can prevent command injection vulnerabilities.
There are many ways for a malicious user to take advantage of vulnerable websites. One of them is called command injection.
Today, you’ll learn what command injection is, how an attacker could use it to jeopardize your web application, and how you can prevent that from happening in your PHP applications.
A command injection attack is based on the execution of arbitrary (and most likely malicious) code on the target system.
What Is Command Injection?
A command injection attack is based on the execution of arbitrary (and most likely malicious) code on the target system.
In other words, it’s a way to use an application designed to do one thing for a completely different purpose.
Let’s take the example of a simple contact form. The purpose of such an application is simple: to allow people to leave their contact information for someone inside the company to get in touch. An attacker might want to use the application to steal information about other applicants, for example. And they might achieve that goal by injecting malicious code.
How Is Command Injection Performed?
In order for a command injection attack to occur, three things must happen at once:
- An application is using some function to make a call to a system shell.
- The application is passing unsafe/unvalidated data to such a call.
- An attacker is aware of this fact and acts on this knowledge.
Examples of Command Injection in PHP
These three PHP functions, if not used safely, can lead to the presence of this vulnerability:
The problem lies in the fact that all of them take an arbitrary string as their first parameter and simply forward it to the underlying operating system.
This doesn’t imply any risk if the string is written by the programmer (aka you), like this:
$command = "ls -l"; $output = exec($command);
But, since the $command variable is a string, nothing prevents you from writing something like the below:
$command = "ls ".$_GET['modifiers']; $output = exec($command);
Which would, of course, produce the exact same result (since the value of $command would be “ls -l”).
Now, let’s look at the following example:
$command = "ls ".$_GET['modifiers']; $output = exec($command);
Here the command is being created from two sources: a fixed string (“ls “) and a URL parameter ( $_GET[‘modifiers’] ).
This means that the actual command that’s about to be executed depends on user input.
Let’s say someone issues a request such as http://yourdomain.com?modifiers=-l .
When the code gets executed, the value of $_GET[‘modifiers] will be “-l”, which will result in the command “ls -l”.
But what if the user isn’t so nice?
What if they were to issue a request such as http://yourdomain.com?-l%3B+rm+%2A+-Rf ?
The resulting command would be “ls -l; rm * -Rf”.
If you know a little bit about the Linux console, you should recognize the command “rm * -Rf” … and be very scared. (In case you’re not familiar with the Linux console, that command means “delete every file in this directory and its subdirectories.”)
It’s very much unlikely that someone will issue such a request by mistake.
But if someone wants to break your site and they know a little PHP (or any other programming language), it’s easy to create such a string.
In case you’re wondering where “-l%3B+rm+%2A+-Rf” comes from, it’s the result of the following:
How Can an Attacker Craft a Command Injection Request?
Probably the easiest way is to use your own forms.
In the example above, imagine there’s a previous page that looks like this:
action="index.php"> name="modifiers" type="text"> value="Get file names" type="submit">