如何找出数据包的HTTP头长度?

时间:2011-04-18 15:43:04

标签: http wireshark netmon

我知道如何手动完成(通过查看十六进制转储)。我怎样才能自动获得相同的信息?我必须使用API​​吗?我有wireshark和Microsoft网络监视器。

4 个答案:

答案 0 :(得分:8)

这可以通过Lua dissector简单地实现,该{{3}}将HTTP头字段添加到数据包树,允许您对其进行过滤,如此屏幕截图所示:

enter image description here

将此Lua脚本复制到您的插件目录(例如${WIRESHARK_HOME}/plugins/1.4.6/http_extra.lua),然后重新启动Wireshark(如果已经运行)。

do
        local http_wrapper_proto = Proto("http_extra", "Extra analysis of the HTTP protocol");
        http_wrapper_proto.fields.hdr_len = ProtoField.uint32("http.hdr_len", "Header length (bytes)")

        -- HTTP frames that contain a header usually include the HTTP
        -- request method or HTTP response code, so declare those here
        -- so we can check for them later in the dissector.
        local f_req_meth    = Field.new("http.request.method")
        local f_resp_code   = Field.new("http.response.code")

        local original_http_dissector
        function http_wrapper_proto.dissector(tvbuffer, pinfo, treeitem)
                -- We've replaced the original http dissector in the dissector table,
                -- but we still want the original to run, especially because we need 
                -- to read its data. Let's wrap the call in a pcall in order to catch
                -- any unhandled exceptions. We'll ignore those errors.
                pcall(
                    function()
                        original_http_dissector:call(tvbuffer, pinfo, treeitem)
                    end
                )

                -- if the request method or response code is present,
                -- the header must be in this frame
                if f_req_meth() or f_resp_code() then

                        -- find the position of the header terminator (two new lines),
                        -- which indicates the length of the HTTP header, and then add
                        -- the field to the tree (allowing us to filter for it)
                        local hdr_str = tvbuffer():string()
                        local hdr_len = string.find(hdr_str, "\r\n\r\n") or string.find(hdr_str, "\n\n\n\n")
                        if hdr_len ~= nil then
                            treeitem:add(http_wrapper_proto.fields.hdr_len, hdr_len):set_generated()
                        end
                end
        end

        local tcp_dissector_table = DissectorTable.get("tcp.port")
        original_http_dissector = tcp_dissector_table:get_dissector(80) -- save the original dissector so we can still get to it
        tcp_dissector_table:add(80, http_wrapper_proto)                 -- and take its place in the dissector table
end

答案 1 :(得分:2)

不幸的是,尽管您可以创建自定义列,但HTTP协议解码器当前不会生成该列中所需的数据。当然,可能还有其他我不熟悉的工具,今天可以做到这一点,但就Wireshark而言,您必须添加该功能。

创建Wireshark插件有一些很好的资源,例如:

http://simeonpilgrim.com/blog/2008/04/29/how-to-build-a-wireshark-plug-in/

http://www.wireshark.org/docs/wsdg_html_chunked/ChDissectAdd.html

http://www.codeproject.com/KB/IP/custom_dissector.aspx

这是一段视频,描述了如何将协议解码器公开的字段添加为自定义列:

http://www.youtube.com/watch?v=XpUNXDkfkQg

问题是,您不想重新实现HTTP协议解码器。

我要做的是找到内置HTTP解码器的源代码,并查看添加新字段,例如http.header_length,就像现有的http.content_length一样:

img

我没有查看代码,但我猜这是一个非常容易添加的东西。如果您向Wireshark团队提交补丁,他们可能还会在下一个版本中包含您的新字段。

答案 2 :(得分:2)

user568493发布的代码对我来说根本不起作用,所以我把它改成了一个后期解析器,而且它还没有正确计算字节数。它还计算IP和以太网字节。

这是我的版本,适用于1.8.2:

local http_wrapper_proto = Proto("http_extra", "Extra analysis of the HTTP protocol");
http_wrapper_proto.fields.hdr_len = ProtoField.uint32("http.hdr_len", "HTTP Header length (bytes)")

-- HTTP frames that contain a header usually include the HTTP
-- request method or HTTP response code, so declare those here
-- so we can check for them later in the dissector.
local f_req_meth    = Field.new("http.request.method")
local f_resp_code   = Field.new("http.response.code")

local original_http_dissector
function http_wrapper_proto.dissector(tvbuffer, pinfo, treeitem)
        -- if the request method or response code is present,
        -- the header must be in this frame
        if f_req_meth() or f_resp_code() then
                local start_offset = 0
                local end_offset = 0
                -- find the position of the header terminator (two new lines),
                -- which indicates the length of the HTTP header, and then add
                -- the field to the tree (allowing us to filter for it)
                local hdr_str = tvbuffer():string()
                if f_req_meth() then
                    start_offset = string.find(hdr_str, "GET")
                    end_offset = string.find(hdr_str, "\r\n\r\n")
                end
                if f_resp_code() then
                    start_offset = string.find(hdr_str, "HTTP")
                    end_offset = string.find(hdr_str, "\r\n\r\n")
                end
                local hdr_len = end_offset - start_offset + 4
                -- 4 Bytes because the \r\n\r\n are not part of the HTTP Payload, hence should be counted in the header length.
                if hdr_len ~= nil then
                    treeitem:add(http_wrapper_proto.fields.hdr_len, hdr_len):set_generated()
                end
        end
end

register_postdissector(http_wrapper_proto)

答案 3 :(得分:0)

我发现这种在链中调用前一个解剖器的方式干扰了为“分块”传输编码完成的HTTP数据包重组。也就是说,如果您的响应具有“Transfer-Encoding:chunked”标头,则原始HTTP解析器会尝试重新组合数据,如果您将其与此类http_wrapper挂钩,则重新组装失败。

例如,这会使http统计失败。统计/ HTTP /数据包计数器会给你,比如6个请求和4个响应,但情况并非如此=)

应该更好地安装这种“附加值”解剖器,并使用'register_postdissector'API调用或测试来仔细重新组装逻辑。