2.log

[bandit] level 15 - 19 본문

HACKING/Bandit

[bandit] level 15 - 19

2.log 2023. 4. 9. 17:35
728x90
반응형

 

 ✅ 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 encryption

 

 

🖊︎ OpenSSL 의 HeartBeat 취약점 이용한 exploit

 

 

netstat -tnl 명령어로 현재 연결된 포트 확인하니 30001번 포트가 LISTEN(연결 대기중) 상태였음

문제에서 localhost가 ssl encrytion 을 사용중이라고 하였으므로, ssl 로 구현된 서버에 원격접속하기 위해 명령어 입력

 

$ openssl s_client -connect 127.0.0.1:30001

 

 

Certificate chain

 

Server certificate

 

SSL/TLS  Connect Info

 

 

연결에 성공하자 인증 체인정보, 서버측 인증정보, SSL/TLS 연결과 관련된 정보가 출력되었고 맨 아랫줄에서 아래와 같이 콘솔창이 잠시 멈추는데 이 때 R을 입력해 Client_Initiated_Renegotiation vulnerability 여부를 확인함

 

 

 

 

renegotiate:wrong 어쩌구... 라며 에러메시지 뜨면 재협상 불가능한, 안전한 상태라는 것임

 

 

출처 : https://www.feistyduck.com/library/openssl-cookbook/online/ 73p

 

 

만약 서버가 해당 renegotiation 을 지원할 경우 저렇게 재협상이 진행되고 저게 가능하다는 것은 곧 취약한 상태임을 의미

(Insecure_Client_Initiated_Renegotiation 에 해당)

 

 

아무쪼록, 해당 취약점은 이용할 수 없으니 다음으로 HeartBleed 취약점을 시도하기 위해 아래 명령 입력 (-tlsextdebug로 연결 시 서버측 상태 출력하고 heartbeat request에 응답하는 서버측 메시지내용 확인하기 위해 -msg 옵션 줌)

 

$ openssl s_client -connect 127.0.0.1:30001 -tlsextdebug -msg

 

 

마지막 라인에 B를 입력해 heartbeat res/req 확인

 

 

 

 

중간에 'Wrong! Please enter the corret current password' 라는 문구가 눈에 띔

아무래도 read R BLOCK 다음 라인에 B 대신 bandit15 접속할 때 썼던 password 넣어보면 될 듯함

 

 

 

 

password 일치하며, 다음 level 로 가는 암호 나옴

 

 

 

 

bandit+)

SSL/TLS 에 대하여 / https://kimkmg.tistory.com/40

netstat 상태값과 옵션 정리 / https://kimkmg.tistory.com/41

Heartbleed 취약점에 대하여 / https://kimkmg.tistory.com/42

Insecure_Client_Initiated_Renegotiation vulnerability / https://kimkmg.tistory.com/43

OpenSSL 과 s_client / https://kimkmg.tistory.com/44

 

 

[참고] ** OpenSSL Cookbook / https://www.feistyduck.com/library/openssl-cookbook/online/

 

 

OpenSSL Cookbook 3rd Edition

 

www.feistyduck.com

 

 


 

 ✅ 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 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.

 

 

 

🖊︎  vim 과 임시디렉토리 활용한 exploit

 

 

nmap 명령어 옵션 이용해 검색 범위를 좁혀 포트조회를 하니 총 5개의 포트번호가 나옴

$ nmap -p [port범위] [ip]

 

$ openssl s_client -connect 127.0.0.1:[port]

 

위 명령어로 하나씩 접속 시도해보니 3개는 자동종료되어 버리고,

다른 하나는 문제에서 말한대로 보낸 것과 동일한 메시지만  계속 출력됨

 

남은 31790 포트로 접속해 디버깅 메시지 함께 출력하는 옵션(-debug -msg)을 주고 heartbeating 내용을 보니

중간에 'Wrong! Please enter the correct current password' 라는 문구가 눈에 띔

 

 

 

 

31790 포트에서 인증서 얻어낼 수 있을 듯 하여 접속할 때 사용한 비밀번호 넣어줌

 

 

 

인증정보 나타남

이제 해당 인증수단으로 bandit17 서버에 원격접속 하면 될 듯 하여 총 3가지 방법 시도

 

 

[1] 2220 -> 31790 포트변경하여 bandit17로 ssh 접속시도 (실패)

 

 

특정 포트로 변경하려면 sshd_config 파일에 해당 포트 추가해주어야 함

 

 

 

 

하지만  sshd_config 파일에 31790 포트 추가하려고 보니 현재 권한으로는  read만 할 수 있는 상태라 파일 수정 불가했음

 

 

 

[2] 포트포워딩 시도 (실패)

 

ssh -L 31790:localhost:2220 bandit17@bandit.labs.overthewire.org -p 2220

 

 

현재 로컬호스트의 listening 포트 중 인증서가 저장된 31790 포트로 bandit19 서버에 SSH 로컬 포트포워딩 시도했으나 인증서의 경로지정이 제대로 안되어 있는지 거부당함..

