© Abhijit Mohanta, Anoop Saldanha 2020
A. Mohanta, A. SaldanhaMalware Analysis and Detection Engineeringhttps://doi.org/10.1007/978-1-4842-6193-4_14

14. Memory Forensics with Volatility

Abhijit Mohanta1  and Anoop Saldanha2
(1)
Independent Cybersecurity Consultant, Bhubaneswar, Odisha, India
(2)
Independent Cybersecurity Consultant, Mangalore, Karnataka, India
 

In previous chapters, we talked about malware dissection using static and dynamic analysis using different kinds of tools. Every tool and method has its pros and cons. You might conclude about a sample by performing a static analysis without even having to go for dynamic analysis. But there are chances where dynamic analysis may fail, and then you have to go for reverse-engineering the sample. Alternatively, you can also go for another technique called memory forensics, where you have a chance to analyze and determine if a given sample is malware or not without going for complex reverse engineering techniques. This chapter talks about how we can analyze and dissect malware using Volatility, a well-known memory forensics utility.

What Are Memory Forensics?

When malware executes, it can create certain processes, files, registry entries, install rootkits, and so forth. These malware artifacts can sometimes be destroyed by the malware so that an analyst can’t detect them and thereby figure out the presence of the malware. For example, a malicious process may terminate after code injecting into a legitimate process. Or a temporary file created by malware is deleted after it’s used. Using forensic techniques, you can retrieve this kind of information and data, although it was destroyed by the malware, thereby enabling us to identify malicious activity.

To perform forensics, you need two kinds of data: volatile and non-volatile. Non-volatile data is the data that is stored on a hard disk or permanent storage and is available even if the system is shut down. Non-volatile data can be retrieved from the hard disk. Volatile data is stored in the RAM or other transient memory. Volatile data is lost after the computer is powered off. Forensics involves the acquisition of data—both volatile and non-volatile—from a system and then working on the data offline. What we mean by offline is that you can obtain this data from a system and analyze this extracted data on any other computer without the presence of the computer whose data you extracted.

When we specifically talk about memory forensics, it involves the acquisition of the volatile data, which is the content in the physical RAM (see Chapter 4). The acquired volatile data is also called a memory dump .

But why dump the contents of the RAM? The RAM contains the data structures, the data related to processes running on the system, and the kernel. This includes virtual memory of all processes, virtual memory of the kernel, handles, mutexes, network connections, and other resources that are currently being used by all processes and the kernel. All these data and data structures are available in the memory dump we extract from the system’s RAM. Other than that, you might also be able to retrieve the following data relevant to malware analysis.
  • Presence of rootkits and API hooking

  • Terminated processes

  • Files being used by the processes at the time of data dumping/acquisition

  • Registry entries created

  • Terminated network connections

Do note that you won’t be able to extract information from all the files used by a process in the memory dump, as it is not possible to store the entire file system in memory. You can only retrieve information on files that are currently in use by a process, or to use a technical term, you can say files that a process has open handles to.

Why Another Technique?

In previous chapters, you learned about various static and dynamic analysis tools like APIMiner, ProcMon, Wireshark, String Analysis, and Process Hacker to analyze and detect malware. Why learn another technique? The following are some of the reasons why.
  • This is probably the most notable and useful need for memory forensics. As a part of an incident response team, if you are called to investigate an existing malware infection in a computer, the malware might have finished execution or carried out most of its tasks. You have now missed your analysis window to run tools like ProcMon, Wireshark, Process Hacker, and so forth. In such cases, memory forensics can be extremely helpful, and you can reconstruct the sequence of events inflicted by the malware.

  • Identifying code injection by malware with tools like APIMiner is super easy. But the same might be a lot harder if you rely on ProcMon and Process Hacker. As with APIMiner, the task is easy with a memory forensic tool like Volatility.

  • A lot of malware has anti-analysis armoring features that detect the presence of analysis tools like ProcMon, Wireshark, OllyDbg, Process Hacker, and so forth. On detecting their presence, the malware might exit or exhibit benign behavior to fool any analysis. By using a separate memory forensics VM with no analysis tools installed, you can now run malware, extract a memory dump on this VM, and analyze the dump to figure out the malware’s events and intentions, thereby circumventing any anti-analysis armoring by the malware.

  • For malware that provides a kernel-mode rootkit driver, it has been observed many times that while using a rootkit scanner, the system might crash. The reason may be due to the changes caused in the kernel by the malware, or it can be buggy kernel drivers from the scanners we use. Instead of using a scanner, taking a memory dump and using it to analyze the kernel data structures for any injected rootkits can be helpful.

  • Each tool has pros and cons. Sometimes you need a combination of tools and techniques to derive a conclusion from your analysis. It is also advisable to cross-check the output of various tools. Volatility, the memory forensic tools which we are going to use in this chapter, is a one-stop tool using which you can verify right from code injection to API hooking and rootkits.

Memory Forensics Steps

There are two main steps involved in memory forensics.
  • Memory acquisition

  • Memory analysis or forensics

The first step, memory acquisition , involves capturing the contents of the RAM using a memory capture tool, which creates a memory dump file. The second step, memory analysis or forensics, involves an analysis of this memory dump file obtained from the first step. In the next set of sections, let’s go through how to carry out both steps to analyze and identify malware.

Memory Acquisition

Forensics starts with data acquisition. Since we want to look at the memory, we need to use memory acquisition tools. Memory acquisition tools take a complete dump of the RAM, which includes both the user-mode memory space for all the processes and the kernel-mode memory as well. The task of dumping memory is simple, and there are enough free tools to help you with the process. The following are some of the tools that you can use to dump the memory.
  • FTK Imager

  • Ram Capture

  • DumpIt

  • Ram Capturer

  • Memoryze

