This wiki has been migrated to https://gitlab.com/wireshark/wireshark/-/wikis/home and is now deprecated. Please use that site instead.
Differences between revisions 32 and 33
Revision 32 as of 2006-12-22 06:06:04
Size: 6885
Editor: LuisOntanon
Comment:
Revision 33 as of 2006-12-23 04:08:00
Size: 6920
Editor: LuisOntanon
Comment:
Deletions are marked like this. Additions are marked like this.
Line 88: Line 88:
 Examples of generic Lua code can be found in [http://lua-users.org/wiki/SampleCode The Sample Code] page of Lua-Users wiki. Examples of generic Lua code can be found in [http://lua-users.org/wiki/SampleCode The Sample Code] page of Lua-Users wiki.
Line 90: Line 90:
XXX -- Wireshark examples still missing Examples of wireshark specific scripts can be found in [:Lua/Examples: here]

Lua

Lua's been added to Wireshark as a language for prototyping and scripting.

For more information about Lua refer to [http://www.lua.org Lua's main site], there you can find its [http://www.lua.org/manual/5.0/manual.html Reference Manual] and a [http://www.lua.org/pilhttp://www.lua.org/pil book] that describes the language. There is also [http://lua-users.org/wiki/ The lua-users wiki].

beware the GPL

Wireshark is released under [http://www.gnu.org/licenses/gpl.html GPL] so every derivative work based on Wireshark must be released under the terms of the GPL.

/!\ Even if the code you write in Lua does not need to be GPL'ed. The code written in Lua that uses bindings to Wireshark must be distributed under the GPL terms. see the [http://www.gnu.org/licenses/gpl-faq.html#TOCIfInterpreterIsGPL GPL FAQ] for more info /!\

There is at least one Wireshark author that will not allow to distribute derivative work under different terms. To distribute Lua code that uses Wireshark's bindings under different terms would be a clear violation of the GPL.

If it isn't clear to you what the GPL is and how it works please consult your laywer.

Lua in Wireshark

Lua can be used to write [:Lua/Dissectors: dissectors], post-dissectors and [:Lua/Taps: taps].

Although it's possible to write [:Lua/Dissectors: dissectors] in Lua, Wireshark dissectors are written in C, as C is several times faster than Lua. Lua is ok for prototyping dissectors, during Reverse Engineering you can use your time for finding out how things work instead of compiling and debugging your C dissector.

Post-dissectors are dissectors meant to run after every other dissector has run. They can add items the dissection tree so they can be used to create your own extensions to the filtering mechanism.

[:Lua/Taps: Taps] are used to collect information after the packet has been dissected.

Getting Started

/!\ Note: since 0.99.4 (possibly before), Lua has been built-in to Wireshark. To check if it is included, see the Help->About page. However, by default it is not enabled. To enable Lua, edit the file init.lua - see below (How Lua fits into Wireshark) for details - this will add a Tools Menu with a Lua Console and Lua Evaluate items. /!\

Lua is available as a plugin since Wireshark 0.99.0 but it won't be installed by default. If you want to use it make sure you install it.

If you want to install the lua plugin:

  • *NIX: configure --with-lua=<lua_directory>

  • Windows: select the lua plugin at the setup.exe

To check if it is already installed go to Help->About Wireshark go to the Plugins tab and look for it.

attachment:about.png

To test if it works write a simple Lua script like:

-- hello.lua
-- Lua's implementation of D. Ritchie's hello world program.

    print("hello world!")

Name this script hello.lua and place it in the current directory.

run tshark -X lua_script:hello.lua from the command prompt and you should see something like:

$ tshark -X lua_script:hello.lua
hello world!
Capturing on en0
1   0.000000 111.123.234.55 -> 111.123.234.255 NBNS Name query NB XXX.COM<00>

If you can read "hello world!" in the first line after you run tshark Lua is ready to go.

/!\ Please note: this won't work with the Win32 version of Wireshark, as the console is opened after the lua engine is loaded (or the console it not opened at all, depending on the Preferences)! XXX - We should fix this somehow!

How Lua fits into Wireshark

Every time wireshark starts it will search for a script called init.lua located in the global configuration directory of Wireshark. If Wireshark finds this file it will run the script.

Once <global config dir>/init.lua has run that there are two variables that tell wireshark whether to continue looking for scripts.

If the first init script sets the variable disable_lua to true Wireshark will stop reading scripts and shut down the lua engine right after the script was run.

If Wireshark is running suexec (i.e. as root but launched by another user) it will check if the variable run_user_scripts_when_superuser is set to true before loading any further scripts.

Once this first script was run Wireshark will continue running <personal config dir>/init.lua and then all scripts passed with the -X lua_script:xxx.lua command line option in the given order.

All these scripts will be run before packets are read, at the end of the dissector registration process. So, what you have to do is to register a series of functions that will be called while processing packets.

Wireshark's Lua API

The automatically generated documentation to the API can be found here: attachment:wsluarm.html

Examples

Examples of generic Lua code can be found in [http://lua-users.org/wiki/SampleCode The Sample Code] page of Lua-Users wiki.

Examples of wireshark specific scripts can be found in [:Lua/Examples: here]

Discussion

This page is a good start. However, some things remain unclear:

  • How to install/use lua?
  • What's the difference between a post-dissector and a tap

That's what it is, a start... I think it's soon to complete as things are changing as I go ahead.

  • I do not have yet a clear idea on how Lua will be invoked from wireshark.
    • So far as a temporary solution:
      • it either looks for ~/.wireshark/init.lua and loads that script
      • or looks for a file pointed by the environment variable WIRESHARK_LUA_INIT.
    • It will change as soon as I (or someone else, proposals are welcome) come out with a good way to do it.
  • Tap vs. Postdissector
    • a post dissector is it's like a normal dissector called every time a tree needs to be generated for a frame it just gets called at last.
    • a tap is run once after the first dissection of a packet and it has no access to the tree, it cannot add fields (a postdissector can but it will be called every time a tree needs to be generated).
      • -- Luis E. G. O.

Lua (last edited 2020-03-31 15:55:54 by GeraldCombs)