0
votes

I'm currently writing some dissector for Wireshark in Lua. The Lua code has become quite large. Because of that I'm splitting it up into multiple files (modules). I got that working. By the way my goal is that the user just needs to copy the files into the plugins directory so that the dissector is automatically loaded every time Wireshark is started.

Now, to gain access to the other files from the "main file" I need to do this:

package.prepend_path(".\plugins\3.3\modulesDir") local mymodule= require "module"

This works fine, but it has some disadvantages. Most importantly if a user uses a different version of Wireshark I need to change the path in the Lua code. Same if it's a different directory (Linuy, Mac OS).

To get around this I did some research on how to get the path of the current Lua file and came up with this:

local moduleDir = debug.getinfo(2, "S").source:sub(2) moduleDir = moduleDir:match("(.*[/\\])")

This works platform indepedently, so it looks to be the perfect solution for what I want. If I execute this using Wireshark > Tools > Lua > Evaluate it work perfectly fine.
BUT: If I do it in the Lua file (which is my dissector) then I get the error "attempt to index a nil value". I tried various different versions of this line but it always appears that the debug table is nil. I'm using Wireshark version 3.3.

Has anyone an idea how to get it running? Or a different approach to getting the directory where the Lua file is in? Thanks in advance.

2
I saw some suggestions to use arg[0], but that doesn't work. I think because the script is called automatically because it's in the Wireshark's plugin directory. - immnek

2 Answers

0
votes

If you are looking for "global configuration directory" then you can use Dir.global_config_path(). Here you have that init.lua is in this path, and here lua function dtails.

0
votes

On Windows, I have my Lua files in the "Personal Lua Plugins" directory, which when you look at Wireshark's "Help -> About Wireshark -> Folders" dialog, is just %APPDATA%\Wireshark\plugins. So, perhaps you can just move your folder from path\to\plugins\3.3\modulesDir to just path\to\plugins\modulesDir?

I believe you will then only require:

package.prepend_path("modulesDir")

And this will allow your Lua dissector and modules to work not only with the Wireshark 3.3 development version, but also future releases as well. And if your dissector can't work with older versions of Wireshark for some reason, you can always do something like:

if get_version() < "3.3" then
    return
end

Lastly, have a look at how Hadriel Kaplan solved this for his protobuf.lua dissector, where his required modules are in the "modules" directory. It's basically as I've described above. See: https://github.com/128technology/protobuf_dissector