用Wireshark lua编写的协议解析器查看Content-Type为application/x-www-form-urlencoded的HTTP抓包

本文介绍如何使用Lua脚本增强Wireshark的功能,自动解析并展示HTTP POST请求中的application/x-www-form-urlencoded数据,包括参数提取与URL解码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、问题:

用Wireshark抓包查看HTTP POST消息,Content-Type为application/x-www-form-urlencoded(对应HTML为<form method="POST">那种提交后的网络包)时不太方便,因为抓包只显示了原始的param1=value1&param2=value2这样的数据,没有把参数提出出来,并对参数进行url decode,如下图所示:

 

二、问题解决过程及心得

Google上没有找到解决方法,最近发现wireshark可以用lua编写解析器,我就试着自己写了一个。过程中走了一些弯路总结一下:

1. 不能用postdissectors,因为HTTP请求有时候是会分几个TCP包的,这种情况下postdissectors只能得到每个包的数据,不能得到完整的application/x-www-form-urlencoded体部内容。

2. 不用自己重新写一个dissector去监听http的tcp端口,这样,你不仅还要调用原来http的dissector,还要自己判断tcp分包了没(reassembled)。

 

刚开始的几种方法都不太理想,最后,看了一下wireshark源码中http解析包的实现,发现只要写一个dissector注册到media_type的dissector table里就可以了。具体的分析请参考《如何在wireshark里用lua脚本编写dissector解析HTTP BODY (after TCP reassembled)》(http://blog.youkuaiyun.com/jasonhwang/archive/2010/04/25/5526383.aspx)。

 

 

 

三、如何搞定!!

好,上面的东西大家看不懂没关系只要照下面步骤来一下就搞定了:

1. 在wireshark安装目录(如C:/Program Files/Wireshark)下编写一个文本文件my_form_urlencoded.lua,文件内容如下:

 

 

 

然后修改wireshark安装目录下的init.lua文件:

(1)把disable_lua = true; do return end;这行注释掉:在前面加“--”

(2)然后在init.lua文件最后面加一句:dofile("my_form_urlencoded.lua")

 

OK大功告成,重新打开抓包界面变为如下图:

 

可以看到,原来的Line-based Text...被替换成了行MIME Encapsulation ...。看该节点下[Raw Data]为原始数据,[Decoded Data]下为解开后的一个个参数(经过url decode)。

 

如果要显示原来的Line-based Text...行也可以,到preferences窗口里找到my_form_urlencoded协议,然后选上"Show orignal wireshark's data-text-lines dissection item in Package Details tree"选项即可。

 

 

 

实际上这种方法还可以解析其它媒体类型的HTTP Body,如何编写解析HTTP Body媒体数据的方法请参考《如何在wireshark里用lua脚本编写dissector解析HTTP BODY (after TCP reassembled)》(http://blog.youkuaiyun.com/jasonhwang/archive/2010/04/25/5526383.aspx)。

 

 

注:wireshark版本为1.2.7

 

 

2015年 更新:

-- Decode param=value from "application/x-www-form-urlencoded" type http body
-- Author: Huang Qiangxiong (qiangxiong.huang@gmail.com)
-- change log:
--      2010-08-23
--          Place spaces before and after '=' between name and value ( convert '=' to ' = ').
--      2010-04-20
--          Just can play.
--      2010-04-24   
--          Add option "Turn on/off debug tree item" to preference window.
--          Add option "add_orig_item" to preference window.
------------------------------------------------------------------------------------------------
do
    local form_urlencoded_proto = Proto("my_form_urlencoded", "MIME Encapsulation [HQX's plugins]: application/x-www-form-urlencoded")

    --setup options that could be found in preferences->MY_FORM_URLENCODED
    local prefs = form_urlencoded_proto.prefs
    prefs.debug_flag = Pref.bool("Turn on debug (a [DEBUG Tree proto: my_form_urlencoded] item will appear in Package Details tree)", 
                                 false, 
                                 "If you turn of debug, (a [DEBUG Tree proto: my_form_urlencoded] item will appear in Package Details tree)")
    prefs.add_orig_item = Pref.bool("Show orignal wireshark's data-text-lines dissection item in Package Details tree", 
                                    false, 
                                    "Show orignal wireshark's data-text-lines dissection item in Package Details tree")
    -----------DEBUG Function ------------------------------------------------
    --local debug_flag = true
    local dmap = {}
    function d(tree, msg)
        if prefs.debug_flag and tree then
            local dt = dmap[tree]
            if dt == nil then 
                dt = tree:add("[DEBUG Tree for " .. form_urlencoded_proto.name .. "]")
                dmap[tree] = dt
            end
            dt:add("[DEBUG] " .. msg) 
        end
    end
    ---------------------------------------------------------------------------------
    
    ---- url decode (from www.lua.org guide)
    function unescape (s)
        s = string.gsub(s, "+", " ")
        s = string.gsub(s, "%%(%x%x)", function (h)
            return string.char(tonumber(h, 16))
        end)
        return s
    end
   
    ---- save old dissector
    local media_type_table = DissectorTable.get("media_type")
    local old_dissector = media_type_table:get_dissector("application/x-www-form-urlencoded")

    ---- my dissector
    function form_urlencoded_proto.dissector(tvb, pinfo, tree)
        d(tree, "pinfo.curr_proto=" .. pinfo.curr_proto)
        d(tree, "tvb:offset()=" .. tvb:offset()) 
        d(tree, "tvb:len()=" .. tvb:len()) 
        
        if prefs.add_orig_item then
            old_dissector:call(tvb, pinfo, tree)
        end
        
        -- begin build my tree
        local tvb_range = tvb()
        local content = tvb_range:string()
        
        -- add proto item to tree
        local subtree = tree:add(form_urlencoded_proto, tvb_range)
        
        -- add raw data to tree
        subtree:add(tvb_range, "[Raw Data] (" .. tvb_range:len() .. " bytes)"):add(tvb_range, content)

        -- add param value pair to tree
        local pairs_tree = subtree:add(tvb_range, "[Decoded Data]")
        local si = 1
        local ei = 0
        local count = 0
        while ei do
            si = ei + 1
            ei = string.find(content, "&", si)
            local xlen = (ei and (ei - si)) or (content:len() - si + 1)
            if xlen > 0 then
                -- pairs_tree:add(tvb(si-1, xlen), unescape(content:sub(si, si+xlen-1)))
                pairs_tree:add(tvb(si-1, xlen), unescape(content:sub(si, si+xlen-1):gsub("=", " = ", 1)))
                count = count + 1
            end
        end
        pairs_tree:append_text(" (" .. count .. ")")
        
    end

    -- register this dissector
    media_type_table:add("application/x-www-form-urlencoded", form_urlencoded_proto)

end

 

 

 

 

 

 

 

 

 

 

 

 

<think>嗯,用户这次提出的问题与之前的内容直接相关。用户想要分析特定服务"cap-aco-xmabc"的HTTP请求URL和Headers问题,特别是涉及"Provisionalheadersareshown"警告的情况。这需要结合HTTP协议知识和引用中提到的各类异常原因进行系统性排查。回顾引用资料可以发现,"Provisionalheadersareshown"本质上表示浏览器未收到完整响应。引用4列举了七种可能原因:浏览器插件干扰、服务端未响应、HTTPS问题、跨域拦截、服务器超时、缓存问题和URL错误。其中第四点跨域问题在引用2和5中也有印证。用户提到的服务名称"cap-aco-xmabc"可能包含特定线索:-"cap"可能是公司/项目缩写-"aco"可能指代访问控制(AccessControl)-"xmabc"可能是环境标识这暗示问题可能发生在测试环境,需要优先排查跨域配置和接口准确性。根据引用1和3,还需要验证Content-Type一致性。服务若要求JSON但实际发送URL编码数据,或ResponseType设置错误,都会引发异常。用户引用的微信小程序案例说明API参数格式错误也会产生同类警告。解决方案应该分三个层面推进:首先检查基础请求要素,确认URL拼写正确且服务可达。引用4和5的案例都显示这是最常见错误;其次聚焦跨域问题,需严格对照引用2进行CORS头部配置。服务器端要允许cap-aco-xmabc来源;最后通过Network工具分析请求全流程,引用4提示要注意观察是否被浏览器扩展拦截。</think> 针对"cap-aco-xmabc"服务的HTTP请求URL和Headers问题(含`Provisional headers are shown`警告),结合引用内容分析如下: --- ### **核心问题诊断与排查步骤** #### 1. **URL准确性验证** - **确保接口地址正确**:检查请求URL是否完整匹配后端服务端点(如`https://cap-aco-xmabc.com/api/endpoint`),避免拼写错误或路径错误(原因7)[^4]。 - **协议一致性**:确认服务是否强制使用HTTPS(原因3)[^4]。 #### 2. **跨域问题排查(CORS)** - **服务端配置**: - 后端需在响应头中返回: ```http Access-Control-Allow-Origin: https://your-client-domain.com // 或 *(不推荐) Access-Control-Allow-Methods: POST, GET, OPTIONS Access-Control-Allow-Headers: Content-Type, Authorization ``` 引用2指出需清除旧配置后重新设置`Access-Control-Allow-Origin`[^2]。 - **预检请求(OPTIONS)处理**:确保服务正确处理`OPTIONS`预检请求(引用5中方法为POST时必触发)[^5]。 #### 3. **Headers配置问题** - **Content-Type匹配**: - 若请求体为JSON:`Content-Type: application/json` - 若为表单:`Content-Type: application/x-www-form-urlencoded` 不匹配会导致请求被拒绝(引用1)[^1]。 - **关键Header缺失**:检查`Authorization`、`Cookie`等认证头(引用3)[^3]。 #### 4. `Provisional headers are shown`专项排查 - **浏览器插件干扰**:禁用Chrome插件(如Postman、广告拦截器)测试(原因1)[^4]。 - **缓存问题**:在请求中添加随机参数避免缓存(如`?_t=${Date.now()}`)(原因6)[^4]。 - **服务端未响应**:确认后端接口有响应输出(如Spring Boot需加`@ResponseBody`注解)(原因2)[^4]。 - **请求未发出**:用Wireshark/Fiddler抓包验证请求是否真正发出。 --- ### **解决方案建议** 1. **服务端修正**: ```java // Spring Boot示例:显式设置CORS和响应注解 @RestController @CrossOrigin(origins = "https://client-domain.com") public class XmabcController { @PostMapping("/api") @ResponseBody public ResponseData handleRequest(@RequestBody RequestParam params) {...} } ``` 2. **客户端修正(前端/小程序)**: ```javascript // 请求示例(添加时间戳避免缓存) wx.request({ url: `https://cap-aco-xmabc.com/api?t=${new Date().getTime()}`, header: { "Content-Type": "application/json", "Authorization": "Bearer token123" }, method: "POST", success: (res) => { ... } }) ``` --- ### **验证流程** 1. 浏览器开发者工具 > Network标签: - 检查请求状态码(红色表示失败) - 查看响应头是否含`Access-Control-Allow-Origin` 2. 使用curl命令测试: ```bash curl -X POST 'https://cap-aco-xmabc.com/api' \ -H 'Content-Type: application/json' \ -H 'Origin: https://client-domain.com' \ -d '{"key": "value"}' ``` ---
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值