WireShark Lua Example

本文介绍 Wireshark 中使用 Lua 脚本进行协议注册、文件转储、列编辑、对话框创建及包计数等功能的具体实现方法。

From: http://wiki.wireshark.org/Lua/Examples


 

 

Using Lua to register protocols to more ports

 

 

-- register http to handle ports 4888-4891
do
        local tcp_port_table = DissectorTable.get("tcp.port")
        local http_dissector = tcp_port_table:get_dissector(80)
        for port in {4888,4889,4890,4891} do
                tcp_port_table:add(port,http_dissector)
        end
end

 

 

dumping to multiple files

 

 

-- Create a file named by_ip/''ip_addess''.cap with all ip traffic of each ip host. (works for tshark only)
-- Dump files are created for both source and destination hosts
do
        local dir = "by_ip"
        local dumpers = {}
        local function init_listener()
                local tap = Listener.new("ip")
                -- we will be called once for every IP Header.
                -- If there's more than one IP header in a given packet we'll dump the packet once per every header
                function tap.packet(pinfo,tvb,ip)
                        local ip_src, ip_dst = tostring(ip.ip_src), tostring(ip.ip_dst)
                        local src_dmp, dst_dmp
                        src_dmp = dumpers[ip_src]
                        if not src_dmp then
                                src_dmp = Dumper.new_for_current( dir .. "/" .. ip_src .. ".pcap" )
                                dumpers[ip_src] = src_dmp
                        end
                        src_dmp:dump_current()
                        src_dmp:flush()
                        dst_dmp = dumpers[ip_dst]
                        if not dst_dmp then
                                dst_dmp = Dumper.new_for_current( dir .. "/" .. ip_dst .. ".pcap"  )
                                dumpers[ip_dst] = dst_dmp
                        end
                        dst_dmp:dump_current()
                        dst_dmp:flush()
                end
                function tap.draw()
                        for ip_addr,dumper in pairs(dumpers) do
                                 dumper:flush()
                        end
                end
                function tap.reset()
                        for ip_addr,dumper in pairs(dumpers) do
                                 dumper:close()
                        end
                        dumpers = {}
                end
        end
        init_listener()
end

 

 

editing columns

 

/!\ Inacurate and outdated, to be refactored to reflect current state of WSLUA API.

 

-- The following exaple inverts the src and dst columns in wireshark extracting the values with FieldExtractor
do
    local ip_src = FieldExtractor.new("ip.src")
    local ip_dst = FieldExtractor.new("ip.dst")
    local stupid_joke_tap = Tap.new("stupid_joke")
    stupid_joke_tap:use_fields(ip_src,ip_dst)
    stupid_joke_tap:register()
        function per_packet.stupid_joke(pinfo)
                col_src = pinfo:col.src
                col_dst = pinfo:col.dst
                col_src:set(ip_dst:get())
                col_dst:set(ip_src:get())
        end
end

 

 

dialogs an TextWindows

 

/!\ Inacurate and outdated, to be refactored to reflect current state of WSLUA API.

 

--  This Example will add a menu "Lua Dialog Test" that when selected will pop a dialog prompting the user for input that when accepted will pop a window with a result.

if gui_enabled() then
   local splash = TextWindow.new("Hello!");
   splash:set("This time wireshark has been enhanced with an useles feature.\n")
   splash:append("Go to Statistics->Useless Feature and check it out!")
end
function dialog_menu()
    function dialog_func(person,eyes,hair)
        local win = TextWindow.new("The Person");
        win:set(person)
        win:append(" with " .. eyes .." eyes and")
        win:append(" " .. hair .. " hair.");
    end

    new_dialog("Dialog Test",dialog_func,"A Person","Eyes","Hair")
end
register_menu("Useless Feature",dialog_menu)

 

 

Packet counter

 

 

do
    packets = 0;
    local function init_listener()
        local tap = Listener.new("frame","ip.addr == 10.0.0.0/8")
        function tap.reset()
            packets = 0;
        end
        function tap.packet(pinfo,tvb,ip)
            packets = packets + 1
        end
        function tap.draw()
            print("Packets to/from 10.0.0./8",packets)
        end
    end
    init_listener()
