Skip to content

Commit 0cb34b1

Browse files
authored
Merge pull request #1333 from HackTricks-wiki/research_update_src_binary-exploitation_stack-overflow_ret2win_ret2win-arm64_20250825_082821
Research Update Enhanced src/binary-exploitation/stack-overf...
2 parents 1842320 + 81b9512 commit 0cb34b1

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

src/binary-exploitation/stack-overflow/ret2win/ret2win-arm64.md

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,21 @@ int main() {
3333
Compile without pie and canary:
3434

3535
```bash
36-
clang -o ret2win ret2win.c -fno-stack-protector -Wno-format-security -no-pie
36+
clang -o ret2win ret2win.c -fno-stack-protector -Wno-format-security -no-pie -mbranch-protection=none
3737
```
3838

39+
- The extra flag `-mbranch-protection=none` disables AArch64 Branch Protection (PAC/BTI). If your toolchain defaults to enabling PAC or BTI, this keeps the lab reproducible. To check whether a compiled binary uses PAC/BTI you can:
40+
- Look for AArch64 GNU properties:
41+
- `readelf --notes -W ret2win | grep -E 'AARCH64_FEATURE_1_(BTI|PAC)'`
42+
- Inspect prologues/epilogues for `paciasp`/`autiasp` (PAC) or for `bti c` landing pads (BTI):
43+
- `objdump -d ret2win | head -n 40`
44+
45+
### AArch64 calling convention quick facts
46+
47+
- The link register is `x30` (a.k.a. `lr`), and functions typically save `x29`/`x30` with `stp x29, x30, [sp, #-16]!` and restore them with `ldp x29, x30, [sp], #16; ret`.
48+
- This means the saved return address lives at `sp+8` relative to the frame base. With a `char buffer[64]` placed below, the usual overwrite distance to the saved `x30` is 64 (buffer) + 8 (saved x29) = 72 bytes — exactly what we’ll find below.
49+
- The stack pointer must remain 16‑byte aligned at function boundaries. If you build ROP chains later for more complex scenarios, keep the SP alignment or you may crash on function epilogues.
50+
3951
## Finding the offset
4052

4153
### Pattern option
@@ -112,6 +124,8 @@ from pwn import *
112124
# Configuration
113125
binary_name = './ret2win'
114126
p = process(binary_name)
127+
# Optional but nice for AArch64
128+
context.arch = 'aarch64'
115129

116130
# Prepare the payload
117131
offset = 72
@@ -187,6 +201,47 @@ print(p.recvline())
187201
p.close()
188202
```
189203

190-
{{#include ../../../banners/hacktricks-training.md}}
204+
### Notes on modern AArch64 hardening (PAC/BTI) and ret2win
205+
206+
- If the binary is compiled with AArch64 Branch Protection, you may see `paciasp`/`autiasp` or `bti c` emitted in function prologues/epilogues. In that case:
207+
- Returning to an address that is not a valid BTI landing pad may raise a `SIGILL`. Prefer targeting the exact function entry that contains `bti c`.
208+
- If PAC is enabled for returns, naive return‑address overwrites may fail because the epilogue authenticates `x30`. For learning scenarios, rebuild with `-mbranch-protection=none` (shown above). When attacking real targets, prefer non‑return hijacks (e.g., function pointer overwrites) or build ROP that never executes an `autiasp`/`ret` pair that authenticates your forged LR.
209+
- To check features quickly:
210+
- `readelf --notes -W ./ret2win` and look for `AARCH64_FEATURE_1_BTI` / `AARCH64_FEATURE_1_PAC` notes.
211+
- `objdump -d ./ret2win | head -n 40` and look for `bti c`, `paciasp`, `autiasp`.
212+
213+
### Running on non‑ARM64 hosts (qemu‑user quick tip)
191214

215+
If you are on x86_64 but want to practice AArch64:
216+
217+
```bash
218+
# Install qemu-user and AArch64 libs (Debian/Ubuntu)
219+
sudo apt-get install qemu-user qemu-user-static libc6-arm64-cross
220+
221+
# Run the binary with the AArch64 loader environment
222+
qemu-aarch64 -L /usr/aarch64-linux-gnu ./ret2win
223+
224+
# Debug with GDB (qemu-user gdbstub)
225+
qemu-aarch64 -g 1234 -L /usr/aarch64-linux-gnu ./ret2win &
226+
# In another terminal
227+
gdb-multiarch ./ret2win -ex 'target remote :1234'
228+
```
192229

230+
### Related HackTricks pages
231+
232+
-
233+
{{#ref}}
234+
../../rop-return-oriented-programing/rop-syscall-execv/ret2syscall-arm64.md
235+
{{#endref}}
236+
-
237+
{{#ref}}
238+
../../rop-return-oriented-programing/ret2lib/ret2lib-+-printf-leak-arm64.md
239+
{{#endref}}
240+
241+
242+
243+
## References
244+
245+
- Enabling PAC and BTI on AArch64 for Linux (Arm Community, Nov 2024). https://community.arm.com/arm-community-blogs/b/operating-systems-blog/posts/enabling-pac-and-bti-on-aarch64-for-linux
246+
- Procedure Call Standard for the Arm 64-bit Architecture (AAPCS64). https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst
247+
{{#include ../../../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)