Practical Malware Analysis (3)

1: What is the address of DllMain?

image-20191213173917324.png

2: In the Imports window, navigate to gethostbyname. What address does the imported function resolve to?

image-20191213174017362.png

3: How many functions call gethostbyname?

image-20191213174044141.png

4: Focus on the call to gethostbyname at 0x10001757. Can you determine which DNS request will be triggered?

image-20191213180757726.png

At the DNS call, a string is pushed onto the stack beforehand. Following that reference leads to the requested name.

image-20191213180828528.png

image-20191213180859421.png

image-20191213180904886.png

This is the address to query. The push is preceded by add eax,0dh, so it points to pics.praticalmalwareanalysis.com.

5: How many local variables does IDA Pro identify in the subroutine at 0x10001656?

image-20191213212713210.png

IDA identifies 23 local variables in this subroutine.

6: How many parameters does IDA Pro identify in the subroutine at 0x10001656?

It identifies one parameter: IpThreadParameter=dword ptr 4.

7: Use the Strings window to locate the string \cmd.exe /c in the disassembly. Where is it?

image-20191213213025628.png

8: What happens in the region of code referencing \cmd.exe /c?

image-20191213213438591.png

The first part of this region looks like it prepares a command-line operation.

image-20191213213936548.png

The code starts a process, uses GetSystemDirectoryA to find the system directory, and executes cmd.exe or command.exe from that directory.

9: In the same region at 0x100101C8, dword_1008E5C4 appears to be a global variable that helps decide which path to take. How does the malware set it? (Hint: use cross-references to dword_1008E5C4.)

image-20191213223357790.png

I have already renamed the item at this location in my IDA database.

The preceding condition uses the GetVersionExA API to inspect the Windows version.

image-20191213223110763.png

Looking up this API shows that it checks for windows 2000: before Windows 2000, the command interpreter was command.exe, which explains the check above.

10: Among the hundreds of instructions in the subroutine at 0x1000FF58 are several string comparisons using memecmp. What happens if the comparison with robotwork succeeds (memcmp returns 0)?

First, I looked at the code before the comparisons:

image-20191213223836408.png

image-20191213223847953.png

This string is displayed on screen, preceded by code that displays the time. The last line is interesting: it mentions an encryption magic number and dword_1009E5D0. We will reverse it later; first examine memcmp as requested.

image-20191213224323988.png

The value passed as s is a command received from the remote socket.

image-20191213224352251.png

These memcmp calls look like the virtual machines often seen in CTFs. They check remote commands and use different functions to execute them.

I located the branch for the RobotWork command:

image-20191213224534691.png

I followed the call and began reversing the function; its name has been changed in this screenshot.

image-20191214141604980.png

It gets the Worktime value from HKEY_LOCAL_MACHIME\SOFTWARE\Microsoft\Windows\CurrentVersion and passes it to sub_100038EE.

Following the next call shows what happens to that value:

image-20191214145022619.png

The function adds an offset to the worktime value and sends the result to the remote socket.

image-20191214145217009.png

The earlier dword_1008E5D0 is related to encryption of the magic number; we will decipher it later.

11: What does the PSLIST export do?

I found PSLIST in IDA’s exports window:

image-20191214145325997.png

Following it leads to the function body:

image-20191214145536247.png

This is a function we encountered earlier. Its result indicates whether Windows is 32-bit and whether its version is later than Windows Server 2003.

A branch follows. Since the function call is not yet clear, follow the branch for a matching result, starting with sub_1000664C().

image-20191214150758268.png

After following the code, I concluded that it gets the current process list, sends it to the remote socket, and writes it to a file named Xinstall.dll. It returns 0 on success and 1 on failure. Rename it getProcessList.

The other branch calls sub_10006518().

image-20191214151035079.png

This function also gets the process list, but only writes Xinstall.dll; it does not send the list to the socket. Rename it getProcessListNoSend().

That completes the analysis of the PSLIST export.

image-20191214151445007.png

First it gets the operating system version. If it is too old or too new, it returns false. For windows 2000-windows 8.1 x32, it enumerates processes and uses the process name passed as str to decide which one to send.

