亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 使用Lua擴展ethereal網(wǎng)絡(luò)截包 [打印本頁]

作者: yanghoo    時間: 2009-06-23 15:13
標(biāo)題: 使用Lua擴展ethereal網(wǎng)絡(luò)截包
From:
http://www.luaer.cn/bbs/read-Lua-tid-27.html
一直用ethereal做一些網(wǎng)絡(luò)截包的工作,感嘆于ethereal強大的同時,也為其無法如analsys捕獲工具的netpdl語言一般擴展而感到不便,自己寫解析器又覺得麻煩,現(xiàn)在ethereal由了0.99.0版本(一個跟前一個版本跳躍很大的版本號),支持lua擴展,真是福音。
當(dāng)然做法就是采用lua擴展,把一些基本功能變成lua可以調(diào)用的庫,然后在啟動的時候把lua腳本作為參數(shù)傳遞進去。用lua腳本可以做三種方式的事情:
tap:一種從每一幀中提取數(shù)據(jù)的方式,可以用顯示過濾器定義,下面是一個例子:
    一個簡單的tap,可以用于tethereal,用于統(tǒng)計通過10.0.0.1得http和dns包:


    -- this is going to be our counter
    http_packets = 0

    -- this is going to be our tap
    tap_http = nil

    -- first we declare the tap called "http tap" with the filter it is going to use
    tap_http = new_tap("mytap","ip.addr == 10.0.1.3 && http")

    -- then we define a function to (re)initialize our counter
    -- this one is going to be called every time the capture restarts (2)
    function tap_http.init()
        http_packets = 0
    end

    -- this function will get called at the end(3) of the capture to print the summary
    function tap_http.draw()
        print("http packets:",http_packets)
    end

    -- this function is going to be called once each time the filter of the tap matches
    function tap_http.packet()
        http_packets = http_packets + 1

        -- We return true to have ethereal printing a report every few seconds
        -- if we returned false ethereal would call the draw function never
        return true
    end

    GUI方式
    -- text_window_tap.lua
    -- an example of a tap that registers a menu
    -- and prints to a text window

    instances = 0 -- number of instances of the tap created so far

    function mytap_menu()
        instances = instances + 1

        local td = {}
        -- the tap data, passed to every function of the tap
        -- beware not to use a global for taps with multiple instances or you might
        -- find it been written by more instances of the tap, not what we want.
     
        td.win = TextWindow.new("My Tap " .. instances) -- the window we'll use
        td.text = "" -- the text of the tap
        td.instance = instances -- the instance number of this tap

        -- this tap will be local to the menu_function that called it
        -- it's called mytap
        -- has no filter (filter = nil)
        -- and we pass to it the tap data so that it gets passed to the tap's functions
        local tap = new_tap("mytap"..instances,nil, td)
     
        -- make sure the tap doesn't hang arround after the window was closed
        td.win:at_close(remove_tap,tap)

        -- this function will be called for every packet
        function tap.packet(pinfo,tvb,tapdata)
            local text = "packet " .. pinfo.number
            tapdata.text = tapdata.text .. "\n" .. text
            -- print("packet " .. pinfo.number, tapdata.instance)
        end

        -- this function will be called once every few seconds to redraw the window
        function tap.draw(tapdata)
            tapdata.win:set(tapdata.text)
            -- print("draw", tapdata.instance)
        end

        -- this function will be called before every run of the tap
        function tap.init(tapdata)
            tapdata.text = ""
            -- print("init", tapdata.instance)
        end

    end

    -- last we register the menu
    -- the first arg is the menu name
    -- the 2nd arg is the function to be called
    -- the third argument (defaults to false) tells to re-run the capture once the function is run
    register_menu("Lua Tap Test",mytap_menu,true)

    -- print("registered")
dissectors:解析器
解析器用與分析包的數(shù)據(jù),類似C解析器,可以注冊lua寫的解析器來某個協(xié)議,ethereal會將一個數(shù)據(jù)緩沖區(qū)和一個包信息傳遞給lua處理函數(shù)


    -- trivial protocol example

    -- declare our protocol
    trivial_proto = Protocol("trivial","TRIVIAL","Trivial Protocol")

    -- create a function to dissect it
    function trivial_proto.dissector(buffer,pinfo,tree)
        pinfo.cols.protocol = "TRIVIAL"
        local subtree = tree:add_item(trivial_proto,buffer(),"Trivial Protocol Data"):add_subtree()
        subtree:add_item(buffer(0,2),"The first two bytes: " .. buffer(0,2):uint())
    end

    -- load the udp.port table
    udp_table = DissectorTable.get("udp.port")

    -- register our protocol to handle udp port 7777
    udp_table:add(7777,trivial_proto)

postdissectors 后解析器:
一個后解析器是在其他解析器已經(jīng)被調(diào)用后才調(diào)用的解析器,由于所有的域已經(jīng)被解析,所以這種類型的解析器可以存取所有域并且能加域到解析樹中:

    -- trivial postdissector example

    -- declare some Fields to be read
    ip_src_f = Field("ip.src")
    ip_dst_f = Field("ip.dst")
    tcp_src_f = Field("tcp.srcport")
    tcp_dst_f = Field("tcp.dstport")

    -- declare our (pseudo) protocol
    trivial_proto = Protocol("trivial","TRIVIAL","Trivial Postdissector")

    -- create the fields for our "protocol"
    src_F = ProtoField.string("trivial.src","Source");
    dst_F = ProtoField.string("trivial.dst","Destination");
    conv_F = ProtoField.string("trivial.conv","Conversation","A Conversation");

    -- add the field to the protocol
    trivial_proto.fields = ProtoFieldArray.new(src_F, dst_F, conv_F)

    -- create a function to "postdissect" each frame
    function trivial_proto.dissector(buffer,pinfo,tree)
        -- obtain the current values the protocol fields
        local tcp_src = tcp_src_f()
        local tcp_dst = tcp_dst_f()
        local ip_src = ip_src_f()
        local ip_dst = ip_dst_f()
        if tcp_src then
          local subtree = tree:add_item(trivial_proto,"Trivial Protocol Data"):add_subtree()
          local src = ip_src .. ":" .. tcp_src
          local dst = ip_dst .. ":" .. tcp_dst
          local conv = src  .. "->" .. dst
   
          subtree:add_item(src_F,src)
          subtree:add_item(dst_F,dst)
          subtree:add_item(conv_F,conv)
        end
    end

    -- register our protocol as a postdissector
    register_postdissector(trivial_proto)
               
               
               
               

本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u2/79955/showart_1974466.html




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2