Unveiling Network Secrets: A Deep Dive into Nmap

Ionut Vasile
8 min readJun 5, 2023

--

In today’s interconnected world, understanding the complex networks that enable our digital communications is more important than ever. Whether you’re a seasoned cybersecurity professional or a budding tech enthusiast, one tool stands out as indispensable for network exploration and security auditing — Nmap. Nmap, short for Network Mapper, is a powerful open-source utility trusted by IT and cybersecurity experts worldwide to scan networks, discover hosts, and detect vulnerabilities.

Born out of the ever-evolving landscape of network security, Nmap is not just a tool, but a reflection of the complexities inherent in modern networks. With its range of functionalities, Nmap allows users to probe their networks for weaknesses, making it an essential part of any network administrator or cybersecurity professional’s toolkit. This comprehensive article aims to guide you through the fascinating world of Nmap. From understanding its various switches and scan types, diving deep into the Nmap Scripting Engine (NSE), to exploring advanced techniques for firewall evasion, this journey will equip you with the knowledge to harness the power of Nmap effectively and responsibly. So, buckle up and prepare to uncover the secrets of your network with Nmap.

Nmap Switches

Nmap switches are command-line options that control the operation of Nmap, from setting the scan type, adjusting the output, to more advanced and specific tasks. They are used to customize the scan according to your needs, from simple host discovery tasks to comprehensive vulnerability analysis:

Here are some commonly used Nmap switches:

1. `-p`: Port scan. This allows you to specify the port range for the scan. For example, `-p 1–100` scans ports from 1 to 100.

2. `-sS`: SYN scan. This is also known as half-open scanning because it doesn’t complete the TCP handshake.

3. `-sT`: Connect scan. This switch completes the usual TCP handshake.

4. `-sU`: UDP scan. This is used to identify open UDP ports.

5. `-sV`: Version detection. It interrogates ports to determine the software version they are running.

6. `-O`: OS detection. It uses various techniques to determine the operating system of the target.

7. `-v`: Verbose mode. This switch increases the amount of information Nmap provides.

8. `-A`: Aggressive scan. This is a combination of several scanning options, including OS detection, version detection, script scanning, and traceroute.

9. `-F`: Fast scan. This scans fewer ports than the default scan.

10. `-T<0–5>`: Timing template. This controls the timing and performance of the scan. 0 is slow and cautious, while 5 is fast and aggressive.

11. `-Pn`: No ping. This tells Nmap not to ping the target before scanning. Useful when the target has ICMP echo requests blocked but still has open ports.

12. `-iL`: Input from list. This allows you to run the scan on a range of targets specified in a given file.

13. `-oN`: Normal output. This allows you to save the output to a file.

14. `-oX`: XML output. This allows you to save the scan results in an XML file.

15. ` — script`: Script scan. This uses the Nmap Scripting Engine (NSE) to perform more advanced scanning tasks, which we will cover later in this article.

Nmap scans

Nmap scan types determine how Nmap will interact with the target host in an attempt to find open ports and services. Different scan types are used because different networks, systems, and firewalls respond differently to various types of probes. They are chosen based on the specifics of the scanning job at hand, including the nature of the target and the amount of information needed.

Here are some of the most common scan types:

1. TCP Connect Scan (-sT): This is the most basic form of TCP scanning. It attempts to establish a full TCP connection with the target. If the connection is established, the port is considered open. This type of scan is easy to detect and can be easily blocked by firewalls.

2. SYN Scan (-sS): Also known as a “half-open” scan, this is a more subtle way to detect open ports without completing the TCP handshake. A SYN packet is sent, and if a SYN/ACK packet is received, the port is considered open. The final ACK packet is never sent, hence the term “half-open”. This scan is faster and less likely to be logged than a TCP Connect scan, but it still requires raw packet privileges.

3. UDP Scan (-sU): This type of scan checks for open UDP ports. UDP, being a connectionless protocol, makes this kind of scan less accurate and slower. An ICMP Port Unreachable error is an indication of a closed port, but many systems limit ICMP error messages, which can make this scan less reliable.

4. FIN, NULL, and Xmas Scans (-sF, -sN, -sX): These are stealthy scans that can bypass certain firewalls or logging systems. They involve sending TCP packets with specific flags set (or unset, in the case of NULL). Closed ports should reply with a RST packet according to the TCP standard, while no response is assumed to mean the port is open. These scans don’t work against all systems.

5. ACK Scan (-sA): This scan type tests whether a firewall is stateful (keeps track of connections) or not. It does not determine whether a port is open or closed, but whether it’s filtered or unfiltered.

6. Version Detection (-sV): After open ports are discovered, this scan can be used to determine more details about the services running on those ports.

7. Idle Scan (-sI): This is a stealthy scan method that involves spoofing the IP address of another host (the “zombie” host). The scan can reveal open ports without revealing the IP address of the scanner to the target.

