现在的位置: 首页 > 综合 > 正文

如何用Wireshark lua编写的协议解析器,查看HTTP包的URI/URL的Query String里的参数

2017年11月25日 ⁄ 综合 ⁄ 共 3406字 ⁄ 字号 评论关闭

http://blog.csdn.net/jasonhwang/article/details/5710166

Wireshark解析HTTP GET方法不会解析URI里Query字符串里的参数(通常由GET方式提交form数据),本文介绍用lua编写一个简单的协议解析器,让这些参数解析出来,并显示在wireshark协议解析窗口里。

 

首先编写以下解析器lua脚本(用文本编辑器编辑即可),取文件名为my_http_querystring_decoder.lua:

  1. -- Decode param=value from query string of http request uri (http.request.uri)  
  2. -- Author: Huang Qiangxiong (qiangxiong.huang@gmail.com)  
  3. -- change log:  
  4. --      2010-07-01  
  5. --          Just can play.  
  6. ------------------------------------------------------------------------------------------------  
  7. do  
  8.     local querystring_decoder_proto = Proto("my_http_querystring_decoder",   
  9.                                             "Decoded HTTP URI Query String [HQX's plugins]")  
  10.   
  11.     ---- url decode (from www.lua.org guide)  
  12.     function unescape (s)  
  13.         s = string.gsub(s, "+"" ")  
  14.         s = string.gsub(s, "%%(%x%x)", function (h)  
  15.             return string.char(tonumber(h, 16))  
  16.         end)  
  17.         return s  
  18.     end  
  19.       
  20.     ---- convert string to hex string  
  21.     function string2hex (s)  
  22.         local hex = "";  
  23.         for i=1#s, 1 do  
  24.             hex = hex .. string.format("%x", s:byte(i))  
  25.         end  
  26.         return hex  
  27.     end  
  28.       
  29.     local f_http_uri = Field.new("http.request.uri")  
  30.   
  31.     ---- my dissector  
  32.     function querystring_decoder_proto.dissector(tvb, pinfo, tree)  
  33.         local http_uri = f_http_uri()  
  34.         -- ignore packages without "http.request.uri"  
  35.         if not http_uri then return end  
  36.           
  37.         -- begin build my tree  
  38.         local content = http_uri.value  
  39.         local idx = content:find("?")  
  40.         if not idx then return end -- not include query string, so stop parsing  
  41.           
  42.         local tab = ByteArray.new(string2hex(content)):tvb("Decoded HTTP URI Query String")  
  43.         local tab_range = tab()  
  44.           
  45.         -- add proto item to tree  
  46.         local subtree = tree:add(querystring_decoder_proto, tab_range)  
  47.           
  48.         -- add raw data to tree  
  49.         subtree:add(tab_range, "[HTTP Request URI] (" .. tab_range:len() .. " bytes)"):add(tab_range, content)  
  50.   
  51.         -- add param value pair to tree  
  52.         local pairs_tree = subtree:add(tab_range, "[Decoded Query String]")  
  53.         local si = 1  
  54.         local ei = idx  
  55.         local count = 0  
  56.         while ei do  
  57.             si = ei + 1  
  58.             ei = string.find(content, "&", si)  
  59.             local xlen = (ei and (ei - si)) or (content:len() - si + 1)  
  60.             if xlen > 0 then  
  61.                 pairs_tree:add(tab(si-1, xlen), unescape(content:sub(si, si+xlen-1)))  
  62.                 count = count + 1  
  63.             end  
  64.         end  
  65.         pairs_tree:append_text(" (" .. count .. ")")  
  66.           
  67.     end  
  68.   
  69.     -- register this dissector  
  70.     register_postdissector(querystring_decoder_proto)  
  71. end  

 

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

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

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

OK大功告成,打开HTTP抓包,若其请求中URI带QueryString,则界面如下:

 

 可以看到,在协议解析树上新增了Decoded HTTP URI Query String ... 节点。看该节点下[HTTP Request URI]为原始HTTP Request URI,[Decoded Query String]下为解开后的一个个参数(经过url decode)。而且在wireshark的“Packet Bytes”窗口里新增了一个“Decoded HTTP URI Query String”的数据tab,专门显示HTTP URI内容,用于显示参数在URL里的原始形式。

 

 

另外,如果要解析HTTP Body部分的参数(用POST方法提交form数据),请参考《用Wireshark lua编写的协议解析器查看Content-Type为application/x-www-form-urlencoded的HTTP抓包》(http://blog.csdn.net/jasonhwang/archive/2010/04/25/5525700.aspx)。

抱歉!评论已关闭.