Nmap Beyond Basics: Using NSE Scripts for Vulnerability Scanning

If you work in cybersecurity, the Network Mapper (Nmap) is likely the first tool you run in the morning and the last one you close at night. It is the industry standard for network discovery and port scanning.

Most beginners learn the standard incantation: nmap -sC -sV <target>. They know that -sV probes for service versions. They know that -sC runs “default scripts.” But very few stop to ask: What exactly are those scripts doing?

Here is the reality: Nmap is not just a port scanner. It is a powerful, extensible execution engine capable of vulnerability detection, advanced reconnaissance, and even exploitation. It does this through the Nmap Scripting Engine (NSE).

If you are only using Nmap to find open ports, you are driving a Ferrari in first gear. In this guide, we are going to shift gears. We will explore how to use NSE to turn Nmap from a simple doorbell-ringer into a full-fledged vulnerability scanner that can rival tools like Nessus or Nikto for specific tasks.

What is the Nmap Scripting Engine (NSE)?

The NSE is one of Nmap’s most powerful and flexible features. It allows users to write (and share) simple scripts using the Lua programming language to automate a wide variety of networking tasks.

When you install Nmap, you aren’t just installing a binary; you are installing a database of over 600 scripts (and growing). These scripts are categorized to help you choose the right tool for the job. Understanding these categories is critical for an ethical hacker, as some scripts are safe, while others can crash a server.

The Key Categories

  • auth: Checks for default credentials (e.g., ftp-anon).
  • broadcast: Uses broadcast packets to discover hosts not listed in your target range.
  • brute: Performs brute-force attacks against login portals.
  • default: The scripts run when you use -sC or -A. These are generally considered safe and fast.
  • discovery: Queries registries (like WHOIS or SNMP) to learn more about the target.
  • dos: Denial of Service scripts. Warning: These will crash vulnerable services. Use with extreme caution.
  • exploit: Actually attempts to exploit a vulnerability.
  • intrusive: Scripts that generate a lot of noise or might disrupt the target.
  • malware: Checks if the target is infected with known malware.
  • safe: Scripts that are unlikely to crash the target or trigger alarms.
  • version: Extended service version detection.
  • vuln: The gold mine. Checks for specific known vulnerabilities.

For this guide, we will focus heavily on the vuln, exploit, and discovery categories.


Scenario 1: The Ghost of SMB (Detecting MS17-010)

One of the most famous vulnerabilities in history is EternalBlue (MS17-010), the exploit behind the WannaCry ransomware. While you could fire up Metasploit to check for this, Metasploit is heavy and slow to load. Nmap is instantaneous.

Scanning for SMB vulnerabilities is a perfect use case for NSE.

The Command

Instead of running a generic scan, we will tell Nmap to look specifically for SMB scripts related to vulnerabilities.

nmap -p 445 --script smb-vuln-ms17-010 <target_ip><br>

Breaking it Down

  • -p 445: We know SMB runs on port 445. Don’t waste time scanning other ports.
  • --script smb-vuln-ms17-010: This calls the specific Lua script designed to check for the EternalBlue flaw.

The Output

If the target is vulnerable, Nmap will give you a clear, actionable report:

Host script results:
| smb-vuln-ms17-010: 
|   VULNERABLE:
|   Remote Code Execution vulnerability in Microsoft SMBv1 servers (ms17-010)
|     State: VULNERABLE
|     IDs:  CVE:CVE-2017-0143
|     Risk factor: HIGH
|       A critical remote code execution vulnerability exists in Microsoft SMBv1...

Pro Tip: You can use wildcards. If you want to run all SMB vulnerability checks, you can use:

nmap -p 445 --script smb-vuln* <target_ip></code>

This will check for MS17-010, MS08-067 (Conficker), and others simultaneously. This is a lightweight alternative to a full vulnerability scanner run.


Scenario 2: Web Reconnaissance Without Dirbuster

When you encounter a web server (Port 80/443), your instinct might be to launch Gobuster or Dirbuster immediately. However, NSE has a surprisingly robust set of HTTP scripts that can do the preliminary heavy lifting for you, often with more stealth.

The http-enum Script

This script acts like a mini-Nikto. It checks for common folders, default files, and known vulnerable applications.

nmap -p 80 --script http-enum <target_ip>

It uses a fingerprint file to identify products. For example, if it sees a /wp-login.php, it knows it’s dealing with WordPress and will report it.

Finding WAFs (Web Application Firewalls)

