0% found this document useful (0 votes)
39 views17 pages

Nmap Tutorial

Uploaded by

Leon jr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views17 pages

Nmap Tutorial

Uploaded by

Leon jr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Nmap Analysis: Network Scanning and Security

Auditing

Abstract
Nmap (Network Mapper) stands as a crucial open-source tool for
network discovery, security auditing, and vulnerability assessment. This
tutorial aims to empower network administrators, security professionals,
and ethical hackers with a comprehensive understanding of Nmap's
functionalities. Beginning with installation guidance across multiple
platforms, it proceeds to explore scanning techniques, including SYN
scans, TCP connect scans, and UDP scans.

Port scanning, service and version detection, and OS fingerprinting are


discussed in detail, alongside insights into optimizing scan performance
and leveraging the Nmap Scripting Engine (NSE) for advanced tasks
like vulnerability scanning. Furthermore, evasion techniques for
firewalls and IDS, such as packet fragmentation and decoy scans, are
elucidated with practical examples.

The tutorial emphasizes best practices for output handling and analysis,
showcasing the integration of Nmap with other tools for streamlined
security workflows. By the conclusion, readers will be equipped to
conduct thorough network scans, identify vulnerabilities, and fortify
network security effectively.
Installation

Nmap is available for a wide range of platforms, including Linux,


Windows, and macOS. The installation process is straightforward and
can be accomplished through various methods, depending on the
operating system.

Linux

On Linux distributions like Debian, Ubuntu, RHEL, CentOS, and


Fedora, Nmap can be installed using package managers:
---------------------------------------------------------------------------------------
# Debian/Ubuntu
sudo apt-get install nmap

# RHEL/CentOS/Fedora
sudo yum install nmap
---------------------------------------------------------------------------------------

