OWASP top10--SQL注入(一)

       SQL注入式攻击技术,一般针对基于Web平台的应用程序.造成SQL注入攻击漏洞的原因,是由于程序员在编写Web程序时,没有对浏览器端提交的参数进行严格的过滤和判断。用户可以修改构造参数,提交SQL查询语句,并传递至服务器端,从而获取想要的敏感信息,甚至执行危险的代码或系统命令。

      虽然SQL注入攻击技术早已出现,但是时至今日仍然有很大一部分网站存在SQL注入漏洞,各大门户网站同样存在SQL注入漏洞。由于SQL漏润存在的普遍性,因此SQL入侵攻击技术往往成为黑客入侵攻击网站渗透内部服务的首选技术,其危害性非常大。


01:SQL注入漏洞概念/原理

  1. 由于程序员在编写Web程序时,没有对浏览器端提交的参数进行严格的过滤和判断  
  2. 注入产生的原因是接受相关参数未经处理直接带入数据库查询操作;注入攻击属于服务端攻击,他与操作系统、数据库类型、脚本语言类型无关 。

环境准备:win2003  phpstudy2016  mysql靶场环境、pikachu靶场环境

打开 musql靶场环境:

查看源代码:(sql.php)

<?
$id=$_GET['id'];//接受get方式传递参数名id的值并赋值给变量id

{
     $conn=mysql_connect("127.0.0.1","root","root");
if($conn){
      echo "ok!";
}//判断连接是否成功
mysql_select_db('sql',$conn);//选择连接请求为conn的数据库
$sql="select * from user where id=$id";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)){ //数组遍历
      echo "用户ID: ".$row['id']."<br >";
      echo "用户名: ".$row['username']."<br >";
      echo "用户密码: ".$row['password']."<br >";
      echo "用户邮箱: ".$row['email']."<br >";

}
mysql_close($conn);
echo "<hr>";
echo "你当前执行的sql语句为:";
echo "select * from user where id=$id";
}
?>

 说明:从GET请求中获取的参数id直接赋值给变量$id时,没有进行任何过滤或验证,产生SQL注入攻击漏洞。(参数id给变量$id未经过滤,直接带入数据库查询)

就可以在参数中编写SQL语句信息,最终需要将SQL语句发送到服务端(数据库服务)

02:SQL注入步骤


- 探测到SQL注入的注入点
- 需要绕过SQL注入防范机制
- 发送SQL注入SQL语句命令
- 获取数据库数据/篡改数据库数据 


03:SQL注入漏洞检测方法

单引号  

and

or  

Xor

%26%26

true  false

order by

单引号:

 mysql靶场:

http://10.0.0.101:90/mysql/sql.php?id=1

不加单引号能输出内容:

 加单引号不能输出内容:

打开phpstudy菜单mysql数据库命令行:

  查看数据库直接执行命令输出的内容:

 说明:主要判断是否与数据交互


and (和):

and 1=1和and 1=2

 mysql靶场

and 1=1输出数据库内容:

and 1=2 未输出数据库内容:

 查看数据库直接执行命令输出的内容:

and 1=1为真,数据库输出内容;and 1=2为假,数据库没有输出内容;

说明:防护机制未对and进行过滤,执行sql语句成功与数据库交互输出内容。


 or(或):

or 1=1   or 1=2 (or表示或)

or 1=1输出全部内容:(or表示或;1=1为真全部输出)

 or 1=2只输出查询内容:

 查看数据库直接执行命令输出的内容:


Xor(异或):

Xor 1=1   Xor 1=2( Xor表示异或)

(单引号、and、or被防护机制过滤了可尝试Xor进行绕过)

Xor 1=1输出di不是1的内容:

Xor1=2输出di为1的内容: 

 查看数据库直接执行命令输出的内容:

Xor说明:select * from test where A xor B

如果B为真,则查出不满足A条件数据;

如果B为假,则查出满足A条件数据;


%26%26(&&经url编码):

%26%26 1=1  %26%26 1=2

(因在URL中,&通常用于分隔多个参数;将&&通过URL编码绕过为%26%26)

%26%26 1=1

%26%26 1=2


true  false:

true为真  false为假

and true   and false

or true   or false

Xor true   Xor false

%26%26 true   %26%26 false

