我的目标是使用我在 Wireshark 的数据包详细信息窗口中看到的字段进行自动捕获文件分析。我使用了 tshark 和一个基于 Lua 示例创建的 Lua 脚本。
我用于测试单个帧文件作为输入。这是我在 Windows 命令行中使用的脚本和 tshark 参数:
tshark.exe -q -r ecat_single.pcapng -X lua_script:field_extractor.lua
field_extractor 脚本:
-- this is our tap
local ecatTap = Listener.new();
local function remove()
-- this way we remove the listener that otherwise will remain running indefinitely
ecatTap:remove();
end
-- calling tostring() on random FieldInfo's can cause an error, so this func handles it
local function getstring(finfo)
local ok, val = pcall(tostring, finfo)
if not ok then val = "(unknown)" end
return val
end
-- this function will be called once for each packet
function ecatTap.packet(pinfo,tvb,tapinfo)
--
local fields = { all_field_infos() }
--
for ix, finfo in ipairs(fields) do
--
-- The name and value of field will be printed unconditionally.
print("[" .. ix .. "] " .. finfo.name .. " = " .. getstring(finfo) )--.. "\n")
-- Here follow operations depending on the finfo value.
if finfo.name == "ecat.cmd" then
if finfo.value == 4 then
print("FPRD command has found!")
-- ...
end
end
end
end
该脚本的输出:
[1] esl = 01:01:05:10:00:00:80:20:ac:60:64:2d:00:00:00:00
[2] eth = 00:e0:f4:2d:de:66:10:70:05:01:00:00:88:a4
[3] ecatf = 10:10
[4] ecat = 04:16:01:01:0c:08:04:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
[5] ecat.subframe.pad_bytes = 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
all_field_infos() 函数返回的字段不包含我在“数据包详细信息”窗口中可以看到的信息。数据在那里,但似乎没有被剖析。 all_field_infos() 的描述指出,仅收集底层代码填充的字段。在我尝试使用 -V 命令行选项(它打印数据包详细信息)之前,我并不完全清楚这意味着什么。
使用此选项,我可以访问达到目的所需的所有字段,但将所有数据包信息打印到命令行控制台。分析的捕获文件可能是大量文本数据,我无法通过 -q/-Q 选项静音。
我的问题是如何强制 all_field_infos() 函数为我提供所有字段而不淹没命令窗口?
仔细阅读相关文档找到了解决方案。侦听器的新方法具有可选参数,其中之一会影响底层字段的生成。
Listener.new([tap], [filter], [allfields])
所有字段(可选)
是否生成所有字段。默认为 false。注意:这会影响性能。
上面脚本中需要修改的内容:
local ecatTap = Listener.new(nil, nil, true)
现在 GUI 的“数据包详细信息”窗口中的所有字段均已生成。然而我不知道这个参数,我把这个问题留在这里,因为我没有在示例中找到类似的东西,它可能对其他人有帮助。