We used FTP Imager Lite to dump our memory. The tool is extremely lightweight and easy to use. Figure 14-1 is a screenshot of the tool.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig1_HTML.jpg
Figure 14-1

FTK Imager Lite tool to create a memory dump

You can create a memory dump by clicking the Capture Memory button. As an exercise, use this tool to capture a dump of your analysis VM. Once the dump is created, verify the presence of the dump file on the disk and also its size.

The memory dump file created has a special file format that can be understood by memory forensics tools. To analyze our dumps, we used a well-known open source memory forensics tool called Volatility. But before we get to use Volatility, let’s run some malware samples and create/download some memory dumps that we can dissect.

Sample-14-1.mem

Our first malware sample is Sample-14-1 from the samples repo, where it is named Sample-14-1.txt because it is a text file that holds the instructions that you need to follow to download the real malware sample.

There are two directions that you can go here to obtain the memory dump. You can use any of the following two steps to obtain the memory dump concerning this malware sample run.
  • Run Sample-14-1.exe inside your analysis VM for a minute or so, and then using the FTK Imager tool, create a memory dump.

  • We have already done the dirty work for you. We ran the malware Sample-14-1.exe in our analysis VM, created a memory dump called Sample-14-1.mem, and uploaded it for you to download directly. The URL link to download this dump file can be obtained from the file Sample-14-1.mem.zip.txt.

You have dynamically analyzed the sample using other static and dynamic tools you learned about in the previous chapters and have noted some of the artifacts related to this malware. We have listed these artifacts so that when analyzing this memory dump with Volatility later in this chapter, you can correlate what we discover, with the following artifacts.
  • Creates a new PE executable file at C:Users<user>AppDataLocalsbjgwpgv.exe

  • Creates a new registry key called lrxsmbwu at HKCUSoftwareMicrosoftWindowsCurrentVersionRun with a value of C:Users<user>AppDataLocalsbjgwpgv.exe

  • Creates a new process svchost.exe in suspended mode and inject code into it using process hollowing. Various unpacked strings can be seen.

  • Creates a mutex named 2gvwnqjz1.

If you run the malware sample and generate a memory dump, the artifacts that you see may be different from what we listed.

Sample-14-2.mem

Similar to our previous dump Sample-14-1.mem, you can generate the dump by running the malware Sample-11-7-ssdt-rootkit and then using FTK Imager, create your own memory dump. Or you can download the memory dump that we generated and uploaded, which you can download from the URL specified in Sample-14-2.mem.zip.txt.

From the dynamic analysis we have conducted on Sample-11-7-ssdt-rootkit.exe, a notable artifact we discovered is that it installs a rootkit by inserting a kernel module rootkit.sys which then hooks the NtQueryDirectoryFile and NTCreateFile service functions in the SSDT.

Sample-14-3.mem

This dump was taken when downloading a file using Internet Explorer from www.softpedia.com/get/Internet/Browsers/Internet-Explorer-11.shtml. The sole intention of this dump is to help you to understand how to dissect and identify network connections using Volatility. The URL to download this dump is located in the file Sample-14-3.mem.zip.txt in the samples repo.

Note

All the memory dumps generated in Sample-14-1.mem, Sample-14-2.mem, and Sample-14-3.mem have been taken on a fresh baseline snapshot of our malware analysis VM. What this means is that we executed the malware sample and then dumped the memory using FTK Imager. We then reverted the analysis VM to the baseline snapshot before executing the next malware and taking the memory dump again.

Memory Analysis/Forensics

Now that we have obtained all the memory dumps, we can start analysis work on them using the Volatility tool. But before we analyze the dumps, we need some information about the system on which the memory dumps were created. The system information extracted needs to be provided to the Volatility tool when we analyze the dumps. System information includes the operating system, the version, and the processor type. To obtain this information, you can use the systeminfo command on the command prompt, as seen in Figure 14-2.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig2_HTML.jpg
Figure 14-2

systeminfo command to obtain information about the system

The operating system is Windows 7, the version is Service Pack 1, and the processor type is X86. These information bits can be combined and represented with a single notation Win7SP1x86, and we can call it ImageInfo. We use the ImageInfo in our various Volatility tool commands later, and whenever you see <ImageInfo> in the command, replace it with the notation you generated for your system, which in our case is Win7SP1x86.

For our exercises, we have created a setup that looks like Figure 14-3. You can follow the same setup as well. Create a folder called C:forensic and under this folder create these other folders vad_dump, process_dump, modules_dump, misc_dump, malfind_dump, file_dump, registry_dump, and dll_dump. We use these folders to save various analysis information extracted from the dumps using the Volatility tool. Also, copy the memory dumps Sample-14-1.mem, Sample-14-2.mem, and Sample-14-3.mem to the same folder. You don’t exactly need to structure your folders, but it’s best for the sake of clarity.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig3_HTML.jpg
Figure 14-3

Directory structure we are following for our exercises

Let’s start analyzing the memory dumps using Volatility. Volatility is a command-line tool, so to run it, open the cd command prompt to the C:forensic directory, and run the command seen in Figure 14-4. It prints the help for the tool, and as seen in the screenshot, it takes various arguments.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig4_HTML.jpg
Figure 14-4

Volatility help output shows various options provided by the tool for analysis

