Cross-Site Request Forgery (CSRF)
Cross-Site Request Forgery (CSRF) is a subtechnique of Request Forgery where attackers trick authenticated users into executing unwanted actions on a web application. By exploiting the trust that a web application has in an authenticated user's browser, CSRF attacks can force users to perform state-changing operations like modifying account settings, making purchases, or transferring funds. The attack is particularly effective because the malicious requests include all the user's authentication tokens and cookies, making them appear legitimate to the target application.
Examples in the Wild
Notable CSRF Attacks:
0.0.0.0 Day CSRF Vulnerability (20+ Years Unpatched) A long-standing CSRF vulnerability, dubbed the "0.0.0.0 day", has remained unpatched for over two decades. The vulnerability allows malicious websites to bypass security in major browsers by exploiting how browsers handle certain DNS responses. This vulnerability demonstrates how fundamental web security assumptions can remain flawed for extended periods.
Drive-by Pharming Attack (2014) A massive CSRF campaign compromised over 300,000 home routers across multiple countries by exploiting router DNS settings. The attackers used cross-site request forgery vulnerabilities to modify DNS configurations on various router models including TP-Link, D-Link, Micronet, and Tenda. The attack was particularly sophisticated as it: - Overwrite DNS settings to redirect traffic to attacker-controlled servers - Used a secondary legitimate DNS (Google's 8.8.8.8) to avoid detection - Affected users were concentrated in Vietnam, India, Italy, and Thailand - Exploited routers that weren't using default passwords through CSRF
GitHub OAuth Token Exposure (2017) A CSRF vulnerability in GitHub's OAuth implementation allowed attackers to steal users' OAuth tokens by tricking them into visiting malicious websites. The attack could expose repository access tokens and potentially lead to source code theft or malicious code injection. GitHub's post-mortem revealed that missing state validation in the OAuth flow enabled the attack.
Router Configuration Attacks Multiple router manufacturers have been affected by CSRF vulnerabilities that allowed attackers to modify DNS settings, enable remote access, or change admin passwords through malicious websites. Notable examples include: - ASUS Router CSRF (CVE-2014-9583) - D-Link Router CSRF (CVE-2019-13361) - TP-Link Router CSRF (CVE-2020-24586)
Banking Trojans with CSRF Several banking trojans have incorporated CSRF attacks to automate fraudulent transactions: - The Dridex malware used CSRF to initiate wire transfers - The Gozi trojan exploited CSRF to modify payment destinations - The TrickBot malware chain included CSRF for account takeover
Attack Mechanism
Common CSRF Exploitation Techniques:
-
Basic Form Submission
<!-- Malicious HTML form --> <form action="https://bank.com/transfer" method="POST"> <input type="hidden" name="amount" value="1000"> <input type="hidden" name="to" value="attacker"> <input type="submit" value="Click me!"> </form> <script> // Auto-submit form on page load document.forms[0].submit(); </script>
-
XMLHttpRequest Exploitation
// CSRF via XHR function csrfAttack() { var xhr = new XMLHttpRequest(); xhr.open("POST", "https://target.com/api/action", true); xhr.withCredentials = true; // Send cookies xhr.setRequestHeader("Content-Type", "application/json"); xhr.send(JSON.stringify({ "action": "update_email", "new_email": "attacker@evil.com" })); }
-
Image-Based CSRF
<!-- CSRF via image tag --> <img src="https://bank.com/transfer?amount=1000&to=attacker" style="display:none" onerror="this.style.display='none'">
Detection Challenges
Why Traditional Security Tools Fail:
-
Request Legitimacy
# Request characteristics csrf_request: - cookies: "valid" - session: "authenticated" - origin: "different domain" # How to determine legitimacy?
-
Token Validation
# CSRF token issues token_problems: - missing_token: "no protection" - predictable_token: "guessable" - token_reuse: "replay attacks" # How to ensure proper tokens?
-
Header Verification
# Header validation security_headers: - origin: "sometimes missing" - referer: "can be spoofed" - custom_header: "not always present" # Which headers to trust?
Required Application Security Strategy:
# CSRF prevention rules
- rule: "Token Validation"
condition: |
request.has_valid_csrf_token AND
token.not_expired AND
token.matches_session
severity: critical
# Origin verification
- rule: "Origin Check"
condition: |
request.origin_allowed AND
request.referer_valid AND
request.same_site_context
severity: high
# Request validation
- rule: "Request Legitimacy"
condition: |
request.method_requires_protection AND
request.state_changing AND
request.authenticated_context
severity: critical
Key Detection Requirements:
- Token Management
- Cryptographic token generation
- Per-session token tracking
-
Token lifecycle management
-
Origin Validation
- Strict origin checking
- Referer validation
-
SameSite cookie enforcement
-
Request Analysis
- Method verification
- State change detection
- Authentication validation
References & Resources
Official Sources:
Technical Analysis: