【问题标题】:Lua length of Frame for Parsing用于解析的帧的 Lua 长度
【发布时间】:2021-03-19 11:17:44
【问题描述】:

如果我在记事本中打开它,我有一个显示 glibberish 信息的二进制文件。 我正在开发一个与wireshark一起使用的插件。

所以我的问题是我需要帮助。我正在读取一个文件,需要在文件中找到'V''0''0''1'(0x56 0x30 0x30 0x31),因为它是一个Header的开始,意味着里面有一个数据包。我需要对整个文件执行此操作,例如解析。还应该以 V 0 0 1 开始框架,而不是以它结束。 我目前有一个代码,我正在搜索 0x7E 并对其进行解析。我需要的是框架的长度。例如找到 V 0 0 1 ,因此文件中从 V 到下一个 V 0 0 1 之前的位置的长度。这样我就可以使用长度并将其添加到捕获的长度以获取位置,wireshark 可以使用。

例如我使用 0x7E 的不完美代码:

local line = file:read()
local len = 0

for c in (line or ''):gmatch ('.') do
    len = len + 1
    if c:byte() == 0x7E then
        break
    end
    
end

if not line then 
    return false 
end

frame.captured_length = len

这也是Frame以7E结尾的问题,这是错误的。我需要一些非常适合“V”“0”“0”“1”的东西。也许我需要使用 string.find? 请帮帮我!

这是一个示例,如果我在 Visual Studio Code 中使用 HEX-Editor,我的文件会是什么样子。

【问题讨论】:

标签: parsing lua binary header hex


【解决方案1】:

Lua 有一些简洁的模式工具。总结如下:

  • (...) 导出() 中所有捕获的文本并提供给我们。
  • -, +, *, ?, "尽可能少的可选匹配", "尽可能多的强制匹配", "尽可能多的可选匹配", "可选匹配只有一次",分别。
  • ^$:分别是文件开头或结尾的根。

我们将使用这个通用输入和输出进行测试:

local output = {}
local input = "V001Packet1V001Packet2oooV001aaandweredonehere"

执行此操作的最简单方法可能是递归拆分字符串,一个以“V”之前的字符结尾,另一个以“1”之后的字符开始。我们将使用导出 V001 之前和之后的部分的模式:

local this, next = string.match(input, "(.-)V001(.*)")
print(this,next)    --> "", "Packet1V001Packet2..."

足够简单。现在我们需要再做一次,我们还需要消除第一个空包,因为这是模式的一个怪癖。我们可能只是说不应该添加任何空的this 字符串:

if this ~= "" then
    table.insert(output, this)
end

现在,对于thisnext,最后一个数据包将返回 nil,因为最后不会有另一个 V001。当模式不匹配时,我们可以通过简单地添加字符串的最后一部分来做好准备。

全部放在一起:

local function doStep(str)
    local this, next = string.match(str, "(.-)V001(.*)")
    print(this,next)

    if this then 
        --  There is still more packets left
        if this ~= "" then
            --  This is an empty packet
            table.insert(output, this)
        end

        if next ~= "" then
            --  There is more out there!
            doStep(next)
        end
    else
        --  We are the last survivor.
        table.insert(output, str)
    end
end

当然,这可以改进,但这应该是一个很好的起点。为了证明它有效,这个脚本:

doStep(input)
print(table.concat(output, "; "))

打印这个:

Packet1; Packet2ooo; aaandweredonehere

【讨论】:

    猜你喜欢
    • 2015-03-17
    • 2013-05-14
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多