利用wireshark进行KCP抓包
  W00av3sJEpAL 2023年11月02日 87 0

 

 

Install wireshark 3.6.12

 

https://2.na.dl.wireshark.org/win64/all-versions/Wireshark-win64-3.6.12.exe

 

 

Download kcp_dissector.lua

 

---@ KCP Protocol dissector plugin

 

do

 

  local bit32 = bit32 or bit

 

  local NAME = "KCP"

  local PORT = 20001

 

  local KCP = Proto(NAME, "KCP Protocol")

 

  -- KCP Protocol Fields.

  local conv  = ProtoField.uint32(NAME .. ".conv", "Conv", base.DEC)

  local cmd   = ProtoField.uint8(NAME .. ".cmd", "Cmd", base.DEC)

  local frg   = ProtoField.uint8(NAME .. ".frg", "Frg", base.DEC)

  local wnd   = ProtoField.uint16(NAME .. ".wnd", "Wnd", base.DEC)

 

  local ts    = ProtoField.uint32(NAME .. ".ts", "ts", base.DEC)

  local sn    = ProtoField.uint32(NAME .. ".sn", "sn", base.DEC)

  local una   = ProtoField.uint32(NAME .. ".una", "una", base.DEC)

  local len   = ProtoField.uint32(NAME .. ".len", "len", base.DEC)

  local data  = ProtoField.string(NAME .. ".data", "data", base.UNICODE)

 

--[[

 

  0               4   5   6       8 (BYTE)

  +---------------+---+---+-------+

  |     conv      |cmd|frg|  wnd  |

  +---------------+---+---+-------+   8

  |     ts        |     sn        |

  +---------------+---------------+  16

  |     una       |     len       |

  +---------------+---------------+  24

  |                               |

  |        DATA (optional)        |

  |                               |

  +-------------------------------+

 

--]]

 

  KCP.fields = {

    conv, cmd, frg, wnd,

    ts,        sn,

    una,       len,

           data

  }

 

  local function CMD_TO_STRING(CMD)

    if CMD:le_uint() == 81 then

      return "CMD_PUSH(81)"

    elseif CMD:le_uint() == 82 then

      return "CMD_ACK(82)"

    elseif CMD:le_uint() == 83 then

      return "CMD_WASK(83)"

    elseif CMD:le_uint() == 84 then

      return "CMD_WINS(84)"

    end

    return CMD:le_uint()

  end

 

  local function WND_TO_STRING(WND)

    return "WND_RCV_SIZE(" .. WND:le_uint() .. ")"

  end

 

  local function FRG_TO_STRING(FRG)

    return FRG:uint() == 1 and "YES(1)" or "FALSE(0)"

  end

 

 

  local function LEN_TO_STRING(LEN)

    return LEN:le_uint()

  end

 

  local segment = 0

 

  -- KCP dissect packet

  function KCP.dissector (Buffer, Menu, T)

 

    -- Creating a protocol tree.

    local Tree = T:add(KCP, Buffer())

 

    -- Registered Protocol Name

    Menu.cols.protocol = KCP.name

 

    -- Calculate the data offset value

    local offset  = 0

 

 

    local CONV =  Buffer(offset, 4)

    Tree:add_le(conv, CONV)

    Tree:append_text(", conv: " .. CONV:le_uint())

    offset = offset + 4

 

    local CMD =  Buffer(offset, 1)

    Tree:add_le(cmd, CMD)

    Tree:append_text(", cmd: " .. CMD_TO_STRING(CMD))

    offset = offset + 1

 

    local FRG =  Buffer(offset, 1)

    Tree:add_le(frg, FRG)

    Tree:append_text(", frg: " .. FRG_TO_STRING(FRG))

    offset = offset + 1

 

    local WND =  Buffer(offset, 2)

    Tree:add_le(wnd, WND)

    Tree:append_text(", wnd: " .. WND_TO_STRING(WND))

    offset = offset + 2

 

    local TS =  Buffer(offset, 4)

    Tree:add_le(ts, TS)

    Tree:append_text(", ts: " .. TS:le_uint())

    offset = offset + 4

 

    local SN =  Buffer(offset, 4)

    Tree:add_le(sn, SN)

    Tree:append_text(", sn: " .. SN:le_uint())

    offset = offset + 4

 

    local UNA =  Buffer(offset, 4)

    Tree:add_le(una, UNA)

    Tree:append_text(", una: " .. UNA:le_uint())

    offset = offset + 4

 

    local LEN =  Buffer(offset, 4)

    Tree:add_le(len, LEN)

    Tree:append_text(", len: " .. LEN_TO_STRING(LEN))

    offset = offset + 4

 

    local DATA =  Buffer(offset, Buffer:len() - offset)

    Tree:add(data, DATA:string(ENC_UTF_8))

    -- Tree:append_text(", data: " .. DATA:string(ENC_UTF_8))

    offset = offset + (Buffer:len() - offset)

 

    if CMD:le_uint() == 81 then

      local info = "CMD_PUSH, SN(" .. SN:le_uint() .. ")"

      if UNA:le_uint() > 0 then

        info = info .. "WAIT_SN(" .. UNA:le_uint() .. ")"

      end

      Menu.cols.info = info

    elseif CMD:le_uint() == 82 then

      Menu.cols.info = "CMD_ACK, SN(" .. SN:le_uint() .. "), NEXT_SN(" .. UNA:le_uint() .. ")"

    elseif CMD:le_uint() == 83 then

      -- TODO

    elseif CMD:le_uint() == 84 then

      -- TODO

    end

 

  end

 

  DissectorTable.get("udp.port"):add(PORT, KCP)

end

 

 

修改lua脚本中kcp使用的udp端口,如下

 

local PORT = 20001

 

把修改后的lua脚本放置到个人lua插件目录中

 

 

利用wireshark进行KCP抓包_Wireshark

 

载入lua插件

 

 

利用wireshark进行KCP抓包_lua_02

开始抓包

 

 

利用wireshark进行KCP抓包_抓包_03

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: 用户身份切换 下一篇: 组管理
  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  jLXKB6vexBrB   2023年11月13日   27   0   0 SSL抓包钥匙串