Advanced Firewall Evasion with Packet Fragmentation
In this step, we're going to explore a more advanced firewall evasion technique called packet fragmentation. Before we dive in, let's understand what packet fragmentation is. When data is sent over a network, it's divided into smaller units called packets. Some firewalls and Intrusion Detection Systems (IDS) are designed to inspect these packets for any signs of malicious activity. However, these security systems can have difficulty processing fragmented packets, which are packets that have been broken down into even smaller pieces. This difficulty can create an opportunity for us to bypass certain security measures during our scans.
We'll continue using our local HTTP server as the target for our scan. First, we need to navigate to the appropriate directory in the terminal where we performed our previous scan. This directory is where our project files are located, and it's important to be in this directory so that our commands work correctly. To do this, we'll run the following command:
cd /home/labex/project
Now that we're in the right directory, we're ready to run a scan using Nmap's fragmentation option. This option will break the IP packets into smaller fragments, making it harder for packet filters to detect our scan. Here's the command we'll use:
sudo nmap -f -Pn --reason -p 8000 localhost
Let's break down the additional option -f
in this command. The -f
option tells Nmap to fragment the IP packets. By doing this, we're making it more challenging for packet filters to analyze the packets and detect that we're performing a scan.
After running the command, you should see output similar to the following:
Starting Nmap 7.80 ( https://nmap.org ) at 2025-03-18 16:46 CST
Nmap scan report for localhost (127.0.0.1)
Host is up, received user-set (0.000062s latency).
Other addresses for localhost (not scanned): ::1
PORT STATE SERVICE REASON
8000/tcp open http-alt syn-ack ttl 64
Nmap done: 1 IP address (1 host up) scanned in 0.08 seconds
This output shows us the results of our scan. It tells us that the host (localhost) is up and that port 8000 is open.
Next, we want to save these results to a separate file. This way, we can refer back to them later and compare them with other scan results. To save the results, we'll run the following command:
sudo nmap -f -Pn --reason -p 8000 localhost > /home/labex/project/nmap_frag_scan.txt
The >
symbol in this command redirects the output of the Nmap scan to the specified file.
Now, let's check the contents of this file to make sure the results were saved correctly. We'll use the cat
command, which is used to display the contents of a file:
cat /home/labex/project/nmap_frag_scan.txt
The scan results might look similar to our previous scan, but the underlying technique is different. In this case, Nmap fragmented the IP packets into smaller pieces, which makes it harder for stateless packet filters to detect the scan.
Let's compare the two scan methods we've used so far: the regular scan and the fragmented scan. We'll create a new file to store this comparison. Here are the commands to do that:
echo "Comparing regular scan vs. fragmented scan:" > /home/labex/project/scan_comparison.txt
echo "---------------------------------------------" >> /home/labex/project/scan_comparison.txt
echo "" >> /home/labex/project/scan_comparison.txt
echo "1. Regular scan with -Pn:" >> /home/labex/project/scan_comparison.txt
cat /home/labex/project/nmap_scan.txt >> /home/labex/project/scan_comparison.txt
echo "" >> /home/labex/project/scan_comparison.txt
echo "2. Fragmented scan with -f -Pn:" >> /home/labex/project/scan_comparison.txt
cat /home/labex/project/nmap_frag_scan.txt >> /home/labex/project/scan_comparison.txt
These commands first write a header to the comparison file, then add the results of the regular scan and the fragmented scan to the file.
Now, let's examine our comparison by displaying the contents of the comparison file:
cat /home/labex/project/scan_comparison.txt
In our controlled environment, the results of the two scans might look similar. However, in real-world scenarios, these different techniques can have varying levels of success against different firewall configurations. Packet fragmentation (-f) is particularly effective against stateless packet filters. Stateless packet filters examine each packet independently and often cannot reassemble the fragments to inspect the complete packet, which gives our fragmented scan a better chance of bypassing these filters.
For even more fragmentation, Nmap allows you to increase the level by using multiple -f flags (e.g., -ff) or by specifying a custom Maximum Transmission Unit (MTU) size with the --mtu option. The MTU is the largest size of a packet that can be transmitted over a network. By specifying a custom MTU, you can control how the packets are fragmented.
This technique demonstrates how packet fragmentation can be used as an additional method to evade firewall detection when conducting security assessments.