Objective
Learn how sensitive data can be exposed through insecure API requests and how attackers can intercept this data. Understand how to mitigate this risk by securing API communications with HTTPS, token-based authentication (OAuth), and encrypting sensitive data.
Scenario
You are assessing a web application that communicates sensitive information (e.g., passwords and credit card details) through API requests over an insecure HTTP connection. Due to the absence of encryption and secure authentication, attackers can intercept and exploit this data.
Lab Setup
Prerequisites:
- Basic knowledge of APIs, HTTP requests, and web security.
- XAMPP/LAMP/WAMP stack installed (or any web server with PHP support).
- Tools like Burp Suite or Wireshark for traffic interception.
Step 1: Create the Vulnerable API
- PHP Script for Insecure API Endpoint
- Create a file
payment_api.php
:<?php header("Content-Type: application/json"); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $card_number = $_POST['card_number']; $cvv = $_POST['cvv']; $amount = $_POST['amount']; $response = [ 'status' => 'success', 'message' => "Payment of $$amount processed for card ending in " . substr($card_number, -4) ]; echo json_encode($response); } ?>
- Create a file
- Running the Application
- Start the Apache server.
- Place
payment_api.php
in the web server’s root directory (htdocs
for XAMPP). - Use curl to simulate API requests:
curl -X POST http://localhost/payment_api.php -d "card_number=4111111111111111&cvv=123&amount=100"
- Expected Result:
{ "status": "success", "message": "Payment of $100 processed for card ending in 1111" }
Exploitation Steps
Step 1: Intercepting Sensitive Data
- Open Wireshark or Burp Suite.
- Capture network traffic while sending the API request.
- Expected Result:
- Sensitive data such as the credit card number and CVV is visible in plaintext.
Step 2: Exploiting Exposed API Endpoints
- Use Burp Suite to modify the intercepted request.
- Change the amount or card number in transit.
- Expected Result:
- The server processes the modified request without verification.
Solution and Prevention
Problem Analysis
- The API transmits sensitive data over HTTP without encryption or authentication.
Fixing the Vulnerability
- Enforce HTTPS for Secure Communication
- Configure the server to use SSL/TLS:
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
- Configure the server to use SSL/TLS:
- Implement Token-Based Authentication (OAuth)
- Replace basic authentication with secure tokens:
$headers = apache_request_headers(); if ($headers['Authorization'] !== 'Bearer secure_token_123') { http_response_code(401); echo json_encode(["error" => "Unauthorized"]); exit(); }
- Replace basic authentication with secure tokens:
- Encrypt Sensitive Data Before Transmission
- Encrypt data using AES-256 before sending:
$encrypted_card = openssl_encrypt($card_number, 'aes-256-cbc', 'encryption_key', 0, 'iv');
- Encrypt data using AES-256 before sending:
- Input Validation and Rate Limiting
- Validate inputs and limit request frequency to prevent abuse.
Testing After Fix
- Resend the payment request over HTTP.
- Expected Result:
- The server redirects to HTTPS, and sensitive data is encrypted.
- Attempt to access the API without an authentication token.
- Expected Result:
- The server responds with a 401 Unauthorized error.
Conclusion
In this lab, you exploited Sensitive Data Exposure by intercepting insecure API requests containing sensitive information. You also learned how to mitigate this risk using HTTPS, token-based authentication (OAuth), and encryption of sensitive data.
0 Comments