Before you launch a heavy attack, you need to know if a WAF is blocking you. The http-waf-detect and http-waf-fingerprint scripts are essential here.

nmap -p 80,443 --script http-waf-detect --script-args="http-waf-detect.aggro" <target_ip>

Note the use of --script-args. NSE scripts often accept arguments to modify their behavior. In this case, we are telling the script to be “aggressive” in its detection methods to ensure we don’t miss a silent WAF.


Scenario 3: Brute Forcing with Precision

While tools like Hydra or Medusa are dedicated brute-forcers, Nmap is surprisingly capable for quick checks, especially when you want to test default credentials across a whole network.

Let’s say you are on an internal penetration test and want to check if any MSSQL servers are using the default sa account with a weak password.

Bash

nmap -p 1433 --script ms-sql-brute --script-args userdb=users.txt,passdb=passwords.txt &lt;target_ip>

Why use Nmap for this?

The advantage here is parallelism. If you provide Nmap with a list of targets (CIDR notation like 192.168.1.0/24), it can check the entire subnet for weak SQL passwords in one go. Hydra is generally designed to attack one host at a time (though it can do lists, managing the output is messier).

Warning: Brute force scripts are in the intrusive category. They generate thousands of log entries. Do not run this if you are trying to be stealthy.


Scenario 4: The “Vuln” Category – The Mini-Nessus

If you want to throw caution to the wind and ask Nmap to find everything it can, you can invoke the entire vuln category.

nmap -sV --script vuln <target_ip>

This command runs every script categorized as a vulnerability check against the detected services.

  • If it finds port 21 open, it checks for FTP vulnerabilities (like vsftpd backdoor).
  • If it finds port 80, it checks for Slowloris susceptibility, CSRF, and SQLi.
  • If it finds port 3389 (RDP), it checks for BlueKeep (CVE-2019-0708).

The Downside

This scan is slow and loud. Because it is running dozens of scripts, it will take significant time to complete. Furthermore, some of these scripts might be unstable. It is not a replacement for a professional scanner like Nessus or Qualys, but for a CTF or a quick spot-check, it is invaluable.


Advanced Tip: Debugging and Updating

“Why isn’t the script working?”

If you run a script and get no output, it usually means the script didn’t find the vulnerability. However, sometimes the script failed to run due to a timeout or configuration error.

To see what is happening under the hood, use the -d (debug) flag.

nmap -d --script smb-vuln-ms17-010 <target>

This will show you the step-by-step execution of the Lua code, including any errors or reasons why the script decided to quit early.

Keeping the Database Fresh

New scripts are added to Nmap frequently. If you are using the version of Nmap that came pre-installed on your Kali Linux ISO from 2023, you are missing out on detections for 2024/2025 vulnerabilities.

Always update your NSE database before a major engagement:

sudo nmap --script-updatedb

This command updates the script.db mapping file, ensuring Nmap knows where to find the latest scripts on your system.


Writing Your Own Scripts (The Next Level)

The true power of NSE lies in its extensibility. The scripts are written in Lua, a lightweight scripting language that is easy to read.

On a Linux system, these scripts are located in /usr/share/nmap/scripts/.

Go ahead and open one. nano /usr/share/nmap/scripts/ftp-anon.nse

You will see it is just a text file. It has a description, a rule (defining when it should run, e.g., “port 21 is open”), and an action (what to do, e.g., “try logging in as anonymous”).

If you encounter a new, custom vulnerability in a specific application during an assessment, you don’t have to wait for the Nmap developers to release a script. You can copy an existing script, modify the payload in the action section, and save it as my-custom-exploit.nse.

This capability allows you to build a custom scanner for proprietary applications or unique environment configurations.


Conclusion

Nmap is often underestimated because it is so ubiquitous. We take it for granted. But by leveraging the Nmap Scripting Engine, you transform a simple mapping tool into a sophisticated threat intelligence gathering platform.

The difference between a script kiddie and a professional is often efficiency. A script kiddie launches 10 different tools to check 10 different things. A professional knows that nmap --script can likely do 8 of those things in a single, clean command.

Your Homework:

  1. Run ls /usr/share/nmap/scripts | grep http on your machine.
  2. Pick three scripts you have never heard of.
  3. Read their description inside the file.
  4. Test them in your home lab or a controlled CTF environment.

Stop treating Nmap like a hammer. It’s a Swiss Army Knife. Start using the other blades.

What is your favorite “hidden gem” NSE script? Let me know in the comments below!