MySQL 審計

簡介

????數(shù)據(jù)庫審計(簡稱DBAudit)能夠?qū)崟r記錄網(wǎng)絡(luò)上的數(shù)據(jù)庫活動,對數(shù)據(jù)庫操作進行細(xì)粒度審計的合規(guī)性管理,對數(shù)據(jù)庫遭受到的風(fēng)險行為進行告警,對攻擊行為進行阻斷。它通過對用戶訪問數(shù)據(jù)庫行為的記錄、分析和匯報,用來幫助用戶事后生成合規(guī)報告、事故追根溯源,同時加強內(nèi)外部數(shù)據(jù)庫網(wǎng)絡(luò)行為記錄,提高數(shù)據(jù)資產(chǎn)安全。

????MySQL Audit plugin主要有如下幾種:

????1. MySQL Enterprise Audit Plugin:This plugin is not open source and is only available with MySQL Enterprise, which has a significant cost attached to it. It is the most stable and robust.

????2. Percona Audit Log Plugin:Percona provides an open source auditing solution that installs with Percona Server 5.5.37+ and 5.6.17+. This plugin has quite a few output features as it outputs XML, JSON and to syslog. Percona’s implementation is the first to be a drop-in replacement for MySQL Enterprise Audit Plugin. As it has some internal hooks to the server to be feature-compatible with Oracle’s plugin, it is not available as a standalone for other versions of MySQL. This plugin is actively maintained by Percona.

????3. McAfee MySQL Audit Plugin:Around the longest and has been used widely. It is open source and robust, while not using the official auditing API. It isn’t updated as often as one may like. There hasn’t been any new features in some time. It was recently updated to support MySQL 5.7.

????4. MariaDB Audit Plugin:The only plugin that claims to support MySQL, Percona Server and MariaDB. It is open source and constantly upgraded with new versions of MariaDB. Versions starting at 1.2 are most stable, and it may be risky to use versions below that in your production environment. Versions below 1.2 may be unstable and I have seen it crash production servers. Older versions also log clear text passwords.

? ? 總結(jié)一下,官方的審計插件不開源,Percona有自己的審計插件,McAfee的插件基本不更新,MariaDB可以兼容MySQL。


安裝

? ? 以Percona Audit Plugin為例,Percona Server從5.5.37/5.6.17后支持audit。

? ? 安裝步驟:

? ? 1. 獲取plugin安裝位置:show global variables like 'plugin_dir';

? ? 2. 若該位置存在audit_log.so,則繼續(xù)安裝,若不存在,則去Percona官網(wǎng)下載對應(yīng)版本;

? ? 3. 安裝plugin:install plugin audit_log soname 'audit_log.so';

? ? 4. 查看audit parameter: show global variables like 'audit%',默認(rèn)參數(shù)如下:


參數(shù)說明

? audit_log_format:日志格式,支持XML、Json、CSV三種格式;

? audit_log_buffer_size:日志緩存,單位為Kb;

? audit_log_file:日志文件存儲位置,默認(rèn)在數(shù)據(jù)目錄;

? audit_log_flush:刷新日志緩存;

? audit_log_policy:記錄日志策略,支持all、login、query、none;

? audit_log_handler:日志輸出位置,支持file和syslog;

? audit_log_rotate_on_size:組合參數(shù),audit_log_handler為file時可使用,每個日志文件分割的大小,單位為Kb,最好為audit_log_buffer_size的整數(shù)倍;

? audit_log_rotations:組合參數(shù),保留的日志文件個數(shù);

? audit_log_strategy:組合參數(shù),audit_log_handler為file時可使用。日志刷新策略,支持ASYNCHRONOUS、PERFORMANCE、SEMISYNCHRONOUS、SYNCHRONOUS;

Json格式的日志示例如下:

{

? ? "audit_record": {

? ? ? ? "name": "Query",

? ? ? ? "record": "7656447233_2018-06-25T10:50:30",

? ? ? ? "timestamp": "2018-07-08T17:11:17 UTC",

? ? ? ? "command_class": "select",

? ? ? ? "connection_id": "48174660",

? ? ? ? "status": 0,

? ? ? ? "sqltext": "select * from test.test;",

? ? ? ? "user": "root[root] @? [localhost]",

? ? ? ? "host": "",

? ? ? ? "os_user": "",

? ? ? ? "ip": "",

? ? ? ? "db": "test"

? ? }

}


