最新消息:一个只会PHP的程序员不是好搬砖的

MySQL抓包审计工具:mysql-sniffer

开发工具 时光弧线 20浏览

简介

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 ../

输出如下:

MySQL抓包审计工具:mysql-sniffer
make

报错/usr/lib64/libpthread.so.0: error adding symbols: DSO missing from command line

MySQL抓包审计工具:mysql-sniffer

解决报错 根据github上的issue中有人遇到这个问题,解决如下:

修改文件mysql-sniffer/src/CMakeLists.txt 添加libpthread.so.0

MySQL抓包审计工具:mysql-sniffer
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抓包审计工具: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,命令耗时,返回数据行数,执行语句。

[root@localhost 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

转载请注明:53IT » MySQL抓包审计工具:mysql-sniffer