需求

监听通过网卡的保有mysql流量,举行辨析,可在不影响现成专门的学问情状下,举行入侵检查实验(IDS)或数额集成

斟酌要点

开场开采 用mysql-front访问数据库和mysql
的客户端访问时数据包格式分化,纠结很久,不知晓,mysql-front源码看了眼,delphi,不懂,弃

压缩分析

当链接mysql时,若启用-C参数表示,对于再三再四数据启用压缩,压缩格式为zlib

mysql的压缩函数为:

 1 // mysql-source/mysys/my_compress.c
 2 
 3 my_bool my_compress(uchar *packet, size_t *len, size_t *complen)
 4 {
 5   DBUG_ENTER("my_compress");
 6   if (*len < MIN_COMPRESS_LENGTH)
 7   {
 8     *complen=0;
 9     DBUG_PRINT("note",("Packet too short: Not compressed"));
10   }
11   else
12   {
13     uchar *compbuf=my_compress_alloc(packet,len,complen);
14     if (!compbuf)
15       DBUG_RETURN(*complen ? 0 : 1);
16     memcpy(packet,compbuf,*len);
17     my_free(compbuf);
18   }
19   DBUG_RETURN(0);
20 }
21 
22 
23 uchar *my_compress_alloc(const uchar *packet, size_t *len, size_t *complen)
24 {
25   uchar *compbuf;
26   uLongf tmp_complen;
27   int res;
28   *complen=  *len * 120 / 100 + 12;
29 
30   if (!(compbuf= (uchar *) my_malloc(key_memory_my_compress_alloc,
31                                      *complen, MYF(MY_WME))))
32     return 0;                    /* Not enough memory */
33 
34   tmp_complen= (uint) *complen;
35   res= compress((Bytef*) compbuf, &tmp_complen, (Bytef*) packet, (uLong) *len);
36   *complen=    tmp_complen;
37 
38   if (res != Z_OK)
39   {
40     my_free(compbuf);
41     return 0;
42   }
43 
44   if (*complen >= *len)
45   {
46     *complen= 0;
47     my_free(compbuf);
48     DBUG_PRINT("note",("Packet got longer on compression; Not compressed"));
49     return 0;
50   }
51   /* Store length of compressed packet in *len */
52   swap_variables(size_t, *len, *complen);
53   return compbuf;
54 }

 其中第35行调用了zlib中的compress()函数,可是该处仅对compress()举办了包装,并未协商剖判部分,大家一连往下看。

 

凡事项目搜索指标代码相比费力,可以先在头文件中寻觅关键消息,于是找到了上边包车型客车代码

// mysql-source/include/sql_state.h

{ ER_NET_UNCOMPRESS_ERROR                 ,"08S01", "" }

 

那是在mysql在条分缕析压缩的数额时壹旦出错的提醒消息和错误码,依次能够查找其引述,发掘了实在的多寡包压缩代码

 1 // mysql-source/sql/net_serv.cc
 2 
 3 static uchar *
 4 compress_packet(NET *net, const uchar *packet, size_t *length)
 5 {
 6   uchar *compr_packet;
 7   size_t compr_length;
 8   const uint header_length= NET_HEADER_SIZE + COMP_HEADER_SIZE;
 9 
10   compr_packet= (uchar *) my_malloc(key_memory_NET_compress_packet,
11                                     *length + header_length, MYF(MY_WME));
12 
13   if (compr_packet == NULL)
14     return NULL;
15 
16   memcpy(compr_packet + header_length, packet, *length);
17 
18   /* Compress the encapsulated packet. */
19   if (my_compress(compr_packet + header_length, length, &compr_length))
20   {
21     /*
22       If the length of the compressed packet is larger than the
23       original packet, the original packet is sent uncompressed.
24     */
25     compr_length= 0;
26   }
27 
28   /* Length of the compressed (original) packet. */
29   int3store(&compr_packet[NET_HEADER_SIZE], static_cast<uint>(compr_length));
30   /* Length of this packet. */
31   int3store(compr_packet, static_cast<uint>(*length));
32   /* Packet number. */
33   compr_packet[3]= (uchar) (net->compress_pkt_nr++);
34 
35   *length+= header_length;
36 
37   return compr_packet;
38 }

 从八-1玖行能够见到,压缩数量的组包进度,前边分别加了NET_HEADER_SIZE +
COMP_HEADER_SIZE
长的决定字段

 

探求该宏,发掘其定义如下

1 // mysql-source/include/mysql_com.h
2 
3   /* Constants when using compression */
4 #define NET_HEADER_SIZE 4        /* standard header size */
5 #define COMP_HEADER_SIZE 3        /* compression header extra size */

NET_HEADER_SIZE 字段中 长度字段存款和储蓄 数据部分 未解压时的长短

COMP_HEADER_SIZE 字段是用来积累  解压后的
数据的长度,我们得以依次申请内部存款和储蓄器,然后调用zlib对减少内容张开辨析就可以。

 

 借使不深入分析直接实行对wireshark抓到的多寡开始展览zlib分析的话,由于决定字段的存在会解压缩失利,在python中的报错如下

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect data check

 初始看到那个张冠李戴很看不惯也不想看zlib深入分析细节,才有了从mysql找原因的正文,现在能够记录zlib
压缩字符串的始发日常是\x78\x玖c,出现同样错误的能够看看是还是不是精确