NSE libraries in Lua are preferred, but the Nmap Scripting Engine also supports C/C++ modules via the Lua C API. This is only recommended if you require better performance or integrating an already existing project.
This recipe will teach you how to create an NSE library in C/C++.
Let's go through the process of creating a C library and accessing it with the Lua C API. Our module will only contain a single function that prints a message on screen:
- Create your library source and header files. C library file names must be prepended with the string
nse_
. For our library test, we will neednse_test.cc
andnse_test.h
. First, creatense_test.cc
and paste the following code:
extern "C" { #include "lauxlib.h" #include "lua.h" } #include "nse_test.h" static int hello_world(lua_State *L) { printf("Hello World From a C library\n"); return 1; } static const struct luaL_Reg testlib[] = { {"hello", hello_world...