Active Members Fi8sVrs Posted December 16, 2015 Active Members Report Posted December 16, 2015 Bluffing Network Scan ToolsWhat you see may not be what you getDate of writing: 10/2015Author: Emeric Nasi - emeric.nasi[at]sevagas.comWebsite: SevagasLicense: This work is licensed under a Creative Commons Attribution 4.0 International License Note: Pentesting / Network security basics are recommended to understand this paper.1. Introduction1.1. Relying on automatic toolsI have often seen tutorials or even pro pentesters relying too much, if not uniquely on automaticscanning tools. It may be due to lack of knowledge, or more often due to lack of available time.Obviously when you have 5 week days to complete a full corporate pentest, you can only do your best,and it won't be perfect!Anyway I just wanted to write a little something to remind that automatic tools results are alwaysinterpretation of incoming data. Tools expect a certain behavior from systems, and will make someassumptions. If you do not know this, you may be fooled by false positives or worse loose your valuabletime!Just a quick example, when you successfully ping a machine, so you assume it's alive. But in fact, it justmeans you received and ICMP Echo Reply packet in answer to sending an ICMP Echo Request. This echoreply could have been send by another machine than the targeted one. It can be part of a tarpit strategy!Now let's focus on some major features of security scan tools:OS fingerprintingPort ScanningBanner grabbing1.2. Set upI've ran my tests using two virtual machines running both on Linux. The targeted host is an Ubuntu 14.04Linux with kernel 3.13.0 and has IP 192.168.0.16.I targeted this host with free widely known scanning tools which are:Nmap (https://nmap.org/)OpenVas (OpenVAS - OpenVAS - Open Vulnerability Assessment System)Nikto (Nikto2 | CIRT.net)All the trick code in this article are summed up in a bash script in section §6 as an Annex at the end of thedocument. You can copy past this code in a bash, run it as root, modify it and have fun!2. OS Fingerprinting2.1. Behind the sceneOS Fingerprinting, whether it is active (ex. using Nmap) or passive (ex. using p0f) relies on how the targetIP stack behaves in a way that distinguishes it from other systems.Automatic tools will compare several behaviors and packets fields to a signature database. Automatictools will assume that IP stack behavior will always be the same for a given OS. This is indeed normallythe case, unless you modify it.2.2. How it can be trickedOS fingerprinting can be tricked by modifying the behavior of the system TCP/IP stack.One of the values which are looked at by OS fingerprinting tools is an IP default Time To Leave (TTL) fieldin the packet. It is also very easy to modify on a Linux Box. The default TTL value on Linux is generally setto 64. As you can check by doing: # sysctl net.ipv4.ip_default_ttl net.ipv4.ip_default_ttl = 64 You can change this value to 128 which is, among others, Microsoft Windows default TTL value. # sysctl -w net.ipv4.ip_default_ttl=128Let's see how Nmap and OpenVas react to this simple command.Example 1: Nmap OS detectionThe -O option is used by Nmap to fingerprint. The way Nmap fingerprints OS is fully explained athttps://nmap.org/book/man-os-detection.html# nmap -O 192.168.0.16Starting Nmap 6.00 ( http://nmap.org ) at 2015-09-27 18:13 CESTNmap scan report for 192.168.0.16Host is up (0.00042s latency).Not shown: 995 closed portsPORT STATE SERVICE21/tcp filtered ftp...MAC Address: xxxxxxxxxxxxxxxxxxxxxxNo exact OS matches for host (If you know what OS is running on it, see http://nmap.org/submit/ )....Nmap builds signatures based on various behaviors it expects from the target IP stack.In this case we can see we came up with something outside of Nmap signature base. However we justchange one value, the TTL. With more kernel tweaking, we could totally fake another OS signature on aLinux BoxWe can use the osscan-guess option to see what Nmap thinks our machine is most probably:# nmap -O --osscan-guess 192.168.0.16Starting Nmap 6.00 ( http://nmap.org ) at 2015-09-27 18:17 CESTNmap scan report for 192.168.0.16Host is up (0.00042s latency).....MAC Address: xxxxxxxxxxxxxxxxxxxxxDevice type: WAP|media device|general purpose|webcam|specialized|printer|PBXRunning (JUST GUESSING): Netgear embedded (92%), Western Digital embedded (92%), Linux2.6.X|3.X|2.4.X (89%), AXIS Linux 2.6.X (88%), Crestron 2-Series (87%), Lexmark embedded (87%), Vodaviembedded (86%), Comtrend embedded (86%)Example 2: OpenVAS OS detectionThe same test was done with OpenVas open source vulnerability scanner. The default scan does also runOS fingerprinting mechanism, based on ICMP fingerprinting.3. Port scanning3.1. Behind the scenePort scanning also relies on IP stack behavior, but this time on expected normal implementation of theRFC (which is not always the case on all OS but let's not start on this topic!).The most common scans used by tools are Syn scan and TCP connect scan. There are also other variantof exotic scans playing with various possibilities of the TCP flag. Here is the normal behavior, expectedby port scan tools:If the port is opened:Scanner <---... SYN ...---> TargetScanner <---... SYN,ACK ...---> TargetIf the port is closed:Scanner <---... SYN ...---> TargetScanner <---... RST ...---> TargetFor any other behavior, the port is considered filtered, behind a firewall.Concerning other types of scans, you can find more information on this post I wrote a few years ago:http://www.sevagas.com/?Iptables-firewall-versus-nmap-and,31'>http://www.sevagas.com/?Iptables-firewall-versus-nmap-and,313.2. How to detect itKnowing how it works makes it possible to build a defense. In the code below we use iptables to logvarious scan attempts, including exotic scans. iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix"Firewall>XMAS scan " iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix "Firewall>XMAS-PSH scan " iptables -A INPUT -p tcp --tcp-flags ALL ALL -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix"Firewall>XMAS-ALL scan "iptables -A INPUT -p tcp --tcp-flags ALL FIN -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix"Firewall>FIN scan " iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -m limit --limit 6/h --limit-burst 1 -j LOG --logprefix"Firewall>SYN/RST scan " iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix"Firewall>SYN/FIN scan " iptables -A INPUT -p tcp --tcp-flags ALL NONE -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix"Firewall>Null scan "# Prevent Syn port scan (will detect syn on 5 different port in less than 3 sec) iptables -A INPUT -m psd -p tcp -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix "Firewall>Syn scandetected "Note that the previous lines will only logs the scan, they will not prevent it. The goal is more to show thatby knowing the TCP flags used by ports scaners, we can detect them.Concerning Syn and TCP connect scan, since they correspond to normal TCP connection behavior, theyare much more difficult to detect. One way, as seen above, is to use the iptables psd module which candetect a certain amount of connection to different ports from the same IP in a given lapse of time.This will not work is the scan is really slow. Another possibility is to ''trap'' some ports which are neverused on your systems. For example port 445 (SMB) on a Linux webserver or port 23 (Telnet) anywhere.In these cases you are sure a hit on the port is a malicious activity. This is easy to do using iptables.3.3. How to trick port scanningAn easy way to trick Syn and TCP connect port scanning is to pretend some or all ports are opened. Thiscan be done by responding Syn,Ack packet to any Syn query. This way, closed and filtered ports can bethought opened.We can do that using iptables TARPIT target:iptables -A fw-aggressive -p tcp --destination-port 21 -j TARPITiptables -A fw-aggressive -p tcp --destination-port 137:139 -j TARPITiptables -A fw-aggressive -p tcp --destination-port 445 -j TARPITThe TARPIT target will answer Syn/ACK packet to any Syn query on the filtered ports. This is useful totrick automatic port scanner, especially those used by fast spreading worms and botnets.Example 1: OpenVAS port scanningAs you can see, this default OpenVas scan assumes that our rogue ports are opened. It fell into the tarpit.In reality, none of these ports are opened on the machine.A well-crafted tarpit could lead vulnerability scanner to try to find vulnerabilities on the supposedservices and also return false positives. This is the case for example when you implement a Honeypot.The Dionaea honeypot (http://dionaea.carnivore.it/) for example does a good job implementing falseinsecure SMB services (however default banner grabbing will detect Dionaea as a service if it is notmodified).This is a great way to loose a lot of time for the ''attacker''...Example 2: Nmap port scanningThe iptables TARPIT does not work against latest versions of Nmap# nmap 192.168.0.16...Not shown: 995 closed portsPORT STATE SERVICE21/tcp filtered ftp139/tcp filtered netbios-ssn445/tcp filtered microsoft-ds...We can see that despite the tarpit, Nmap detects that the ports are being filtered. On other versions,Nmap will declare the ports as Open. In fact it is possible to detect the use of iptables TARPIT. This isbecause the iptables TARPIT model returns a SYN/ACK packet which has singularities (for example, a maxsized segment of 0, which is not normally the case). This is well explained in this linkhttps://www.fishnetsecurity.com/6labs/blog/port-scanning-through-tarpits.'>https://www.fishnetsecurity.com/6labs/blog/port-scanning-through-tarpits. It is thus possible to detectthe usage of TARPIT iptables target for the scanner.Anyway, even if Nmap can now bypass this simple use of TARPIT; It is possible to build a moresophisticated one which returns a Syn/Ack packet in a much more realistic fashion.4. Banner Grabbing4.1. Behind the sceneBanner grabbing process is pretty simple, the scanner will act this time on application layer. It will send aclassic application request to the service, which will answer back. In the answer, the tools expect to findthe service name and version.4.2. How to trick itThe most common methods used to trick both port scanning and banner grabbing is to use honeypots orsophisticated tarpits applications.If you want to test a quick demo, here is a nice little trick from Nick Marsh (seehttp://marc.info/?l=nmap-dev&m=142811327627471&w=2) to send HTTP application scanner to aninfinite loophole.Run the next command on the target machine# ncat -lkv -p 8080 --sh-exec "echo 'HTTP/1.1 200 OK\r\n'; cat /dev/urandom"Note: This is not something to run on production system, it may eat up your CPU after several scanattempts!Example 1: Using NmapA classic Nmap scan will just show the port is opened:8080/tcp open http-proxyHowever it is another story for Nmap script which attempts banner grabbing.Nmap -sC or Nmap -A options will result in Nmap trying to pull an infinit HTTP message. This makesNmap run infinitely while consuming a lot of resources, and at one point, Nmap crashes.Example 2: Using Niktonikto -host 192.168.0.16 -port 8080- Nikto v2.1.4---------------------------------------------------------------------------+ Target IP: 192.168.0.16+ Target Hostname: 192.168.0.16+ Target Port: 8080+ Start Time: 2015-09-28 09:34:03---------------------------------------------------------------------------+ Server: No banner retrievedIt is the same for Nikto. During the scan, while nothing happened, Nikto process was eating up all its hostCPU resource and I had to finally kill it.5. To ConcludeWith easy to implement examples, we can see that it is possible to trick security tools. When runningvulnerability scanners or any other kind of security tools, remember what is going on behind the scene.Remember that the tools answers are ''assumptions'' and not definitive answers.The Security tools can also be prone to vulnerabilities when running which can result in the scannerbeing attacked by its target as we saw in the HTTP infinite tarpit case.Warning: The methods in this article are all related to Security by Obscurity. It is generally recommendedto avoid Security by Obscurity but rather use standard and strong methodology. However for somecompanies, Security By Obscurity is, when well used, only one of the security layers protecting thesystem.In my opinion Security by Obscurity should only be used:- If your system already has really solid security ''by standards''- If you are fully aware of the risks induced by your non-standards security method- If both IT and Security team are aware of the implemented mechanism (or else you will ''obscure'' yourselfs..)For me, experimenting on this topic is mainly about having some fun with my favorite Operating System!Here are some interesting links if you want to go further:http://www.sans.org/reading-room/whitepapers/tools/about-face-defending-organization-penetrationtesting-teams-33553http://labrea.sourceforge.net/Intro-History.htmlhttps://nmap.org/book/man-os-detection.htmlhttps://www.fishnetsecurity.com/6labs/blog/port-scanning-through-tarpitshttp://www.netfilter.org/projects/patch-o-matic/pom-external.htmlhttp://www.sevagas.com/?Iptables-firewall-versus-nmap-and6. ANNEX: Demo Bash Script-------------------- Begin script ---------------------------#!/bin/bash# Emeric NASI - www.sevagas.com# Some fun tweeking using linux kernel and firewall to trick security scanning tools# Run as root## Note: This is not a defensive firewall script, do not use as is to protect your network ports!## Rules to pretend ports are openedfalse_opened_ports(){iptables -N fw-aggressive # This chain is used to process IP where we apply aggressive defencemeasuresiptables -A INPUT -m comment --comment "Pretend ports are opened" --jump fw-aggressiveiptables -A fw-aggressive -p tcp --destination-port 21 -j TARPITiptables -A fw-aggressive -p tcp --destination-port 137:139 -j TARPITiptables -A fw-aggressive -p tcp --destination-port 445 -j TARPIT # If you have DROP default output policy remember to:#iptables -A OUTPUT -p tcp --tcp-flags SYN,ACK SYN,ACK --source-port 21 -j ACCEPT#iptables -A OUTPUT -p tcp --tcp-flags SYN,ACK SYN,ACK --source-port 137:139 -j ACCEPT#iptables -A OUTPUT -p tcp --tcp-flags SYN,ACK SYN,ACK --source-port 445 -j ACCEPT}# Port scanning detectiondetect_port_scan (){ # Drop and Log various port scannings types (Fin, Null, Xmas tree, etc.)" iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -m limit --limit 6/h --limit-burst 1 -j LOG --logprefix"Firewall>XMAS scan " iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix "Firewall>XMAS-PSH scan " iptables -A INPUT -p tcp --tcp-flags ALL ALL -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix"Firewall>XMAS-ALL scan " iptables -A INPUT -p tcp --tcp-flags ALL FIN -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix"Firewall>FIN scan " iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -m limit --limit 6/h --limit-burst 1 -j LOG --logprefix"Firewall>SYN/RST scan "iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix"Firewall>SYN/FIN scan " iptables -A INPUT -p tcp --tcp-flags ALL NONE -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix"Firewall>Null scan " # prevent Syn port scanning with psd extension if ! locate libxt_psd.so &>/dev/null && [ $INTERNET ] then echo "WARNING: Iptables extension xtables-addons-common must be installed for optimal work." fi if locate libxt_psd.so &>/dev/null || [ -f /lib/xtables/libxt_psd.so ] then # Prevent Syn port scan (will detect syn on 5 different port in less than 3 sec)" iptables -A INPUT -m psd -p tcp -m limit --limit 6/h --limit-burst 1 -j LOG --log-prefix "Firewall>Synscan detected " fi}# Rules for local trafficlocal_traffic (){ iptables -A INPUT -s 127.0.0.0/8 ! -i lo --jump DROP iptables -A INPUT -s 127.0.0.1/32 --jump ACCEPT iptables -A INPUT -i lo --jump ACCEPT iptables -A OUTPUT -o lo --jump ACCEPT}# Reset and clean all firewall rulescleanFirewall (){ # Clean all iptables rules iptables -F iptables -t nat -F iptables -t mangle -Z iptables -X iptables -t nat -X iptables -t mangle -X # Restore default policies iptables -P INPUT ACCEPT iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT}# Mask OS by changing IP stack behaviourmask_os(){ # Modify default TTL value sysctl -w net.ipv4.ip_default_ttl=128 # Linux normal value is 64}# brings nmap -sC into infinit loop (because trying to get infinit header...)# Also very harmful against other ex Nikto# Method by Nick Marsh (see http://marc.info/?l=nmap-dev&m=142811327627471&w=2)http_infinit_tarpit (){ ncat -lkv -p 8080 --sh-exec "echo 'HTTP/1.1 200 OK\r\n'; cat /dev/urandom" # Note: This is notsomething to run on producton system, it may eat up your CPU!}# Main functionmain(){ cleanFirewall mask_os local_traffic detect_port_scan false_opened_ports http_infinit_tarpit}main-------------------- End of script ---------------------------Source Quote