Computer Security

#3 Wargame leviathan3 (Level3 ~ Level5), db,dc,px, ltrace, 아스키코드 변환 본문

Wargame:leviathan

#3 Wargame leviathan3 (Level3 ~ Level5), db,dc,px, ltrace, 아스키코드 변환

쿠리 Kuri 2022. 7. 28. 18:30

Level3 -> Level4

1. ls -al 을 살펴보니 level3 의 setuid 파일이 있다.

leviathan3@leviathan:~$ ./level3
Enter the password> Ahdiemoo1j
bzzzzzzzzap. WRONG
leviathan3@leviathan:~$ r2 level3
leviathan3@leviathan:~$ ls -al
total 32
drwxr-xr-x  2 root       root        4096 Aug 26  2019 .
drwxr-xr-x 10 root       root        4096 Aug 26  2019 ..
-rw-r--r--  1 root       root         220 May 15  2017 .bash_logout
-rw-r--r--  1 root       root        3526 May 15  2017 .bashrc
-r-sr-x---  1 leviathan4 leviathan3 10288 Aug 26  2019 level3
-rw-r--r--  1 root       root         675 May 15  2017 .profile

 

 

 

2.실행시켜보고 패스워드 아무거나 쳐보면, WRONG 틀린다고 나온다. radare2로 이를 분석 해보자.

r2 level3

 

 

 

3.코드를 살펴보니, h0no33 이랑 kaka랑 비교해서 다르면 Enter the password 가 뜬다 한다. 뭐 당연히 다르니, Enter the password 가 뜰 것이다. 그 뒤에 do_stuff 로 이동한다는데 저기 함수를 한번 살펴보자.

do_stuff

여기서도 s1 과 s2를 strcmp로 이용해 비교해보고 이번엔 맞으면 geteuid ~~ 하면서 권한획득되고 하는거 보니, 여기가 진짜 패스워드가 숨겨져 있는 것 같다.

 

 

 

4.이제, 저 비교하는 중요한   0x080485b7      e814feffff     call sym.imp.strcmp         ; int str   이 지점을 r2디버거 모드를 실행해  브레이크 포인트를 걸어서 메모리 상태를 확인 해보자!

leviathan3@leviathan:~$ r2 level3 -d
Process with PID 3579 started...
= attach 3579 3579
bin.baddr 0x08048000
Using 0x8048000
asm.bits 32
glibc.fc_offset = 0x00148
 -- Log On. Hack In. Go Anywhere. Get Everything.