Volatility is a command-line tool, and if the output of the tool is too large, it is inconvenient to view the output from the command prompt. You can instead redirect the output of the command to a text file using >> operator , as seen in Figure 14-5.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig5_HTML.jpg
Figure 14-5

Redirecting the output of Volatility tool to a text file using the >> operator

The redirected output into the file help.txt from the command can now be viewed using a text editor like Notepad or Notepad++, as seen in Figure 14-6.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig6_HTML.jpg
Figure 14-6

The redirected output in Figure 14-5 can be viewed using Notepad++ editor

The screenshot displays the list of commands provided by the tool. Now Volatility commands are made available by the tool through plugins. Standalone vanilla Volatility displays only those commands which form the core Volatility plugins. For additional plugins, you need to use the plugin options. In the next few sections, we discuss some of the important Volatility plugins.

Volatility Command Format

Listing 14-1 is the common command format for the Volatility tool.
volatility-2.5.standalone.exe -f <memory_dump_file> --profile <ImageInfo> command
Listing 14-1

Standard Volatility Command Format

The -f option specifies the entire path to the memory dump file that we are analyzing. The --profile option requires ImageInfo, which we derived earlier section (i.e., Win7SP1x86). You can also double confirm the value of ImageInfo by using the Volatility imageinfo command, as you learn shortly.

Some of the commands provide the option to extract the modules’ memory and files out of the memory dump. The extracted data can be dumped to files in a directory we specify using the -D <dump_directory> or --dump=<dump_directory> option.

Also, note that some of the commands or plugins are specific to a memory dump belonging to a particular type of ImageInfo. A command that works for Windows XP memory dump may not work for Windows 7 memory dump and vice versa. So you should always refer to the manual of the commands. You can refer to the commands in the Volatility wiki located at https://github.com/volatilityfoundation/volatility/wiki/Command-Reference.

Image Information

We derived the ImageInfo value Win7SP1x86 that we need to supply to all Volatility command lines. We can confirm the ImageInfo that we derived by using the Volatility imageinfo command, which displays the possible ImageInfo values for the memory dump we are analyzing. On executing the command, you might get multiple ImageInfo options as the command guesses the various possible values from the dump. As an exercise, run the command listed in Figure 14-7 against Sample-14-1.mem memory dump.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig7_HTML.jpg
Figure 14-7

The various ImageInfos/Profiles suggested by Volatility for Sample-14-1.mem

The imageinfo command displays a lot of information about the memory dump. KDBG search is the technique used by Volatility to extract information about the dump. It also displays us the time when the memory dump was taken. But the most important information we should be concerned about is the suggested ImageInfos, which are displayed as Suggested Profiles(s). As seen, the command displays two possibilities for ImageInfo: Win7SP0x86 and Win7SP1x86. We need to select one of the two ImageInfos for the rest of the commands against Volatility for this specific dump. We go with the value of Win7SP1x86 as it matches the same value we derived earlier in this chapter with the systeminfo command in Figure 14-2.

In the next set of sections, let’s explore the commands one by one and analyze our memory dumps.

Listing Processes and Services

Let’s start by listing the processes in our memory dump, with some of the commands listed in Table 14-1. With these commands, we can list the processes on the system from which the memory dump was extracted. The processes present in the dump are the ones that were present in the system at the time instance the dump was taken.
Table 14-1

Some Volatility Commands to List the Various Processes in the Memory Dump

Command

Description

pslist

Lists processes on system from which dump was taken

pstree

Displays process list with tree view

psscan

Lists killed and hidden processes

psxview

Displays processes using various techniques

svcscan

Lists installed services

pslist

The pslist command displays the list of processes from the dump. The command works by walking the double linked list structures of EPROCESS objects using PsActiveProcessHead (refer to Chapter 11). You can run the command on Sample-14-1.mem using the command line specified in Listing 14-2.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 pslist
Listing 14-2

The Command Line for pslist Command on Sample-14-1.mem Memory Dump

The partial output from the command can be seen in Figure 14-8.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig8_HTML.jpg
Figure 14-8

The output from pslist command against Sample-14-1.mem memory dump

The output from the command shows the following process fields.
  • Offset: The address of the EPROCESS structure

  • Name: The name of the process

  • PID: The process ID (PID)

  • PPID: The PID of the process’s parent process

  • Thds: The number of threads in that process (it is 0 if the process terminated on the system at the time the dump was extracted)

  • Hnds: The number of open handles in the process

  • Sess: The session ID

  • Wow64: Holds the value 1 if the process is 64-bit, 0 if 32-bit.

  • Start: When the process was created

In the section in which we downloaded/extracted the memory dump Sample-14-1.mem file, we explained that we extracted this memory dump from the system after we ran the malware Sample-14-1.exe. As you can see in the screenshot, the Sample-14-1.exe malware sample process with PID 2492 has terminated because the number of threads is 0.

pstree

Let’s try another command, pstree , which displays the same list of processes as pslist but in a tree format, displaying the parent-child relationships. You can run the command on Sample-14-1.mem using the command line specified in Listing 14-3.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 pstree
Listing 14-3

The Command Line for pstree Command on Sample-14-1.mem Memory Dump

The partial output from the command can be seen in Figure 14-9.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig9_HTML.jpg
Figure 14-9

The output from pstree command against Sample-14-1.mem memory dump

Do you see anything suspicious here? The process svchost.exe with PID 272 is a child of our malware Sample-14-1.exe process. svchost.exe is a system process whose parent process usually is services.exe. The fact that some other process is its parent is enough to raise a red flag that Sample-14-1.exe is malware and that it has created a child process svchost.exe for the sake of some kind of stealth, probably involving code injection.