部署策略

????為了方便存儲與分析日志,審計日志采用Json格式。

? 修改MySQL配置文件,添加:

????[mysqld]

????### AUDIT ###

????audit_log_format=JSON

????audit_log_rotate_on_size=10485760000 --單個日志文件大小為1G

????audit_log_rotations=10? ? ? ? ? ? ? --保留10個日志文件

? 安裝plugin:

????install plugin audit_log soname 'audit_log.so';(audit_log_format為靜態(tài)參數(shù),install plugin后不能更改,所以事先寫配置文件)


性能損耗

????根據(jù)官方統(tǒng)計,開啟audit后,性能損耗大概在3%~5%(測試方式未給出,測試環(huán)境為Xeon、32G Ram、Samsung PRO SSD)。Tpcc 128線程測試結(jié)果為,響應(yīng)時間增加0.7ms,事務(wù)數(shù)減少7%。


局限性

? 審計文件不能加密;

? 對存儲過程和觸發(fā)器不做審計;

? 對load data infile等操作不做審計;


社區(qū)版MySQL審計

????社區(qū)版MySQL審計可采用MySQL的另一分支--MariaDB自帶的server_audit.so,MariaDB_5.5.37版本和MariaDB_10.0.10以后版本的audit插件支持MariaDB, MySQL、Percona Server使用。

????備注:MariaDB_5.x.x和MariaDB_10.x.x區(qū)別:

? MariaDB_5.x.x:兼容MySQL5.x.x的,接口幾乎一致,只限于社區(qū)版;

? MariaDB_10.x.x:10.x.x使用新技術(shù),接口會與mysql逐漸區(qū)別開來。目標(biāo)就是以后想MariaDB新接口過渡;


安裝

????插件安裝方式同上,配置文件的修改與之前不同,詳情如下:

? server_audit_events='CONNECT,QUERY,TABLE,QUERY_DDL,QUERY_DML,QUERY_DCL'

? server_audit_logging=on

? server_audit_file_path =/data/log/audit.log

? server_audit_file_rotate_size=200000000

? server_audit_file_rotations=200

? server_audit_file_rotate_now=ON

MariaDB的審計插件不支持熱修改,需要重啟MySQL服務(wù)


參數(shù)說明

? server_audit_output_type:指定日志輸出類型,可為SYSLOG或FILE

? server_audit_logging:啟動或關(guān)閉審計

? server_audit_events:指定記錄事件的類型,可以用逗號分隔的多個值(connect,query,table),如果開啟了查詢緩存(query cache),查詢直接從查詢緩存返回數(shù)據(jù),將沒有table記錄

? server_audit_file_path:如server_audit_output_type為FILE,使用該變量設(shè)置存儲日志的文件,可以指定目錄,默認(rèn)存放在數(shù)據(jù)目錄的server_audit.log文件中

? server_audit_file_rotate_size:限制日志文件的大小

? server_audit_file_rotations:指定日志文件的數(shù)量,如果為0日志將從不輪轉(zhuǎn)

? server_audit_file_rotate_now:強制日志文件輪轉(zhuǎn)

? server_audit_incl_users:指定哪些用戶的活動將記錄,connect將不受此變量影響,該變量比server_audit_excl_users優(yōu)先級高

? server_audit_syslog_facility:默認(rèn)為LOG_USER,指定facility

? server_audit_syslog_ident:設(shè)置ident,作為每個syslog記錄的一部分

? server_audit_syslog_info:指定的info字符串將添加到syslog記錄

? server_audit_syslog_priority:定義記錄日志的syslogd priority

? server_audit_excl_users:該列表的用戶行為將不記錄,connect將不受該設(shè)置影響

? server_audit_mode:標(biāo)識版本,用于開發(fā)測試


卸載

????插件卸載方式同上,需要注意的是,防止server_audit插件被卸載,需要在配置文件中添加如下選項,重啟生效:

????????[mysqld]

????????server_audit=FORCE_PLUS_PERMANENT

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容