Skip to content

Commit

Permalink
Merge pull request #2 from devanlai/lua-poc
Browse files Browse the repository at this point in the history
Add support for Lua dissectors
  • Loading branch information
dehydr8 committed Nov 8, 2023
2 parents 25227f5 + 45d23d4 commit 06e9cd1
Show file tree
Hide file tree
Showing 15 changed files with 886 additions and 9 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Cross-compiling Wireshark for emscripten/WASM isn't straightforward as it also d
* `0004-export-wireshark-common.patch` - Expose some headers and objects that are not part of `epan`
* `0005-force-data-dir.patch` - Force `/wireshark` as the data directory. It is needed for loading preferences, profiles and color filters
* `0006-threadless-registration.patch` - Makes dissector registrations threadless
* `0007-export-lrexlib.patch` - Expose `lrexlib`, which is really a private dependency, but which isn't linked properly if not exported.

## Usage
The Wiregasm `Dissect Session` implementation is effectively a tiny subset of `sharkd` APIs.
Expand Down Expand Up @@ -84,5 +85,19 @@ sess.delete();
wg.destroy();
```

To add custom Lua dissectors, add your dissectors to the plugins directory
before initializing wiregasm:

```javascript
// read lua file from local FS
const dissector_data = await fs.readFile("path/to/dissector.lua");

// write lua file to the virtual emscripten FS plugin directory
wg.FS.writeFile("/plugins/dissector.lua", dissector_data)

