CyberDefenders Ransomed lab writeup

Overview#

PlatformCyberDefenders
CategoryMalware Analysis
DifficultyHard
FocusUnpacking, stack strings, shellcode, process hollowing
Lab LinkRansomed

A high-entropy executable flagged on a workstation. Static analysis doesn’t get you far on this one because the real code is encrypted. At runtime it decrypts shellcode into fresh memory, jumps into it, and the shellcode hollows out a new copy of the program. Most of this lab is x32dbg, with IDA for the static parts and scdbg for the dumped shellcode.

Q1: entropy#

What is the value of entropy?

PEStudio, first page:

Entropy Analysis
PEStudio: entropy 7.677

Plain code and data usually sit somewhere between 5 and 6.5. Above 7 means a big chunk of the file is compressed or encrypted, so expect the strings and imports you see on disk to be incomplete.

Answer: 7.677

Q2: sections#

What is the number of sections?

Same tool we used first, PEStudio, sections tab:

Sections Analysis
Four sections: .text, .data, .rsrc, .reloc

.text, .data, .rsrc, .reloc. Nothing unusual in the layout, no UPX-style names. The encrypted payload is inside one of the normal sections.

Answer: 4

Q3: string obfuscation#

What is the name of the technique used to obfuscate string?

strings doesn’t show kernel32.dll or any of the APIs it ends up using. In IDA, around +0x9bee0:

