Here is a detailed analysis of insecure code in the provided files, along with their full paths:
1. Broken Authentication
File: ./repos/Web-Security-Workshop-Shellamtes/Broken authentification/index.php
Insecure Code:
php Copy code
$query="SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password'";
$result=mysqli_query($connection,$query);
Issue:
SQL Injection Vulnerability : User-controlled input ($username and $password) is directly interpolated into the SQL query without sanitization or parameterized queries.
Fix:
Use prepared statements with parameterized queries:
php Copy code
$query = "SELECT * FROM `users` WHERE `username`=? AND `password`=?";
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, 'ss', $username, $password);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
2. Command Injection
File: ./repos/Web-Security-Workshop-Shellamtes/Command injection/index.php
Insecure Code:
php Copy code
$msg= shell_exec ("ping -c 4 $ip");
Issue:
Command Injection Vulnerability : The $ip variable is directly passed to the shell_exec function, allowing an attacker to execute arbitrary shell commands.
Fix:
Validate and sanitize the input to ensure it is a valid IP address:
php Copy code
if (filter_var($ip, FILTER_VALIDATE_IP)) {
$msg = shell_exec("ping -c 4 " . escapeshellarg($ip));
} else {
$msg = "Invalid IP address.";
}
3. File Upload Vulnerabilities
File: ./repos/Web-Security-Workshop-Shellamtes/FileUploadV1/upload.php
Insecure Code:
php Copy code
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $file)) {
$query="INSERT INTO `images` (`id`,`path`) VALUES (NULL,'$file')";
$result=mysqli_query($connection,$query);
}
Issues:
Unrestricted File Upload : No validation of file type or extension.
File Overwriting : Existing files with the same name can be overwritten.
Fix:
Validate the file type and ensure unique filenames:
php Copy code
$allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
if (in_array($_FILES['fileToUpload']['type'], $allowed_types)) {
$file = $dir . uniqid() . "_" . basename($_FILES['fileToUpload']['name']);
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $file)) {
// Save to database safely
}
} else {
echo "Invalid file type.";
}
File: ./repos/Web-Security-Workshop-Shellamtes/FileUploadV2/upload.php
Insecure Code:
php Copy code
if (in_array($_FILES['fileToUpload']['type'], $types)) {
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $file);
}
Issue:
File Validation Bypass : The validation is based only on the MIME type, which can be spoofed.
Fix:
Validate both the MIME type and the file extension:
php Copy code
$allowed_extensions = ['jpg', 'jpeg', 'png'];
$file_extension = pathinfo($_FILES['fileToUpload']['name'], PATHINFO_EXTENSION);
if (in_array($file_extension, $allowed_extensions) && in_array($_FILES['fileToUpload']['type'], $types)) {
// Process the file
} else {
echo "Invalid file.";
}
File: ./repos/Web-Security-Workshop-Shellamtes/FileUploadV3/upload.php
Insecure Code:
php Copy code
$extensions = array('txt', 'pdf', 'docx', 'doc');
if (in_array($ext, $extensions)) {
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $file);
}
Issue:
Unrestricted File Upload : Similar to the issue above, this validation only checks the file extension, which is insufficient.
Fix:
Perform server-side content inspection in addition to extension checks.
4. SQL Injection
File: ./repos/Web-Security-Workshop-Shellamtes/Injection/index.php
Insecure Code:
php Copy code
$query="SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password'";
$result=mysqli_query($connection,$query);
Issue:
SQL Injection Vulnerability : Same as in "Broken Authentication."
Fix:
Use parameterized queries (same as above).
File: ./repos/Web-Security-Workshop-Shellamtes/InjectionV2/index.php
Secure Code:
This file uses prepared statements:
php Copy code
$query="SELECT * FROM `users` WHERE `username`=? AND `password`=?";
$stmnt=mysqli_prepare($connection,$query);
mysqli_stmt_bind_param($stmnt,'ss',$username,$password);
mysqli_stmt_execute($stmnt);
Fix:
No action needed; the code is secure.
5. Local File Inclusion (LFI)
File: ./repos/Web-Security-Workshop-Shellamtes/LFI/index.php
Insecure Code:
php Copy code
include ("$language");
Issue:
LFI Vulnerability : User-controlled input ($_GET['lang']) is directly included, allowing arbitrary file inclusion.
Fix:
Validate the input against an allowed list of files:
php Copy code
$allowed_languages = ['lang.php', 'fr.php', 'ang.php'];
if (in_array($language, $allowed_languages)) {
include($language);
} else {
echo "Invalid language.";
}
File: ./repos/Web-Security-Workshop-Shellamtes/LFI2/index.php
Insecure Code:
php Copy code
include ("$language.php");
Issue:
LFI Vulnerability : Same as above.
Fix:
6. Reflected XSS
File: ./repos/Web-Security-Workshop-Shellamtes/Reflected/index.php
Insecure Code:
php Copy code
$msg= "$name is not found";
Issue:
Reflected XSS : User input ($_GET['name']) is directly echoed back without sanitization.
Fix:
Sanitize the output using htmlspecialchars:
php Copy code
$msg = htmlspecialchars("$name is not found", ENT_QUOTES, 'UTF-8');
7. Stored XSS
File: ./repos/Web-Security-Workshop-Shellamtes/XSS/index.php
Insecure Code:
php Copy code
$query = "INSERT INTO messages (`id`, `titre`, `message`) VALUES (NULL, '$titre', '$message')";
Issue:
Stored XSS : User input ($titre and $message) is stored in the database without sanitization or escaping, and later displayed without sanitization.
Fix:
Sanitize input with htmlspecialchars before storing or displaying:
php Copy code
$titre = htmlspecialchars($titre, ENT_QUOTES, 'UTF-8');
$message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
File: ./repos/Web-Security-Workshop-Shellamtes/XSSV2/index.php
Secure Code:
This file uses htmlspecialchars to sanitize input:
php Copy code
$titre = htmlspecialchars($titre, ENT_QUOTES);
$message = htmlspecialchars($message, ENT_QUOTES);
Fix:
No action needed; the code is secure.
Summary of Insecure Files:
SQL Injection :
./repos/Web-Security-Workshop-Shellamtes/Broken authentification/index.php
./repos/Web-Security-Workshop-Shellamtes/Injection/index.php
Command Injection :
./repos/Web-Security-Workshop-Shellamtes/Command injection/index.php
File Upload Vulnerabilities :
./repos/Web-Security-Workshop-Shellamtes/FileUploadV1/upload.php
./repos/Web-Security-Workshop-Shellamtes/FileUploadV2/upload.php
./repos/Web-Security-Workshop-Shellamtes/FileUploadV3/upload.php
Local File Inclusion :
./repos/Web-Security-Workshop-Shellamtes/LFI/index.php
./repos/Web-Security-Workshop-Shellamtes/LFI2/index.php
XSS (Reflected and Stored) :
./repos/Web-Security-Workshop-Shellamtes/Reflected/index.php
./repos/Web-Security-Workshop-Shellamtes/XSS/index.php