1. 程式人生 > >如何用Wireshark lua編寫的協議解析器,檢視HTTP包的URI/URL的Query String裡的引數

如何用Wireshark lua編寫的協議解析器,檢視HTTP包的URI/URL的Query String裡的引數

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 ([email protected]
    )  
  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.     ---- url decode (from www.lua.org guide)  
  11.     function unescape (s)  
  12.         s = string.gsub(s, "+"" ")  
  13.         s = string.gsub(s, "%%(%x%x)", function (h)  
  14.             return string.char(tonumber(h, 16))  
  15.         end)  
  16.         return s  
  17.     end  
  18.     ---- convert string to hex string  
  19.     function string2hex (s)  
  20.         local hex = "";  
  21.         for i=1#s, 1 do
  22.             hex = hex .. string.format("%x", s:byte(i))  
  23.         end  
  24.         return hex  
  25.     end  
  26.     local f_http_uri = Field.new("http.request.uri")  
  27.     ---- my dissector  
  28.     function querystring_decoder_proto.dissector(tvb, pinfo, tree)  
  29.         local http_uri = f_http_uri()  
  30.         -- ignore packages without "http.request.uri"
  31.         ifnot http_uri then return end  
  32.         -- begin build my tree  
  33.         local content = http_uri.value  
  34.         local idx = content:find("?")  
  35.         ifnot idx then return end -- not include query string, so stop parsing  
  36.         local tab = ByteArray.new(string2hex(content)):tvb("Decoded HTTP URI Query String")  
  37.         local tab_range = tab()  
  38.         -- add proto item to tree  
  39.         local subtree = tree:add(querystring_decoder_proto, tab_range)  
  40.         -- add raw data to tree  
  41.         subtree:add(tab_range, "[HTTP Request URI] (" .. tab_range:len() .. " bytes)"):add(tab_range, content)  
  42.         -- add param value pair to tree  
  43.         local pairs_tree = subtree:add(tab_range, "[Decoded Query String]")  
  44.         local si = 1
  45.         local ei = idx  
  46.         local count = 0
  47.         while ei do  
  48.             si = ei + 1
  49.             ei = string.find(content, "&", si)  
  50.             local xlen = (ei and (ei - si)) or (content:len() - si + 1)  
  51.             if xlen > 0 then  
  52.                 pairs_tree:add(tab(si-1, xlen), unescape(content:sub(si, si+xlen-1)))  
  53.                 count = count + 1
  54.             end  
  55.         end  
  56.         pairs_tree:append_text(" (" .. count .. ")")  
  57.     end  
  58.     -- register this dissector  
  59.     register_postdissector(querystring_decoder_proto)  
  60. 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裡的原始形式。