MySQL安全审计(init_connect)
1、常规安全
在说审计之前我们先提一点一般我们常用的MySQL的安全注意事项。
- 指定完善的MySQL安全流程
- 用户授权邮件备注
- 每个人对应权限均需留底
- 所有用户非管理员及特殊账户,均精细化授权
2、sql审计
MySQL安全审计,对于小公司来说既没有数据库中间件平台,也没有SQL审计平台,那么如果做一个简单高性能的数据库审计呢?今日浏览global属性时发现 init_connect的属性,这个属性可以执行用户连接后做的操作,官方使用的autocommit 做的实例,而且可以支持动态修改。那我们是不是可以利用这个特性和binlog记录实践ID,审计所有人都做了在什么时间登录做了什么事情。
官方资料: https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html
此实验的难点:
- 用户授权,所有用户及对该表有select,insert权限
- 研究在主从情况模式下如何区分对待(级联)
- 对链接的性能会有多大差距(主要针对非连接池或链接复用的情况下)
2.1 实验
创建存储用户数据的表
(root@localhost:mysql.sock) [(none)]>create database accesslog;
Query OK, 1 row affected (0.08 sec)
(root@localhost:mysql.sock) [(none)]>use accesslog
CREATE TABLE accesslog (`id` int(11) primary key auto_increment,
`thread_id` bigint COMMENT '线程ID',
`time` datetime COMMENT '时间',
`localname` varchar(100) COMMENT '连接的ID用户+地址',
`matchname` varchar(100) COMMENT '匹配到的用户ID');
创建所有需要监控的账号,给予INSERT权限
(root@localhost:mysql.sock) [accesslog]>select connection_id(), now(), user(), current_user();
+-----------------+---------------------+----------------+----------------+
| connection_id() | now() | user() | current_user() |
+-----------------+---------------------+----------------+----------------+
| 5 | 2017-06-20 09:02:12 | root@localhost | root@localhost |
+-----------------+---------------------+----------------+----------------+
1 row in set (0.05 sec)
想必大家看到如上信息,就明白了我们要干什么了,对吧。
那我们配置init_connect
SET GLOBAL init_connect='INSERT INTO accesslog.accesslog(thread_id,time,localname,matchname) values(connection_id(), now(), user(), current_user());';
接下来,见证奇迹的时刻到了
需要注意的就是,官方为了保证安全,有super权限的将not exec,原文如下(所以super。。。。):
For users that have the SUPER privilege, the content of init_connect is not executed. This is done so that an erroneous value for init_connect does not prevent all clients from connecting. For example, the value might contain a statement that has a syntax error, thus causing client connections to fail. Not executing init_connect for users that have the SUPER privilege enables them to open a connection and fix the init_connect value.
创建测试用户:
(root@localhost:mysql.sock) [(none)]>grant create ,drop, select ,insert, update on testdemo.* to evantest@'%' identified by '123';
Query OK, 0 rows affected, 1 warning (0.05 sec)
(root@localhost:mysql.sock) [(none)]>grant insert on accesslog.* to evantest@'%';
Query OK, 0 rows affected (0.00 sec)
测试效果
[root@MySQL-Node-07 mysql_test_data]# mysql -uevantest -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(evantest@localhost:mysql.sock) [(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| accesslog |
| testdemo |
+--------------------+
3 rows in set (0.00 sec)
(evantest@localhost:mysql.sock) [(none)]>use testdemo
Database changed
(evantest@localhost:mysql.sock) [testdemo]>create table testtable(id int comment 'test');
Query OK, 0 rows affected (0.02 sec)
查看数据库:
(root@localhost:mysql.sock) [accesslog]>select * from accesslog;
+----+-----------+---------------------+--------------------+----------------+
| id | thread_id | time | localname | matchname |
+----+-----------+---------------------+--------------------+----------------+
| 1 | 6 | 2017-06-20 09:06:32 | root@localhost | root@localhost |
| 2 | 10 | 2017-06-20 09:17:35 | evantest@localhost | evantest@% |
+----+-----------+---------------------+--------------------+----------------+
2 rows in set (0.00 sec)
查看执行了那些操作:
解析binlog
# at 2895
#170620 9:17:35 server id 1 end_log_pos 2926 CRC32 0x45ce5d15 Xid = 67
COMMIT/*!*/;
# at 2926
#170620 9:18:02 server id 1 end_log_pos 2991 CRC32 0x86520809 Anonymous_GTID last_committed=11 sequence_number=12
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2991
#170620 9:18:02 server id 1 end_log_pos 3118 CRC32 0xf03899c0 Query thread_id=10 exec_time=0 error_code=0
use `testdemo`/*!*/;
SET TIMESTAMP=1497921482/*!*/;
create table testtable(id int comment 'test')
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
这样我们就可以通过线程ID + 时间锁定对应的人在对应的时间做了那些事情。
MySQL安全审计(init_connect)的更多相关文章
- MySql 性能调优策略
本主题调优针对于my.cnf配置来做详细的参数说明 示例配置如下: #cat my.cnf # MySQL client library initialization. [client] port = ...
- 更改 AWS RDS mysql时区 -摘自网络
AWS RDS AWS上搭建数据库的时候,不是DB on EC2就是RDS,但是选择RDS时,Timezone怎么处理? 「面向全球提供的AWS来讲理所当然的是UTC」,而RDS也不是例外.把服务器迁 ...
- linux MySQL5.7 rpm安装(转)
删除旧包: # rpm -qa | grep -i mysql # rpm -ev mysql-libs-* --nodeps 安装rpm包: # rpm -ivh mysql-community-c ...
- 10-MySQlL DBA笔记-基础知识
第四部分 运维篇 首先来了解一下数据库的定义,数据库是高效的.可靠的.易用的.安全的多用户存储引擎,我们可以通过它访问大量的持久化数据.我们管理和维护数据库,本质上也是要确保如上的特性,尽可能地保证数 ...
- Mysql-从库只读设置
主从设置中,如果从库在my.cnf中使用init_connect来限制只读权限的话,从库使用非超级用户(super权限)登陆数据时,无法进行任何操作,仅可维持主从复制. init_connect='S ...
- CentOS7下Jumpserver V3.0 部署
环境准备 # 准备一台 2核4G (最低)且可以访问互联网的 64 位 Centos 7 主机 [root@localhost ~]# hostnamectl --static set-hostnam ...
- mysql init_connect 参数的其他用处
http://blog.itpub.net/133735/viewspace-691196/ init_connect 是可以动态在线调整的,这样就有了一些其他的用处 经过测试init_conne ...
- MySQL5.7 (审计)通过init_connect + binlog 实现MySQL审计功能
转载自:https://blog.51cto.com/13941177/2173620 一.简介 1.概述 mysql本身已经提供了详细的sql执行记录–general log ,但是开启它有以下几个 ...
- mysql 5.7添加server_audit 安全审计功能
mysql 5.7添加server_audit 安全审计功能 一.根据链接下载插件 参考链接下载 http://blog.itpub.net/31441024/viewspace-2213103 l ...
随机推荐
- netty核心组件之EventLoopGroup和EventLoop
这节我们着重介绍netty最为核心的组件EventLoopGroup和EventLoop EventLoopGroup:顾名思义就是EventLoop的组,下面来看它们的继承结构 在netty中我们可 ...
- LeetCode739 每日温度
根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高的天数.如果之后都不会升高,请输入 0 来代替. 例如,给定一个列表 temperatures = [73, 74 ...
- nginx: [emerg] bind() to 0.0.0.0:80 failed (10013:
问题出现 今天在win10安装nginx时候,启动nginx.exe时在dos窗口出现了这个错误,特此记录一下. 解决方法 上面报错信息的意思大概是:0.0.0:80地址访问不被允许.可能是80端口号 ...
- 八:架构,搭建,WAF
WAF防护分析 什么是WAF应用 如何快速识别WAF 识别WAF对于安全测试的意义 CMS识别技术 源码获取技术 架构信息获取 站点搭建分析 搭建习惯-目录型站点 sti.blcu-bbs 目录型站点 ...
- SQL Server解惑——查询条件IN中能否使用变量
在SQL Server的查询条件中,能否在IN里面使用变量呢? 如果可以的话,有没有需要注意的地方或一些限制呢?在回答这个问题前,我们先来看看这个例子: IF EXISTS (SELECT 1 FRO ...
- xtrabackup迁移mysql5.7.32
问题描述:利用外部xtrabackup工具来做迁移mysql数据库,或者恢复数据库 xtrabackup迁移mysql 1.环境 mysql源库 mysql目标迁移库 IP 192.168.163.3 ...
- P1140 相似基因(字符串距离,递推)
题目链接: https://www.luogu.org/problemnew/show/P1140 题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了44种核苷酸,简记作A,C,G,TA,C, ...
- 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code
问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...
- LeetCode202. 快乐数
题目 编写一个算法来判断一个数 n 是不是快乐数. 快乐数定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1, 也可能是 无限循环 但始终变不到 ...
- 攻防世界 - Web(三)
PHP2: 1.进入页面,进行抓包或后台扫描都没有什么发现,然后网上查一波wp,发现是关于.phps文件,进入index.phps,弹出一段代码,查看源代码, <?php if("ad ...