뭔가 31790 포트로 접속하면 알아서 인증서 경로인식해서 바로 접속 될 줄 알았는데 이 방법도 안되는 걸 확인했으니 마지막 방법을 써야할 듯 함 (사실 마지막 방법을 제일 처음쓰는게 현명했을 듯)

 

 

 

 

[3] /tmp 폴더에 인증 파일 추가하여 ssh 접속 (성공)

 

우선 vim 창 켜서 임시 디렉토리에 'key'라는 파일명으로 복사한 인증정보 저장

$ vim /tmp/key

 

 

그런 다음 key 경로지정해서 접속하는 방법으로 bandit17 서버에 ssh 접속

 

$ ssh -i /tmp/key bandit17@bandit.labs.overthewire.org -p 2220

 

 

 

성공

 

 

bandit+)

sshd_config 파일 수정하여 포트 변경하기 / https://kimkmg.tistory.com/52

리다이렉트와 포워드 차이 / https://kimkmg.tistory.com/45

포트포워딩과 SSH 포트포워딩/터널링에 대하여 / https://kimkmg.tistory.com/53

 


 

 ✅ Level 17 : There are 2 files in the homedirectory:passwords.old and passwords.new. The password for the next level is inpasswords.newand is the only line that has been changed betweenpasswords.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

 

 

🖊︎  diff 명령어로 파일 간 차이점 찾기

 

diff -c 명령어로 passwords.old 와 passwords.new 두 파일간 차이점 출력

$ diff -c passwords.new passwords.old

 

 

! 표시된 라인이 차이 존재하는 라인

 

 

-d 옵션으로 검색하면 해당 라인만 좀 더 깔끔하게 확인 가능

 

$ diff -d passwords.new passwords.old

 

 


 

<bandit 18 접속 후 화면>

 

bandit18 접속시 최종 화면

 

 

해당 암호로 bandit18 서버에 접속 성공 했으나 바로 종료됨

Note 메시지에서 언급되었듯 Byebye! 아래 라인을 보니 'Connection to bandit.labs.overthewire.org closed' 라고 함

아무래도 다음 문제 푸는 힌트가 될 듯 함

 

 

 

bandit +)diff 명령어 / https://kimkmg.tistory.com/46

 


 

 ✅ Level 18 : The password for the next level is stored in a filereadmein the homedirectory. Unfortunately, someone has modified.bashrcto log you out when you log in with SSH.

 

 

🖊︎  bash 취약점 이용한 exploit

 

 

 

ssh 접속 시 bash 옵션을 주어 실행하고, 커맨드 라인에 ls -al 명령을 주니 바로 readme 파일 발견 할 수 있었음

 

 

 

 

이는 bash 옵션으로 ssh 실행 시 로그인 shell 이 아닌 새 bash shell 이 실행되어, bash shell 이 시작될 때 자동으로 실행되는 .bashrc 파일 역시 프로세스 종료시키도록 수정된 .bashrc 파일이 아닌 새로운 bash shell 의 .bashrc 파일이기에 가능한 일임

 

 

 

bandit+ )

Shell 과 bashrc / https://kimkmg.tistory.com/48

Bash 취약점에 대하여 / https://kimkmg.tistory.com/51

 


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.

 

 

🖊︎  setuid 취약점 활용한 exploit

 

 

bandit20-do라는 파일은 본래 권한없는 사용자도 실행 시 소유자권한으로 실행가능한 setuid bit (s) 가 설정되어 있음

파일 실행하는 명령으로  ./ [file명] 으로 실행해 봄 

 

 

 

다른 사용자처럼 실행하라는 문구가 뜨면서 example 이 나와서 저대로 다시 실행해 봄

 

 

 

 

실행해보니 uid, gid, groups 는 bandit19 인데 euid(effective user id) 는 bandit20 으로 나타남

setuid bit 가 설정되어 있었기에 파일 실행을 명령한 사용자는 bandit19 이지만 프로그램 실행시에 사용자를 bandit20으로 변경해 실행한 것임

 

그렇다면 암호가 있는 /etc/bandit_pass/bandit20 파일에 접근할 때에도 상기의 방법을 적용하여 bandit20 의 권한으로 실행 할 수 있을 것이기에, ./bandit20-do 자체를 하나의 실행 명령어로 하여 아래와 같이 암호가 있는 파일을 cat 해 봄

 

$ ./bandit20-do cat /etc/bandit_pass/bandit20

 

 

 

성공

 

 

 

bandit+)

setuid 에 대하여 / https://kimkmg.tistory.com/49

install 명령어로 권한변경하는 법 / https://kimkmg.tistory.com/50

 

 

 

 

728x90
반응형

'HACKING > Bandit' 카테고리의 다른 글

[bandit] level 21 ~ 22  (1) 2023.04.22
[bandit] level 20  (0) 2023.04.16
[bandit] level 10 ~ 14  (0) 2023.03.24
[bandit] level 00 - 09  (0) 2023.03.21
Comments