Mysql数据库安全性问题【防注入】
来源:互联网 发布:时间旅行 美剧 知乎 编辑:程序博客网 时间:2024/05/16 11:54
一、SQL注入实例
后台的插入语句代码:
$unsafe_variable = $_POST['user_input']; mysql_query("INSERT INTO `table` (`column`) VALUES ('$unsafe_variable')");
当POST的内容为:
value'); DROP TABLE table;--
以上的整个SQL查询语句变成:
INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')
二、防止SQL注入措施
1.使用预处理语句和参数化查询。(‘Use prepared statements and parameterized queries.’)
SQL语句和查询的参数分别发送给数据库服务器进行解析。这种方式有2种实现:
(1)使用PDO(PHP data object)
$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name'); $stmt->execute(array('name' => $name)); foreach ($stmt as $row) { // do something with $row }
(2)使用MySQLi
$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?'); $stmt->bind_param('s', $name); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { // do something with $row }
2.对查询语句进行转义(最常见的方式)
$unsafe_variable = $_POST["user-input"]; $safe_variable = mysql_real_escape_string($unsafe_variable); mysql_query("INSERT INTO table (column) VALUES ('" . $safe_variable . "')");
$mysqli = new mysqli("server", "username", "password", "database_name"); // TODO - Check that connection was successful. $unsafe_variable = $_POST["user-input"]; $stmt = $mysqli->prepare("INSERT INTO table (column) VALUES (?)"); // TODO check that $stmt creation succeeded // "s" means the database expects a string $stmt->bind_param("s", $unsafe_variable); $stmt->execute(); $stmt->close(); $mysqli->close();
3.限制引入的参数
$orders = array("name","price","qty"); //field names $key = array_search($_GET['sort'],$orders)); // see if we have such a name $orderby = $orders[$key]; //if not, first one will be set automatically. smart enuf :) $query = "SELECT * FROM `table` ORDER BY $orderby"; //value is safe
4.对引入参数进行编码
SELECT password FROM users WHERE name = 'root' --普通方式 SELECT password FROM users WHERE name = 0x726f6f74 --防止注入 SELECT password FROM users WHERE name = UNHEX('726f6f74') --防止注入 set @INPUT = hex("%实验%"); select * from login where reset_passwd_question like unhex(@INPUT) ;
有一些讨论意见,所以我最后想弄清楚。这两种方法非常相似,但它们在某些方面有点不同:
x前缀只能用于数据列如char、varchar、文本块,二进制,等等。
其使用也有点复杂,如果你要插入一个空字符串。你必须完全替代”,或者你会得到一个错误。
任何列工作;您不必担心空字符串。
参考stackoverflow:http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php
1 0
- Mysql数据库安全性问题【防注入】
- Mysql数据库安全性问题【防注入】
- Mysql数据库防SQL注入原理
- MySQL数据库防SQL注入原理
- 简单数据库防注入
- 数据库防sql注入
- 数据库防注入攻击
- 数据库防注入
- PHP mysql防注入
- PHP防MYSQL注入
- mysql防注入
- mysql防注入函数
- mysql防sql注入
- mysql防注入函数
- MySql防注入
- 防注入的问题
- 防脚本注入问题
- 数据库安全防SQL注入
- 汇编调用c函数为什么要设置栈
- CAJViewer 无法获取document路径问题--360卫士C盘搬家
- java中getClass()和getSuperClass()的讲解
- 解决warning MSB8012:问题
- cygwin安装和使用
- Mysql数据库安全性问题【防注入】
- jquery删除指定数组元素
- Chrome 快捷键 整理版
- httpclient用户名密码认证示例
- 第六周 括号的匹配
- 第四周项目3—单链表应用(3)
- 第六周项目2--建立链栈算法库
- 让SQL用户快速进入Hadoop大数据时代 —— Transwarp Inceptor是怎样炼成的
- 第七周—项目 - 队列数组