查询 维护 download nalu 执行 ext super tar
Mysql 版本: 5.6.24-72.2
由于审计的关键在于 DML 语句,而所有的 DML 语句都可以通过 binlog 记录。
不过遗憾的是目前 MySQL binlog 中只记录,产生这条记录的 connection id(随连接数自增,循环使用),这对之后的反查没有任何帮助。
因此考虑通过 init-connect,在每次连接的初始化阶段,记录下这个连接的用户,和 connection_id 信息。
在后期审计进行行为追踪时,根据 binlog 记录的行为及对应的 connection-id 结合 之前连接日志记录 进行分析,得出最后的结论
1. 设置 init-connect :
1.1 创建用于存放连接信息的表
- create database AuditDB
- default charset utf8;
- use AuditDB;
- CREATE TABLE accesslog(`id`int(11) primary key auto_increment, `LoginTime`timestamp, `LocalName`varchar(30), `MatchName`varchar(30));
1.2 保证所有的用户对此表有写权限
- use mysql;
- insert into db(Host, Db, User, Insert_priv) values(' % ', 'AuditDB', '', 'Y');
- flush privileges;
1.3 设置 init-connect
在 my.cnf 中的 [mysqld] 的 block 添加以下配置;
- init - connect = 'insert into AuditDB.accesslog(id, LoginTime, LocalName, MatchName) values(connection_id(), now(), user(), current_user());'
这里必须开启 binlog:
log-bin=xxx
1.4 重启数据库生效
- service mysql restart
2. 测试:
2.1 创建用户, 授权。 创建表
- CREATE USER[email protected] IDENTIFIED BY'123456';
- grant all on maildb. * to[email protected] % ';
- mysql - h localhost - umonitor - p123456 - D maildb create table test1(name varchar(32), id int(11));
2.2 查询每次登录的记录:
- mysql - h localhost - umonitor - p123456 select * from AuditDB.accesslog;
查找是谁创建的 test1 表,先在 binlog 中查找到创建表时候的 thread_id
- [[email protected] _01~]#mysqlbinlog / usr / local / mysql / binlog / binlog.000021 | grep--color'test1' - B 5#150917 12 : 41 : 43 server id 11 end_log_pos 939 CRC32 0x5691b7f5 Xid = 20 COMMIT
- /*!*/
- ;#at 939#150917 12 : 42 : 18 server id 11 end_log_pos 1066 CRC32 0xde7951a0 Query thread_id = 4(此处)exec_time = 0 error_code = 0 SET TIMESTAMP = 1442464938
- /*!*/
- ;
- create table test1(name varchar(32), id int(11))
根据 thread_id 在 AuditDB.accesslog 对应 id 字段: 就可以查出这是 [email protected] 干的了.
- mysql > select * from AuditDB.accesslog where id = 4; + ----+---------------------+-------------------+-------------------+|id | LoginTime | LocalName | MatchName | +----+---------------------+-------------------+-------------------+|4 | 2015 - 09 - 17 12 : 41 : 43 | [email protected] | [email protected] | +----+---------------------+-------------------+-------------------+
3. Q&A
Q:使用 init-connect 会影响服务器性能吗?
A:理论上,只会在用户每次连接时往数据库里插入一条记录,不会对数据库产生很大影响。除非连接频率非常高(当然,这个时候需要注意的就是如何进行连接复用和控制,而非是不是要用这种方法的问题了)
Q:access-log 表如何维护
A: 由于是一个 log 系统,推荐使用 archive 存储引擎,有利于数据厄压缩存放。如果数据库连接数量很大的话,建议一定时间做一次数据导出,然后清表。
Q:表有其他用途么?
A:有!access-log 表当然不只用于审计,当然也可以用于对于数据库连接的情况进行数据分析,例如每日连接数分布图等等,只有想不到没有做不到。
Q:会有遗漏的记录吗?
A:会的,init-connect 是不会在 super 用户登录时执行的。所以 access-log 里不会有数据库超级用户的记录,这也是为什么我们不主张多个超级用户,并且多人使用的原因。
参考: http://www.cnblogs.com/cenalulu/archive/2012/05/09/2491736.html
wiki 首页:https://github.com/mcafee/mysql-audit/wiki
二进制包下载:https://bintray.com/mcafee/mysql-audit-plugin/release 包含了 5.1,5.5,5.6 对应的二进制包
下载: https://bintray.com/artifact/download/mcafee/mysql-audit-plugin/1.0.8/audit-plugin-mysql-5.6-1.0.8-527-linux-x86_64.zip
1 安装配置插件
1.1 解压:
- unzip audit - plugin - mysql - 5.6 - 1.0.8 - 527 - linux - x86_64.zip
1.2 查看 mysql 插件目录:
- mysql > SHOW GLOBAL VARIABLES LIKE'plugin_dir'; + ---------------+------------------------------------+|Variable_name | Value | +---------------+------------------------------------+|plugin_dir | /usr/local / mysql / lib / mysql / plugin / |+---------------+------------------------------------+
1.3 复制下载的 so 文件至 plugin_dir,创建日志目录
- cd audit - plugin - mysql - 5.6 - 1.0.8 - 527 cp lib / libaudit_plugin.so / usr / local / mysql / lib / mysql / plugin / mkdir / usr / local / mysql / audit_log / chown mysql.mysql / usr / local / mysql / audit_log /
1.4 下载 offset 脚本,根据版本计算
offsets 具体可以参考 https://github.com/mcafee/mysql-audit/wiki/Troubleshooting
- wget https: //raw.github.com/mcafee/mysql-audit/master/offset-extract/offset-extract.sh
- #chmod + x offset - extract.sh#. / offset - extract.sh / usr / local / mysql / bin / mysqld
- //offsets for: /usr/local/mysql/bin/mysqld (5.6.24-72.2)
- {
- "5.6.24-72.2",
- "c518d31ce76de4d470fcf2712877712e",
- 7680,
- 7728,
- 4384,
- 5024,
- 88,
- 2720,
- 96,
- 0,
- 32,
- 104,
- 152,
- 7848
- },
1.5:配置 my.cnf,在 mysqld 块里面加入以下内容:
- plugin - load = AUDIT = libaudit_plugin.so audit_offsets = 7680,
- 7728,
- 4384,
- 5024,
- 88,
- 2720,
- 96,
- 0,
- 32,
- 104,
- 152,
- 7848 audit_json_file = ON audit_json_log_file = /usr/local / mysql / audit_log / mysql - audit.json audit_record_cmds = insert,
- delete,
- update,
- create,
- drop,
- revoke,
- alter,
- grant,
- set#针对这些语句来审计
1.6 重启 mysql 数据库
- service mysql restart
2.1 验证是否生效:
查看版本:
- mysql > SHOW GLOBAL STATUS LIKE'AUDIT_version'; + ---------------+-----------+|Variable_name | Value | +---------------+-----------+|Audit_version | 1.0.8 - 527 | +---------------+-----------+
查看是否开启:
- mysql > SHOW GLOBAL VARIABLES LIKE'audit_json_file'; + -----------------+-------+|Variable_name | Value | +-----------------+-------+|audit_json_file | ON | +-----------------+-------+
可以创建一个 test1 表,查看 / usr/local/mysql/audit_log/mysql-audit.json 文件中会有记录.
2.2 重要的参数说明:
1. audit_json_file #是否开启 audit 功能
2. audit_json_log_file #记录文件的路径和名称信息
3. audit_record_cmds #audit 记录的命令, 默认为记录所有命令可以设置为任意 dml、dcl、ddl 的组合
如:audit_record_cmds=select,insert,delete,update
还可以在线设置 set global audit_record_cmds=NULL(表示记录所有命令)
其他配置参数参考: https://github.com/mcafee/mysql-audit/wiki/Configuration
mysql 审计实现方法
来源: http://www.bubuko.com/infodetail-2163689.html