Lab 19: Security Headers

Objective

Audit and exploit missing HTTP security headers using a live web API from Kali Linux:

  1. Header audit β€” use curl to enumerate exactly which security headers are missing from an unprotected API (score: 0/7)

  2. Reflected XSS amplified by missing CSP β€” send a <script> tag in a query parameter; with no Content-Security-Policy, the browser would execute it

  3. Clickjacking risk from missing X-Frame-Options β€” demonstrate how the app can be embedded in a malicious iframe

  4. MIME sniffing via missing X-Content-Type-Options β€” upload polyglot content that a browser would execute as a different type

  5. Secure endpoint comparison β€” audit the protected version (score: 7/7) and confirm every header is present and correct

  6. Implement the full header set β€” write a Flask middleware that adds all 7 headers globally


Background

Security headers are the cheapest, highest-ROI security controls available β€” a one-time server configuration change that mitigates entire vulnerability classes at the browser level.

Real-world impact of missing headers:

  • Missing CSP β†’ XSS escalation: The 2018 British Airways breach ($228M fine, 500,000 customers) began with a skimming script injected into their payment page. A strong CSP blocking script-src 'self' would have prevented the injected script from executing.

  • Missing X-Frame-Options β†’ Clickjacking: In 2009, Adobe Flash settings pages were clickjacked via invisible iframes, allowing attackers to silently enable webcam access. X-Frame-Options: DENY would have blocked this.

  • Missing HSTS β†’ SSL strip: Attackers on the same network (coffee shop Wi-Fi, hotel) can downgrade HTTPS to HTTP before the first connection. HSTS tells the browser to always use HTTPS, preventing the downgrade.

  • Missing X-Content-Type-Options β†’ MIME sniffing: A file uploaded as text/plain but containing HTML gets rendered as HTML by IE/Chrome if nosniff is absent β€” XSS via file uploads.

  • Missing Referrer-Policy β†’ data leakage: Without this header, the browser sends the full URL (including query params with PII) in the Referer header to third-party analytics/CDN providers.

OWASP coverage: A05:2021 (Security Misconfiguration)


Architecture

Time

35 minutes


Lab Instructions

Step 1: Environment Setup


Step 2: Launch Kali and Run Baseline Header Check

πŸ“Έ Verified Output:


Step 3: Automated Header Audit (Score 0/7)

πŸ“Έ Verified Output:


Step 4: XSS Reflected by Missing CSP

πŸ“Έ Verified Output:

πŸ’‘ CSP (Content-Security-Policy) is the strongest XSS mitigation available. With script-src 'self', even if an attacker successfully injects a <script> tag, the browser refuses to execute it β€” the tag renders as visible text. Think of CSP as a whitelist for what your page is allowed to do. It's a second layer of defence: even if injection happens, execution is blocked.


Step 5: Clickjacking via Missing X-Frame-Options

πŸ“Έ Verified Output:


Step 6: HSTS β€” Preventing SSL Strip Attacks

πŸ“Έ Verified Output:


Step 7: Implement a Flask Security Header Middleware


Step 8: Cleanup


Header Reference

Header
Value
Protects Against

Content-Security-Policy

default-src 'self'; script-src 'self'; frame-ancestors 'none'

XSS, clickjacking, injection

Strict-Transport-Security

max-age=31536000; includeSubDomains; preload

SSL strip, downgrade attacks

X-Frame-Options

DENY

Clickjacking

X-Content-Type-Options

nosniff

MIME sniffing, content-type confusion

Referrer-Policy

strict-origin-when-cross-origin

Referrer-based data leakage

Permissions-Policy

geolocation=(), camera=(), microphone=()

API abuse, covert data collection

Cache-Control

no-store

Sensitive data cached by browser/proxy

Free Tools

Further Reading

Last updated