Windows
For Windows systems, visit the official Nmap website
(https://nmap.org/download.html) and download the latest version of the
Windows installer. Run the installer and follow the prompts to complete
the installation process.
Alternatively, you can install Nmap on Windows using a package
manager like Chocolatey. Open an elevated Command Prompt or
PowerShell window and run the following command:

--------------------------------------------------------------------------------------
choco install nmap
--------------------------------------------------------------------------------------

This will download and install the latest version of Nmap from the
Chocolatey repository.

After installation, you can run Nmap from the Command Prompt or
PowerShell by simply typing `nmap` followed by the desired options
and targets.
macOS
On macOS, Nmap can be installed using the Homebrew package
manager:

---------------------------------------------------------------------------------------
-brew install nmap
Basic Nmap Scan

After successful installation, you can begin with a basic Nmap scan to
discover active hosts on your network and gather information about
open ports and services. Open a terminal or command prompt and run
the following command:
---------------------------------------------------------------------------------------
nmap <target>
---------------------------------------------------------------------------------------
Replace `<target>` with the IP address, hostname, or network range you
want to scan. For example:
---------------------------------------------------------------------------------------
nmap 192.168.1.1 # Scan a single IP address
nmap 192.168.1.0/24 # Scan a subnet using CIDR notation
nmap example.com # Scan a domain name
---------------------------------------------------------------------------------------

This command will perform a default TCP connect scan and display
information about open ports, services, and other relevant details.
Scan Types

Nmap offers various scan types to gather different levels of information


and employ different scanning techniques. Here are some common scan
types:

- SYN Scan (-sS): A stealthy scan that determines open ports by sending
SYN packets and analyzing the responses. This is the default scan type.
- TCP Connect Scan (-sT): A comprehensive scan that establishes full
TCP connections with the target system to determine open ports. This is
the default scan type when running Nmap without root privileges.
- UDP Scan (-sU): Scans for open UDP ports on the target system.
- ACK Scan (-sA): Determines whether ports are filtered or unfiltered by
sending ACK packets.
- Window Scan (-sW): Attempts to determine open ports by analyzing
the TCP Window field in the response packets.
- Maimon Scan (-sM): A stealthy scan that uses FIN/ACK packets to
determine open ports.
To specify a scan type, use the corresponding option. For example:

-----------------------------------------------------------------------------------
nmap -sS 192.168.1.1 # SYN scan
nmap -sU 192.168.1.1 # UDP scan
-------------------------------------------------------------------------------------
Port Specification
By default, Nmap scans the most common 1000 ports. However, you
can specify the ports or port ranges you want to scan using the `-p`
option:
---------------------------------------------------------------------------------------
nmap -p 22,80,443 192.168.1.1 # Scan specific ports
nmap -p 1-65535 192.168.1.1 # Scan all ports
nmap -p U:53,T:21-25,80 192.168.1.1 # Scan TCP and UDP ports
nmap -p- 192.168.1.1 # Scan all ports (same as 1-65535)

You can also use service names instead of port numbers:

---------------------------------------------------------------------------------------
nmap -p http,https 192.168.1.1 # Scan HTTP and HTTPS ports
---------------------------------------------------------------------------------------
Service and Version Detection

Nmap can attempt to determine the version of the running services on


open ports using the `-sV` option:
---------------------------------------------------------------------------------------
nmap -sV 192.168.1.1
---------------------------------------------------------------------------------------

This can provide valuable information about the services, their versions,
and potential vulnerabilities. You can adjust the intensity level of
version detection using the `--version-intensity` option:

---------------------------------------------------------------------------------------
nmap -sV --version-intensity 8 192.168.1.1 # Higher intensity (0-9)
---------------------------------------------------------------------------------------

Higher intensity levels increase the possibility of accurate service and


version detection but may take longer to complete.
Operating System Detection

Nmap can also attempt to identify the operating system running on the
target system using TCP/IP stack fingerprinting. Enable OS detection
with the `-O` option:
---------------------------------------------------------------------------------------
nmap -O 192.168.1.1
---------------------------------------------------------------------------------------

This feature can help you understand the target environment and
potential vulnerabilities associated with specific operating systems.

Timing and Performance


Nmap provides various timing and performance options to adjust the
scan speed and behavior. You can use the `-T` option to set the timing
template:
- `-T0`: Paranoid (very slow and stealthy)
- `-T1`: Sneaky (slow and stealthy)
- `-T2`: Polite (slows down the scan to use less bandwidth and target
resources)
- `-T3`: Normal (default speed)
- `-T4`: Aggressive (faster, assumes a reasonably fast and reliable
network)
- `-T5`: Insane (fastest, assumes an extraordinarily fast network)

For example:
---------------------------------------------------------------------------------------
nmap -T4 192.168.1.1 # Aggressive scan
---------------------------------------------------------------------------------------

You can also adjust other timing-related options, such as `--host-


timeout`, `--min-rtt-timeout`, `--max-rtt-timeout`, `--min-parallelism`,
and `--max-parallelism`, to fine-tune the scan behavior.
NSE Scripts

Nmap includes a powerful scripting engine (NSE) that allows you to


automate and extend its functionality. You can run predefined scripts or
write your own to perform advanced tasks, such as vulnerability
scanning, brute-forcing, and information gathering.
To run a specific script, use the `--script` option:

---------------------------------------------------------------------------------------
nmap --script=banner 192.168.1.1 # Scan with the "banner" script
---------------------------------------------------------------------------------------
You can also use wildcards to run multiple scripts:

---------------------------------------------------------------------------------------
nmap --script=http* 192.168.1.1 # Run all HTTP-related scripts
---------------------------------------------------------------------------------------

To run the default set of scripts considered useful for discovery and safe,
use the `-sC` or `--script=default` option:

---------------------------------------------------------------------------------------
nmap -sC 192.168.1.1
nmap --script=default 192.168.1.1
---------------------------------------------------------------------------------------
Some useful NSE script examples include:

- `nmap -Pn --script=http-sitemap-generator example.com` (HTTP site


map generator)
- `nmap -n -Pn -p 80 --open -sV -vvv --script banner,http-title -iR 1000`
(Fast search for random web servers)
- `nmap -Pn --script=dns-brute domain.com` (Brute-force DNS
hostnames and subdomains)
- `nmap -n -Pn -vv -O -sV --script smb-enum*,smb-ls,smb-
mbenum,smb-os-discovery,smb-s*,smb-vuln*,smbv2* -vv 192.168.1.1`
(Safe SMB scripts)

Firewall and IDS Evasion

Nmap provides several options to help evade firewalls and network


intrusion detection systems (NIDS). These techniques can be useful
when scanning networks with strict security measures in place, but
should be used responsibly and with proper authorization.
1. Fragmentation (-f)
This option sends packets fragmented into smaller pieces, making it
harder for packet filters to detect the scan. The \`-f\` flag causes the IP
data to be split across many packets.
----------------------------------------------------------------------
nmap -f 192.168.1.1
---------------------------------------------------------------------

2. .Decoys (-D)
The decoy option sends scans from spoofed IP addresses, making it
appear as if the scan is coming from multiple sources. This can help
evade security systems that filter based on the source IP address.
-----------------------------------------------------------------------------
nmap -D 192.168.1.101,192.168.1.102,192.168.1.103 192.168.1.1
-----------------------------------------------------------------------------
3. Source Port (-g)
This option specifies the source port number to use for the scan. Some
firewalls and IDS may be configured to allow or block specific port
numbers.
-----------------------------------------------------------------------------
nmap -g 53 192.168.1.1 # Scan using source port 53 (DNS)
-----------------------------------------------------------------------------

4. Data Length (--data-length)


This option appends random data to sent packets to evade certain
IDS/IPS systems that may be looking for specific packet sizes or
patterns.
-----------------------------------------------------------------------------
nmap --data-length 200 192.168.1.1
-----------------------------------------------------------------------------
Output Handling

Nmap provides several options for handling and formatting scan output:
- Normal Output (-oN): Saves output in a normal human-readable
format.
- XML Output (-oX): Saves output in XML format.
- Grepable Output (-oG): Saves output in a format suitable for grep.
- All Formats (-oA): Saves output in all major formats (normal, XML,
and grepable).
- Append Output (--append-output): Appends a scan to a previous scan
file.
- Verbosity (-v/-vv): Increases the verbosity level of the output.
- Debugging (-d/-dd): Increases the debugging level of the output.
- Reason (--reason): Displays the reason a port is in a particular state.
- Open Only (--open): Only shows open (or possibly open) ports.
- Packet Trace (--packet-trace): Shows all packets sent and received.
Example output handling commands:
---------------------------------------------------------------------------------------
nmap -p80 -sV -oG - --open 192.168.1.1/24 | grep open # Scan for web
servers and grep open hosts
nmap -iR 10 -n -oX out.xml | grep "Nmap" | cut -d " " -f5 > live-hosts.txt
# Generate live host list
ndiff scan1.xml scan2.xml # Compare Nmap XML output files
xsltproc nmap.xml -o nmap.html # Convert Nmap XML to HTML
---------------------------------------------------------------------------------------
Scripting and Automation

Nmap supports scripting and automation through its built-in scripting


engine (NSE) and external tools. This allows you to automate complex
tasks, create custom scanning routines, and integrate Nmap into larger
security workflows.

- NSE Scripts: Nmap comes with a vast collection of pre-written scripts


for various tasks, such as vulnerability detection, brute-forcing, and
information gathering. You can also write your own scripts using the
Lua programming language.
- External Tools: Nmap can be integrated with external tools and
frameworks, such as Python, Bash, PowerShell, and others, enabling you
to create custom automation scripts and workflows.

Example scripting and automation tasks:

- Scheduled network scanning and reporting


- Continuous vulnerability monitoring
- Network inventory and asset management
- Penetration testing and red team operations
- Automated security auditing and compliance checks

Best Practices and Security Considerations

When using Nmap for network scanning and security auditing, it's
essential to follow best practices and security considerations:

- Obtain Proper Authorization: Always obtain permission before


scanning systems or networks you don't own or manage.
- Responsible Usage: Use Nmap responsibly and ethically, respecting
privacy and legal boundaries.
- Network Impact: Be aware of the potential impact of your scans on
network performance and system resources.
- Logging and Monitoring: Log and monitor your Nmap activities for
auditing and troubleshooting purposes.
- Regular Updates: Keep Nmap and its associated scripts up-to-date to
ensure access to the latest features and security improvements.
- Scripting Security: Review and test custom scripts thoroughly before
deploying them in production environments.
- Output Handling: Protect sensitive information contained in Nmap
output files by implementing appropriate access controls and encryption.
Conclusion

Nmap is a powerful and versatile tool that plays a crucial role in network
security and vulnerability assessment. By leveraging its extensive
capabilities, including port scanning, service and version detection,
operating system fingerprinting, scripting, and evasion techniques, you
can gain valuable insights into your network infrastructure and identify
potential security risks.
This tutorial has provided a comprehensive overview of Nmap's
functionalities, equipping you with the knowledge and skills to
effectively utilize this tool in your network security efforts. Whether you
are a network administrator, security professional, or ethical hacker,
mastering Nmap will enhance your ability to protect systems, discover
vulnerabilities, and maintain a secure network environment.

You might also like