0% found this document useful (0 votes)
8 views4 pages

SQL Injection & XSS

The document provides a step-by-step guide for setting up a web security lab using a VM with Apache, MariaDB, and PHP, focusing on SQL Injection (SQLi) and Cross-Site Scripting (XSS) vulnerabilities. It includes installation commands, database and user creation, and examples of vulnerable code for both SQLi and XSS, along with methods to fix these vulnerabilities. The guide emphasizes the importance of using prepared statements for SQLi prevention and htmlspecialchars for XSS mitigation.

Uploaded by

gh31balushi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

SQL Injection & XSS

The document provides a step-by-step guide for setting up a web security lab using a VM with Apache, MariaDB, and PHP, focusing on SQL Injection (SQLi) and Cross-Site Scripting (XSS) vulnerabilities. It includes installation commands, database and user creation, and examples of vulnerable code for both SQLi and XSS, along with methods to fix these vulnerabilities. The guide emphasizes the importance of using prepared statements for SQLi prevention and htmlspecialchars for XSS mitigation.

Uploaded by

gh31balushi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Web Security Lab Commands (SQL Injection + XSS)

One VM (Ubuntu/Kali/UWE VM) • Apache + MariaDB + PHP

1) Installation (Fresh VM)


sudo apt update

sudo apt install apache2 -y


sudo service apache2 start
sudo service apache2 status

sudo apt install mariadb-server mariadb-client -y


sudo service mariadb start
sudo service mariadb status

sudo apt install php php-mysqli php-mysql -y


sudo service apache2 restart
php -m | grep mysqli

2) Start / Check Services (Every Lab Session)


sudo service apache2 start
sudo service mariadb start

sudo service apache2 status


sudo service mariadb status

3) Create Database + User (SQLi Lab)


Open MariaDB shell:

sudo mariadb

Run these SQL statements inside MariaDB:

CREATE DATABASE sqli_lab;

CREATE USER 'sqliuser'@'localhost' IDENTIFIED BY


'sqli123!';
GRANT ALL PRIVILEGES ON sqli_lab.* TO
'sqliuser'@'localhost';
FLUSH PRIVILEGES;
USE sqli_lab;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
password VARCHAR(50)
);

INSERT INTO users (username, password) VALUES


('admin','admin123'),
('student','student123');

EXIT;

4) Create Vulnerable SQL Injection Page


Create / edit the file:

sudo nano /var/www/html/[Link]

Paste this code into [Link]:

<?php
$conn = mysqli_connect("[Link]", "sqliuser",
"sqli123!", "sqli_lab");

if (!$conn) {
die("Connection failed");
}

if (isset($_POST['username'])) {
$user = $_POST['username'];
$pass = $_POST['password'];

$query = "SELECT * FROM users WHERE username='$user'


AND password='$pass'";
echo "<pre>$query</pre>";

$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
echo "<h2>Login Successful</h2>";
} else {
echo "<h2>Login Failed</h2>";
}
}
?>
<form method="POST">
Username: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br>
<input type="submit">
</form>

Restart Apache:

sudo service apache2 restart

Open in browser: [Link]

5) SQL Injection Test Inputs


Normal login:

Username: admin
Password: admin123

SQLi bypass (demo):

Username: admin' --
Password: anything

6) Way to fix it
SQL Injection is prevented by using prepared statements that separate SQL logic from user input.

$stmt = $conn->prepare(

"SELECT * FROM users WHERE username=? AND password=?"

);

$stmt->bind_param("ss", $user, $pass);

$stmt->execute();

$result = $stmt->get_result();

7)Create Vulnerable XSS Page


Create / edit the file:

sudo nano /var/www/html/[Link]

Paste this code into [Link]:


<?php
if (isset($_GET['name'])) {
$name = $_GET['name'];
echo "Hello " . $name;
}
?>

<form method="GET">
Name: <input type="text" name="name">
<input type="submit">
</form>

Restart Apache:

sudo service apache2 restart

Open in browser: [Link]

8) XSS Test Inputs


Normal input:

Aisha

XSS payload (demo):

<script>alert('XSS')</script>

9) One-Line XSS Fix


Replace this line:

echo "Hello " . $name;

With:

echo "Hello " . htmlspecialchars($name);

You might also like