end
### 回答1: 非常感谢您的问题。我了解到您对于wireshark lua插件 ProtoField.uint32的使用有疑问。 首先,ProtoField.uint32是wireshark lua插件中的一个函数,用于指定字段的类型为32位的无符号整数。它的使用方法是在wireshark的脚本中调用该函数,并传递相关参数,如字段名称、描述、偏移量等,以便wireshark能够正确地解析协议中的数据。 如果您在使用ProtoField.uint32时遇到问题,建议您查看wireshark官方文档或相关的开发者社区,以获取更详细的帮助和支持。如果您需要更具体的技术指导或解决方案,可以向相关专业人士或开发者咨询,或者寻找更详细的教程和示例。 再次感谢您的提问,希望我的回答能够对您有所帮助。如果您有其他问题或需要进一步的帮助,请随时联系我。 ### 回答2: Wireshark是一款开源的网络协议分析工具,可以用于抓取和分析网络数据包。它支持使用Lua语言编写插件来扩展其功能。 Wireshark提供了一个名为ProtoField.uint32的函数,它用于创建一个新的网络协议字段,该字段的类型为无符号32位整数。ProtoField.uint32函数的语法如下: ```lua ProtoField.uint32(name, abbreviation, base, mask, [display]) ``` 其中参数的含义如下: - name:字段的名称,用于在显示过滤器和面板中标识该字段。 - abbreviation:字段的缩写,用于在协议分析结果中显示。 - base:字段的进制表示方式,可以为2、10、16之一,分别代表二进制、十进制和十六进制。 - mask:字段的掩码,用于提取关键信息。可以留空或者设置为nil,表示不使用掩码。 - display:字段的显示格式,可选参数。 使用ProtoField.uint32创建的字段可以在协议解析器脚本中使用,通过访问协议分析树中的相应字段对象获取字段的值。例如,可以通过以下方式获取字段值: ```lua local field = ProtoField.uint32("my_protocol.field_name", "My Field", base.DEC) -- ... function my_protocol.dissector(buffer, pinfo, tree) -- ... local field_value = buffer(0, 4):uint() local field_item = tree:add(field, buffer(0, 4)) -- ... end ``` 在上述例子中,我们定义了一个名为"my_protocol.field_name"的字段,缩写为"My Field",进制表示方式为十进制。然后在协议解析器的dissector函数中,使用buffer(0, 4):uint()获取前4个字节的无符号32位整数值,并将其添加到协议分析树中。 通过WiresharkLua插件和ProtoField.uint32函数,我们可以轻松地创建和使用自定义的网络协议字段,以满足特定协议的分析需求。 ### 回答3: Wireshark是一款开源的网络协议分析工具,它支持使用Lua编写插件来扩展其功能。在Wireshark中,ProtoField.uint32是一种用于表示无符号32位整数的数据类型,可以在Lua插件中使用它来定义和访问协议字段。 使用ProtoField.uint32,我们可以在Lua插件中定义一个协议字段,并指定其名称、显示的标题、描述等属性。例如,下面是一个使用ProtoField.uint32定义的示例: local my_protocol_field = ProtoField.uint32("myprotocol.field", "My Protocol Field", "This is an example protocol field") 在上面的示例中,我们定义了一个名为"myprotocol.field"的协议字段,显示标题为"My Protocol Field",描述为"This is an example protocol field"。 我们还可以通过ProtoField.uint32创建的字段来访问和解析捕获的数据包。 当我们使用ProtoField.uint32定义协议字段后,我们可以通过调用Field对象的相应方法来访问和解析数据包中的该字段的值。例如,我们可以使用"my_protocol_field"字段对象的方法来访问和解析数据包中的协议字段: local my_protocol_field_extractor = my_protocol_field() 在上面的示例中,我们创建了一个名为"my_protocol_field_extractor"的字段提取器,它可以用于从捕获的数据包中提取和解析"my_protocol_field"字段的值。 通过使用ProtoField.uint32和相应的字段提取器,我们可以在Wireshark中编写更复杂的Lua插件,用于解析和分析各种不同的网络协议。这使得我们能够更好地理解和调试网络通信,并从中获取有用的信息。 总结起来,Wireshark Lua插件中的ProtoField.uint32可以让我们定义和访问无符号32位整数类型的协议字段,并通过字段提取器提取和解析数据包中的该字段的值。这为我们分析网络通信提供了便利,同时也为定位和解决网络问题提供了有力的工具。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值