【wireshark】插件开发(四):Lua插件Post-dissector和Listener

2023-11-02 00:10

本文主要是介绍【wireshark】插件开发(四):Lua插件Post-dissector和Listener,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. Post-dissector

post-dissector和dissector不同,它会在所有dissectors都执行过后再被执行,这也就post前缀的由来。post-dissector的构建方式和dissector差不多,主要一个区别是注册的方式,post-dissector调用的是register_postdissetor接口。下面给出两个示例。

1.1 最简单的Post-dissector

这个示例主要是演示post-dissector脚本的骨架,它的功能是在packet list的所有info列加上了一个字符串"hello world"。

-- @brief A simple post-dissector, just append string to info column
-- @author zzq
-- @date 2015.08.13local myproto = Proto("hello","Dummy proto to edit info column")-- the dissector function callback
function myproto.dissector(tvb,pinfo,tree)pinfo.cols.info:append(" hello world")
end-- register our new dummy protocol for post-dissection
register_postdissector(myproto)

此插件运行效果如下图:

1.2 识别协议特征

这个示例简单地演示了如何使用post-dissector来识别协议特征。例子中,通过识别tcp载荷中是否含有字符串”weibo“来判断报文是否为weibo报文,如果是,则在packet list的protocol列标出,并在proto tree添加树节点,给出滑动特征在TCP载荷中的位置。

代码如下,其中有好多小问题,但这不是重点,重点是了解如何编写Lua插件。

-- @brief A post-dissector, to indentify pattern in payload
-- @author zzq
-- @date 2015.08.26local weibo = Proto("weibo", "Weibo Service")local function get_payload_offset(data, proto_type)local mac_len = 14;local total_len;local ip_len = (data(14, 1):uint() - 64) * 4;if (proto_type == 0x06) thenlocal tcp_len = (data(46, 1):uint()/16) * 4;total_len = mac_len + ip_len + tcp_len;elseif (proto_type == 0x11) thenlocal udp_len = 8;total_len = mac_len + ip_len + udp_len;endreturn total_len
end-- the dissector function callback
function weibo.dissector(tvb, pinfo, tree)local proto_type = tvb(23, 1):uint();if(proto_type ~= 0x06) thenreturnendlocal offset = get_payload_offset(tvb, proto_type)local data = tvb(offset):string();local i, j = string.find(data, "weibo")if(i) thenpinfo.cols.protocol = weibo.namelocal subtree = tree:add(weibo, tvb(offset+i-1))subtree:append_text(", ptn_pos: " .. i .. "-" .. j)end
end-- register our plugin for post-dissection
register_postdissector(weibo)

运行效果如下图。

2. Listener

Listner用来设置一个监听条件,当这个条件发生时,执行事先定义的动作。

实现一个Listner插件至少要实现以下接口:

  • 创建Listener
    listener = Listener.new([tap], [filter]),其中tap, filter分别是tap和过滤条件
  • listener.packet
    在条件命中时调用
  • listener.draw
    在每次需要重绘GUI时调用
  • listener.reset
    清理时调用

以上实现代码一般都包在一个封装函数中,最后把这个函数注册到GUI菜单:

register_menu(name, action, [group])

 

下面的示例代码对pcap文件中的http报文进行了简单的计数统计:

-- @brief a simple Listener plugin
-- @author zzq
-- @date 2015.08.13local function zzq_listener()local pkts = 0local win = TextWindow.new("zzq Listener")local tap = Listener.new(nil, "http")win:set_atclose(function() tap:remove() end)function tap.packet (pinfo, tvb, tapinfo)pkts = pkts + 1endfunction tap.draw()win:set("http pkts: " .. pkts)endfunction tap.reset()pkts = 0end-- Rescan all packets and just run taps - don’t reconstruct the display.
    retap_packets()
endregister_menu("freeland/zzq Listener", zzq_listener, MENU_STAT_GENERIC)

要查看运行结果,要选择”Statistics“菜单中的freeland/zzq Listerner子菜单来触发。此插件的运行效果如下图:

 

转载于:https://www.cnblogs.com/zzqcn/p/4846745.html

这篇关于【wireshark】插件开发(四):Lua插件Post-dissector和Listener的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/326746

相关文章

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

Python开发简易网络服务器的示例详解(新手入门)

《Python开发简易网络服务器的示例详解(新手入门)》网络服务器是互联网基础设施的核心组件,它本质上是一个持续运行的程序,负责监听特定端口,本文将使用Python开发一个简单的网络服务器,感兴趣的小... 目录网络服务器基础概念python内置服务器模块1. HTTP服务器模块2. Socket服务器模块

Java 与 LibreOffice 集成开发指南(环境搭建及代码示例)

《Java与LibreOffice集成开发指南(环境搭建及代码示例)》本文介绍Java与LibreOffice的集成方法,涵盖环境配置、API调用、文档转换、UNO桥接及REST接口等技术,提供... 目录1. 引言2. 环境搭建2.1 安装 LibreOffice2.2 配置 Java 开发环境2.3 配

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3