// initialize and use wiregasm as usual
wg.init();
```

## License
Wiregasm is a derivative work of the [Wireshark](https://github.com/wireshark/wireshark) project, hence it is licensed under the same [GNU GPLv2](LICENSE) license.
35 changes: 32 additions & 3 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ MESON_SETUP = meson setup $(BUILD_DIR) $< --default-library static --prefix $(PR
MESON_BUILD = meson compile -C $(BUILD_DIR) && meson install -C $(BUILD_DIR)

mostlyclean:
rm -rf c-ares ffi gcrypt glib gpg-error pcre wireshark
rm -rf .c-ares .ffi .gcrypt .glib .gpg-error .pcre .wireshark
rm -rf c-ares ffi gcrypt glib gpg-error lua pcre wireshark
rm -rf .c-ares .ffi .gcrypt .glib .gpg-error .lua .pcre .wireshark
rm -rf .sum-*

clean: mostlyclean
Expand Down Expand Up @@ -253,6 +253,33 @@ glib: glib-$(GLIB_MINOR_VERSION).tar.xz .sum-glib
+$(MESON_BUILD)
touch $@

#
# LUA
#
LUA_VERSION := 5.2.4
LUA_URL := https://www.lua.org/ftp/lua-$(LUA_VERSION).tar.gz
LUA_CHECKSUM := cd77148aba4b707b6c159758b5e8444e04f968092eb98f6b4c405b2fb647e709370d5a8dcf604176101d3407e196a7433b5dcdce4fe9605c76191d3649d61a8c
# Borrowed from https://github.com/ysugimoto/webassembly-lua/blob/master/Dockerfile
LUA_CONF := CC='emcc -s WASM=1'

$(TARBALLS)/lua-$(LUA_VERSION).tar.gz:
$(call download,$(LUA_URL))

.sum-lua: lua-$(LUA_VERSION).tar.gz
$(call checksum,$(LUA_CHECKSUM),$<)
touch $@

lua: lua-$(LUA_VERSION).tar.gz .sum-lua
$(CLEANUP)
tar xzfo $<
$(MOVE)

.lua: lua
echo "[+] Building Lua"
$(MAKE) -C $< clean
emmake make -C $< generic INSTALL_TOP=$(PREFIX) install $(LUA_CONF)
touch $@

#
# Wireshark (epan)
#
Expand Down Expand Up @@ -303,9 +330,11 @@ wireshark: wireshark-v$(WIRESHARK_VERSION).tar.bz2 .sum-wireshark
$(APPLY) $(PATCHES)/wireshark/0004-export-wireshark-common.patch
$(APPLY) $(PATCHES)/wireshark/0005-force-data-dir.patch
$(APPLY) $(PATCHES)/wireshark/0006-threadless-registration.patch
$(APPLY) $(PATCHES)/wireshark/0007-export-lrexlib.patch
$(APPLY) $(PATCHES)/wireshark/0008-export-wslua-headers.patch
$(MOVE)

.wireshark: wireshark .c-ares .gcrypt .glib
.wireshark: wireshark .c-ares .gcrypt .glib .lua
echo "[+] Building Wireshark"
$(CMAKECLEAN)
emcmake cmake -G Ninja -S $< -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:STRING=$(PREFIX) -DCMAKE_FIND_ROOT_PATH:STRING=$(PREFIX) $(WIRESHARK_CONF)
Expand Down
2 changes: 2 additions & 0 deletions lib/wiregasm/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ using namespace emscripten;
EMSCRIPTEN_BINDINGS(Wiregasm)
{
emscripten::function("init", &wg_init);
emscripten::function("reloadLuaPlugins", &wg_reload_lua_plugins);
emscripten::function("destroy", &wg_destroy);
emscripten::function("getColumns", &wg_get_columns);
emscripten::function("upload", &wg_upload_file, allow_raw_pointers());
emscripten::function("checkFilter", &wg_check_filter);
emscripten::function("getUploadDirectory", &wg_get_upload_dir);
emscripten::function("getPluginsDirectory", &wg_get_plugins_dir);
}

EMSCRIPTEN_BINDINGS(DissectSession)
Expand Down
5 changes: 5 additions & 0 deletions lib/wiregasm/ext/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ if (!Module["handleStatus"])
console.log(type, status);
};

// initialize FS with directories
Module["onRuntimeInitialized"] = () => {
Module.FS.mkdir("/plugins");
Module.FS.mkdir("/uploads");
};
4 changes: 3 additions & 1 deletion lib/wiregasm/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ foreach d : [
'gobject-2.0',
'gmodule-2.0',
'cares',
'gcrypt'
'gcrypt',
'lua',
'lrexlib'
]
dependencies += [ cxx.find_library(d, dirs: [ contrib_lib_dir ], required: true) ]
endforeach
Expand Down
21 changes: 19 additions & 2 deletions lib/wiregasm/wiregasm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "wiregasm.h"
#include "lib.h"
#include <wsutil/privileges.h>
#include <epan/wslua/init_wslua.h>

using namespace std;

Expand All @@ -21,6 +22,11 @@ string wg_get_upload_dir()
return string(UPLOAD_DIR);
}

string wg_get_plugins_dir()
{
return string(get_plugins_dir());
}

vector<string> wg_get_columns()
{
vector<string> v;
Expand Down Expand Up @@ -55,6 +61,19 @@ string wg_upload_file(string name, int buffer_ptr, size_t size)
return ret;
}

bool wg_reload_lua_plugins()
{
if (!wg_initialized) {
return false;
}

wslua_reload_plugins(NULL, NULL);

on_status(INFO, "Reload complete!");

return true;
}

bool wg_init()
{
if (wg_initialized)
Expand All @@ -65,8 +84,6 @@ bool wg_init()

on_status(INFO, "Initializing..");

g_mkdir_with_parents(UPLOAD_DIR, 0755);

init_process_policies();
relinquish_special_privs_perm();

Expand Down
2 changes: 2 additions & 0 deletions lib/wiregasm/wiregasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ struct CheckFilterResponse
// globals

bool wg_init();
bool wg_reload_lua_plugins();
void wg_destroy();
string wg_upload_file(string name, int buffer_ptr, size_t size);
vector<string> wg_get_columns();
CheckFilterResponse wg_check_filter(string filter);
string wg_get_upload_dir();
string wg_get_plugins_dir();

class DissectSession
{
Expand Down
4 changes: 2 additions & 2 deletions patches/wireshark/0005-force-data-dir.patch
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ index a55086ccbb..6ab8c0585d 100644
-add_definitions(-DPLUGIN_DIR=\"${CMAKE_INSTALL_PREFIX}/${PLUGIN_INSTALL_LIBDIR}\")
-add_definitions(-DEXTCAP_DIR=\"${CMAKE_INSTALL_PREFIX}/${EXTCAP_INSTALL_LIBDIR}\")
-add_definitions(-DDATA_DIR=\"${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}\")
+add_definitions(-DPLUGIN_DIR=\"/nonexistent/plugins\")
+add_definitions(-DEXTCAP_DIR=\"/nonexistent/extcap\")
+add_definitions(-DPLUGIN_DIR=\"/plugins\")
+add_definitions(-DEXTCAP_DIR=\"/extcap\")
+add_definitions(-DDATA_DIR=\"/wireshark\")

add_subdirectory(wmem)
Expand Down
34 changes: 34 additions & 0 deletions patches/wireshark/0007-export-lrexlib.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
From 56160a1aeabb9513c2e93cd49c40f0c56514c58d Mon Sep 17 00:00:00 2001
From: Devan Lai <[email protected]>
Date: Sun, 5 Nov 2023 10:31:35 -0800
Subject: [PATCH] Export lrexlib

---
epan/wslua/lrexlib/CMakeLists.txt | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/epan/wslua/lrexlib/CMakeLists.txt b/epan/wslua/lrexlib/CMakeLists.txt
index 1f5f13f278..a60b3f916f 100644
--- a/epan/wslua/lrexlib/CMakeLists.txt
+++ b/epan/wslua/lrexlib/CMakeLists.txt
@@ -24,3 +24,17 @@ add_compile_definitions(
VERSION=\"2.9.1\"
PCRE2_CODE_UNIT_WIDTH=8
)
+
+set_target_properties(lrexlib PROPERTIES
+ PREFIX "lib"
+ LINK_FLAGS "${WS_LINK_FLAGS}"
+ FOLDER "DLLs"
+ INSTALL_RPATH "${LIBRARY_INSTALL_RPATH}"
+)
+
+install(TARGETS lrexlib
+ EXPORT WiresharkTargets
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
--
2.34.1

31 changes: 31 additions & 0 deletions patches/wireshark/0008-export-wslua-headers.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
From 1dec7120a409696d1e975a1217958526ce6fc411 Mon Sep 17 00:00:00 2001
From: Osama Khalid <[email protected]>
Date: Tue, 7 Nov 2023 11:54:03 -0500
Subject: [PATCH] Export init_wslua.h header

---
epan/wslua/CMakeLists.txt | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/epan/wslua/CMakeLists.txt b/epan/wslua/CMakeLists.txt
index 39b23d3c5c..10216cc205 100644
--- a/epan/wslua/CMakeLists.txt
+++ b/epan/wslua/CMakeLists.txt
@@ -9,6 +9,14 @@

add_subdirectory(lrexlib)

+set(WSLUA_PUBLIC_HEADERS
+ init_wslua.h
+)
+
+install(FILES ${WSLUA_PUBLIC_HEADERS}
+ DESTINATION "${PROJECT_INSTALL_INCLUDEDIR}/epan/wslua"
+)
+
set(WSLUA_MODULES
${CMAKE_CURRENT_SOURCE_DIR}/lua_bitop.c
${CMAKE_CURRENT_SOURCE_DIR}/wslua_address.c
--
2.34.1

Loading

0 comments on commit 06e9cd1

Please sign in to comment.