iptables:test_the_firewall
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
iptables:test_the_firewall [2016/10/19 12:30] – [Discovering the Versions of Services on the Server] peter | iptables:test_the_firewall [2019/11/29 17:57] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== IPTables - Test the firewall ====== | ||
- | ===== Scan your Target for Open TCP Ports ===== | ||
- | |||
- | Scan our target host for open TCP ports. | ||
- | |||
- | There are actually a few TCP scans that nmap knows how to do. The best one to usually start off with is a SYN scan, also known as a " | ||
- | |||
- | ==== Setting Up the Packet Capture ==== | ||
- | |||
- | Use **tcpdump** to capture the traffic generated by the test. This will be used to analyze the packets sent and received in more depth later on if we need to. Let's create a directory within **~/ | ||
- | |||
- | <code bash> | ||
- | mkdir ~/ | ||
- | </ | ||
- | |||
- | We can start a **tcpdump** capture and write the results to a file in our **~/ | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump host target_ip_addr -w ~/ | ||
- | </ | ||
- | |||
- | By default, **tcpdump** will run in the foreground. | ||
- | |||
- | We can pause the running process by hitting **CTRL-Z**: | ||
- | |||
- | <code bash> | ||
- | CTRL-Z | ||
- | </ | ||
- | |||
- | This will pause the running process: | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | ^Z | ||
- | [1]+ Stopped | ||
- | </ | ||
- | |||
- | **NOTE**: | ||
- | |||
- | <code bash> | ||
- | bg | ||
- | </ | ||
- | |||
- | You should see a similar line of output, this time without the " | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | [1]+ sudo tcpdump host target_ip_addr -w ~/ | ||
- | </ | ||
- | |||
- | The command is now running in the background, watching for any packets going between our audit and target machines. | ||
- | |||
- | |||
- | ==== Run the SYN Scan ==== | ||
- | |||
- | With **tcpdump** recording our traffic to the target machine, we are ready to run nmap. We will use the following flags to get nmap to perform the actions we require: | ||
- | |||
- | * **-sS**: | ||
- | * **-Pn**: | ||
- | * **-p-**: | ||
- | * **-T4**: | ||
- | * **-vv**: | ||
- | * **--reason**: | ||
- | * **-oN**: | ||
- | |||
- | **NOTE**: | ||
- | |||
- | |||
- | Together, the command will look something like this: | ||
- | |||
- | <code bash> | ||
- | sudo nmap -sS -Pn -p- -T4 -vv --reason -oN ~/ | ||
- | </ | ||
- | |||
- | Even with the timing template set at 4, the scan will likely take quite some time as it runs through 65,535 ports. (The test may run for an hour or more). | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | Starting Nmap 6.49BETA4 ( https:// | ||
- | Initiating Parallel DNS resolution of 1 host. at 16:54 | ||
- | Completed Parallel DNS resolution of 1 host. at 16:54, 0.12s elapsed | ||
- | Initiating SYN Stealth Scan at 16:54 | ||
- | Scanning 198.51.100.15 [65535 ports] | ||
- | Discovered open port 22/tcp on 198.51.100.15 | ||
- | Discovered open port 80/tcp on 198.51.100.15 | ||
- | SYN Stealth Scan Timing: About 6.16% done; ETC: 17:02 (0:07:52 remaining) | ||
- | SYN Stealth Scan Timing: About 8.60% done; ETC: 17:06 (0:10:48 remaining) | ||
- | |||
- | . . . | ||
- | </ | ||
- | |||
- | |||
- | ==== Stop the tcpdump Packet Capture ==== | ||
- | |||
- | Once the scan is complete, we can bring our tcpdump process back into the foreground and stop it. | ||
- | |||
- | Bring it out of the background by typing: | ||
- | |||
- | <code bash> | ||
- | fg | ||
- | </ | ||
- | |||
- | Stop the process by holding the control key and hitting " | ||
- | |||
- | <code bash> | ||
- | CTRL-C | ||
- | </ | ||
- | |||
- | |||
- | ==== Analyzing the Results ==== | ||
- | |||
- | You should now have two files in your **~/ | ||
- | |||
- | Let's look at the nmap.results file first: | ||
- | |||
- | <code bash> | ||
- | less ~/ | ||
- | ~/ | ||
- | # Nmap 6.49BETA4 scan initiated Wed Aug 26 17:05:13 2015 as: nmap -sS -Pn -p- -T4 -vv --reason -oN / | ||
- | Increasing send delay for 198.51.100.15 from 0 to 5 due to 9226 out of 23064 dropped probes since last increase. | ||
- | Increasing send delay for 198.51.100.15 from 5 to 10 due to 14 out of 34 dropped probes since last increase. | ||
- | Nmap scan report for 198.51.100.15 | ||
- | Host is up, received user-set (0.00097s latency). | ||
- | Scanned at 2015-08-26 17:05:13 EDT for 2337s | ||
- | Not shown: 65533 closed ports | ||
- | Reason: 65533 resets | ||
- | PORT STATE SERVICE REASON | ||
- | 22/tcp open ssh | ||
- | 80/tcp open http syn-ack ttl 63 | ||
- | |||
- | Read data files from: / | ||
- | # Nmap done at Wed Aug 26 17:44:10 2015 -- 1 IP address (1 host up) scanned in 2336.85 seconds | ||
- | </ | ||
- | |||
- | This shows that port 22 and port 80 are open on the scanned host in order to allow SSH and HTTP traffic. | ||
- | |||
- | If we want to see the actual packet traffic that was sent to and received from the target, we can read the packets file back into tcpdump, like this: | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump -nn -r ~/ | ||
- | </ | ||
- | |||
- | This file contains the entire conversation that took place between the two hosts. | ||
- | |||
- | For instance, to view only the traffic sent to the target, you can type: | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump -nn -r ~/ | ||
- | </ | ||
- | |||
- | Likewise, to view only the response traffic, you can change the " | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump -nn -r ~/ | ||
- | </ | ||
- | |||
- | Open TCP ports would respond to these requests with a SYN packet. | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump -nn -r ~/ | ||
- | </ | ||
- | |||
- | This will show you only the successful SYN responses, and should match the ports that you saw in the nmap run: | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | reading from file packets, link-type EN10MB (Ethernet) | ||
- | 17: | ||
- | 17: | ||
- | </ | ||
- | |||
- | You can do more analysis of the data as you see fit. It has all been captured for asynchronous processing and analysis. | ||
- | |||
- | |||
- | ===== Scan your Target for Open UDP Ports ===== | ||
- | |||
- | Now that you have a good handle on how to run these tests, we can complete a similar process to scan for open UDP ports. | ||
- | |||
- | ==== Setting Up the Packet Capture ==== | ||
- | |||
- | Once again, let's create a directory to hold our results: | ||
- | |||
- | <code bash> | ||
- | mkdir ~/ | ||
- | </ | ||
- | |||
- | Start a tcpdump capture again. | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump host target_ip_addr -w ~/ | ||
- | </ | ||
- | |||
- | Pause the process and put it into the background: | ||
- | |||
- | <code bash> | ||
- | CTRL-Z | ||
- | bg | ||
- | </ | ||
- | |||
- | |||
- | ==== Run the UDP Scan ==== | ||
- | |||
- | Now, we are ready to run the UDP scan. Due to the nature of the UDP protocol, this scan will typically take **significantly** longer than the SYN scan. In fact, it could take over a day if you are scanning every port on the system. | ||
- | |||
- | Most of the flags will be the same as we used for the SYN scan. In fact, the only new flag is: | ||
- | |||
- | * **-sU**: | ||
- | |||
- | === Speeding up the UDP Test === | ||
- | |||
- | If you are worried about the amount of time this test takes, you may only want to test a subset of your UDP ports at first. | ||
- | |||
- | Because you are scanning your own infrastructure, | ||
- | |||
- | <code bash> | ||
- | sudo sysctl net.ipv4.icmp_ratelimit | ||
- | </ | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | net.ipv4.icmp_ratelimit = 1000 | ||
- | </ | ||
- | |||
- | The " | ||
- | |||
- | <code bash> | ||
- | sudo sysctl -w net.ipv4.icmp_ratelimit=0 | ||
- | </ | ||
- | |||
- | It is very important to revert this value after your test. | ||
- | |||
- | |||
- | === Running the Test === | ||
- | |||
- | Be sure to write the results to the **~/ | ||
- | |||
- | <code bash> | ||
- | sudo nmap -sU -Pn -p- -T4 -vv --reason -oN ~/ | ||
- | </ | ||
- | |||
- | Even with disabling ICMP rate limiting on the target, this scan took about 2 hours and 45 minutes during our test run. After the scan is complete, you should revert your ICMP rate limit (if you modified it) on the target machine: | ||
- | |||
- | <code bash> | ||
- | sudo sysctl -w net.ipv4.icmp_ratelimit=1000 | ||
- | </ | ||
- | |||
- | |||
- | ==== Stop the tcpdump Packet Capture ==== | ||
- | |||
- | Bring the tcpdump process back into the foreground on your audit machine by typing: | ||
- | |||
- | <code bash> | ||
- | fg | ||
- | </ | ||
- | |||
- | Stop the packet capture by holding control and hitting " | ||
- | |||
- | <code bash> | ||
- | CTRL-c | ||
- | </ | ||
- | |||
- | |||
- | ==== Analyzing the Results ==== | ||
- | |||
- | Now, we can take a look at the generated files. | ||
- | |||
- | The resulting nmap.results file should look fairly similar to the one we saw before: | ||
- | |||
- | <file bash ~/ | ||
- | less ~/ | ||
- | ~/ | ||
- | # Nmap 6.49BETA4 scan initiated Thu Aug 27 12:42:42 2015 as: nmap -sU -Pn -p- -T4 -vv --reason -oN / | ||
- | Increasing send delay for 198.51.100.15 from 0 to 50 due to 10445 out of 26111 dropped probes since last increase. | ||
- | Increasing send delay for 198.51.100.15 from 50 to 100 due to 11 out of 23 dropped probes since last increase. | ||
- | Increasing send delay for 198.51.100.15 from 100 to 200 due to 3427 out of 8567 dropped probes since last increase. | ||
- | Nmap scan report for 198.51.100.15 | ||
- | Host is up, received user-set (0.0010s latency). | ||
- | Scanned at 2015-08-27 12:42:42 EDT for 9956s | ||
- | Not shown: 65532 closed ports | ||
- | Reason: 65532 port-unreaches | ||
- | PORT STATE | ||
- | 22/ | ||
- | 80/ | ||
- | 443/udp open|filtered https | ||
- | |||
- | Read data files from: / | ||
- | # Nmap done at Thu Aug 27 15:28:39 2015 -- 1 IP address (1 host up) scanned in 9956.97 seconds | ||
- | </ | ||
- | |||
- | A key difference between this result and the SYN result earlier will likely be the amount of ports marked open|filtered. | ||
- | |||
- | Analyzing the tcpdump output is also significantly more difficult because there are no connection flags and because we must match up ICMP responses to UDP requests. | ||
- | |||
- | We can see how nmap had to send out many packets to the ports that were reported as open|filtered by asking to see the UDP traffic to one of the reported ports: | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump -nn -P out -r ~/ | ||
- | </ | ||
- | |||
- | You will likely see something that looks like this: | ||
- | |||
- | Output | ||
- | |||
- | <code bash> | ||
- | reading from file / | ||
- | 14: | ||
- | 14: | ||
- | 14: | ||
- | 14: | ||
- | 14: | ||
- | 14: | ||
- | </ | ||
- | |||
- | Compare this to the results we see from one of the scanned ports that was marked as " | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump -nn -P out -r ~/ | ||
- | </ | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | reading from file / | ||
- | 13: | ||
- | </ | ||
- | |||
- | We can try to manually reconstruct the process that nmap goes through by first compiling a list of all of the ports that we're sending UDP packets to using something like this: | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump -nn -P out -r ~/ | ||
- | </ | ||
- | |||
- | Then, we can see which ICMP packets we received back saying the port was unreachable: | ||
- | |||
- | <code bash> | ||
- | sudo tcpdump -nn -P in -r ~/ | ||
- | </ | ||
- | |||
- | We can see then take these two responses and see which UDP packets never received an ICMP response back by typing: | ||
- | |||
- | <code bash> | ||
- | comm -3 outgoing response | ||
- | </ | ||
- | |||
- | This should mostly match the list of ports that nmap reported (it may contain some false positives from lost return packets). | ||
- | |||
- | |||
- | ===== Host and Service Discovery ===== | ||
- | |||
- | We can run some additional tests on our target to see if it is possible for nmap to identify the operating system running or any of the service versions. | ||
- | |||
- | Let's make a directory to hold our versioning results: | ||
- | |||
- | <code bash> | ||
- | mkdir ~/ | ||
- | </ | ||
- | |||
- | |||
- | ==== Discovering the Versions of Services on the Server ==== | ||
- | |||
- | We can attempt to guess the versions of services running on the target through a process known as fingerprinting. | ||
- | |||
- | A tcpdump wouldn' | ||
- | |||
- | The nmap scan we need to use is triggered by the **-sV** flag. Since we already did SYN and UDP scans, we can pass in the exact ports we want to look at with the -p flag. Here, we'll look at 22 and 80 (the ports that were shown in our SYN scan): | ||
- | |||
- | <code bash> | ||
- | sudo nmap -sV -Pn -p 22,80 -vv --reason -oN ~/ | ||
- | </ | ||
- | |||
- | If you view the file that results, you may get information about the service running, depending on how " | ||
- | |||
- | <code bash> | ||
- | less ~/ | ||
- | </ | ||
- | |||
- | shows | ||
- | |||
- | <file bash ~/ | ||
- | # Nmap 6.49BETA4 scan initiated Thu Aug 27 15:46:12 2015 as: nmap -sV -Pn -p 22,80 -vv --reason -oN / | ||
- | Nmap scan report for 198.51.100.15 | ||
- | Host is up, received user-set (0.0011s latency). | ||
- | Scanned at 2015-08-27 15:46:13 EDT for 8s | ||
- | PORT STATE SERVICE REASON | ||
- | 22/tcp open ssh | ||
- | 80/tcp open http syn-ack ttl 63 nginx 1.4.6 (Ubuntu) | ||
- | Service Info: OS: Linux; CPE: cpe:/ | ||
- | |||
- | Read data files from: / | ||
- | Service detection performed. Please report any incorrect results at https:// | ||
- | # Nmap done at Thu Aug 27 15:46:21 2015 -- 1 IP address (1 host up) scanned in 8.81 seconds | ||
- | </ | ||
- | |||
- | Here, you can see that the test was able to identify the SSH server version and the Linux distribution that packaged it as well as the SSH protocol version accepted. | ||
- | |||
- | |||
- | ==== Discovering the Host Operating System ==== | ||
- | |||
- | We can try to have nmap guess the host operating system based on characteristics of its software and responses as well. This works much in the same way as service versioning. | ||
- | |||
- | The flag we need in order to perform operating system detection is **-O** (the capitalized letter " | ||
- | |||
- | <code bash> | ||
- | sudo nmap -O -Pn -vv --reason -oN ~/ | ||
- | </ | ||
- | |||
- | If you view the output file, you might see something that looks like this: | ||
- | |||
- | <code bash> | ||
- | less ~/ | ||
- | </ | ||
- | |||
- | shows | ||
- | |||
- | <file bash ~/ | ||
- | # Nmap 6.49BETA4 scan initiated Thu Aug 27 15:53:54 2015 as: nmap -O -Pn -vv --reason -oN / | ||
- | Increasing send delay for 198.51.100.15 from 0 to 5 due to 65 out of 215 dropped probes since last increase. | ||
- | Increasing send delay for 198.51.100.15 from 5 to 10 due to 11 out of 36 dropped probes since last increase. | ||
- | Increasing send delay for 198.51.100.15 from 10 to 20 due to 11 out of 35 dropped probes since last increase. | ||
- | Increasing send delay for 198.51.100.15 from 20 to 40 due to 11 out of 29 dropped probes since last increase. | ||
- | Increasing send delay for 198.51.100.15 from 40 to 80 due to 11 out of 31 dropped probes since last increase. | ||
- | Nmap scan report for 198.51.100.15 | ||
- | Host is up, received user-set (0.0012s latency). | ||
- | Scanned at 2015-08-27 15:53:54 EDT for 30s | ||
- | Not shown: 998 closed ports | ||
- | Reason: 998 resets | ||
- | PORT STATE SERVICE REASON | ||
- | 22/tcp open ssh | ||
- | 80/tcp open http syn-ack ttl 63 | ||
- | No exact OS matches for host (If you know what OS is running on it, see https:// | ||
- | TCP/IP fingerprint: | ||
- | OS: | ||
- | OS: | ||
- | OS: | ||
- | OS: | ||
- | OS: | ||
- | OS: | ||
- | OS: | ||
- | OS: | ||
- | OS: | ||
- | |||
- | Uptime guess: 1.057 days (since Wed Aug 26 14:32:23 2015) | ||
- | Network Distance: 2 hops | ||
- | TCP Sequence Prediction: Difficulty=245 (Good luck!) | ||
- | IP ID Sequence Generation: All zeros | ||
- | |||
- | Read data files from: / | ||
- | OS detection performed. Please report any incorrect results at https:// | ||
- | # Nmap done at Thu Aug 27 15:54:24 2015 -- 1 IP address (1 host up) scanned in 30.94 seconds | ||
- | </ | ||
- | |||
- | We can see that in this case, nmap has no guesses for the operating system based on the signature it saw. If it had received more information, | ||
- | |||
- | Operating system identification can help an attacker determine which exploits may be useful on the system. Configuring your firewall to respond to fewer inquiries can help to hinder the accuracy of some of these detection methods. | ||
- | |||
- | ===== Conclusion ===== | ||
- | |||
- | Testing your firewall and building an awareness of what your internal network looks like to an outside attacker can help minimize your risk. The information you find from probing your own infrastructure may open up a conversation about whether any of your policy decisions need to be revisited in order to increase security. | ||
- | |||
- | To get an idea of some policy improvements for your firewall, check out [[https:// | ||
- | |||
- | |||
- | ===== References ===== | ||
- | |||
- | https:// |
iptables/test_the_firewall.1476880221.txt.gz · Last modified: 2020/07/15 09:30 (external edit)