第二周进展

本周计划:完成自己的部分

遇到了许多的问题,比如插件载入后每次报错:

Lua: Error during loading:
C:\Program Files\Wireshark\plugins\RSA.lua:42: bad argument #1 to 'add' (DissectorTable_add: invalid integer or range)
stack traceback:
	[C]: in function 'add'
	C:\Program Files\Wireshark\plugins\RSA.lua:42: in main chunk
	[C]: in function 'dofile'
	C:\Program Files\Wireshark\init.lua:671: in main chunk

Lua: Error during loading:
C:\Program Files\Wireshark\plugins\RSA.lua:8: bad argument #2 to 'Proto' (Proto_new: there cannot be two protocols with the same description)
stack traceback:
	[C]: in function 'Proto'
	C:\Program Files\Wireshark\plugins\RSA.lua:8: in main chunk

查阅到资料,不符合但是大概清楚了原因
Lua: Error during loading:C:\Users\Shinelon\AppData\Roaming\Wireshark\plugins\sd.lua:11: bad argume

学习笔记:

Lua插件API介绍

1.Proto

    表示一个新的Protocol,在Wireshark中Protocol对象有很多用处,解析器是其中主要的一个。主要接口有:

接口          说明
proto:__call (name,desc)    创建Proto对象。name和desc分别是对象的名称和描述,前者可用于过滤器等
proto.name      get名称
proto.fields      get/set字段
proto.prefs      get配置项
proto.init         初始化,无参数
proto.dissector    解析函数,3个参数tvb,pinfo,tree,分别是报文内容,报文信息和解析树结构
proto:register_heuristic (listname, func)    为Proto注册一个启发式解析器,被调用时,参数func将被传入与dissector方法相同的3个参数

 

2 ProtoField

    表示协议字段,一般用于解析字段后往解析树上添加节点。根据字段类型不同,其接口可以分为两大类。

    整型:
    • ProtoField.{type} (abbr, [name], [desc],[base], [valuestring], [mask])
    type包括:uint8, uint16, uint24, uint32, uint64, framenum

    其他类型
    • ProtoField.{type} (abbr, [name], [desc])
    type包括:float, double, string, stringz, bytes, bool, ipv4, ipv6, ether,oid, guid

    这些接口都会返回一个新的字段对象。方括号内是可选字段,花括号内是可替换的类型字段。

    如下图的例子,

-- create fields of red
fields_M             = ProtoField.uint8 (NAME1 .. ".M", "M", base.HEX,Payload_type,0x80)
fields_pt            = ProtoField.uint8 (NAME1 .. ".PT", "PT", base.DEC,Payload_type,0x7F)
fields_seqno         = ProtoField.uint16(NAME1 .. ".seqno", "Sequence number")
fields_h264bytes     = ProtoField.bytes(NAME1 .. ".bytes", "H264Data")
fields_fec           = ProtoField.bytes(NAME1 .. ".fec", "FEC Payload")

 

3 Tvb

    Tvb(Testy Virtual Buffer)表示报文缓存,也就是实际的报文数据,可以通过下面介绍的TvbRange从报文数据中解出信息。主要接口有:

接口    说明
tvb:__tostring()    将报文数据转化为字符串,可用于调试
tvb:reported_len()    get tvb的(not captured)长度
tvb:len()    get tvb的(captured)长度
tvb:reported_length_remaining()    获取当前tvb的剩余长度,如果偏移值大于报文长度,则返回-1
tvb:offset()    返回原始偏移

    我们最常使用应该就是“tvb:len()”

4 Pinfo

    报文信息(packet information)。主要接口有:

接口    说明
pinfo.len pinfo.caplen    get报文长度
pinfo.abs_ts    get报文捕获时间
pinfo.number    get报文编号
pinfo.src pinfo.dst    get/set报文的源地址、目的地址
pinfo.columns pinfo.cols    get报文列表列(界面)

    取得报文列表列后,就可以设置该列的文本,比如

    -- show protocol name in protocol column
    pinfo.cols.protocol = red.name

 

5 DissectorTable

    表示一个具体协议的解析表,比如,协议TCP的解析表”tcp.port”包括http,smtp,ftp等。可以依次点击wireshark菜单“Internals”、“Dissector tables”,来查看当前的所有解析表。如下图,我们选择 rtp.pt解析表在“Integer tables”选项卡中,顾名思义,它是通过类型为整型的 rtp 端口号来识别下游协议的:
    在这里插入图片描述
    DissectorTable的主要接口有:
    接口说明

接口    说明
DissectorTable.get(name)    get名为name的解析表的引用
dissectortable:add(pattern, dissector)    将Proto或Dissector对象添加到解析表,即注册。pattern可以是整型值,整型值范围或字符串,这取决于当前解析表的类型
dissectortable:remove(pattern, dissector)    将满足pattern的一个或一组Proto、Dissector对象从解析表中删除

解析RSA的代码示例

-- 创建一个新的Lua插件脚本,例如 "RSA.lua"

local NAME1 = "RSA"
local PORT = 13400
-- 创建插件对象
local RSA = Proto(NAME1,"RSA Protocol")  -- 更改描述以确保唯一性

-- 定义处理函数
function RSA.dissector(buffer, pinfo, tree)
    -- 解析SSL握手消息
    local handshake_type = buffer(0, 1):uint()
    
    if handshake_type == 0x16 then
        -- SSL Handshake消息类型为0x16表示握手消息
        -- 这里继续解析握手消息以查找证书和算法信息
        local certificate_msg_length = buffer(3, 3):uint() * 256 + buffer(4, 1):uint()
        local certificate_start_offset = 5  -- 假设证书开始于偏移量5
        
        if buffer:len() >= certificate_start_offset + certificate_msg_length then
            -- 解析证书消息
            local certificate_buffer = buffer(certificate_start_offset, certificate_msg_length)
            
            -- 提取证书中的公钥算法信息
            local public_key_algorithm = "Unknown"
            local subject_key_info = certificate_buffer(12, 2):uint()
            if subject_key_info == 0x0400 then
                public_key_algorithm = "RSA"
            end
            
            -- 在信息列中设置公钥算法
            pinfo.cols.info:set("公钥算法:" .. public_key_algorithm)
        end
    end
end

-- 向Wireshark注册插件
local wtap_encap_table = DissectorTable.get("wtap_encap")
wtap_encap_table:add(0x01, RSA) -- 使用正确的整数或范围作为第一个参数,例如0x01(这是示例,请根据您的需要更改)

-- 输出插件信息
_G[NAME1] = RSA

这周任务没有完成,但学了好多,再几天就能完成任务了

posted @ 2023-11-26 19:08  周意凯  阅读(49)  评论(0编辑  收藏  举报