Finding malware on your Windows box (using the command line)

Security professionals typically overlook the Windows command line, instead spending their time with more complex GUI-based forensics tools. In this tip, Ed Skoudis explains how just a few command-line tricks can help users closely examine the configuration of a Windows machine and discover whether a box is infected by malware.

I hope that headline doesn't scare you. A lot of people shun the Windows command line in favor of more complicated...

graphical user interfaces. But when it comes to investigating malware infections, various command-line tools can be incredibly helpful, since GUI-based tools can't always do the job.

First off, invoke a command prompt. Go to Start --> Run and type in 'cmd.exe.' Putting '.exe' at the end of the entry is important; it is a far safer way of bringing up a command prompt. To trick users, a malicious program might purposely be called 'cmd.com,' and in such a case, the malware would run if just 'cmd' were typed. Thus, go ahead and type 'cmd.exe' instead.

Next, with a command prompt going, run the netstat command and take a gander at the listening ports on your systems. A lot of people know that 'netstat –na' can provide a list of TCP and UDP ports on the machine. Adding an 'o' to the command arguments can reveal the process ID of each process using a port. And, starting with XP SP2, an added 'b' flag will show the EXE name using each port, along with the dynamic link libraries (DLLs) that it has loaded to communicate with the network. Beware of the 'b,' however. The function can chew up some serious CPU time, tying up between 60% and 100% of your processor for up to a minute.

But, wait, there is more. Suppose you want to look at port usage and see how it changes over time. Adding a space and then an integer to that netstat command, as in "netstat –nao 1", will run the command with a frequency equivalent to the integer, in this case, for every one second. The display will be dumped on the screen continuously, as shown below.

Of course, to sift for malware that uses TCP and UDP ports, you need an idea of what a system's normal port usage should be. To research the ports that are in use on a box, try searching for specific ports in Google. Also, Microsoft has a list of common ports that are used by both Windows clients and servers . You can also research ports associated with both Microsoft and third-party applications, as well as official port assignment lists.

If an unusual TCP or UDP port is found to be in use, you might want to do a Google search. Use a "site:" directive that is associated with an antivirus company like Symantec, Sophos, or McAfee. The sites may have a write-up about malware that uses the given port. Here's an example of a helpful search query:

site:symantec.com tcp port 4444.

A write-up from the antivirus vendor may give further insight into any registry keys that the malware may have altered. To query these registry keys at the command line, use the reg command. Even if the antivirus Web site doesn't provide any registry keys to look for, you may want to research the most common registry keys altered by malware: keys associated with system startup and user logon. Commonly known as the "run" registry keys, the reg command can help display their values at the command line. Please note that a lot of legitimate software activates itself using these keys. After running the command, you will discover items under these registry keys. Again, a few Google searches for what is displayed will help separate legitimate settings from malware.

C:\> reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Run
C:\> reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Runonce
C:\> reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Runonce 

I'd recommend running the commands an additional time, replacing HKLM with HKCU. The substitution will find user-centric auto-start registry keys, instead of just the system-wide settings under HKLM.

It's also smart to check out the autostart folder for any unexpected programs starting from there. Run the familiar old dir command, using /A to display files with or without any attributes set, as well as hidden and unhidden files.

C:\> dir /A "C:\Documents and Settings\All Users\Start Menu\Programs\Startup" 

'net users' and 'localgroup administrators'
Some malware adds an account to the local machine. Therefore, it's important to run the command 'net users,' which checks for accounts defined on the system. Also, because some bots add an account to the local administrators group, make sure to run "localgroup administrators," which should check out this particular group's membership. Do you know all of the people in your administrators group? The figure below shows some sample output.

'tasklist /svc'
There is one more vital area to analyze: running processes. On Windows XP Professional, the tasklist command can provide a wealth of information. By itself, it shows running processes, their process ID numbers and memory usage. But running 'tasklist /svc' reveals all of the services running out of each process, as shown below. This provides more to search for when researching whether the investigated system may be infected with evil programs. In particular, the svchost.exe processes are usually quite busy, running many services. Spyware sometimes inserts additional services into these processes or others to monitor or control a machine surreptitiously.

In the end, this handful of commands can provide deeper insight into the configuration of a Windows machine. But practice makes perfect. Spend time analyzing clean systems, so that you grow familiar with what is on a "normal" Windows machine. Then, you can become more sensitive to the weird stuff that malware may inject. With some preparation and practice, command-line skills will significantly improve your understanding of Windows machines and prepare you to battle boldly in the fight against malware.

More on this topic

  • In our Intrusion Defense School, learn other ways to catch Windows malware.
  • How good is Windows Vista virus protection? Peter H. Gregory explains.
  • Learn how certain antimalware tools use the command line to update on demand.

About the author:
Ed Skoudis is a SANS instructor and a founder and senior security consultant with Intelguardians, a Washington, DC-based information security consulting firm. His expertise includes hacker attacks and defenses, the information security industry and computer privacy issues. In addition to Counter Hack Reloaded, Ed is also the author of Malware: Fighting Malicious Code. He was also awarded 2004, 2005 and 2006 Microsoft MVP awards for Windows Server Security, and is an alumnus of the Honeynet Project. As an expert on SearchSecurity.com, Ed answers your questions related to information security threats.

This was last published in August 2007

Dig Deeper on Malware, virus, Trojan and spyware protection and removal