Home > Database > Mysql Tutorial > sql注入原理及防范_MySQL

sql注入原理及防范_MySQL

WBOY
Release: 2016-06-01 13:06:31
Original
1215 people have browsed it

一: sql注入原理



原理就是利用服务器执行sql参数时的特殊性,将正常的数据库字段注入特征字符组成新的SQL语句,导致原来的sql语句“变了味”


举一个简单的例子
$id = $_GET['id']; /// id= 10001 OR 1 = 1 #
$qq = $_GET['qq']; // qq= "261414' OR 1 = 1 #"
$sql = "SELECT * FROM `user` WHERE id=$id AND $qq = '".$qq."'";
$results = $db->query($sql);


二: 如何防范:
1. web参数过滤,只允许约定的字符出现,但过于暴力


2. 拼装的sql语句用mysql_real_escape_string方法进行过滤
$username = mysql_real_escape_string($_GET['username']);


3. mysql用户权限设置,不要用root


4. 使用sql注入扫描工具测试


附:

PDO的prepare是怎么防范SQL注入的
当调用prepare()时,查询语句先被发送给了MYSQL,此时只有占位符发送过去,没有用户提交的数据(MYSQL做预编译,一定程度上会优化查询效率,特别是对同个SQL模版进行多次调用时);当调用execute()时,用户提交的值才会传送给MYSQL。从而达到防范SQL注入的目的。.
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template