Script template

An Nmap script is basically categorized into three sections, which are discussed here. We will use the script from https://svn.nmap.org/nmap/scripts/smtp-enum-users.nse as an example to define the data in these categories:

description = [[
<code>smtp-enum-users.methods={EXPN,RCPT,VRFY}</code>
]]
categories = {"auth","external","intrusive"}
author = "Duarte Silva <duarte.silva@serializing.me>"
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
dependencies = {"dependant script"}

Ajp

cassandra

Amqp

citrixxml

asn1

Comm

base32

Creds

base64

Cvs

Bin

Datafiles

Bit

Dhcp

Bitcoin

dhcp6

Bittorrent

Dns

Bjnp

Dnsbl

Brute

Dnssd

Eigrp

Drda

ftp

Eap

 

For reference, we can look at the script at https://svn.nmap.org/nmap/scripts/smtp-enum-users.nse to see how the libraries are defined:

local nmap = require "nmap"
local shortport = require "shortport"
local smtp = require "smtp"
local stdnse = require "stdnse"
local string = require "string"
local table = require "table"
local unpwdb = require "unpwdb"

These libraries have various functions defined in them, for which we can pass arguments using the following syntax: <function name>(arg1, arg2, arg3). For example, smtp.check_reply("MAIL", response).

The following is the rule used in the example script smtp-enum-users.nse:

portrule = shortport.port_or_service({ 25, 465, 587 },
{ "smtp", "smtps", "submission" })
action = function(host, port)
local status, result = go(host, port)
-- The go function returned true, lets check if it
-- didn't found any accounts.
if status and #result == 0 then
return stdnse.format_output(true, "Couldn't find any accounts")
end

Some of the libraries require the script to be in specific formats and must use the NSEDoc format. We will see how to fit the script into such a format in this recipe. In this recipe, we will have a look at creating a script to identify whether default Tomcat files are present on a remote host.