Port Scanning
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:
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.
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.
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.
You can also specify a specific range, like this
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.
Now let's sort out the ips from that file.
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 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.
The syntax for running a script is:
To find the "man"-pages, the info about a script we write:
Run multiple scripts
Can be run by separating the script with a comma
Run the default scripts
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
You can also import nmap scans. But you must first output it in xml-format with the following flag
Good practice would be to output the scan-results in xml, grepable and normal format. You do that with
Then you can load it into the database with the following command.
Metasploit PortScan modules
If you for some reason don't have access to nmap you can run metasploits modules that does portscans
Last updated