首页 > 编程技术 > php

PHP SQL 注入攻击预防办法与注入分析

发布时间:2016-11-25 15:25

1. php教程 配置文件 php.ini 中的 magic_quotes_gpc 选项没有打开,被置为 off 2. 开发者没有对数据类型进行检查和转义

不过事实上,第二点最为重要。我认为, 对用户输入的数据类型进行检查,向 mysql教程 提交正确的数据类型,这应该是一个 web 程序员最最基本的素质。但现实中,常常有许多小白式的 web 开发者忘了这点, 从而导致后门大开。

为什么说第二点最为重要?因为如果没有第二点的保证,magic_quotes_gpc 选项,不论为 on,还是为 off,都有可能引发 sql 注入攻击。下面来看一下技术实现:

一. magic_quotes_gpc = off 时的注入攻击

magic_quotes_gpc = off 是 php 中一种非常不安全的选项。新版本的 php 已经将默认的值改为了 on。但仍有相当多的服务器的选项为 off。毕竟,再古董的服务器也是有人用的。

当magic_quotes_gpc = on 时,它会将提交的变量中所有的 '(单引号)、"(双号号)、(反斜线)、空白字符,都为在前面自动加上 。下面是 php 的官方说明:

view sourceprint?

 

magic_quotes_gpc boolean

 

sets the magic_quotes state for gpc (get/post/cookie) operations. when magic_quotes are on, all ' (single-quote), " (double quote), (backslash) and nul's are escaped with a backslash automatically


如果没有转义,即 off 情况下,就会让攻击者有机可乘。以下列测试脚本为例:

 1<?
 2if ( isset($_post["f_login"] ) )
 3{
 4  // 连接数据库教程...
 5  // ...代码略...
 6
 7  // 检查用户是否存在
 8  $t_struname = $_post["f_uname"];
 9  $t_strpwd = $_post["f_pwd"];
10  $t_strsql = "select * from tbl_users where username='$t_struname' and password = '$t_strpwd' limit 0,1";
11
12  if ( $t_hres = mysql_query($t_strsql) )
13  {
14    // 成功查询之后的处理. 略...
15  }
16}
17?>
18
19<html><head><title>sample test</title></head>
20<body>
21<form method=post action="">
22  username: <input type="text" name="f_uname" size=30><br>
23  password: <input type=text name="f_pwd" size=30><br>
24
25  <input type="submit" name="f_login" value="登录">
26</form>
27</body>

 

在这个脚本中,当用户输入正常的用户名和密码,假设值分别为 zhang3、abc123,则提交的 sql 语句如下:

1 select * from tbl_users
2 where username='zhang3' and password = 'abc123' limit 0,1

 


如果攻击者在 username 字段中输入:zhang3' or 1=1 #,在 password 输入 abc123,则提交的 sql 语句变成如下:

1 select * from tbl_users
2 where username='zhang3' or 1=1 #' and password = 'abc123' limit 0,1

 

 

 


由于 # 是 mysql中的注释符, #之后的语句不被执行,实现上这行语句就成了:

1 select * from tbl_users
2 where username='zhang3' or 1=1

 

 

 


这样攻击者就可以绕过认证了。如果攻击者知道数据库结构,那么它构建一个 union select,那就更危险了:

假设在 username 中输入:zhang3 ' or 1 =1 union select cola, colb,cold from tbl_b #

在password 输入: abc123,

则提交的 sql 语句变成:

1 select * from tbl_users
2 where username='zhang3 '
3 or 1 =1 union select cola, colb,cold from tbl_b #' and password = 'abc123' limit 0,1

 

 

 


这样就相当危险了。如果agic_quotes_gpc选项为 on,引号被转义,则上面攻击者构建的攻击语句就会变成这样,从而无法达到其目的:

1 select * from tbl_users
2 where username='zhang3' or 1=1 #'
3 and password = 'abc123'
4 limit 0,1
5
6 select * from tbl_users
7 where username='zhang3 ' or 1 =1 union select cola, colb,cold from tbl_b #'
8 and password = 'abc123' limit 0,1

 

 

 


二. magic_quotes_gpc = on 时的注入攻击

当 magic_quotes_gpc = on 时,攻击者无法对字符型的字段进行 sql 注入。这并不代表这就安全了。这时,可以通过数值型的字段进行sql注入。

在最新版的 mysql 5.x 中,已经严格了数据类型的输入,已默认关闭自动类型转换。数值型的字段,不能是引号标记的字符型。也就是说,假设 uid 是数值型的,在以前的 mysql 版本中,这样的语句是合法的:

