Lab1
Lab1
Objective:
• Install Python:
• Install PyCharm:
• Install XAMPP:
• Download XAMPP from the Apache Friends website.
• Start the XAMPP control panel and ensure Apache and MySQL
services are running.
• Create a new directory for your project inside the htdocs folder
(e.g., C:\xampp\htdocs\myproject).
php
<?php
?>
• Sites Tab: Shows the sites and URLs that ZAP has accessed.
• Spidering:
• Go to Tools > Spider, enter the target URL, and start the
spidering process.
• Active Scanning:
• Intercepting Traffic:
• Browse the target web application, and Burp will capture the
HTTP requests.
Plugins
Extensibility Extensible with plugins (Pro)
available
Automated
Yes Yes (Pro Edition)
Testing
Feature OWASP ZAP Burp Suite
Manual Testing
Yes Yes
Tools
<html>
<head>
<title>XSS Demo</title>
</head>
<body>
<label for="name">Name:</label>
</form>
</body>
</html>
if (file_exists($file_path)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($fil
e_path) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;
} else {
echo "File not found.";
}
?>
• Step 3: Accessing Files:
$file = $_GET['file'];
if (in_array($file, $allowed_files)) {
$file_path = 'files/' . $file;
if (file_exists($file_path)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' .
basename($file_path) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;
} else {
echo "File not found.";
}
} else {
echo "Access Denied.";
}
?>
3.3. Cross-Site Request Forgery (CSRF):
• Description:
Unprotected Page
<?php
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['username'] = "default";
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Update the username
$_SESSION['username'] = $_POST['username'];
echo "Profile updated without CSRF protection.";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Unprotected Page</title>
</head>
<body>
<h1>Unprotected Page</h1>
<p>Current Username: <?php echo
htmlspecialchars($_SESSION['username']); ?></p>
<form method="POST">
<input type="text" name="username" placeholder="Enter new
username">
<button type="submit">Update Profile</button>
</form>
</body>
</html>
Attacker Page:
<!DOCTYPE html>
<html>
<head>
<title>Attacker Page</title>
</head>
<body>
<h1>Attacker Page</h1>
<form action="unprotected.php" method="POST">
<input type="hidden" name="username" value="hacked">
<button type="submit">Execute CSRF Attack</button>
</form>
</body>
</html>
• Mitigation:
if (!isset($_SESSION['username'])) {
$_SESSION['username'] = "default";
}
if (!isset($_SESSION['token'])) {
$_SESSION['token'] = bin2hex(random_bytes(32));
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!isset($_POST['token']) || $_POST['token'] !==
$_SESSION['token']) {
die("Invalid CSRF token");
}
// Update the username
$_SESSION['username'] = $_POST['username'];
echo "Profile updated with CSRF protection.";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Protected Page</title>
</head>
<body>
<h1>Protected Page</h1>
<p>Current Username: <?php echo
htmlspecialchars($_SESSION['username']); ?></p>
<form method="POST">
<input type="text" name="username" placeholder="Enter new
username">
<input type="hidden" name="token" value="<?php echo
$_SESSION['token']; ?>">
<button type="submit">Update Profile</button>
</form>
</body>
</html>
Testing
Unprotected Page:
Navigate to unprotected.php.
Fill in the form with a new username (e.g., "newuser") and submit.
The username should update, and the page will display "Profile
updated without CSRF protection."
Protected Page:
Navigate to protected.php.
Fill in the form with a new username (e.g., "newuser") and submit.
The username should update, and the page will display "Profile
updated with CSRF protection."
Attacker Page:
Open attacker.html.
4. Case Studies
• Other users who view the comment have the script executed in
their browsers.
• Discussion:
• Scenario:
• Discussion:
• Scenario:
5. Practical Assignment
• Task:
• Try to fix one problem that you might have in your application.
• Submission:
• Task:
• Submission: