> For the complete documentation index, see [llms.txt](https://ghostinthehive.gitbook.io/ghostinthehive-as-a-ghost-in-the-hive/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ghostinthehive.gitbook.io/ghostinthehive-as-a-ghost-in-the-hive/windows-and-malware/malware-dissecting/unpacking-ramnit.md).

# Unpacking Ramnit

**Working on this** [**Sample**](https://app.any.run/tasks/164cf960-adb7-40c9-8439-dfcbfff1a2bb/)

***

## <mark style="color:red;">Static Analysis</mark>

### **-> Basic Threat Intel**

<figure><img src="/files/qB4if9edbHvksrDbMx4l" alt=""><figcaption><p>Virus Total</p></figcaption></figure>

This Sample does loads of stuff, we got a hint that it might be a **Ramnit** Trojan. **Ramnit** is a Famous Banker Malware Family.

***

### **-> Basic Static Analysis**

In addition to high entropy\[1], the Sample is detected with a resource as an ***AutoIt Script***\[1\`\`], which Malware loves a lot being easy/small to automate the **WindowsGUI** tasks and other Windows general scripting purposes.

<figure><img src="/files/x8W1T63Pz1NI6EnrxYYI" alt=""><figcaption></figcaption></figure>

Also another interesting section ***`.text`*** \[1\`\`\`]with a very high entropy and suspicious characteristics such as being *Writable/Executable*, with the execution ***Entry-Point*** starting from there. So now we want to run the Sample in a Debugger to dynamically unpack it and dump the actual payload off of this mess.

<figure><img src="/files/mgm32HYKByWbi74Rd06U" alt=""><figcaption></figcaption></figure>

***

## <mark style="color:red;">Dynamically Unpacking the Sample</mark>

**1→**

Jumping to **User Code** and checking the ***Memory Map***\[1\`] , we find ourselves inside that suspicious section detected **`.`*****`text`*** at the **Basic Static Analysis** step.

Setting up breakpoints\[1\`\`] at ***`VirtualAlloc()`/ `VirtualProtect()`/ `CreateProcesInternalW()` →*** in case the binary decides to spawn another process and continue executing from there.

<figure><img src="/files/vpNPWsqWgXkarcwtUeyg" alt=""><figcaption></figcaption></figure>

The Code Starts with **`pushad`** which is an assembly instruction used to ***save/push*** registers and most commonly used for using the registers in the unpacking process without changing the original registers' values, with a later call to **`popad`** that would restore the registers to their original state. So we can follow this sequence and look for a good jump to some code \[ `jmp eax` : in this case ]and continue from there and see if it gets us anywhere, but we are interested on how it uses Process Injection to inject the Unpacked Payload so we will focus on that.

<figure><img src="/files/QbkOPZLiS6Y6j9pBYPOA" alt=""><figcaption></figcaption></figure>

**2→**

Once hitting Run\[F9], we find a file being dropped ***`Zeichnungen Mustermgr.exe`***\[2\`].

Noticing the full path to the dropped file that's been pushed to the stack\[2\`\`], and if we walked through the disassembly, we find the malware will query the system to deduce the ***`ComputerName()`/ `CurrentWorkingDir()`*** ..etc to construct the full path to which the file will be dropped. but since we do not really care about how it performs this, we won't look at that deeply now.

<figure><img src="/files/9d7oC1tcNOqCieLdWQ9C" alt=""><figcaption></figcaption></figure>

**3→**

We also hit the breakpoint at ***`CreateProcessInternalW()`*** with the path to the file been dropped\[3\`], we would now set a breakpoint on ***`NtResumeThread()`*** to catch the process before it resumes, and a breakpoint on ***`WriteProcessMemory()`*** to see if there is any data being written.\[3\`\`]

<figure><img src="/files/3VHCDCqk95i7lnXULJlK" alt=""><figcaption></figcaption></figure>

Continuing on with Run\[F9], we instantly hit the breakpoint on ***`NtResumeThread()`,***\[3\`\`\`]. Opening **`ProcessHacher`** or **`Procexp`** we find a child process spawned with the name of the file being dropped `Zeichnungen Mustermgr.exe`.

<figure><img src="/files/aLunuDlKtaINw6VoVmPQ" alt=""><figcaption></figcaption></figure>

**4→**

Opening a new debugger to attach to the new process with the name ***`Zeichnungen Mustermgr.exe`\*\*\*\*\*\*\*\*.***

<figure><img src="/files/QhbyOW2TMuJGduT19KqX" alt=""><figcaption></figcaption></figure>

**5→**

Continuing on Run\[F9] from ***`NtResumeThread()`,*** we can see a popup window about it being an **AutoIt** script, we will leave this open for now and check the debugger window attached with ***`Zeichnungen Mustermgr.exe`\*\*\*\*\*\*\*\*.***

<figure><img src="/files/HXnTCtMYa6I7Gga6HGl0" alt=""><figcaption></figcaption></figure>

NOTE: if we closed the popup window, the process will exit leaving the dropped file on disk, So we can choose between leaving the main process and attaching a new debugger window on ***`Zeichnungen Mustermgr.exe`,*** Or we can close the main process and debug the dropped file normally like we do, either way from here on all next debugging steps are same.

***

### -> Debugging the Dropped File/ Spawned Child Process

**6→**

For ***`Zeichnungen Mustermgr.exe`*** we would want to put the same breakpoints as before: ***`VirtualAlloc()`****/**** ****`VirtualProtect()`****/\*\*\*\*****&#x20;****`CreateProcessInternalW()`****/\*\*\*\***** ****`WriteProcessMemory()`****.***

On Running, the first thing we hit is ***`VirtualAlloc()`*****,** On execute till return we follow the memory address in ***Dump1*** and run, we see pretty much **shellcode** being written in the dump which what definitely will unpack/ decrypt the actual executable.

<figure><img src="/files/hBx9u9PcZCi0hvf3sAug" alt=""><figcaption></figcaption></figure>

Continuing on we notice two calls to ***`VirtualAlloc()`***, with two allocated region of memory that are only filled after returning from last call to ***`VirtualAlloc()`.***\[6\`], Both Dumps are filled with the exact executable\[6\`\`].

<figure><img src="/files/ue4WKF52YlzzfqZeyiew" alt=""><figcaption></figcaption></figure>

**7→**

Right now we could save this binary directly from the Dump Window, or if we have **ProcessHacker/ Procex** open, navigate to ***`Zeichnungen Mustermgr.exe`*** and dump the memory at the memory address of **Dump3** before its being mapped.

<figure><img src="/files/hxKEXHodMZ5eXRflPxhF" alt=""><figcaption></figcaption></figure>

**Dump3** is just larger in size that **Dump2**, maybe that's why I choose it?, you can check **Dump2** and analyze it to see if both are the same or not.

***

## <mark style="color:red;">Analyzing the Dumped Binary</mark>

**1→**

Performing **Basic Static Analysis,** we find the dump is **`UPX`** packed with high entropy, which is quit straightforward as most Malware using **AutoIt** scripts often compress it with **`UPX`.**

<figure><img src="/files/kQJKq0NpgtybquVN9Dwr" alt=""><figcaption></figcaption></figure>

**2→**

Now we can automatically unpack the dump with a simple `upx -d <upxpacked.exe>` command.

For the **`UPX`** part we can still use Generic Unpacking and fire the dumped binary in a debugger and set breakpoints on ***`VirtualAlloc()`/ `VirtualProtect()`***, or find code patterns like *`[popad | pushad]`* and continue from there. This Sample used Custom Packing which was interesting, for more information about Packers, Check [**Malware Unpacking**](/ghostinthehive-as-a-ghost-in-the-hive/windows-and-malware/windows-internals/malware-unpacking.md) from **The REM-Essential Series**.
