Mastering The Nmap Scripting Engine - Sample Chapter
Mastering The Nmap Scripting Engine - Sample Chapter
$ 44.99 US
27.99 UK
P U B L I S H I N G
ee
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Sa
m
Acknowledgments
As always, I would like to dedicate this book to a lot of special people who have helped
me get where I am.
Special thanks to Fyodor for mentoring me during Google's Summer of Code and giving
me the opportunity to join the Nmap project.
A big thanks to the development team: David Fifield, Ron Bowes, Patrik Karlsson,
Tom Sellers, Patrick Donelly, Daniel Miller, Brendan Coles, Henri Doreau, Toni Routto,
Djalal Harouni, Vlatko Kosturjak, Kris Katterjohn, Martin Holst Swende, Jacek
Wielemborek, and Luis Martin, from whom I have learned a lot.
Special thanks to my father, Dr. Paulino Caldern Medina, who is no longer with us but
will be greatly missed. Thanks to my mother, Edith, and brothers, Yael and Omar, who
have always been supportive and given nothing but love.
A big thanks goes to Martha Moguel, without whom this book would have been better
while everything else would have been worse. Thank you for always being there for me.
I will always love you.
Special thanks to the rest of the Websec ninjas: Lenin "Alevsk" Huerta, Luis "Sinnet"
Colunga, Luis "Kazcinski" Ramirez, Roberto "LightOS" Salgado, and Pedro
"Hkm" Joaquin.
A big thanks to my friends from USA, Colombia, Mexico, Cozumel, and Canada. It is
impossible to list all of you, but know that I appreciate all your love and support. You are
always in my heart.
Greetings to my b33rcon friends: Carlos Ayala, Marcos Schejtman, Luis Castaeda,
Diego Bauche, and Alejandro Hernandez.
This chapter will introduce you to NSE, covering several topics from installation and
development environment setup to advanced usage tips. If you are familiar with the
following topics, you may skip this chapter:
Scanning phases
NSE applications
If you are not familiar with NSE already, this chapter will get you prepared for what
is coming in the next chapters. For those with some experience, I still recommend
going through this chapter as I'm including advanced tips related to script selection
and usage. Fire up your terminals and let's get to work.
Installing Nmap
Nmap binaries for all major platforms can be found at the official website, at
http://nmap.org/download.html. A lot of distributions also offer official packages.
However, if you want the latest features and NSE scripts, you need to work with the
development branch. The code in this branch is more stable than the name implies,
as the developers make sure their code is working before submitting it to this branch.
By always running a copy from the development branch, you also always have the
latest bug fixes.
Chapter 1
Once the Subversion client is installed, we grab the development branch from the
official repositories with the following command:
$svn co https://svn.nmap.org/nmap
The preceding command downloads the latest revision of the development branch
into a new directory in your current directory. We will refer to this folder as your
working copy. Before compiling, you may need additional tools and libraries such
as OpenSSL. Make sure you have all the requirements installed by running the
following command:
#apt-get install make g++ libssl-dev autoconf
Now we can compile and install Nmap. Go to the nmap directory that was just
created by Subversion and enter the following command:
$./configure
If everything worked correctly, you should see an ASCII dragon warning you about
the power of Nmap, like this:
[9]
Now run Nmap to ensure that it was installed correctly. You should see your
build information:
#nmap -v
Nmap version 6.47SVN ( http://nmap.org )
Platform: x86_64-apple-darwin14.0.0
Compiled with: nmap-liblua-5.2.3 openssl-0.9.8za nmap-libpcre-7.6
libpcap-1.5.3 nmap-libdnet-1.12 ipv6
Compiled without:
Available nsock engines: kqueue poll select
Once your working copy is synchronized with the remote repository, we need to
rebuild Nmap:
$make
Again to install the binaries and data files in the system, use this command:
#make install
[ 10 ]
Chapter 1
The previous command ran a SYN scan with OS detection (-O), service detection
(-sV), and most importantly with NSE on (-sC). The -sC option enables the NSE
and runs any script in the default category. This set of scripts is considered safe as
it won't perform any operations that could interfere with a service running on the
target host. However, note that some of the scripts perform actions that can raise
alarms in intrusion detection systems (IDS) and intrusion prevention systems (IPS).
An unprivileged scan can't access raw sockets, which generally results in
a slower scan. However, the SYN scan is the default type of scan executed
when Nmap runs in privileged mode.
[ 11 ]
dns-recursion: This detects DNS servers that allow recursion that may be
expiration technique
The previously mentioned scripts are only a few compared to the current total of
almost 500. That's a whole lot more of information that can be collected by simply
using NSE.
Script categories
The collection of NSE scripts is divided into the following categories:
Script category
auth
Description
broadcast
brute
default
discovery
dos
exploit
external
fuzzer
intrusive
malware
safe
version
vuln
[ 12 ]
Chapter 1
To select a whole category, simply use the name of the category (see the Script
categories section) as the argument. For example, to run the exploit category,
use the following command:
nmap --script exploit <target>
You can also run several categories by separating them with a comma:
nmap --script discovery,intrusive <target>
Similarly with categories, you can execute several scripts by separating the paths
with a comma:
nmap --script /path/to/script.nse,/another/path/script2.nse <target>
To execute all the scripts contained in a folder, you only need to pass the folder name
as an argument:
nmap --script /path/to/folder/ <target>
nmap --script /custom-nse-scripts/ scanme.nmap.org
Keep in mind that the --script option accepts relative and absolute paths to
scripts and folders. Besides the current directory, relative paths can be looked for
in the following directories:
--datadir
$NMAPDIR
~/.nmap
%HOMEPATH%\AppData\Roaming\nmap
The directory containing nmap
The directory containing nmap followed by this relative path: ../share/nmap
NMAPDATADIR
[ 14 ]
Chapter 1
For example, the not exploit expression will match any script that does
not belong to the exploit category:
#nmap -sV --script "not exploit" <target>
If you are selecting scripts, you can also use the wildcard character, *:
#nmap --script "snmp-*" <target>
[ 15 ]
Not a lot of Nmap users know this but you can also omit the script name when
setting arguments:
$nmap -p80 --script http-trace --script-args path <target>
If you are working with scripts that share argument names, you must avoid name
conflicts manually:
$nmap --script http-majordomo2-dir-traversal,http-axis2-dir-traversal
--script-args http-axis2-dir-traversal.uri=/axis2/,uri=/
majordomo/ <target>
$nmap --script http-majordomo2-dir-traversal,http-axis2-dir-traversal
--script-args uri=/axis2/,http-majordomo2-dir-traversal.uri=/
majordomo/ <target>
$nmap --script http-majordomo2-dir-traversal,http-axis2-dir-traversal
--script-args
http-axis2-dir-traversal.uri=/axis2/,http-majordomo2-dirtraversal.uri=/majordomo/ <target>
The alias in script arguments will only work if the NSE script uses the
stdnse.get_script_args()function to load the arguments (refer to
Chapter 4, Exploring the Nmap Scripting Engine API and Libraries). You are
encouraged to always use this function, but there are a few scripts that
were submitted before the function was introduced.
Chapter 1
Let's say we want to force the execution of the http-title NSE script against the
service running on port 1212:
$nmap --script +http-title -p1212 192.168.1.210
Without the + sign, the script will not run but, since we added it, the report comes
back with the following:
Nmap scan report for 192.168.1.210
Host is up (0.00026s latency).
PORT
STATE SERVICE
1212/tcp open
lupa
|_http-title: W00t!
You can also turn on the debugging mode of Nmap with the -d[1-9] flag.
This flag can be followed by an integer that denotes the debugging level and
should be between 1 and 9. The higher the level, the more verbose is the output:
#nmap -sV -script exploit -d3 <target>
The --packet-trace option includes all the packets sent and received, not only the
traffic generated by NSE:
#nmap -O --script myscript.nse --packet-trace <target>
[ 17 ]
prerule
postrule
portrule
hostrule
Let's review some examples of these different script rules. This will also help you
learn to debug scripts for those times when you run into problems:
[ 18 ]
Chapter 1
-- seconds, Service detection's NULL probe detects
it as tcpwrapped.
return port.service == "tcpwrapped"
and port.protocol == "tcp" and port.state ==
"open"
and
not(shortport.port_is_excluded(port.number,port.protocol))
end
Information-gathering
Information-gathering is one of the strengths of NSE, and the collection of scripts
available is astonishing. These scripts use different techniques and data sources
to obtain additional host information such as virtual hosts, service versions, user
lists, and even geolocation. Keep in mind that some of these scripts query external
services, and the accuracy of the information depends on each database.
STATE SERVICE
1900/udp open
upnp
| upnp-info:
| 192.168.1.1
|
|
Location: http://192.168.1.1:5431/dyndev/uuid:3872c05b-c117
-17c1-5bc0-12345
|
Manufacturer: Comtrend
Name: WANDevice.1
Manufacturer: Comtrend
[ 20 ]
Chapter 1
|
Name: WanConnectionDevice.1
Manufacturer: Comtrend
|_
hostmap-bfk
hostmap-robtex
hostmap-ip2hosts
We can run them at the same time with the following command:
$nmap -sn --script "hostmap*" <target>
If there are any records on the external databases, they will be shown in the results:
Nmap scan report for packtpub.com (83.166.169.228)
Host is up (0.13s latency).
hosts:
packtpub.com
|_
83.166.169.228
| hostmap-robtex:
|
|_
hosts:
packtpub.com
| hostmap-ip2hosts:
|
hosts:
www.packtpub.com
packtpub.com
|_
83.166.169.228
[ 21 ]
IP: 192.168.1.202
MAC: 08:00:27:16:4f:71
IP: 192.168.1.206
MAC: 40:25:c2:3f:c7:24
|_
All the hosts that responded to the broadcast ping will be shown. Additionally,
using the newtargets argument, these hosts will be added to the scan queue:
# nmap --script broadcast-ping --script-args newtargets
Starting Nmap 6.47SVN ( http://nmap.org ) at 2014-11-30 22:05 CST
Pre-scan script results:
| broadcast-ping:
|_
IP: 192.168.0.8
MAC: 6c:ad:f8:7b:83:ab
STATE SERVICE
8008/tcp open
http
8009/tcp open
ajp13
[ 22 ]
Chapter 1
Optionally, these targets can also be added to the scanning queue on the fly:
#nmap -sL --script=targets-sniffer --script-args=newtargets -e
<interface>
Starting Nmap 6.47SVN ( http://nmap.org ) at 2014-11-30 22:15 CST
Pre-scan script results:
| targets-sniffer: Sniffed 5 address(es).
| 224.0.0.251
| fe80::7a31:c1ff:fec1:9c0a
| 192.168.0.8
| 192.168.0.2
|_239.255.255.250
Nmap scan report for 192.168.0.8
Host is up (0.0066s latency).
Not shown: 98 closed ports
PORT
STATE SERVICE
8008/tcp open
http
8009/tcp open
ajp13
[ 23 ]
STATE SERVICE
49152/tcp open
unknown
Password auditing
Brute-force password-auditing scripts have grown to cover a lot of different services,
thanks to the brute NSE library. This library allows NSE developers to easily launch
dictionary attacks by implementing a simple class that uses other NSE libraries
such as unpwd, which gives access to a username and password database. If any
credentials are found during the execution, they will be added to a credentials
database that can be read by other scripts.
If any credentials are found, they will be included in the script output:
3306/tcp open mysql
| mysql-brute:
| root:<empty> => Valid credentials
|_ test:test => Valid credentials
[ 24 ]
Chapter 1
The output of this script is similar to that of other scripts that depend on the
brute library:
PORT STATE SERVICE REASON
25/tcp open stmp syn-ack
| smtp-brute:
| Accounts
| acc0:test - Valid credentials
| acc1:test - Valid credentials
| acc3:password - Valid credentials
| acc4:12345 - Valid credentials
| Statistics
|_ Performed 3190 guesses in 81 seconds, average tps: 39
Vulnerability scanning
NSE offers a great framework for penetration testers who need to create tools to
detect and exploit vulnerabilities. Nmap offers a lot of options such as low-level
packet creation and handling, libraries used to communicate with the most popular
protocols, and an interface to report vulnerabilities. For those who don't need to
write new tools but simply want to scan their network, there are very useful scripts
to detect common misconfigurations and automate tedious tasks such as finding
forgotten backup files and performing security checks.
Each control in the database will be audited. The following are the results of a clean
MySQL server installation on an Ubuntu server:
PORT STATE SERVICE
3306/tcp open mysql
| mysql-audit:
| CIS MySQL Benchmarks v1.0.2
| 3.1: Skip symbolic links => PASS
| 3.2: Logs not on system partition => PASS
| 3.2: Logs not on database partition => PASS
[ 25 ]
[ 26 ]
Chapter 1
If the host is vulnerable, the output will return something similar to this:
PORT STATE SERVICE REASON
80/tcp open http syn-ack
| http-slowloris:
| Vulnerable:
| the DoS attack took +5m35s
| with 400 concurrent connections
|_ and 1900 sent queries
[ 27 ]
443/tcp open
https
syn-ack
| ssl-poodle:
|
VULNERABLE:
State: VULNERABLE
IDs:
|
and
CVE:CVE-2014-3566
OSVDB:113251
|
other products, uses nondeterministic CBC padding, which
makes it easier
|
via a
|
Check results:
|
|
TLS_RSA_WITH_3DES_EDE_CBC_SHA
References:
https://www.imperialviolet.org/2014/10/14/poodle.html
http://osvdb.org/113251
http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-3566
|_
https://www.openssl.org/~bodo/ssl-poodle.pdf
Chapter 1
set
set
set
set
expandtab
softtabstop=2
shiftwidth=2
copyindent
You can also download the file from my GitHub repository at https://
github.com/cldrn/nmap-nse-scripts/blob/master/.vimrc.
Halcyon IDE
For those who love working with graphical environments, there is an unofficial IDE,
named Halcyon IDE, created exclusively to develop NSE scripts. It is written in Java
and allows developers to test and debug scripts within itself, providing features such
as code completion and syntax highlighting. The following screenshot shows the
Halcyon IDE:
[ 29 ]
Summary
In this chapter, we introduced NSE and its amazing capabilities. By now, you should
have installed the latest version of Nmap and have your development environment
ready to go. The Nmap options covered in this chapter will be all you need to
comfortably run and debug NSE scripts. Pay close attention to the different script
rules (prerule, postrule, portrule, and hostrule) that will be shown throughout
the book.
Now we are ready to start writing NSE scripts and get familiar with all the available
libraries. In the following chapters, you will discover the true power of NSE. The
next chapter covers the fundamentals of Lua programming, so prepare yourself to
learn this amazing scripting language.
[ 30 ]
www.PacktPub.com
Stay Connected: