找回密码
 立即注册
查看: 453|回复: 0

wireshark 书写lua插件

[复制链接]
发表于 2022-2-26 11:18 | 显示全部楼层 |阅读模式
未书写插件前


wireshark的协议不支持之前,报文几乎难以分析,举例如下

image-20210908144418891

Wireshark插件路径

MAC


image-20210908142121894

重新加载Wireshark中的lua插件

MAC


image-20210908141433962

从一个新协议的样板开始

pulsar_protocol = Proto("Pulsar", "Pulsar Protocol")pulsar_protocol.fields = {}function pulsar_protocol.dissector(buffer, pinfo, tree)    length = buffer:len()    if length == 0 then        return    end    pinfo.cols.protocol = pulsar_protocol.name    local subtree = tree:add(pulsar_protocol, buffer(), "Pulsar Protocol Data")endlocal tcp_port = DissectorTable.get("tcp.port")tcp_port:add(6650, pulsar_protocol)
我们从协议对象开始,命名为pulsar_protocol。构造函数两个参数分别为名称和描述。协议需要一个fields表和dissecotr函数。我们现在还没有任何field,所以fields表为空。对于每一个报文,dissctor函数都会被调用一次。

dissector函数有三个入参,buffer,pinfo,和tree。buffer包含了网络包的内容,是一个Tvb对象。pinfo包含了wireshark中展示packet的列信息,是一个Pinfo对象。tree是wireshark报文详情显示的内容,是TreeItem对象。

在dissector函数中,我们检查buffer的长度,如果长度为0,则立即返回

pinfo对象包含着列信息,我们可以将pinfo的protocol设置为pulsar,显示在wireshark的界面中。接下来在packet的结构中创建一个子树,最后,我们把协议绑定到6650端口上。让我们加载这个lua插件
mkdir -p ~/.local/lib/wireshark/pluginscp $DIR/../../pulsar_dissector.lua ~/.local/lib/wireshark/plugins/pulsar_dissector.lua
结果符合预期

image-20210908145821228

添加长度字段


让我们添加一个长度字段,pulsar协议中,长度字段即就是前4个字节,定义字段
message_length = ProtoField.int32("pulsar.message_length", "messageLength", base.DEC)pulsar_protocol.fields = { message_length }
pulsar.message_length可以用在过滤器字段中。messageLength是子树中的label。第三个字段决定了这个值会被如何展示

最后,我们把长度值加入到Wireshark的tree中
subtree:add(message_length, buffer(0,4))
pulsar的协议是大端序,我们使用add函数。如果协议是小端序,我们就可以使用addle函数。

image-20210908153825471

我们添加的message_length字段已经可以显示在Wireshark中了
添加额外信息

protoc加入到wireshark

参考及附录

proto field函数列表


https://www.wireshark.org/docs/wsdg_html_chunked/lua_module_Proto.html#lua_fn_ProtoField_char_abbr___name____base____valuestring____mask____desc__
wireshark解析protobuf


https://ask.wireshark.org/question/15787/how-to-decode-protobuf-by-wireshark/

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-11 05:27 , Processed in 0.136312 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表