Prison Management System - SQL Injection Authentication Bypass

Exploit Author: Sanjay Singh Analysis Author: www.bubbleslearn.ir Category: WebApps Language: PHP Published Date: 2024-05-13
# Exploit : Prison Management System Using PHP -SQL Injection Authentication Bypass
# Date: 15/03/2024
# Exploit Author: Sanjay Singh
# Vendor Homepage: https://www.sourcecodester.com
# Software Link:https://www.sourcecodester.com/sql/17287/prison-management-system.html
# Tested on: Windows ,XAMPP
# CVE : CVE-2024-33288


# Proof of Concept:
Step 1-Visit http://localhost/prison/
Step 2 - Click on Admin Dashboard button and redirect on login page.
Step 3– Enter username as admin' or '1'='1 and password as 123456
Step 4 – Click sing In and now you will be logged in as admin.


Prison Management System — SQL Injection Authentication Bypass (CVE-2024-33288)

This article examines a reported SQL injection vulnerability in a sample Prison Management System (reported as CVE-2024-33288). It explains the vulnerability class, why authentication bypass can occur, safe remediation patterns for PHP applications, detection and mitigation strategies, and best practices for operators and developers to harden applications against similar flaws.

Summary of the issue

SQL injection vulnerabilities occur when untrusted input is incorporated into SQL queries without proper handling, allowing an attacker to alter the intended query logic. In an authentication context, this may permit an attacker to bypass login checks and gain unauthorized access. The vulnerability discussed here affects a PHP-based sample application and is typical of systems that construct SQL queries by concatenating user-supplied values directly into query strings.

Impact and risk

  • Authentication bypass: unauthorized access to administrative or user accounts.
  • Data exposure or modification: once authenticated, an attacker may read or modify sensitive records (prisoner data, schedules, logs).
  • Privilege escalation: access to admin functionality can lead to system-level changes or data exfiltration.
  • Persistence and lateral movement: attackers with login access may plant backdoors or abuse other functionality.

Vulnerability class: why authentication queries are often targeted

Login forms typically run SQL queries that check for a matching username and password. When the username or password values are concatenated into a SQL string without parameterization, crafted input can change the WHERE clause so it always evaluates true (a tautology) or otherwise bypasses checks.

Example of a vulnerable pattern (conceptual)

// Vulnerable conceptual pattern (do NOT use in production)
// Shows structure that leads to risk if values are concatenated directly
$username = $_POST['username'];
$password = $_POST['password'];

$query = "SELECT id, password_hash FROM users WHERE username = '$username' AND password = '$password'";
$result = $db->query($query);

Explanation: This example demonstrates concatenating raw user input into a SQL string. If input contains SQL metacharacters, the resulting SQL may behave differently from what was intended, enabling bypass or data tampering.

Secure alternatives — PHP examples

Use parameterized queries (prepared statements) and proper password hashing. Below are secure patterns using PDO and password hashing functions built into PHP.

// Secure PDO example with password hashing (recommended)
$dsn = 'mysql:host=localhost;dbname=prison;charset=utf8mb4';
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
$pdo = new PDO($dsn, $dbUser, $dbPass, $options);

$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';

$stmt = $pdo->prepare('SELECT id, password_hash FROM users WHERE username = :username LIMIT 1');
$stmt->execute([':username' => $username]);
$user = $stmt->fetch();

if ($user && password_verify($password, $user['password_hash'])) {
    // Successful login: initialize session, regenerate ID, set minimal session data
    session_regenerate_id(true);
    $_SESSION['user_id'] = $user['id'];
    // set role, last_login timestamp, etc.
} else {
    // Generic error response (do not reveal which field failed)
}

Explanation: This code uses PDO prepared statements and bound parameters so user input cannot change SQL structure. Passwords are compared with password_verify against a stored hash produced by password_hash. The code also shows session regeneration on successful authentication to reduce session fixation risk.

How to properly store passwords

// Creating password hash when registering a user
$plaintext = 'user-password';
$hash = password_hash($plaintext, PASSWORD_DEFAULT); // PHP handles salt and algorithm
// Store $hash in users.password_hash column

Explanation: Use password_hash to create a secure hash. Do not use reversible encryption or unsalted hashes. PHP's functions manage algorithm choice and salts, and password_verify compares securely.

Database and access hardening

  • Use a dedicated database user with the least privileges needed (SELECT/INSERT/UPDATE on specific tables only).
  • Avoid using a high-privileged account (e.g., root) from the application layer.
  • Enable parameterized queries at the application layer; avoid building SQL by concatenation.
  • Set proper error handling: do not display raw database errors to users.

Detection and testing (defensive)

  • Automated scanning: run application security scanners (SAST/DAST) as part of CI/CD to flag SQL injection patterns.
  • Penetration testing: only performed by authorized testers against systems you control or have permission to test.
  • Logging and monitoring: detect anomalous authentication attempts (unusual locations, rapidly repeated attempts, anomalous queries recorded in DB logs).
  • Use a web application firewall (WAF) as an additional layer to block common injection patterns, while not relying on it as the sole defense.

Mitigations and operational recommendations

  • Fix code: replace any string-concatenated SQL with prepared statements and bound parameters.
  • Hash passwords: ensure stored passwords use a strong hashing algorithm (bcrypt/Argon2 via password_hash).
  • Least privilege: restrict DB user permissions.
  • Secure configuration: enforce HTTPS, secure session cookies (HttpOnly, Secure, SameSite), and session timeout policies.
  • Audit and patch management: keep libraries and sample applications updated; remove or secure sample/demo apps on production systems.
  • Input validation and output encoding: validate inputs for expected formats and contexts, encode outputs rendered into HTML.

Example migration plan for a vulnerable login endpoint

  • Inventory: locate all authentication and DB-access code paths.
  • Replace query concatenation with parameterized queries.
  • Migrate password storage to password_hash if not already used (prompt password reset if necessary).
  • Run unit and integration tests; include negative tests to ensure malformed inputs cannot alter SQL logic.
  • Deploy to staging and perform authorized security tests.
  • Monitor logs closely after deployment for suspicious activity and failed login anomalies.

Example policy and configuration checklist for operators

Area Recommended Action
Code Use prepared statements, parameter binding, and ORM abstractions where appropriate
Passwords Use password_hash/password_verify (bcrypt/Argon2); enforce strong password policies
Database Least privilege user accounts; limit network exposure; audit DB logs
Deployment Remove sample/demo applications from production; secure default credentials
Monitoring Enable login anomaly detection and centralized logging

Responsible disclosure and CVE reference

If you discover a vulnerability in an application you operate or test, follow responsible disclosure practices: notify the vendor or maintainer privately, provide reproduction steps and remediation guidance, and allow reasonable time for a patch before public disclosure. The referenced report is tracked as CVE-2024-33288; operators should confirm whether a vendor patch or updated package is available for their specific application/version.

Closing guidance

Authentication bypass via SQL injection is a common and serious flaw but also one of the most preventable. The effective defense is simple in principle: never build SQL queries by concatenating user input; always use prepared statements and bind parameters, store passwords with modern hashing functions, and apply defense-in-depth (least privilege, logging, secure config, WAF). Regular code review and automated security checks integrated into development pipelines will greatly reduce the risk of regression.