[英]Extract mysql query from tcpdump
我正在使用tcpdump
从端口 3306 捕获 TCP 数据包,该数据包转发到 MySQL 服务器
sudo tcpdump -X -i ens5 -s 0 -tttt dst port 3306
并执行 SQL select * from user_trading_volume limit 1
from MySQL 客户端
捕获的结果如下
2020-05-27 07:46:44.330084 IP ip-10-0-1-33.ap-northeast-2.compute.internal.59750 > ip-10-30-1-179.ap-northeast-2.compute.internal.mysql: Flags [P.], seq 1945:2020, ack 16715, win 512, options [nop,nop,TS val 3790143765 ecr 4258512397], length 75
0x0000: 4500 007f 54fb 4000 4006 ce8c 0a00 0121 E...T.@.@......!
0x0010: 0a1e 01b3 e966 0cea 76a0 9245 c975 2466 .....f..v..E.u$f
0x0020: 8018 0200 1763 0000 0101 080a e1e9 0115 .....c..........
0x0030: fdd3 be0d 1703 0300 46f5 525d 17c9 20ac ........F.R]....
0x0040: 62e6 fcdc ba82 11fc 91c2 c187 7ca8 a542 b...........|..B
0x0050: 6ed8 a1fa b1d8 01bd 1240 61d9 686e 183d n........@a.hn.=
0x0060: f2fc 9b9a a62d c212 8d4d e1c6 e67a 4bdc .....-...M...zK.
0x0070: ea2e 75dc 68cf 5c45 1721 2ced c511 ca ..u.h.\E.!,....
2020-05-27 07:46:44.331029 IP ip-10-0-1-33.ap-northeast-2.compute.internal.59750 > ip-10-30-1-179.ap-northeast-2.compute.internal.mysql: Flags [.], ack 17677, win 505, options [nop,nop,TS val 3790143766 ecr 4258513778], length 0
0x0000: 4500 0034 54fc 4000 4006 ced6 0a00 0121 E..4T.@.@......!
0x0010: 0a1e 01b3 e966 0cea 76a0 9290 c975 2828 .....f..v....u((
0x0020: 8010 01f9 1718 0000 0101 080a e1e9 0116 ................
0x0030: fdd3 c372
但捕获的数据包不可读(这意味着不是 ASCII)
我正在使用 AWS 极光(mysql 5.7)
有谁知道这个包是什么意思?
PS。 I tried it in my local environment too and could retrieve matching SQL from packet as below (run mysql within docker container and executed query through mysql workbench)
16:59:46.628631 IP (tos 0x0, ttl 64, id 59587, offset 0, flags [DF], proto TCP (6), length 98)
view-localhost.52652 > view-localhost.3318: Flags [P.], cksum 0xfe56 (incorrect -> 0x1538), seq 61:107, ack 899, win 512, options [nop,nop,TS val 632447157 ecr 632447154], length 46
E..b..@.@.S...............@....=.....V.....
%.`.%.`.*....select * from user_trading_volume limit 1
您可以使用 tcpdump 捕获数据,然后使用选项-w
将其重定向到文件。 然后使用wireshark加载它。
https://www.wireshark.org/docs/wsug_html_chunked/AppToolstcpdump.html
Looking at the first byte, this looks like two raw IP packets (45 => IP version 4 , typical 20byte header ( 5 * 4 bytes). Wikipedia has more info on IP headers .
因此,我们应该能够将其转换回 pcap。 我们可以使用text2pcap将此文本转储转换为数据包捕获,这是 Wireshark 附带的命令行实用程序。
将给定的文本作为文件temp
,我们可以将其转换为 pcap
$ cat temp | grep -v 2020 | cut -c3-49 | sed 's/ \(\w\w\)/ \1 /g' \
| text2pcap -l 101 - temp.pcap
Input from: Standard input
Output to: temp.pcap
Output format: pcap
Wrote packet of 127 bytes.
Wrote packet of 52 bytes.
Read 2 potential packets, wrote 2 packets (235 bytes).
在这里,我们清理输入,以便 text2pcap 不会失败:
grev -v 2020
:删除 2020... 信息行cut -c3-49
:删除前面的 0x 和 ASCII 表示sed 's/ \(\w\w\)/ \1 /g'
:将 hexdump 从 2 字节然后空间转换为 1 字节然后空间( 09ab
=> 09 ab
)text2pcap -l 101 - temp.cap
:从标准输入读取并写入 temp.pcap 作为原始 IP 数据包(见下文)您现在可以在Wireshark中查看此捕获以查看字段是什么。
回到初始字节,当通常像以太网这样的链路层启动数据包时,该字节启动 IP 层。 这意味着我们不能使用典型的链路层 1(以太网)。 原始 IP 的链接层为 101,因此我们需要将 text2pcap 指定为-l 101
。 -
是标准输入,然后我们将文件写为 temp.pcap。
在 Wireshark 中加载时,数据包 1 的有效负载为 75 个字节,并且不是 ASCII。 您可能希望使用MySQL 协议参考手动解码这些字节。 因为根据文档,
MySQL 协议用于 MySQL 客户端和 MySQL 服务器之间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.