Table of Contents for
Practical Malware Analysis

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Practical Malware Analysis by Andrew Honig Published by No Starch Press, 2012
  1. Cover
  2. Practical Malware Analysis: The Hands-On Guide to Dissecting Malicious Software
  3. Praise for Practical Malware Analysis
  4. Warning
  5. About the Authors
  6. About the Technical Reviewer
  7. About the Contributing Authors
  8. Foreword
  9. Acknowledgments
  10. Individual Thanks
  11. Introduction
  12. What Is Malware Analysis?
  13. Prerequisites
  14. Practical, Hands-On Learning
  15. What’s in the Book?
  16. 0. Malware Analysis Primer
  17. The Goals of Malware Analysis
  18. Malware Analysis Techniques
  19. Types of Malware
  20. General Rules for Malware Analysis
  21. I. Basic Analysis
  22. 1. Basic Static Techniques
  23. Antivirus Scanning: A Useful First Step
  24. Hashing: A Fingerprint for Malware
  25. Finding Strings
  26. Packed and Obfuscated Malware
  27. Portable Executable File Format
  28. Linked Libraries and Functions
  29. Static Analysis in Practice
  30. The PE File Headers and Sections
  31. Conclusion
  32. Labs
  33. 2. Malware Analysis in Virtual Machines
  34. The Structure of a Virtual Machine
  35. Creating Your Malware Analysis Machine
  36. Using Your Malware Analysis Machine
  37. The Risks of Using VMware for Malware Analysis
  38. Record/Replay: Running Your Computer in Reverse
  39. Conclusion
  40. 3. Basic Dynamic Analysis
  41. Sandboxes: The Quick-and-Dirty Approach
  42. Running Malware
  43. Monitoring with Process Monitor
  44. Viewing Processes with Process Explorer
  45. Comparing Registry Snapshots with Regshot
  46. Faking a Network
  47. Packet Sniffing with Wireshark
  48. Using INetSim
  49. Basic Dynamic Tools in Practice
  50. Conclusion
  51. Labs
  52. II. Advanced Static Analysis
  53. 4. A Crash Course in x86 Disassembly
  54. Levels of Abstraction
  55. Reverse-Engineering
  56. The x86 Architecture
  57. Conclusion
  58. 5. IDA Pro
  59. Loading an Executable
  60. The IDA Pro Interface
  61. Using Cross-References
  62. Analyzing Functions
  63. Using Graphing Options
  64. Enhancing Disassembly
  65. Extending IDA with Plug-ins
  66. Conclusion
  67. Labs
  68. 6. Recognizing C Code Constructs in Assembly
  69. Global vs. Local Variables
  70. Disassembling Arithmetic Operations
  71. Recognizing if Statements
  72. Recognizing Loops
  73. Understanding Function Call Conventions
  74. Analyzing switch Statements
  75. Disassembling Arrays
  76. Identifying Structs
  77. Analyzing Linked List Traversal
  78. Conclusion
  79. Labs
  80. 7. Analyzing Malicious Windows Programs
  81. The Windows API
  82. The Windows Registry
  83. Networking APIs
  84. Following Running Malware
  85. Kernel vs. User Mode
  86. The Native API
  87. Conclusion
  88. Labs
  89. III. Advanced Dynamic Analysis
  90. 8. Debugging
  91. Source-Level vs. Assembly-Level Debuggers
  92. Kernel vs. User-Mode Debugging
  93. Using a Debugger
  94. Exceptions
  95. Modifying Execution with a Debugger
  96. Modifying Program Execution in Practice
  97. Conclusion
  98. 9. OllyDbg
  99. Loading Malware
  100. The OllyDbg Interface
  101. Memory Map
  102. Viewing Threads and Stacks
  103. Executing Code
  104. Breakpoints
  105. Loading DLLs
  106. Tracing
  107. Exception Handling
  108. Patching
  109. Analyzing Shellcode
  110. Assistance Features
  111. Plug-ins
  112. Scriptable Debugging
  113. Conclusion
  114. Labs
  115. 10. Kernel Debugging with WinDbg
  116. Drivers and Kernel Code
  117. Setting Up Kernel Debugging
  118. Using WinDbg
  119. Microsoft Symbols
  120. Kernel Debugging in Practice
  121. Rootkits
  122. Loading Drivers
  123. Kernel Issues for Windows Vista, Windows 7, and x64 Versions
  124. Conclusion
  125. Labs
  126. IV. Malware Functionality
  127. 11. Malware Behavior
  128. Downloaders and Launchers
  129. Backdoors
  130. Credential Stealers
  131. Persistence Mechanisms
  132. Privilege Escalation
  133. Covering Its Tracks—User-Mode Rootkits
  134. Conclusion
  135. Labs
  136. 12. Covert Malware Launching
  137. Launchers
  138. Process Injection
  139. Process Replacement
  140. Hook Injection
  141. Detours
  142. APC Injection
  143. Conclusion
  144. Labs
  145. 13. Data Encoding
  146. The Goal of Analyzing Encoding Algorithms
  147. Simple Ciphers
  148. Common Cryptographic Algorithms
  149. Custom Encoding
  150. Decoding
  151. Conclusion
  152. Labs
  153. 14. Malware-Focused Network Signatures
  154. Network Countermeasures
  155. Safely Investigate an Attacker Online
  156. Content-Based Network Countermeasures
  157. Combining Dynamic and Static Analysis Techniques
  158. Understanding the Attacker’s Perspective
  159. Conclusion
  160. Labs
  161. V. Anti-Reverse-Engineering
  162. 15. Anti-Disassembly
  163. Understanding Anti-Disassembly
  164. Defeating Disassembly Algorithms
  165. Anti-Disassembly Techniques
  166. Obscuring Flow Control
  167. Thwarting Stack-Frame Analysis
  168. Conclusion
  169. Labs
  170. 16. Anti-Debugging
  171. Windows Debugger Detection
  172. Identifying Debugger Behavior
  173. Interfering with Debugger Functionality
  174. Debugger Vulnerabilities
  175. Conclusion
  176. Labs
  177. 17. Anti-Virtual Machine Techniques
  178. VMware Artifacts
  179. Vulnerable Instructions
  180. Tweaking Settings
  181. Escaping the Virtual Machine
  182. Conclusion
  183. Labs
  184. 18. Packers and Unpacking
  185. Packer Anatomy
  186. Identifying Packed Programs
  187. Unpacking Options
  188. Automated Unpacking
  189. Manual Unpacking
  190. Tips and Tricks for Common Packers
  191. Analyzing Without Fully Unpacking
  192. Packed DLLs
  193. Conclusion
  194. Labs
  195. VI. Special Topics
  196. 19. Shellcode Analysis
  197. Loading Shellcode for Analysis
  198. Position-Independent Code
  199. Identifying Execution Location
  200. Manual Symbol Resolution
  201. A Full Hello World Example
  202. Shellcode Encodings
  203. NOP Sleds
  204. Finding Shellcode
  205. Conclusion
  206. Labs
  207. 20. C++ Analysis
  208. Object-Oriented Programming
  209. Virtual vs. Nonvirtual Functions
  210. Creating and Destroying Objects
  211. Conclusion
  212. Labs
  213. 21. 64-Bit Malware
  214. Why 64-Bit Malware?
  215. Differences in x64 Architecture
  216. Windows 32-Bit on Windows 64-Bit
  217. 64-Bit Hints at Malware Functionality
  218. Conclusion
  219. Labs
  220. A. Important Windows Functions
  221. B. Tools for Malware Analysis
  222. C. Solutions to Labs
  223. Lab 1-1 Solutions
  224. Lab 1-2 Solutions
  225. Lab 1-3 Solutions
  226. Lab 1-4 Solutions
  227. Lab 3-1 Solutions
  228. Lab 3-2 Solutions
  229. Lab 3-3 Solutions
  230. Lab 3-4 Solutions
  231. Lab 5-1 Solutions
  232. Lab 6-1 Solutions
  233. Lab 6-2 Solutions
  234. Lab 6-3 Solutions
  235. Lab 6-4 Solutions
  236. Lab 7-1 Solutions
  237. Lab 7-2 Solutions
  238. Lab 7-3 Solutions
  239. Lab 9-1 Solutions
  240. Lab 9-2 Solutions
  241. Lab 9-3 Solutions
  242. Lab 10-1 Solutions
  243. Lab 10-2 Solutions
  244. Lab 10-3 Solutions
  245. Lab 11-1 Solutions
  246. Lab 11-2 Solutions
  247. Lab 11-3 Solutions
  248. Lab 12-1 Solutions
  249. Lab 12-2 Solutions
  250. Lab 12-3 Solutions
  251. Lab 12-4 Solutions
  252. Lab 13-1 Solutions
  253. Lab 13-2 Solutions
  254. Lab 13-3 Solutions
  255. Lab 14-1 Solutions
  256. Lab 14-2 Solutions
  257. Lab 14-3 Solutions
  258. Lab 15-1 Solutions
  259. Lab 15-2 Solutions
  260. Lab 15-3 Solutions
  261. Lab 16-1 Solutions
  262. Lab 16-2 Solutions
  263. Lab 16-3 Solutions
  264. Lab 17-1 Solutions
  265. Lab 17-2 Solutions
  266. Lab 17-3 Solutions
  267. Lab 18-1 Solutions
  268. Lab 18-2 Solutions
  269. Lab 18-3 Solutions
  270. Lab 18-4 Solutions
  271. Lab 18-5 Solutions
  272. Lab 19-1 Solutions
  273. Lab 19-2 Solutions
  274. Lab 19-3 Solutions
  275. Lab 20-1 Solutions
  276. Lab 20-2 Solutions
  277. Lab 20-3 Solutions
  278. Lab 21-1 Solutions
  279. Lab 21-2 Solutions
  280. Index
  281. Index
  282. Index
  283. Index
  284. Index
  285. Index
  286. Index
  287. Index
  288. Index
  289. Index
  290. Index
  291. Index
  292. Index
  293. Index
  294. Index
  295. Index
  296. Index
  297. Index
  298. Index
  299. Index
  300. Index
  301. Index
  302. Index
  303. Index
  304. Index
  305. Index
  306. Index
  307. Updates
  308. About the Authors
  309. Copyright