1 insert into tbl_user set uid="1";
2 select * from tbl_user where uid="1";

 

 

 


在最新的 mysql 5.x 中,上面的语句不是合法的,必须写成这样:

1 insert into tbl_user set uid=1;
2 select * from tbl_user where uid=1;

 

 

 


这样我认为是正确的。因为作为开发者,向数据库提交正确的符合规则的数据类型,这是最基本的要求。

那么攻击者在 magic_quotes_gpc = on 时,他们怎么攻击呢?很简单,就是对数值型的字段进行 sql 注入。以下列的 php 脚本为例:

 1 <?
 2  if ( isset($_post["f_login"] ) )
 3 {
 4   // 连接数据库...
 5   // ...代码略...
 6 
 7   // 检查用户是否存在
 8    $t_struid = $_post["f_uid"];
 9   $t_strpwd = $_post["f_pwd"];
10   $t_strsql = "select * from tbl_users where uid=$t_struid and password = '$t_strpwd' limit 0,1";
11   if ( $t_hres = mysql_query($t_strsql) )
12   {
13     // 成功查询之后的处理. 略...
14   }
15
16 }
17 ?>
18 <html><head><title>sample test</title></head>
19 <body>
20 <form method=post action="">
21   user id: <input type="text" name="f_uid" size=30><br>
22
23   password: <input type=text name="f_pwd" size=30><br>
24   <input type="submit" name="f_login" value="登录">
25 </form>
26 </body>

 

 

 


上面这段脚本要求用户输入 userid 和 password 登入。一个正常的语句,用户输入 1001和abc123,提交的 sql 语句如下:

select * from tbl_users where userid=1001 and password = 'abc123' limit 0,1

 

 

 

 

如果攻击者在 userid 处,输入:1001 or 1 =1 #,则注入的sql语句如下:

select * from tbl_users where userid=1001 or 1 =1 # and password = 'abc123' limit 0,1

 

 

 

 

攻击者达到了目的。

三. 如何防止 php sql 注入攻击

如何防止 php sql 注入攻击?我认为最重要的一点,就是要对数据类型进行检查和转义。总结的几点规则如下:

php.ini 中的 display_errors 选项,应该设为 display_errors = off。这样 php 脚本出错之后,不会在 web 页面输出错误,以免让攻击者分析出有作的信息。

调用 mysql_query 等 mysql 函数时,前面应该加上 @,即 @mysql_query(...),这样 mysql 错误不会被输出。同理以免让攻击者分析出有用的信息。另外,有些程序员在做开发时,当 mysql_query出错时,习惯输出错误以及 sql 语句,例如:  1 $t_strsql = "select a from b....";
 2 if ( mysql_query($t_strsql) )
 3 {
 4   // 正确的处理
 5 }
 6 else
 7 {
 8   echo "错误! sql 语句:$t_strsql rn错误信息".mysql_query();
 9   exit;
10 }

 

 

 
 

这种做法是相当危险和愚蠢的。如果一定要这么做,最好在网站的配置文件中,设一个全局变量或定义一个宏,设一下 debug 标志:

 1 //全局配置文件中:
 2 define("debug_mode",0);    // 1: debug mode; 0: release mode
 3
 4 //调用脚本中:
 5 $t_strsql = "select a from b....";
 6 if ( mysql_query($t_strsql) )
 7 {
 8   // 正确的处理
 9 }
10 else
11 {
12   if (debug_mode)
13     echo "错误! sql 语句:$t_strsql rn错误信息".mysql_query();
14   exit;
15 }

 

 

 
 

 

对提交的 sql 语句,进行转义和类型检查。

 

四. 我写的一个安全参数获取函数

为了防止用户的错误数据和 php + mysql 注入 ,我写了一个函数 papi_getsafeparam(),用来获取安全的参数值:

 1 define("xh_param_int",0);
 2 define("xh_param_txt",1);
 3 function papi_getsafeparam($pi_strname, $pi_def = "", $pi_itype = xh_param_txt)
 4 {
 5   if ( isset($_get[$pi_strname]) )
 6     $t_val = trim($_get[$pi_strname]);
 7   else if ( isset($_post[$pi_strname]))
 8     $t_val = trim($_post[$pi_strname]);
 9   else
10     return $pi_def;
11
12   // int
13   if ( xh_param_int == $pi_itype)
14   {
15     if (is_numeric($t_val))
16       return $t_val;
17     else
18       return $pi_def;
19   }
20  
21   // string
22   $t_val = str_replace("&", "&amp;",$t_val);
23   $t_val = str_replace("<", "&lt;",$t_val);
24   $t_val = str_replace(">", "&gt;",$t_val);
25   if ( get_magic_quotes_gpc() )
26   {
27     $t_val = str_replace(""", "&quot;",$t_val);
28     $t_val = str_replace("''", "&#039;",$t_val);
29   }
30   else
31   {
32     $t_val = str_replace(""", "&quot;",$t_val);
33     $t_val = str_replace("'", "&#039;",$t_val);
34   }
35   return $t_val;
36 }

 

 

 


