将 Wireshark Dissector 中的 userdata 转换为十六进制

我正在编写 Wireshark Dissector lua 脚本。

如何将 userdata 转换为十六进制字符串?

我想要得到这样的输出 0102030405060708000a0b0c0d0e0f10

我可以使用 tostring 进行转换为十六进制字符串。

但它省略了长数据。 输出图像

如何将 userdata 转换为十六进制字符串而不省略长数据?

proto = Proto("Test", "我的测试协议")

function proto.dissector(buffer, pinfo, tree)
  print(tostring(buffer()))
  -- "userdata"
  -- print(type(buffer()))
end

tcp_table = DissectorTable.get("tcp.port")
tcp_table:add(1234, proto)

环境

  • Wireshark 3.2.1
点赞
用户2858170
用户2858170

我对 Wireshark 不是很熟悉,但是从快速查看手册中,我得知 buffer 是 一个Tvb ,而 buffer() 等效于 buffer:range() ,它返回一个 TvbRange

有一个 Tvb:__tostring 函数宣称:

把一个 Tvb 的字节转换为字符串。这主要用于调试,因为如果字符串太长,则会被截断。

打印 TvbRange 截断到 24 个字节。

我建议从你的 Tvb 中获取一个 ByteArray ,并使用以下语句获取该 ByteArray 的十六进制字符串:

11.8.1.14. bytearray:tohex([lowercase], [separator]) 将一个 ByteArray 的字节作为十六进制 ascii 码字符串获取出来,带有给定的分隔符。

那么你的代码可能会像这样:

proto = Proto("Test", "My Test Protocol")
function proto.dissector(buffer, pinfo, tree)
  print(buffer:bytes():tohex())
  -- or print(buffer():bytes():tohex())
  -- but I guess if you don't give a range in buffer()
  -- it will be pretty much the same as buffer.
end
2020-06-07 07:49:17