Exploitation Scripts (`exploit`) with Nmap

Learn how to use Nmap’s exploitation scripts for testing and exploiting known security vulnerabilities in target systems.

Introduction

Nmap (Network Mapper) is a powerful open-source tool for network discovery and security auditing. While primarily used for reconnaissance and vulnerability scanning, Nmap also includes an advanced scripting engine (NSE - Nmap Scripting Engine) that allows users to automate tasks, including exploitation of vulnerable services. The exploit category of NSE scripts is specifically designed for testing and exploiting known security vulnerabilities in target systems.

In this article, we will delve into the details of using Nmap’s exploitation scripts, how they work, ethical considerations, and practical examples of their usage.

Understanding Nmap’s Exploit Scripts

Nmap’s scripting engine (NSE) consists of several categories of scripts, one of which is exploit. These scripts are designed to:

  • Exploit known vulnerabilities.
  • Test system defenses by running proof-of-concept (PoC) exploits.
  • Assist in penetration testing by automating exploitation steps.

How NSE Exploit Scripts Work

Each NSE script is written in Lua and operates using defined categories, including exploit. When an exploit script is executed, it attempts to:

  1. Identify if the target system is vulnerable.
  2. Execute the exploit against the vulnerable system.
  3. Provide output that confirms whether the exploit was successful.

Note: Many exploit scripts do not have automatic payload execution and require user confirmation or specific arguments to function correctly.

Installing and Updating NSE Scripts

Before using exploit scripts, ensure that your Nmap installation is up-to-date. To update the NSE script database, use:

sudo nmap --script-updatedb

To check available exploit scripts, list them using:

ls /usr/share/nmap/scripts/exploit-*.nse

You can also search for a specific keyword within exploit scripts using:

grep -i exploit /usr/share/nmap/scripts/*.nse

Using exploitation scripts against systems you do not own or have explicit permission to test is illegal and could result in severe consequences. Ethical hackers and penetration testers should always follow these guidelines:

  • Obtain explicit written permission before conducting any security testing.
  • Follow responsible disclosure practices when reporting vulnerabilities.
  • Use exploit scripts in controlled, legal environments such as penetration testing labs.

Practical Usage of Exploit Scripts

1. Exploiting MS08-067 (Windows SMB Vulnerability)

MS08-067 is a critical vulnerability in Windows SMB (Server Message Block) that allows remote code execution. It was famously exploited by the Conficker worm.

To test a target system for this vulnerability using Nmap:

sudo nmap -p 445 --script=smb-vuln-ms08-067 <target-IP>

If the system is vulnerable, Nmap will return a message confirming the exploitability.

2. Exploiting FTP with Anonymous Login

Some FTP servers allow anonymous logins, which can be leveraged to gain unauthorized access.

sudo nmap -p 21 --script=ftp-anon <target-IP>

If the FTP server allows anonymous login, you will see output similar to:

| ftp-anon: Anonymous FTP login allowed (220 Welcome message)

3. Exploiting Heartbleed Vulnerability (CVE-2014-0160)

The infamous Heartbleed vulnerability in OpenSSL allows an attacker to read sensitive memory from a server.

To test for Heartbleed using Nmap:

sudo nmap -p 443 --script=ssl-heartbleed <target-IP>

If the target is vulnerable, the script will return information confirming memory exposure.

4. Exploiting RDP Vulnerability (CVE-2019-0708 - BlueKeep)

The BlueKeep vulnerability (CVE-2019-0708) affects older versions of Windows RDP.

To check for BlueKeep using Nmap:

sudo nmap -p 3389 --script=rdp-vuln-ms12-020 <target-IP>

If the target is vulnerable, Nmap will display a confirmation.

Writing Your Own Exploit Script

Nmap allows users to write their own NSE scripts for exploitation. Here’s a basic example of an exploit script template:

local nmap = require "nmap"
local shortport = require "shortport"
local stdnse = require "stdnse"

portrule = shortport.port_or_service(80, "http")

action = function(host, port)
    local output = "Potential vulnerability detected on " .. host.ip
    return output
end

This script checks for HTTP services running on port 80 and returns a simple message.

To run the script, save it as custom-exploit.nse and execute:

sudo nmap -p 80 --script=custom-exploit <target-IP>

Limitations of Exploit Scripts

  1. Limited Exploit Availability: Nmap is not a full-fledged exploitation framework like Metasploit. Its exploit scripts are limited to known vulnerabilities.
  2. No Post-Exploitation Capabilities: Unlike Metasploit, Nmap does not provide session management or privilege escalation features.
  3. Reliability Issues: Some scripts may fail due to firewall rules, system patches, or incorrect configurations.

Nmap vs. Metasploit: Which One to Use?

FeatureNmapMetasploit
Primary UseScanning, ReconnaissanceExploitation, Post-Exploitation
Scripting LanguageLua (NSE)Ruby (MSF)
AutomationHighHigh
Exploit DatabaseLimitedExtensive
Post-ExploitationNoYes

Nmap is best suited for preliminary assessments and scanning, while Metasploit is the preferred tool for detailed exploitation and penetration testing.

Conclusion

Nmap’s exploitation scripts provide a powerful means of testing vulnerabilities in networks and applications. While not as comprehensive as dedicated exploit frameworks like Metasploit, they are useful for quick vulnerability assessments and security testing. However, ethical and legal considerations must always be followed to ensure responsible use.

By understanding how NSE exploit scripts work, security professionals can enhance their penetration testing capabilities and strengthen network security defenses.

In summary, Nmap’s exploit scripts offer a useful tool for vulnerability testing and penetration testing, but they should be used responsibly and in compliance with ethical and legal regulations.