|
Level: Intermediate Martin Brown (mc@mcslp.com), Professional writer, Freelance 05 May 2009 You can find out a lot about your network by using a variety of different tools. I you want to understand the layout of your network, where packets are going, and what people are doing, then you need to use a variety of different tools that can help you to build up a picture of your network and what is going on. This tutorial examines techniques for monitoring the traffic and content of your UNIX® network and how to read and diagnose problems on your network. Before you start This tutorial is for UNIX systems administrators who are looking for ways to discover and determine information about their network structure and configuration, including what services and systems are running on different machines. To get the best out of this tutorial, you should have a basic knowledge of the UNIX operating system, and a basic understanding of how networks and the Internet protocol (IP) operate. When accessing a new UNIX system, or even understanding an existing one, a key part of the puzzle to how the system operates is the network configuration. There are many aspects of the network that you need to know and understand to correctly identify problems and prevent future problems. By using some basic tools and commands you can determine a lot about the configuration of a single system, and through this basic understanding, a good idea of the configuration of the rest of the network. With some additional tools, you can expand that knowledge to cover more systems and services within your network. In this tutorial you will use some basic tools within the UNIX environment that can disclose information about the configuration of your system. By understanding these tools and the information they output, you will be able to gain a greater understanding of your system network configuration and how it works. You will also examine tools and solutions that can look at the wider network and gain more detailed information about your network, its potential security issues, and key points of information that will help you identify and diagnose problems when they do occur. | ||||||||||||||||||||||||||||||
Understanding networks on the host
The first step to understanding your network better is to understand the network configuration of the machine you are currently using. This will give you a number of frames of reference, such as the IP address of the current host, the DNS configuration, and what other machines you can connect to and communicate with.
Finding configuration information
Determining the current configuration of the machine you are working on gives you the base information about your environment. Your first task is to determine the IP address and network mask for the current machine. By using these two values, you can determine the address of your machine and what other machines you can connect to directly on your network (for instance, without the use of a router).
Before you determine the IP address, get the hostname for the system by using the hostname command (see Listing 1).
Listing 1. Getting the hostname
$ hostname |
The ifconfig command will display the current configuration information for all your configured network devices when you use the -a option. For example, Listing 2 shows the output from the ifconfig command on a Solaris machine.
Listing 2. Output from ipconfig on Solaris
$ ifconfig -a |
You can see from this output that there is a loopback device, lo0, with the normal address of 127.0.0.1 for localhost. You can also see that the same device also has an equivalent IPv6 address.
The pcn0 device is configured with a network address of 192.168.1.25, and with a netmask of fffffc00, equivalent to 255.255.252.0. You can also see that in this case the address was set using DHCP (from the list of DHCP flags).
The netmask is particularly important, because with the netmask alone you can tell the size (in terms of registered IP addresses) of your immediate network. In this case, 255.255.252.0 equates to four class C addresses, because 256 (the maximum number of hosts) minus 252 (the number of masked hosts) equals four.
By combining the netmask with the configured IP address, you can guess the range of the IP addresses in the local network. Because IP blocks are usually split by whole groups and in sequence, you can tell that the IP address span of the network is 192.168.0.0 through 192.168.3.255. You can determine this because with a netmask of four class C addresses you would normally split the entire range (192.168.0.0-192.168.255.255) into equal blocks -- with the address prefix of 192.168.1.x it must be in the first block of four addresses.
Different operating systems output the information (and the detail) in different ways. Listing 3 shows the output from a Linux® system.
Listing 3. Output on a Linux system
eth0 Link encap:Ethernet HWaddr 00:1d:60:1b:9a:2d |
Listing 4 shows the output from a Mac OS X™ system.
Listing 4. Output from a Mac OS X system
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384 |
In all cases, you can generally find the Internet address and netmask of the connected network devices. Obviously, if you have multiple network devices then you will get the information for each device in the output, and it may be that you can reach a wide range of different networks and systems from just one machine.
|
Finding name resolution services
Your next step in determining the configuration of the current machine should relate to the configuration of the name service system that will convert name and domain names on your system into an IP address when you access a service on another machine.
The configuration of this on most machines is through the /etc/nsswitch.conf file, which contains a list of different naming services (hosts, users, and more) and the order in which the different services (DNS, NIS, or local files) should be used for resolution. You can see an example of this in Listing 5.
Listing 5. Resolving the name service system
passwd: files |
In Listing 5, for example, the hostname information is resolved first by looking at the local files on the system (for example, /etc/hosts) and then the domain name system (DNS).
If the DNS has been configured, then the /etc/resolv.conf file will tell you which machines are being used to convert names into IP addresses. A sample of the file is shown here in Listing 6.
Listing 6. Which machines are being used to convert names into IP addresses
domain example.pri |
This information can be useful if you want to query these machines directly for information. You can use tools such as dig and nslookup to extract information about the name service and resolution of names and IP addresses.
|
Hosts outside of your network (that is, beyond the scope of your network mask in comparison to your current IP address) are sent to a router to be forwarded on to another machine. Routers can be used at all levels of your network, including between departments, different physical sites, and to public and external sites such as the Internet.
The netstat command can tell you which machines or routers are contacted when your machine wants to communicate with machines outside the 'local' network. For example, Listing 7, below, is from a Solaris machine.
Listing 7. netstat command
$ netstat -r |
The default route shows the gateway (router) used to route packets that are either outside of the current network, or that are not already covered by another route for a specific IP address or IP address range.
Because you might need to determine this information in a situation where your current nameservice is not working, or not returning the right information, you can also specify the -n option to show the information using IP addresses instead of names.
|
The netstat command can also be used to determine what services are being shared and exposed on the current host. This includes all network services, including DNS, NFS, Web services, and other information. The information displayed is based upon the ports that are open and in the 'listening' state waiting for client connections, or ports that are already open and communicating with a client.
This information can prove invaluable, both to determine if a service is running, and as part of a standard security check to determine whether a machine is sharing or exposing itself to more risk than is necessary.
You can see an example of the output in Listing 8, here using -a to display all the open ports and services, both established (open) and listening for new connections. By default, netstat also shows the open UNIX domain sockets, which are only accessible to the current machine. For brevity these have been removed from the output.
Listing 8. Output using
-a$ netstat -a |
As you can see from this output, the machine is quite busy. The third column shows the hostname and port, separated by a colon, for each open connection or listening connection. If the TCP or UDP service number matches a known port number (as defined within the /etc/services file), then the service name is displayed in the output. For the host, either the hosts name, an alternative IP address, or the '*' symbol is displayed. The asterisk indicates that the service and ports are open and listening on all IP addresses.
For example, you can tell from this output that the machine is configured to support NFS, and has open (established) connections, as shown in Listing 9.
Listing 9. Machine is configured to support NFS
$ netstat -a|grep nfs |
It is also possible using this output to see which machines are currently communicating with this machine. For example, you can extract a list of the machines connected to this one by looking at the fifth column, and then sorting and removing duplicates from the list (see Lisiting 10).
Listing 10. Extracting a list of connected machines
$ netstat -a|egrep 'tcp|udp'|grep ESTABLISHED|awk '{ print $5; }'|cut -d: -f1|sort|uniqFinding information about other hosts Once you have the basic information about your machine, you can start to spread out and look at other machines in your network to determine the available and services that they provide. With the right tools, you can even try to determine what operating system these machines are running and what services the machines might be sharing. The easiest and most obvious tool for checking remote machines is to use the ping tool to check whether a particular host is up and available. The ping tool does something very simple. It sends a packet to the remote host requesting a response. When the response has been received, the ping tool calculates the time difference, and the time taken to send and receive the packet can be used as an indication of how near or far a machine is from its current location. For example, if you ping a machine on your own network, you are likely to get a response to the ping packet very quickly (see Listing 11). Listing 11. Pinging machine on your own network
Different implementations of the ping tool work in different ways. By default on Linux and Mac OS X, the tool will continually send packets and wait for a response until you force the application to terminate with Control-C. On Solaris™, AIX®, and some other UNIX variants, without any additional arguments the ping tools will merely indicate if the remote host responded (see Listing 12). Listing 12. Pinging without additional arguments on UNIX variants
To perform the longer test, use the Listing 13. Using the -s option for pinging
The time field for each line gives you an indication of the speed and latency (the delay before response, and often an indication of the level of activity) for each packet. When you stop the output, you get a summary of the number of packets sent, received, and the time statistics. The further the distance that the ping packets have to travel, the longer the response time from the remote host. For example, if you try to ping a public server on the Internet, the time taken for the response packet can be significantly higher (see Listing 14). Listing 14. Pinging a public server on the Internet
Compare the times for this connection to an Internet service (193ms) with the time for a local host (0.23ms). The ping tool can also be a quick way of determining whether you can even reach the remote host that you want to connect to. Running ping on a host that does not exist returns a very specific error (see Listing 15). Listing 15. Pinging a host that does not exist
The ping tools relies on knowing what other machines are available on your network. Let's see how you can determine what hosts might be on the network without knowing their names or IP addresses
Discovering hosts on your network Within the Ethernet network system (and others), all devices on your network have a unique address associated with the hardware network device. The Media Access Control (MAC) number uniquely identifies the network device and, through the use of higher-level protocols such as the Internet Protocol you can associate the MAC address with the host name. This is used (in reverse) by the operating system when sending packets out on the network. When you send packets to a specific hostname, the operating system attempts to resolve the hostname into a MAC address so it can construct the hardware (Ethernet) packet to be sent out on the network. The Address Resolution Protocol (ARP) handles this mapping, and you can use the arp tool to display the currently held information about the hosts and their host names or IP addresses. Because any machine on the network that wants to communicate with another must have sent out a packet with the MAC address and the IP address, the information gleaned by your system in the ARP cache can be a useful way to find out what other machines are on the network (see Listing 16). Listing 16. Using the arp command
With modern Ethernet switches, in place of the older hub structure, the information output by arp may be limited to the packets sent and received to or from a particular host. If you can run arp on a server you will get a longer list of information, but this isn't always possible or practical. On some network switches you have a network management or monitoring port where all packets are echoed, and which you can use to gain information about the other network devices and therefore the network structure. If you don't have access to this information, you may need a more brute force approach to finding hosts on your network.
Finding other hosts on your network The nmap tool is a utility that can perform a variety of different scans across your network to find and determine different levels of information. At a basic level, it can be used to find all of the hosts within a given network. Earlier the article examined how to get the current IP address and netmask information for a host. You can use this information to set the basic search parameters for nmap to try and find all of the hosts on the network. To specify this information, you have to use the CIDR style addresses. The CIDR format uses the IP address of the host, and the number of bits in the network mask, to determine the span of the network. From the example host, 192.168.1.25 was the IP address, and the network mask was 255.255.252.0. This is equivalent to 22 bits -- 8 bits for the first part, 8 bits for the second, and 6 bits for the third part. Running nmap with this address will scan every single IP address within the range (for instance, every address between 192.168.0.0 and 192.168.3.255) and determine which hosts reply. You can perform a number of different tests, including a test using the standard ping protocol, or a more extensive test that tries other network ports in case the ping protocol has been disabled. For example, the ping test displays the list of hosts in Listing 17. Listing 17. Running nmap to scan range of IP addresses
The ping check can give you a very quick idea of what other machines are on the network. In this case, 11 hosts have been discovered, but not all of them can be resolved back to a name. This is a fault in the DNS configuration that should be fixed, as some systems use the reverse lookup (IP address to name) as a security check to ensure the client IP address has not been faked.
Finding other services on your network The ping check is useful, but if you want to know the services an individual machine is actually exposing itself to, use the TCP check. A TCP check takes longer, as nmap will try to open ports using the TCP/IP protocol from each host within the list. This can be more effective at displaying what hosts are on your network, and at providing detailing information about the open ports for each host. You can see this in Listing 18. Listing 18. Using a TCP check
From this output, you can see that there are a number of servers on the network providing a variety of services. The device at 192.168.0.1, for example, provides HTTP and HTTP-proxy services. Sp does bear.mcslp.pri, in addition to smtp imap, nfs, and MySQL services. To determine more specific information about these services, you can use nmap again with the version argument to get a more specific list of the version information about the protocols and ports that are open on a specific host. For example, checking what appears to be the main server (bear), you can get a very good idea of exactly what is running behind each of these ports (see Listing 19). Listing 19. Using nmap with the version argument
In this case, you can see a number of specific services, this time showing the version and even application information that is provided in each case.
Determining unidentified hosts on your network When you have found a host on your network, especially one that you may not immediately recognize, you may want to know more about the host. The TCP port scan shows you what services are being supported by the host, but this may not necessarily tell you the whole story. Some devices and systems may or may not expose ports in a manner that doesn't make it immediately obvious what is on your network. The nmap operating system scan examines the open ports and tries to work out what the system is behind the different services. This can make the difference between identifying a server with open ports and a new device on your network. For example, if you run the operating system identification on the server bear, you can identify the system as running a traditional version of Linux, which probably indicates a standard computer, as shown in Listing 20. Listing 20. nmap operating system scan
The OS scan is not perfect, and it relies on finger printing techniques to determine what the open ports and returned version information means. For example, the scan below in Listing 21 has identified a number of potential operating systems that might be behind the port types. Listing 21. Scan indentifying a number of potential operating systems
It is worth noting that the nmap scan can be used on both local and remote networks. In the remote test above, nmap determined how different systems the packets had to go through before they reached their destination. Understanding the different devices between you and other machines on your network are often the final part of understanding and determining your network layout.
Determining the network structure Whenever an IP network packet is sent out on the network, a special counter is incremented each time a system forwards the packets on to another network or system. Forwarding of packets happens within a number of different systems. If you have multiple network switches connected together, each hub can identify itself as a new device. In addition, wireless access points and traditional routers are all examples of devices that forward packets and are therefore considered part of the network route of the packet. In most network environments, hubs, switches, and other components within your local network do not increment this value, but as you stretch out wider across your network, the network gets larger and more complex, so understanding the route taken by individual packets can help you to identify performance and connectivity problems. The primary tool for displaying the route information for communicating with a host is traceroute. This determines the IP address of each host within a given path from the current host to the destination. If the host is immediately local, then the route is obviously direct (see Listing 22). Listing 22. Using traceroute
For hosts in the local network that might be accessible through a local router or bridge, see Listing 23. Listing 23. Hosts in the local network
Connections to distant networks may show each router and step the packets have taken (see Listing 24). Listing 24. Connections to distant networks
Using this method, in combination with nmap to determine the list of hosts, you can gain a better understanding about the hosts on your network and which routers and systems are used to reach these systems.
|
This can be useful when you suspect there is a user or computer connected to the machine that you do not recognize or don't expect.
To find out about these other machines, you need to start looking at the other computers within your network.