Malware Basics and Security Analysis
Malware Types and Characteristics
Main Malware Types
Computer Virus
Definition: Malicious code capable of self-replication and infecting other programs or files
Characteristics:
- Attaches to legitimate programs or files
- Requires host program execution to activate
- Can modify or destroy system files
- Has propagation capability
Common Variants:
- Boot sector virus: Infects disk boot sector
- File infection virus: Infects executable files
- Macro virus: Infects macros in documents (e.g., Word, Excel)
Trojan Horse
Definition: Malware disguised as a legitimate program or file
Characteristics:
- Does not have self-replication capability
- Typically spread through social engineering
- Can perform various malicious activities (e.g., data theft, remote control)
- Users mistakenly believe it is a legitimate program and run it actively
Common Types:
- Remote Access Trojan (RAT): Allows attacker to remotely control victim system
- Keylogger Trojan: Records user keyboard input
- Disguised software Trojan: Masquerades as legitimate software (e.g., game crackers)
Ransomware
Definition: Malware that encrypts victim files and demands ransom
Characteristics:
- Encrypts important user files (documents, pictures, databases, etc.)
- Displays ransom message demanding payment (usually in cryptocurrency)
- May have time limits, with files deleted or ransom increased upon expiration
- Typically spread via phishing emails or vulnerability exploitation
Common Variants:
- File encryption ransomware: Encrypts user files
- Screen locker ransomware: Locks screen and displays ransom message
- Double extortion: Not only encrypts files but also threatens to leak sensitive data
Other Malware Types
Worm
Definition: Malicious program capable of self-replication and automatic propagation through networks
Characteristics:
- Does not require host program to propagate
- Exploits network vulnerabilities or social engineering for spread
- Can quickly infect large numbers of systems
- May carry other malicious payloads
Famous Cases:
- ILOVEYOU worm (2000)
- Conficker worm (2008)
- WannaCry ransomware (includes worm propagation mechanism)
Spyware
Definition: Malware that secretly collects user information
Characteristics:
- Runs covertly, usually without user knowledge
- Collects keyboard input, screenshots, network activity, etc.
- May send data to remote servers
- Often used for ad tracking or identity theft
Common Types:
- Adware: Displays unwanted advertisements
- Keylogger: Records keyboard input
- Information stealer: Steals login credentials, credit card information, etc.
Adware
Definition: Software that displays unwanted advertisements
Characteristics:
- Usually included as ad components in legitimate software
- May collect user browsing habits for targeted ads
- May include spyware functionality
- Users may inadvertently agree to installation
Botnet
Definition: Network of computers infected by malware and controlled by an attacker
Characteristics:
- Infected computers are called “zombies” or “bots”
- Attacker controls botnet via Command and Control (C&C) server
- Can be used for Distributed Denial of Service (DDoS) attacks
- Can be used to send spam or spread other malware
Infection Mechanisms and Propagation Methods
Common Infection Mechanisms
File Infection
Methods:
- Modifies executable files to execute malicious code when program loads
- Attaches to document files (e.g., Office documents, PDFs) via macros or scripts
- Infects critical system files (e.g., boot sector, dynamic link libraries)
Examples:
- Virus attaches to .exe file and activates when user runs it
- Macro virus infects Word document and executes when user opens document and enables macros
Memory Infection
Methods:
- Injects code into running processes via vulnerability exploitation
- Uses DLL injection to inject malicious code into legitimate processes
- Modifies code or data structures in memory
Examples:
- Exploits browser vulnerability to inject malicious code into browser process
- Uses DLL hijacking to load malicious DLL into legitimate applications
Startup Item Infection
Methods:
- Adds to system startup items to execute on system boot
- Modifies registry startup items
- Infects boot manager or boot loader
Examples:
- Adds malicious program path under Windows registry Run key
- Modifies MBR (Master Boot Record) to load malicious code
Network Infection
Methods:
- Performs remote code execution via network service vulnerabilities
- Spreads via phishing emails or malicious links
- Spreads via unprotected network shares
Examples:
- Uploads and executes malicious scripts via web server vulnerability
- Sends emails with malicious attachments via phishing
Common Propagation Methods
Email Propagation
Methods:
- Sends emails with malicious attachments
- Phishing emails containing malicious links
- Automatically executes malicious code via email client vulnerabilities
Characteristics:
- Uses social engineering to trick users into opening attachments or clicking links
- May masquerade as legitimate emails (e.g., invoices, delivery notifications)
- May include urgent or threatening language to prompt quick action
Malicious Website Propagation
Methods:
- Automatically downloads malware when visiting compromised websites
- Performs drive-by download attacks via browser vulnerabilities
- Masquerades as legitimate websites (e.g., banking, e-commerce sites)
Characteristics:
- Users often visit malicious sites unknowingly
- May distribute malicious ads via ad networks
- May combine with other attack methods (e.g., phishing)
Removable Media Propagation
Methods:
- Infects USB drives, external hard drives, and other removable storage
- Automatically executes malicious code via autoplay
- Spreads via file sharing
Characteristics:
- Exploits user habits with removable media
- May masquerade as legitimate files or folders
- Propagates between different devices
Software Vulnerability Exploitation Propagation
Methods:
- Performs remote code execution via OS or application vulnerabilities
- Spreads via vulnerability exploitation frameworks (e.g., Metasploit)
- Exploits known vulnerabilities in unpatched software
Characteristics:
- Does not require user interaction to propagate
- Can spread rapidly on a large scale
- Usually targets specific software or system versions
Social Engineering Propagation
Methods:
- Distributes disguised as legitimate software or updates
- Sends malicious links or files via instant messaging
- Impersonates technical support for fraud
Characteristics:
- Exploits user trust or ignorance
- May combine with other propagation methods
- Difficult to completely prevent with technical measures
Malware Sample Acquisition and Safe Operations
Sample Acquisition Methods
Legitimate Acquisition Methods
Methods:
- Malware analysis platforms (e.g., VirusTotal, MalwareBazaar)
- Samples released by security research institutions
- Public malware sample repositories
- Samples from legally captured network traffic
Precautions:
- Ensure sample sources are legitimate and reliable
- Comply with relevant laws and regulations
- Obtain necessary authorization before acquiring samples
Self-Captured Samples
Methods:
- Set up honeypot systems to capture attack samples
- Analyze malicious files in infected systems
- Capture malicious downloads from network traffic
- Obtain samples from user-reported infection incidents
Precautions:
- Ensure capture process is legal and compliant
- Take appropriate security measures to prevent secondary infections
- Record sample source and capture environment
Sample Analysis Environment Setup
Isolated Analysis Environment
Recommended Configuration:
- Use virtual machines (e.g., VMware, VirtualBox) to create isolated environment
- Configure network isolation (e.g., internal network or no network)
- Use snapshot functionality for easy environment restoration
- Disable unnecessary services (e.g., shared folders, clipboard sharing)
Security Measures:
- Physical isolation: Perform analysis on separate physical machines
- Network isolation: Completely disconnect from network or use dedicated network
- System isolation: Use dedicated analysis systems, not mixed with production systems
Analysis Tool Preparation
Common Tools:
- Static analysis tools:
- IDA Pro: Disassembly and reverse engineering
- Ghidra: NSA open-source decompiler
- PEiD: Detects executable file information
- Strings: Extracts string information
- Dynamic analysis tools:
- Process Monitor: Monitors system activity
- Process Explorer: Views process information
- Wireshark: Network traffic analysis
- Cuckoo Sandbox: Automated malware analysis
- Auxiliary tools:
- Dependency Walker: Analyzes DLL dependencies
- VirusTotal: Online malware scanning
- VirusTotal GUI: Localized VirusTotal analysis
Safe Operation Standards
Basic Principles:
- Always assume samples are malicious
- Minimize data and software in analysis environment
- Use non-administrator accounts for analysis
- Regularly back up analysis environment
- Record all analysis steps and findings
Specific Measures:
- Disable shared folders and clipboard in virtual machines
- Use network isolation or dedicated network
- Create system snapshots before analysis
- Use sandbox environment for dynamic analysis
- Thoroughly clean environment after analysis
Safe Operation Procedures
Sample Reception and Storage
Procedure:
- Verify source and integrity when receiving samples
- Store samples on isolated storage devices
- Record sample metadata (e.g., source, acquisition time, hash values)
- Perform preliminary classification and labeling
Security Measures:
- Use encrypted storage to protect samples
- Restrict access permissions to sample storage
- Record all sample access and operation logs
Sample Analysis
Procedure:
- Perform static analysis (without executing sample):
- Extract file information (e.g., hash values, digital signatures)
- Analyze file structure (e.g., PE header, resource section)
- Extract strings and code snippets
- Detect known malicious features
- Perform dynamic analysis (execute in controlled environment):
- Monitor system calls and API calls
- Record network activity
- Observe file system and registry modifications
- Analyze process behavior
- Comprehensively analyze results to determine malware type and behavior
Security Measures:
- Use isolated virtual machine environment for analysis
- Disable unnecessary services and features
- Use snapshot functionality for easy environment restoration
- Record all analysis steps and findings
Sample Handling and Reporting
Procedure:
- Clean analysis environment after analysis
- Generate analysis report, including:
- Malware basic information (name, type, family)
- Behavior analysis (infection mechanism, propagation method, malicious activities)
- Technical details (code analysis, network communication, persistence methods)
- Recommended mitigation measures
- Handle samples according to organizational policy (e.g., archive, destroy)
- Share analysis results with relevant departments (e.g., IT security team, law enforcement)
Security Measures:
- Ensure analysis reports do not contain sensitive information
- Handle malware samples according to organizational policy
- Restrict report access permissions
- Record report distribution status
Malware Prevention and Response
Preventive Measures
User Education
Measures:
- Train users to identify phishing emails and malicious links
- Educate users not to open attachments from unknown sources
- Remind users to use strong passwords and multi-factor authentication
- Conduct regular security awareness training
Best Practices:
- Simulate phishing attacks to test user vigilance
- Provide clear security policies and guidelines
- Encourage reporting of suspicious activities
System Hardening
Measures:
- Keep operating system and applications updated
- Install and update antivirus software
- Configure firewall to restrict unnecessary network access
- Disable unnecessary services and features
- Run applications with least privilege principle
Best Practices:
- Regularly perform security assessments and vulnerability scans
- Implement patch management processes
- Configure security baseline settings
Network Security
Measures:
- Implement network segmentation and isolation
- Configure intrusion detection/prevention systems (IDS/IPS)
- Use secure network protocols (e.g., HTTPS, SSH)
- Restrict external access to internal network
- Monitor network traffic anomalies
Best Practices:
- Regularly review network configuration and security policies
- Implement network access control lists (ACL)
- Use VPN to protect remote access
Detection and Response
Detection Techniques
Methods:
- Signature detection: Detects based on known malware signatures
- Behavior detection: Detects based on anomalous behavior patterns
- Heuristic detection: Detects based on suspicious code features
- Sandbox detection: Executes suspicious files in isolated environment
- Threat intelligence: Detects based on known threat indicators
Tools:
- Antivirus software
- Endpoint Detection and Response (EDR) systems
- Network traffic analysis tools
- Log analysis tools
- Threat intelligence platforms
Response Procedures
Steps:
- Detect suspicious activity or malware
- Isolate affected systems or network segments
- Collect evidence (logs, memory dumps, file samples)
- Analyze malware behavior and impact
- Determine attack scope and impact
- Take mitigation measures (e.g., remove malware, patch vulnerabilities)
- Restore affected systems
- Perform post-incident analysis and improve defenses
Best Practices:
- Develop and drill incident response plans
- Establish incident response team
- Record all response steps and decisions
- Perform post-incident review and improvement
Recovery Measures
Steps:
- Restore affected systems from clean backups
- Reinstall operating system and applications
- Restore data (ensure not infected)
- Reset all account passwords
- Check network configuration and security settings
- Monitor system behavior after recovery
- Perform security assessment and hardening
Best Practices:
- Regularly test backup restoration processes
- Keep backups isolated and secure
- Record recovery process and results
- Perform post-recovery security assessment
Advanced Protection Techniques
Behavior Analysis
Techniques:
- Monitor process behavior and system calls
- Detect anomalous file operations (e.g., encrypting large numbers of files)
- Analyze network communication patterns
- Detect malicious code in memory
- Identify suspicious registry modifications
Tools:
- EDR solutions
- User and Entity Behavior Analytics (UEBA) systems
- Behavior analysis engines
Threat Intelligence
Applications:
- Use known Indicators of Compromise (IOC) to detect malicious activity
- Analyze attack patterns and TTPs (Tactics, Techniques, and Procedures)
- Share threat intelligence to improve overall defense
- Predict potential attacks and vulnerability exploitation
Sources:
- Commercial threat intelligence services
- Open-source threat intelligence platforms
- Industry Sharing and Analysis Centers (ISAC)
- Government and law enforcement shared information
Zero Trust Architecture
Principles:
- Never trust any user or device by default
- Continuously verify all access requests
- Implement least privilege access
- Monitor and record all access activities
- Protect all communications and data
Implementation:
- Multi-factor authentication
- Micro-segmented networks
- Continuous identity verification
- Encrypt all data
- Real-time monitoring and response
Malware Types and Basic Characteristics
Before diving into analysis techniques, we first need to understand the basic characteristics and behavior patterns of different malware types, which helps us identify and analyze specific malware types during static analysis.
Virus
A virus is malicious code capable of self-replication and infecting other programs or files. Its basic characteristics include:
- Parasitic: Virus needs to attach to other programs or files to exist and propagate
- Infectious: Can infect other programs or files through various mechanisms
- Destructive: May modify, delete, or destroy system files and data
- Latent: May hide itself to avoid detection
Typical Virus Behavior Analysis:
- Check if file headers are modified
- Analyze suspicious code snippets in programs (e.g., self-replication code)
- Detect modifications to critical system areas (e.g., boot sector, system files)
Trojan Horse
A Trojan is malware disguised as a legitimate program or file, characterized by:
- Deceptive: Masquerades as legitimate software to trick users into execution
- Covert: Usually does not self-replicate, hides its malicious functions
- Remote Control: May include remote access functionality
- Data Theft: May steal sensitive user information
Typical Trojan Behavior Analysis:
- Detect hidden network connections
- Analyze suspicious network communication code in programs
- Check if program includes remote control functions (e.g., command execution interfaces)
Ransomware
Ransomware is malware that encrypts user files and demands ransom, characterized by:
- Encryption Behavior: Uses strong encryption algorithms to encrypt user files
- Ransom Message: Displays ransom message demanding payment
- Time Limit: May set payment deadline, with ransom increased upon expiration
- Double Extortion: Not only encrypts files but also threatens to leak sensitive data
Typical Ransomware Behavior Analysis:
- Detect file encryption operations (e.g., encrypting large numbers of files)
- Analyze encryption algorithm implementation in program
- Check if program includes ransom message display functionality
Static Analysis Basics and Environment Setup
Static analysis is the technique of analyzing malware code and behavior without executing it. To perform effective static analysis, we need to set up an appropriate environment and prepare necessary tools.
Analysis Environment Setup
Recommended Environment Configuration:
- Operating System: Windows 10/11 or Linux (recommend using virtual machines)
- Virtual Machine Software: VMware Workstation or VirtualBox
- Snapshot Functionality: For quick restoration of analysis environment
- Network Isolation: Analysis environment should be isolated from production network
Security Measures:
- Use dedicated analysis virtual machines, not mixed with production systems
- Configure network isolation (e.g., internal network or no network)
- Regularly back up analysis environment
- Use snapshot functionality for easy environment restoration
- Record all analysis steps and findings
Common Static Analysis Tools
- IDA Pro: Powerful disassembly and reverse engineering tool
- Ghidra: NSA open-source decompiler
- PEiD: Detects executable file information and packing status
- UPX: Common packing/unpacking tool
- Strings: Extracts strings from executable files
- Dependency Walker: Analyzes DLL dependencies
- VirusTotal: Online malware scanning and analysis platform
Sample Acquisition and Preparation
Legitimate Sample Acquisition Methods:
- Malware analysis platforms (e.g., MalwareBazaar, VirusTotal)
- Samples released by security research institutions
- Public malware sample repositories
- Samples from legally captured network traffic
Sample Handling Procedures:
- Verify source and integrity when receiving samples
- Store samples on isolated storage devices
- Record sample metadata (e.g., source, acquisition time, hash values)
- Perform preliminary classification and labeling
- Perform analysis in virtual machines
Signature Extraction and YARA Rule Writing
Signature extraction is the foundational technique for malware detection, and YARA rules are a powerful tool for malware detection and classification based on signatures.
Signature Extraction Principles
A signature is a unique or highly specific code snippet or data pattern in malware used to identify specific malware or malware families. Signatures can be:
- Byte Sequences: Specific byte sequences (e.g., “MZ” indicates PE file header)
- Code Patterns: Specific instruction sequences or API call sequences
- Data Patterns: Specific strings or data structures
Signature Extraction Steps:
- Use disassembly tools to analyze malware
- Identify unique code snippets or data in malware
- Extract these snippets as signatures
- Verify signature uniqueness and specificity
YARA Rule Basics
YARA is an open-source tool for identifying and classifying malware, using text-based rules to describe malware characteristics.
YARA Rule Basic Structure:
rule ExampleRule {
meta:
description = "Example YARA rule"
author = "Your Name"
date = "2023-01-01"
strings:
$s1 = "malicious_string" nocase
$s2 = { 4D 5A } // "MZ" in hexadecimal
condition:
$s1 or $s2
}
Rule Components:
- meta: Rule metadata (description, author, date, etc.)
- strings: Strings or binary patterns used in rule
- condition: Matching condition (logical expression)
Practical YARA Rule Writing
Let’s learn how to write YARA rules through a practical example.
Goal: Detect malware containing the specific string “evil_code”
Steps:
- Use string extraction tools to extract all strings from malware
- Identify unique strings in malware
- Write YARA rule to match the string
Example Code:
# Use Python to extract strings from PE files
import pefile
def extract_strings(pe_file):
pe = pefile.PE(pe_file)
strings = []
for section in pe.sections:
data = section.get_data()
# Extract printable strings
for i in range(len(data)):
if data[i] == 0: # String terminator
continue
if 32 <= data[i] <= 126: # Printable ASCII characters
s = ""
j = i
while j < len(data) and 32 <= data[j] <= 126:
s += chr(data[j])
j += 1
if len(s) > 4: # Only extract strings longer than 4
strings.append(s)
i = j - 1
return strings
# Extract strings
strings = extract_strings("malware_sample.exe")
print("Extracted strings:", strings)
YARA Rule Based on Extracted Strings:
rule MalwareWithEvilCode {
meta:
description = "Detects malware containing 'evil_code' string"
author = "Your Name"
date = "2023-01-01"
strings:
$evil_string = "evil_code" nocase
condition:
$evil_string
}
Advanced YARA Rule Techniques
- Using Regular Expressions:
rule ComplexPattern {
strings:
$pattern = /malicious_[a-z0-9]{5}/
condition:
$pattern
}
- Combining Multiple Conditions:
rule MultiConditionRule {
strings:
$s1 = "string1"
$s2 = { 4D 5A }
condition:
$s1 and $s2
}
- Using Wildcards and Byte Patterns:
rule BytePatternRule {
strings:
$pattern = { 48 89 5C 24 ?? 48 89 74 24 ?? }
condition:
$pattern
}
- Using Conditional Logic:
rule ConditionalRule {
strings:
$s1 = "admin"
$s2 = "user"
condition:
filesize < 1MB and ($s1 or $s2)
}YARA Rule Testing and Optimization
After writing YARA rules, we need to test their accuracy and performance:
- Test Rule Accuracy:
- Test rules with known malware samples
- Ensure rules correctly identify target malware
- Ensure rules do not produce false positives
- Optimize Rule Performance:
- Avoid overly complex conditional logic
- Use efficient string matching patterns
- Limit rule scope (e.g., specific file types)
Python Example for Testing YARA Rules:
import yara
# Compile YARA rule
rules = yara.compile(source='''
rule TestRule {
strings:
$s1 = "test_string"
condition:
$s1
}
''')
# Scan file
matches = rules.match("test_file.exe")
print("Matches:", matches)
Packing Detection Techniques
Packing is a common malware protection technique that compresses or encrypts malware code, making it difficult to analyze directly. Detecting packing is an important step in static analysis.
Common Packing Tools
- UPX: Most commonly used open-source packing tool
- ASPack: Commercial packing tool
- PECompact: Commercial packing tool
- Themida: Advanced packing tool (often used to protect malware)
PEiD Packing Detection
PEiD is a popular packing detection tool that can identify various packers and compilers.
PEiD Detection Principles:
- Analyze PE file header and import table
- Identify packer-specific code patterns or signatures
- Match known packer characteristics
Using PEiD to Detect Packing:
- Download and install PEiD
- Open PEiD and load target PE file
- View detection results to identify packer type
Manual Packing Detection Techniques
In addition to tools, we can manually detect packing:
- Analyze Import Table:
- Packed programs usually have only a few imported functions (e.g., GetModuleHandle, GetProcAddress)
- Normal programs have more imported functions
- Analyze Code Section:
- Packed program code sections usually look like random data or compressed data
- Normal program code sections contain readable instructions
- Analyze Entropy:
- Packed programs usually have high entropy (more random data)
- Normal programs have lower entropy (more regular data)
Python Example for Calculating File Entropy:
import math
def calculate_entropy(data):
if not data:
return 0.0
entropy = 0.0
for x in range(256):
p_x = float(data.count(chr(x))) / len(data)
if p_x > 0:
entropy += - p_x * math.log(p_x, 2)
return entropy
# Read file data
with open("malware_sample.exe", "rb") as f:
data = f.read()
# Calculate entropy
entropy = calculate_entropy(data)
print("File entropy:", entropy)
# Determine if possibly packed (usually entropy > 7.0 may indicate packing)
if entropy > 7.0:
print("Possible packed file (high entropy)")
else:
print("Likely not packed (low entropy)")
Automated Packing Detection Tools
In addition to PEiD, many automated tools can detect packing:
- Detect It Easy (DIE): Powerful PE file analysis tool
- Exeinfo PE: Lightweight PE file information tool
- PEView: PE file structure viewer
Using Detect It Easy to Detect Packing:
- Download and install DIE
- Open DIE and load target PE file
- View information in “Header” and “Sections” tabs
- View detection results in “Packers” tab
Unpacking Techniques
Unpacking is a key step in static analysis of packed malware, allowing us to access and analyze the original code of packed programs.
Manual Unpacking Techniques
Manual unpacking requires deep understanding of packing algorithms and PE file structure, usually including the following steps:
- Analyze Packed Program:
- Identify packer type and packing algorithm
- Analyze packed program entry point and unpacking process
- Locate Unpacking Code:
- Find unpacking-related functions and code sections
- Identify API calls used during unpacking
- Extract Original Code:
- Capture original code during unpacking
- Save original code as new PE file
Manual Unpacking Example (UPX Packing):
- Load packed program into debugger (e.g., OllyDbg)
- Set breakpoints on unpacking-related API calls (e.g., VirtualAlloc, memcpy)
- Run program and pause when executing unpacking code
- Analyze unpacking process in memory
- After unpacking completes, dump original code from memory to file
Automated Unpacking Tools
For common packers, many automated unpacking tools are available:
- UPX: Can unpack UPX-packed programs
upx -d malware_sample.exe - PEiD: Some versions include unpacking functionality
- Exeinfo PE: Can identify packers and provide unpacking suggestions
- Unpacker Tools: Dedicated unpackers for specific packers
Example Using UPX to Unpack:
# Install UPX
sudo apt-get install upx
# Unpack packed program
upx -d malware_sample.exe
# Check unpacked file
file malware_sample.exe
Advanced Unpacking Techniques
For complex packers or custom packing, more advanced unpacking techniques may be needed:
- Dynamic Unpacking Analysis:
- Use debugger to track unpacking process
- Dump code from memory after unpacking completes
- Memory Dumping Techniques:
- Use tools (e.g., Cheat Engine) to dump unpacked memory
- Convert dumped memory to PE file
- Anti-Debugging and Anti-VM Detection Bypass:
- Identify anti-debugging and anti-VM techniques in packed programs
- Use appropriate tools to bypass these detections
Example Using OllyDbg for Dynamic Unpacking:
- Load packed program into OllyDbg
- Set hardware breakpoints on unpacking-related API calls
- Run program and pause when executing unpacking code
- Analyze registers and memory during unpacking
- After unpacking completes, use memory dump function to dump original code
- Save dumped memory as new PE file
Post-Unpacking Analysis
After unpacking, we obtain the original malware code and can perform deeper analysis:
- Use IDA Pro or Ghidra for Disassembly:
- Analyze program control flow and logic
- Identify malicious functions (e.g., encryption, network communication)
- Extract Signatures:
- Extract signatures from unpacked code
- Write YARA rules for detection
- Behavior Analysis:
- Analyze program API calls and system interactions
- Identify malicious behavior patterns
Comprehensive Analysis Case
Let’s practice the static analysis techniques learned through a comprehensive case.
Case: Analyzing a Packed Ransomware Sample
Goal: Analyze a packed ransomware sample, extract its signatures, and write YARA rules
Steps:
- Sample Acquisition and Preparation:
- Acquire packed ransomware sample from legitimate sources
- Perform analysis in isolated virtual machine environment
- Preliminary Analysis:
- Use PEiD to detect packer type
- Use Detect It Easy to view file information
- Packing Detection:
- Analyze import table (usually only a few API calls)
- Calculate file entropy (determine if possibly packed)
- Unpacking Process:
- Load packed program into OllyDbg
- Set breakpoints on unpacking-related API calls
- Run program and capture unpacking process
- Dump code from memory after unpacking completes
- Post-Unpacking Analysis:
- Use IDA Pro to disassemble unpacked code
- Analyze program control flow and functions
- Identify ransomware encryption and ransom functions
- Signature Extraction:
- Extract unique strings and code snippets from unpacked code
- Identify ransomware-specific behavior patterns
- Write YARA Rules:
- Write YARA rules based on extracted signatures
- Test rule accuracy
- Optimize rule performance
Example Analysis Results:
- Detected sample uses UPX packing
- After unpacking, identified ransomware uses AES encryption algorithm
- Extracted signatures include specific encryption function calls and ransom strings
- Written YARA rules can accurately detect this ransomware family
Dynamic Malware Analysis
- Sandbox analysis (Cuckoo, Any.Run)
- Behavior pattern recognition (C2 communication, persistence)
- Debugging malicious code techniques
Advanced Malware Techniques
- Rootkit analysis (user-mode and kernel-mode)
- Virtual machine detection and anti-sandbox techniques
- Self-destruct mechanisms and encryption analysis
Malware Dynamic Analysis Basics and Environment Setup
Dynamic analysis is the technique of executing malware in a controlled environment and observing its behavior. To perform effective dynamic analysis, we need to set up an appropriate environment and prepare necessary tools.
Analysis Environment Setup
Recommended Environment Configuration:
- Operating System: Windows 10/11 or Linux (recommend using virtual machines)
- Virtual Machine Software: VMware Workstation or VirtualBox
- Snapshot Functionality: For quick restoration of analysis environment
- Network Isolation: Analysis environment should be isolated from production network
- Hardware Virtualization Support: Enable VT-x/AMD-V to support advanced analysis tools
Security Measures:
- Use dedicated analysis virtual machines, not mixed with production systems
- Configure network isolation (e.g., internal network or no network)
- Regularly back up analysis environment
- Use snapshot functionality for easy environment restoration
- Record all analysis steps and findings
- Consider using physically isolated hardware for high-risk analysis
Common Dynamic Analysis Tools
- Cuckoo Sandbox: Open-source automated malware analysis system
- Any.Run: Cloud-based interactive malware analysis platform
- Process Monitor: Monitors system activity (files, registry, processes, etc.)
- Process Explorer: Views process information (including threads, modules, etc.)
- Wireshark: Network traffic analysis
- API Monitor: Monitors API calls
- RegShot: Registry change monitoring
- Sysinternals Suite: Includes various system monitoring tools
Sandbox Analysis Techniques
Sandbox analysis is the core technique of dynamic analysis, allowing us to execute malware in an isolated environment and observe its behavior.
Cuckoo Sandbox Analysis
Cuckoo is an open-source automated malware analysis system that can execute malware and collect detailed behavior information.
Cuckoo Analysis Process:
- Installation and Configuration:
- Install Cuckoo and its dependencies
- Configure virtual machines as analysis environment
- Set up network isolation and snapshot functionality
- Submit Sample for Analysis:
- Submit malware sample to Cuckoo
- Configure analysis options (e.g., analysis duration, monitored APIs)
- Analysis Report Generation:
- Cuckoo executes sample and collects behavior data
- Generates detailed analysis report, including:
- File operations (create, modify, delete)
- Registry operations
- Network communication
- Process creation and injection
- API calls
Cuckoo Report Example Analysis:
# Python example for parsing Cuckoo report
import json
def analyze_cuckoo_report(report_path):
with open(report_path, 'r') as f:
report = json.load(f)
# Extract basic information
info = report.get('info', {})
print(f"Sample MD5: {info.get('target', {}).get('md5')}")
print(f"Sample SHA256: {info.get('target', {}).get('sha256')}")
# Extract behavior summary
behavior = report.get('behavior', {})
if behavior:
print("\nBehavior Summary:")
for process in behavior.get('processes', []):
print(f"Process: {process.get('process_name')}")
for api_call in process.get('calls', []):
print(f" API: {api_call.get('api')}")
print(f" Arguments: {api_call.get('arguments')}")
print(f" Return: {api_call.get('return')}")
# Analyze report
analyze_cuckoo_report("cuckoo_report.json")
Cuckoo Advanced Configuration:
- Custom Monitoring:
- Add custom monitoring modules (e.g., monitor specific API calls)
- Configure finer-grained behavior collection
- Distributed Analysis:
- Set up multiple analysis nodes to improve analysis capacity
- Configure load balancing
- Threat Intelligence Integration:
- Integrate Cuckoo with threat intelligence platforms
- Automatically tag known malware
Any.Run Interactive Analysis
Any.Run is a cloud-based interactive malware analysis platform that provides a more intuitive analysis experience.
Any.Run Analysis Features:
- Real-Time Interaction: Can observe malware behavior in real time
- Visual Interface: Provides intuitive behavior visualization
- Network Simulation: Can simulate different network environments
- Integrated Tools: Built-in various analysis tools
Any.Run Analysis Process:
- Upload Sample: Upload malware to Any.Run platform
- Configure Analysis Environment: Select operating system and configuration
- Execute Analysis: Start sample execution
- Observe Behavior: Observe malware behavior in real time
- Generate Report: Generate detailed report after analysis
Any.Run Interaction Techniques:
- Set Breakpoints: Set breakpoints at specific API calls
- Modify Environment: Dynamically modify system environment to observe malware reaction
- Network Traffic Analysis: Analyze network communication in real time
- Process Tree Analysis: View process tree created by malware
Behavior Pattern Recognition Techniques
Understanding malware behavior patterns is key to detection and defense. We need to identify common malicious behaviors, such as C2 communication, persistence mechanisms, etc.
C2 Communication Analysis
Command and Control (C2) communication is the key mechanism for malware to communicate with attackers.
Common C2 Communication Characteristics:
- Domain Generation Algorithm (DGA): Generates random domains for communication
- Hardcoded IP/Domain: Directly connects to known IP or domain
- Encrypted Communication: Uses SSL/TLS or other encryption protocols
- Unconventional Ports: Uses non-standard ports for communication
- Abnormal Domain Resolution: Frequently resolves different domains
C2 Communication Detection Techniques:
- Network Traffic Analysis:
- Use Wireshark to capture and analyze network traffic
- Identify suspicious DNS requests and connections
- API Call Monitoring:
- Monitor network-related API calls (e.g., socket, connect, send)
- Analyze connection target addresses and ports
- Domain Analysis:
- Analyze hardcoded domains in malware
- Detect DGA-generated domain patterns
C2 Communication Analysis Example:
# Python example using Scapy to analyze network traffic
from scapy.all import *
def analyze_network_traffic(pcap_file):
packets = rdpcap(pcap_file)
suspicious_connections = []
for packet in packets:
if packet.haslayer(IP) and packet.haslayer(TCP):
src_ip = packet[IP].src
dst_ip = packet[IP].dst
dst_port = packet[TCP].dport
# Detect unconventional port connections
if dst_port not in [80, 443, 22, 21]: # Common port list
suspicious_connections.append((src_ip, dst_ip, dst_port))
# Detect SSL/TLS connections (via SNI or certificate)
if packet.haslayer(TLS):
print(f"TLS connection detected: {src_ip}:{packet[TCP].sport} -> {dst_ip}:{dst_port}")
return suspicious_connections
# Analyze network traffic
suspicious = analyze_network_traffic("malware_traffic.pcap")
print("Suspicious connections:", suspicious)
Persistence Mechanism Analysis
Persistence is the mechanism for malware to remain in the system long-term.
Common Persistence Techniques:
- Registry Keys:
- HKCU\Software\Microsoft\Windows\CurrentVersion\Run
- HKLM\Software\Microsoft\Windows\CurrentVersion\Run
- Scheduled Tasks:
- Creates scheduled tasks to execute on system startup
- Service Creation:
- Installs malicious services
- Startup Folder:
- Places shortcuts in startup folder
- Browser Extensions:
- Installs malicious browser extensions
Persistence Detection Techniques:
- Registry Monitoring:
- Use RegShot or similar tools to monitor registry changes
- Analyze malware modifications to registry
- File System Monitoring:
- Monitor startup folder and scheduled task directories
- Detect newly created startup items
- Process Monitoring:
- Monitor processes executed on system startup
- Identify suspicious startup processes
Persistence Analysis Example:
# Python example for monitoring registry changes
import winreg
import time
def monitor_registry_changes(key_path, value_name):
# Open registry key
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path, 0, winreg.KEY_READ | winreg.KEY_NOTIFY)
# Get initial value
try:
initial_value, _ = winreg.QueryValueEx(key, value_name)
except FileNotFoundError:
initial_value = None
print(f"Monitoring {key_path}\\{value_name} for changes...")
while True:
# Wait for registry change notification
winreg.NotifyChangeKeyValue(key, True, winreg.REG_NOTIFY_CHANGE_LAST_SET)
# Get current value
try:
current_value, _ = winreg.QueryValueEx(key, value_name)
except FileNotFoundError:
current_value = None
# Detect changes
if current_value != initial_value:
print(f"Change detected in {key_path}\\{value_name}:")
print(f" Previous value: {initial_value}")
print(f" New value: {current_value}")
initial_value = current_value
time.sleep(1)
# Monitor Run key changes
monitor_registry_changes(r"Software\Microsoft\Windows\CurrentVersion\Run", "MalwareEntry")
Debugging Malicious Code Techniques
Debugging is an important technique for deeply understanding malware behavior, allowing us to execute code step by step and observe its behavior.
Debugging Tool Selection
- OllyDbg: Classic Windows debugger
- x64dbg: Modern Windows debugger, supports 32-bit and 64-bit
- WinDbg: Microsoft’s powerful debugger, supports kernel debugging
- GDB: Powerful debugger under Linux
- IDA Pro: Disassembly tool with debugging functionality
Debugging Technique Basics
Basic Debugging Steps:
- Load Sample: Load malware into debugger
- Set Breakpoints: Set breakpoints at key locations
- Step Execution: Use single-step execution commands to observe code behavior
- Observe Registers and Memory: Check register values and memory contents
- Analyze API Calls: Monitor malware API calls
x64dbg Debugging Example:
- Open x64dbg and load malware
- Set breakpoint at entry point (usually program starting address)
- Run program and pause when reaching breakpoint
- Use “Follow in Dump” function to view code in memory
- Single-step execution (F7 step into, F8 step over)
- Observe register changes and memory modifications
Advanced Debugging Techniques
- Dynamic Code Analysis:
- Analyze generated code during execution (e.g., decrypted code)
- Use memory dump function to obtain code after execution
- Anti-Debugging Technique Bypass:
- Identify common anti-debugging techniques (e.g., IsDebuggerPresent, NtQueryInformationProcess)
- Use debugger plugins or manually modify code to bypass detection
- Conditional Breakpoints:
- Set breakpoints based on specific conditions (e.g., register values, memory contents)
- Use expressions to set complex conditions
- Memory Breakpoints:
- Set memory access breakpoints to monitor access to specific memory regions
- Used to analyze data decryption or code injection
Example Bypassing IsDebuggerPresent:
; Original code (checks if being debugged)
call IsDebuggerPresent
test eax, eax
jnz exit ; Exit if being debugged
; Bypass method: Directly modify return value
; After calling IsDebuggerPresent, set eax to 0
mov eax, 0
Advanced Malware Technique Analysis
Rootkit Analysis
A Rootkit is malware that hides its own existence and that of other programs, and can run in user mode or kernel mode.
User-Mode Rootkit Analysis:
- API Hooking Detection:
- Detect common API hooking techniques (e.g., IAT Hook, Inline Hook)
- Use tools (e.g., API Monitor) to monitor API calls
- DLL Injection Detection:
- Detect malicious DLL injection into legitimate processes
- Use Process Explorer to view DLLs loaded by processes
- File Hiding Detection:
- Detect hidden files and directories
- Use Rootkit detection tools (e.g., GMER)
Kernel-Mode Rootkit Analysis:
- SSDT Hook Detection:
- Detect hooks in System Service Description Table (SSDT)
- Compare current SSDT with normal system SSDT
- IDT Hook Detection:
- Detect hooks in Interrupt Description Table (IDT)
- Analyze modifications to interrupt handlers
- Kernel Object Hiding:
- Detect hidden kernel objects (e.g., processes, threads, files)
- Use kernel debugger (e.g., WinDbg) for analysis
Rootkit Detection Tools:
- GMER: Comprehensive Rootkit detection tool
- TDSSKiller: Specifically detects TDSS Rootkit
- RootkitRevealer: Detects hidden items in files and registry
- IceSword: Rootkit detection tool under Windows
Virtual Machine Detection and Anti-Sandbox Techniques
Malware often uses virtual machine detection and anti-sandbox techniques to avoid analysis.
Common Virtual Machine Detection Techniques:
- Hardware Feature Detection:
- Detect specific hardware features (e.g., VMware-specific instructions)
- Check system information (e.g., BIOS version, hardware ID)
- Behavior Detection:
- Detect virtual machine-specific behaviors (e.g., high CPU usage, specific interrupts)
- Check system call patterns
- Environment Variable Detection:
- Check specific environment variables (e.g., VMware-specific variables)
Common Anti-Sandbox Techniques:
- Time Detection:
- Detect if execution time is too short (sandboxes usually execute quickly)
- Use high-precision timers to measure execution time
- User Interaction Detection:
- Detect lack of user interaction (e.g., mouse movement, keyboard input)
- Simulate user behavior to bypass detection
- Resource Detection:
- Detect if system resources are limited (e.g., memory, CPU limits)
- Attempt to allocate large resources to detect limits
Virtual Machine Detection Bypass Techniques:
- Modify Hardware Features:
- Modify returned hardware information
- Use hooks to intercept hardware detection calls
- Simulate Normal Behavior:
- Simulate normal system call patterns
- Add delays to simulate real user operations
- Detect Sandbox-Specific Behavior:
- Detect sandbox-specific API calls or behaviors
- Change behavior based on detection results
Self-Destruct Mechanisms and Encryption Analysis
Malware often uses self-destruct mechanisms and encryption techniques to hide its code and data.
Self-Destruct Mechanism Analysis:
- File Deletion:
- Malware deletes itself after completing operations
- Analyze trigger conditions for deletion
- Code Overwrite:
- Malware overwrites its code region
- Use memory dump techniques to obtain code before overwrite
- Process Termination:
- Malware terminates its own process
- Analyze termination conditions and methods
Encryption Technique Analysis:
- Static Encryption Analysis:
- Analyze encryption algorithm implementation
- Identify used encryption libraries or custom implementations
- Dynamic Encryption Analysis:
- Capture encryption operations during execution
- Analyze encryption input and output
- Key Extraction:
- Analyze key generation and storage
- Attempt to extract encryption keys
Encryption Analysis Example:
# Python example for analyzing simple encryption algorithms
def analyze_encryption_sample(encrypted_data):
# Assume simple XOR encryption
# Try common key lengths
for key_length in range(1, 17):
# Try to extract possible keys
possible_key = []
for i in range(key_length):
# Collect bytes at corresponding positions
bytes_at_pos = [encrypted_data[j] for j in range(i, len(encrypted_data), key_length)]
# Simple frequency analysis (assume plaintext is English)
# This is just an example, actual analysis needs more complexity
possible_key.append(max(set(bytes_at_pos), key=bytes_at_pos.count))
# Try decryption with possible key
decrypted = bytes([encrypted_data[i] ^ possible_key[i % key_length]
for i in range(len(encrypted_data))])
# Check if decryption result contains printable characters
printable_chars = sum(1 for b in decrypted if 32 <= b <= 126)
if printable_chars > len(decrypted) * 0.5: # More than 50% printable characters
print(f"Possible key length: {key_length}")
print(f"Possible key: {bytes(possible_key)}")
print(f"Decrypted sample: {decrypted[:50]}...") # Print first 50 bytes
# Analyze encrypted data
with open("encrypted_sample.bin", "rb") as f:
encrypted_data = f.read()
analyze_encryption_sample(encrypted_data)
Comprehensive Analysis Case
Let’s practice the dynamic analysis techniques learned through a comprehensive case.
Case: Analyzing Malware Using Rootkit and Virtual Machine Detection
Goal: Analyze a malware sample using Rootkit techniques and virtual machine detection, identify its behavior, and bypass defense mechanisms
Steps:
- Sample Acquisition and Preparation:
- Acquire malware sample from legitimate sources
- Perform analysis in isolated virtual machine environment
- Preliminary Analysis:
- Use PEiD to detect packer type
- Use Detect It Easy to view file information
- Sandbox Analysis:
- Submit sample to Cuckoo Sandbox
- Analyze behavior in report (especially Rootkit-related behavior and virtual machine detection)
- Behavior Pattern Recognition:
- Identify C2 communication patterns
- Analyze persistence mechanisms
- Detect virtual machine detection behavior
- Debugging Analysis:
- Load sample into x64dbg
- Set breakpoints at virtual machine detection code
- Analyze detection logic and bypass methods
- Rootkit Analysis:
- Use GMER to detect Rootkit behavior
- Use WinDbg to analyze kernel-mode Rootkit (if present)
- Identify hidden processes and files
- Encryption Analysis:
- Analyze encryption in network communication
- Attempt to extract encryption keys
- Comprehensive Report:
- Summarize malware behavior and functions
- Provide detection and defense suggestions
Case Analysis Results:
- Detected sample uses VMware-specific API calls for virtual machine detection
- Identified Rootkit techniques used to hide malicious processes
- Discovered use of custom encryption algorithm for network communication
- Provided methods to bypass virtual machine detection and Rootkit detection suggestions



