WAR GAME/Bandit

[OverTheWire: Bandit] level25 -> level26

jir4vvit 2020. 3. 7. 21:38

사용 툴: cygwin

 

1) 문제

level25 -> level26 문제

bandit25에서 bandit26으로 로그인하는 것은 쉬울수도 있다고 한다. 난 별로 안쉬웠다.

bandit26의 shell은 /bin/bash가 아니라고 한다.

그것이 무엇인지, 그것이 어떻게 작동하는지, 그리고 그것을 벗어나는 방법을 알아보자.

 


2) 문제풀이

bandit26으로 로그인해서 홈디렉터리를 살펴보았다.

bandit25@bandit:~$ ls -al
total 32
drwxr-xr-x  2 root     root     4096 Mar  6 13:56 .
drwxr-xr-x 41 root     root     4096 Oct 16  2018 ..
-rw-r-----  1 bandit25 bandit25   33 Mar  6 13:56 .bandit24.password
-r--------  1 bandit25 bandit25 1679 Oct 16  2018 bandit26.sshkey
-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-----  1 bandit25 bandit25    4 Mar  6 13:56 .pin
-rw-r--r--  1 root     root      675 May 15  2017 .profile
bandit25@bandit:~$ file bandit26.sshkey
bandit26.sshkey: PEM RSA private key
bandit25@bandit:~$

bandit26.sshkey라는 파일이 있길래 살펴봤더니, private key였다.

ssh로 접속할때 저 파일을 같이내면 bandit26에 바로 로그인할 수 있을 것같다.

 

bandit25@bandit:~$ ssh -i bandit26.sshkey bandit26@localhost
Could not create directory '/home/bandit25/.ssh'.
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:98UL0ZWr85496EtCRkKlo20X3OPnyPSB5tB5RPbhczc.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/bandit25/.ssh/known_hosts).
This is a OverTheWire game server. More information on http://www.overthewire.org/wargames

Linux bandit 4.18.12 x86_64 GNU/Linux

      ,----..            ,----,          .---.
     /   /   \         ,/   .`|         /. ./|
    /   .     :      ,`   .'  :     .--'.  ' ;
   .   /   ;.  \   ;    ;     /    /__./ \ : |
  .   ;   /  ` ; .'___,/    ,' .--'.  '   \' .
  ;   |  ; \ ; | |    :     | /___/ \ |    ' '
  |   :  | ; | ' ;    |.';  ; ;   \  \;      :
  .   |  ' ' ' : `----'  |  |  \   ;  `      |
  '   ;  \; /  |     '   :  ;   .   \    .\  ;
   \   \  ',  /      |   |  '    \   \   ' \ |
    ;   :    /       '   :  |     :   '  |--"
     \   \ .'        ;   |.'       \   \ ;
  www. `---` ver     '---' he       '---" ire.org


Welcome to OverTheWire!

If you find any problems, please report them to Steven or morla on
irc.overthewire.org.

--[ Playing the games ]--

  This machine might hold several wargames.
  If you are playing "somegame", then:

    * USERNAMES are somegame0, somegame1, ...
    * Most LEVELS are stored in /somegame/.
    * PASSWORDS for each level are stored in /etc/somegame_pass/.

  Write-access to homedirectories is disabled. It is advised to create a
  working directory with a hard-to-guess name in /tmp/.  You can use the
  command "mktemp -d" in order to generate a random and hard to guess
  directory in /tmp/.  Read-access to both /tmp/ and /proc/ is disabled
  so that users can not snoop on eachother. Files and directories with
  easily guessable or short names will be periodically deleted!

  Please play nice:

    * don't leave orphan processes running
    * don't leave exploit-files laying around
    * don't annoy other players
    * don't post passwords or spoilers
    * again, DONT POST SPOILERS!
      This includes writeups of your solution on your blog or website!

--[ Tips ]--

  This machine has a 64bit processor and many security-features enabled
  by default, although ASLR has been switched off.  The following
  compiler flags might be interesting:

    -m32                    compile for 32bit
    -fno-stack-protector    disable ProPolice
    -Wl,-z,norelro          disable relro

  In addition, the execstack tool can be used to flag the stack as
  executable on ELF binaries.

  Finally, network-access is limited for most levels by a local
  firewall.

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

  _                     _ _ _   ___   __
 | |                   | (_) | |__ \ / /
 | |__   __ _ _ __   __| |_| |_   ) / /_
 | '_ \ / _` | '_ \ / _` | | __| / / '_ \
 | |_) | (_| | | | | (_| | | |_ / /| (_) |
 |_.__/ \__,_|_| |_|\__,_|_|\__|____\___/
Connection to localhost closed.
bandit25@bandit:~$

읭? 접속할 때 처음부터 Enjoy your stay!까진 기존 접속과 똑같아보이는데

bandit26이라는 문구가 뜨면서 접속이 끊어졌다고 말한다.

뭐지..?

 

bandit25@bandit:~$ cat /etc/passwd | grep bandit26
bandit26:x:11026:11026:bandit level 26:/home/bandit26:/usr/bin/showtext
bandit25@bandit:~$ cat /usr/bin/showtext
#!/bin/sh

export TERM=linux

more ~/text.txt
exit 0
bandit25@bandit:~$

그래서 사용자들의 간단한 정보가 들어있는 /etc/passwd을 열어보았다.

bandit26:x:11026:11026:bandit level 26:/home/bandit26:/usr/bin/showtext

제일 오른쪽에 위치한 파일(일곱번째 필드)는 로그인 할 때 처음으로 실행되게할 프로그램을 말한다.

다른 필드가 궁금하다면 FTZ의 trainer6을 참고하라.

 

그래서 /usr/bin/showtext가 무슨 프로그램인지 한번 열어보았다.

#!/bin/sh

export TERM=linux

more ~/text.txt #여길 주목!!
exit 0

코드에서 주목한 곳을 보면 more 명령어가 쓰이고 있다.

more 명령어는 cat과 동일한 역할을 하는데, 다른 점이 있다면 한 페이지씩 읽는다는 것이다.

이것을 잘 활용하여 문제를 풀어야한다.

 

이렇게 화면을 줄이고 다시 접속하게 되면 한 페이지가 다 안들어오게 된다.

여기서 여러 명령어들을 실행할 수 있는데, v를 입력하게 되면 vi 에디터가 실행되게 된다.

놀랍지 않은가..!

 

:e /etc/bandit_pass/bandit26

저렇게 명렁하게 되면 vi에디터를 종료하지않고 해당파일을 편집할 수 있다.

여기서 /etc/bandit_pass/bandit26이라는 파일을 편집할 수 있다는 뜻이다.

이렇게 풀어도 되고 다른 방법으로 풀어도 된다.

 

:set shell=/bin/bash
:sh

저렇게 입력을 해주면 바로 bandit26의 쉘을 따낼 수 있다!

 

끝~

참고로 bandit level26->level27로 갈때

처음부터 바로 bandit26으로 접속이 안되기때문에 (bandit26으로 접속하려면 bandit25로 접속해서 문제를 풀고 bandit26으로 로그인해야한다는 뜻)

유의해야한다...