如果数字(1=1)被防护机制过滤可尝试此方式绕过;(true为真、false为假)

and true

and false 

 查看数据库直接执行命令输出的内容: 


mod(8,7)in(1)  mod(8,7)in(2)(求余函数)

使用求余函数mod;即mod(8,7)in(1)为真;mod(8,7)in(2)为假

and mod(8,7)in(1)   and mod(8,7)in(2)

or mod(8,7)in(1)   or mod(8,7)in(2)

Xor mod(8,7)in(1)   Xor mod(8,7)in(2)

%26%26 mod(8,7)in(1)   %26%26 mod(8,7)in(2)

and mod(8,7)in(1)为真;输出内容:

and mod(8,7)in(2)为假; 不输出内容:


order by(排序):

order by 4   order by 5 (探测有几列信息)

order by 4为真;user表有4个列输出内容:

 order by 5为假;user表有5个列不输出内容:

  查看数据库直接执行命令输出的内容:

 说明:

select * from user where id=1 order by 5 

这个SQL查询将从user表中选择所有id为1的记录,并按照第5个列进行排序。这里的数字5通常是指列的索引,从1开始计数。这意味着它会尝试按照表中第五个列的值进行排序。 如果user表中没有第五个列,或者5超出了列的数量,查询将会出错。


 04:哪些网页会有SQL注入漏洞

  • 与数据库交互的相关页面

    • http://www.*****.com/***.asp?id=xx (ASP注入)

    • http://www.*****.com/***.php?id=xx (php注入)

    • http://www.*****.com/***.jsp?id=xx (jsp注入)

有传参的地方

登录的地方

更新的地方

注册的地方

留言板

查询搜索

删除等。。

  • Http Header注入

    • 把http header直接代入数据库

  • Cookie注入

    • 数据参数写入到cookice参数里面

  • 可能出现注入的地方:http头、cookices、referee、user agent,post提交数据包的地方等等

 05:SQL注入分类

数字型

and 1=1  and 1=2

打开pikachu靶场环境,选择数字型注入:

http://10.0.0.101:90/pikachu/vul/sqli/sqli_id.php

点击查询--用bp抓包--发到bp重发器--插入sql注入语句测试: 

and 1=1

 and 1=2

 

存在sql注入点 


 搜索型

'%xx%' or 1=1

user' and 1=1 #    user' and 1=2 #

打开pikachu靶场环境:

http://10.0.0.101:90/pikachu/vul/sqli/sqli_str.php

查看源代码:

<?php
/**
 * Created by runner.han
 * There is nothing new under the sun
 */


$SELF_PAGE = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);

if ($SELF_PAGE = "sqli_str.php"){
    $ACTIVE = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','active open','','','active','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');
}

$PIKA_ROOT_DIR =  "../../";
include_once $PIKA_ROOT_DIR . 'header.php';

include_once $PIKA_ROOT_DIR."inc/config.inc.php";
include_once $PIKA_ROOT_DIR."inc/function.php";
include_once $PIKA_ROOT_DIR."inc/mysql.inc.php";

$link=connect();
$html='';

if(isset($_GET['submit']) && $_GET['name']!=null){
    //这里没有做任何处理,直接拼到select里面去了
    $name=$_GET['name'];
    //这里的变量是字符型,需要考虑闭合
    $query="select id,email from member where username='$name'";
    $result=execute($link, $query);
    if(mysqli_num_rows($result)>=1){
        while($data=mysqli_fetch_assoc($result)){
            $id=$data['id'];
            $email=$data['email'];
            $html.="<p class='notice'>your uid:{$id} <br />your email is: {$email}</p>";
        }
    }else{

        $html.="<p class='notice'>您输入的username不存在,请重新输入!</p>";
    }
}



?>

 其中: $query="select id,email from member where username='$name'";

'$name'变量是字符型有单引号,要绕过需要先加单引号进行闭合,原来的单引号进行注释:

如:user' and 1=2 #

$query="select id,email from member where username='user' and 1=2 #'";

通过添加 '来尝试关闭原本的字符串,然后添加and 1=2 一个始终为假,最后用#注释掉剩余的查询部分。

 user' and 1=1 # 为真;输出内容:

 

 user' and 1=2 # 为假,不输出内容:

 


搜索型

'%user%' or 1=1        '%user%' or 1=2

 打开pikachu靶场环境:

