---
title: "2. NMAP"
date: 2024-07-17
draft: false
---

https://nmap.org/book/man-port-scanning-techniques.html
 It is used to:
- Audit the security aspects of networks
- Simulate penetration tests
- Check firewall and IDS settings and configurations
- Types of possible connections
- Network mapping
- Response analysis
- Identify open ports
- Vulnerability assessment as well.
Flag:
TCP-SYN scan (`-sS`)
- Default. Popular. Send SYN, never completes handshake. 


If we disable port scan (`-sn`), Nmap automatically ping scan with `ICMP Echo Requests` (`-PE`). Once such a request is sent, we usually expect an `ICMP reply` if the pinging host is alive. The more interesting fact is that our previous scans did not do that because before Nmap could send an ICMP echo request, it would send an `ARP ping` resulting in an `ARP reply`. We can confirm this with the "`--packet-trace`" option. To ensure that ICMP echo requests are sent, we also define the option (`-PE`) for this.


#### Connect Scan

The Nmap [TCP Connect Scan](https://nmap.org/book/scan-methods-connect-scan.html) (`-sT`) uses the TCP three-way handshake to determine if a specific port on a target host is open or closed. The scan sends an `SYN` packet to the target port and waits for a response. It is considered open if the target port responds with an `SYN-ACK` packet and closed if it responds with an `RST` packet.

The `Connect` scan is useful because it is the most accurate way to determine the state of a port, and it is also the most stealthy.
It is also useful when the target host has a personal firewall that drops incoming packets but allows outgoing packets.
Connect scan is slower than other types of scans

## Filtered Ports

When a port is shown as filtered, it can have several reasons. In most cases, firewalls have certain rules set to handle specific connections. The packets can either be `dropped`, or `rejected`. When a packet gets dropped, `Nmap` receives no response from our target, and by default, the retry rate (`--max-retries`) is set to 1. This means `Nmap` will resend the request to the target port to determine if the previous packet was not accidentally mishandled.

Let us look at an example where the firewall `drops` the TCP packets we send for the port scan. Therefore we scan the TCP port **139**, which was already shown as filtered. To be able to track how our sent packets are handled, we deactivate the ICMP echo requests (`-Pn`), DNS resolution (`-n`), and ARP ping scan (`--disable-arp-ping`) again.

As a response, we receive an `ICMP` reply with `type 3` and `error code 3`, which indicates that the desired port is unreachable.

## Discovering Open UDP Ports

Some system administrators sometimes forget to filter the UDP ports in addition to the TCP ones. Since `UDP` is a `stateless protocol` and does not require a three-way handshake like TCP. We do not receive any acknowledgment. Consequently, the timeout is much longer, making the whole `UDP scan` (`-sU`) much slower than the `TCP scan` (`-sS`).
 disadvantage of this is that we often do not get a response back because `Nmap` sends empty datagrams to the scanned UDP ports, and we do not receive any response. So we cannot determine if the UDP packet has arrived at all or not. If the UDP port is `open`, we only get a response if the application is configured to do so.

#### XML Output

With the XML output, we can easily create HTML reports that are easy to read, even for non-technical people. This is later very useful for documentation, as it presents our results in a detailed and clear way. To convert the stored results from XML format to HTML, we can use the tool `xsltproc`.

# Performance

Scanning performance plays a significant role when we need to scan an extensive network or are dealing with low network bandwidth. We can use various options to tell `Nmap` how fast (`-T <0-5>`), with which frequency (`--min-parallelism <number>`), which timeouts (`--max-rtt-timeout <time>`) the test packets should have, how many packets should be sent simultaneously (`--min-rate <number>`), and with the number of retries (`--max-retries <number>`) for the scanned ports the targets should be scanned.

## Max Retries

Another way to increase the scans' speed is to specify the retry rate of the sent packets (`--max-retries`). The default value for the retry rate is `10`, so if `Nmap` does not receive a response for a port, it will not send any more packets to the port and will be skipped.


#### IMPORTANT INFO ABOUT FIREWALLS
Firewalls check whether individual network packets are passed/ignored/blocked. Mechanism designed to prevent unwanted connections. Software component. 

- IDS/IPS
	- Like a firewall, intrusion detection systems / intrusion prevention systems are software based. 
		- IDS - Scans net for potential attacks, analyzes them, and reports. 
		- IPS complements IDS by taking specific defensive measures if potential attk is detected 
		- Analysis based on pattern matching/signatures 

We know that when a port is shown as filtered it can be for several reasons. In most cases, firewalls have certain rules set to handle specific connections. Packets can be DROPPED or REJECTED. Dropped packets are ignored and no response is returned from host. 
This is different for rejected packets that are returned with a RESET(RST) flag. These packets can contain diff types of icmp error codes or contain nothing at all.

Potential rejected packet error codes:
- Net Unreachable
- Net Prohibited
- Host Unreachable
- Host Prohibited
- Port Unreachable
- Proto Unreachable

Nmap's TCP ACK scan (`-sA`) method is much harder to filter for firewalls and IDS/IPS systems than regular SYN (`-sS`) or Connect scans (`sT`) because they only send a TCP packet with only the `ACK` flag.
===

When a port is closed or open, the host must respond with an `RST` flag.
Unlike outgoing conns, all connection attempts (with SYN) from external nets are usually blocked by firewalls. Packets with the ACK flag are often passed by the firewall because the firewall cannot determine whether the connection was first established from the external network or the internal network. 


IDS = Examine all conns between host
	Help admins detect potential attacks. Manually decide how to handle. 
	Way to trigger: Aggressively scanning a single port and its service. Based on what happens we can detect if the network has monitoring
IPS = Take measures configured by administrator independently, automatically prevent potential attacks
	Method to detect IPS - Scan from a single host (VPS). If at any time this host is blocked and can no longer access the target, we know that the admin has taken a security measure. Accordingly, continue pentest with another VPS. (Virtual Private Server)

### Decoys
Scenario: Admins block specific subnets from different regions in principle, preventing any access to target. 
Alternatively; when IPS blocked us. 

DECOY scanning method (-D) becomes the right choice in these scenarios. With this method:
	Nmap generates various random IP addresses inserted into the IP header to disguise the origin of the sent packet
	We can generate random (RND) a specific number (i.e. 5) of IP addresses separated by a colon(:)
	Real IP is randomly placed between generated IP addresses.
	Critical point: Decoy must be alive. Otherwise, service on target may be unreachable due to SYN-flooding security mechanisms 

![](/img/Pasted image 20240713113648.png)

### DNS Proxying

By default nmap performs reverse dns resolution unless otherwise specified. 
DNS queries are also passed in most cases because the given web server is supposed to be found / visited.
DNS queries are made over UDP 53 
TCP 53 was previously only used for `zone transfers` between dns servers or data transfer > 512bytes. 
This is changing due to ipv6 and dnssec expansions. These changes cause dns reqs to be made via tcp 53

NMAP gives us a way to specify dns servers ourselves: 
`--dns-server <ns>,<ns>` 

This could be fundamental if we are in a demilitarized zone (DMZ). Company's DNS servers are usually more trusted than those from the internet. 
For example, we could use them to interact with the hosts of the internal network. As another example, we can use TCP port 53 as a source port for our scans. 
`--source port` If administrator uses the firewall to control this port and does not filter IDS/IPS properly, our TCP packets will be trusted. 




# Nmap Scripting Engine

---

Nmap Scripting Engine (`NSE`) is another handy feature of `Nmap`. It provides us with the possibility to create scripts in Lua for interaction with certain services. There are a total of 14 categories into which these scripts can be divided:

|**Category**|**Description**|
|---|---|
|`auth`|Determination of authentication credentials.|
|`broadcast`|Scripts, which are used for host discovery by broadcasting and the discovered hosts, can be automatically added to the remaining scans.|
|`brute`|Executes scripts that try to log in to the respective service by brute-forcing with credentials.|
|`default`|Default scripts executed by using the `-sC` option.|
|`discovery`|Evaluation of accessible services.|
|`dos`|These scripts are used to check services for denial of service vulnerabilities and are used less as it harms the services.|
|`exploit`|This category of scripts tries to exploit known vulnerabilities for the scanned port.|
|`external`|Scripts that use external services for further processing.|
|`fuzzer`|This uses scripts to identify vulnerabilities and unexpected packet handling by sending different fields, which can take much time.|
|`intrusive`|Intrusive scripts that could negatively affect the target system.|
|`malware`|Checks if some malware infects the target system.|
|`safe`|Defensive scripts that do not perform intrusive and destructive access.|
|`version`|Extension for service detection.|
|`vuln`|Identification of specific vulnerabilities.|

https://nmap.org/nsedoc/index.html

---
## Scanning Options

|**Nmap Option**|**Description**|
|---|---|
|`10.10.10.0/24`|Target network range.|
|`-sn`|Disables port scanning.|
|`-Pn`|Disables ICMP Echo Requests|
|`-n`|Disables DNS Resolution.|
|`-PE`|Performs the ping scan by using ICMP Echo Requests against the target.|
|`--packet-trace`|Shows all packets sent and received.|
|`--reason`|Displays the reason for a specific result.|
|`--disable-arp-ping`|Disables ARP Ping Requests.|
|`--top-ports=<num>`|Scans the specified top ports that have been defined as most frequent.|
|`-p-`|Scan all ports.|
|`-p22-110`|Scan all ports between 22 and 110.|
|`-p22,25`|Scans only the specified ports 22 and 25.|
|`-F`|Scans top 100 ports.|
|`-sS`|Performs an TCP SYN-Scan.|
|`-sA`|Performs an TCP ACK-Scan.|
|`-sU`|Performs an UDP Scan.|
|`-sV`|Scans the discovered services for their versions.|
|`-sC`|Perform a Script Scan with scripts that are categorized as "default".|
|`--script <script>`|Performs a Script Scan by using the specified scripts.|
|`-O`|Performs an OS Detection Scan to determine the OS of the target.|
|`-A`|Performs OS Detection, Service Detection, and traceroute scans.|
|`-D RND:5`|Sets the number of random Decoys that will be used to scan the target.|
|`-e`|Specifies the network interface that is used for the scan.|
|`-S 10.10.10.200`|Specifies the source IP address for the scan.|
|`-g`|Specifies the source port for the scan.|
|`--dns-server <ns>`|DNS resolution is performed by using a specified name server.|

## Output Options

|**Nmap Option**|**Description**|
|---|---|
|`-oA filename`|Stores the results in all available formats starting with the name of "filename".|
|`-oN filename`|Stores the results in normal format with the name "filename".|
|`-oG filename`|Stores the results in "grepable" format with the name of "filename".|
|`-oX filename`|Stores the results in XML format with the name of "filename".|

## Performance Options

|**Nmap Option**|**Description**|
|---|---|
|`--max-retries <num>`|Sets the number of retries for scans of specific ports.|
|`--stats-every=5s`|Displays scan's status every 5 seconds.|
|`-v/-vv`|Displays verbose output during the scan.|
|`--initial-rtt-timeout 50ms`|Sets the specified time value as initial RTT timeout.|
|`--max-rtt-timeout 100ms`|Sets the specified time value as maximum RTT timeout.|
|`--min-rate 300`|Sets the number of packets that will be sent simultaneously.|
|`-T <0-5>`|Specifies the specific timing template.|