Lab 17-3 Solutions

Short Answers

  1. The malware immediately terminates inside a VM, unlike Lab 12-2 Solutions, which performs process replacement on svchost.exe.

  2. If you force the jumps at 0x4019A1, 0x4019C0, and 0x401467 to be taken, and the jump at 0x401A2F to not be taken, the malware performs process replacement using a keylogger from its resource section.

  3. The malware uses four different anti-VM techniques:

    • It uses the backdoor I/O communication port.

    • It searches the registry key SYSTEM\CurrentControlSet\Control\DeviceClasses for the string vmware.

    • It checks the MAC address to see if it is the default used by VMware.

    • It searches the process list with a string-hashing function for processes starting with the string vmware.

  4. To avoid the anti-VM techniques used by this malware, you can remove VMware tools and modify the MAC address.

  5. In OllyDbg, you can apply the following patches:

    • NOP-out the instruction at 0x40145D.

    • Change the instructions at 0x40199F and 0x4019BE to xor eax, eax.

    • Modify the instruction at 0x40169F to jmp 0x40184A.

Detailed Analysis

As noted in the lab description, this malware is the same as Lab12-02.exe except that it includes anti-VM techniques. Therefore, a good place to start is with a review of Lab 12-2 Solutions.

Searching for Vulnerable Instructions

