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 21-2 Solutions

Short Answers

  1. The malware contains the resource sections X64, X64DLL, and X86. Each of the resources contains an embedded PE file.

  2. Lab21-02.exe is compiled for a 32-bit system. This is shown in the PE header’s Characteristics field, where the IMAGE_FILE_32BIT_MACHINE flag is set.

  3. The malware attempts to resolve and call IsWow64Process to determine if it is running on an x64 system.

  4. On an x86 machine, the malware drops the X86 resource to disk and injects it into explorer.exe. On an x64 machine, the malware drops two files from the X64 and X64DLL resource sections to disk and launches the executable as a 64-bit process.

  5. On an x86 system, the malware drops Lab21-02.dll into the Windows system directory, which will typically be C:\Windows\System32\.

  6. On an x64 system, the malware drops Lab21-02x.dll and Lab21-02x.exe into the Windows system directory, but because this is a 32-bit process running in WOW64, the directory is C:\Windows\SysWOW64\.

  7. On an x64 system, the malware launches Lab21-02x.exe, which is a 64-bit process. You can see this in the PE header, where the Characteristics field has the IMAGE_FILE_64BIT_MACHINE flag set.

  8. On both x64 and x86 systems, the malware performs DLL injection into explorer.exe. On an x64 system, it drops and runs a 64-bit binary to inject a 64-bit DLL into the 64-bit running explorer.exe. On an x86 system, it injects a 32-bit DLL into the 32-bit running explorer.exe.

Detailed Analysis

Because this malware is the same as Lab12-01.exe except with an added x64 component, a good place to begin our analysis is with Lab 12-1 Solutions. Let’s start by examining the new strings found in this binary, as follows:

IsWow64Process
Lab21-02x.dll
X64DLL
X64
X86
Lab21-02x.exe
Lab21-02.dll

We see a couple of strings that reference x64, as well as the string IsWow64Process, an API call that can tell malware if it is running as a 32-bit process on a 64-bit machine. We also see three suspicious filenames: Lab21-02.dll, Lab21-02x.dll, and Lab21-02x.exe.

Next, we look at the malware in PEview, as shown in Figure C-70.

PEview showing three different resource sections

Figure C-70. PEview showing three different resource sections

We see three different resource sections: X64, X64DLL, and X86. Each appears to contain an embedded PE format file, as evidenced by the MZ header and DOS stub. If we perform a quick dynamic analysis of this malware on x86 and x64 systems, they both produce the annoying pop-ups just like Lab 12-1 Solutions.

Next, we move our analysis to IDA Pro to find out how the malware uses IsWow64Process. We see that Lab21-02.exe begins with the same code as Lab12-01.exe, which dynamically resolves the API functions for iterating through the process list. After those functions are resolved, the code deviates and attempts to dynamically resolve the IsWow64Process function, as shown in Example C-231.

Example C-231. Dynamically resolving IsWow64Process and calling it

004012F2         push    offset aIswow64process  ; "IsWow64Process"
004012F7         push    offset ModuleName       ; "kernel32"
004012FC         mov     [ebp+var_10], 0
00401303         call    ebx ; GetModuleHandleA 
00401305         push    eax             ; hModule
00401306         call    edi ; GetProcAddress 
00401308         mov     myIsWow64Process, eax
0040130D         test    eax, eax 
0040130F         jz      short loc_401322
00401311         lea     edx, [ebp+var_10]
00401314         push    edx
00401315         call    ds:GetCurrentProcess
0040131B         push    eax
0040131C         call    myIsWow64Process 

At , the malware obtains a handle to kernel32.dll and calls GetProcAddress at in order to try to resolve IsWow64Process. If it succeeds, it loads the address of the function into myIsWow64Process.

The test at is used to determine if the malware found the IsWow64Process function, which is available only on newer OSs. The malware does this resolution check first for compatibility with older systems that do not support IsWow64Process. Next, the malware gets its own PID using GetCurrentProcess, and then calls IsWow64Process at , which will return true in var_10 only if the process is a 32-bit application running under WOW64.

Based on the result of the IsWow64Process check, there are two code paths for the malware to take: x86 and x64. We’ll begin our analysis with the x86 path.

X86 Code Path

The x86 code path first passes the strings Lab21-02.dll and X86 to sub_401000. Based on our static analysis, we can guess and rename this function extractResource, as shown in Example C-232 at .

Example C-232. extractResource being called with X86 parameters

004013D9         push    offset aLab2102_dll     ; "Lab21-02.dll"
004013DE         push    offset aX86             ; "X86"
004013E3         call    extractResource        ; formerly sub_401000

Examining the extractResource function, we see that it, in fact, extracts the X86 resource to disk and appends the second argument to the result of GetSystemDirectoryA, thereby extracting the X86 resource to C:\Windows\System32\Lab21-02.dll.

