暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL安全审计

数据库BOSS 2021-04-16
2315

  • 1.审计作用

  • 2.审计工具

  • 3.工具的选择

  • 4.AUDIT Plugin for MySQL*

  • 5.init_connect

1.审计作用

主要将用户对数据库的各类操作行为记录审计日志,以便日后进行跟踪、查询、分析,以实现对用户操作的监控和审计。

2.审计工具

  • MySQL 5.5企业版增加审计功能--需要付费。
  • init-connect+binlog
  • AUDIT Plugin for MySQL*(开源的)
  • ELK

3.工具的选择

利用mysql的general log提供了详细的sql执行记录,但是开启它有以下几个缺点:

  • 无论sql有无语法错误,只要执行了就会记录,导致记录大量无用信息,后期的筛选有难度。
  • sql并发量很大时,log的记录会对io造成一定的印象,是数据库效率降低。
  • 日志文件很容易快速膨胀,不妥善处理会对磁盘空间造成一定影响。

今天我们用一个不错的开源工具AUDIT Plugin for MySQL*来演示审计。

4.AUDIT Plugin for MySQL*

开源访问地址

https://github.com/mcafee/mysql-audit

数据库版本支持

v1.1.7: Updating with the latest offsets for
@wbarrettmcafee wbarrettmcafee released this on 10 Dec 2020
-Added support for Percona 5.7.30-33, 5.7.29-32, 5.7.28-31, 5.7.27-30, 5.7.20-19, 5.7.21-20, 5.7.26-29.
-Added support for MySQL 8.0.22, 8.0.21, 8.0.20, 8.0.19, 8.0.18, 8.0.17, 8.0.16, 8.0.15, 8.0.14, 5.6.49, 5.7.31, 5.6.44, 5.6.45, 5.6.46, 5.6.47, 5.6.48, 5.7.30, 5.7.29, 5.7.26, 5.7.27,5.7.28, 5.5.60, 5.5.61, 5.6.39, 5.6.44, 5.6.40, 5.6.41, 5.7.22 and 5.7.23.
-Added support for MariaDB 10.1.41, 10.1.43, 10.2.27, 10.2.29, 5.5.66, 5.5.65, 10.1.47, 10.2.34, 10.2.25, 10.2.26, 10.2.23, 10.2.30, 10.2.33, 10.1.46, 10.2.31, 10.1.44, 10.1.45, 10.2.31, 10.2.32, 5.5.67, 5.5.68, 5.5.60, 5.5.61, 10.0.35, 10.0.36, 10.1.32 - 10.1.36, 10.2.14 - 10.2.18

使用说明

https://github.com/mcafee/mysql-audit/wiki
https://bintray.com/package/files/mcafee/mysql-audit-plugin/dev-snapshot

安装

-- 下载好二进制文件,上传到服务器
[root@jssdb01 lib]# pwd
/jssdb/soft/audit-plugin-mysql-5.7-1.1.7-821/lib
[root@jssdb01 lib]# ls
libaudit_plugin.so

-- To see the configured plugin dir, login to MySQL and issue the following command:
mysql> show global variables like 'plugin_dir';
+---------------+--------------------------------+
| Variable_name | Value                          |
+---------------+--------------------------------+
| plugin_dir    | /jssdb/mysql/mysql/lib/plugin/ |
+---------------+--------------------------------+

--将插件放入指定目录,赋予权限
[root@jssdb01 plugin]# chown -R mysql:mysql /jssdb/mysql/    -- mysql的安装路径
[root@jssdb01 plugin]# chmod 755 libaudit_plugin.so 

/** 加载so文件(官方提供两种)
--Add to the MySQL option file (my.cnf) at the [mysqld] section the option:
plugin-load=AUDIT=libaudit_plugin.so 
--You will need to issue the following sql command to install the plugin:
INSTALL PLUGIN AUDIT SONAME 'libaudit_plugin.so';
mysql> INSTALL PLUGIN AUDIT SONAME 'libaudit_plugin.so';
ERROR 1123 (HY000): Can't initialize function 'AUDIT'; Plugin initialization function failed.
--按照官方手册配置
[root@jssdb01 utils]# chmod +x offset-extract.sh 
[root@jssdb01 utils]# ./offset-extract.sh /jssdb/mysql/mysql/bin/mysqld
//offsets for: /jssdb/mysql/mysql/bin/mysqld (5.7.30)
{"5.7.30","e13cbbb2ed17be83e8b3a1a541d706ba", 7824, 7872, 3632, 4792, 456, 360, 0, 32, 64, 160, 536, 7988, 4360, 3648, 3656, 3660, 6072, 2072, 8, 7056, 7096, 7080, 13472, 148, 672},
--我在my.cnf中添加了配置,还有解决不了的:See Troubleshooting
plugin-load=AUDIT=libaudit_plugin.so
audit_offsets=7824, 7872, 3632, 4792, 456, 360, 0, 32, 64, 160, 536, 7988, 4360, 3648, 3656, 3660, 6072, 2072, 8, 7056, 7096, 7080, 13472, 148, 672
--查看版本
mysql> SHOW GLOBAL STATUS LIKE 'AUDIT_version';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| Audit_version | 1.1.7-821 |
+---------------+-----------+
mysql> flush privileges;
**/