psscan

The psscan command works by scanning pool tags and can identify processes hidden by rootkits. We don’t have a hidden process with Sample-14-1.mem here, so we won’t discover any new process with this command. You can try out the command, though, the command line for which is listed in Listing 14-4.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 psscan
Listing 14-4

The Command Line for psscan Command on Sample-14-1.mem Memory Dump

psxview

The psxview command works by comparing multiple techniques of identifying processes, which includes the methods used by both pslist and psscan. You can run the command on Sample-14-1.mem using the command line specified in Listing 14-5.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 psxview
Listing 14-5

The Command Line for psxview Command on Sample-14-1.mem Memory Dump

As seen in the command output in Figure 14-10, it lists whether the various techniques were able to identify the processes listed from the memory dump. As seen pslist, psscan, thrdproc, pspcid, csrss, session, and deskthrd are the techniques to identify processes from the dump. A value of True indicates if the technique succeeded in identifying the process and False indicates it failed.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig10_HTML.jpg
Figure 14-10

The output from psxview command against Sample-14-1.mem memory dump

There is another command, svcscan, which we haven’t tried in our exercises. We strongly suggest you try it out to check the output.

Virtual Memory Inspection

The memory dumps we acquired contains the entire contents of the RAM at the time of memory acquisition. A lot about virtual memory is present in the memory dumps. You learned the importance of virtual memory in previous chapters, where we used its contents for string analysis to analyze unpacked contents of a malware process, injected code, and so forth.

Windows assigns a structure called the VAD tree for every one of its processes. A VAD tree structure is made up of multiple nodes, which are again structures. Each structure contains information about a virtual memory block used by a process (see Chapter 4). The structure contains the memory block’s permissions and size, and so forth. All these structures combined make up the VAD tree, which is what we view from a high-level using Process Hacker.

You recall the way that we inspected the memory structure of our malware processes and its contents using Process Hacker. You can do the same by examining the VAD tree with the help of Volatility. Table 14-2 lists some of the Volatility commands to inspect the VAD tree.
Table 14-2

Some Volatility Commands to List the VAD Tree and the Memory It Points To

Commands

Description

vadinfo

Information about VAD tree nodes, including memory blocks, address range and their page permissions

vadree

Displays tree view of the VAD tree

vaddump

Dumps the contents of the virtual memory pages

vadinfo

The vadinfo command displays detailed information about the memory blocks of the processes. In the Sample-14-1.mem dump, we placed our suspicions on the svchost.exe process as process hollowed by the Sample-14-1.exe malware. Let’s investigate the VAD tree of this svchost.exe process, which has a PID of 272 (refer to Figure 14-9 for the PID). You can run the command on Sample-14-1.mem using the command line specified in Listing 14-6.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 vadinfo -p 272
Listing 14-6

vadinfo Command on Sample-14-1.mem memory Dump to View Its vad Tree

Figure 14-11 shows an excerpt from the log output of the vadinfo command.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig11_HTML.jpg
Figure 14-11

The output from vadinfo command that lists the vad tree for svchost.exe process

Figure 14-11 shows that the various VAD nodes are listed, with each node corresponding to a memory block belonging to our svchost.exe process with PID 272. Each node’s information holds various fields describing the memory block it represents. The following is a description of the fields shown.
  • Start: The start address of the memory block

  • End: The end address of the memory block

  • Flags: Indicates the state of the pages in the block (e.g., if the pages are PRIVATE and in COMMITTED state)

  • Protection: Indicates the page permissions, (e.g., PAGE_EXECUTE_READWRITE)

Do you see anything suspicious in the nodes for this process? As seen in the screenshot, we have highlighted the node, which does seem suspicious. As you can see in the highlighted memory block, it is PrivateMemory and has PAGE_EXECUTE_READWRITE(RWX) permission. In Chapter 10, you learned that these pages, which are both PRIVATE and have RWX permissions, have been allocated for code injection. The memory block from this VAD node makes a good candidate for further investigation.

vaddump

The vaddump command dumps the contents of the memory pages for all the VAD nodes. Run the command in Listing 14-7, which can dump the contents of the memory blocks into the C:/forensic/vad_dump.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 vaddump -p 272 -D C:/forensic/vad_dump
Listing 14-7

The vaddump Command to Dump the Memory Contents of Process PID 272 of Sample-14-1.mem

In Figure 14-12, the memory content of the memory blocks from all the VAD tree nodes are dumped into files in the C:forensicvad_dump with the extension .dmp, which is created for each node/memory_block that is dumped.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig12_HTML.jpg
Figure 14-12

The memory contents dumped into various files using the vaddump command

You can also see that the memory block for the suspicious node we identified in Figure 14-11 has been dumped as well. As an exercise, go through the contents of this specific dump file by using the static string analysis tool BinText, as well as through all the other dump files and note down any suspicious strings that identify the malware and maybe even its type and family. Also, keep an eye out for deobfuscated strings like For base! that we obtained from our earlier dynamic analysis using Process Hacker and String Analysis.

As an exercise, you can try out other commands listed in Table 14-3, that are related to memory extraction.
Table 14-3

Other Memory Analysis–Related Volatility Commands

Command

Description

memmap

Shows mapping between physical and virtual memory

procdump

Dumps the main executable process module

memdump

Extracts all memory resident pages

Listing Process Modules

In the last section, you saw different ways to list processes. We were able to list the contents of a process’s memory using various memory dumping commands like vaddump.