Next, the malware sets SeDebugPrivilege with the call to sub_401130, which uses the API functions OpenProcessToken, LookupPrivilegeValueA, and AdjustTokenPrivileges, as explained in Using SeDebugPrivilege. Then the malware calls EnumProcesses and loops through the process list looking for a module base name of explorer.exe using the strnicmp function.

Finally, the malware performs DLL injection of Lab21-02.dll into explorer.exe using VirtualAllocEx and CreateRemoteThread. This method of DLL injection is identical to Lab 12-1 Solutions. Comparing the MD5 hash of Lab21-02.dll with Lab12-01.dll, we see that they are identical. Therefore, we conclude that this malware operates the same as Lab 12-1 Solutions when it is run on a 32-bit machine. We must investigate the x64 code path to figure out if this malware operates differently on a 64-bit machine.

X64 Code Path

The x64 code path begins by calling the extractResource function twice to extract the X64 and X64DLL resources to disk, as shown in Example C-233.

Example C-233. Resource extraction of two binaries when run on x64

0040132F         push    offset aLab2102x_dll    ; "Lab21-02x.dll"
00401334         push    offset aX64dll          ; "X64DLL"
00401339         mov     eax, edi
0040133B         call    extractResource
...
0040134D         push    offset aLab2102x_exe    ; "Lab21-02x.exe"
00401352         push    offset aX64             ; "X64"
00401357         mov     eax, edi
00401359         call    extractResource

The two binaries are extracted to the files Lab21-02x.dll and Lab21-02x.exe, and placed into the directory returned by GetSystemDirectoryA. However, if we run this malware dynamically on a 64-bit system, we won’t see those binaries in C:\Windows\System32. Since Lab21-02.exe is a 32-bit binary running on a 64-bit machine, it is running under WOW64. The system directory is mapped to C:\Windows\SysWOW64, and that is where we will find these files on a 64-bit machine.

Next, the malware launches Lab21-02x.exe on the local machine using ShellExecuteA. Looking at the PE header of Lab21-02x.exe, we see that the IMAGE_FILE_64BIT_MACHINE flag is set for the Characteristics field. This tells us that this binary is compiled for and will run as a 64-bit process.

In order to disassemble Lab21-02x.exe with IDA Pro, we need to use the x64 advanced version of IDA Pro. When we disassemble this file, we see that from a high level, its structure looks like Lab21-02.exe. For example, Lab21-02x.exe also starts by dynamically resolving the API functions for iterating through the process list. Lab21-02x.exe deviates from Lab21-02.exe when it builds a string using lstrcpyA and lstrcatA, as seen at and in Example C-234.

Example C-234. Building the DLL path string and writing it to a remote process

00000001400011BF         lea     rdx, String2 ; "C:\\Windows\\SysWOW64\\"
00000001400011C6         lea     rcx, [rsp+1168h+Buffer] ; lpString1
...
00000001400011D2         call    cs:lstrcpyA 
00000001400011D8         lea     rdx, aLab2102x_dll      ; "Lab21-02x.dll"
00000001400011DF         lea     rcx, [rsp+1168h+Buffer] ; lpString1
00000001400011E4         call    cs:lstrcatA 
...
00000001400012CF         lea     r8, [rsp+1168h+Buffer] ; lpBuffer
00000001400012D4         mov     r9d, 104h               ; nSize
00000001400012DA         mov     rdx, rax        ; lpBaseAddress
00000001400012DD         mov     rcx, rsi        ; hProcess
00000001400012E0         mov     [rsp+1168h+var_1148], 0
00000001400012E9         call    cs:WriteProcessMemory

The string built matches the location of where the DLL was dropped to disk: C:\Windows\SysWOW64\Lab21-02x.dll. The result of this string will be contained in the local variable Buffer (shown in bold in the listing). Buffer is eventually passed to WriteProcessMemory in register r8 (lpBuffer parameter) at , and luckily IDA Pro has recognized and added comments for the parameters, even though there are not any push instructions.

Seeing the DLL string written to memory like this followed by a call to CreateRemoteThread tells us that this binary also performs DLL injection. We find the string explorer.exe in the strings listing and track its cross-reference to 0x140001100, as shown in Example C-235 at .

Example C-235. Code that uses QueryFullProcessImageNameA to look for the explorer.exe process

00000001400010FA         call    cs:QueryFullProcessImageNameA
0000000140001100         lea     rdx, aExplorer_exe     ; "explorer.exe"
0000000140001107         lea     rcx, [rsp+138h+var_118]
000000014000110C         call    sub_140001368

This code is called within the process iteration loop, and the result of QueryFullProcessImageNameA is passed with explorer.exe to sub_140001368. By inference, we can conclude that this is some sort of string-comparison function that the IDA Pro FLIRT library didn’t recognize.

This malware operates in the same way as the x86 version by injecting into explorer.exe. However, this 64-bit version injects into the 64-bit version of Explorer. We open Lab21-02x.dll in the advanced version of IDA Pro and see that it is identical to Lab21-02.dll, but compiled for x64.