簡介
MySQL Sniffer 是一個基於 MySQL 協議的抓包工具,實時抓取 MySQLServer 端的請求,並格式化輸出。輸出內容包訪問括時間、訪問用戶、來源 IP、訪問 Database、命令耗時、返回數據行數、執行語句等。有批量抓取多個端口,后台運行,日志分割等多種使用方式,操作便捷,輸出友好。
同時也適用抓取 Atlas 端的請求,Atlas 是奇虎開源的一款基於MySQL協議的數據中間層項目,項目地址:https://github.com/Qihoo360/Atlas
同類型工具還有vc-mysql-sniffer,以及 tshark 的 -e mysql.query 參數來解析 MySQL 協議。
使用
下載安裝就不多說了,github 地址:https://github.com/Qihoo360/mysql-sniffer (點擊文章末尾 “閱讀原文” 跳轉)。
建議在 centos6.2 及以上編譯安裝,並用 root 運行。
工具參數:
-
./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,命令耗時,返回數據行數,執行語句。
-
mysql-sniffer -i eth0 -p 3306
-
2017-02-23 14:47:45 testuser 10.xx.xx.xx NULL 0ms 1 select @@version_comment limit 1
-
2017-02-23 14:47:45 testuser 10.xx.xx.xx NULL 0ms 1 select USER()
-
2017-02-23 14:47:48 testuser 10.xx.xx.xx NULL 0ms 13 show databases
-
2017-02-23 14:47:51 testuser 10.xx.xx.xx NULL 0ms 1 SELECT DATABASE()
-
2017-02-23 14:47:51 testuser 10.xx.xx.xx mysql 0ms 0 use mysql
-
2017-02-23 14:47:53 testuser 10.xx.xx.xx mysql 0ms 29 show tables
-
2017-02-23 14:47:54 testuser 10.xx.xx.xx mysql 0ms 1 select 1
-
2017-02-23 14:48:01 testuser1 10.xx.xx.xx NULL 0ms 0 set autocommit=1
-
2017-02-23 14:48:01 testuser1 10.xx.xx.xx NULL 0ms 0 set autocommit=1
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
4. 實時抓取多個連續遞增的端口並打印到文件
-l 指定日志輸出路徑,-r 指定端口范圍,日志文件將以各自 port.log 命名。
-
//抓取端口包括(3306,3307,3308,3309,3310)
-
mysql-sniffer -i eth0 -r 3306-3310 -l /tmp
5. 實時抓取多個連續遞增的端口同時過濾某幾個端口,並打印到文件
-l 指定日志輸出路徑,-r 指定端口范圍, -w 指定過濾端口列表逗號分割,日志文件將以各自 port.log 命名。
-
//抓取端口包括(3306,3307,3310)
-
mysql-sniffer -i eth0 -r 3306-3310 -w 3308,3309 -l /tmp
6. 抓取某個端口以 daemon 模式運行,並打印到文件
-l 指定日志輸出路徑,-p 指定端口, -n 指定數據包個數,日志文件將以各自 port.log 命名。
-
mysql-sniffer -i eth0 -p 3306 -l /tmp -d
7. 抓取某個端口並截取指定長度的 SQL
-p 指定端口, -t 指定SQL長度,將輸出 SQL的前n個字符(適用於 SQL 過長的場景)。
-
mysql-sniffer -i eth0 -p 3306 -t 100
更多
MySQL Sniffer 更多使用方式以及 Atlas 等其他技術請關注我們后續的相關分享。