2

我现在正在编写 Wireshark Dissector lua 脚本。

如何转换userdata为十六进制字符串?
我想得到这样的输出0102030405060708000a0b0c0d0e0f10

我可以使用tostring.
但它省略了长数据。 输出图像

如何在userdata不省略长数据的情况下转换为十六进制字符串?

proto = Proto("Test", "My Test Protocol")

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
4

1 回答 1

3

我对 Wireshark 不是很熟悉,但是通过快速查看手册,我发现缓冲区是Tvb

并且buffer()相当于buffer:range()返回一个TvbRange

有一个 Tvb:__tostring 函数声称

将 Tvb 的字节转换为字符串。这主要用于调试目的,因为如果字符串太长,将被截断。

打印 TvbRange 被截断为 24 个字节。

我会尝试从您的 Tvb 获取 ByteArray 并使用获取该 ByteArray 的十六进制字符串

11.8.1.14。bytearray:tohex([lowercase], [separator])使用给定的分隔符获取 ByteArray 中字节的 Lua 字符串作为 hex-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-07T07:49:17.237 回答