8. IP Protocol Scan (-sO): This scan determines which IP protocols (TCP, ICMP, IGMP, etc.) are supported by the target. Each protocol is reported as being open if it responds affirmatively to the protocol-specific probe.

9. Script Scan (-sC or — script): Uses the Nmap Scripting Engine (NSE) to run scripts against scan targets, allowing for very flexible behavior based on the scripting language Lua.

NSE Scripts

NSE scripts are scripts run by Nmap’s Scripting Engine, providing more advanced network scanning capabilities. They are used for a wide variety of purposes, from advanced service detection to vulnerability detection and even exploitation. These scripts are written in the Lua programming language. There are hundreds of NSE scripts bundled with Nmap, which are organized into several categories:

1. Auth: These scripts are used to test whether you can authenticate to services without a password or with a weak one.

2. Broadcast: These scripts are used to discover more hosts on the network by sending broadcast frames.

3. Brute: These scripts perform brute-force password guessing.

4. Default: Scripts that are safe to run, useful, and don’t take a lot of time. They are run in default script scan `-sC`.

5. Discovery: These scripts interact with a service to discover more about the network.

6. Dos: These scripts check whether a service is vulnerable to a denial-of-service (DoS) attack.

7. Exploit: These scripts exploit security vulnerabilities.

8. External: These scripts rely on third-party resources to provide additional information about a target, like its geographic location or whether it appears on a blacklist.

9. Fuzzer: These scripts send random and unexpected data to services to see how they react.

10. Intrusive: Scripts that could potentially crash services or be considered aggressive.

11. Malware: These scripts are used to discover if a host is infected with malware or is part of a botnet.

12. Safe: These scripts are designed to not crash services and they’re also network light.

13. Version: These scripts try to determine what software version is running on a particular port.

14. Vuln: These scripts check for specific known vulnerabilities in services.

Firewall Evasion

These are techniques used by Nmap to evade firewall detection and restrictions during scanning. These techniques are used when scanning targets that have firewalls in place that may block or alter standard scanning techniques and when the network or host you are scanning is behind a firewall.

Here are some techniques for firewall evasion that Nmap provides:

1. Fragmentation (`-f` or ` — mtu`): Nmap can fragment packets, making it harder for packet filters, intrusion detection systems, and other tools to identify what you’re doing. The `-f` option causes the requested scan to use tiny fragmented IP packets, while ` — mtu` lets you specify your own MTU size.

2. Decoy Scanning (`-D`): Nmap can spoof decoy scan source IP addresses to provide a misleading trail. The idea is to append the IP addresses of other hosts to your scans, making it appear that these hosts are scanning the target network.

3. Idle/Zombie Scanning (`-sI`): In this method, Nmap uses an idle system (zombie) on the network to send the packets, making it appear that the idle host is doing the scanning.

4. MAC Address Spoofing (` — spoof-mac`): If you’re scanning on a local network, Nmap can change the MAC address it’s using. This can help avoid MAC-based filters.

5. Using a Specific Network Interface (`-e`): If your machine has multiple network interfaces, you can specify which one Nmap should use for its scans.

6. TCP ACK Scan (`-sA`): This scan type can be used to map out firewall rulesets, identifying filtered ports and unfiltered ports.

7. FTP Bounce Scan (`-b`): This technique involves using an FTP server to port scan another host. It exploits the FTP protocol’s `PORT` command to request access to a port on a third machine, providing a means to bypass firewalls that filter incoming packets.

8. Timing Options (`-T`): Using slower timing options can make the scan stealthier.

9. Source Port Manipulation (` — source-port` or `-g`): This technique involves sending packets with the source port set to something that the firewall might expect and allow (like port 53 for DNS).

Remember, these techniques should be used responsibly and ethically. Never use them to probe networks or systems without explicit permission. Misusing these techniques can lead to serious legal consequences.

Conclusion

In our digital age, the importance of understanding and securing network infrastructure cannot be overstated. Nmap, as a robust and versatile tool, plays a pivotal role in this domain, enabling users to explore and audit their networks with remarkable depth and flexibility. This open-source utility has proven itself as a cornerstone of network security, providing both professionals and enthusiasts with the means to reveal hosts, open ports, services, and even potential vulnerabilities.

Throughout this article, we have delved into the core functionalities of Nmap, explored its wide array of switches, understood various scan types, and ventured into the powerful Nmap Scripting Engine. We also discussed advanced techniques for firewall evasion that demonstrate the tool’s comprehensive capabilities. However, the responsibility of using such a potent tool ethically and legally lies with us.

As you step forward with this knowledge, remember that the power of Nmap extends beyond its ability to scan and probe. It is a testament to the open-source ethos — a commitment to shared knowledge, collaborative improvement, and widespread empowerment in cybersecurity. As we continue navigating our interconnected world, tools like Nmap will only become more essential in our efforts to understand and secure the vast digital landscapes we inhabit.

--

--

Ionut Vasile
Ionut Vasile

Written by Ionut Vasile

An eager learner with a wide range area of understanding in different technologies.

No responses yet