Learn how to use Nmap, a powerful network scanning and discovery tool, to analyze networks, find live hosts, identify services and open ports.

Beginner30 minutes

Step 1: Understanding Nmap

Nmap (Network Mapper) is a free and open-source tool used for network discovery and security auditing. It can be used to:

- Scan networks to find live hosts

- Detect open ports and services running on hosts

- Determine operating systems and versions

- Detect potential vulnerabilities

Nmap uses raw IP packets to probe targets in a stealthy manner, making it an invaluable tool for network administrators and security professionals.

Practice Exercise

Research and explain the legal and ethical considerations when using network scanning tools like Nmap.

Show Solution
When using Nmap, it's crucial to respect legal and ethical boundaries. Only scan networks and systems you own or have explicit permission for. Unauthorized scanning of networks you don't own could be considered illegal hacking. Always inform network administrators before scanning to avoid misunderstandings or potential legal issues. Additionally, be mindful of scanning policies, which may restrict or prohibit certain scanning activities.

Step 2: Basic Nmap Scanning

To perform a basic TCP connect scan on a target host or network range, use the following command:

nmap [target]

This will scan the specified target (IP address or hostname) and report any open ports and services it finds. For example:

nmap 192.168.1.100

You can also scan a range of IP addresses:

nmap 192.168.1.0/24

nmap 192.168.1.100
nmap 192.168.1.0/24

Practice Exercise

