MySQL使用MariaDB Audit Plugin实现审计功能

MySQL使用MariaDB Audit Plugin实现审计功能。

一、安装MariaDB Audit Plugin

1.1 准备MariaDB安装环境

为了不对使用mysql的主机造成影响,建议使用另一台与mysql主机系统一致的主机来安装MariaDB,然后不用执行下面的卸载操作

text
1
2
3
sudo apt-get autoremove --purge mysql-\*
sudo rm -rf /var/lib/mysql*
sudo rm -rf /etc/mysql/

1.2 安装MariaDB

text
1
sudo apt-get install mariadb-server

安装完成之后在/usr/lib/mysql/plugin目录中有server_audit.so插件,将该插件拷贝出来备用(拷贝到mysql主机的/usr/lib/mysql/plugin目录中)

1.3 MySQL环境恢复

如果是在mysql的主机上进行安装MariaDB操作,使用如下命令将MariaDB卸载,并重新安装mysql

text
1
2
3
4
5
sudo apt-get remove --purge mariadb-\*
sudo rm -rf /etc/mysql/
sudo rm -rf /var/lib/mysql*
sudo apt-get remove --purge mysql-\*
sudo apt-get install mysql-server mysql-client

1.4 添加MySQL配置

修改/etc/mysql/my.cnf文件,在文件末尾添加如下内容

text
1
2
3
4
5
[mysqld]
log_output=FILE
server_audit_file_path=/var/log/mysql/audit.log
server_audit_events = 'QUERY_DCL,QUERY_DML_NO_SELECT,QUERY_DML,QUERY_DDL,TABLE,QUERY,CONNECT'
server_audit_logging=1

重新启动MySQL

text
1
service mysql restart

二、Mysql审计日志分析

2.1 server_audit_events变量过滤功能

使用mysql -uroot -p登陆数据库,执行如下命令修改变量为如下

text
1
set global server_audit_events='QUERY_DDL';

执行如下SQL语句

text
1
2
3
4
5
create database test2;
use test2;
create table table1 (col1 VARCHAR(20));
insert into table1 values ("lili");
select * from table1;
text
1
2
20191019 20:56:50,delta-mysql,root,localhost,16,1300,QUERY,test,'create database test2',0
20191019 20:57:29,delta-mysql,root,localhost,16,1306,QUERY,test2,'create table table1 (col1 VARCHAR(20))',0

由上图可见只有create类型的日志被记录下来,insert和select类型的操作日志并没有被记录下来,并且操作类型字段显示为QUERY类型

使用mysql -uroot -p登陆数据库,执行如下命令修改变量为如下

text
1
set global server_audit_events='QUERY_DDL,QUERY_DML';
text
1
2
3
4
5
create database test3;
use test3;
create table table2 (col1 VARCHAR(20));
insert into table2 values ("lili");
select * from table2;

前两行是上面的日志,后面的是新产生的日志

text
1
2
3
4
5
6
7
8
9
10
11
12
20191019 20:56:50,delta-mysql,root,localhost,16,1300,QUERY,test,'create database test2',0
20191019 20:57:29,delta-mysql,root,localhost,16,1306,QUERY,test2,'create table table1 (col1 VARCHAR(20))',0
20191019 21:45:18,delta-mysql,root,localhost,16,1312,QUERY,test2,'create table table2 (col1 VARCHAR(20))',0
20191019 21:45:33,delta-mysql,root,localhost,16,1313,QUERY,test2,'insert into table2 values ("lili")',0
20191019 21:45:56,delta-mysql,root,localhost,16,1314,QUERY,test2,'select * from table2',0
20191019 21:47:52,delta-mysql,root,localhost,16,1316,QUERY,test2,'create databse test3',1064
20191019 21:47:58,delta-mysql,root,localhost,16,1317,QUERY,test2,'create database test3',0
20191019 21:48:08,delta-mysql,root,localhost,16,1318,QUERY,test2,'SELECT DATABASE()',0
20191019 21:48:15,delta-mysql,root,localhost,16,1320,QUERY,test2,'SELECT DATABASE()',0
20191019 21:48:31,delta-mysql,root,localhost,16,1324,QUERY,test3,'create table table2 (col1 VARCHAR(20))',0
20191019 21:48:31,delta-mysql,root,localhost,16,1325,QUERY,test3,'insert into table2 values ("lili")',0
20191019 21:48:31,delta-mysql,root,localhost,16,1326,QUERY,test3,'select * from table2',0

由上可见,create,select和insert类型的日志均被记录下来,并且操作类型字段均为QUERY类型,并不是QUERY_DDL和QUERY_DML类型

根据官方文档显示,QUERY_DDL操作类型指的是进行CREATE等操作时才产生QUERY_DDL操作类型的日志,QUERY_DML操作类型是进行SELECT等操作时才产生QUERY_DML操作类型的日志。

综上所述,server_audit_events确实对操作日志起到了过滤作用,但是实际日志中操作类型字段只有QUERY。说明QUERY_DDL确实起到了日志类型过滤作用,实际操作类型字段仍为QUERY。

2.2TABLE Event分析

按照官方文档显示,执行查询操作时会显示操作类型为READ的TABLE事件日志

text
1
2
3
4
5
6
20170817 16:04:33,ip-172-30-0-38,root,localhost,29,913,READ,company,employees,
20170817 16:04:33,ip-172-30-0-38,root,localhost,29,913,READ,company,employees_salaries,
20170817 16:04:33,ip-172-30-0-38,root,localhost,29,913,READ,company,ref_job_titles,
20170817 16:04:33,ip-172-30-0-38,root,localhost,29,913,READ,company,org_departments,
20170817 16:04:33,ip-172-30-0-38,root,localhost,29,913,QUERY,company,
'SELECT * FROM employee_pay WHERE title LIKE \'%Executive%\' OR title LIKE \'%Manager%\'',0

下面是在MySQL中执行查询操作产生的audit日志,由最后一条可以看出,并没有READ类型的操作日志。

text
1
2
20191020 10:48:09,delta-mysql,root,localhost,3,31,QUERY,wordpress,'SELECT * FROM wp_options WHERE option_name LIKE \'%Executive%\' OR title LIKE \'%Manager%\'',1054
20191020 10:48:19,delta-mysql,root,localhost,3,32,QUERY,wordpress,'SELECT * FROM wp_options WHERE option_name LIKE \'%Executive%\' OR option_name LIKE \'%Manager%\'',0

通过查询资料,TABLE事件只支持MariaDB 5.5.31 以及更新的版本,MySQL Server不提供MariaDB Audit Plugin需要的信息来追踪TABLE事件。

三、参考资料

MariaDB Audit Plugin 官方文档MYSQL-MARIADB AUDIT PLUGIN INSTALLATION AND CONFIGURATION