4.Lock AbstractDigital

Recently, the media has been covering many Internet break-ins, their reporters spinning tales of clever bandits who magically gain access to machines with a simple keystroke. In real life, however, it is rarely that simple. Cracking into a computer system requires planning. A cracker has to find a target machine, and then find out what ports the machine is listening on before a system can be compromised.

This is usually done through scanning, the art of finding machines on a network and testing them to see what ports are listening. Scanning networks and hosts is a cracker's first strategy before launching an attack. By using scanners such as Nmap, the "bad guys" are able to sweep networks and look for vulnerable targets. Once these targets are identified, an intruder is able to scan for listening ports. Nmap will also use TCP stack fingerprinting to determine the type of machine being scanned accurately.

This article will cover using Nmap to allow a security administrator to probe his/her site to get a "cracker's view" of his/her network. By employing the same tools used by an intruder, an administrator will see what his/her site looks like to the bad guys and hopefully will be able to initiate steps to help secure his/her systems.

CybersecNmap is covered under the GNU General Public License (GPL) and can be downloaded free of charge from https://nmap.org. It comes as a tarred source as well as in RPM format. As of this writing, the current stable version is v.2.12. There are a few graphical front ends; however, this article will focus on using Nmap from the command line.

The usage syntax of Nmap is fairly simple. Options to `nmap` on the command line are different types of scans specified with the -s flag. A ping scan, for example, is "-sP." Options are then specified, followed by the hosts or networks to be targeted. Nmap's functionality is greatly increased when run as root because non-super-users don't have the ability to create the custom packets that Nmap utilizes.

Nmap is very flexible in specifying targets. Simply scan one host or entire network by pointing Nmap to the network address with a "/mask" appended to it. Targeting "victim/24" will target the Class C network, whereas "victim/16" will target the Class B, and so on.

In addition, Nmap will allow you to specify networks with wild cards, as in 192.168.7.*, which is the same as 192.168.7.0/24, or 192.168.7.1,4,8-12 to scan the selected hosts on that subnet.

Ping Sweeping

Intruders are able to sweep entire networks looking for targets with Nmap. This is usually done with a ping scan using the "-sP" flag. By default, Nmap will send an ICMP echo and a TCP ACK to each host it scans. Nmap will consider hosts that respond to either to be up. In this example, scan all hosts on the 192.168.7.0 network:

# nmap -sP 192.168.7.0/24 

Starting nmap V. 2.12 by Fyodor (This email address is being protected from spambots. You need JavaScript enabled to view it., www.insecure.org/nmap/)
Host  (192.168.7.11) appears to be up.
Host  (192.168.7.12) appears to be up.
Host  (192.168.7.76) appears to be up.
Nmap run completed -- 256 IP addresses (3 hosts up) scanned in 1 second

Sometimes, you may merely want to check the availability of a system without sending ICMP echo requests, which some sites may block. In this case, a TCP "ping" sweep can be used to scan a target's network.

A TCP "ping" will send an ACK to each machine on a target network. Machines that are up should respond with a TCP RST. To use the TCP "ping" option with a ping scan, include the "-PT" flag to target a specific port on the network you're probing. In our example, we'll use port 80 (HTTP), which is the default, and it will probably be allowed through the target's border routers and possibly even its firewall. Note that the targeted port does not need to be open on the hosts being probed to determine if the machine is up or not. Launch this type of scan as follows:


# nmap -sP -PT80 192.168.7.0/24
TCP probe port is 80

Starting nmap V. 2.12 by Fyodor (This email address is being protected from spambots. You need JavaScript enabled to view it., www.insecure.org/nmap/)
Host  (192.168.7.11) appears to be up.
Host  (192.168.7.12) appears to be up.
Host  (192.168.7.76) appears to be up.
Nmap run completed -- 256 IP addresses (3 hosts up) scanned in 1 second

When a potential intruder knows which machines on the target's network are alive, the next step is port scanning.

Nmap provides different types of port scans: TCP connect, TCP SYN, Stealth FIN, Xmas Tree, and Null, as well as UDP scans.

Port Scanning

An attacker using TCP connect scans to probe is easily detected. Nmap will use the connect() system call to open connections to interesting ports on the target host and complete the 3-way TCP handshake. Logs on the host machine will show these ports being opened by the attacker. A TCP connect scan is used with the "-sT" flag as:


