Nmap Cheat Sheet Part 4
Nmap Cheat Sheet Part 4
Resources
By advance Googling I came to know that the following IP address is protected by WAF (web
application firewall) as well as some kind of IDS. We tried with some kind of brute force attack,
SQL injection. Every time we put some special character, it wass showing “Firewall
authentication failed”. We came to know that this thing can be bypassed with HTTP verb
tampering. We will discuss that later.
Ethical Hacking Training – Resources (InfoSec)
So firewall is present. Let’s do Nmap for more reefing results. So first do an Nmap scan with –
Pn.
Now we confirmed that remote shell is running in the remote server which is filtered. So we
are sure there is a firewall behind the scene. Let’s do the scan with specified port to get
details.
We came to know that there are lots of services running in the network with port specification
and timing options.
Observation:
The scanned firewall runs various services for the inside network, including DNS, SSH, HTTPS
and Web proxy. These are accessible to all PCs on the internal network. It also runs a
transparent proxy on port 80, so that client browser settings are not required to be changed.
Evade or Evasion or Bypass of a Firewall:
Well a bypass or evasion or evade is nothing but another way to get into the system. To block
malicious attack or spam, admin uses firewall or IDS/IPS. But from an attacker’s point of view,
he will find a way to bypass the rule for firewall; there are lots of way to bypass the firewall for
an Nmap scan. We will discuss everything below. But one thing is sure shot “skill and
common sense” that is used by an attacker.
1. Fragmentation:
In general, the word fragmentation means dividing large objects into small parts. Similarly,
Nmap uses 8 bytes of packet for bypassing firewall/ids/ips. Nmap will split into small small
packets for bypassing firewall. This technique is very old, still it will work if there is a
misconfiguration of firewall.
Nmap –f host
MAXIMUM Transmission Unit. It is an alias of fragmentation, but here we can specify the scan
for a custom amount of packets.
Nmap –mtu 16 host
The above Nmap scan instructs to use 16 bytes of packets instead of 8 bytes in the previous
scan. So we can specify the custom packets which should be multiple of 8.
2. Decoy
This type of scan is very stealthy and undetectable. In this scan, targets are scanned by
multiple fake or spoofed IP addresses. This way the firewall can be bypassed and the firewall
will think that the attack or scan is done by multiple resources or ip addresses.
Decoys are used both in the initial ping scan (using ICMP, SYN, ACK, or whatever) and during
the actual port scanning phase. Decoys are also used during remote OS detection (-O).
Decoys do not work with version detection or TCP connect scan.
This effectively makes it appear that the target is being scanned by multiple systems
simultaneously. Using decoys allows the actual source of the scan to “blend into the crowd”,
which makes it harder to trace where the scan is coming from
There are two ways to perform decoy scan:
1.nmap –D RND:10 TARGET
Here Nmap will generate random 10 IPs and it will scan the target using 10 IP and source.
2.nmap –D decoy1,decoy2,decoy3 target
Here decoys are specified by the attacker.
The below network capture show multiple decoys which will fool the firewall.
–source-port option:
Every TCP segment contains a source port number in addition to a destination. By default,
Nmap will randomly pick an available outgoing source port to probe a target. The –source-
port option will force Nmap to use the specified port as the source for all packets. This
technique can be used to exploit weaknesses in firewalls that are improperly configured to
blindly accept incoming traffic based on a specific port number. Port 20 (FTP), port 53 (DNS),
and 67 (DHCP) are common ports susceptible to this type of scan.
Nmap –source-port port target
Generates a random
0 (zero)
MAC address
the rpc server must be mapped to the host which can be confirmed by port mapper.if the port
mapper (rpcbind) service (UDP or TCP port 111) is available
This shows that hosts frequently offer many RPC services, which increases the probability that
one is exploitable. We also noticed that most of the services are on strange high-numbered
ports (which may change for any number of reasons) and split between UDP and TCP
transport protocols.
Nmap can determine all of the information by directly communicating with open RPC ports
through the following three-step process.
1. The TCP and/or UDP port scan finds all of the open ports.
2. Version detection determines which of the open ports use the SunRPC protocol.
3. The RPC brute force engine determines the program identity of each RPC port by trying a
null command against each of the 600 programs numbers in nmap-rpc. Most of the time
Nmap guesses wrong and receives an error message stating that the requested program
number is not listening on the port. Nmap continues trying each number in its list until
success is returned for one of them. Nmap gives up in the unlikely event that it exhausts
all of its known program numbers or if the port sends malformed responses that suggest
it is not really RPC.
The above scan has results for RPC services, but unfortunately we did not get any SUN RPC,
because we have an Ubuntu machine. In a real pen testing environment, we can gather
information about SUN if we have a SUN server.
SSL Post-processor Scan
Nmap has the capacity to detect the SSL encryption protocol and then launch an encrypted
session through which it executes normal version detection. As with the RPC grinder discussed
previously, the SSL post-processor/scan is automatically executed whenever an appropriate
(SSL) port is detected.
Command: nmap -Pn -sSV -T4 –F target
Miscellaneous further
information which was
immediately available
and might be useful.
Examples include
i/info/
whether an X server is
open to unauthenticated
connections, or the
protocol number of SSH
servers.
Softmatch Directive
Syntax: softmatch <service> <pattern>
Examples:
softmatch ftp m/^220 [-.\w ]+ftp.*\r\n$/i
softmatch smtp m|^220 [-.\w ]+SMTP.*\r\n|
softmatch pop3 m|^\+OK [-\[\]\(\)!,/+:<>@.\w ]+\r\n$|
The softmatch directive is similar in format to the match directive discussed above. The main
difference is that scanning continues after a softmatch, but it is limited to probes that are
known to match the given service. This allows for a normal (“hard”) match to be found later,
which may provide useful version information.
ports and sslports Directives
Syntax: ports <portlist>
Examples:
ports 21,43,110,113,199,505,540,1248,5432,30444
ports 111,4045,32750-32810,38978
This line tells Nmap what ports the services identified by this probe are commonly found on. It
should only be used once within each Probe section. The syntax is a slightly simplified version
of that taken by the Nmap -p option. Syntax: sslports <portlist>
Example:
sslports 443
This is the same as ‘ports’ directive described above, except that these ports are often used to
wrap a service in SSL. For example, the HTTP probe declares “sslports 443” and SMTP-
detecting probes have an “sslports 465” line because those are the standard ports for HTTPS
and SMTPS respectively. The <portlist> format is the same as with ports. This optional
directive cannot appear more than once per Probe.
totalwaitms Directive
Syntax: totalwaitms <milliseconds>
Example:
totalwaitms 5000
This rarely necessary directive specifies the amount of time Nmap should wait before giving
up on the most recently defined Probe against a particular service. The Nmap default is
usually fine.
Rarity Directive
Syntax: rarity <value between 1 and 9>
Example:
rarity 6
The rarity directive roughly corresponds to how infrequently this probe can be expected to
return useful results. The higher the number, the rarer the probe is considered and the less
likely it is to be tried against a service.
Fallback Directive
Syntax: fallback <Comma separated list of probes>
Example:
fallback GetRequest,GenericLines
This optional directive specifies which probes should be used as fallbacks if there are no
matches in the current Probe section. For TCP probes without a fallback directive, Nmap first
tries match lines in the probe itself and then does an implicit fallback to the NULL probe. If the
fallback directive is present, Nmap first tries to match lines from the probe itself, then those
from the probes specified in the fallback directive (from left to right). Finally, Nmap will try the
NULL probe. For UDP, the behavior is identical except that the NULL probe is never tried
Practice for above:
Ok we have studied enough theory, it is time for practice, because theory is boring unless and
until practice is done. So let us do some pen testing. Following are the steps a pen tester can
follow for real life hacking or security assessments.
1. Locate the Nmap file probes: command:locate nmap-service-probes
Here we are editing via gedit and we will put all rules as we need for our pen testing. Follow
the below picture. Save the file and go for pen testing.
Now it is the best time to start our ninja skill for pen testing using Nmap. Obviously using
Nmap we can do lot of things. Here we will start with basic web app pen testing.
First we will start with web-server pen-testing. Basically a web server is a space or a server
which handles all scripts, web pages and validation, if required and then sends to client’s
browser. I a web server has vulnerablilities, then it is prone to different types of attack like sqli,
xss, csrf, HPP, RCE, etc. So it is better to do server side validation. Here we will discuss some
common vulnerabilities that we will pen test using Nmap.
HTTP Methods by Nmap:
HTTP methods are nothing but different types of requests handled by a web server to deliver
web pages.Web servers support different HTTP methods according to their configuration and
software, and some of them could be dangerous under certain conditions. Pen testers need a
way of quickly listing the available methods. Some methods are GET, HEAD, POST, TRACE,
DEBUG, OPTION, DELETE, TRACK, PUT, etc. A full list of methods that are handled by the
browser can be found at the link: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
As previously described, Nmap can do easy work with an NSE script. Check the below script:
Cmd: nmap -p80,443 –script http-methods scanme.nmap.org
The argument -p80,443 –script http-methods makes Nmap launch the httpmethods script if a
web server is found on ports 80 or 443 (-p80,443).
To individually check for the HTTP methods, the following scripts are needful.
Cmd:nmap -p80,443 –script http-methods –script-args http-methods.retest
scanme.nmap.org
By default, the script http-methods uses the root folder as the base path ( / ). If we wish to set
a different base path, set the argument http-methods.url-path:
Cmd:nmap -p80,443 –script http-methods –script-args http-methods.urlpath=/mypath/
scanme.nmap.org
The HTTP methods TRACE, CONNECT, PUT, and DELETE might present a security risk, and they
need to be tested thoroughly if supported by a web server or application. TRACE makes
applications susceptible to Cross Site Tracing (XST) attacks and could lead to attackers
accessing cookies marked as httpOnly. The CONNECT method might allow the web server to
be used as an unauthorized web proxy. The methods PUT and DELETE have the ability to
change the contents of a folder, and this could obviously be abused if the permissions are not
set properly.
You can learn more about common risks associated with each method here:
https://www.owasp.org/index.php/Test_HTTP_Methods_%28OTG-CONFIG-006%29
HTTP User Agent:
There are some packet filtering products that block requests that use Nmap’s default HTTP
User Agent. You can use a different HTTP User Agent by setting the argument http.useragent:
cmd: nmap -p80 –script http-methods –script-args http.useragent=”Mozilla 5″ <target>
HTTP pipelining
Some web servers allow the encapsulation of more than one HTTP request in a single packet.
This may speed up the execution of an NSE HTTP script, and it is recommended that it is used
if the web server supports it. The HTTP library, by default, tries to pipeline 40 requests and
auto adjusts the number of requests according to the traffic conditions, based on the Keep-
Alive header.
Cmd: nmap -p80 –script http-methods –script-args http.pipeline=25 <target>
Additionally, we can use the argument http.max-pipeline to set the maximum number of HTTP
requests to be added to the pipeline. If the script parameter http.pipeline is set, this argument
will be ignored:
cmd:nmap -p80 –script http-methods –script-args http.max-pipeline=10 <target>
HTTP-Proxy scanning with Nmap:
HTTP proxies are used to make requests through their addresses, therefore hiding our real IP
address from the target. Detecting them is important if you are a system administrator who
needs to keep the network secure, or an attacker who spoofs his real origin. The following
command shows how to detect open proxy:
cmd: nmap –script http-open-proxy -p8080 <target>
Here the argument –script http-open-proxy -p8080 to launch the NSE script http-open-proxy
if a web server is found running on port 8080.
Different pattern:
We may use a different pattern by a specified URL to target for scanning. It can be done by a
specified NSE Script. Follow the below command:
Cmd: nmap –script http-open-proxy –script-args http-open-
proxy.url=http://whatsmyip.org,http-open-.pattern=”Your IP address is” -p8080
<target>
The fingerprints are stored in the file http-fingerprints.lua in /nselib/data/, and they are
actually LUA tables. An entry looks like something like the following:
To display all the entries that returned a status code that could possibly indicate a page exists,
use the script argument http-enum.displayall:
nmap script http-enum http-enum.displayall -p80 — <target>
BY HTTP pipelining
Some web servers allow the encapsulation of more than one HTTP request in a single packet.
This may speed up the execution of an NSE HTTP script, and it is recommended that it is used
if the web server supports it. The HTTP library, by default, tries to pipeline 40 requests and
automatically adjusts that number according to the traffic conditions, based on the Keep-Alive
header.
$ nmap -p80 –script http-enum –script-args http.pipeline=25 <target>
Additionally, we can use the argument http.max-pipeline to set the maximum number of HTTP
requests to be added to the pipeline. If the script parameter http.pipeline is set, this argument
will be ignored:
nmap -p80 –script http-methods –script-args http.max-pipeline=10
pass: In this mode, for each password listed in passdb, every user in usedb will be tried.
nmap –script http-brute –script-args brute.mode=pass <target>
fcreds: This mode requires the additional argument brute.credfile.
nmap –script http-brute –script-args brute.mode=creds,brute.credfile=./creds.txt
<target> mod_userdir Pentesting:
Apache’s module UserDir provides access to the user directories by using URIs with the syntax
/~username/. With Nmap we can perform dictionary attacks and determine a list of valid
usernames on the web server. To try to enumerate valid users in a web server with
mod_userdir; use Nmap with these
arguments:
nmap -p80 –script http-userdir-enum <target>
To include more attack payloads, use the script argument http-waf-detect.aggro. This mode
generates more HTTP requests but can also trigger more products:
nmap -p80 –script http-waf-detect –script-args=”http-waf-detect.aggro” <target>
The script http-unsafe-output-escaping was written by Martin Holst Swende, and it spiders a
web server to detect the possible problems with the way web applications return output
based on user input. The script inserts the following payload into all the parameters it finds:
ghz%3Ehzx%22zxc%27xcv
The payload shown above is designed to detect the characters > ” ‘, which could cause Cross
Site Scripting vulnerabilities. The official documentation of the scripts http-unsafe-output-
escaping and httpphpself-xss can be found at the following URLs:
http://nmap.org/nsedoc/scripts/http-phpself-xss.html
http://nmap.org/nsedoc/scripts/http-unsafe-output-escaping.html
Detecting SQL Injection:
SQL injection vulnerabilities are caused by the lack of sanitation of user input, and they allow
attackers to execute DBMS queries that could compromise the entire system.
To scan a web server looking for files vulnerable to SQL injection by using Nmap, use the
following command:
nmap -p80 –script http-sql-injection <target>
The httpspider library behavior can be configured via library arguments. By default, it uses
pretty conservative values to save resources, but during a comprehensive test, we need to
tweak several of them to achieve optimum results. For example, the library will only crawl 20
pages by default, but we can set the argument httpspider.maxpagecount accordingly for
bigger sites, as shown in the following command:
nmap -p80 –script http-sql-injection –script-args httpspider.maxpagecount=200
<target>
Another interesting argument is httpspider.withinhost, which limits the web crawler to a given
host. This is turned on by default, but if you need to test a collection of web applications
linked to each other, you could use the following command:
nmap -p80 –script http-sql-injection –script-args httpspider.withinhost=false <target>
The official documentation for the library can be found at
http://nmap.org/nsedoc/lib/httpspider.html
HTTP User Agent
There are some packet filtering products that block requests made using Nmap’s default HTTP
User Agent. We can use a different User Agent value by setting the argument http.useragent:
nmap -p80 –script http-sql-injection –script-args http.useragent=”Mozilla 42″ <target>
HTTP pipelining
Some web servers allow the encapsulation of more than one HTTP request in a single packet.
This may speed up the execution of an NSE HTTP script, and it is recommended that this is
used if the web server supports it. The HTTP library, by default, tries to pipeline 40 requests
and automatically adjusts that number according to the traffic conditions, based on the Keep-
Alive header.
nmap -p80 –script http-sql-injection –script-args http.pipeline=25<target
N.B: there are some examples for which a screenshot is unavailable because it’s impossible for
everything to be put here. My recommendation is for all readers to try all commands and let
me know if any problem occurs. There are lots of NSE scripts available for pen testing. The
above are a few examples. All the scripts will be discussed in the upcoming installments.
CYBER THREAT DISCLOSURE POLICY:
The above scanning /malicious activity is done by the proper permission of the
respective website domain owner. It is recommended to not use these skills for
attacking/hacking a website.