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.

image-20191223220040221.png

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:

image-20191223220603411.png

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

image-20191223221008103.png

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:

image-20191223223257410.png

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

image-20191223223350956.png

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:

image-20191224130550304.png

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:

image-20191224130757083.png

image-20191224130907842.png

A URL appears in the list, suggesting that the sample might open it.

I opened the main function to check:

image-20191224131042462.png

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.

image-20191224131202003.png

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:

image-20191224134213628.png

image-20191224134245470.png

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:

image-20191224134700424.png

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:

image-20191224141845795.png

CreateFileA, CreateFileMappingA, FindNextFileA, FindFirstFileA, and CopyFileA find, create, and copy files.

Next I checked its strings:

image-20191224162158101.png

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:

image-20191224163753430.png

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:

image-20191224171253226.png

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

image-20191224171359651.png

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

image-20191224172400232.png

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.

image-20191224210044783.png

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

image-20191224211248722.png

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

image-20191224211542279.png

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

image-20191224212332825.png

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.

image-20191224213644141.png

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.

image-20191224214706559.png

image-20191224214650312.png

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

image-20191224215335016.png

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

image-20191224215606090.png

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:

image-20191224220400836.png

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

image-20191224220323285.png

procmon confirms this in the process information.

I then inspected the location the sample had modified:

image-20191224220743147.png

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.

image-20191224221550703.png

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.