# nmap -sT 192.168.7.12

Starting nmap V. 2.12 by Fyodor (This email address is being protected from spambots. You need JavaScript enabled to view it., www.insecure.org/nmap/)
Interesting ports on (192.168.7.12):
Port    State       Protocol  Service
7       open        tcp        echo            
9       open        tcp        discard         
13      open        tcp        daytime         
19      open        tcp        chargen         
21      open        tcp        ftp             
...

Nmap run completed -- 1 IP address (1 host up) scanned in 3 seconds

Stealth Scanning

What if an attacker wants to scan a host without leaving his calling card in the system logs on the target machine? TCP SYN scans are less prone to logging on the target's machine because a full handshake never completes. A SYN scan starts by sending a SYN packet, the first packet in TCP negotiation. Any open ports will respond with a SYN|ACK, as they should. However, the attacker sends a RST instead of an ACK, which terminates the connection. The advantage is that the 3-way handshake never completes, and fewer sites will log this probe type. Closed ports will respond to the initial SYN with an RST, allowing Nmap to determine that the host isn't listening on that port. The "-sS" flag will launch a SYN scan against a host or network as:


# nmap -sS 192.168.7.7

Starting nmap V. 2.12 by Fyodor (This email address is being protected from spambots. You need JavaScript enabled to view it., www.insecure.org/nmap/)
Interesting ports on saturnlink.nac.net (192.168.7.7):
Port    State       Protocol  Service
21      open        tcp        ftp             
25      open        tcp        smtp            
53      open        tcp        domain          
80      open        tcp        http            
...

Nmap run completed -- 1 IP address (1 host up) scanned in 1 second

Although SYN scans are more likely to be unnoticed, they can still be detected by some intrusion detection countermeasures. The Stealth FIN, Xmas Tree, and Null scans are used to evade packet filters and firewalls that may be watching for SYN packets directed toward restricted ports. These three scans should return an RST for closed ports, whereas open ports should drop the packet. A FIN "-sF" scan will send a FIN packet to each port, whereas the Xmas Tree scan "-sX" turns on the FIN, URG, and PUSH flags, and a Null Scan "-sN" turns off all flags. Because of Microsoft's compliance with TCP standards, the FIN, Xmas Tree, and Null scans are only effective on non-Microsoft operating systems.

UDP Scanning

If an attacker is looking for popular UDP holes to exploit, such as a rpcbind hole or cDc Back Orifice. He/she will want to know what UDP ports are listening, and to find these ports will most likely initiate a UDP scan. Using the UDP scan "-sU," an attacker can determine what ports are open to UDP on a host. Nmap will send a 0-byte UDP packet to each port. If the host returns a "port unreachable" message, that port is considered closed. This method can be time-consuming because most UNIX hosts limit the rate of ICMP errors. Fortunately, Nmap detects this rate and slows itself down so as not to overflow the target with messages that would have been ignored. Launch a UDP scan as follows:


# nmap -sU 192.168.7.7

WARNING:  -sU is now UDP scan -- for TCP FIN scan use -sF
Starting nmap V. 2.12 by Fyodor (This email address is being protected from spambots. You need JavaScript enabled to view it., www.insecure.org/nmap/)
Interesting ports on saturnlink.nac.net (192.168.7.7):
Port    State       Protocol  Service
53      open        udp        domain          
111     open        udp        sunrpc          
123     open        udp        ntp             
137     open        udp        netbios-ns      
138     open        udp        netbios-dgm     
177     open        udp        xdmcp           
1024    open        udp        unknown         

Nmap run completed -- 1 IP address (1 host up) scanned in 2 seconds

OS Fingerprinting 

Often, an intruder may be more familiar with exploits for a particular operating system and may be looking for machines he can compromise easily. A common option is TCP/IP fingerprinting with the "-O" option to determine the remote operating system. This must be combined with a port scan, not a ping scan. Nmap accomplishes this by sending different probes to the host, which will narrow the target operating system. Fingerprinting the TCP stack includes such techniques as FIN probing to see what kind of response the target has, BOGUS flag probing to see the remote host's reaction to undefined flags sent with a SYN packet, TCP Initial Sequence Number (ISN) sampling to find patterns of ISN numbers, as well as other methods of determining the remote operating system. 

