You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wireshark Lua dissector加载模块时debug.getinfo为nil问题求助

Solution for Getting Current Lua Script Directory in Wireshark 3.3 Dissectors

First, let's break down why your original debug.getinfo approach fails: Wireshark restricts access to the debug module in the plugin loading context for security and sandboxing reasons. That's why you're seeing the "attempt to index a nil value" error—debug simply isn't available when the dissector loads automatically on startup.

Here are three reliable alternative methods to get your module directory, tailored to work across different Wireshark versions and operating systems:

Wireshark provides standard Lua API functions to access its core directories, which works consistently across all supported systems. This is the most robust approach:

local function setup_module_path()
    -- Replace "modules" with your actual subdirectory name (if using one)
    local module_subdir = "modules"
    local sep = package.config:sub(1, 1) -- Get OS-specific path separator
    
    -- Get standard Wireshark plugin directories
    local personal_plugins = persconffile_path() .. sep .. "plugins"
    local global_plugins = programfile_path() .. sep .. "plugins"
    
    -- Build full paths to your module directory
    local personal_module_path = personal_plugins .. sep .. module_subdir
    local global_module_path = global_plugins .. sep .. module_subdir
    
    -- Add the existing path to package search order
    if Dir.exists(personal_module_path) then
        package.prepend_path(personal_module_path)
    elseif Dir.exists(global_module_path) then
        package.prepend_path(global_module_path)
    else
        -- Fallback to current directory if all else fails
        package.prepend_path("." .. sep .. module_subdir)
    end
end

-- Run the setup before requiring modules
setup_module_path()

-- Now you can safely require your modules
local my_protocol_utils = require("protocol_utils")
local my_message_parser = require("message_parser")

Why this works:

  • persconffile_path() returns the user's personal Wireshark config directory (e.g., ~/.config/wireshark on Linux, %APPDATA%\Wireshark on Windows)
  • programfile_path() returns the global Wireshark installation directory
  • Dir.exists() checks if the module directory exists before adding it to the path
  • Uses the OS-native path separator to avoid cross-platform issues

Method 2: Add Current Directory to Package Path (Simplest)

If your modules are in the same directory as your main dissector script, you can directly add the current directory to package.path:

-- Add current directory to package search path
local sep = package.config:sub(1, 1)
package.prepend_path("." .. sep)

-- Require your modules
local core_dissector = require("core_dissector")

Note:

This works best if you keep all your plugin files in the same folder. It's simple but less flexible if you use subdirectories for modules.

Method 3: Locate Your Main Script with package.searchpath

If you know the exact filename of your main dissector script (e.g., my_custom_protocol.lua), you can use package.searchpath to find its absolute path:

local main_script_name = "my_custom_protocol.lua"
local script_path = package.searchpath(main_script_name, package.path)

if script_path then
    local sep = package.config:sub(1, 1)
    local module_dir = script_path:match("(.*" .. sep .. ")")
    package.prepend_path(module_dir)
end

-- Require modules
local packet_parser = require("packet_parser")

Caveat:

This method breaks if the user renames your main script, so it's best for plugins where you control the filename distribution.


内容的提问来源于stack exchange,提问作者immnek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 21:57:32