http://10.0.0.101:90/pikachu/vul/sqli/sqli_search.php

查看源代码:

<?php
/**
 * Created by runner.han
 * There is nothing new under the sun
 */


$SELF_PAGE = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);

if ($SELF_PAGE = "sqli_search.php"){
    $ACTIVE = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','active open','','','','active','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');
}

$PIKA_ROOT_DIR =  "../../";
include_once $PIKA_ROOT_DIR . 'header.php';

include_once $PIKA_ROOT_DIR."inc/config.inc.php";
include_once $PIKA_ROOT_DIR."inc/function.php";
include_once $PIKA_ROOT_DIR."inc/mysql.inc.php";

$link=connect();
$html1='';
$html2='';
if(isset($_GET['submit']) && $_GET['name']!=null){

    //这里没有做任何处理,直接拼到select里面去了
    $name=$_GET['name'];

    //这里的变量是模糊匹配,需要考虑闭合
    $query="select username,id,email from member where username like '%$name%'";
    $result=execute($link, $query);
    if(mysqli_num_rows($result)>=1){
        //彩蛋:这里还有个xss
        $html2.="<p class='notice'>用户名中含有{$_GET['name']}的结果如下:<br />";
        while($data=mysqli_fetch_assoc($result)){
            $uname=$data['username'];
            $id=$data['id'];
            $email=$data['email'];
            $html1.="<p class='notice'>username:{$uname}<br />uid:{$id} <br />email is: {$email}</p>";
        }
    }else{

        $html1.="<p class='notice'>0o。..没有搜索到你输入的信息!</p>";
    }
}



?>

其中$query="select username,id,email from member where username like '%$name%'";

'%$name%'会匹配任何包含 $name 的字符串,无论$name在用户名中的位置如何。

如果 $name = "user",查询会找到所有用户名中包含 "user" 的记录,如 "user123"、"superuser" 等。

如:user%' or  1=1 #

$query="select username,id,email from member where username like '%user%' or  1=1 #%'"

user%' or  1=1 # ;or或;1=1为真 ;输出所有含user的内容:

 

 user%' or  1=2 # ;or或;1=2为假 ;输出只含user的内容:

 


XX型(特殊类型)

username=('xx') or 1=1

user') or  1=1 #       user') or  1=2 # 

打开pikachu靶场环境:

http://10.0.0.101:90/pikachu/vul/sqli/sqli_x.php

查看源代码:

<?php
/**
 * Created by runner.han
 * There is nothing new under the sun
 */


$SELF_PAGE = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);

if ($SELF_PAGE = "sqli_search.php"){
    $ACTIVE = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','active open','','','','','active','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');
}

$PIKA_ROOT_DIR =  "../../";
include_once $PIKA_ROOT_DIR . 'header.php';

include_once $PIKA_ROOT_DIR."inc/config.inc.php";
include_once $PIKA_ROOT_DIR."inc/function.php";
include_once $PIKA_ROOT_DIR."inc/mysql.inc.php";

$link=connect();
$html='';

if(isset($_GET['submit']) && $_GET['name']!=null){
    //这里没有做任何处理,直接拼到select里面去了
    $name=$_GET['name'];
    //这里的变量是字符型,需要考虑闭合
    $query="select id,email from member where username=('$name')";
    $result=execute($link, $query);
    if(mysqli_num_rows($result)>=1){
        while($data=mysqli_fetch_assoc($result)){
            $id=$data['id'];
            $email=$data['email'];
            $html.="<p class='notice'>your uid:{$id} <br />your email is: {$email}</p>";
        }
    }else{

        $html.="<p class='notice'>您输入的username不存在,请重新输入!</p>";
    }
}



?>

 其中$query="select id,email from member where username=('$name')";

('$name')加了单引号和括号,进行绕过需要加单引号括号进行闭合。

如:user') or  1=1 #

$query="select id,email from member where username=('user') or  1=1 #')"

 

 user') or  1=1 # ;or或;1=1为真 ;输出所有含user的内容:

user') or  1=2 # ;or或;1=2为假 ;输出只含user的内容:

 


 

声明:

  • 此文章只做技术研究,谨遵守国家相关法律法规,请勿用于违法用途,如果您对文章内容有疑问,可以尝试留言私信,如有侵权请联系小编处理。
  • 20
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值