[0xf7fd9a20]> aaaaa
[x] Analyze all flags starting with sym. and entry0 (aa)
[Warning: Invalid range. Use different search.in=? or anal.in=dbg.maps.x
Warning: Invalid range. Use different search.in=? or anal.in=dbg.maps.x
[x] Analyze function calls (aac)
[x] Analyze len bytes of instructions for references (aar)
[x] Check for objc references
[x] Check for vtables
[TOFIX: aaft can't run in debugger mode.ions (aaft)
[x] Type matching analysis for all functions (aaft)
[x] Propagate noreturn information
[x] Use -AA or aaaa to perform additional experimental analysis.
[Warning: Invalid range. Use different search.in=? or anal.in=dbg.maps.x
[x] Finding function preludes
[x] Enable constraint types analysis for variables

이후 pdf @sym.do_stuff로 들어가 바로 db 0x080485b7 이용해 저 strcmp 부분에서 브레이크 포인트를 걸어주자.

[0xf7fd9a20]> db 0x080485b7

 

 

 

 

6. 그다음 dc를 이용해 프로세서를 실행 시켜주면, 아까 걸어놓은 브레이크 포인트에서 프로그램이 멈출 것이다.

dc

 

 

 

7. px @esp 를 입력해 스택포인터의 레지스터를 확인 해보자.

px @esp

여길 보면 주소를 읽을때는 거꾸로 읽는데,  ffff d555  ,  ffff d560 이렇게 있다는 것을 알 수 있다. 따라서,

strcmp(0xffffd555,0xffffd560); 이렇게 되어있는 상황이다.

그럼 0xffffd560 에는 저기 잘 보면 7465 7374 가 들어있는데 이는 아스키코드고 환산하면 test 로 보여진다.

마찬가지로, 0xffffd555 를 보면 73 6e6c 0772 696e 7466 까지 해서 snlprintf 라는것을 알 수 있다.

따라서, 우리가 찾던 level3 비밀번호는 snlprintf 이다. 

 

 

 

8.다시 이제 돌아가 ./level3 에서 비밀번호에 snlprintf 를 넣고, cat /etc/leviathan_pass/leviathan4 를 통해 leviathan4의 비밀번호를 찾아내자!

leviathan3@leviathan:~$ ./level3
Enter the password> snlprintf
[You've got shell]!
$ id
uid=12004(leviathan4) gid=12003(leviathan3) groups=12003(leviathan3)
$ cat /etc/leviathan_pass/leviathan4
vuH0coox6m

 

 

 

9.위에서 얻은 leviathan4의 비밀번호: vuH0coox6m 를 통해 leviathan4에 접속하면 성공!

--[ Tools ]--

 For your convenience we have installed a few usefull tools which you can find
 in the following locations:

    * pwndbg (https://github.com/pwndbg/pwndbg) in /usr/local/pwndbg/
    * peda (https://github.com/longld/peda.git) in /usr/local/peda/
    * gdbinit (https://github.com/gdbinit/Gdbinit) in /usr/local/gdbinit/
    * pwntools (https://github.com/Gallopsled/pwntools)
    * radare2 (http://www.radare.org/)
    * checksec.sh (http://www.trapkit.de/tools/checksec.html) in /usr/local/bin/checksec.sh

--[ More information ]--

  For more information regarding individual wargames, visit
  http://www.overthewire.org/wargames/

  For support, questions or comments, contact us through IRC on
  irc.overthewire.org #wargames.

  Enjoy your stay!

leviathan4@leviathan:~$

이 방법 외에도 ltrace 를 이용해 아주  쉽게 문제를 해결 할 수 있다!

ltrace 명령

ltrace 명령을 사용하여 공유 라이브러리에 대한 동적 호출을 가로채고 기록할 수 있다. 

 

ltrace ./level3

위를 보면 바로, strcmp("test\n", "snlprintf\n") 가 보인다.

따라서 snlprintf 가 비밀번호라는 것을 알 수 있다!

 

 


Level4 -> Level5

1. ls -al 을 살펴보니 trash 라는 디렉토리가 있고, cd 로 접근해 살펴보니 bin이라는 setuid 파일이 존재한다. 해당 부분을 실행 시켜보자.

leviathan4@leviathan:~$ ls -al
total 24
drwxr-xr-x  3 root root       4096 Aug 26  2019 .
drwxr-xr-x 10 root root       4096 Aug 26  2019 ..
-rw-r--r--  1 root root        220 May 15  2017 .bash_logout
-rw-r--r--  1 root root       3526 May 15  2017 .bashrc
-rw-r--r--  1 root root        675 May 15  2017 .profile
dr-xr-x---  2 root leviathan4 4096 Aug 26  2019 .trash
leviathan4@leviathan:~$ cd .trash
leviathan4@leviathan:~/.trash$ ls
bin
leviathan4@leviathan:~/.trash$ ./bin
01010100 01101001 01110100 01101000 00110100 01100011 01101111 01101011 01100101                                                                                                                                                                                                           01101001 00001010

 

 

 

2. 아스키코드가 나오는데, 일단 우리는 공부의 목적이니, r2 로 리버싱해보자.

r2 bin

 

 

 

3.쭉 살펴보니, leviathan5의 비밀번호를 불러다가 계속 한 바이트씩 계속 돌면서 0과1 로 이진법으로 바꾼뒤, 우리에게 출력 해주는 것 같다.

 

 

 

4. 이진수로 된 아까 bin 내용이 결국 비밀번호인 것을 알았으니, 아스키코드로 변환 해주자.

python 활용

 

 

 

5. 위에서 얻은 leviathan5의 비밀번호:Tith4cokei 로 leviathan5에 접속 하면 성공!

--[ Tools ]--

 For your convenience we have installed a few usefull tools which you can find
 in the following locations:

    * pwndbg (https://github.com/pwndbg/pwndbg) in /usr/local/pwndbg/
    * peda (https://github.com/longld/peda.git) in /usr/local/peda/
    * gdbinit (https://github.com/gdbinit/Gdbinit) in /usr/local/gdbinit/
    * pwntools (https://github.com/Gallopsled/pwntools)
    * radare2 (http://www.radare.org/)
    * checksec.sh (http://www.trapkit.de/tools/checksec.html) in /usr/local/bin/checksec.sh

--[ More information ]--

  For more information regarding individual wargames, visit
  http://www.overthewire.org/wargames/

  For support, questions or comments, contact us through IRC on
  irc.overthewire.org #wargames.

  Enjoy your stay!

leviathan5@leviathan:~$

 

Comments