We begin by loading the binary into IDA Pro and searching for vulnerable x86 instructions using findAntiVM.py (as in Lab 17-1 Solutions). This script identifies one anti-VM instruction at 0x401AC8 and highlights it in red. We notice that this is the backdoor I/O communication port being queried via the in instruction. This anti-VM technique is contained in the function named sub_401A80 by IDA Pro. This function returns 1 if it is executing inside a VM; otherwise, it returns 0. There is only one cross-reference from the beginning of the main function, as shown at in Example C-165.

Example C-165. The check after the call to query the I/O communication port

0040199A         call    sub_401A80    ; Query I/O communication port
0040199F         test    eax, eax 
004019A1         jz      short loc_4019AA 
004019A3         xor     eax, eax
004019A5         jmp     loc_401A71

The jz instruction at must be taken, or the main method will terminate immediately by jumping to 0x401A71. We disable this anti-VM technique by setting the zero flag to 1 when execution arrives at the jz instruction. To permanently disable this technique, change the test instruction at into xor eax, eax as follows:

  1. Start OllyDbg and place your cursor on line 0x40199F.

  2. Press the spacebar and enter xor eax, eax in the text box.

  3. Click Assemble.

Finding Anti-VM Techniques Using Strings

Next, we use Strings to compare the output from Lab 12-2 Solutions to the output from Lab17-03.exe. The following are the new strings found in this lab:

