Using Nmap Scripting Engine (NSE)
The Nmap Scripting Engine (NSE) is a powerful tool that allows you to expand Nmap's capabilities using Lua scripts. Lua is a lightweight programming language, and these scripts can perform a wide variety of tasks. For example, they can help with advanced service detection, which means identifying what services are running on a network device more precisely. They can also be used for vulnerability scanning, which is crucial for finding security weaknesses in a system.
Let's use NSE to gather more information about our HTTP server. NSE scripts are grouped into different categories. These categories include "default", which contains commonly used and safe scripts; "discovery", which is for finding network services; "safe", which won't cause any harm to the target system; and "vuln", which is used for vulnerability scanning.
First, we want to see what HTTP-related scripts are available. To do this, we'll use the following command:
ls /usr/share/nmap/scripts/http*
This command lists all the NSE scripts in the /usr/share/nmap/scripts/
directory that start with http
. When you run this command, you'll see a list of scripts designed to interact with HTTP services. These scripts can perform different functions, from simple information gathering, like getting the title of a web page, to more complex tasks such as scanning for vulnerabilities in an HTTP service.
Now, let's use the http-title
script to extract the title from our web page. The http-title
script is designed to look at an HTTP service and find the title of the web page it serves. Here's the command to run this script:
sudo nmap --script=http-title -p 8000 localhost > /home/labex/project/nmap_script_output.txt
In this command, sudo
is used to run the command with administrative privileges because some Nmap operations may require them. nmap
is the main command, and --script=http-title
tells Nmap to use the http-title
script. -p 8000
specifies that we're targeting port 8000, which is where our HTTP server is running. localhost
is the target, which means we're running the scan on our own machine. The >
symbol redirects the output of the command to the file /home/labex/project/nmap_script_output.txt
.
Let's check the output of the command. We can do this by using the following command:
cat /home/labex/project/nmap_script_output.txt
The cat
command is used to display the contents of a file. When you run this command, you should see output that includes information about the title of our web page. The output might look like this:
Starting Nmap 7.80 ( https://nmap.org ) at 2023-XX-XX XX:XX XXX
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000040s latency).
PORT STATE SERVICE
8000/tcp open http-alt
|_http-title: Directory listing for /
Nmap done: 1 IP address (1 host up) scanned in X.XX seconds
Now, let's try a more comprehensive scan using multiple scripts. The "default" category includes safe scripts that provide useful information about the target. To run all the scripts in the "default" category against our HTTP server on port 8000, we use the following command:
sudo nmap --script=default -p 8000 localhost > /home/labex/project/nmap_default_scripts.txt
This command is similar to the previous one, but instead of specifying a single script, we're telling Nmap to run all the scripts in the "default" category.
Let's examine the results of this scan. We can do this by using the cat
command again:
cat /home/labex/project/nmap_default_scripts.txt
The output will contain more detailed information about our HTTP server, gathered by various NSE scripts that run by default.
Finally, let's explore the script help feature to learn more about a specific script. If you want to know more about the http-title
script, you can use the following command:
nmap --script-help=http-title
This command displays documentation for the http-title
script. The documentation includes the script's purpose, how to use it, and the format of its output. Understanding script documentation is essential when using NSE for specific tasks because it helps you know what the script does and how to use it correctly.