MySQL抓包審計工具:mysql-sniffer
最近公司在搞資料審計安全,但是又不想開審計日誌消耗伺服器效能,就去找第三方工具。
下面就是本人在測試以及生產環境安裝使用mysql-sniffer的步驟以及解決安裝過程中的各種報錯。
github地址:https://github.com/Qihoo360/mysql-sniffer
簡介
MySQL Sniffer 是一個基於 MySQL 協議的抓包工具,實時抓取 MySQLServer 端或 Client 端請求,並格式化輸出。輸出內容包括訪問時間、訪問使用者、來源 IP、訪問 Database、命令耗時、返回資料行數、執行語句等。有批量抓取多個埠,後臺執行,日誌分割等多種使用方式,操作便捷,輸出友好。
同時也適用抓取 Atlas 端的請求,Atlas 是奇虎開源的一款基於MySQL協議的資料中間層專案,專案地址:https://github.com/Qihoo360/Atlas
同類型工具還有vc-mysql-sniffer,以及 tshark 的 -e mysql.query 引數來解析 MySQL 協議。
使用
建議在 centos6.2 及以上編譯安裝,並用 root 執行。
依賴
glib2-devel(2.28.8)、libpcap-devel(1.4.0)、libnet-devel(1.1.6)
安裝步驟
1 首先安裝依賴包,以及git
yum install cmake libpcap-devel glib2-devel libnet-devel git gcc-c++
git clone https://github.com/Qihoo360/mysql-sniffer
cd mysql-sniffer
mkdir proj
cd proj
cmake ../
輸出如下:
make
報錯/usr/lib64/libpthread.so.0: error adding symbols: DSO missing from command line
解決報錯 根據github上的issue中有人遇到這個問題,解決如下:
修改檔案mysql-sniffer/src/CMakeLists.txt 新增libpthread.so.0
TARGET_LINK_LIBRARIES(mysql-sniffer optimized libnidstcpreasm.a libpthread.so.0 libnet.a libpcap.a libglib-2.0.a libgthread-2.0.a librt.so) TARGET_LINK_LIBRARIES(mysql-sniffer debug libnidstcpreasm-dbg.a libpthread.so.0 libnet.a libpcap.a libglib-2.0.a libgthread-2.0.a librt.so)
重新make成功,在bin下生成命令mysql-sniffer
安裝完成。
使用
./mysql-sniffer -h
Usage ./mysql-sniffer [-d] -i eth0 -p 3306,3307,3308 -l /var/log/mysql-sniffer/ -e stderr
[-d] -i eth0 -r 3000-4000
-d daemon mode.
-s how often to split the log file(minute, eg. 1440). if less than 0, split log everyday
-i interface. Default to eth0
-p port, default to 3306. Multiple ports should be splited by ','. eg. 3306,3307
this option has no effect when -f is set.
-r port range, Don't use -r and -p at the same time
-l query log DIRECTORY. Make sure that the directory is accessible. Default to stdout.
-e error log FILENAME or 'stderr'. if set to /dev/null, runtime error will not be recorded
-f filename. use pcap file instead capturing the network interface
-w white list. dont capture the port. Multiple ports should be splited by ','.
-t truncation length. truncate long query if it's longer than specified length. Less than 0 means no truncation
-n keeping tcp stream count, if not set, default is 65536. if active tcp count is larger than the specified count, mysql-sniffer will remove the oldest one
示例
1. 實時抓取某埠資訊並列印到螢幕
輸出格式為:時間,訪問使用者,來源 IP,訪問 Database,命令耗時,返回資料行數,執行語句。
[[email protected] bin]# ./mysql-sniffer -i ens160 -p 3306
2018-12-11 11:45:55 dba 10.238.133.45 NULL 0ms 0 SET NAMES utf8
2018-12-11 11:46:00 dba 10.238.133.45 NULL 0ms 0 SET NAMES utf8
2018-12-11 11:46:00 dba 10.238.133.45 NULL 0ms 2 SHOW VARIABLES LIKE 'lower_case_%'
2018-12-11 11:46:00 dba 10.238.133.45 NULL 0ms 1 SHOW VARIABLES LIKE 'profiling'
2018-12-11 11:46:00 dba 10.238.133.45 NULL 0ms 4 SHOW DATABASES
2018-12-11 11:46:01 dba 10.238.133.45 NULL 0ms 0 SET NAMES utf8
2018-12-11 11:46:01 dba 10.238.133.45 mysql 0ms 0 use mysql
2018-12-11 11:46:01 dba 10.238.133.45 mysql 0ms 1 SELECT @@character_set_database, @@collation_database
2. 實時抓取某埠資訊並列印到檔案
-l 指定日誌輸出路徑,日誌檔案將以 port.log 命名。
mysql-sniffer -i eth0 -p 3306 -l /tmp
3. 實時抓取多個埠資訊並列印到檔案
-l 指定日誌輸出路徑,-p 指定需要抓取的埠列表逗號分割。日誌檔案將以各自 port.log 命名。
mysql-sniffer -i eth0 -p 3306,3307,3310 -l /tmp