Advanced Target Specification
In this step, we'll delve into more advanced Nmap target specification techniques. Nmap is a powerful tool in network reconnaissance, and it offers various ways to specify multiple targets in a single command. This is extremely useful when you need to scan multiple hosts or networks at once, saving you time and effort.
Let's start by creating a text file that contains multiple IP addresses we want to scan. We'll include the localhost (127.0.0.1) and a couple of other local IP addresses. The localhost is a special IP address that refers to the current device itself. By including it, we can test the scanning process on our own machine.
cd /home/labex/project/nmap_scans
echo "127.0.0.1" > targets.txt
echo "127.0.0.2" >> targets.txt
echo "127.0.0.3" >> targets.txt
In the above code, the cd
command changes the current working directory to /home/labex/project/nmap_scans
. The echo
command is used to output text. The >
operator creates a new file and writes the text into it, while the >>
operator appends the text to an existing file. So, we first create a file named targets.txt
and write 127.0.0.1
into it. Then we append 127.0.0.2
and 127.0.0.3
to the same file.
Now, let's take a look at the file we just created to make sure the IP addresses are correctly added.
cat targets.txt
The cat
command is used to display the contents of a file. After running this command, you should see the following output:
127.0.0.1
127.0.0.2
127.0.0.3
Next, we'll use the -iL
flag in Nmap to scan all the IP addresses listed in the targets.txt
file at once. The -iL
flag tells Nmap to read the targets from a file.
nmap -iL targets.txt > multiple_targets_scan.txt
Here, the nmap
command is used to perform the network scan. The -iL
flag specifies the file containing the targets. The >
operator redirects the output of the scan to a file named multiple_targets_scan.txt
.
Let's check the results of the scan:
cat multiple_targets_scan.txt
After running this command, you should see the scan results for all three IP addresses. Note that 127.0.0.1 will usually show open ports because it's the localhost and there might be some services running on it. However, 127.0.0.2 and 127.0.0.3 might not respond because they're not typically configured on most systems.
Another way to specify multiple targets is to use space notation or CIDR notation directly in the Nmap command.
nmap 127.0.0.1 127.0.0.2 > space_notation_scan.txt
In this command, we use spaces to separate the IP addresses. This tells Nmap to scan both 127.0.0.1 and 127.0.0.2. The output of the scan is redirected to a file named space_notation_scan.txt
.
Let's check the results:
cat space_notation_scan.txt
We can also use CIDR notation to scan a range of IP addresses. CIDR (Classless Inter-Domain Routing) notation is a way to represent a range of IP addresses in a concise form.
nmap 127.0.0.0/30 > cidr_notation_scan.txt
The 127.0.0.0/30
in CIDR notation represents a range of IP addresses from 127.0.0.0 to 127.0.0.3, which is a total of 4 addresses. The output of this scan is redirected to a file named cidr_notation_scan.txt
.
Let's check the results:
cat cidr_notation_scan.txt
Finally, let's explore how to exclude specific targets from a scan. This is useful when you want to scan a network but don't want to include certain hosts.
nmap 127.0.0.0/30 --exclude 127.0.0.3 > exclude_scan.txt
In this command, we use the --exclude
flag to tell Nmap to skip the IP address 127.0.0.3
when scanning the range 127.0.0.0/30
. The output of the scan is redirected to a file named exclude_scan.txt
.
Let's check the results:
cat exclude_scan.txt
You should see scan results for 127.0.0.0, 127.0.0.1, and 127.0.0.2, but not for 127.0.0.3.
These advanced target specification techniques allow you to precisely control which hosts Nmap scans, making your network reconnaissance more efficient and targeted.