Scan your local network (or a test network if you don't have permission) and analyze the output. What information does Nmap provide about the live hosts it finds?

Show Solution
When scanning a network, Nmap will list the live hosts it detects. For each live host, it will report:

- The IP address
- Hostname (if it can be resolved)
- Operating system guess based on fingerprinting
- Open ports and services running on those ports

This gives you a basic overview of the devices on the network, their operating systems, and the services they are running. Analyzing this information can help identify potential security risks or misconfigurations.

Step 3: Port Scanning Techniques

Nmap offers several scanning techniques to detect open ports on a target host or network. The most common techniques are:

- TCP Connect Scan (-sT): This is the default scan type. It attempts to establish a full TCP connection with the target host.

- SYN Scan (-sS): This scan is more stealthy and can bypass some firewalls. It sends SYN packets to port and analyzes responses.

- UDP Scan (-sU): Used to scan for open UDP ports.

- Version Scan (-sV): Attempts to determine the version of services running on open ports.

For example, to perform a stealth SYN scan:

nmap -sS 192.168.1.100

nmap -sT 192.168.1.100 # TCP Connect Scan
nmap -sS 192.168.1.100 # SYN Scan 
nmap -sU 192.168.1.100 # UDP Scan
nmap -sV 192.168.1.100 # Version Scan

Practice Exercise

You are a security consultant tasked with scanning a client's web server. Which Nmap scan types would you use, and why? How would you interpret the results to identify potential vulnerabilities?

Show Solution
For a web server, I would use a combination of TCP SYN scan (`-sS`) to stealthily detect open TCP ports, and a version scan (`-sV`) to identify the specific services and versions running on those open ports.

The SYN scan is a good choice because it is less likely to be detected by firewalls or intrusion detection systems. The version scan will give me detailed information about the services, which I can then cross-reference against vulnerability databases to identify potential risks.

For example, if the server is running an outdated version of Apache with known vulnerabilities, I would flag that as a potential issue in my report and recommend upgrading to a patched version. Similarly, if I find any other services with known exploits, those would be considered high-risk and need to be addressed.

Step 4: Host Discovery

Before scanning a network, it's often useful to discover live hosts first. Nmap provides options to perform host discovery scans:

- Ping Scan (-sn): Sends ICMP echo requests to determine if hosts are online.

- TCP SYN Scan (-sS -P0): Sends TCP SYN packets to detect live hosts by analyzing responses.

- ARP Ping Scan (-PR): Uses ARP requests to discover live hosts on the local network.

For example, to perform a ping scan on a network range:

nmap -sn 192.168.1.0/24

This will report all live hosts it finds within that network range.

nmap -sn 192.168.1.0/24 # Ping Scan
nmap -sS -P0 192.168.1.0/24 # TCP SYN Scan
nmap -PR 192.168.1.0/24 # ARP Ping Scan

Practice Exercise

You are setting up a new office network and need to inventory all devices connected to it. Describe the steps you would take using Nmap to discover live hosts and gather information about them.

Show Solution
To inventory devices on a new office network using Nmap, I would follow these steps:

1. **Perform a Ping Scan**: Run `nmap -sn 192.168.1.0/24` to quickly discover all live hosts on the 192.168.1.0/24 network range. This will give me a list of IP addresses with active devices.

2. **Scan Live Hosts**: For each live host IP discovered, run a more comprehensive scan like `nmap -sV -O 192.168.1.x` to detect open ports, services, and attempt to identify the operating system.

3. **Analyze Results**: Review the Nmap output for each host. Note the open ports, services running on those ports (and versions if detected), and the operating system guess.

4. **Catalog Devices**: Create an inventory spreadsheet or document, listing each live host IP, hostname (if resolved), operating system, open ports, and services. This gives me a comprehensive view of all devices on the network.

5. **Cross-Reference Services**: For any critical services detected (e.g. web servers, databases), cross-reference the versions against vulnerability databases to identify potential risks that need to be addressed.

By following this process, I can quickly build an accurate inventory of devices on the new network and identify potential security concerns that may need further investigation or remediation.

Step 5: Advanced Scanning Techniques

Nmap offers many advanced scanning techniques and options to perform more comprehensive network reconnaissance. Some examples include:

- OS Detection (-O): Attempts to detect the operating system of target hosts.

- Script Scanning (--script): Runs additional Nmap scripts to gather more information or exploit vulnerabilities.

- Output Formats (-oN, -oX): Saves output in different formats like normal, XML, or grepable.

- Timing Options (-T): Adjusts timing parameters to make scans faster or more stealthy.

- Firewall Evasion (-f, --scan-delay, --source-port): Techniques to bypass firewalls or IDS/IPS systems.

For example, to perform an aggressive scan with scripts and output to XML:

nmap -sV -O -p- --script=vuln 192.168.1.100 -oX output.xml

nmap -O 192.168.1.100 # OS Detection
nmap --script=vuln 192.168.1.100 # Vulnerability Script Scan
nmap -T4 192.168.1.100 # Faster timing
nmap -f --scan-delay 5s 192.168.1.100 # Fragment packets, delay scan

Practice Exercise

You are tasked with auditing a client's internal network for potential vulnerabilities. Describe an Nmap command line that would perform a comprehensive scan, including firewall evasion techniques, and output the results in a machine-readable format for further analysis.

Show Solution
To perform a comprehensive vulnerability scan on a client's internal network while evading firewalls and IDS/IPS systems, and output the results in a machine-readable format, I would use the following Nmap command:

```
nmap -sS -sV -O -p- --script=vuln --script-args=unsafe=1 --max-hostgroup 512 --stats-every 30s --max-retries 3 --host-timeout 30m --min-hostgroup 128 --max-parallelism 512 --defeat-rst-ratelimit --send-ip --spoof-mac 0 --source-port 53 --data-length 9800 --randomize-hosts --scan-delay 5s -D 192.168.1.1,192.168.1.2,192.168.1.3 -oX output.xml 192.168.1.0/24
```

This command performs the following actions:

- `-sS`: Performs a stealthy SYN scan to detect open ports
- `-sV`: Attempts to detect service/version info on open ports
- `-O`: Attempts to detect the target operating system
- `-p-`: Scans all 65535 ports instead of just most common 1000
- `--script=vuln --script-args=unsafe=1`: Runs vulnerability detection scripts in "unsafe" mode
- Randomization and timing options to make the scan stealthier and bypass firewalls/IDS
- `-D`: Uses a decoy list of IP addresses to confuse defensive systems
- `-oX output.xml`: Outputs results in XML format for further analysis

This comprehensive command will thoroughly scan the target network (192.168.1.0/24) for vulnerabilities while attempting to evade defensive systems. The XML output can then be parsed and analyzed by other tools or scripts.

Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.

Sign in

Click to access the login or register cheese