We know that code and data inside the process’ memory are distributed into modules, which includes the main process module and various other DLLs (see Chapter 4). Volatility provides you various commands listed in Table 14-4, in which you not only list the various modules of a process but also dump the contents of its memory into dump files, the same way we did with vaddump in the previous section.
Table 14-4

Commands That List and Dump the Modules of Processes

Command

Description

dlllist

Lists the DLLs in a process’s memory

dlldump

Dumps the memory contents of the DLLs to the disk

ldrmodules

Displays hidden modules

dlllist

The dlllist command lists the DLLs used by the processes. Run the command in Listing 14-8 to print a list of all the DLLs across all processes.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 dlllist
Listing 14-8

dlllist Volatility Command to List All the Dll Modules Across All Processes

In previous sections, we identified that our suspicious process in Sample-14-1.mem is svchost.exe bearing PID 272. We can specifically list the DLLs from this process by using the -p option, as seen in the command line specified in Listing 14-9.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 dlllist -p 272
Listing 14-9

dlllist Volatility Command to List DLLs of a Specific Process Using the -p Option

Figure 14-13 is an excerpt from the output of the command.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig13_HTML.jpg
Figure 14-13

The DLLs listed by the dlllist command for our suspicious svchost.exe process

As seen in the screenshot, It displays various field bearing information on the various DLLs loaded by the process, the description for which are provided. As you go through the content of the various fields, try to correlate them with the same information that you extracted using a tool like Process Hacker.
  • Base: The start address of the module.

  • Size: The size of the module.

  • LoadCount: The number of times a module is loaded and unloaded using Loadlibrary() and Freelibray(). The default value is

    –1, which you can see in hex as 0xffff.

  • Path: The path to the module’s DLL file on the disk

Volatility also provides a dlldump command, in which you can dump the memory content of the DLLs into a directory, as you did with vaddump. This is more useful when malicious DLLs are loaded as plugins in browsers. You can dump the DLLs to the disk and inspect the contents of the dumped DLLs using BinText to identify malicious strings. As an exercise, try out the command listed in Listing 14-10, which dumps the memory contents of the DLLs for the malicious svchost.exe process.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 dlldump -D dll_dump -p 272
Listing 14-10

dlldump Volatility Command to Dump the Memory Contents of DLLs to the Disk

For our Sample-14-1.mem memory dump, did we find anything suspicious through the dlllist command? Not yet. But with dlldump command and BinText we possibly can find some malicious strings. Earlier, we had a hunch that svchost.exe was likely to be the target of process hollowing by our main malware process Sample-14-1.exe. Let’s see if we can find related hints to confirm this.

ldrmodules

ldrmodules attempts to display all the modules that were loaded by the process regardless of whether they are currently hidden by the malware. This is in contrast to the dlllist command we saw in the previous section which only listed currently loaded modules by a process. Run the command shown in Listing 14-11.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 ldrmodules -p 272
Listing 14-11

ldrmodules Volatility Command to List All DLLs Hidden or Otherwise

An excerpt from the output of the command can be seen in Figure 14-14.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig14_HTML.jpg
Figure 14-14

All the modules hidden or otherwise listed by ldrmodules for our suspicious svchost.exe process

The fields in the command output are similar to those in the output for the dlllist command in Figure 14-13. The InLoad, InInit, and InMem fields indicate the presence of the module in memory. The MappedPath field indicates if the module has a file on disk. Using these new fields, you can identify if some form of code injection was used or not.

Do you see in the screenshot that the InInit field is set to False and MappedPath field is empty because it could not find the svschost.exe module file path on disk. Don’t you think it is suspicious? Can you connect it to any malicious technique? The values for these two fields indicate that the svchost.exe module has been unmapped from the memory. We explained that the Sample-14-1.exe malware—against which we created this dump—creates a new process, svchost.exe, in a SUSPENDED state. Does it ring a bell?

All the hints point to process hollowing, where a malicious process launches another process in SUSPENDED state, in this case svchost.exe and unmaps the sections/modules in the svchost.exe process and maps its malicious code. You see the same hints for unmapping the modules in the ldrmodules output.

Listing Handles

There are several resources in the operating system which are represented as objects. Process, mutex, threads, files, semaphores, memory sections—everything can be treated as an object. An object you can consider as a form of metadata to represent that particular resource. A process or the kernel which needs to manipulate these resource objects first needs to do so through obtaining a handle (a kind of reference) to the object. The handle to an object is considered open if a particular process is still using it, in which case the handle stays in memory.

We can figure out if a malware process is using any of the resources we specified by enumerating the open handles in the malicious process. To view all the handles for our malicious svchost.exe process with PID 272, run the command in Listing 14-12.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 handles -p 272
Listing 14-12

The handles Command to List All the Open Handles for Our svchost.exe Process

After running the command, you obtain a long list with more than 230 rows. This displays all kinds of handles used by our svchost.exe process, the partial output of which is pasted in Figure 14-15.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig15_HTML.jpg
Figure 14-15

The open handles in our malicious svchost.exe process from Sample-14-1.mem

The command displays various fields to describe each open handle, the description for which are listed.
  • Offset: The virtual address of the handle object

  • Pid: The PID of the process that has the handle

  • Handle: The unique number/value to reference this handle

  • Access: The permissions to the handle

  • Type: The type of the object for which handle is open

Volatility is based on the type of object that the handle references. It prints the Type value, as seen. The following are various possible types of objects/handles used by Volatility commands.
  • Mutant

  • Thread

  • Key

  • Section

  • Directory

  • File

  • Process

  • Driver

  • Device

