Usage and Examples
While NSE has a complex implementation for efficiency, it is
strikingly easy to use. Simply specify
-sC
to enable the most common scripts. Or specify the
--script
option to choose your own scripts to
execute by providing categories, script file names, or the name of
directories full of scripts you wish to execute. You can customize
some scripts by providing arguments to them via the
--script-args
and --script-args-file
options.
The --script-help
shows a description of what each selected script does.
The two remaining options,
--script-trace
and --script-updatedb
,
are generally only used for script debugging and development. Script scanning is also included as part of the -A
(aggressive scan) option.
Script scanning is normally done in combination with a port scan,
because scripts may be run or not run depending on the port states
found by the scan. With the -sn
option it is
possible to run a script scan without a port scan, only host
discovery. In this case only host scripts will be eligible to run.
To run a script scan with neither a host discovery nor a port scan,
use the -Pn -sn
options together with
-sC
or --script
. Every host will
be assumed up and still only host scripts will be run. This
technique is useful for scripts like
whois-ip
that only use the remote system's address and don't require it to be
up.
Scripts are not run in a sandbox and thus could accidentally or maliciously damage your system or invade your privacy. Never run scripts from third parties unless you trust the authors or have carefully audited the scripts yourself.
Script Categories
NSE scripts define a list of categories they belong to.
Currently defined categories are
auth
,
broadcast
,
brute
,
default
.
discovery
,
dos
,
exploit
,
external
,
fuzzer
,
intrusive
,
malware
,
safe
,
version
, and
vuln
.
Category names are not case sensitive. The following list describes each category.
-
auth
-
broadcast
Scripts in this category typically do discovery of hosts not listed on the command line by broadcasting on the local network. Use the
newtargets
script argument to allow these scripts to automatically add the hosts they discover to the Nmap scanning queue.-
brute
-
default
-
discovery
-
dos
-
exploit
-
external
Scripts in this category may send data to a third-party database or other network resource. An example of this is
whois-ip
, which makes a connection to whois servers to learn about the address of the target. There is always the possibility that operators of the third-party database will record anything you send to them, which in many cases will include your IP address and the address of the target. Most scripts involve traffic strictly between the scanning computer and the client; any that do not are placed in this category.-
fuzzer
-
intrusive
-
malware
-
safe
-
version
-
vuln
- Prerule scripts
These scripts run before any of Nmap's scan phases, so Nmap has not collected any information about its targets yet. They can be useful for tasks which don't depend on specific scan targets, such as performing network broadcast requests to query DHCP and DNS SD servers. Some of these scripts can generate new targets for Nmap to scan (only if you specify the newtargets NSE argument). For example, dns-zone-transfer can obtain a list of IPs in a domain using a zone transfer request and then automatically add them to Nmap's scan target list. Prerule scripts can be identified by containing a
prerule
function (see the section called “Rules”).- Host scripts
Scripts in this phase run during Nmap's normal scanning process after Nmap has performed host discovery, port scanning, version detection, and OS detection against the target host. This type of script is invoked once against each target host which matches its
hostrule
function. Examples are whois-ip, which looks up ownership information for a target IP, and path-mtu which tries to determine the maximum IP packet size which can reach the target without requiring fragmentation.- Service scripts
These scripts run against specific services listening on a target host. For example, Nmap includes more than 15 http service scripts to run against web servers. If a host has web servers running on multiple ports, those scripts may run multiple times (one for each port). These are the most commong Nmap script type, and they are distinguished by containing a
portrule
function for deciding which detected services a script should run against.- Postrule scripts
These scripts run after Nmap has scanned all of its targets. They can be useful for formatting and presenting Nmap output. For example, ssh-hostkey is best known for its service (portrule) script which connects to SSH servers, discovers their public keys, and prints them. But it also includes a postrule which checks for duplicate keys amongst all of the hosts scanned, then prints any that are found. Another potential use for a postrule script is printing a reverse-index of the Nmap output—showing which hosts run a particular service rather than just listing the services on each host. Postrule scripts are identified by containing a
postrule
function.Many scripts could potentially run as either a prerule or postrule script. In those cases, we recommend using a prerule for consistency.
Command-line Arguments
These are the five command-line arguments specific to script scanning:
-
-sC
-
--script
<filename>
|<category>
|<directory>
/|<expression>
[,...] File and directory names may be relative or absolute. Absolute names are used directly. Relative paths are searched for in the
scripts
subdirectory of each of the following places until found:--datadir
$NMAPDIR
~/.nmap
(not searched on Windows)
(only on Windows)<APPDATA>
\nmapthe directory containing the nmap
executablethe directory containing the nmap
executable, followed by../share/nmap
(not searched on Windows)NMAPDATADIR
(not searched on Windows)the current directory. See the section called “Script Selection” for examples and a full explanation of the
--script
option.Nmap scripts are stored in a
scripts
subdirectory of the Nmap data directory by default (see Chapter 14, Understanding and Customizing Nmap Data Files). For efficiency, scripts are indexed in a database stored inscripts/script.db
, which lists the category or categories in which each script belongs. The argumentall
will execute all scripts in the Nmap script database, but should be used cautiously since Nmap may contain exploits, denial of service attacks, and other dangerous scripts.-
--script-args
<args>
Provides arguments to the scripts. See the section called “Arguments to Scripts” for a detailed explanation.
-
--script-args-file
<filename>
This option is the same as
--script-args
except that you pass the arguments in a file rather than on the command-line. See the section called “Arguments to Scripts” for a detailed explanation.-
--script-help
<filename>
|<category>
|<directory>
|<expression>
|all[,...] Shows help about scripts. For each script matching the given specification, Nmap prints the script name, its categories, and its description. The specifications are the same as those accepted by
--script
; so for example if you want help about thessl-enum-ciphers
script, you would run nmap --script-help ssl-enum-ciphers. A sample of script help is shown in Example 9.2, “Script help”.If the
-oX
option is used, an XML representation of the script help will be written to the given file.-
--script-trace
-
--script-updatedb
Some other Nmap options have effects on script scans. The most
prominent of these is
-sV
.
A version scan automatically executes
the scripts in the
version
category.
The scripts
in this category are slightly different from other scripts because their
output blends in with the version scan results and they do not produce any
script scan output to the screen. If the
-oX
option is used, typical script output will still be available in the
XML output file.
Another option which affects the scripting engine is
-A
.
The aggressive Nmap mode implies
the -sC
option.
Script Selection
The --script
option takes a comma-separated list
of categories, filenames, and directory names. Some simple
examples of its use:
- nmap --script default,safe
Loads all scripts in the
default
andsafe
categories.- nmap --script smb-os-discovery
Loads only the
smb-os-discovery
script. Note that the.nse
extension is optional.- nmap --script default,banner,/home/user/customscripts
Loads the script in the
default
category, thebanner
script, and all.nse
files in the directory/home/user/customscripts
.
When referring to scripts from script.db
by
name, you can use a shell-style ‘*
’
wildcard.
- nmap --script "http-*"
Loads all scripts whose name starts with
http-
, such ashttp-auth
andhttp-open-proxy
. The argument to--script
had to be in quotes to protect the wildcard from the shell.
More complicated script selection can be done using the
and
, or
, and
not
operators to build Boolean expressions. The
operators have the same
precedence
as in Lua: not
is the highest, followed by
and
and then or
. You can
alter precedence by using parentheses. Because expressions contain
space characters it is necessary to quote
them.
- nmap --script "not intrusive"
Loads every script except for those in the
intrusive
category.- nmap --script "default or safe"
This is functionally equivalent to nmap --script "default,safe". It loads all scripts that are in the
default
category or thesafe
category or both.- nmap --script "default and safe"
Loads those scripts that are in both the
default
andsafe
categories.- nmap --script "(default or safe or intrusive) and not http-*"
Loads scripts in the
default
,safe
, orintrusive
categories, except for those whose names start withhttp-
.
Names in a Boolean expression may be a category, a filename from
script.db
, or all
. A name
is any sequence of characters not containing
‘
’,
‘,
’,
‘(
’,
‘)
’, or
‘;
’, except for the sequences
and
, or
, and
not
, which are operators.
Arguments to Scripts
Arguments may be passed to NSE scripts using the
--script-args
option. The arguments describe a table of
key-value pairs and possibly array values. The arguments are provided to
scripts as a table in the registry called
nmap.registry.args
, though they are normally accessed through the stdnse.get_script_args
function.
The syntax for script arguments is similar to Lua's table constructor
syntax. Arguments are a comma-separated list of
name=value
pairs. Names and values may be strings not
containing whitespace or the characters
‘{
’,
‘}
’,
‘=
’, or
‘,
’.
To include one of these characters in a string, enclose the string in
single or double quotes. Within a quoted string,
‘\
’ escapes a quote. A backslash is only
used to escape quotation marks in this special case; in all other cases a
backslash is interpreted literally.
Values may also be tables enclosed in {}
, just as in
Lua. A table may contain simple string values, for example a list of proxy
hosts; or more name-value pairs, including nested tables.
Script arguments are often qualified with the relevant
script name so that a user doesn't unintentionally affect multiple
scripts with a single generic name. For example, you can set
the timeout for responses to the
broadcast-ping
script (and only that script)
by setting broadcast-ping.timeout
to the
amount of time you're willing to wait. Sometimes,
however, you want a script argument applied more widely. If you
remove the qualification and specify just
timeout=250ms
, you will be setting the value
for more than a dozen scripts in addition to
broadcast-ping
. You can even combine
qualified and unqualified arguments, and the most specific match
takes precedence. For example, you could specify
rlogin-brute.timeout=20s,timeout=250ms
. In
that case, the timeout will be 20 seconds for the
rlogin-brute
script, and 250 milliseconds for all other
scripts which support this variable
(broadcast-ping
,
lltd-discovery
, etc.)
Rather than pass the arguments on the command line with
--script-args
, you may store them in a file
(separated by commas or newlines) and specify just the file name
with --script-args-file
. Options specified
with --script-args
on the command-line take
precedence over those given in a file. The filename may be
given as an absolute path or relative to Nmap's usual
search path (NMAPDIR, etc.)
Here is a typical Nmap invocation with script arguments:
nmap -sC --script-args 'user=foo,pass=",{}=bar",paths={/admin,/cgi-bin},xmpp-info.server_name=localhost'
Notice that the script arguments are surrounded in single quotes. For the Bash shell, this prevents the shell from interpreting the double quotes and doing automatic string concatenation. Naturally, different shells may require you to escape quotes or to use different quotes. See your relevant manual. The command results in this Lua table:
nmap.registry.args = { user = "foo", pass = ",{}=bar", paths = { "/admin", "/cgi-bin" }, xmpp-info.server_name="localhost" }
While you could access the values directly from nmap.registry.args
, it is normally better to use the stdnse.get_script_args
function like this:
local server_name = stdnse.get_script_args("xmpp-info.server_name")
All script arguments share a global namespace, the
nmap.registry.args
table. For this reason, short or
ambiguous names like user
are not recommended. Some
scripts prefix their arguments with their script name, like
smtp-open-relay.domain
.
Arguments used by libraries, which can
affect many scripts, usually have names beginning with the name of the
library, like smbuser
and
creds.snmp
.
The online NSE Documentation Portal at https://nmap.org/nsedoc/
lists the arguments that each script
accepts, including any library arguments that may influence the script.
Complete Examples
- nmap -sC example.com
A simple script scan using the default set of scripts.
- nmap -sn -sC example.com
A script scan without a port scan; only host scripts are eligible to run.
- nmap -Pn -sn -sC example.com
A script scan without host discovery or a port scan. All hosts are assumed up and only host scripts are eligible to run.
- nmap --script smb-os-discovery --script-trace example.com
- nmap --script snmp-sysdescr --script-args creds.snmp=admin example.com
- nmap --script mycustomscripts,safe example.com
Execute all scripts in the
mycustomscripts
directory as well as all scripts in thesafe
category.