12: Use graph mode to draw cross-references to sub_10004E79. Which API might be called on entering this function? Based only on the APIs, what would you name it?

image-20191214152132352.png

Graph mode would not open for me, but the cross-reference view provides the same information:

image-20191214152152206.png

After renaming the functions, it is clear that this function gets the system language and sends it to the socket. Rename it sendLanguageInfo().

13: How many Windows APIs does DllMain call directly? How many are called at depth 2?

image-20191214152906311.png

Since I could not open the graph, I used one from the solution:

img

14: At 0x10001358 there is a call to Sleep, an API taking a delay in milliseconds. Looking backward through the code, how long will the program sleep if this code executes?

image-20191214153227893.png

I opened the pseudocode to trace the sleep duration:

image-20191214153248408.png

The delay is 1000 × v14 milliseconds. To determine the duration, we need v14, which the code derives from a character using atoi.

image-20191214153355596.png

The ASCII value of [ is 91; adding 13 gives a total sleep of 104 seconds.

15: At 0x10001701 there is a call to socket. What are its three arguments?

image-20191214154045868.png

The three arguments are pushed onto the stack in reverse order. I looked up the function prototype to interpret them.

Official documentation:

https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket

1
2
3
4
5
6
7
8
9
#include <cket.c>
int __socket (domain, type, protocol)
int domain;
int type;
int protocol;
{
__set_errno (ENOSYS);
return -1;
}

af = 2 = AF_INET

type = 1 = SOCK_STREAM

protocol = 6 = IPPROTO_TCP

Thus, it creates a TCP stream connection.

16: Using the MSDN socket page and IDA Pro symbolic constants, can you make the arguments more meaningful? What are they after the changes?

image-20191214155026920.png

Applying IDA’s Symbolic Constant names makes those arguments much easier to read:

17: Search for uses of the in instruction (opcode 0xED). This instruction and the magic string VMXh can detect VMware. Are they used here? Do cross-references to the function executing in provide further evidence of VMware detection?

Search for in with alt+t and find the vmxh magic value:

image-20191214155409861.png

image-20191214155604603.png

The cross-references show three places that call the VMware-detection function:

image-20191214155733402.png

When the check finds VMware, the program writes a message to its log:

image-20191214155814569.png

This confirms the VMware detection.

18: Jump to 0x1001D988. What do you find?

image-20191214160029679.png

The bytes look like garbled text at first.

I wrote a small script to XOR the bytes and see what they contain:

1
2
3
4
5
6
7
8
9
10
11
12
13
a = [0x2D,0x31,0x3A,0x3A,0x27,0x75,0x3C,0x26,0x75,0x21,
0x3D,0x3C,0x26,0x75,0x37,0x34,0x36,0x3E,0x31,0x3A,
0x3A,0x27,0x79,0x75,0x26,0x21,0x27,0x3C,0x3B,0x32,
0x75,0x31,0x30,0x36,0x3A,0x31,0x30,0x31,0x75,0x33,
0x3A,0x27,0x75,0x05,0x27,0x34,0x36,0x21,0x3C,0x36,
0x34,0x39,0x75,0x18,0x34,0x39,0x22,0x34,0x27,0x30,
0x75,0x14,0x3B,0x34,0x39,0x2C,0x26,0x3C,0x26,0x75,
0x19,0x34,0x37,0x75,0x6F,0x7C,0x64,0x67,0x66,0x61]
res = ''
for i in a:
chs = i^0x55
res+=chr(chs)
print(res)

The decoded result:

image-20191214160558499.png

Extra

We still need to reverse and decipher the earlier magicNumberWaitiongForCrack.

image-20191214161136943.png

The variable appears in sendtoSocket(), a function called in many places.

I inspected the variable’s cross-references:

image-20191214161218525.png

I found the last function that assigns it and followed the call:

image-20191214161326717.png

It is a random number: get the CPU cycle count, take the low five bits, add 1, and use the result as an offset.

image-20191214161458729.png

Combined with the earlier function, this shows that when remote Enmagic is received, it displays this random number. Rename the variable randomMagicNumber.

When the program sends data, it adds this number as part of its encryption.