SQL Injection
SQL Injection
SQL Injection (SQLi) is a web security vulnerability that allows an attacker to interfere with
the queries an application makes to its database. It can allow attackers to view data that they
are not normally able to retrieve, such as other users' data, or inject commands to alter the
database structure and content. Here’s a detailed breakdown of how SQL Injection works, its
different types, examples, and defenses.
SQL Injection (SQLi) is a security flaw that lets attackers insert malicious SQL code into an
application’s database query, potentially accessing or modifying data unauthorizedly.
SQL Injection is one of the oldest yet still highly relevant vulnerabilities in web applications.
With careful validation, use of prepared statements, and regular security testing, SQLi risks
can be minimized effectively.
Lab Steps:
1. Install and Configure LAMP Stack
o Install Apache, MySQL, and PHP.
o Configure a basic web server and database.
2. Set Up the Vulnerable Web Application
o Create a MySQL database with a simple users table.
o Write a PHP application vulnerable to SQL injection.
3. Test the SQL Injection Vulnerability
o Access the web application and attempt to retrieve unauthorized data using
SQL injection payloads.
4. Use SQLMap for Automated Exploitation
o Leverage SQLMap to exploit the vulnerability and retrieve database
information.
5. Mitigation and Code Fixes
o Discuss secure coding practices, including prepared statements and input
validation.
<?php
// Database connection
$servername = "localhost";
$username = "root";
$password = ""; // Enter your MySQL root password if you set one
$dbname = "student_db";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['submit'])) {
$user_id = $_POST['user_id'];
// Vulnerable SQL Query
$sql = "SELECT * FROM users WHERE id = $user_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "User: " . $row['username'] . "<br>";
echo "Password: " . $row['password'] . "<br>";
}
} else {
echo "No user found.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>SQL Injection Lab</title>
</head>
<body>
<h2>SQL Injection Simulation</h2>
<form method="POST">
<label for="user_id">Enter User ID:</label>
<input type="text" name="user_id" required>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Lab 2
Lab 3: