Bandit Level 25 → Level 26

장일영·2024년 5월 13일

Bandit

목록 보기
26/33

Level

Goal

Logging in to bandit26 from bandit25 should be fairly easy… The shell for user bandit26 is not /bin/bash, but something else. Find out what it is, how it works and how to break out of it.

bandit25에서 bandit26으로 로그인하는 방법은 매우 쉽다... bandit26 유저의 기본 쉘은 /bin/bash가 아닌 다른 것이다. 이것이 무엇인지, 어떻게 동작하며 어떻게 이를 사용할 수 있는지 알아내라.

Commands you may need to solve this level

ssh, cat, more, vi, ls, id, pwd

Exploit

bandit25@bandit:~$ ls -al
total 32
drwxr-xr-x  2 root     root     4096 Oct  5  2023 .
drwxr-xr-x 70 root     root     4096 Oct  5  2023 ..
-rw-r-----  1 bandit25 bandit25   33 Oct  5  2023 .bandit24.password
-r--------  1 bandit25 bandit25 1679 Oct  5  2023 bandit26.sshkey
-rw-r--r--  1 root     root      220 Jan  6  2022 .bash_logout
-rw-r--r--  1 root     root     3771 Jan  6  2022 .bashrc
-rw-r-----  1 bandit25 bandit25    4 Oct  5  2023 .pin
-rw-r--r--  1 root     root      807 Jan  6  2022 .profile

bandit25@bandit:~$ file bandit26.sshkey
bandit26.sshkey: PEM RSA private key

bandit26.sshkey 파일은 PEN RSA private key다. 따라서 이를 이용해 bandit26에 접속할 수 있을지도 모른다.

bandit25@bandit:~$ ssh -i ./bandit26.sshkey bandit26@localhost -p 2220

해당 키를 이용해 bandit26에 접근하면 "Connection to localhost closed." 메세지와 함께 종료된다.

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

bandit25@bandit:~$ file /usr/bin/showtext
/usr/bin/showtext: POSIX shell script, ASCII text executable

bandit26 계정의 기본 쉘을 확인해보니 /usr/bin/showtext로 지정되어 있는 것을 확인할 수 있다. 이 경우 bandit26 계정으로 로그인하면 bash shell이 실행되는 것이 아니라 /usr/bin/showtext가 실행된다.

bandit25@bandit:~$ cat /usr/bin/showtext
#!/bin/sh

export TERM=linux

exec more ~/text.txt
exit 0

이 파일이 실행되면 ~/text.txt 파일의 내용을 출력하고 exit 0으로 종료한다. 해당 파일(/home/bandit26/text.txt)은 bandit25 유저 권한으로는 읽을 수 없다. text.txt 파일의 내용은 알 수 없으나 현재 알 수 있는 것은 more 커맨드로 해당 파일을 전부 읽어오기 전까지는 bandit26 유저로 접속해 있을 수 있다. more 커맨드의 특성상 shell 화면에 내용을 전부 출력할 수 없으면 Enter Key를 눌러 다음 내용을 출력하게 된다. 전체 내용이 보여지기 전까지 bandit26 유저로 로그인 한 상태를 유지할 수 있다.

       v
           Start up an editor at current line. The editor is taken from the
           environment variable VISUAL if defined, or EDITOR if VISUAL is not
           defined, or defaults to vi(1) if neither VISUAL nor EDITOR is
           defined.

이 때 v를 입력해 에디터에 진입할 수 있고 에디터 내에서 vi 명령어 :e를 통해 권한이 있는 파일을 불러올 수 있다. :e /etc/bandit_pass/bandit26를 입력하면 권한이 있으므로 해당 파일의 내용을 읽을 수 있다.

아니면 :sh 커맨드를 이용할 수도 있다. 현재 shell 설정에 문제가 있으므로 :set shell=/bin/bash 커맨드로 shell을 지정하고, :sh 커맨드로 에디터를 빠져나가 shell에 진입할 수 있다. 이 경우 bandit26 권한을 가진 상태로 진입하게 되므로 패스워드에 접근할 수 있다.

0개의 댓글