Computer Security

#12 Wargame bandit 9 (Level19 ~ Level21), setuid,nc 명령어 본문

Wargame:Bandit

#12 Wargame bandit 9 (Level19 ~ Level21), setuid,nc 명령어

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

Level19 -> Level20

Level19

다음 레벨에 액세스하려면 홈 디렉토리에서 setuid 바이너리를 사용해야 합니다. 
인수 없이 실행하여 사용법을 알아보세요.
이 레벨의 비밀번호는 setuid 바이너리를 사용한 후 일반적인 위치(/etc/bandit_pass)에서 찾을 수 있습니다.

 

일단 디렉토리에 있는 setuid를 사용해서 비밀번호를 찾아보자.


setuid란?



SetUID(s)란, 말 그대로 파일을 실행할 때 Set(적용해라) UID(파일의 소유자로)다.

다시말해, SetUID가 적용된 파일을 실행하면, 실행 파일이 끝날 때 까지
파일의 소유자의 UID가 된다. 

그럼 왜 "소유자 자체"가 아니라 "소유자 UID"로 바뀌는 것일까?

"UID"란 계정을 가리키는 "유일한 숫자"여서 "UID를 쓴다"는 것은, "UID가 가리키는 계정을 사용하는 것"과 마찬가지이기 때문이다.

이것이 얼마나 대단하고 위험 한 것이냐면 SetUID가 설정돼 있는 "root"의 파일을 실행하면
실행이 끝날 때 까지 실행하는 사람의 UID가 root의 UID "0"이 되어  root의 권한을 갖는 것이다.


1.일단 디렉토리가 있는지 확인하고,  ./bandit20-do 을 해본다.

bandit19@bandit:~$ ls
bandit20-do
bandit19@bandit:~$ ./bandit20-do
Run a command as another user.
  Example: ./bandit20-do id

 

 

2.bandit20의 권한을 빌려서 실행 시킬수 있으니, 비밀번호를 여는데 사용해보자.

bandit19@bandit:~$ ./bandit20-do cat /etc/bandit_pass/bandit20
GbKksEFF4yrVs6il55v6gwY5aVje5f0j

 

 

3.알아낸 비밀번호로 bandit20 접속하면 끝!

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

    * gef (https://github.com/hugsy/gef) in /usr/local/gef/
    * 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!

bandit20@bandit:~$

Level20 -> Level21

bandit20

홈 디렉토리에는 다음을 수행하는 setuid 바이너리가 있습니다.
명령줄 인수로 지정한 포트에서 localhost에 연결합니다. 
그런 다음 연결에서 텍스트 줄을 읽고 이전 수준(bandit20)의 암호와 비교합니다. 
암호가 정확하면 다음 레벨(bandit21)의 암호를 전송합니다.
참고: 자신의 네트워크 데몬에 연결하여 생각한 대로 작동하는지 확인하십시오.

 

1.일단 ls를 이용해 확인해보니 suconnect라는 파일이 있다. 

bandit20@bandit:~$ ls
suconnect

2.포트넘버를 입력하면 해당 포트에해당하는 통신을 시작한다고한다.

올바른 암호를 받으면 다음단계의 암호를 던져준다.

suconnect는 port이름을 넘겨서 그 포트로 연결하게 된다.

bandit20@bandit:~$ ./suconnect
Usage: ./suconnect <portnumber>
This program will connect to the given port on localhost using TCP. If it receives the correct password from the other side, the next password is transmitted back.

이제 포트를 열고 접속해야하는데, 리눅스 nc명령어를 알아보자!

 

nc 란?

  • Netcat(ornc)은 TCP 또는 UDP 프로토콜을 사용하여 네트워크 연결을 통해 데이터를 읽고 쓰는 명령줄 유틸리티다.
  • 네트워크 및 시스템 관리자 무기고에서 가장 강력한 도구 중 하나이며, 네트워킹 툴의 스위스 군용 칼이라고 할 수 있다. 
  • Netcat은 교차 플랫폼이며 리눅스, MacOS, 윈도우즈 및 BSD에서 사용할 수 있다.
  • Netcat을 사용하여 네트워크 연결을 디버그 및 모니터링하고, 열린 포트를 검사하고, 데이터를 전송하고, 프록시로 사용하는 등의 작업을 수행할 수 있다. 
  • Netcat 패키지는 MacOS 및 Ubuntu, Debian 또는 CentOS와 같은 널리 사용되는 Linux 배포에 미리 설치되어 있다.

 

사용법

$ nc [OPTIONS] [HOST] [PORT]

 

  • -n : 호스트 네임과 포트를 숫자로만 입력받는다.
  • -v : verbosity 를 증가 시킨다. 더 많은 정보를 얻을수 있다.
  • -o [filename]: 보내거나 받은 데이터를 헥스덤프하여 파일에 저장한다.
  • -u : TCP connection 대신에 UDP connection 이 이루어 진다.
  • -p [port number or name] : local-port 를 지정한다. 주로 -l 과 같이 사용하게 된다.
  • -s [ip address or DNS] : local ip address 를 지정한다. 모든 플렛폼에서 지원되지는 않는다.
  • -l : listen 모드로 nc을 띠우게 된다. 당연히 target host는 입력하지 않는다. -p와 같이 사용하게 된다. ncserver 로서 쓸때 사용.
  • -e [filename] : -DGAPING_SECURITY_HOLE 옵션으로 Make 되었을 때 사용가능하다.
  • -t : -DTELNET 옵션으로 컴파일 되었을 때 사용가능하다. telnetd에 접속이 가능하도록 접속시 telnet과 같은 협상과정을 거친다.
  • -i [interval time] : nc는 일반적으로 8K 씩 데이터를 보내고 받는데 그렇게 Standard input의 한 라인씩 interval time마다 보내게 된다.
  • -z : connection을 이루기위한 최소한의 데이터 외에는 보내지 않도록 하는 옵션.
  • -r : port 지정이 여러개로 되어 있으면 이때 scanning 순서를 randomize하고 (일반적으로 범위로 지정하면 높은 번호의 포트부터 스캔한다) 또한 -p 옵션에서 지정가능한 local portrandomize 한. 이때 주의 할 것은 -p-roverride 한다는 것이다.

3. 터미널을 하나를 더 열어서 한쪽에는 port를 열어준다.

nc

 

4.한쪽은 그 포트에 연결시켜서 첫번째 터미널에서 20레벨의 패스워드를 넣으면 2번째 연결된 터미널에서 21레벨의 패스워드를 다시 첫번째 터널로 보내준다!

bandit21 패스워드는 :  gE269g2h3mw3pwgrj0Ha9Uoqen1c9DGr 이다.

nc

 

5. 위에서 얻은 gE269g2h3mw3pwgrj0Ha9Uoqen1c9DGr 로 bandit21에 접속하면 성공!

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

    * gef (https://github.com/hugsy/gef) in /usr/local/gef/
    * 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!

bandit21@bandit:~$
Comments