OverTheWire Bandit
OverTheWire: Bandit를 리눅스 연습 겸 풀어봤다.
각 단계에서 얻은 password로 다음 레벨의 아이디(e.g.
bandit1)에 접속할 수 있다.
Level 0
The password for the next level is stored in a file called readme located in the home directory. Use this password to log into bandit1 using SSH. Whenever you find a password for a level, use SSH (on port 2220) to log into that level and continue the game.
ssh를 이용해서 bandit0 유저로bandit.labs.overthewire.org:2220에 접속하면 된다.
| |
home directory에 있는 readme라는 파일을 읽어 bandit1의 password를 찾아낸다. 찾아낸 password는 로컬 머신에 복사해둬야 한다.
| |
Level 1
The password for the next level is stored in a file called - located in the home directory
home directory의 - 파일(dashed file)을 읽어야 한다.
(ls -al로 home directory의 파일들을 확인)
-라는 이름의 파일은 cat -로 바로 읽게 되면 아무 반응이 없는데, -가 표준 입력 stdin을 의미해서 입력을 기다리는 상태가 되기 때문이다.
| |
아래와 같이 읽을 수 있다.
| |
Level 2
The password for the next level is stored in a file called
--spaces in this filename--located in the home directory
파일명에 공백이 있는 파일을 읽어야 한다. 공백 앞에 \를 붙여야 읽을 수 있다.
| |
Level 3
The password for the next level is stored in a hidden file in the inhere directory.
~/inhere 디렉토리의 숨은 파일을 읽어야 한다. ls는 숨은 파일은 알려주지 않으므로 ls -a를 사용해야 한다.
| |
주로 ls -al을 사용하는데, l 옵션은 권한, 소유자, 그룹, 크기, 시간 등을 한 줄씩 출력해줘서 읽기 편한듯,,
Level 4
The password for the next level is stored in the only human-readable file in the inhere directory. Tip: if your terminal is messed up, try the “reset” command.
human-readable 파일을 읽어야 한다.
file 커맨드로 파일의 MIME 타입 등의 정보를 알 수 있다.
| |
파일 하나만 ASCII text으로 사람이 읽을 수 있다.
Level 5
The password for the next level is stored in a file somewhere under the inhere directory and has all of the following properties:
inhere 디렉토리에서 아래 조건을 만족하는 파일을 찾아서 읽어야 한다.
| |
find .는 현재 디렉토리 하위의 모든 파일과 디렉토리들을 보여준다.
| |
-type: 검색할 타입을 지정한다.
| type | 의미 |
|---|---|
f | 일반 파일 |
d | 디렉토리 |
l | 심볼릭 링크 |
c | 문자 디바이스 |
b | 블록 디바이스 |
p | 파이프(FIFO) |
s | 소켓 |
-size: 사이즈로 검색한다.
| size | 의미 |
|---|---|
c | bytes |
b | 512-byte blocks |
k | kilobytes (1024B) |
M | megabytes |
G | gigabytes |
-executable: 실행 가능한 것을 의미하며 앞에 !을 붙여 부정으로 바꿀 수 있다.
Level 6
The password for the next level is stored somewhere on the server and has all of the following properties:
server 내의 어딘가에 있는 파일 중 아래 조건을 만족하는 파일을 읽어야 한다.
| |
| |
-user, -group로 owner 검색이 가능하다.
2>/dev/null를 쓰지 않으면 find 커맨드 실행 중 권한이 없는 디렉토리/파일을 만날 때 에러 메시지 Permission Denied를 stderr(FD 2)로 출력한다.
2>/dev/null은 sderr 2를 /dev/null로 리다이렉션(>)하여 에러 출력이 버려지도록 한다.
Level 7
The password for the next level is stored in the file data.txt next to the word millionth
겁나 큰 용량의 data.txt 파일 내에서 millionth라는 단어의 옆에 써있는 password를 읽어야 한다.
| |
Level 8
The password for the next level is stored in the file data.txt and is the only line of text that occurs only once
data.txt 에서 유일한 line을 찾아야 한다.
| |
sort로 파일의 line들을 정렬한다. 여러 옵션을 추가로 사용할 수 있다.
uniq 커맨드는 중복되는 line을 제거하고 출력해준다. -d는 중복된 내용만 출력, -u는 고유한 내용만 출력, -i는 대소문자 구분 무시 옵션이다.
Level 9
The password for the next level is stored in the file data.txt in one of the few human-readable strings, preceded by several ‘=’ characters.
data.txt에서 읽을 수 있는, =가 앞에 있는 문자열만을 출력해야 한다.
| |
strings는 파일에 포함된 string을 뽑아낼 수 있다.
Level 10
The password for the next level is stored in the file data.txt, which contains base64 encoded data
base64 인코딩된 문자열이 data.txt에 들어있다.
| |
base64 -d 커맨드로 문자열을 디코딩하여 원래 바이너리를 출력한다.
Level 11
The password for the next level is stored in the file data.txt, where all lowercase (a-z) and uppercase (A-Z) letters have been rotated by 13 positions
시저 암호가 적용된 data.txt 파일을 해석해야 한다. Rot13이므로 A → N Z→ M으로 치환한다.
정규식은 아니라고 한다.. (tr에서 쓰인 규칙일 뿐)
| |
Level 12
The password for the next level is stored in the file data.txt, which is a hexdump of a file that has been repeatedly compressed. For this level it may be useful to create a directory under /tmp in which you can work. Use mkdir with a hard to guess directory name. Or better, use the command “mktemp -d”. Then copy the datafile using cp, and rename it using mv (read the manpages!)
hexadump file을 압축 해제해가면서 원본 데이터 파일을 얻어야 한다.
| |
file *로 얻은 파일이 어떤 방식으로 압축된 파일인지 확인 후 이에 맞는 압축 해제 방식을 사용해서 text 파일을 얻을 때까지 반복한다.
| |
위 3개만 반복해서 나왔던 듯.. 10번 정도 하면 text 파일이 나온다.
Level 13
The password for the next level is stored in /etc/bandit_pass/bandit14 and can only be read by user bandit14. For this level, you don’t get the next password, but you get a private SSH key that can be used to log into the next level. Look at the commands that logged you into previous bandit levels, and find out how to use the key for this level.
/etc/bandit_pass/bandit14를 읽으면 되지만.. bandit13으로는 읽을 수 없다..
| |
$HOME 디렉토리에 ssh key를 하나 주는데, 이걸로 bandit14 아이디로 접속할 수 있다.
| |
근데 sshkey.private의 권한이 640으로 설정되어 있는데, private key를 ssh 접속에 사용하기 위해선 권한이 600이어야 한다!
| |
sshkey.private 내용을 local 머신에 복사해와서 아무 파일에 저장해두고 600으로 바꾸고 bandit14에 접속해서 password를 획득한다.
| |
Level 14
The password for the next level can be retrieved by submitting the password of the current level to port 30000 on localhost.
localhost:30000으로 level 14의 passwd를 보내면 된다.
nc를 이용해서 30000번 포트로 비밀번호를 보내면 다음 레벨 비밀번호를 알려준다
| |
Level 15
The password for the next level can be retrieved by submitting the password of the current level to port 30001 on localhost using SSL/TLS encryption.
Helpful note: Getting “DONE”, “RENEGOTIATING” or “KEYUPDATE”? Read the “CONNECTED COMMANDS” section in the manpage.
Level 14와 비슷하지만 이번 문제는 SSL/TLS encryption을 해서 30001포트에 보내야 한다.
openssl로 암호화 세션을 먼저 생성한다.
| |
s_client는 SSL/TLS 세션을 연결하겠다는 뜻이다.
세션이 연결되면 level14 password를 붙여넣는다.
Level 16
The credentials for the next level can be retrieved by submitting the password of the current level to a port on localhost in the range 31000 to 32000. First find out which of these ports have a server listening on them. Then find out which of those speak SSL/TLS and which don’t. There is only 1 server that will give the next credentials, the others will simply send back to you whatever you send to it.
Helpful note: Getting “DONE”, “RENEGOTIATING” or “KEYUPDATE”? Read the “CONNECTED COMMANDS” section in the manpage.
31000 ~ 32000번 사이의 포트 중 LISTENING 상태이고 SSL/TLS 응답이 가능한 하나의 포트를 찾아야 한다. 이 중에 하나만 진짜인데, 나머지는 응답으로 내가 보낸 문자를 그대로 돌려준다.
| |
후보가 몇 개 없어서 하나하나 해봤다.
| |
openssl s_client는 보내는 문자열 시작이 k이면 데이터가 아니라 제어 명령으로 인식한다고 한다. level 16 password가 k로 시작해서 KEYUPDATE라는 응답을 받게 된다.
이를 피하려면 -quiet 옵션을 붙여서 인터렉티브 모드로 들어가지 않도록 해야 한다.
Correct!가 나오면 RSA private key를 준다. (비밀번호가 아니라 credentials를 준다.)
Level 17
There are 2 files in the homedirectory: passwords.old and passwords.new. The password for the next level is in passwords.new and is the only line that has been changed between passwords.old and passwords.new
NOTE: if you have solved this level and see ‘Byebye!’ when trying to log into bandit18, this is related to the next level, bandit19
key라는 파일을 만들고 안에 private key를 붙여넣고 -i 옵션을 이용해서 ssh 접속한다.
| |
diff로 두 파일을 한 line씩 비교해서 다른 라인을 찾는다. password.new에 있는 라인이 password다.
| |
Level 18
The password for the next level is stored in a file readme in the homedirectory. Unfortunately, someone has modified .bashrc to log you out when you log in with SSH.
누군가 장난을 쳐서 로그인하자마자 로그아웃된다. readme를 읽으면 되므로 ssh 뒤에 command를 달아서 즉시 출력한다.
command는 ;로 여러 명렁을 구분할 수 있다.
| |
Level 19
To gain access to the next level, you should use the setuid binary in the homedirectory. Execute it without arguments to find out how to use it. The password for this level can be found in the usual place (/etc/bandit_pass), after you have used the setuid binary.
homedirectory에 있는 setuid binary를 이용하면 된다. 예시를 보려면 arguments 없이 써보라고 한다.
| |
whoami를 실행하니 다음 레벨 유저인 bandit20이 나온다. setuid binary는 바이너리 파일의 소유자 권한으로 명령을 실행할 수 있는 파일이다. 위험하다.
| |
Level 20
There is a setuid binary in the homedirectory that does the following: it makes a connection to localhost on the port you specify as a commandline argument. It then reads a line of text from the connection and compares it to the password in the previous level (bandit20). If the password is correct, it will transmit the password for the next level (bandit21).
NOTE: Try connecting to your own network daemon to see if it works as you think
suconnect라는 setuid binary는 임의 포트로 요청 시 요청받은 포트에서 bandit20의 password를 입력하면 다음 단계 password를 주는 파일이다.
ssh 세션 두 개를 열어야 한다.
| |
Level 21
A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.
주기적으로 실행되는 crontab이 힌트를 줄 것이다./etc/cron.d를 보라니깐 거기로 가본다.
| |
cronjob_bandit22를 까본다. (-644로 other이 읽을 수 있다.)
| |
| |
sh 파일을 까보면 /etc/bandit_pass/bandit22 파일을 /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv에 매 분마다 넣고 있음을 볼 수 있다.
| |
Level 22
A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.
NOTE: Looking at shell scripts written by other people is a very useful skill. The script for this level is intentionally made easy to read. If you are having problems understanding what it does, try executing it to see the debug information it prints.
남이 쓴 sh를 읽는 건 useful skill이다!
| |
| |
그냥 shell 스크립트에 적힌대로 따라해서 비밀번호 위치를 알아내(면 안된다!)..
sh 내에서 실행한 whoami는 crontab으로 실행되고 있는데 crontab을 실행시키는 주체가 bandit23이다. (* * * * * bandit23 /usr/bin/cronjob_bandit23.sh &> /dev/null)
따라서 myname=bandit23으로 넣어줘야 한다.
| |
Level 23
A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.
NOTE: This level requires you to create your own first shell-script. This is a very big step and you should be proud of yourself when you beat this level!
NOTE 2: Keep in mind that your shell script is removed once executed, so you may want to keep a copy around…
습관적으로 열어본다.
| |
| |
cronjob_bandit24.sh를 열어 보면 긴 스크립트가 나오는데, /var/spool/bandit24/foo 디렉토리의 모든 파일(*, .*)을 순회하면서 ., ..를 제외한 파일들에 대해 owner가 bandit23이면 timeout 60초를 걸고 -s 9 (SIGKILL) 강제종료하고 그 파일을 실행시킨 후 강제 삭제한다.
우리가 읽어야 할 파일이 /etc/bandit_pass/bandit24에 있는데 bandit24로 읽을 수 있다.
crontab은 bandit24로 실행되고 있고 /foo 하위 모든 파일을 실행시키므로, bandit24이 실행 가능한 shell script를 만들고 /foo 하위에 넣어둔 뒤 password 파일을 우리가 원하는 파일로 쓰도록 하면 된다.
| |
/tmp 디렉토리는 other의 권한이 -wt인데 여기서 t는 sticky bit로, /tmp 하위 파일들은 owner만이 삭제가 가능하다. /tmp/grrru.sh를 만든다.
| |
| |
대충 비밀번호 가져올 파일 하나 생성해서 넣어주는 식으로 만들었다. grrru.sh를 실행할 주체는 bandit24(cronjob이 실행하므로)이므로 other에 x권한을 줘야 한다.
| |
crontab이 순회하는 디렉토리로 grrru.sh를 복사한다.
| |
이러고 1분 기다리면 cronjob이 돌아서 grrru.sh를 실행해준 뒤 삭제한다.
| |
Level 24
A daemon is listening on port 30002 and will give you the password for bandit25 if given the password for bandit24 and a secret numeric 4-digit pincode. There is no way to retrieve the pincode except by going through all of the 10000 combinations, called brute-forcing.
You do not need to create new connections each time
localhost:30002로 $PASSWORD 0000 꼴의 데이터를 보내면 답을 주는데, 정답 pin을 찾아야 다음 비밀번호를 알려준다.
| |
0000 ~ 9999를 하나씩 보낼 수도 있지만, connection을 한 번 만들면 pin 하나 보낼 때마다 또 연결할 필요가 없다고 하니 script를 작성해서 한 번에 보내보자.
| |
grrru24 파일을 만들고 한 줄씩 전송할 문자열을 생성해둔다.
| |
뭔가 중간에 Correct!가 나와서 못찾을 줄 알았는데 맨 아랫 줄에 나왔다. (사람마다 다를수도,,)
| |
Level 25
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.
NOTE: if you’re a Windows user and typically use Powershell to
sshinto bandit: Powershell is known to cause issues with the intended solution to this level. You should use command prompt instead.
bandit26의 기본 쉘은 /bin/bash가 아니다. 어떤 쉘을 기본으로 사용하는지 먼저 확인한다.
| |
/usr/bin/showtext를 기본 쉘로 사용하고 있다.
| |
머신에 로그인한 직후 실행되는
기본 쉘(login shell)은/bin/bash같은 쉘 프로그램뿐 아니라, 실행 권한이 있는 임의의 실행 파일(ELF 바이너리 또는.sh스크립트)도 될 수 있다.
1 2file /usr/bin/showtext /usr/bin/showtext: POSIX shell script, ASCII text executable
/usr/bin/showtext에서는 more ~/text.txt를 실행 후 즉시 종료된다.
bandit25로 접속하면 HOME 디렉토리에 bandit26.key가 있는데 이걸로 Level 25를 풀어야 하므로 scp로 local에 가져오거나 bandit25 쉘에서 바로 ssh 접속하거나 해야 한다.
| |
위 ssh 접속을 하면 바로 나와지는데, bandit26이라는 아스키아트(?)를 보여주고 바로 종료된다. more 커맨드를 통해 출력된 그림인데, more 커맨드는 표준 출력이 TTY(터미널)인 경우에 화면 높이를 계산해서 출력해야 할 줄 수보다 화면 높이가 작다면 --More--이 뜨면서 pager 모드로 전환된다고 한다.
그래서 화면 높이를 작게 줄여서 다시 접속한다. ASCII 아트 높이가 6이니깐 6보다 작으면 될 듯 하다.
| |
--More--과 함꼐 Paging 모드로 들어왔다. Enter를 치면 한 줄씩 보여주고, 위 화살표를 누르면 다시 올라간다.
리눅스에서 more 명령어 실행 중 v를 누르면 현재 위치에서 vi 편집기를 실행하여 파일을 편집할 수 있다.
| |
vi 편집기에서 :set shell?를 하면 현재 쉘 설정을 볼 수 있다.
| |
현재 접속 id가 bandit26이므로 vi 편집기 내에서 shell을 /bin/bash로 바꾸면 :sh를 통해 현재 vi 세션 내에서 새로운 쉘을 실행할 수 있다.
| |
원하는 비밀번호를 가져온다.
| |
여기서 나가지 않고 Level 26을 바로 할 수 있다.
Level 26
Good job getting a shell! Now hurry and grab the password for bandit27!
HOME으로 가서 ls를 실행하면 bandit27-do가 보인다.
| |
저번에 봤던 setuid파일이다.
| |
리눅스/유닉스 계열 운영체제에서 setuid같은 특정 권한이 설정된 파일을 실행하면 일시적으로 파일 소유자나 다른 사용자의 권한(EUID)를 획득해서 시스템 리소스에 접근할 수 있다.euid=11027(bandit27)로 되어있으므로 이 파일을 이용해서 bandit27 파일을 읽을 수 있다.
| |
Level 27
There is a git repository at
ssh://bandit27-git@bandit.labs.overthewire.org/home/bandit27-git/repovia the port2220. The password for the userbandit27-gitis the same as for the userbandit27.Clone the repository and find the password for the next level.
bandit27로 접속해서 git clone을 시도했다.
| |
근데 이러니깐 localhost에서 localhost로 ssh 접속은 리소스 절약을 위해 차단되었다고 한다.
| |
그래서 그냥 local 머신에서 바로 git clone해서 README를 읽었다.
Level 28
There is a git repository at
ssh://bandit28-git@bandit.labs.overthewire.org/home/bandit28-git/repovia the port2220. The password for the userbandit28-gitis the same as for the userbandit28.Clone the repository and find the password for the next level.
git clone 후 README.md를 까봤다.
| |
| |
README.md에는 별 내용이 없다.git log를 확인했더니 fix info leak라는 커밋이 있다.
| |
이전 커밋으로 되돌려 본다.
| |
Level 29
There is a git repository at
ssh://bandit29-git@bandit.labs.overthewire.org/home/bandit29-git/repovia the port2220. The password for the userbandit29-gitis the same as for the userbandit29.Clone the repository and find the password for the next level.
Level 28과 똑같이 git을 뒤져가며 찾는 문제다.dev branch로 바꾼 후에 log를 뒤져서 찾아냈다.
| |
git log에서 -p 옵션은 diff를 상세하게 보여주는 옵션이다.
| |
Level 30
There is a git repository at
ssh://bandit30-git@bandit.labs.overthewire.org/home/bandit30-git/repovia the port2220. The password for the userbandit30-gitis the same as for the userbandit30.Clone the repository and find the password for the next level.
git branch -rvv해도 branch는 하나밖에 없고 git log를 봐도 아무것도 없다.
| |
git --no-pager로 보면 commit id를pager없이 바로 출력할 수 있다.
git tag를 하니 단서가 나온다.
| |
그냥 바로 password같은 문자열이 나온다..!
| |
Level 31
There is a git repository at
ssh://bandit31-git@bandit.labs.overthewire.org/home/bandit31-git/repovia the port2220. The password for the userbandit31-gitis the same as for the userbandit31.Clone the repository and find the password for the next level.
git clone해오면 README.md가 있다.
| |
push를 하라는데, 이게 맞나 긴가민가했지만 써있는대로 key.txt 만들고 May I come in?이라고 쓰고 add했다.
| |
.gitignore에서 *.txt가 있어서 -f를 써야 한다.
| |
push는 안되지만 password를 알려준다.
Level 32
After all this
gitstuff, it’s time for another escape. Good luck!
git 문제는 끝났다!
| |
접속하면 UPPERCASE SHELL라는 쉘이 실행된다. 입력하는 모든 command를 대문자로 치환하는 쉘이다.
| |
문제 해결 후 회상
$0을 입력하면 현재 실행 중인 쉘의 실행 경로가 입력되어 쉘이 재실행된다.
이 문제의 세팅을 보면 login shell은 uppershell이다.
| |
| |
$0을 실행하면 sh가 실행되는데, 이는 이 명령을 실행하던 shell이 sh였다는 뜻이다.
로그인 직후 uppershell가 실행되고 얘가 sh를 실행했다는 뜻(?)인 듯 하다.
여기서 /usr/bin/bash를 실행하면 bandit에서 보던 bash shell이 실행된다.
| |
해결 방법
| |
exit하는 것도 어렵다
| |
| |
Level 33
At this moment, level 34 does not exist yet.
That’s it!