Nmap's Operating System detection feature is a very accurate and effective tool, as demonstrated by fingerprinting the stack of this Solaris 2.7 machine with a SYN scan:


# nmap -sS -O 192.168.7.12

Starting nmap V. 2.12 by Fyodor (This email address is being protected from spambots. You need JavaScript enabled to view it., www.insecure.org/nmap/)
Interesting ports on comet (192.168.7.12):
Port    State       Protocol  Service
7       open        tcp        echo            
9       open        tcp        discard         
13      open        tcp        daytime         
19      open        tcp        chargen         
21      open        tcp        ftp             
...
TCP Sequence Prediction: Class=random positive increments
                         Difficulty=17818 (Worthy challenge)
Remote operating system guess: Solaris 2.6 - 2.7

Nmap run completed -- 1 IP address (1 host up) scanned in 5 seconds

Do you notice the TCP Sequence Prediction? When given the -O option, Nmap also tells us how difficult TCP sequence number prediction is for the remote host. This is valuable to an attacker looking for hosts that can be vulnerable to session hijacking.

Ident Scanning

An attacker often may look for a machine in which he/she has a specific exploit for a process, such as a web server running as root. If the target is running identd, an attacker using Nmap can find out what user owns the HTTP daemon by including the "-I" option to a TCP connect scan. We'll demonstrate by scanning a Linux web server:


# nmap -sT -p 80 -I -O www.yourserver.com

Starting nmap V. 2.12 by Fyodor (This email address is being protected from spambots. You need JavaScript enabled to view it., www.insecure.org/nmap/)
Interesting ports on www.yourserver.com (xxx.xxx.xxx.xxx):
Port    State       Protocol  Service         Owner
80      open        tcp        http            root

TCP Sequence Prediction: Class=random positive increments
                         Difficulty=1140492 (Good luck!)
Remote operating system guess: Linux 2.1.122 - 2.1.132; 2.2.0-pre1 - 2.2.2

Nmap run completed -- 1 IP address (1 host up) scanned in 1 second

If your webserver is misconfigured and running as root, as this one is, it will probably be a late night at the data center.

While it is bad security practice to run Apache as root, you can block incoming ident requests by commenting out "auth" in /etc/inetd.conf and restarting inetd. Another method of stopping ident requests is implementing firewall rules at your network border with ipchains or your favorite firewall. This will prevent the curious hooligan from probing your site to determine what user owns what daemons.

What Are My Other Options as an Nmap User?

Cyber 4508911  340In addition to these scans, Nmap offers a myriad of options. One of which, "-PT," we've already covered. This option uses a ping scan to direct a TCP "ping" sweep at commonly unfiltered ports on a target machine or network.

An option that is useful with scans is "-P0". Since Nmap will ping a target with TCP "ping" and ICMP echo before attempting a port scan, sites blocking ICMP and TCP probes will not be scanned by default. The "-P0" option to a scan will allow

One option you should get into the habit of using is "-v," a verbose option that can be used with all types of scans. You can use this flag once or twice to get more information about the target's machine.

The ability to target specific ports is accomplished with the "-p " option. For instance, if an attacker wanted to probe your webserver for FTP (port 21), telnet (port 23), name service (port 53), and HTTP (port 80) and wanted to know the OS you were using, he/she may try the SYN scan:


# nmap -sS -p 21,23,53,80 -O -v www.yourserver.com

Factor in a database of popular exploits, and even a novice cracker could be well on his way to getting root access to your machine.

Final Thoughts on Scanning and Defending Networks with Nmap

What measures exist to counter an attacker using Nmap? Numerous tools exist, such as Scanlogd, Courtney, and Shadow; however, using these tools is not a substitute for a knowledgeable administrator on the front line. Because scans are often a precursor to an attack, sites should prioritize monitoring them.

Proactively using Nmap to monitor their sites, system and network administrators can discover what a potential intruder sees as he/she probes your machines. Therefore, set up a Linux box with Nmap outside your border routers, open a tin of Penguin mints, and start scanning! Nmap to scan a host without initially pinging it.