MySQL通信協議研究2(登陸認證)

今天看登陸認證,須要指出的是MySQL支持多種登陸方式,並且支持SSL,咱們只看最簡單的,基礎流程以下:算法


Client               Server
  |      handshake     |
  |<-------------------|
  |   authentication   |
  |------------------->|
  |     auth result    |
  |<-------------------|
  |                    |


一、handshakesql

格式:(Initial Handshake Packet)
數據庫

1              [0a] protocol version
string[NUL]    server version
4              connection id
string[8]      auth-plugin-data-part-1
1              [00] filler
2              capability flags (lower 2 bytes)
  if more data in the packet:
1              character set
2              status flags
2              capability flags (upper 2 bytes)
  if capabilities & CLIENT_PLUGIN_AUTH {
1              length of auth-plugin-data
  } else {
1              [00]
  }
string[10]     reserved (all [00])
  if capabilities & CLIENT_SECURE_CONNECTION {
string[$len]   auth-plugin-data-part-2 ($len=MAX(13, length of auth-plugin-data - 8))
  if capabilities & CLIENT_PLUGIN_AUTH {
    if version >= (5.5.7 and < 5.5.10) or (>= 5.6.0 and < 5.6.2) {
string[EOF]    auth-plugin name
    } elseif version >= 5.5.10 or >= 5.6.2 {
string[NUL]    auth-plugin name
    }
  }


抓包:c#

00000000  42 00 00 00 0A 35 2E 31  2E 34 39 2D 63 6F 6D 6D   B....5.1 .49-comm
00000010  75 6E 69 74 79 2D 6C 6F  67 00 14 00 00 00 49 69   unity-lo g.....Ii
00000020  57 55 27 5E 26 42 00 FF  F7 1C 02 00 00 00 00 00   WU'^&B.. ........
00000030  00 00 00 00 00 00 00 00  00 5A 7C 24 39 32 2E 2F   ........ .Z|$92./
00000040  43 40 5A 25 46 00                                  C@Z%F.


解析:dom

42 00 00                    //數據長度,3字節,0x42=66字節
00                          //序號,1字節
0A                          //協議,1字節,0x0A=10,表示第10版協議
35 2E 31 2E 34 39 2D 63 6F 6D 6D 75 6E 69 74 79 2D 6C 6F 67 00  //版本信息,字符串,以\0結尾,內容爲5.1.49-community-log
14 00 00 00                 //鏈接ID,4字節,0x14=20
5e 63 59 72 54 2c 7b 4a     //加密串的前半部分,定長8字節
00                          //固定填充0
FF F7                       //服務端屬性的低16位,2字節,枚舉參見網站
1C                          //字符集,1字節,0x1c=28=gbk_chinese_ci
02 00                       //服務端狀態,2字節,枚舉參見網站
00 00                       //服務端屬性的高16位,2字節
00                          //固定填充0
00 00 00 00 00 00 00 00 00 00   //固定填充0,10字節
5A 7C 24 39 32 2E 2F 43 40 5A 25 46 00      //加密串的後半部分,以\0結尾,加密串總共8+12=20字節


二、authenticationide

格式:(Handshake Response Packet)
大數據

4              capability flags, CLIENT_PROTOCOL_41 always set
4              max-packet size
1              character set
string[23]     reserved (all [0])
string[NUL]    username
  if capabilities & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA {
lenenc-int     length of auth-response
string[n]      auth-response
  } else if capabilities & CLIENT_SECURE_CONNECTION {
1              length of auth-response
string[n]      auth-response
  } else {
string[NUL]    auth-response
  }
  if capabilities & CLIENT_CONNECT_WITH_DB {
string[NUL]    database
  }
  if capabilities & CLIENT_PLUGIN_AUTH {
string[NUL]    auth plugin name
  }
  if capabilities & CLIENT_CONNECT_ATTRS {
lenenc-int     length of all key-values
lenenc-str     key
lenenc-str     value
   if-more data in 'length of all key-values', more keys and value pairs
  }


抓包:網站

00000000  40 00 00 01 8D A6 03 00  FF FF FF 00 21 00 00 00   @....... ....!...
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00   ........ ........
00000020  00 00 00 00 74 65 73 74  00 14 B4 2F BB 65 7A D4   ....test .../.ez.
00000030  55 BA 9E E4 4B 34 A3 2C  F6 58 92 7A A7 A2 76 6D   U...K4., .X.z..vm
00000040  6E 70 6E 00                                        npn.


解析:加密

40 00 00                    //數據長度,3字節,0x40=64字節
01                          //序號,1字節,同一個動做的全部請求與響應會遞增此值
8D A6 03 00                 //客戶端支持的屬性,4字節,枚舉參見網站
FF FF FF 00                 //最大數據包長度,4字節,0xffffff=16777215=約16MB
21                          //字符集,1字節,0x21=33=utf8
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   //固定填充0,23字節
74 65 73 74 00              //用戶名,\0結尾的字符串,內容爲test
14                          //密碼串長度,1字節,0x14=20字節
B4 2F BB 65 7A D4 55 BA 9E E4 4B 34 A3 2C F6 58 92 7A A7 A2   //密碼的加密串,20字節(算法見後)
76 6D 6E 70 6E 00           //初始數據庫,\0結尾的字符串,內容爲vmnpn


加密算法:spa

SHA1(password) XOR SHA1("20-bytes random data from server" <concat> SHA1(SHA1(password)))


三、auth ok

格式:(OK Packet)

1              [00] the OK header
lenenc-int     affected rows
lenenc-int     last-insert-id
  if capabilities & CLIENT_PROTOCOL_41 {
2              status_flags
2              warnings
  } elseif capabilities & CLIENT_TRANSACTIONS {
2              status_flags
  }
string[EOF]    info


抓包:

00000000  07 00 00 02 00 00 00 02  00 00 00                  ........ ........


解析:

07 00 00                    //數據長度,3字節,0x07=7字節
02                          //序號,1字節,在上一個包的基礎上又+1了
00                          //狀態標識,1字節,0x00表示成功
00                          //影響行數,變長數值
00                          //LastInsertId,變長數值
02 00                       //狀態,2字節,枚舉參見網站
00 00                       //消息


四、auth fail

格式:(ERR Packet)

1              [ff] the ERR header
2              error code
  if capabilities & CLIENT_PROTOCOL_41 {
string[1]      '#' the sql-state marker
string[5]      sql-state
  }
string[EOF]    error-message


抓包:

00000000  48 00 00 02 FF 15 04 23  32 38 30 30 30 41 63 63   .......# 28000Acc
00000010  65 73 73 20 64 65 6E 69  65 64 20 66 6F 72 20 75   ess deni ed for u
00000020  73 65 72 20 27 74 65 73  74 27 40 27 54 69 61 6E   ser 'tes t'@'Tian
00000030  59 75 2D 50 43 27 20 28  75 73 69 6E 67 20 70 61   Yu-PC' ( using pa
00000040  73 73 77 6F 72 64 3A 20  59 45 53 29               ssword:  YES)


解析:

48 00 00                    //數據長度,3字節,0x48=72字節
02                          //序號,1字節,在上一個包的基礎上又+1了
FF                          //狀態標識,1字節,0xff表示錯誤
15 04                       //錯誤碼,2字節,0x415=1045
23                          //固定1字節,#
32 38 30 30 30              //SQL狀態,固定5字節,內容=28000
41 63 63 65 ... 59 45 53 29 //出錯信息,字符串,內容=Access denied for user 'test'@'TianYu-PC' (using password: YES)


未完待續……

相關文章
相關標籤/搜索