Practical Malware Analysis: CH 7: Analyzing Malicious Windows Programs
Practical Malware Analysis: CH 7: Analyzing Malicious Windows Programs
Practical Malware Analysis: CH 7: Analyzing Malicious Windows Programs
• From Ch 2
Registry Code
.REG Files
Networking APIs
Berkeley Compatible Sockets
• Winsock libraries, primarily in ws2_32.dll
– Almost identical in Windows and Unix
Server and Client Sides
• Server side
– Maintains an open socket waiting for connections
– Calls, in order, socket, bind, listen, accept
– Then send and recv as necessary
• Client side
– Connects to a waiting socket
– Calls, in order, socket, connect
– Then send and recv as necessary
Simplified
Server
Program
Realistic code
would call
WSAGetLastError
many times
The WinINet API
• Higher-level API than Winsock
• Functions in Wininet.dll
• Implements Application-layer protocols like
HTTP and FTP
• InternetOpen – connects to Internet
• InternetOpenURL –connects to a URL
• InternetReadFile –reads data from a
downloaded file
Following Running Malware
Transferring Execution
• jmp and call transfer execution to another
part of code, but there are other ways
– DLLs
– Processes
– Threads
– Mutexes
– Services
– Component Object Model (COM)
– Exceptions
DLLs (Dynamic Link Libraries)
• Share code among multiple applications
• DLLs export code that can be used by other
applications
• Static libraries were used before DLLs
– They still exist, but are much less common
– They cannot share memory among running
processes
– Static libraries use more RAM than DLLs
DLL Advantages
• Using DLLs already included in Windows
makes code smaller
• Software companies can also make custom
DLLs
– Distribute DLLs along with EXEs
How Malware Authors Use DLLs
• Store malicious code in DLL
– Sometimes load malicious DLL into another
process
• Using Windows DLLs
– Nearly all malware uses basic Windows DLLS
• Using third-party DLLs
– Use Firefox DLL to connect to a server, instead of
Windows API
Basic DLL Structure
• DLLs are very similar to EXEs
• PE file format
• A single flag indicates that it's a DLL instead of
an EXE
• DLLs have more exports & fewer imports
• DllMain is the main function, not exported,
but specified as the entry point in the PE
Header
– Called when a function loads or unloads the
library
Processes
• Every program being executed by Windows is
a process
• Each process has its own resources
– Handles, memory
• Each process has one or more threads
• Older malware ran as an independent process
• Newer malware executes its code as part of
another process
Many Processes Run at Once
Memory Management
• Each process uses resources, like CPU, file
system, and memory
• OS allocates memory to each process
• Two processes accessing the same memory
address actually access different locations in
RAM
– Virtual address space
Creating a New Process
• CreateProcess
– Can create a simple remote shell with one
function call
– STARTUPINFO parameter contains handles for
standard input, standard output, and standard
error streams
• Can be set to a socket, creating a remote shell
Code to Create a Shell