MySQL 抓包工具 mysql-sniffer

简介
MySQL Sniffer 是一个基于 MySQL 协议的抓包工具,实时抓取 MySQLServer 端的请求,并格式化输出。输出内容包访问括时间、访问用户、来源 IP、访问 Database、命令耗时、返回数据行数、执行语句等。有批量抓取多个端口,后台运行,日志分割等多种使用方式,操作便捷,输出友好。

下载:github 地址:https://github.com/Qihoo360/mysql-sniffer

安装:
1)安装依赖包:Centos:

yum install cmake
yum install  libpcap-devel
yum install  glib2-devel
yum install  libnet-devel

2)下载安装

cd mysql-sniffer
mkdir proj
cd proj
cmake ../
make

在bin目录下面生成了:
mysql-sniffer

参数说明:

root@db-yoon:~# 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. #端口,多个端口,分割,不能和-f一起使用
         -r port range, Don't use -r and -p at the same time  #端口范围,不能和-p一起使用
         -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 #截取制定长度的SQL
         -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 #指定数据包个数

输出格式:时间,访问用户,来源 IP,访问 Database,命令耗时,返回数据行数,执行语句。

1. 实时抓取某端口信息并打印到屏幕:-i 指定网卡名,-p 指定抓取的端口

[root@localhost ~]# mysql-sniffer -i eth0 -p 3306

2. 实时抓取某端口信息并打印到文件:-l 指定日志输出路径,日志文件将以 port.log 命名。

[root@localhost ~]# mysql-sniffer -i eth0 -p 3306 -l /tmp/

3. 实时抓取多个端口信息并打印到文件:-l 指定日志输出路径,-p指定需要抓取的端口列表逗号分割,日志文件将以各自 port.log 命名。

[root@localhost ~]# mysql-sniffer -i eth0 -p 3306,3307,3307 -l /tmp/

4. 实时抓取多个连续递增的端口并打印到文件:-l 指定日志输出路径,-r 指定端口范围,日志文件将以各自 port.log 命名

[root@localhost ~]# mysql-sniffer -i eth0 -r 3306-3309 -l /tmp/

5. 实时抓取多个连续递增的端口同时过滤某几个端口,并打印到文件:-l 指定日志输出路径,-r 指定端口范围, -w 指定过滤端口列表逗号分割,日志文件将以各自 port.log 命名。

[root@localhost ~]# mysql-sniffer -i eth0 -r 3306-3311 -w 3307,3309 -l /tmp/  

6. 抓取某个端口以 daemon 模式运行(-d),并打印到文件:-l 指定日志输出路径,-p 指定端口, -n 指定数据包个数,日志文件将以各自 port.log 命名。

[root@localhost tmp]# mysql-sniffer -i eth0 -p 3306 -l /tmp/ -d

7. 抓取某个端口并截取指定长度的 SQL:-p 指定端口, -t 指定SQL长度,将输出 SQL的前n个字符(适用于 SQL 过长的场景)。

[root@localhost ~]# mysql-sniffer -i eth0 -p 3306 -t 10

 

posted @ 2020-04-23 14:48  __Yoon  阅读(369)  评论(0编辑  收藏  举报