Introduction to Reconnaissance
Reconnaissance in a cyber-attack consists of actively or passively gathering information that can be leveraged in other phases of an attack. The MITRE ATT&CK Enterprise Matrix provides different tactics and techniques related to reconnaissance. In this interactive lab, you will learn how to perform reconnaisance of networks, physical and virtual systems, as well as modern applications using Nmap.
Active vs Passive Reconnaissance
Active Reconnaissance
When an attacker participates in active reconnaissance, probes are sent to a target or cluster of devices in hopes to evoke a response. Based on what devices are being scanned, probes could house numerous protocols at varying levels of aggressiveness. For instance, a smart light dimmer would not be able to withstand an excessive amount of queries, and could inevitably crash.
Examples of active reconnaissance include:
- Host enumeration
- Network enumeration
- User enumeration
- Group enumeration
- Network share enumeration
- Web page enumeration
- Application enumeration
- Service enumeration
- Packet crafting
Keep into account what the device constraints might be and adjust settings within your scanner to account.
Passive Reconnaissance
With passive reconnaissance, an attacker’s tools do not need to interact directly with target devices. By listening to traffic on a given network, attackers are able to glean information about the relationship of devices on the network. Doing so is less invasive and is unlikely for an individual to crash services on targeted devices. The likelihood of detection on a network is also minimal as there is no traffic being produced.
Examples of passive reconnaissance include:
- Domain enumeration
- Packet inspection
- Open-source intelligence (OSINT)
- Recon-ng
- Eavesdropping
Preforming Active Reconnaissance
The act of gathering information on any given target is called enumeration.
One of the first things you would do in a penetration test is the external enumeration of hosts. This is the discovery of internet-facing hosts on a target network, and doing so will help identify the systems that are most exposed.
After identifying accessible hosts, you can start by discovering the services that are being exposed. In ideal scenarios, devices should be deployed behind a firewall to minimize exposed services on a network. By executing a port scan, a list of exposed ports can be determined and acted upon.
Port scans are performed by scanning tools that send numerous types of probes to a host in hopes to determine if a service is currently running.
In the next few steps, we will use the popular yet powerful network scanner Nmap to probe open ports using a variety of protocols.
ntroduction to Nmap
About Nmap
Nmap is by far one of the most popular network scanning tools available and can run on most operating systems found in the field.
Installing Nmap
Installing Nmap on Ubuntu is done by running the following:
sudo apt install nmap -y
To check the current version installed run:
nmap -V
If installed, you should see output similar to:
Nmap version 7.80 ( https://nmap.org )
Platform: x86_64-pc-linux-gnu
...
Nmap versions will differ as repos update.
To bring up the help page, type: nmap -h
Nmap 7.80 ( https://nmap.org )
Usage: nmap [Scan Type(s)] [Options] {target specification}
TARGET SPECIFICATION:
Can pass hostnames, IP addresses, networks, etc.
Ex: scanme.nmap.org, microsoft.com/24, 192.168.0.1; 10.0.0-255.1-254
-iL <inputfilename>: Input from list of hosts/networks
-iR <num hosts>: Choose random targets
--exclude <host1[,host2][,host3],...>: Exclude hosts/networks
--excludefile <exclude_file>: Exclude list from file
HOST DISCOVERY:
...
...
...
Nmap Cheat Sheet
A Nmap cheat sheet provided by H4cker.org can be viewed by visiting, or simply using curl and accessing https://h4cker.org/cheat/nmap.
curl https://h4cker.org/cheat/nmap
Docker
The rest of the scenario will use Docker to simulate a vulnerable host on a network. The gravemind Docker container is obtained from Docker Hub. Multiple hosts will be set up for you using Docker.
Please verify that two gravemind instances are running using the following command:
docker ps
If no containers running, a setup script is provided in your home directory and can be executed by running:
bash ~/gravemind_docker_network_setup.sh
These two containers have statically assigned IP Addresses:
- Container 1: 10.0.0.5
- Container 2: 10.0.0.6
TCP Scanning with Nmap
With the newly created containers, we can start probing the host with various scans found on Nmap.
It is important to understand the six different port states that are recognized by Nmap on the following table:
| State | Explantation |
|---|---|
| Open | A service that is accepting TCP, UDP, SCTP packets. |
| Closed | A port with no active service taking requests. |
| Filtered | Unable to differentiate as the packets are being filtered and preventing the probes from reaching the port. |
| Open/Filtered | Unable to determine between Open or Filtered and occurs when open ports typically do not give a response. |
| Closed/Filtered | Unable to determine between Closed or Filtered. |
Establishing a TCP Connection
Next, we must understand how TCP connections are generally created and sustained.
At a high level, when initiating a TCP connection between a client and server, the client will send the server a “SYN” request, “asking” for a connection to be established. Once the server has processed the response, a “SYN-ACK” response will be sent back to the client, acknowledging it has been received. The client answers with an “ACK”, and once it has been received by the server, communication has been established. This three-phase sequence of events is called the TCP 3-way handshake.
Nmap uses this handshake to check which ports are open.
TCP Connect Scan (-sT)
TCP Connect Scan uses the full TCP 3-way handshake to establish a connection to a host and see what ports are open, filtered, or closed. A port is filtered if there is no response from the host; usually blocked by a firewall.
To run a TCP connect scan on our first container, type the command:
nmap -sT 10.0.0.5
The following is the output:
Starting Nmap 7.80 ( https://nmap.org ) at 2021-08-06 23:44 UTC
Nmap scan report for 10.0.0.5
Host is up (0.00025s latency).
Not shown: 997 closed ports
PORT STATE SERVICE
22/tcp open ssh
53/tcp open domain
80/tcp open http
MAC Address: 02:42:0A:00:00:05 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 0.17 seconds
We can see by the output, Nmap has found three open services. Port 80 is running an HTTP service, port 22 is running an ssh server, and port 53 is running a DNS service.
To limit the port range when scanning use the -p tag to scan the first 100 ports. For example:
nmap -sT -p 1-100 10.0.0.5
A single port can be passed into the -p tag followed by the port as follows:
nmap -sT -p 80 10.0.0.5
The following are the status codes for TCP Connect Scan:
| Status | Response | Analysis |
|---|---|---|
| Open | TCP SYN-ACK | The service is listening on the port. |
| Closed | TCP RST | The service is not listening on the port. |
| Filtered | No response from target | The port is firewalled. |
TCP SYN Scan (-sS)
Using TCP connect scan is not efficient and can be detected by a decent intrusion detection system (IDS). However, we can use a more stealthy approach by not completing the TCP connection.
TCP SYN Scan is the most popular scan when using Nmap and is often called “half-open scanning”. It performs thousands of port scans per second and will not be picked up by an IDS. The scan can also differentiate between open, closed, and filtered states.
As mentioned earlier, a TCP 3-way handshake starts by sending an SYN packet and waiting for an SYN/ACK. Once the server replies with a SYN/ACK, Nmap considers the port open, but does not complete the connection. If an RST or “reset” is sent back, the port is considered not listening. Lastly, if no response was sent after a few tries, the port is decided as filtered.
To run a TCP SYN scan on our first gravemind container, we can input the following:
nmap -sS 10.0.0.5
Note that this port scan will require root privileges and can be escalated by using the sudo command.
sudo nmap -sS 127.0.0.1
Below is the output of the command:
Starting Nmap 7.80 ( https://nmap.org ) at 2021-08-07 00:20 UTC
Nmap scan report for 10.0.0.5
Host is up (0.0000070s latency).
Not shown: 997 closed ports
PORT STATE SERVICE
22/tcp open ssh
53/tcp open domain
80/tcp open http
MAC Address: 02:42:0A:00:00:05 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 0.21 seconds
As expected, ports 53, 80, and 22 are shown to be open.
To limit the port range when scanning use the -p tag to scan the first 100 ports. For example:
nmap -sS -p 1-100 10.0.0.5
A single port can be passed into the -p tag followed by the port as follows:
nmap -sS -p 80 10.0.0.5
The following are the status codes for TCP SYN:
| Status | Response | Analysis |
|---|---|---|
| Open | TCP SYN-ACK | The service is listening on the port. |
| Closed | TCP RST | The Service is not listening on the port. |
| Filtered | No response from target or ICMP destination unreachable | The port is firewalled. |
TCP FIN Scan (-sF)
If an SYN or TCP Connect scan has been spotted by a firewall, you could instead use a TCP FIN Scan. This type of scan sends a FIN packet to a target port and expects an RST packet back. If the port is open, then the port would have ignored the FIN packet, however, if the port is closed, then an RST packet is sent.
TCP Fin scans are not effective against Windows-based systems as they will respond with RST packets regardless of the state.
To initiate a FIN scan, type:
nmap -sF -p 80 10.0.0.5
The expected output shows the open HTTP port on our container:
Starting Nmap 7.80 ( https://nmap.org ) at 2021-08-07 00:22 UTC
Nmap scan report for 10.0.0.5
Host is up (0.000088s latency).
PORT STATE SERVICE
80/tcp open|filtered http
MAC Address: 02:42:0A:00:00:05 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 0.39 seconds
To limit the port range when scanning use the -p tag to scan the first 100 ports. For example:
nmap -sF -p 1-100 10.0.0.5
A single port can be passed into the -p tag followed by the port as follows:
nmap -sF -p 80 10.0.0.5
TCP FIN scan status codes can be marked as the following:
| Status | Response | Analysis |
|---|---|---|
| Filtered | ICMP unreachable error received | Closed port should respond with RST. |
| Closed | RST packet received | Closed port should respond with RST. |
| Open/Filtered | No response received | Open port should drop FIN. |
UDP Scanning with Nmap
UDP is a common protocol among services that can handle packet loss, such as streaming, DNS queries, and DHCP.
UDP Scan (-sU)
Nmap sends UDP packets to the top 1000 most common ports unless a port is specified. If an ICMP unreachable message is received from the target, then the port is marked closed. If there is no response, it will be marked as open/filtered.
To initiate a UDP scan, type the following:
nmap -sU 10.0.0.5
It may take a while. Press enter to see the status at any point.
The output below shows the UDP port 53 as open due to the container running a DNS server bound to that port.
Starting Nmap 7.80 ( https://nmap.org ) at 2021-08-05 00:03 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000050s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 999 closed ports
PORT STATE SERVICE
5353/udp open zeroconf
Nmap done: 1 IP address (1 host up) scanned in 0.21 seconds
To limit the port range when scanning use the -p tag to scan the first 100 ports. For example:
nmap -sU -p 1-100 10.0.0.5
A single port can be passed into the -p tag followed by the port as follows:
nmap -sU -p 53 10.0.0.5
| Status | Response | Analysis |
|---|---|---|
| Open | Data returned from port | The service is listening on the port. |
| Closed | ICMP error message received | The service is not listening on the port. |
| Open/Filtered | No ICMP response from target | The port is firewalled or timed out. |
Host Discovery Scan
In cases where you would need to list all available hosts on a network, the Host Discovery Scan would be ideal.
By default the scan sends an ICMP echo request, TCP SYN to port 443, TCP ACK to port 80, and ICMP timestamp request.
Host Discovery Scan (-sn)
To initiate a host scan on a single host, type the following:
nmap -sn 10.0.0.5
The following is the expected output for our gravemind container:
Starting Nmap 7.80 ( https://nmap.org ) at 2021-08-07 01:10 UTC
Nmap scan report for 10.0.0.5
Host is up (0.00018s latency).
Nmap done: 1 IP address (1 host up) scanned in 0.00 seconds
Ranges can also be passed into the command by appending the subnet range:
nmap -sn 10.0.0.0/24
The following shows both containers (10.0.0.05, 10.0.0.06) are up and accessible:
Starting Nmap 7.80 ( https://nmap.org ) at 2021-08-07 01:09 UTC Nmap scan report for katacoda (10.0.0.1) Host is up (0.00097s latency). Nmap scan report for 10.0.0.5 Host is up (0.00079s latency). Nmap scan report for 10.0.0.6 Host is up (0.00023s latency). Nmap done: 256 IP addresses (3 hosts up) scanned in 2.61 seconds
Nmap Scripting Engine
The Nmap Scripting Engine allows users to create scripts that automate Nmap’s base functions. Nmap then executes these scripts in parallel making the process much more efficient.
Envolking Nmap Scripting Engine
The scripting engine can be accessed by using the -sC option, or if you have developed custom scripts, with the --script option. By typing nmap -sC 10.0.0.0/24 we can view additional information enumerated from our gravemind containers.
An example output would be as follows:
...
Nmap scan report for 10.0.0.5
Host is up (0.00019s latency).
Not shown: 997 closed ports
PORT STATE SERVICE
22/tcp open ssh
| ssh-hostkey:
| 2048 29:41:fe:e1:90:a4:70:91:ad:5c:12:67:1e:f8:6a:3d (RSA)
| 256 76:e5:9f:aa:69:20:19:85:05:ad:19:59:f6:b7:33:18 (ECDSA)
|_ 256 fc:6e:05:55:c2:eb:18:49:4b:96:9b:d8:e6:0e:a1:d7 (ED25519)
53/tcp open domain
| dns-nsid:
|_ bind.version: 9.11.5-P4-5.1+deb10u5-Debian
80/tcp open http
|_http-generator: Super Secret Application Platform
|_http-title: WebSploit Gravemind
Nmap scan report for 10.0.0.6
Host is up (0.00019s latency).
Not shown: 997 closed ports
PORT STATE SERVICE
22/tcp open ssh
| ssh-hostkey:
| 2048 29:41:fe:e1:90:a4:70:91:ad:5c:12:67:1e:f8:6a:3d (RSA)
| 256 76:e5:9f:aa:69:20:19:85:05:ad:19:59:f6:b7:33:18 (ECDSA)
|_ 256 fc:6e:05:55:c2:eb:18:49:4b:96:9b:d8:e6:0e:a1:d7 (ED25519)
53/tcp open domain
| dns-nsid:
|_ bind.version: 9.11.5-P4-5.1+deb10u5-Debian
80/tcp open http
|_http-generator: Super Secret Application Platform
|_http-title: WebSploit Gravemind
...
Features
Nmap has built-in scripts for each of the following and more!
- User Enumeration
- Group Enumeration
- Network Share Enumeration
- Service Enumeration
Advanced Nmap Scanning and the Nmap Scripting Engine
Visit the “Advanced Nmap Scanning and the Nmap Scripting Engine” Katacoda scenario to learn more about the scripting engine, and how we use the features listed above!