티스토리 뷰

728x90
반응형

Introduction

Category : Web

Difficulty : easy

Description : There's a new trend of an application that generates a spooky name for you. Users of that application later discovered that their real names were also magically changed, causing havoc in their life. Could you help bring down this application?

문제 첫 페이지

Code Analysis

application/blueprints/routes.py 파일의 코드를 보면 GET 파라미터 들어온 text 값을 spookify 함수로 넘기고 있습니다.

@web.route('/')
def index():
    text = request.args.get('text')
    if(text):
        converted = spookify(text)
        return render_template('index.html',output=converted)
    
    return render_template('index.html',output='')

spookify 함수를 보면 아래와 같습니다.

def spookify(text):
	converted_fonts = change_font(text_list=text)

	return generate_render(converted_fonts=converted_fonts)

change_font 함수에서 문자들을 랜덤하게 아래와 같이 유니코드 또는 일반 문자로 font type을 변경하는 작업을 해줍니다.

그런 다음에 반환된 문자열들의 list를 generate_render 함수에 다시 넘겨주는데 해당 함수는 아래와 같습니다.

from mako.template import Template
# ...
def generate_render(converted_fonts):
	result = '''
		<tr>
			<td>{0}</td>
        </tr>
        
		<tr>
        	<td>{1}</td>
        </tr>
        
		<tr>
        	<td>{2}</td>
        </tr>
        
		<tr>
        	<td>{3}</td>
        </tr>

	'''.format(*converted_fonts)
	
	return Template(result).render()
    # ...

딱 보니 mako template 에서 SSTI 취약점이 발생하고 있는 것을 알 수 있습니다. mako template 명세서는 아래 문서를 참고하면 되겠습니다.

https://www.makotemplates.org/

 

welcome to Mako!

Website content copyright © by Michael Bayer. All rights reserved. Mako and its documentation are licensed under the MIT license. mike(&)zzzcomputing.com

www.makotemplates.org

${코드} 형태로 작성하여 임의 코드를 실행하고 그 결과 값을 return 할 수 있는 것으로 보여집니다.

위 결과는 5*5 한 결과가 실제로 렌더링 되어 25가 출력되는 것을 볼 수 있습니다. 이제 python 코드를 작성해서 flag 파일만 추출하면 되겠습니다.

Exploit

그냥 단순하게 ${open('/flag.txt').read()} 라고 입력하고 Spookify 버튼을 누르면 아래와 같이 Flag가 출력되는 것을 볼 수 있습니다.

 

더 나아가면 mako template 에도 self 객체를 이용한 system 함수 호출 등으로 이어지게 할 수도 있겠습니다.

 

- 끝 -

728x90
반응형
댓글