Lua - 显示字段ASCII解剖器

我目前正在处理我的第一个协议解剖器。我遇到了一个无法解决的问题。基本上,我有一个8字节长的字段(但定义在9字节上),所以我创建了一个位域来定义这个protofield。

以下是我迄今为止测试的字段的定义:

a) local harer_id = ProtoField.string   ("myProto.harer_id","Harer ID", base.ASCII)
b) local harer_id =  ProtoField.uint64   ("myProto.harer_id", "Harer ID", base.HEX )

然后我以以下方式将其添加到解剖树中:

  local harer_id_long = tvbuf:range(16,9)
  body:add(harer_id, harer_id_long:bitfield(4,64))

这最终会产生以下错误:

a) 不会出错,但它不会以ASCII格式返回值
    我得到的是:0x0000000000313030
    我想要的是:0x0000000000313030100)
b) 在错误的self(字符串预期,得到的是userdata)上调用'add'

如果您有任何建议,我会很感激您的帮助。

非常感谢您的关注,

马丁

编辑1:

我编写了这段代码,它将从字段值的每个字节获取ASCII表值:

但我不知道如何使其工作,以便在数据包视图中显示ASCII值。

 function getASCII (str)
    resultStr = ""
    asciiValue=""
    for i = 3, string.len(tostring(str))-1, 2 do
        asciiValue = string.char(tonumber(tostring(string.sub(tostring(str),i,i+1)), 16))
        if asciiValue~=nil then
            resultStr = resultStr .. tostring(tonumber(asciiValue))
        end
    end
    resultStr = string.gsub(resultStr, "nil", "")
    return resultStr
 end
点赞
用户2755698
用户2755698

也许有更有效的方法来完成这个任务,但你可以尝试这样做:

harer_id = ProtoField.string("myProto.harer_id", "Harer ID", base.ASCII)

harer_item = body:add(harer_id, tvbuf(16, 9))
harer_item:set_text("Harer ID: " ..
    tonumber(
        string.char(bit.bor(bit.band(bit.lshift(tvbuf(16, 1):uint(), 4), 0xf0), bit.rshift(tvbuf(17, 1):uint(), 4))) ..
        string.char(bit.bor(bit.band(bit.lshift(tvbuf(17, 1):uint(), 4), 0xf0), bit.rshift(tvbuf(18, 1):uint(), 4))) ..
        string.char(bit.bor(bit.band(bit.lshift(tvbuf(18, 1):uint(), 4), 0xf0), bit.rshift(tvbuf(19, 1):uint(), 4))) ..
        string.char(bit.bor(bit.band(bit.lshift(tvbuf(19, 1):uint(), 4), 0xf0), bit.rshift(tvbuf(20, 1):uint(), 4))) ..
        string.char(bit.bor(bit.band(bit.lshift(tvbuf(20, 1):uint(), 4), 0xf0), bit.rshift(tvbuf(21, 1):uint(), 4))) ..
        string.char(bit.bor(bit.band(bit.lshift(tvbuf(21, 1):uint(), 4), 0xf0), bit.rshift(tvbuf(22, 1):uint(), 4))) ..
        string.char(bit.bor(bit.band(bit.lshift(tvbuf(22, 1):uint(), 4), 0xf0), bit.rshift(tvbuf(23, 1):uint(), 4))) ..
        string.char(bit.bor(bit.band(bit.lshift(tvbuf(23, 1):uint(), 4), 0xf0), bit.rshift(tvbuf(24, 1):uint(), 4)))
        )
    )
2017-08-08 17:52:36
用户2755698
用户2755698

以下是另一种方法,也适合我使用。我不确定你更喜欢哪个,但是您现在有两个可以选择的方法(假设您可以让我的原始方法工作):

local harer_id = ProtoField.uint64("myProto.harer_id", "Harer ID", base.HEX)

harer_item = body:add(harer_id, tvbuf(16, 9):bitfield(4, 64))
harer_item:set_len(9)

vals = {}
for i = 0, 7 do
    vals[i] = bit.bor(buf(16 + i, 2):bitfield(4, 8), 0x30)
end
harer_item:append_text(" (" ..
    tonumber(string.format("%c%c%c%c%c%c%c%c", vals[0], vals[1], vals[2], vals[3], vals[4], vals[5], vals[6], vals[7])) ..
    ")")

编辑: 这里是一个简单的 Lua 解析器和示例数据包,您可以使用它来测试此解决方案:

- 协议
local p_foo = Proto("foo", "FOO Protocol")

- 字段
local f_foo_res1 = ProtoField.uint8("foo.res1", "Reserved 1", base.DEC, nil, 0xf0)
local f_foo_str = ProtoField.uint64("foo.str", "String", base.HEX)
local f_foo_res2 = ProtoField.uint8("foo.res2", "Reserved 2 ", base.DEC, nil, 0x0f)
local f_foo_res3 = ProtoField.uint8("foo.res3", "Reserved 3", base.HEX)
local f_foo_ipv6 = ProtoField.ipv6("foo.ipv6", "IPv6 Address")

p_foo.fields = { f_foo_res1, f_foo_str, f_foo_res2, f_foo_res3, f_foo_ipv6 }

- 解析
function p_foo.dissector(buf, pinfo, tree)
    local foo_tree = tree:add(p_foo, buf(0,-1))

    pinfo.cols.protocol:set("FOO")
    foo_tree:add(f_foo_res1, buf(0, 1))

    str_item = foo_tree:add(f_foo_str, buf(0, 9):bitfield(4, 64))
    str_item:set_len(9)

    vals = {}
    for i = 0, 7 do
        vals[i] = bit.bor(buf(i, 2):bitfield(4, 8), 0x30)
    end
    str_item:append_text(" (" ..
        tonumber(string.format("%c%c%c%c%c%c%c%c", vals[0], vals[1], vals[2], vals[3], vals[4], vals[5], vals[6], vals[7])) ..
        ")")

    foo_tree:add(f_foo_res2, buf(9, 1))
    foo_tree:add(f_foo_res3, buf(10, 1))
    foo_tree:add(f_foo_ipv6, buf(11, 16))
end

- 注册
local udp_table = DissectorTable.get("udp.port")
udp_table:add(33333, p_foo)

使用 text2pcap 将此数据转换为 Wireshark 可读取的数据包,或使用 Wireshark 的 "File -> Import From Hex Dump..." 功能:

0000  00 0e b6 00 00 02 00 0e b6 00 00 01 08 00 45 00
0010  00 37 00 00 40 00 40 11 b5 ea c0 00 02 65 c0 00
0020  02 66 82 35 82 35 00 23 00 00 03 03 13 23 33 43
0030  53 63 70 80 64 20 01 0d b8 00 00 00 00 00 00 00
0040  00 00 00 00 01

我的 Wireshark 详细信息:

用 Qt 5.6.2 编译(64 位),带有 WinPcap(4_1_3),带有 GLib 2.42.0,带有 zlib 1.2.8,带有 SMI 0.4.8,带有 c-ares 1.12.0,带有 Lua 5.2.4,带有 GnuTLS 3.4.11,带有 Gcrypt 1.7.6,带有 MIT Kerberos,带有 GeoIP,带有 nghttp2 1.14.0,带有 LZ4,带有 Snappy,带有 libxml2 2.9.4,带有 QtMultimedia,带有 AirPcap,带有 SBC,带有 SpanDSP。

2017-08-09 15:37:24