Dreamhack - xss-2

·2025년 7월 14일

Dreamhack-Writeups

목록 보기
20/52
post-thumbnail

xss-2

문제 링크

https://dreamhack.io/wargame/challenges/268

문제 설명

여러 기능과 입력받은 URL을 확인하는 봇이 구현된 서비스입니다.

XSS 취약점을 이용해 flag.txt, FLAG 변수에 있는 플래그를 획득하는 문제입니다.

풀이과정

  1. 생성된 서버에서 flag에 들어가면 입력 할 수 있는 url 칸이 나옵니다. 이 입력 칸을 이용해 xss 공격을 하는 문제입니다.

  2. 생선된 서버의 vuln(xss) page 에 접속해 보았습니다.

    URL에 script 공격이 포함되어 있지만, <script>alert(1)</script> 이 실행되지 않았습니다.
    이를 통해 <script> 태그를 통한 공격은 방어가 되어 있음을 확인하였습니다.
    즉, XSS 공격을 위해서는 다른 우회법이 필요함을 알 수 있습니다.

  3. app.py 를 확인해 봅니다.

    def check_xss(param, cookie={"name": "name", "value": "value"}):
     url = f"http://127.0.0.1:8000/vuln?param={urllib.parse.quote(param)}"
     return read_url(url, cookie)

    이 코드를 통해 내부적으로 http://127.0.0.1:8000/vuln?param=... 을 열어봄을 알 수 있습니다.
    이어서 리턴되는 read_url 함수를 확인해 보았습니다.

    def read_url(url, cookie={"name": "name", "value": "value"}):
     ...
     driver.get("http://127.0.0.1:8000/") 
     driver.add_cookie(cookie)            
     driver.get(url)                     
    

    이 코드를 통해 http://127.0.0.1:8000/ 에 코드를 심는다는걸 확인할 수 있습니다.

  4. flag에 접속해 <img src=x> 로 XSS 공격을 시도합니다.

    • <script>를 활용한 공격을 못하기에, 그 우회로 <img> 태그를 이용합니다.
    • onerror<img> 같은 태그에서 이미지 파일을 불러오려다가 실패하면, onerror 속성에 들어있는 자바스크립트가 자동 실행 됩니다.
    • 이를 통해 scr엔 없는 이미지 값을 입력하고, onerror 를 통해 진짜 접근하고 싶던 링크가 실행되게 할 수 있습니다. fetch 는 자바스크립트에서 HTTP 요청을 보낼 수 있게 해주는 함수입니다.
    • fetch('http://127.0.0.1:8000/memo?memo='+document.cookie) 로 보내면 서버 내부의 memo_text 변수에 플래그가 저장됩니다.
    • +는 자바스크립트에서 문자열 이어붙이기 연산자이며, 최종적으로 http://127.0.0.1:8000/memo?memo=helloflag=DH{...}로 이동하게 만들어, 플래그가 메모 페이지에 기록되도록 합니다.
  5. memo 에 접속해보니 성공적으로 플래그 값을 획득할 수 있었습니다.


배운점

  • XSS의 실전 활용을 배워볼 수 있었습니다.
  • +는 자바스크립트에서 문자열 이어붙이기 연산자임을 알게 되었습니다.
  • document.cookie를 통해 브라우저의 쿠키를 탈취할 수 있다는 것을 확인할 수 있었습니다.
  • <script>가 막혀 사용이 불가능 한 경우, 다른 방법을 이용해 충분히 우회가 가능함을 배웠습니다.
  • 실전 보안에서는 단순히 특정 키워드를 막는다고 해서 보안이 되는게 아니란걸 배웠습니다.

Summary (English)

  • This challenge involves exploiting an XSS vulnerability to extract the flag stored in either flag.txt or the FLAG variable.
  • The server uses a bot that internally visits the user-provided URL using Selenium, simulating an administrator.
  • Direct <script> tag injection is filtered, so scriptless XSS techniques are required.
  • The attacker uses an <img> tag with a broken src and a malicious onerror handler to trigger JavaScript:
    <img src=x onerror="fetch('http://127.0.0.1:8000/memo?memo='+document.cookie)">
  • fetch() sends a GET request to /memo including the document's cookies, which contain the flag.
  • Visiting the /memo endpoint afterward reveals the flag that was exfiltrated by the bot's browser.
  • Key takeaways:
    • XSS is still possible even without
    • document.cookie allows cookie theft from the bot's browser.
    • Simple keyword filtering is not a sufficient defense against XSS attacks.
profile
CTF 풀이 및 실습 중심 학습을 기록합니다.

0개의 댓글