Port Scanning

# Stealthy
nmap -sS ip.X

# Scan all ports, might take a while.
nmap ip.X -p-

# Scan for UDP
nmap ip.X -sU
unicornscan -mU -v -I ip.X

# Scan for version, with NSE-scripts and trying to identify OS
nmap ip -sV -sC -O

# All out monsterscan
nmap -vvv -Pn -A -iL listOfIP.txt

# Fast scan
nmap ip.X -F

# Only scan the 100 most common ports
nmap ip.X --top-ports 100

Basics - tcp-connect scan

Okay, so a bit of the basics of Nmap and how it works. When one machine initiate a connection with another machine using the transmission-control protocol (tcp) it performs what is know as a three-way handshake. That means:

machine1 sends a syn packet to machine2
machine2 send a syn-ack packet to machine1
machine1 sends a ack packet to machine2.

If machine2 responds with a syn-ack we know that that port is open. This is basically what nmap does when it scans for a port. If machine1 omits the last ack packet the connection is not made. This can be a way to make less noise.

This is the default mode for nmap. If you do not add any flags and scan a machine this is the type of connection it creates.

UDP scan

UDP is after TCP the most common protocol. DNS (53), SNMP (161/162) and DHCP (67/68) are some common ones. Scanning for it is slow and unreliable.

-sU

Output scan to a textfile

Not all output works with grepable format. For example NSE does not work with grepable. So you might want to use xml instead.

# To text-file
-oN nameOfFile

# To grepable format
-oG nameOfFile

# To xml
-oX nameOfFile

Scan an entire IP-range

You might find that a site has several machines on the same ip-range. You can then use nmap to scan the whole range.

The -sn flag stops nmap from running port-scans. So it speeds up the process.

nmap -vvv -sn ip/24

You can also specify a specific range, like this

nmap -sP ip-100
`

Sort out the machines that are up

So let's say you find that 40 machine exists in that range. We can use grep to output those IP:s.

First let's find the IPs that were online. Ip-range is the output from previous command. You can of course combine them all.

cat ip-range.txt | grep -B 1 "Host is up"

Now let's sort out the ips from that file.

grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' ip-range.txt > only-ip.txt

Now you can input all those ips to nmap and scan them.

Scan a range and output if a specific port is open

Nmap has a command to make the output grepable.

nmap -vvv -p 80 ip-100 -oG - | grep 80/open

Nmap scripts

This chapter could also be placed in Vulnerability-analysis and Exploitation. Because nmap scripting is a really versatile tool that can do many things. Here we will focus on it's ability to retrieve information that can be useful in the process to find vulnerabilities

First locate the nmap scripts. Nmap scripts end in .nse. For Nmap script engine.

locate *.nse

The syntax for running a script is:

nmap --script scriptname ip

To find the "man"-pages, the info about a script we write:

nmap -script-help http-vuln-cve2013-0156.nse

Run multiple scripts

Can be run by separating the script with a comma

nmap --script scriptone.nse,sciprt2.nse,script3.nse ip

Run the default scripts

nmap -sC example.com

Metasploit

We can do port-scanning with metasploit and nmap. And we can even integrate nmap into metasploit. This might be a good way to keep your process neat and organized.

db_nmap

You can run db_nmap and all the output will be stored in the metasploit database and available with

hosts
services

You can also import nmap scans. But you must first output it in xml-format with the following flag

nmap ip -oX result.xml

Good practice would be to output the scan-results in xml, grepable and normal format. You do that with

nmap ip -oA result

Then you can load it into the database with the following command.

db_import /path/to/file.xml

Metasploit PortScan modules

If you for some reason don't have access to nmap you can run metasploits modules that does portscans

use auxiliary/scanner/portscan/

Last updated