Instead of viewing all types of handles, you can specifically view a specific type of handle by using the -t option supplying it a value from the preceding list. An example format of the command is shown in Listing 14-13, where <object_type> is a type of the handle.
 volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 handles -p 272 -t <object_type>
Listing 14-13

Volatility Command to Specifically List All Handles Belonging to a Specific Type

mutant

Mutex, or mutants, are another form of objects used by processes, as you learned in Chapter 5. You learned the handles command in the previous section to list all the handles used by a process. You also learned that the handles command can be combined with the -t <object_type> option to list specific types of handles. To specifically list all mutex handles, you can use an <object_type> value of a mutant. As an exercise, run the command specified in Listing 14-14 that lists all the mutants in our malicious svchost.exe process, which has PID 272.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 handles -p 272 -t Mutant
Listing 14-14

Volatility handles Command to Specifically List All Mutant Handles

An excerpt from the output from running the command is seen in Figure 14-16.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig16_HTML.jpg
Figure 14-16

All open Mutant handles in our malicious svchost.exe process

Some of these mutant names are specific to malware or a malware family, and searching for them in Google might point you to similar malware analysis reports by other researchers. As seen in Figure 14-17, the mutant value of 2GVWNQJz1 does point us to other analysis reports that indicate that malware belongs to the kuluoz family.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig17_HTML.jpg
Figure 14-17

The mutant handle we obtained 2GVWNQJz1 points to Kuluoz malware family

Scanning Registry

The Windows Configuration Manager, a Windows OS component, is responsible for managing the registry in memory. The Windows Registry is present in the memory and is represented by a structure named CHMHIVE. When we dumped the physical RAM earlier, we also dumped the registry from the memory into the memory dump file, which we can analyze now using Volatility. Volatility by its pool tag scan technique can identify and extract the registry information for us from the memory dump.

Let’s get into some more details about the registry. The Windows registry stores both the information which is specific to a user as well as system-wide information common to all users. The registry is stored on disk in the folder C:WindowsSystem32config in the DEFAULT, SAM, SECURITY, SOFTWARE, SYSTEM files. As seen in Figure 14-18, these files do not have any extensions and are protected system files, which you can only view on your system if you enable viewing protected system files in Folder Options, as seen in Figure 11-1 in Chapter 11.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig18_HTML.jpg
Figure 14-18

The protected system files that hold the Windows Registry on disk

The NTUSER.dat file stores registry data, located at C:Users<user name> tuser.dat. This file stores registry information that is specific to the logged-in user. ntuser.dat is a protected operating system file, and to view it, you need to enable Viewing Protected Operating System Files in Folder Options.

All these registry values from the files on disk are also present in memory in the CHMHIVE structure, which we can access by using Volatility.

hivelist

You learned about registry hives in Chapter 5. The hivelist command in Volatility displays the hives from the memory dump we obtained, including their locations on disk as well in virtual and physical memory and the RAM. As an exercise, run the command in Listing 14-15 to list the registry hives.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 hivelist
Listing 14-15

Volatility Command Hivelist to Specifically List All Registry Hives in the Dump

An excerpt from the command can be seen in Figure 14-19.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig19_HTML.jpg
Figure 14-19

The registry hives information as printed by the hivelist Volatility command

dumpregistry

The dumpregistry command dumps all the registry hive contents from the memory dump into files, which we can then view and analyze. As an exercise, run the command we have listed in Listing 14-16, which dumps the registry hive into the C:/forensic/registry_dump directory.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 dumpregistry -D C:/forensic/registry_dump
Listing 14-16

dumpregistry Command to Dump the Registry Hives to Files on Disk

The dumped registry files have the extension .reg. In Figure 14-20, the file names start with the virtual address displayed in the hivelist command as seen in Figure 14-19.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig20_HTML.jpg
Figure 14-20

The registry hives dumped as files using the dumpregistry Volatility command

These registry files in Figure 14-20 can be viewed using the Registry Viewer tool, which we installed in Chapter 2. You can start the tool by double-clicking it, which should pop up the window in Figure 14-21, after which you can click Yes.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig21_HTML.jpg
Figure 14-21

Click Yes when Registry Viewer pops this window on startup

Clicking Yes again pops up the window shown in Figure 14-22, where you can hit OK to continue opening Registry Viewer.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig22_HTML.jpg
Figure 14-22

Click OK to continue opening the main Registry Viewer tool window

Navigating as we suggested should open the main Registry Viewer window, as seen in Figure 14-23. You can now open the registry hive files (see Figure 14-20), which we dumped earlier using the dumpregistry Volatility command.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig23_HTML.jpg
Figure 14-23

Main Registry Viewer window using which you can open the .reg registry files

Open the hive file registry.0x88488910.ntuserdat.reg from the folder C:/forensic/registry_dump, which should open the registry pane. Now browse to the SOFTWARE/Microsoft/Windows/CurrentVersion/Run registry key to locate and see if our malware sample created any run entries for itself. In Figure 14-24, our malware has indeed created a registry run to its malware file that runs on system startup. The information extracted also matches the same registry key we figured out in our dynamic analysis.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig24_HTML.jpg
Figure 14-24

Main Registry Viewer window using which you can open the .reg registry files

printkey

The whole previous process of dumping the entire registry using the dumpregistry command , and then opening it using Registry Viewer (all of this for viewing the Run entries set by malware) is way too cumbersome. You already learned the various Run entry persistence keys in Chapter 8, so you can use the printkey command in Volatility, which lets you view the registry key and its value.