int setup_virtual_protect()
{
  _BYTE v1[4]; // [esp+4h] [ebp-4h] BYREF
  strcpy(g_api_name_buffer, "kernel32.dll");
  g_kernel32_handle = (int)GetModuleHandleA(g_api_name_buffer);
  strcpy(g_api_name_buffer, "VirtualProtect");
  g_VirtualProtect_addr = (int)GetProcAddress((HMODULE)g_kernel32_handle, g_api_name_buffer);
  return ((int (__stdcall *)(int, SIZE_T, int, _BYTE *))g_VirtualProtect_addr)(
           g_decrypted_code_ptr,
           g_junk_condition,
           64,
           v1);

Those strcpy calls aren’t in the binary. The disassembly is runs of mov with immediates writing the string piece by piece into a buffer, and Hex-Rays folds that back into a strcpy with a literal. On disk the string never exists in one piece, which is why strings misses it. Then it goes straight into GetModuleHandleA / GetProcAddress.

Answer: stack strings

Q4: allocation API#

What is the API that used malware-allocated memory to write shellcode?

The code above changes protection on g_decrypted_code_ptr, so that memory was allocated somewhere earlier. We can show that by just setting a breakpoint on the VirtualAlloc call in the debugger:

VirtualAlloc Breakpoint
Breakpoint on VirtualAlloc

Answer: VirtualAlloc

Q5: memory protection#

What is the protection of allocated memory?

Here’s the protect call from the same routine, through the resolved pointer:

((void (__stdcall *)(char *, int, int, _DWORD *))(v10 + v11))(
    ImageBaseAddress + 1814528,
    196608,
    64,
    v14
);

Third argument is 64 = 0x40:

ValueConstant
0x04PAGE_READWRITE
0x20PAGE_EXECUTE_READ
0x40PAGE_EXECUTE_READWRITE
PAGE_EXECUTE_READWRITE
0x40 passed as the protection

Writable and executable at once, so it can decrypt in place and run what it decrypted.

Answer: ERW (PAGE_EXECUTE_READWRITE)

Q6: jump to shellcode#

What assembly instruction is used to transfer execution to the shellcode?

Break on kernel32!VirtualAlloc, run to return, and follow what happens to EAX:

VirtualAlloc Hit
VirtualAlloc breakpoint hit

btw the return value of VirtualAlloc is the one to watch, it’s the base address of the shellcode. It goes into a local at [ebp-4]. A few instructions later the loader fills the region, and then:

jmp dword ptr ss:[ebp-4]
Jump to Shellcode
Indirect jmp into the shellcode

An indirect jump through a stack slot. No call, so no return address is pushed, and a static disassembler has no target to follow.

Answer: jmp dword ptr ss:[ebp-4]

Q7: kernel32 functions#

What is the number of functions the malware resolves from kernel32?

Easiest to dump the shellcode and look at it on its own.

Break on the ret of VirtualAlloc:

75412F3E | C2 1000 | ret 10
EAX = 0x04990000

The region is still empty at that point:

Empty Memory Dump
Allocated region, still zeroed

Step a bit further and it’s filled:

Payload Dump
Payload written to the region

The size passed to VirtualAlloc was 0x0011B000. Dump it from the x32dbg command line:

savedata payload.bin, 04990000, 0011B000

Then run it through scdbg:

scdbg.exe /f payload.bin /findsc

/findsc scans for likely entry points. Pick the right one and let it emulate. It logs every API resolution:

4017fa  LoadLibraryA(kernel32)
401831  GetProcAddress(WinExec)
40189f  GetProcAddress(CreateFileA)
4018de  GetProcAddress(WriteFile)
40194c  GetProcAddress(CloseHandle)
4019cf  GetProcAddress(CreateProcessA)
401a60  GetProcAddress(GetThreadContext)
401ad5  GetProcAddress(VirtualAlloc)
401b58  GetProcAddress(VirtualAllocEx)
401bc6  GetProcAddress(VirtualFree)
401c5e  GetProcAddress(ReadProcessMemory)
401cfd  GetProcAddress(WriteProcessMemory)
401d8e  GetProcAddress(SetThreadContext)
401e03  GetProcAddress(ResumeThread)
401ea9  GetProcAddress(WaitForSingleObject)
401f0c  GetProcAddress(GetModuleFileNameA)
401f96  GetProcAddress(GetCommandLineA)

One LoadLibraryA(kernel32), then 16 GetProcAddress calls on that handle. The list alone already says hollowing: CreateProcessA, Get/SetThreadContext, VirtualAllocEx, WriteProcessMemory, ResumeThread.

Answer: 16

Q8: string after RegisterClassExA#

The malware obfuscates two strings after calling RegisterClassExA. What is the first string?

The strings are only in plaintext around the API call, so break on user32!RegisterClassExA and look at the stack at entry:

755779F0 | 8BFF                   | mov edi,edi                             |
755779F2 | 55                     | push ebp                                |
755779F3 | 8BEC                   | mov ebp,esp                             |
755779F5 | 8B4D 08                | mov ecx,dword ptr ss:[ebp+8]            |
755779F8 | 8339 30                | cmp dword ptr ds:[ecx],30               |
755779FB | 0F85 0E260500          | jne user32.755CA00F                     |
75577A01 | 6A 01                  | push 1                                  |
75577A03 | 68 80000000            | push 80                                 |
75577A08 | 6A 00                  | push 0                                  |
75577A0A | 33D2                   | xor edx,edx                             |
75577A0C | E8 BC100400            | call user32.755B8ACD                    |
75577A11 | 5D                     | pop ebp                                 |
75577A12 | C2 0400                | ret 4                                   |
[esp+4]  = 007EB85C
[esp+8]  = 646F6173
[esp+0C] = 6F6E666B
[esp+10] = 75396173
[esp+14] = 00006E69

[esp+4] is the pointer to the WNDCLASSEXA. The dwords after it are ASCII, little-endian:

73 61 6F 64 | 6B 66 6E 6F | 73 61 39 75 | 69 6E
s  a  o  d    k  f  n  o    s  a  9  u    i  n

Following the pointer shows the same string in memory:

RegisterClassExA String
The string on the stack at RegisterClassExA

Step past the call and that buffer gets overwritten, so this is the only point where you can read it.

Answer: saodkfnosa9uin

Q9: dwCreationFlags#

What is the value of dwCreationFlags of CreateProcessA?

Breakpoint on kernel32!CreateProcessA. It stops inside the wrapper, right before it forwards to CreateProcessInternalA:

754993E7 | call kernelbase.CreateProcessInternalA
CreateProcessA Breakpoint
Stopped inside CreateProcessA
EIP : 754993E7  kernelbase.CreateProcessInternalA
EBP : 007EB77C
ESP : 007EB74C
EDX : "C:\Users\XXX\challenge.exe"

EDX is the target: it’s launching another copy of itself.

We’re past the prologue, and the wrapper has already pushed arguments for the inner call, so ESP points at those. Read the original arguments off EBP:

[EBP+08]  lpApplicationName
[EBP+0C]  lpCommandLine
[EBP+10]  lpProcessAttributes
[EBP+14]  lpThreadAttributes
[EBP+18]  bInheritHandles
[EBP+1C]  dwCreationFlags

0x007EB77C + 0x1C = 0x007EB798:

007EB78C  00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 08
dwCreationFlags Stack
dwCreationFlags at [EBP+1C]

04 00 00 08 little-endian is 0x08000004:

0x00000004  CREATE_SUSPENDED
0x08000000  CREATE_NO_WINDOW

Suspended, so the main thread hasn’t run yet when the loader starts rewriting its memory.

Answer: 0x08000004

Q10: injection technique#

The malware uses a process injection technique. What is its name?

What we have so far: a copy of itself started with CREATE_SUSPENDED, and a shellcode import list built around remote memory and thread context. The calls in Q11 show the middle part: unmap the child’s image, allocate in its place, write the payload. SetThreadContext and ResumeThread from the Q7 list are what’s left to point the thread at it and let it go.

Answer: Process Hollowing

Q11: write API#

What is the API used to write the payload into the target process?

After RegisterClassExA the loader goes through:

ReadProcessMemory
GetThreadContext
NtUnmapViewOfSection
VirtualAllocEx
NtWriteVirtualMemory
WriteProcessMemory

The read and the context call get it the child’s image base, NtUnmapViewOfSection throws that image out, VirtualAllocEx grabs the space, and then it writes. NtWriteVirtualMemory is the native call underneath. The API the loader itself calls is WriteProcessMemory.

Answer: WriteProcessMemory

Answers#

QAnswer
17.677
24
3stack strings
4VirtualAlloc
5ERW
6jmp dword ptr ss:[ebp-4]
716
8saodkfnosa9uin
90x08000004
10Process Hollowing
11WriteProcessMemory