Lua解析HTTP协议,找不到字符串结尾

我正在尝试使用lua在wireshark中分离HTTP协议中的字符串数据,但我没有成功找到字符串的结尾,这是我目前的代码:

HTTP_protocol = Proto("ourHTTP", "HTTPProtocol")

first =ProtoField.string("HTTP_protocol.first", "first", base.ASCII)
second =ProtoField.string("HTTP_protocol.second", "second", base.ASCII)
HTTP_protocol.fields = {first}

function HTTP_protocol.dissector(buffer, pinfo, tree)
 length = buffer:len()

 if length ==0 then return end
pinfo.cols.protocol = HTTP_protocol.name
local subtree = tree:add(HTTP_protocol, buffer(), "HTTPProtocol data ")
local string_length

for i = 0, length - 1, 1 do
  if (buffer(i,1):uint() == '\r') then
    string_length = i - 0
    break
  end
end
subtree:add(first, buffer(0,string_length))

end
porttable = DissectorTable.get("tcp.port")
porttable:add(80, HTTP_protocol)

我已经尝试搜索'\r'、'\0'和'\n',但无论如何仍会将所有字符串输入为一个字符串。我做错了什么吗?

点赞
用户8574934
用户8574934

你可以使用 0x0D。它对应的 ASCII 码是 \r。所以代码应该是:

if (buffer(i,1):uint() == 0x0D) then

在 Wireshark 中的表现:

Wireshark 中换行的展示

2019-03-09 11:08:44