好记性不如铅笔头

C && C++, Lua, 编程, 网络通讯

使用LUA脚本来扩展tshark的功能

tshark是wireshark的命令行实现,可以在没有UI的情况下使用wireshark的全部功能。
参考文档【 https://www.wireshark.org/docs/man-pages/tshark.html
由于没有UI交互,那么我们应该如何使用tshark的lua脚本功能呢,这里笔记一种简单的实例。

该实例基于【使用LUA脚本来扩展wireshark的功能

新建一个hello_tshark.lua文件,内容如下:

input_ips = "255.255.255.255"

-- 判断指定元素value是否在数组tab中
local function is_include(value, tab)
    for k,v in ipairs(tab) do
      if v == value then
            return true
      end
    end
    return false
end

-- 将一个字符串分割为一个数组
local function split_string(input, delimiter)
    input = tostring(input)
    delimiter = tostring(delimiter)
    if(delimiter=='') then return false end
    local pos,arr = 0, {}
    for st,sp in function() return string.find(input, delimiter, pos, true) end do
        table.insert(arr, string.sub(input, pos, st - 1))
        pos = sp + 1
    end
    table.insert(arr, string.sub(input, pos))
    return arr
end

-- 支持多个IP地址输入,每个IP间使用','进行分割
-- 输入的IP数组
local input_ip_table = nil
input_ip_table = split_string(input_ips, ",")
local tap = Listener.new() 

function tap.packet(pinfo,tvb)
    if(not ( is_include(tostring(pinfo.src), input_ip_table) or is_include(tostring(pinfo.dst), input_ip_table) ) )  then
        return
       end
       content = string.format("idx:%d\t %s:%d ==>> %s:%d",pinfo.number, pinfo.src, pinfo.src_port, pinfo.dst, pinfo.dst_port)
       print(content)
end

function tap.draw()
end

function tap.reset()
end

以读取文件方式执行tshark,命令如下:

$ tshark -X lua_script:hello_tshark.lua -q -l -r 1.pcapng
idx:9	 0.0.0.0:68 ==>> 255.255.255.255:67
idx:10	 0.0.0.0:68 ==>> 255.255.255.255:67
idx:12	 0.0.0.0:68 ==>> 255.255.255.255:67
idx:13	 0.0.0.0:68 ==>> 255.255.255.255:67
idx:19	 10.42.0.225:5683 ==>> 255.255.255.255:5683
idx:20	 10.42.0.225:5683 ==>> 255.255.255.255:5683
。。。。。。
。。。。。。

 

发表评论

14 − 10 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据