数据库审计功能主要将用户对数据库的各类操作行为记录审计日志,以便日后进行跟踪、查询、分析,以实现对用户操作的监控和审计。审计是一项非常重要的工作,也是企业数据安全体系的重要组成部分,等保测评中也要求有审计日志。对于 DBA 而言,数据库审计也极其重要,特别是发生人为事故后,审计日志便于进行责任追溯,问题查找。
当前 MySQL 社区版本并没有提供相关的插件使用,虽然 MySQL 提供有 binlog 及 general log ,这二者虽然具备部分审计功能,但一般不当做审计日志来看待。
常见的审计插件有 MariaDB Audit Plugin、Percona Audit Log Plugin、McAfee MySQL Audit Plugin 三种,MariaDB 自带的审计插件比较适合用于 MySQL 社区版,下面我们来学习下如何使用审计插件来实现审计功能。
首先我们需要安装一个MySQL,该步骤就先跳过一下。
MariaDB 审计插件的名称是 server_audit.so(Windows系统下是 server_audit.dll ),要注意的是,审计插件一直在更新,不同版本的审计插件功能也不同,推荐使用 >= 1.4.4 版本的插件。
由于 MariaDB Auditing Plugin 集成在MariaDB里面,没有单独提供,所以我们需要先下载一个MariaDB。
下面我们以 CentOS 系统 MySQL 5.7 版本为例来安装下审计插件:
MariaDB下载地址:https://mariadb.com/kb/en/postdownload/mariadb-server-5-5-64/
进入plugin子目录,查看下是否要另外安装依赖
#tar xvpf tar xvpf mariadb-5.5.64-linux-systemd-x86_64.tar.gz
#cd mariadb-10.2.44-linux-systemd-x86_64/lib/plugin
#ldd server_audit.so
ldd: warning: you do not have execution permission for `./server_audit.so'
linux-vdso.so.1 => (0x00007ffdc613d000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2b1c722000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2b1c354000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2b1cb4f000)
查看MySQL plugin的存放地址
mysql> show global variables like 'plugin_dir';
+---------------+--------------------------------+
| Variable_name | Value |
+---------------+--------------------------------+
| plugin_dir | /usr/local/mysql57/lib/plugin/ |
+---------------+--------------------------------+
1 row in set (0.00 sec)
将mariadb 的审计插件 server_audit.so文件复制到该路径下,并更改插件属主及权限
#cp server_audit.so /usr/local/mysql57/lib/plugin/
#chmod +x /usr/local/mysql57/lib/plugin/server_audit.so
#chown mysql.mysql /usr/local/mysql57/lib/plugin/server_audit.so
在线安装插件
mysql> INSTALL PLUGIN server_audit SONAME 'server_audit.so';
Query OK, 0 rows affected (0.08 sec)
mysql> show plugins;
+----------------------------+--------+--------------------+-----------------+---------+
| Name | Status | Type | Library | License |
+----------------------------+--------+--------------------+-----------------+---------+
...
| SERVER_AUDIT | ACTIVE | AUDIT | server_audit.so | GPL |
+----------------------------+--------+--------------------+-----------------+---------+
mysql> show variables like '%server_audit%';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| server_audit_events | |
| server_audit_excl_users | |
| server_audit_file_path | server_audit.log |
| server_audit_file_rotate_now | OFF |
| server_audit_file_rotate_size | 1000000 |
| server_audit_file_rotations | 9 |
| server_audit_incl_users | |
| server_audit_loc_info | |
| server_audit_logging | OFF |
| server_audit_mode | 1 |
| server_audit_output_type | file |
| server_audit_query_log_limit | 1024 |
| server_audit_syslog_facility | LOG_USER |
| server_audit_syslog_ident | mysql-server_auditing |
| server_audit_syslog_info | |
| server_audit_syslog_priority | LOG_INFO |
+-------------------------------+-----------------------+
16 rows in set (0.00 sec)
参数说明
set global server_audit_logging=on;
set global server_audit_events='connect,query,table,query_ddl,query_dcl,query_dml_no_select';
set global server_audit_file_path='/var/log/server_audit.log';
set global server_audit_file_rotate_size=104857600;
[mysqld]下添加以下配置,使得配置永久生效:
server_audit_logging=on
server_audit_events=connect,query,table,query_ddl,query_dcl,query_dml_no_select
server_audit_file_path=/var/log/server_audit.log
server_audit_file_rotate_size=104857600
从另外一台远程连接到数据库上,执行一些操作
[root@mgr2 ~]# mysql -uauth -p123456 -h192.168.***.*** -P***
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 3
Server version: 5.7.26-log Source distribution
Copyright (c) 2000, 2019, 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.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database test;
Query OK, 1 row affected (0.03 sec)
mysql> use test;
Database changed
mysql> create table t1(id int);
Query OK, 0 rows affected (0.09 sec)
mysql> insert into t1 values (1);
Query OK, 1 row affected (0.02 sec)
mysql> delete from t1 where id=1;
Query OK, 1 row affected (0.03 sec)
mysql> exit
Bye
[root@mgr2 ~]# mysql -uauth -p12345 -h192.168.***.*** -P***
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'auth'@'192.168.***.*** (using password: YES)
查看相应日志升级对应的操作记录
20220723 21:28:34,mgr3,auth,192.168.*.*,3,0,CONNECT,,,0
20220723 21:28:34,mgr3,auth,192.168.*.*,3,9,QUERY,,'select @@version_comment limit 1',0
20220723 21:28:47,mgr3,auth,192.168.*.*,3,10,QUERY,,'show databases',0
20220723 21:28:57,mgr3,auth,192.168.*.*,3,11,QUERY,,'create database test',0
20220723 21:28:59,mgr3,auth,192.168.*.*,3,12,QUERY,,'SELECT DATABASE()',0
20220723 21:28:59,mgr3,auth,192.168.*.*,3,14,QUERY,test,'show databases',0
20220723 21:28:59,mgr3,auth,192.168.*.*,3,15,QUERY,test,'show tables',0
20220723 21:29:09,mgr3,auth,192.168.*.*,3,16,QUERY,test,'create table t1(id int)',0
20220723 21:29:17,mgr3,auth,192.168.*.*,3,17,QUERY,test,'insert into t1 values (1)',0
20220723 21:29:24,mgr3,auth,192.168.*.*,3,18,QUERY,test,'delete from t1 where id=1',0
20220723 21:29:48,mgr3,auth,192.168.*.*,3,0,DISCONNECT,test,,0
20220723 21:29:59,mgr3,auth,192.168.*.*,4,0,FAILED_CONNECT,,,1045
至此,我们完成审计插件的初步使用,从审计日志内容中我们可以看出,记录的格式还是很清晰详细的,每列内容都是需要的,根据日志很容易查到对应的操作。不过审计插件也是有优缺点的,优劣势整理如下:
server_audit 审计插件优势:
server_audit 审计插件劣势:
小编提醒:拿MariaDB的so去MySQL里install,这种方式很容易导致 audit plugin工作异常,不推荐这么做。强烈建议使用GreatSQL,自带 audit plugin。
Enjoy GreatSQL :)