Weaponized DOC with TeslaCrypt (?)

Posted on Updated on

Hello,

Recently a weaponized MS Doc file was given to me for analysis. Due to the fact that an initial sandbox analysis detected that it contains an exploit, it raised my curiosity. So let’s see what we have.

fee6xb

So it is a simple Doc file (non xml). Let’s use olevba from Oletools and oledump

ndrod4

gvznpy

Definitely suspicious! Especially this VirtualAlloc call seems quite interesting. Below you can find a screenshot of the extracted VBA code.j2lvcg

As you can see it is full of comments which could be easily removed with a Regular Expression on Notepad++ or Sublime etc. Between the comments there is actual code and junk code which has to be removed also. What caught my attention is the CallWindowProcA and RtlMoveMemory (red color). I have recently read a blog post about the CallWindowProc (http://waleedassar.blogspot.gr/2012/03/visual-basic-malware-part-1.html) that is being currently used by Macro malwares. What this function does, is to execute an allocated memory directly. This is a more direct method than using CreateProcess, ShellExecute, WriteMemory, Hollow Process techniques etc.

After cleaning the file we have the following code (snip)

uh7psa

The autorun function contains a call to cakewalk function which is the main function of the document. Inside cakewalk the following things take place:

  1. Volumetric stream is being called and object sashay is being referenced. We have seen at the output of oledump that there is a stream named volumetric (17). Due to the fact that I haven’t manage to programmaticaly extract such objects (e.g a python library) I prefer to use LibreOffice in order to access the various components. As you can see in the next screenshot volumetric is a UserForm and sashay is a label. The text of the label is the actual shellcode which will be decoded.
  2. The shellcode is passed for decryption/decoding  in function methylated
  3. Then a memory allocation takes place (VirtualAlloc)
  4. Function heft is called which takes the current document’s full name (ThisDocument.FullName) (the code is not included in the previous screenshots)
  5. The allocated memory is filled by a function leuciscus.
  6. Finally the CallWindowProcA is being called in order to execute the decoded shellcode. Note that based on the architecture (32bit or 64bit) different offsets are being used. Also note that in the second argument called diduction is the file name of the current document. This is important and keep that in mind

 

r7ibnv
LibreOffice Macro Editor

So how do we dump the shellcode (From now on this shellcode will be called Stage1 Loader)? Just recreate the function methylated in a macro in an MS Word document, save it to a file and open it in IDA.

Now lets analyze the Stage1 Loader. The starting offset for 32 bit is 2214 (0x8a6).

vpwm8u

So the offset is a function prologue, then we are correct! I will not explain in detail the assembly code because it will need too many screenshots, but the basic idea is the following:

  1. Go to PEB fs:30 and get the loaded DLLs linked list in order to find the second in list (ntdll.dll).
  2. There is a function SeachFunctionName which gets two arguments, a DLL address and a string of a function. It searches the respective function address and returns the value. Screenshot can be seen below with the internals of the function
  3. The function names that are being searched are the following:
    • LdrLoadDll
    • ExpandEnvironmentStringsA
    • ReadFile
    • CreateFile
    • GetFileSize
    • CloseHandle
    • VirtualFree
    • WriteFile
    • CreateProcessA
  4. There is a function block that converts ascii to unicode in order to pass “kernel32.dll” as unicode argument to LdrLoadDll (LdrLoadDll( IN PWCHAR PathToFile OPTIONAL, IN ULONG Flags OPTIONAL, IN PUNICODE_STRING ModuleFileName, OUT PHANDLE ModuleHandle );). The function returns a handle to kernel32.dll. ExpandEnvironmentStringA is a kernel32.dll function all the order ones are ntdll.dll functions
    jtos2w
  5. It tries to open the actual word file (in order words itself). Remember when I mention that you should keep in mind the second argument of CallWindowProcA? Here it is
    gazgkf
    The arg0 is the current document. Why does it use this approach? Let’s see on step 6.
  6. Then it reads the the word document itself until it finds the 0x504f4c41 byte sequence. This is important because the Stage1 Loader is just a loader not the actual malware. It seems that there is another secondary shellcode which is encoded/encrypted inside the Word document, while the Stage1 Loader is an embedded Macro Object. The specific byte sequence is the marker of the beginning of the secondary shellcode (Stage2 Loader).
    ujvmli
    3hz6ov
  7. It then creates a file in %TMP%\oi809.exe, it decodes the code from the previous step and writes it to the oi809.exe.
  8. Finally it executes the oi809.exe

Some internals of the “SearchFunctionName”

lquamh

Also the internals of the FindExportDirectoryRVA, with the various checks for the validity of the PEweguod

If you want to make dynamic analysis of the Stage1 Loader you need a way to break the execution into it. You can use something like the following code. The Stage1 Loader is already dumped into a file (remember the replicated macro we created) and accessed by our code.

HANDLE hFile = CreateFileA(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);
DWORD dwFileSize = GetFileSize(hFile, NULL);
DWORD dwReadData = 0;
DWORD dwOldProtect = 0;

LPVOID dataAlloc = VirtualAlloc(NULL, dwFileSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
LPBYTE target = (LPBYTE)dataAlloc + 2214;
ReadFile(hFile, (LPBYTE)dataAlloc, dwFileSize, &dwReadData, NULL);
char* fff = "C:\\temp\\stage1loader.bin";
__asm int 3;
CallWindowProc((LPBYTE)dataAlloc+2214, fff, 0, 0, 0);

A note, inside the internals of CallWindowProcA there is a call dword ptr[ebp+0x8]. The offset is different in each version of windows. If you can’t find the offset of the call instruction just add a 0xcc byte (int3) in the stage1loader.bin and let it break in the debugger. With manual stack walking you can get the return address which is the  call dword ptr[ebp+0x8] instruction. With a debugger you can add a breakpoint there.

The oi809.exe analysis

To be honest I didn’t have the time to thoroughly analyze the file so I will only give a vague description of what is going on. This is the beginning of the oi809.exe. This particular executable is our Stage2 Loader.

xvt6wj

As you can see there are some delay loops, probably for anti analysis (you can easily set edi value to 1). The most important functions (sorry for the chosen names) are the DecryptPayloadResource which is responsible for decrypting a resource inside the executable and the HollowProcess function which uses the classic technique that, executes it self again, unmaps the executable rewrites it with the needed code, uses SetThreadContext to set the starting point and then ResumeThread to continue execution.

The decryptPayloadResource searches for a resource of type RCDATA(0xA) and ID 0x821B (33307)

hepoa0

wgfx5q

Then it tries to decrypt it

2k4acn

Finally lets have a look and the HollowProcess block which writes the decrypted shellcode in the memory of newly created process.

nx4y32

Nothing new about that. In order to dump the actual malicious content for further analysis you need to add a breakpoint in the debugger in 0x00401930 where the ebx register will contain the payload of size equals to eax value. From ollydbg you can easily dump it.

Uploading the dumped contents  we get a suspicion that the actual malicious file might be Teslacrypt.

9poqe4

Unfortunately I have not analyzed the actual malware. I am sure (if it is Teslacrypt) the are plenty of excellent articles and analyses.

If you have reached this point I would like to thank you for your patience. In case something is wrong with the analysis or you have any suggestions please let me know.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s