Chapter 21. Forensics

So, you have gone and made your computer available to the Internet. In fact, it is a lot more available than you ever expected. With all the hackers and malware floating around in cyberspace, it is a wonder nothing happened sooner. Maybe it has, but you never knew it. It has sure happened to me more than once.

Imagine a scenario in which the latest and greatest Microsoft Windows vulnerability goes public. Apparently there are services running on your computer, available to the world, which have a bug allowing anyone or anything with the know-how to take control of your computer. Not even hours after (or maybe weeks before) this news is made public, exploits circulate the Internet to demonstrate how to best take advantage of this new vulnerability. Unfortunately, you were a little late to patch your server and you have a real problem on your hands. First, your server has inexplicably rebooted several times during the course of the night. Then you received a wake-up call at 8 A.M. from your friendly ISP informing you that your monthly bandwidth has been grossly exceeded because of a sudden increase in FTP traffic. Apparently, your tiny corner of the Internet has become the latest distribution center for stolen source code. You have been hacked: pull the plug!

That scenario was just a bit extreme. A far more likely situation is that a worm, taking advantage of the same new vulnerability, has turned your poor workstation into a spam relay or mindless drone in a massive IRC botnet. Either way, something with malicious intent is running on your system. What can you do to find out more about what is happening? This is where the field of computer forensics comes into play.

There are many tools out there designed to give you more insight into what exactly is happening behind the scenes on your server. This chapter covers some freely available power tools that allow you to dig a bit deeper into your system to discover what that worm or hacker has been doing with all the time they have had free reign.

By the way, after familiarizing yourself with some of these tools, it is a good idea to save a collection of your favorite forensic tools onto a CD. If you are ever called upon to analyze an incident, it is very likely that the system you are examining is offline, as pulling the plug is the only real way to make sure that the machine is not being attacked, monitored, or used while you are checking it out. If the system or situation is particularly sensitive, it may be worth taking the extra step of imaging the tainted drive and examining it on a purpose-built forensics platform. Several of these are available as bootable, CD-based Linux distributions, designed to examine, in a forensically safe manner, images of many different operating systems ranging from Microsoft Windows to Solaris. I recommend FIRE (http://fire.dmzs.com/), FCCU (http://www.d-fence.be/), and Snarl (http://snarl.eecue.com/).

For any Internet-based intrusion into your system, some sort of communication has to happen over the network. Unless a rootkit of some sort was installed that is designed specifically to hide network connections, there are utilities that allow you to have a sneak peek at exactly what your system is doing on the network. Be particularly interested in what ports it is listening on and who it may already be talking to. Most Unix-based operating systems and Microsoft Windows versions come equipped with a tool called Netstat. If you think your system has been backdoored and you are stuck for a place to start, this is it; Netstat is probably already installed on your machine. Since usage for the more common Linux Netstat and Windows Netstat differ, I will address them separately.

On Linux, I call netstat with three different flags; none of them require any arguments:

Example 21-1 shows the command netstat -anp run as root on a Debian server (output is truncated).

The output is separated into two different sections. One, labeled Active Internet Connections, is what you are primarily concerned with. The other section, labeled Active UNIX domain sockets, contains sockets used for local communication between processes on the system. Primary indicators for a backdoor are not usually seen here.

Active Internet Connections begins with a line labeling each field, and then proceeds to list all active sockets for the system. You do not care about all of the fields, but most of them are important:

Now that the fields have been explained, look again at Example 21-1. Do you see anything out of the ordinary there? I do. Check out line number 4. What on earth is Netcat (nc) doing listening on port 9090? Using the process ID that you gain by using the -p flag, now dig a bit further.

$ ps 2085
  PID TTY      STAT   TIME COMMAND
 2085 pts/0    S      0:00 nc -l -p 9090 -e /bin/bash

This is definitely a backdoor of some sort (check out the next section if this does not look familiar to you.) If the ps command reveals nothing significant about the process and you are still unsure of what it does, you can always try to interact with the socket directly. For example:

$ nc localhost 9090
id
uid=0(root) gid=0(root) groups=0(root)
uname -a
Linux american 2.6.12-10-386 #1 Fri Jul 7 01:12:29 UTC 2006 i686 GNU/Linux
^C
$

No good can come of this. Time to pull the plug!

As with Netstat for Linux, I usually call Netstat for Windows with three flags. These might look familiar:

To get a feel for the format of the Windows Netstat output, first run it without the -b flag:

C:\>netstat -an

Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       1196
  TCP    0.0.0.0:5900           0.0.0.0:0              LISTENING       1692
  TCP    127.0.0.1:1025         0.0.0.0:0              LISTENING       164
  TCP    192.168.0.100:3278     207.17.137.201:443     CLOSE_WAIT      2996
  UDP    0.0.0.0:9090           *:*                                    784
  UDP    127.0.0.1:123          *:*                                    1232
  UDP    192.168.0.100:138      *:*                                    4

The output of this command is very similar to Linux netcat. For an explanation of each field, see Chapter 11. The key difference here is that there is no longer an Active Unix domain sockets section. Now dig a little bit further and see what these sockets are about:

C:\>netstat -anb

Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       1196
  c:\windows\system32\WS2_32.dll
  C:\WINDOWS\system32\RPCRT4.dll
  c:\windows\system32\rpcss.dll
  C:\WINDOWS\system32\svchost.exe
  C:\WINDOWS\system32\ADVAPI32.dll
  [svchost.exe]

  TCP    0.0.0.0:5900           0.0.0.0:0              LISTENING       1692
  [winvnc.exe]
  TCP    127.0.0.1:1025         0.0.0.0:0              LISTENING       164
  [alg.exe]

  UDP    0.0.0.0:9090           *:*                                    784
  [BOSERVER.EXE]

  UDP    127.0.0.1:123          *:*                                    1232
  c:\windows\system32\WS2_32.dll
  c:\windows\system32\w32time.dll
  ntdll.dll
  C:\WINDOWS\system32\kernel32.dll
  [svchost.exe]

... and so on.

With the -b flag, netstat gets a good bit noisier. The format is actually the same as without the flag, but in square brackets [] underneath each socket is a list containing the name of the process that created the socket, and above that, the libraries involved. With Windows, there tends to be a lot more ports open to the world in a default installation. It certainly helps to familiarize yourself with these before scrutinizing each and every service. There is one here that should look just the least bit suspicious. My naming convention from Chapter 11 does not really help hide the fact that there is a BO2k Server running on UDP port 9090. If you did not already know what this was, it would be time to break out the Sysinternals Process Explorer to learn a bit more about what this is doing (see Sysinternals).