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.13 local 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.26 local weibo = Proto("weibo", "Weibo Service") local function get_payload_offset(data, proto_type)
local mac_len = ;
local total_len;
local ip_len = (data(, ):uint() - ) * ;
if (proto_type == 0x06) then
local tcp_len = (data(, ):uint()/) * ;
total_len = mac_len + ip_len + tcp_len;
elseif (proto_type == 0x11) then
local udp_len = ;
total_len = mac_len + ip_len + udp_len;
end
return total_len
end -- the dissector function callback
function weibo.dissector(tvb, pinfo, tree)
local proto_type = tvb(, ):uint();
if(proto_type ~= 0x06) then
return
end local offset = get_payload_offset(tvb, proto_type)
local data = tvb(offset):string();
local i, j = string.find(data, "weibo")
if(i) then
pinfo.cols.protocol = weibo.name
local subtree = tree:add(weibo, tvb(offset+i-))
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.13 local function zzq_listener()
local pkts =
local 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 +
end function tap.draw()
win:set("http pkts: " .. pkts)
end function tap.reset()
pkts =
end -- Rescan all packets and just run taps - don’t reconstruct the display.
retap_packets()
end register_menu("freeland/zzq Listener", zzq_listener, MENU_STAT_GENERIC)

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

最新文章

  1. theano broadcasting
  2. Material Design兼容包的使用
  3. 发布一个免费开源软件-- PAD流程图绘制软件PADFlowChart
  4. AppCan JSSDK模块扩展
  5. 浅谈JavaScript中的Function引用类型
  6. OllyICE 调试的程序无法处理异常 解决方法
  7. java.lang.ThreadGroup.enumerate
  8. 【笨嘴拙舌WINDOWS】消息机制
  9. acdream 1685 多民族王国(DFS,并查集)
  10. nginx upstream模块
  11. hdu1232 畅通工程
  12. POJ 2260(ZOJ 1949) Error Correction 一个水题
  13. yum 安装软件提示错误
  14. 重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件
  15. List转换成JSON对象
  16. 饮冰三年-人工智能-Python-30 python开发中常见的错误
  17. harpoxy 配置
  18. django之Form组件补充
  19. vue3.0中如何使用ueditor
  20. Linux MMC framework2:基本组件之host

热门文章

  1. phpstrom+xdebug配置
  2. 2018.09.22 atcoder Snuke's Coloring 2(线段树+单调栈)
  3. Django介绍(2)
  4. [GO]kafka的生产者和消费者
  5. win7-64bit下安装Scipy
  6. @media screen
  7. 17)maven-surefire-plugin
  8. POI解析Excel文件
  9. linux 各项配置汇总
  10. [svn] TortoisSVN的Blam功能