from web srcMySQL - SQL Injection Prevention
If you have ever taken raw user input and inserted it into a MySQL database there's a chance that you have left yourself wide open for a security issue known as What is SQL InjectionSQL injection refers to the act of someone inserting a MySQL statement to be run on your database without your knowledge. Injection usually occurs when you ask a user for input, like their name, and instead of a name they give you a MySQL statement that you will unknowingly run on your database. SQL Injection ExampleBelow is a sample string that has been gathered from a normal user and a bad user trying to use SQL Injection. We asked the users for their login, which will be used to run a SELECT statement to get their information. MySQL & PHP Code:// a good user's name $name = "timmy"; $query = "SELECT * FROM customers WHERE username = '$name'"; echo "Normal: " . $query . " "; // user input that uses SQL Injection $name_bad = "' OR 1'"; // our MySQL query builder, however, not a very safe one $query_bad = "SELECT * FROM customers WHERE username = '$name_bad'"; // display what the new query will look like, with injection echo "Injection: " . $query_bad; Display:
Normal: SELECT * FROM customers WHERE username = 'timmy'
Injection: SELECT * FROM customers WHERE username = '' OR 1''
The normal query is no problem, as our MySQL statement will just select everything from customers that has a username equal to However, the injection attack has actually made our query behave differently than we intended. By using a single quote (') they have ended the string part of our MySQL query
and then added on to our WHERE statement with an OR clause of 1 (always true).
This OR clause of 1 will always be More Serious SQL Injection Attacks
Although the above example displayed a situation where an attacker could possibly get access to a lot of information they shouldn't have, the attacks can be a lot worse. For example an attacker could empty out a table by executing a MySQL & PHP Code:$name_evil = "'; DELETE FROM customers WHERE 1 or username = '"; // our MySQL query builder really should check for injection $query_evil = "SELECT * FROM customers WHERE username = '$name_evil'"; // the new evil injection query would include a DELETE statement echo "Injection: " . $query_evil; Display:
SELECT * FROM customers WHERE username = ' '; DELETE FROM customers WHERE 1 or username = ' '
If you were run this query, then the injected DELETE statement would completely empty your "customers" table. Now that you know this is a problem, how can you prevent it? Injection Prevention - mysql_real_escape_string()
Lucky for you, this problem has been known for a while and PHP has a specially-made function to prevent these attacks. All you need to do is use the mouthful of a function
What Lets try out this function on our two previous injection attacks and see how it works. MySQL & PHP Code://NOTE: you must be connected to the database to use this function! // connect to MySQL $name_bad = "' OR 1'"; $name_bad = mysql_real_escape_string($name_bad); $query_bad = "SELECT * FROM customers WHERE username = '$name_bad'"; echo "Escaped Bad Injection: " . $query_bad . " "; $name_evil = "'; DELETE FROM customers WHERE 1 or username = '"; $name_evil = mysql_real_escape_string($name_evil); $query_evil = "SELECT * FROM customers WHERE username = '$name_evil'"; echo "Escaped Evil Injection: " . $query_evil; Display:
Escaped Bad Injection:
SELECT * FROM customers WHERE username = '' OR 1'' Escaped Evil Injection: SELECT * FROM customers WHERE username = ''; DELETE FROM customers WHERE 1 or username = '' Notice that those evil quotes have been escaped with a backslash , preventing the injection attack. Now all these queries will do is try to find a username that is just completely ridiculous:
And I don't think we have to worry about those silly usernames getting access to our MySQL database. So please do use the handy
如何防止SQL注入 归纳一下,主要有以下几点: 1.永远不要信任用户的输入。对用户的输入进行校验,可以通过正则表达式,或限制长度;对单引号和双"-"进行转换等。 2.永远不要使用动态拼装sql,可以使用参数化的sql或者直接使用存储过程进行数据查询存取。 3.永远不要使用管理员权限的数据库连接,为每个应用使用单独的权限有限的数据库连接。 4.不要把机密信息直接存放,加密或者hash掉密码和敏感的信息。 5.应用的异常信息应该给出尽可能少的提示,最好使用自定义的错误信息对原始错误信息进行包装 6.sql注入的检测方法一般采取辅助软件或网站平台来检测,软件一般采用sql注入检测工具jsky,网站平台就有亿思网站安全平台检测工具。 防止注入的几种办法 其实原来就是我们需要过滤一些我们常见的关键字和符合如: Select,insert,update,delete,and,*,等等 function inject_check($sql_str) {
|outfile', $sql_str); } 或者是通过系统函数间的过滤特殊符号 Addslashes(需要被过滤的内容) PHP其他地方安全设置
1、register_globals = Off
Select * FromTable Where id=2
Select * From·Table· Where `id`=’2’ 3、正确的使用 $_POST $_GET $_SESSION 等接受参数,并加以过滤 4、提高数据库命名技巧,对于一些重要的字段可根据程序特点命名 5、对于常用方法加以封装,避免直接暴露SQL语句
<?php function inject_check($sql_str) { $check=eregi('select|insert|update|delete|'|/*|*|../|./|union|into|load_file |outfile', $sql_str); // 进行过滤 if($check) { echo "输入非法内容!!"; exit(); } else return $sql_str; } $_GET['id']=inject_check($_GET['id']); $sql="select * from `table` where `id`='".$_GET['id']."'"; echo $sql; //下面是过滤单引号的 $_GET['id']=addslashes($_GET['id']);//addslashes的应用 $sql="select * from `table` where `id`='".$_GET['id']."'"; echo $sql; ?> SQL Injection 原理: 结构化查询语言(SQL)是一种用来和数据库交互的文本语言。SQL Injection 就是利 用某些数据库的外部接口把用户数据插入到实际的数据库操作语言(SQL)当中,从而达到入侵数据库乃至操作系统的目的。它的产生主要是由于程序对用户输入 的数据没有进行严格的过滤,导致非法数据库查询语句的执行。 如下面的用户登陆验证程序: 二、开发中可以采取的措施1、prepareStatement(责任编辑:IT) |