Apprentice · updated

OS command injection

PortSwigger Web Security Academy · Apprentice

  • #Command injection
LevelsApprenticePractitioner

OS Command Injection, Simple Case

The vulnerability

The stock check passes the store ID to a shell command. If you add |whoami, the server runs that too and shows you the result.

Fix: Don’t build shell commands from user input.

The lab

Open the lab on PortSwigger

This lab contains an OS command injection vulnerability in the product stock checker.

The application executes a shell command containing user-supplied product and store IDs, and returns the raw output from the command in its response.

To solve the lab, execute the whoami command to determine the name of the current user.

Solution

1. Select a Product and Check Stock: On the website, select any product and click “View details.” On the product details page, click the “Check stock” button.

2. Intercept Stock Check Request: In Burp Suite’s Proxy → HTTP history, find the POST /product/stock request. Right-click and Send to Repeater.

3. Inject whoami Command: In Repeater, modify the storeId parameter in the request body. Append a command concatenation character (e.g., |) followed by the whoami command. For example, change storeId=1 to storeId=1|whoami. Send the modified request.

4. Observe Command Output: The response will contain the output of the whoami command, revealing the username of the current user on the server, thereby solving the lab.

Tip

An active scan on the POST /product/stock endpoint in Burp Suite finds this vulnerability.