
Chapter 4 : Unpacking, Decryption And Deobfuscation Part 1
This chapter focuses on techniques used by malware authors to evade detection and make reverse engineering more difficult. It covers how to identify, unpack, and decrypt malware using various methods.
Introduction to Packers
What is a Packer?

Why Use Packers?
Types of Packers
Common Packing and Encryption Tools
Identifying a Packed Sample
Technique 1: Using Static Signatures

Technique 2: Evaluating PE Section Names

Technique 3: Using Stub Execution Signs
Technique 4: Detecting a Small Import Table

Additional Indicators of Packed Samples
Automated Unpacking Techniques
General Concepts
Technique 1: Official Unpacking Process

Technique 2: OllyScript with OllyDbg
Technique 3: Generic Unpackers


Technique 4: Emulation
Technique 5: Memory Dumps
Limitations of Automated Unpacking
Additional Notes
Manual Unpacking Techniques
Introduction to Manual Unpacking in Malware Analysis
Why Manual Unpacking?
Key Concepts from the Book
Tools Recommended in Mastering Malware Analysis
Manual Unpacking Techniques In Our Chapter
Technique 1 – Memory Breakpoint on Execution
Overview
Understanding Memory Breakpoints
Why Use Memory Breakpoints?
Key Windows APIs to Monitor
Step-by-Step Implementation
1. Load the Packed Sample into a Debugger
2. Set Breakpoints on Memory Allocation APIs

3. Execute and Observe Memory Allocation Calls

4. Locate and Extract the Unpacked Payload
5. Rebuild the Unpacked Executable
Common Challenges and Solutions
1. The Sample Does Not Hit the Breakpoint
2. The Dumped File Is Corrupt
3. Anti-Debugging Techniques Prevent Analysis
Key Takeaways
Technique 2 – Call Stack Backtracing
Overview
Understanding Call Stack Backtracing
Key Indicators of Return-Oriented Execution
Why Use Call Stack Backtracing?

Step-by-Step Implementation
1. Load the Packed Sample into a Debugger
2. Set a Breakpoint on Common Transition Points

3. Execute the Sample and Analyze the Call Stack


4. Identify the Original Entry Point (OEP)

5. Dump the Unpacked Executable
Common Challenges and Solutions
1. The Call Stack Appears Obfuscated
2. Execution Jumps to a Random Memory Address
3. The Dumped File Fails to Execute
Key Takeaways
Technique 3 – Monitoring Memory Allocated Spaces for Unpacked Code
Overview
Step-by-Step Implementation
1. Load the Packed Sample into a Debugger
2. Set Breakpoints on Memory Allocation APIs

3. Execute the Sample and Capture Memory Allocation Events
4. Locate and Extract the Unpacked Payload
5. Rebuild the Unpacked Executable
Common Challenges and Solutions
1. Malware Uses Anti-Debugging Techniques
2. Memory Dumps Contain Corrupted Code
3. Execution Flow is Obfuscated
Key Takeaways
Technique 4 – In-Place Unpacking
Overview
Understanding In-Place Unpacking
Why Use In-Place Unpacking?
Key Indicators of In-Place Unpacking
Step-by-Step Implementation
1. Load the Packed Sample into a Debugger
2. Identify Writable Code Sections
3. Set Breakpoints on Memory Protection Changes
4. Monitor Self-Modifying Code
5. Extract and Rebuild the Unpacked Code
Common Challenges and Solutions
1. Packed Code Overwrites Itself Too Quickly
2. Malware Uses Anti-Debugging Techniques
3. The Dumped Code Does Not Execute Properly
Key Takeaways
Technique 5 – Searching for and Transferring Control to OEP
Overview
Understanding the OEP Transition
Why Locate the OEP?
Key Indicators of an OEP Transition
Step-by-Step Implementation
1. Load the Packed Sample into a Debugger
2. Identify Execution Flow Anomalies
3. Set Breakpoints on Transition Instructions

4. Locate the OEP and Dump the Unpacked Executable
5. Redirect Execution to the OEP
Common Challenges and Solutions
1. The Malware Uses Multiple Layers of Packing
2. Execution Jumps to an Unexpected Location
3. Dumped File Fails to Execute
Key Takeaways
Technique 6 – Stack Restoration-Based Unpacking
Overview
Understanding Stack Restoration in Unpacking
Why Use Stack Restoration?
Key Indicators of Stack-Based Execution Manipulation
Step-by-Step Implementation
1. Load the Packed Sample into a Debugger
2. Set Breakpoints on Stack-Related Instructions
3. Analyze Stack Modifications
4. Identify the OEP and Extract the Unpacked Code
5. Rebuild Execution Flow and Redirect Control to OEP
Common Challenges and Solutions
1. Stack Obfuscation Techniques
2. Unexpected Execution Jumps
3. Dumped File Does Not Run Properly
Key Takeaways
Dumping the unpacked sample and fixing the import table
Overview
Dumping the Unpacked Sample
Why Dump the Unpacked Sample?
Tools Used for Dumping the Unpacked Sample

The OllyDump UI 
The Region Dump window of PETools
Step-by-Step Process to Dump the Sample
Fixing the Import Table
Why Fix the Import Table?
Step-by-Step Process to Rebuild the Import Table

The ImpREC interface 
The import table before and after PE loading
Additional Considerations and Challenges
1. API Addresses Need to Be Restored
2. Memory Dump May Not Contain a Clean PE Structure
3. Some Malware Injects the Unpacked Code Elsewhere
4. Dependency on the Original Packer
Key Takeaways
Last updated