Debugging
Debugging, an interactive approach to malware analysis, enhances understanding of code behavior by enabling real-time examination. By uniting static analysis insights from tools like IDA with debugging techniques, analysts gain a holistic view of malware functionality, sandbox evasion mechanisms, and Indicators of Compromise (IOCs).
Tools for Debugging
x64dbg - A debugger for analyzing and controlling 64-bit executables, complete with:
Disassembly View: Shows the program’s assembly code.
Registers and Stack View: Reveals current CPU register values and stack frame.
Memory Dump: Visualizes program memory for analyzing data structures and variables.
INetSim - Simulates internet services in a controlled environment, enabling malware to interact with fake DNS, HTTP, and other services safely.
Setting Up Debugging in x64dbg
Loading shell.exe in x64dbg
Launch x64dbg and select File > Open.
Navigate to and open shell.exe.
The program halts at its entry point in the disassembly view, with the default breakpoint set.
To begin, press F9 or click Run.
Simulating Internet Services with INetSim
INetSim configures fake internet services, capturing and responding to network requests from the malware sample.
Configuring INetSim
Edit Configuration:
sudo nano /etc/inetsim/inetsim.confSet
service_bind_addressanddns_default_ipto the machine’s IP.Configure DNS defaults:
dns_default_hostname www dns_default_domainname iuqerfsodp9ifjaposdfjhgosurijfaewrwergwea.com
Start INetSim:
sudo inetsimEnsure the target’s DNS is pointed to the INetSim-running machine.
Bypassing Sandbox Checks
Malware frequently checks for virtual or sandbox environments before execution. Here, we patch these checks in x64dbg.
Step-by-Step Sandbox Bypass
Copy Address from IDA:
In IDA, identify the address of the cmp instruction for registry checks.
Use Go to > Expression in x64dbg (Ctrl+G) to locate this address in x64dbg.
Identify and Patch Comparison Instruction:
Find the
cmpinstruction related toSandbox detected(e.g., address0x4032C8).Modify
cmp [rsp+148h+Type], 1tocmp [rsp+148h+Type], 0using Spacebar to edit.
Patch Sandbox Strings in String References:
Search for > Current Module > String references to find
Sandbox detected.Set breakpoints on strings like
0x4032F13, and change conditional jumps (e.g.,jetojne).
Patching and Saving the Bypassed Executable
After successful patching:
Save the Patched Executable:
Press Ctrl+P in x64dbg and select Patch File.
The saved file will bypass sandbox checks in future executions, allowing all behaviors to manifest.
Network Traffic Analysis
Capturing Malware Traffic with Wireshark
Start Wireshark to capture all network traffic generated by the malware.
Analyze:
DNS Requests: Observing connections to domains like
ms-windows-update[.]com.HTTP Requests: Malware appends the computer hostname to the
User-Agent.HTTP Response: INetSim’s response, like the default binary, will trigger messages in malware.
Process Injection Analysis
Process injection is a common technique where malware injects code into another process (e.g., notepad.exe).
Setting Breakpoints for Injection Functions
In x64dbg:
Search and set breakpoints on
VirtualAllocEx,WriteProcessMemory, andCreateRemoteThread.
Attach to notepad.exe:
Open another x64dbg instance, Attach to Process (Alt+A), and select notepad.exe.
Monitor injected code in notepad.exe’s memory using memory dumps.
Verify Shellcode Injection:
Examine WriteProcessMemory’s
lpBaseAddressparameter to identify injection address.Copy and paste this address into notepad.exe's memory dump view.
Inspect Injected Shellcode:
Run shell.exe, observe the populated memory, and save the shellcode for analysis.
Last updated