-- 开启audit,修改my.cnf
audit_json_file=on
audit_json_log_file=/jssdb/mysql/log/3306/audit_log/mysql-audit.json
audit_record_cmds='insert,delete,update,create,drop,alter,grant,revoke,truncate'
-- 重启服务,查看参数
mysql> show variables like '
%audit_json%';
+---------------------------------+----------------------------------------------------------------+
| Variable_name                   | Value                                                          |
+---------------------------------+----------------------------------------------------------------+
| audit_json_file                 | ON                                                             |
| audit_json_file_bufsize         | 1                                                              |
| audit_json_file_flush           | OFF                                                            |
| audit_json_file_retry           | 60                                                             |
| audit_json_file_sync            | 0                                                              |
| audit_json_log_file             | /jssdb/mysql/log/3306/audit_log/mysql-audit.json               |
| audit_json_socket               | OFF                                                            |
| audit_json_socket_name          | /var/run/db-audit/mysql.audit__jssdb_mysql_data_3306_data_3306 |
| audit_json_socket_retry         | 10                                                             |
| audit_json_socket_write_timeout | 1000                                                           |
+---------------------------------+----------------------------------------------------------------+
10 rows in set (0.00 sec)
mysql> show variables like '
%audit_record%';
+-------------------+--------------------------------------------------------------+
| Variable_name     | Value                                                        |
+-------------------+--------------------------------------------------------------+
| audit_record_cmds | insert,delete,update,create,drop,alter,grant,revoke,truncate |
| audit_record_objs |                                                              |
+-------------------+--------------------------------------------------------------+


--进行表的操作
mysql> show tables;
+--------------------+
| Tables_in_jssdb001 |
+--------------------+
| jss_t1             |
| jss_t2             |
+--------------------+
2 rows in set (0.00 sec)

mysql> truncate table jss_t2;
Query OK, 0 rows affected (0.11 sec)

mysql> drop table jss_t2;
Query OK, 0 rows affected (0.01 sec)
--查看日志
[root@jssdb01 audit_log]# tail -100f mysql-audit.json 
{"msg-type":"header","date":"1612066844319","audit-version":"1.1.7-821","audit-protocol-version":"1.0","hostname":"jssdb01","mysql-version":"5.7.30","mysql-program":"/jssdb/mysql/mysql/bin/mysqld","mysql-socket":"/jssdb/mysql/data/3306/mysql.sock","mysql-port":"3306","server_pid":"8236"}
{"msg-type":"activity","date":"1612067317336","thread-id":"2","query-id":"15","user":"root","priv_user":"root","ip":"","host":"localhost","connect_attrs":{"_os":"linux-glibc2.12","_client_name":"libmysql","_pid":"8266","_client_version":"5.7.30","_platform":"x86_64","program_name":"mysql"},"pid":"8266","os_user":"root","appname":"mysql","status":"0","cmd":"truncate","objects":[{"db":"jssdb001","name":"jss_t2","obj_type":"TABLE"}],"query":"truncate table jss_t2"}

5.init_connect

作用

当一个连接进来做一些操作,记录当前连接的ip来源和用户等信息到一个新表里,当做登陆日志信息。(init_connect的设置对来自超级账户的连接不生效)

init-connect只会在连接时执行,不会对数据库产生大的性能影响。

举例

-- 创建审计数据库
CREATE DATABASE audit_db;

-- 创建审计表
CREATE TABLE audit_db.t_audit(
  id INT NOT NULL AUTO_INCREMENT,
  thread_id INT NOT NULL,
  login_time TIMESTAMP,
  localname VARCHAR(50DEFAULT NULL,
  matchname VARCHAR(50DEFAULT NULL
  PRIMARY KEY (id)
COMMENT '审计用户登录信息';

-- 授权所有的用户拥有对审计表的插入权限
INSERT INTO mysql.db(HOST,db,USER,select_priv,Insert_priv) VALUES('%','audit_db','','Y','Y');
FLUSH PRIVILEGES;

-- 在my.cnf中修改
init-connect='insert into audit_db.t_audit(thread_id,login_time,localname,matchname) values(connection_id(),now(),user(),current_user());'

-- 重启服务,使用jss003登录查看
mysql> select * from audit_db.t_audit limit 2;
+----+-----------+---------------------+---------------------+-----------+
| id | thread_id | login_time          | localname           | matchname |
+----+-----------+---------------------+---------------------+-----------+
|  1 |         5 | 2021-01-31 14:24:13 | jss003@localhost    | jss003@%  |
|  2 |         6 | 2021-01-31 14:26:17 | jss003@192.168.0.31 | jss003@%  |
+----+-----------+---------------------+---------------------+-----------+
-- 根据时间段,结合binlog日志可以查询操作记录


文章转载自数据库BOSS,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论