我一直在研究用PHP編寫的opencart。PHP SQL注入可能性
如果你看看下面的PHP文件,
https://github.com/opencart/opencart/blob/master/upload/admin/model/customer/customer.php
的SQL語句看起來像這樣
$this->db->query("INSERT INTO " . DB_PREFIX . "customer
SET customer_group_id = '" . (int)$data['customer_group_id'] . "',
firstname = '" . $this->db->escape($data['firstname']) . "',
lastname = '" . $this->db->escape($data['lastname']) . "',
email = '" . $this->db->escape($data['email']) . "',
telephone = '" . $this->db->escape($data['telephone']) . "',
custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : json_encode(array())) . "',
newsletter = '" . (int)$data['newsletter'] . "',
salt = '',
password = '" . $this->db->escape(password_hash($data['password'], PASSWORD_DEFAULT)) . "',
status = '" . (int)$data['status'] . "',
safe = '" . (int)$data['safe'] . "',
date_added = NOW()");
推薦的方式,以避免PHP SQL注入是使用預準備語句。
我的問題是考慮如何這個特定的代碼是不是使用準備好的語句,這個代碼容易受到SQL注入?
我不是一個PHP專家,所以我可能會錯過這裏明顯的東西。
編輯:
讓我列舉我是有點害怕接受這個代碼是脆弱的原因。
Opencart的(https://github.com/opencart/opencart)是一個擁有200叉子一個流行的開源項目。
這是一個特別的購物車(電子商務)解決方案,因此開發人員會考慮安全性,並且像這樣的sql注入是他們首先檢查的事情之一。
它看起來像某種逃逸使用
$this->db->escape($data['telephone'])
是的,這個代碼是脆弱的,因爲它沒有使用[Prepared Statements](https://www.w3schools.com/php/php_mysql_prepared_statements.asp),**轉義字符串不再是SQL注入的推薦解決方案,它是不夠的** – GrumpyCrouton
可能重複[如何防止SQL注入在PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php ) – Qirel
防止SQL注入的唯一明確方法是使用預準備語句。這在許多其他方面也更好 - 避免轉義,只使用準備好的語句。 – Qirel