vmware
SYSTEM\CurrentControlSet\Control\DeviceClasses
Iphlpapi.dll
GetAdaptersInfo

These strings provide us with interesting leads. For example, the string SYSTEM\CurrentControlSet\Control\DeviceClasses appears to be a registry path, and GetAdaptersInfo is a function used for getting information about the network adapter. Digging deeper into the first string in the listing, vmware, with IDA Pro, we find only one cross-reference to this string from the subroutine sub_4011C0.

Figure C-66 shows the cross-reference graph for sub_4011C0. The arrows leaving sub_4011C0 show that it calls several registry functions. The function also calls itself, as shown by the arrow that loops back (making it a recursive function). Based on the graph, we suspect that the function is recursively checking the registry for the string vmware. Finally, Figure C-66 shows that sub_4011C0 is called from main.

Cross-reference graph for sub_4011C0

Figure C-66. Cross-reference graph for sub_4011C0

Example C-166 shows where sub_4011C0 is called at inside the main function. Three parameters are pushed onto the stack before the call, including the registry key, which we saw in the strings listing.

Example C-166. The parameters for sub_4011C0 and the check after

004019AA         push    2                ; int
004019AC         push    offset SubKey    ; "SYSTEM\\CurrentControlSet\\Control\\Device"...
004019B1         push    80000002h        ; hKey
004019B6         call    sub_4011C0 
004019BB         add     esp, 0Ch
004019BE         test    eax, eax 
004019C0         jz      short loc_4019C9 

Since SYSTEM\CurrentControlSet\Control\DeviceClasses is passed to a recursive registry function, we can assume this function is recursively checking the registry from that path on. This is a system residue check, as described in Chapter 17. If you examine sub_4011C0 further, you will see it loop through the registry subkeys under DeviceClasses. It compares the first six characters (after changing them to lowercase) of each subkey name to the string vmware.

Since our goal is to have the malware run in our safe environment, we just need to ensure that the jz instruction at is taken; otherwise, the program will terminate immediately. We disable this anti-VM technique by making sure the zero flag is 1 when we arrive at the jz instruction. We can permanently disable this check by changing the test instruction at into xor eax, eax using OllyDbg, as described in Searching for Vulnerable Instructions.

Next, we use IDA Pro to check the cross-references for the string GetAdaptersInfo. In Example C-167, we see the string referenced at .

Example C-167. The dynamic resolution of GetAdaptersInfo

004019C9         push    offset aGetadaptersinf  ; "GetAdaptersInfo" 
004019CE         push    offset LibFileName      ; "Iphlpapi.dll"
004019D3         call    ds:LoadLibraryA
004019D9         push    eax                     ; hModule
004019DA         call    ds:GetProcAddress
004019E0         mov     GetAdaptersInfo_Address , eax

The malware dynamically resolves GetAdaptersInfo using LoadLibraryA and GetProcAddress, and loads the resulting address into a global variable, which we have renamed GetAdaptersInfo_Address at to make it easier to recognize function calls to the runtime-loaded address of GetAdaptersInfo.

Checking the cross-references to GetAdaptersInfo_Address, we see it called in two places within the function sub_401670. At a high level, this function appears similar to a function we examined in Lab 12-2 Solutions that loaded the resource section containing the keylogger. However, the function in this lab appears to have a bunch of code added to the start. Let’s examine that code.

Example C-168 shows the start of a series of byte moves at . This byte array initialization can be converted to a byte array by double-clicking var_38 and setting it to an array of size 27. We rename the array to Byte_Array to aid our analysis later on.

Example C-168. Byte array initialization and first call to GetAdaptersInfo_Address