在这个函数中,有三个参数:

$pi_strname: 变量名
$pi_def: 默认值
$pi_itype: 数据类型。取值为 xh_param_int, xh_param_txt, 分别表示数值型和文本型。

 

 

 


如果请求是数值型,那么调用 is_numeric() 判断是否为数值。如果不是,则返回程序指定的默认值。

简单起见,对于文本串,我将用户输入的所有危险字符(包括html代码),全部转义。由于 php 函数 addslashes()存在漏洞,我用 str_replace()直接替换。get_magic_quotes_gpc() 函数是 php 的函数,用来判断 magic_quotes_gpc 选项是否打开。

刚才第二节的示例,代码可以这样调用:

 1 <?
 2 if ( isset($_post["f_login"] ) )
 3 {
 4   // 连接数据库...
 5   // ...代码略...
 6 
 7   // 检查用户是否存在
 8   $t_struid = papi_getsafeparam("f_uid", 0, xh_param_int);
 9   $t_strpwd = papi_getsafeparam("f_pwd", "", xh_param_txt);
10   $t_strsql = "select * from tbl_users where uid=$t_struid and password = '$t_strpwd' limit 0,1";
11   if ( $t_hres = mysql_query($t_strsql) )
12   {
13     // 成功查询之后的处理. 略...
14   }
15 }
16 ?>

注意:以下代码需要打开php教程的gd库,修改php.in文件的配置,把已经注释掉的行之前的分号取消即可:extension=php_gd2.dll。

$width = 165;
        $height = 120;
        $image = imagecreatetruecolor($width,$height);
        $bg_color = imagecolorallocate($image,255,255,255);
        $tm = imagecolorallocate($image,255,0,0);
        imagefilledrectangle($image,0,0,$width,$height,$bg_color);
        imagefill($image,0,0,$bg_color);
        /*
        //$text = random_text(5);
        $text = "ffff";
        $font = 8;
        $struft=iconv("gbk","utf-8",$text);
        $x = imagesx($image);
        $y = imagesy($image);
        $fg_color = imagecolorallocate($image,233,14,91);
        imagestring($image,$font,$x,$y,$struft,$fg_color);
        $_session['captcha'] = $text;
        */
header("content-type:image/png");
        imagepng($image);
        imagedestroy($image);

实例二

validate.php

采用了session识别,稍微改进了一下目前网络上流传的php验证码,加入杂点,数字颜色随机显示,控制4位数字显示;

<?
session_start();
//生成验证码图片
header("content-type: image/png");
$im = imagecreate(44,18);
$back = imagecolorallocate($im, 245,245,245);
imagefill($im,0,0,$back); //背景

srand((double)microtime()*1000000);
//生成4位数字
for($i=0;$i<4;$i++){
$font = imagecolorallocate($im, rand(100,255),rand(0,100),rand(100,255));
$authnum=rand(1,9);
$vcodes.=$authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}

