Practical Malware Analysis Notes (4)
LAB07-01.exe
Static analysis
I started in IDA by opening the imports table. It gives a useful first impression of what the program can do before tracing its logic.

CreateServiceA, StartServiceCtrlDispatcherA, OpenSCManagerA, and other imports relate to system services, indicating system operations. InternetOpenUrlA and InternetOpenA open a URL; CreateMutexA and OpenMutexA create and open mutexes.
With those imports in mind, I opened the program’s main function in IDA:

main() creates a service list named MalService, whose main routine is sub_401040(), and runs it through StartServiceCtrlDispatcherA. Follow the service routine.

The service routine first opens the mutex HGL345. If that succeeds it creates the mutex; otherwise it exits. This ensures only one instance is running.
Next, the routine opens the service manager and obtains the current process and its filename and path, Filename. It uses that path when creating its service. The service-creation arguments are:
| Parameter | Value | Meaning |
|---|---|---|
hSCManager |
SCManagerHandle |
Handle to the service control manager |
IpServiceName |
DisplayName= Malservice |
Name under which the service is installed |
IpDisplayName |
DisplayName = Malservice |
Name displayed in the user interface |
dwDesiredAccess |
2u |
Process access token |
dwServiceType |
0x10u |
Service type: a service in its own process |
dwStartType |
2u |
Start option: automatic at boot |
dwErrorContorl |
0 |
Action on startup failure: ignore the error and continue |
IpBinaryPathName |
&Filename |
Path to the service binary |
The picture is becoming clearer: the program registers itself as a service that starts at boot, using a binary in the current directory.
Continuing through the routine shows a scheduled time of 2100/0/0 00:00:00. The program checks the system clock and, when that time arrives, creates 20 threads, each starting at StartAddress. I followed that function:

It opens Internet using Internet Explorer 8.0, then opens http://www.malwareanalysisbook.com. The string references:

After those calls, the program does not exit: it stays resident as a service, repeatedly calling Sleep.
1. How does the program ensure it continues running after a reboot (persistence)?
It adds itself to automatic startup.
2. Why does it use a mutex?
To ensure only one instance runs at a time.
3. What host-based indicators can detect it?
The mutex HGL345.
The service MalService.
4. What network-based indicators can detect it?
Opening Internet Explorer 8.0 and http://www.malwareanalysisbook.com.
5. What is its purpose?
At a specified time it starts 20 threads to visit the site simultaneously. The likely purpose is to deploy it on compromised machines and launch a DDOS attack at a set time.
6. When does it finish executing?
It starts at 2100/01/01 00:00:00 and remains on the computer afterward.
LAB07-02.exe
Static analysis with IDA
I began by inspecting the imports:

There are few imports. OleInitialize initializes an ole object, CoCreateInstance creates a Com instance, and OleUninitialize deletes an ole object. SysAllocString and SysFreeString manipulate strings.
Next, I looked at the strings:


A URL appears in the list, suggesting that the sample might open it.
I opened the main function to check:

It is short, although the object-oriented operations are a little hard to follow. It appears to create an instance; if successful, it calls the function pointed to at ppv+44. v3 is an allocated string. Follow it.

The allocated string turns out to be the URL. After renaming it, I inspected the OLE function and its CoCreateInstance arguments:
| Parameter | Value | Function |
|---|---|---|
rclsid |
0002DF01-0000-0000-C000-000000000046 |
Identifies an object |
pUnkOuter |
0 |
Indicates whether the object is non-aggregated |
dwClsContext |
4 |
Component class |
riid |
D30C1661-CDAF-11D0-8A3E-00C04FC9E26E |
Interface identifier for object communication |
ppv |
&ppv |
Receives the interface pointer |
The two IDs look like uuid values, so I looked up what they identify:


The IDs suggest that the program opens a browser to visit the website.
After applying the standard structures in IDA, the operation is easier to read:

The code navigates the browser to the site, then releases its variables, closes the object, and returns from main.
1. How does it achieve persistence?
I did not find a persistence mechanism: the sample exits after opening the site.
2. What is its purpose?
To open a website.
3. When does it finish executing?
Once the site opens.
LAB7-3
Static analysis of LAB07-03.exe
I began with the executable’s imports:

CreateFileA, CreateFileMappingA, FindNextFileA, FindFirstFileA, and CopyFileA find, create, and copy files.
Next I checked its strings:

Two paths appear: C:\\windows\\system32\\kernel32.dll and C:\\windows\\system32\\kerne132.dll.
The names are deliberately similar. My initial guess was that the sample replaces the system kernel32.dll with kerne132.dll, which comes from Lab07-03.dll.
I opened main to trace what happens:

The main function is long, so I worked through it one piece at a time.
It first checks the argument: if it contains WARNING_THIS_WILL_DESTROY_YOUR_MACHINE, it continues; otherwise it exits. The rest is strange, so inspect the accompanying DLL.
I began by looking at the DLL’s imports:

There are many socket communication functions, including send, connect, socket, WSAStartup, and recv, indicating network capability. Next I checked its strings:

There is an IP address and key strings such as exec, sleep, and hello. Inspect the main routine and references to these strings.

Reverse engineering shows the DLL’s DLLMain connects to the remote socket 127.26.152.13:80, attempts to send hello, and receives messages. On sleep, it calls sleep(0x60000); on exec, it opens a command line; on q, it exits.
Having examined the DLL, I returned to the executable.

After several functions whose purpose was not yet clear, I reached closeHandle:

It copies the dll to C:\windows\system32\kerne132.dll. Follow a function called by main that uses C:\*.

The function enumerates files on drive C and calls sub_4010A0() for each .exe it finds. I followed that call:

It reads the file and uses IsBadReadPtr() to check whether the process can access the specified memory. If successful, it performs a series of operations, extracting a string and checking whether it is kernel32.dll; if so, it overwrites it with the fake kerne132.dll in memory.
The purpose is to find programs importing this DLL and change their imports to kernel132.dll, interfering with normal programs and opening a backdoor.
With the static analysis complete, I moved on to observe the program in a debugger.
Dynamic analysis of Lab7-3.exe
The static analysis suggests the program infects files under the root directory, preventing normal calls to kernel32.dll. Compile a hello world program and run the sample, then compare the results.

This is a simple 32-bit program built on Windows 10 with gcc -m32 helloworld.cpp -o hello.exe. It imports KERNEL32.dll; infect the file with the sample.
To follow it dynamically, use IDA’s Local Win32 debugger.


With the debugger configured, I began stepping through the code:

The files are read one by one. Set a breakpoint to inspect each file; the first one found is:

That is not the file we want. Continue, but it takes too long, so let it finish. Later we will patch it to enumerate drive C.
After the run finished, I inspected the test executable:

Its import of kernel32 has changed to kerne132, confirming the hypothesis.

procmon confirms this in the process information.
I then inspected the location the sample had modified:

The new kerne132.dll includes the original kernel32.dll plus network code for remote control. The original program continues working while also running the malicious code, a sophisticated infection method.
1. How does the program persist across reboots?
It infects files on drive C so they call the infected kerne132.dll whenever they run, while hiding itself under C:\windows\system32.
2. What are two obvious host-based indicators?
An extra kerne132.dll in C:\windows\system32, and kernel32.dll replaced by kerne132.dll in the import tables of programs on drive C.
It also creates a mutex, SADFHUHF.

3. What is its purpose?
It is a backdoor for remote control.
4. How can it be removed once installed?
Remove the earlier kernel132.dll and restore the imports of the EXEs on drive C to kernel132.dll.
Alternatively, replace kerne132.dll with the contents of kernel32.dll, though this only works on the local machine.