As an exercise, run the command we have listed in Listing 14-17, which logs information about the SoftwareMicrosoftWindowsCurrentVersionRun registry key.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 printkey -K "SoftwareMicrosoftWindowsCurrentVersionRun"
Listing 14-17

printkey Command to Print Information About the RUN Key in Sample-14-1.mem Dump

An excerpt from the output of the command is shown in Figure 14-25. Compare this process with the whole dumping and then using Registry Viewer to view the Run entry that we carried out in the previous section. This is easier, as long as you know the full registry path whose value you want to view.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig25_HTML.jpg
Figure 14-25

printkey shows a Run entry created by the malware in Sample-14-1.mem dump

Identifying Code Injection and API Hooking

Another wonderful function provided by Volatility is identifying code injection, which we can easily identify using a dynamic analysis tool like APIMiner as seen in Chapter 13. But it is harder to figure out if it’s happening using other tools, like ProcMon and Process Hacker. But Volatility has a nice plugin named malfind, which identifies injected codes.

The malfind Plugin

Volatility’s malfind plugin finds code injected into other processes. Malfind relies on both page properties and the VAD tree to identify injected code. As an exercise, run the command in Listing 14-18, which runs the malfind command against the process svchost.exe (we figured out in our previous analysis that it is most likely process hollowed).
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 malfind -p 272
Listing 14-18

malfind Command to Detect Injected Code

An excerpt from the command’s output is shown in Figure 14-26. It shows the memory chunks containing the injected code.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig26_HTML.jpg
Figure 14-26

The injected code output as seen from the output of malfind command

The memory chunks that the malfind command shows as injected may be large and are hard to view on the command prompt. Let’s dump the memory chunk contents to files on the disk using the -D dump option, as seen in Listing 14-19.
volatility-2.5.standalone.exe -f Sample-14-1.mem --profile=Win7SP1x86 malfind -p 272 -D "C:/forensic/malfind_dump"
Listing 14-19

malfind Command with -D Option to Detect and Dump Injected Code to Disk

The command dumps the suspicious chunks to C:forensicmalfind_dump folder, and as seen from the contents in the folder in Figure 14-27, it dumps the injected code into multiple dump files, all of which have the .dmp file extension.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig27_HTML.jpg
Figure 14-27

Multiple dump files containing injected code from the malfind command we used from Listing 14-19

These dump files are regular binary files and are prime candidates for string analysis using tools like BinText. Let’s look at one of the dumps (process.0x843b1098.0xa0000.dmp) using BinText, which holds content from the virtual memory location 0xa0000.

In Figure 14-28, you can see some meaningful strings, like the SoftwareMicrosoftWindowsCurrentVersionRun, For base!!!!! Run entry in the dump, which seems to be unpacked malware data/strings. By using Volatility, you can also obtain the unpacked contents of a malware process and analyze them offline.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig28_HTML.jpg
Figure 14-28

The strings from the malfind dump file that indicates the malware is unpacked

Detecting API Hooks

Volatility provides an apihooks command that scans for the presence of API Hooks. The command line for running this command is shown in Listing 14-20.
volatility-2.5.standalone.exe -f <memory_dump_file_path> --profile=Win7SP1x86 apihooks -p <pid>
Listing 14-20

apihooks Volatility Command That Detects API Hooks

Do note that by running this command, you might find false positives or hooks created by some of the analysis tools, which aren’t malicious (and which we hope you don’t identify as malicious). So it’s better to first take a memory dump on a clean system before running a malware sample, and next run your sample and take a dump. You can then run the apihooks commands on both the clean and malware memory dumps and compare the apihooks outputs to weed out the false positives.

As an exercise, you can execute malware API hooking exercises from Chapter 10 and Chapter 11 and then acquire a memory dump of the system, and then run the apihooks command to detect the hooks.

Inspecting the Kernel

Volatility can scan the kernel and extract various data structures, allowing us to look at the loaded kernel modules, view the SSDT, and so forth. As a benefit of this feature, you can detect the presence of kernel-mode malware. For analysis, we use Sample-14-2.mem, which is the dump that we acquired after we ran Sample-11-7-ssdt-rootkit.exe. It is a rootkit that hooks the SSDT.

Scanning Kernel Modules

Volatility can locate and dump kernel modules, using the modules, modscan, and moddump commands. Both modules and modscan are great for scanning and listing kernel modules. While modules list kernel modules that are currently loaded on the system, modscan also displays modules that have been unloaded. Both commands have very similar output. Run the command from Listing 14-21 that lists all the loaded kernel modules against our dump Sample-14-2.mem.
volatility-2.5.standalone.exe -f Sample-14-2.mem --profile=Win7SP1x86 modules
Listing 14-21

Volatility Modules Command That Lists All the Loaded Kernel Modules

Figure 14-29 shows us an excerpt from running the command.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig29_HTML.jpg
Figure 14-29

Output from running the Volatility modules command from Listing 14-21

When analyzing malware that inserts kernel modules, listing the kernel modules may not give you enough information to identify the malware kernel module. The best way to figure it out is to first acquire a memory dump on a clean analysis VM before running the malware. Next run the malware and acquire a memory dump. You now have 2 memory dumps: one clean and the other with the malicious information. Use the Volatility commands to list the kernel modules on both the clean and malicious memory dumps and search for differences in the loaded kernel modules between the two outputs, which reveals to you the names and the locations of the kernel modules loaded after running your malware. In our case the kernel module loaded by our malware sample is rootkit.sys located at C:hidden ootkit.sys and is located at address 0x920ca000.

