30 Mins

6. Nmap Scripting Engine (NSE) & Vulnerability Scanning

Nmap Scripting Engine (NSE) Structure and Categories

The Nmap Scripting Engine (NSE) is one of Nmap's most powerful features, allowing users to automate networking tasks, service discovery, and vulnerability detection. NSE scripts are written in the Lua programming language. They are organized into categories, which determine their behavior and safety levels.

Categories act as tags, allowing operators to trigger groups of scripts efficiently. For instance, the vuln category focuses on scripts that check for known vulnerabilities, while the safe category ensures scripts do not alter the target state.

Security Operator Note: Always consult the Nmap documentation for the specific risk level of a category before scanning production environments.
CategoryDescription
defaultScripts enabled by default with -sC; fast and non-intrusive.
vulnScripts that identify known security vulnerabilities.
authTests authentication credentials or bypasses.

Running NSE Scripts Against Services

Running NSE scripts is done via the --script flag. You can specify a single script file, a category, or a directory. When targeting a specific service, you should ideally combine the NSE execution with service version detection (-sV).

The default category is the recommended starting point for reconnaissance. The vuln category is more aggressive and should only be used when specific service exploitation research is required.

Security Operator Note: Combine -sV with --script to ensure scripts have the necessary version information to operate correctly.
Text
# Perform service detection and run default scripts
nmap -sV -sC 192.168.1.10

# Run only vulnerability scanning scripts against a target
nmap -sV --script vuln 192.168.1.10

Passing Arguments to NSE Scripts

Many NSE scripts require specific configuration or input to function. The --script-args flag allows passing variables to these scripts. The format is 'script-name.arg=value'.

This is particularly useful for scripts that perform brute-force attacks or require specific API keys, usernames, or target resource paths.

Security Operator Note: Multiple arguments can be separated by commas within the same --script-args flag.
Text
# Example of passing credentials to an FTP brute force script
nmap -p 21 --script ftp-brute --script-args userdb=users.txt,passdb=pass.txt 192.168.1.10

Inspecting Lua NSE Script Logic

Since NSE scripts are standard Lua files, you can inspect their internal logic to understand how they interact with the target. NSE scripts are stored in the scripts directory of the Nmap installation.

Below is a simple Python simulation demonstrating how a basic network scanner might logic-check an open port before performing a scripted task, mirroring the modular approach of NSE.

Security Operator Note: Use grep to search for specific functionality within the Nmap scripts directory to find relevant tools quickly.
Python 3
import socket

def check_service_vulnerability(ip, port):
    # Simulate connecting to a service
    print(f'Checking {ip}:{port} for known signatures...')
    # In real NSE, this would load a Lua script that sends payloads
    return 'Potential vulnerability detected: CVE-2023-XXXXX'

# Simulate target check
result = check_service_vulnerability('127.0.0.1', 80)
print(result)

Knowledge Check

Ready to test your understanding of 6. Nmap Scripting Engine (NSE) & Vulnerability Scanning?

Back