> For the complete documentation index, see [llms.txt](https://dev-angelist.gitbook.io/writeups-and-walkthroughs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev-angelist.gitbook.io/writeups-and-walkthroughs/dvwa/command-injection.md).

# Command Injection

http\://localhost/DVWA/vulnerabilities/exec/

<details>

<summary>What is a command injection?</summary>

Command injection is a type of security vulnerability that occurs when an attacker is able to inject and execute arbitrary commands or code into a software application. This vulnerability is typically found in applications that accept and process input from users or other untrusted sources without proper validation or sanitization.

The most common form of command injection involves the exploitation of input fields or parameters that are used to construct system commands. When the application fails to properly validate or sanitize user input, an attacker can inject malicious commands that get executed by the underlying system with the privileges of the application.

</details>

{% hint style="info" %}
Using BurpSuite and the FoxyProxy extension is recommended.
{% endhint %}

## Low

We've a form with an input type text:

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2F2hNWnt6UHA7rWDaAtfze%2Fimage.png?alt=media&amp;token=48dbcdfe-5d24-4154-b184-45b0b728d34c" alt=""><figcaption></figcaption></figure>

that ask us to enter and IP address to ping.

Inserting an IP address we can confirm that will do a ping request to it:

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FkVaqW7b29cqWi3BkEAvu%2Fimage.png?alt=media&amp;token=22d82a98-ddaa-4a8f-b722-e1e2289696e6" alt=""><figcaption></figcaption></figure>

As always we can start to analyze source code:

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FQUj4lP4A9rExfYKJECIQ%2Fimage.png?alt=media&amp;token=32bd7f09-a9a6-4a9b-99fc-96f33bf15f08" alt=""><figcaption></figcaption></figure>

* There'is a condition to check if input value has been inserted
* The operating system in use is checked to evaluate exactly which ping should be entered (win or \*nix OS)
* In the end, there's generate feedback for the end user.

{% hint style="warning" %}
The input is not sanitized, so I can execute any (potentially malicious) command.
{% endhint %}

#### Payload

```bash
127.0.0.1 ; whoami ; cat /etc/passwd
```

using it, we ping machine with IP 127.0.0.1 and join two extra commands using ; or another join char as |, to take a whoami and see /etc/passwd file:

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FIcBPjIe14Kv5jjLuctPL%2Fimage.png?alt=media&amp;token=a390655a-53a8-4766-8c6c-a90c6d7177d5" alt=""><figcaption></figcaption></figure>

## Medium

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FURGML6Nfb8QKYxtDPMgk%2Fimage.png?alt=media&amp;token=376d216c-d5f1-4335-95f8-ceb07edffd14" alt=""><figcaption></figcaption></figure>

* There's a classic control of input text submitted
* An eventually blacklist word (&& and ;) is replace with a '' black char
* The operating system in use is checked to evaluate exactly which ping should be entered (win or \*nix OS)
* In the end, there's generate feedback for the end user.

{% hint style="warning" %}
The input is not sanitized because blacklist words are eventually removed only one time and not recursevely and we can use others join chars to add a new commands such as |.
{% endhint %}

#### Payload

We've replaced `;` or `&&` with `|`:

```bash
127.0.0.1 | whoami
```

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FrpAsbpLOwDb2BWKM6xkI%2Fimage.png?alt=media&amp;token=64bcf6d4-8a8a-4b34-8722-d53c4958818d" alt=""><figcaption><p>Using another word to anchor commands</p></figcaption></figure>

`;` will be replace by '' and will submit this prohibit payload:

```bash
127.0.0.1 && whoami
```

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2Fb4sPQ1P9MzBWJbo4uXx4%2Fimage.png?alt=media&amp;token=b1ed7824-7634-4713-b2cf-ccc720fbcacf" alt=""><figcaption><p>Using phoibit anchor commands</p></figcaption></figure>

## High

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FKTjFWgXrXfhVzMx0TyJD%2Fimage.png?alt=media&amp;token=284a89a5-a8a8-41d8-bc28-edac9b7ba126" alt=""><figcaption></figcaption></figure>

This command isn't write correctly, it has an extra space '`|`  '&#x20;

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FXDMFDaO77M8LqbrsuckZ%2Fimage.png?alt=media&amp;token=b8d656f2-92f5-4be7-9fa0-0b87c651f299" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}
The input is not sanitized, so I can execute any (potentially malicious) command.
{% endhint %}

Payload

Without leaving a white space after | we can use this payload:

```bash
127.0.0.1 |whoami
```

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FKkAZxcilOFrjWzBoXKF2%2Fimage.png?alt=media&amp;token=6ed7c731-b0a6-4f07-b430-35311caaaae8" alt=""><figcaption></figcaption></figure>

## Impossible

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FpjfPxeVghgM4bnQuAmCe%2Fimage.png?alt=media&amp;token=89d82826-228c-4d1f-80c6-497e142c4d11" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The input is sanitized and it's not vulnerable to a command injection attack.
{% endhint %}

## References

For the making of this solution the following resource were used:

* [https://github.com/LeonardoE95/DVWA/](https://github.com/LeonardoE95/DVWA/tree/main/src/client_side_request_forgery)