You can similarly use the modscan command and verify its output. You can also dump the modules’ memory contents into a folder using the moddump command. You can run the command in Listing 14-22 that dump all the modules using the moddump command to the C:/forensic/modules_dump directory.
volatility-2.5.standalone.exe -f Sample-14-2.mem --profile=Win7SP1x86 moddump -D C:/forensic/modules_dump
Listing 14-22

moddump Command That Dumps the Contents of All Kernel Modules to Disk

If you specifically want to dump the contents of rootkit.sys kernel module you can do by specifying the base address of the kernel module which you obtained from the modules command in Figure 14-29, which is 0x920ca00, as illustrated by the command listed in Listing 14-23.
volatility-2.5.standalone.exe -f Sample-14-2.mem --profile=Win7SP1x86 moddump --base=0x920ca000 -D misc_dump
Listing 14-23

moddump Command That Dumps the Contents of the Specific rootkit.sys Module

Scanning SSDT

Volatility also has the capability of scanning the SSDT and listing it out. It does not display any suspicious API hooks directly. You have to use common sense to figure out the suspicious ones. You can run the command in Listing 14-24 that lists the contents of SSDT from Sample-14-2.mem.
volatility-2.5.standalone.exe -f Sample-14-2.mem --profile=Win7SP1x86 ssdt
Listing 14-24

Volatility ssdt Command to List the Contents of SSDT

Figure 14-30 shows the output from running the command.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig30_HTML.jpg
Figure 14-30

Output from running the ssdt command from Listing 14-24

Every row in the entry is an entry in the SSDT table; for example, in the first row of output, the first entry in the SSDT (i.e., entry 0x0000) is for the NtAcceptConnectPort service function, which is located in the ntoskrnl.exe kernel module at address 0x82c8dc28.

Similarly Entry 0x0042 is for the Service Function NtCreateFile and is located in rootkit.sys at address 0x920cb190. This is suspicious. But why? If you remember from Chapter 11 all the NT service functions in the kernel are located in the ntoskrnl.exe or win32k.sys kernel modules. But NtCreateFile which also is an NT Service Function is located in rootkit.sys according to the output, which is suspicious and indicates that it has been hooked.

Another technique to figure out hooked SSDTs is similar to the technique we explained in the previous section. Obtain two memory dumps: one clean without running the malware and the other after running the malware. Run the SSDT commands on both these dumps and compare the two outputs. If you see that the SSDT entries in the malicious dump output point to a different location in comparison to the clean dump’s output, then it indicates the presence of SSDT hooks.

Network Communication

Volatility also provides support for investigating network activities. At the time of memory acquisition, both active and terminated connections can be identified using Volatility’s various commands, some of which are listed in Table 14-5. These commands may execute on particular image versions (XP or Win7).
Table 14-5

Volatility Commands That Lists Network Activities from the Dump

command

Image version

Description

connections

Windows XP

Active TCP connections

connscan

Windows XP

Finds terminated connections

socket

Windows XP

Displays listening sockets

netscan

Windows 7 onward

Finds TCP and UDP connections

You can use the commands on the dumps you want to analyze. You can run the command in Listing 14-25 that lists all the network connections from the system on which we took the dump Sample-14-3.mem.
volatility-2.5.standalone.exe -f Sample-14-3.mem --profile=Win7SP1x86 netscan
Listing 14-25

netscan Volatility Command to List the Network Connections from the Dump

The output from the command can be seen in Figure 14-31.
../images/491809_1_En_14_Chapter/491809_1_En_14_Fig31_HTML.jpg
Figure 14-31

Output from the netscan Volatility command ran on Sample-14-3.mem

The output displays that a connection has been established between 192.168.159.130, which is our local analysis VM address and 23.57.113.23 using iexplore.exe, which is the process for Internet Explorer. Similarly, you can use the same command to list the various network connections initiated by the malware processes we run. You can figure out the connections initiated by the malware processes by using the process name and PID of the malware processes obtained from the Owner and Pid columns, respectively.

You can also extract and dump packet captures for the network connections from the memory dump using the Bulk Extractor tool, which you can use using the command line shown in Listing 14-26. The PCAPs extracted can then be analyzed using tools like Wireshark and Suricata.
bulk_extractor32.exe -x all -e net -o <directory_to_store_pcaps> <path_to_memory_dump>
Listing 14-26

Command Line for Bulk Extractor Tool to Extract Packet Captures for the Network Connections from the Memory Dumps

Memory forensics cannot be restricted to malware analysis. Volatility supports many other commands and plugins that are useful to track down forms of attacks. We recommend that you go through other commands supported by Volatility documented in the Volatility wiki.

Summary

Malware forensics is another useful analysis technique in the fight against malware. It is especially useful, especially in incident response situations where we can be called to dissect malware in infected systems. In this chapter, you learned the various steps needed to carry out memory forensics, which involves forensics of the system’s memory. You learned how to dump the contents of the system RAM using various tools like FTK Imager. By using the acquired dumps, you learned how to analyze and dissect them using Volatility, the famous open source memory forensics tool. Using Volatility, we dissected real-world memory dumps acquired after running malware samples. With hands-on exercises, you learned various commands provided by Volatility to obtain various malicious artifacts that helped us conclude that the samples used were malicious. You also learned how to use Volatility commands to dissect the kernel memory to figure out the presence of any malware kernel modules and rootkits.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.94.152