When writing your own NSE scripts, you will sometimes want to refactor the code and make it available for others. The process of creating NSE libraries is pretty simple, and there are only a few things to keep in mind. NSE libraries are mostly in Lua, but other programming languages such as C and C++ can also be used.
Let's create a simple Lua library to illustrate how easy it is. First, remember that NSE libraries are stored in the /nselib/
directory in your Nmap data directory by default (see Chapter 3, NSE Data Files, to learn how to locate this directory). Start by creating a file named myfirstlib.lua
inside it. Inside our newly written file, place the following content:
local stdnse = require "stdnse" function hello(msg, name) return stdnse.format("Hello '%s',\n%s", msg, name) end
The first line declares the dependency with the stdnse
NSE library, which stores useful functions related to input handling:
local stdnse = require "stdnse"
The rest is a function declaration...