004016A8        mov     [ebp+var_38], 0 
004016AC        mov     [ebp+var_37], 50h
004016B0        mov     [ebp+var_36], 56h
004016B4        mov     [ebp+var_35], 0
004016B8        mov     [ebp+var_34], 0Ch
004016BC        mov     [ebp+var_33], 29h
...
0040170C        mov     [ebp+var_1F], 0
00401710        mov     [ebp+var_1E], 27h
00401714        mov     [ebp+dwBytes], 0
0040171B        lea     eax, [ebp+dwBytes]
0040171E        push    eax
0040171F        push    0
00401721        call    GetAdaptersInfo_Address 

The call to GetAdaptersInfo_Address at in Example C-168 takes two parameters: a linked list of IP_ADAPTER_INFO structures and the size of that linked list. Here, the linked list passed in is NULL, and the size will be returned in dwBytes. Calling GetAdaptersInfo_Address with the first parameter set to NULL is an easy way to figure out how much data it returns in order to allocate memory for the linked list structure to be used in a second call to GetAdaptersInfo_Address. This is the reason the malware uses dwBytes in subsequent calls to GetProcessHeap and HeapAlloc.

Example C-169 shows that the malware uses HeapAlloc at and calls GetAdaptersInfo_Address a second time at .

Example C-169. Second call to GetAdaptersInfo_Address, which populates the results

0040174B         call    ds:HeapAlloc 
00401751         mov     [ebp+lpMem], eax 
00401754         cmp     [ebp+lpMem], 0
...
00401766         lea     edx, [ebp+dwBytes]
00401769         push    edx
0040176A         mov     eax, [ebp+lpMem]
0040176D         push    eax
0040176E         call    GetAdaptersInfo_Address 

The parameter labeled lpMem by IDA Pro is the return value from HeapAlloc, as seen at . This parameter is passed to the second call of GetAdaptersInfo_Address at instead of NULL. After the call to GetAdaptersInfo_Address, the lpMem parameter is a pointer to a linked list of IP_ADAPTER_INFO structures with a size of dwBytes.

We must add the IP_ADAPTER_INFO structure to IDA Pro since it failed to recognize and label things fully. To do so, press the INSERT key within the Structures window and add the standard structure IP_ADAPTER_INFO. Now apply the structure to data in our disassembly as shown in Table C-9 at , , and .

Table C-9. Before and After Applying Structure Information and Standard Constants

Before

After

mov    edx, [ebp+lpMem]
cmp    dword ptr [edx+1A0h], 6
jz     short loc_4017B9
mov    eax, [ebp+lpMem]
cmp    dword ptr [eax+1A0h], 71h
jnz    short loc_401816
mov    ecx, [ebp+lpMem]
cmp    dword ptr [ecx+190h], 2
jbe    short loc_401816
mov    edx, [ebp+lpMem]
cmp    [edx+IP_ADAPTER_INFO.Type], MIB_IF_TYPE_ETHERNET
jz     short loc_4017B9
mov    eax, [ebp+lpMem]
cmp    [eax+IP_ADAPTER_INFO.Type], IF_TYPE_IEEE80211
jnz    short loc_401816
mov    ecx, [ebp+lpMem]
cmp    [ecx+IP_ADAPTER_INFO.AddressLength], 2
jbe    short loc_401816

The left side of Table C-9 shows the code listing before we apply the IP_ADAPTER_INFO structure offsets and standard constants to the data. To apply the structure, right-click the locations , , and , and you will be given the option to turn numbers into the descriptive strings shown in bold in the right side of the table. Using the MSDN page for IP_ADAPTER_INFO as reference, we learn about the standard constants for Type and see that 0x6 and 0x71 correspond to an adapter type of Ethernet or 802.11 wireless (so the address will be a MAC address).

In the three comparisons shown in Table C-9, the malware is checking for Ethernet or wireless interfaces, and then confirming that the adapter address length is greater than 2. If this check fails, the malware loops to the next adapter in the linked list. If the check succeeds, the code shown in Example C-170 will execute.

Example C-170. Comparing the adapter address to Byte_Array