for($i=0;$i<100;$i++) //加入干扰象素
{
$randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
imagepng($im);
imagedestroy($im);

$_session['vcode'] = $vcodes;
?>

下面看完整实例

<?php

    @header("content-type:text/html; charset=utf-8");

    //打开session

    session_start();

?>

<html>

    <head>

       <meta http-equiv="content-type" content="text/html; charset=utf-8" />

       <title>php验证码示例</title>

    </head>

    <body>

       验证码:<br/>

       <iframe id="iimg" height="100" width=300" width=100% src="img.php" frameborder="0" ></iframe>

       <br/>

       <input type=button value="看不清,换一张" onclick="iimg.location.reload();">

       <br>

       <form action="validate.php" method="post">

           输入验证码:<input name="imgid" style="width:60">

           <input type="submit" value="确定">

       </form>

    </body>

</html>

php判断用户输入的验证码是否与系统生成的一致

<?php @header("content-type:text/html; charset=utf-8");

    //开启session

    session_start();

    //得到用户输入的验证码,并转换成大写

    $imgid_req = $_request['imgid'];

    $imgid_req = strtoupper($imgid_req);

    //验证该字符串是否注册了session变量

    if (session_is_registered($imgid_req)) {

       echo "<font color=blue >通过验证!</font>";

    } else {

       echo "<font color=red >验证错误!</font>";

    }

    //关闭session,以清除所有注册过的变量

    session_destroy();

?>

php教程 防止注入的几种办法

其实原来就是我们需要过滤一些我们常见的关键字和符合如:

select,insert,update,delete,and,*,等等

例子:

function inject_check($sql_str) {
return eregi('select|insert|update|delete|'|/*|*|../|./|union|into|load_file
|outfile', $sql_str); // 进行过滤
}

或者是通过系统函数间的过滤特殊符号

addslashes(需要被过滤的内容)

二、 php其他地方安全设置

1、register_globals = off 设置为关闭状态

2、sql语句书写时尽量不要省略小引号和单引号

select * from table where id=2 (不规范)

select * from ·table· where ·id·=’2’ (规范)

3、正确的使用 $_post $_get $_session 等接受参数,并加以过滤

4、提高数据库教程命名技巧,对于一些重要的字段可根据程序特点命名

5、对于常用方法加以封装,避免直接暴露sql语句

 

要防sql注入我必须从sql语句到php教程 get post 等数据接受处理上来做文章了,下面我们主要讲php 与mysql教程的sql语句上处理方法 ,可能忽略的问题。

看这个例子:

  

// supposed input

  $name = “ilia’; delete from users;”;

  mysql_query(“select * from users where name=’{$name}’”);

  很明显最后数据库教程执行的命令是:

  select * from users where name=ilia; delete from users

  这就给数据库带来了灾难性的后果--所有记录都被删除了。

  不过如果你使用的数据库是mysql,那么还好,mysql_query()函数不允许直接执行这样的操作(不能单行进行多个语句操作),所以你可以放心。如果你使用的数据库是sqlite或者postgresql,支持这样的语句,那么就将面临灭顶之灾了。

  上面提到,sql注入主要是提交不安全的数据给数据库来达到攻击目的。为了防止sql注入攻击,php自带一个功能可以对输入的字符串进行处理,可以在较底层对输入进行安全上的初步处理,也即magic quotes。(php.ini magic_quotes_gpc)。如果magic_quotes_gpc选项启用,那么输入的字符串中的单引号,双引号和其它一些字符前将会被自动加上反斜杠。

  但magic quotes并不是一个很通用的解决方案,没能屏蔽所有有潜在危险的字符,并且在许多服务器上magic quotes并没有被启用。所以,我们还需要使用其它多种方法来防止sql注入。

  许多数据库本身就提供这种输入数据处理功能。例如php的mysql操作函数中有一个叫mysql_real_escape_string()的函数,可将特殊字符和可能引起数据库操作出错的字符转义。

  看这段代码:

  

//如果magic quotes功用启用

  if (get_magic_quotes_gpc()) {

  $name = strips教程lashes($name);

  }else{

  $name = mysql_real_escape_string($name);

  }

  mysql_query(“select * from users where name=’{$name}’”);

  注意,在我们使用数据库所带的功能之前要判断一下magic quotes是否打开,就像上例中那样,否则两次重复处理就会出错。如果mq已启用,我们要把加上的去掉才得到真实数据。

  除了对以上字符串形式的数据进行预处理之外,储存binary数据到数据库中时,也要注意进行预处理。否则数据可能与数据库自身的存储格式相冲突,引起数据库崩溃,数据记录丢失,甚至丢失整个库的数据。有些数据库如 postgresql,提供一个专门用来编码二进制数据的函数pg_escape_bytea(),它可以对数据进行类似于base64那样的编码。

  如:

 

 // for plain-text data use:

  pg_escape_string($regular_strings);

  // for binary data use:

  pg_escape_bytea($binary_data);

php教程 防止sql注入代码

*/ 

 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;
  }
 }
 function checkurl() { //检查来路
  if (preg_replace("/https教程?://([^:/]+).*/i", "1", $_server['http_referer']) !== preg_replace("/([^:]+).*/", "1", $_server['http_host'])) {
   header("location: http://www.111cn.net");
   exit();
  }
 }

//调用
checkurl();
$str = $_get['url'];
inject_check($sql_str);//这条可以在获取参数时执行操作

标签:[!--infotagslink--]

您可能感兴趣的文章: