githubEdit

OS Command Injection

What is it?

Command injection is a vulnerability that allows an attacker to manipulate an application to execute arbitrary system commands on the server. This occurs when an application passes unsafe data, often user input, to a system shell.

A simple example

A vulnerable web application might take a path from a query parameter and use it to read a file, like so:

$file = $_GET['file'];
system("cat /var/www/html/$file");

If an attacker uses a payload such as ; ls -la in the file parameter, they can make the application execute an additional command that lists all files in the current directory.

The server then executes the cat command and the ls command and the attacker receives a list of all files in the current directory.

Command injection can often lead to:

  • Remote code execution

  • Denial of Service

  • Data breach

  • Privilege escalation

Other learning resources:

Writeups:

  • Bullets

Checklist

OS Command Injection

We can inject OS commands through URL params, POST data, etc.

Automation

*Use --batch option for default behavior without user input.

Basic Payloads

If the payload includes whitespaces (' '), we need to change it to '+' or URL encoding ('%20').

URL Encoding

We may be able to bypass specific character filter by encoding them.

Null-terminator

Sometimes, we need to put a null-terminator to ignore subsequent code given by the target application.

Bypass Whitespace Filter

Reference: https://www.ctfnote.com/web/os-command-injection/whitespace-bypassarrow-up-right

If the website filters whitespaces and we cannot inject OS command including spaces e.g. 'sleep 5', we can insert Internal Field Separator (IFS) as whitespace:

Payload Examples:

Below is the ping -c 1 10.0.0.1 command:

Ping

Try pinging to our local machine for checking if our command injection achieves. To confirm the result, start tcpdump in our local machine.

Then execute ping command in POST request. Below are examples for POST data.

Reverse Shell

PHP Reverse Shell

Reference: https://book.hacktricks.xyz/pentesting-web/command-injection#examplesarrow-up-right

Blind Command Injection (Time Delay)

Use ping command to check if the website will be loaded with time delay.

If we find the command can be executed, we can execute the other commands as below.

JSON Injection

PHP Injection

Indirect Payloads with Shell Script

If we cannot inject command directly as above, try injecting from files.

Create a shell script. The filename here is evil.sh`.

Host this file by starting web server in the directory where the evil.sh exists.

In target website, inject command to let target server download the shell script and execute it. Before that, we need to start listerner by nc -lvnp 4444 in another terminal in local machine. Here is the example.

We might get a shell.

Exploitation

Basic command chaining

Using logic operators

Commenting out the rest of a command

Using a pipe for command chaining

Testing for blind injection

Out-of-band testing

Command Injection

circle-info

Command injection is an attack in which the goal is execution of arbitrary commands on the host operating system via a vulnerable application.

Last updated