004017CC         jmp     short loc_4017D7
004017CE         mov     edx, [ebp+var_3C]
004017D1         add     edx, 3 
004017D4         mov     [ebp+var_3C], edx
...
004017DD         mov     ecx, 3 
004017E2         mov     eax, [ebp+var_3C]
004017E5         lea     edi, [ebp+eax+Byte_Array] 
004017E9         mov     esi, [ebp+lpMem]
004017EC         add     esi, 194h 
004017F2         xor     edx, edx
004017F4         repe cmpsb
004017F6         jnz     short loc_401814

To make this code more readable, right-click the 194h at and change it to IP_ADAPTER_INFO.Address.

The code is comparing the currently referenced IP_ADAPTER_INFO’s address to an index in Byte_Array. Byte_Array is indexed at using EAX, which is filled with var_3C, a loop counter that we see incremented by 3 at . The repe cmpsb instruction compares Byte_Array to the IP_ADAPTER_INFO.Address for 3 bytes (because ECX is set to 3 at ), which means it is checking to see if the first 3 bytes of the MAC address are {00h,50h,56h} or {00h,0Ch,29h} and so on. An Internet search for “00,0C,29” tells us that it is a common start of the default MAC address for VMware. Since the array is of size 27, we know that this code compares nine different MAC addresses (most associated with VMware).

We permanently disable this check by avoiding the MAC address comparisons altogether. Modify the jnz instruction at 0x40169F to be jmp 0x40184A using OllyDbg’s Assemble functionality, as we did earlier to force the malware to skip the adapter checks and go straight to the resource section manipulation code.

Reviewing the Final Check

The final anti-VM check in this malware is in sub_401400, which performs process replacement. The code in Example C-171 shows a call at , which determines if the jz at will be taken. If the jump is not taken, the code will terminate without performing the process replacement.

Example C-171. Final anti-VM check

00401448         xor     eax, eax 
...
00401456         push    6
00401458         push    0F30D12A5h
0040145D         call    sub_401130 
00401462         add     esp, 8
00401465         test    eax, eax
00401467         jz      short loc_401470 

As shown in Example C-171, the anti-VM function sub_401130 takes two parameters: 6 and the integer 0xF30D12A5. This function loops through the process listing by calling CreateToolhelp32Snapshot, Process32First, and Process32Next. Process32Next is inside a loop with the code shown in Example C-172.

Example C-172. Code for comparing a process name string

0040116D         mov     edx, [ebp+arg_4]
00401170         push    edx
00401171         lea     eax, [ebp+pe.szExeFile]
00401177         push    eax
00401178         call    sub_401060   ; make lowercase
0040117D         add     esp, 4
00401180         push    eax
00401181         call    sub_401000   ; get string hash
00401186         add     esp, 8
00401189         mov     [ebp+var_130], eax
0040118F         mov     ecx, [ebp+var_130]
00401195         cmp     ecx, [ebp+arg_0] 

The function sub_401060 called at takes a single parameter containing the name of the process and sets all of the parameter’s characters to lowercase. The function sub_401000 called at takes two parameters: 6 (arg_4) and the lowercase string returned from sub_401060. The result of this function is compared to the 0xF30D12A5 (arg_0) at . If the result is equal to 0xF30D12A5, the function will return 1, which will cause the malware to terminate. In other words, sub_401000 is taking the process name and turning it into a number, and then seeing if that number is equal to a preset value. sub_401000 is a simple string-hashing function. Given the parameter "vmware", it returns 0xF30D12A5. The malware is cleverly using a string hash to avoid using the string vmware in the comparison, which would have made easy pickings for the malware analyst.

To permanently disable this final anti-VM check, we can NOP-out the call to sub_401130 at 0x40145D. This forces the malware to skip the check and go straight to the process-replacement code because the xor at in Example C-171 ensures that the EAX register will be 0.

Summary

This malware performs four different checks for VMware. Three of these check for system residue, and the other queries the I/O communication port. The system residue checking techniques include the following:

  • Check the first 3 bytes of the MAC address for known values associated with virtual machines.

  • Check the registry for the key vmware under the registry path SYSTEM\CurrentControlSet\Control\DeviceClasses.

  • Check the process listing for processes beginning with the string vmware in any combination of uppercase and lowercase letters.