NSE scripts consist of two–five descriptive fields along with either a port or host rule defining when the script should be executed and an action block containing the actual script instructions. Values can be assigned to the descriptive fields just as you would assign any other Lua variables. Their names must be lowercase as shown in this section.
The description field describes what a script is testing
for and any important notes the user should be aware of. Depending on script complexity, the description may vary from a few sentences to a few paragraphs. The first paragraph should be a brief synopsis of the script function suitable for stand-alone presentation to the user. Further paragraphs may provide much more script detail.
The categories field defines one or
more categories to which a script belongs (see
the section called “Script Categories”). The categories are case-insensitive and may be specified in any order. They are listed in an array-style Lua table as in this example:
categories = {"default", "discovery", "safe"}
The author field contains the script authors' names and can also contain contact information (such as home page URLs). We no longer recommend including email addresses because spammers might scrape them from the nsedoc web site. This optional field is not used by NSE, but gives script authors their due credit or blame.
Nmap is a community project and we welcome all sorts of
code contributions, including NSE scripts. So if you write a
valuable script, don't keep it to yourself!
The optional license field helps ensure that we have
legal permission to distribute all the scripts which come with Nmap. All of those scripts
currently use the standard Nmap license
(described in the section called “Nmap Copyright and Licensing”). They include
the following line:
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
The Nmap license is similar to the GNU GPL. Script authors may
use a BSD-style license (no advertising clause) instead if they prefer
that.
The dependencies field is an array containing the
names of scripts that should run before this script. This is used when
one script can make use of the results of another. For example, most of
the smb-* scripts depend on
smb-brute,
because the accounts found by smb-brute may allow
the other scripts to get more information.
When we say “depend on”, we mean it in a loose sense. That is, a
script will still run despite missing dependencies. Given the
dependencies, the script will run after all the scripts listed in the
dependencies array. This is an example of the
dependencies table from
smb-os-discovery:
dependencies = {"smb-brute"}
The dependencies table is optional. NSE will assume
the script has no dependencies if the field is omitted.
Dependencies establish an internal ordering of scripts, assigning each
one a number called a “runlevel”[].
When
running your scripts you will see the runlevel (along with the total number of
runlevels) of each grouping of scripts run in NSE's output:
NSE: Script scanning 127.0.0.1.
NSE: Starting runlevel 1 (of 3) scan.
Initiating NSE at 17:38
Completed NSE at 17:38, 0.00s elapsed
NSE: Starting runlevel 2 (of 3) scan.
Initiating NSE at 17:38
Completed NSE at 17:38, 0.00s elapsed
NSE: Starting runlevel 3 (of 3) scan.
Initiating NSE at 17:38
Completed NSE at 17:38, 0.00s elapsed
NSE: Script Scanning completed.
Nmap uses the script rules to determine whether a script should be run
against a target. A script contains either a port
rule, which governs which ports of a target the scripts may
run against, or a host rule, which specifies that
the script should be run only once against a target IP and only if
the given conditions are met. A rule is a Lua function that returns
either true or false. The
script action is only performed if its rule
evaluates to true. Host rules accept a host
table as their argument and may test, for example, the IP address or
hostname of the target. A port rule accepts both host and port tables
as arguments for any TCP or UDP port in the
open,
open|filtered,
or unfiltered port states. Port rules generally test factors such as the port number, port state, or listening service name in deciding whether to run against a port. Example rules are shown in the section called “The Rule”.
The action is the heart of an NSE script. It contains all of the
instructions to be executed when the script's port or host rule
triggers. It is a Lua function which accepts the same arguments as the
rule and can return either nil or a string. If a string is returned by a service script, the string and script's filename are printed in the Nmap port table output. A string returned by a host script is printed below the port table. No output is produced if the
script returns nil. For an example of an NSE
action refer to the section called “The Mechanism”.