# Unpacking Dridex

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

Starting off with some Basic Analysis, we can deduce the Sample is packed

### **1→ Basic Threat Intel**

Possibly Dridex / Packed

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-23e9644c3f121994be38db41b5184d95ddc8dbfe%2F22a15d248739686876c481cc6b0f63f1.png?alt=media" alt=""><figcaption></figcaption></figure>

***

### **2→ Basic Static Analysis**

Though the entropy of the file is quite low, we can still find a *black listed* *section **`text1`,*** a noticeable difference in ***`RawSize` / `VirtualSize`*** with ***`.rsrc`*** section and ***`size-of-code`*** of 0bytes that makes it pretty obvious it's packed.

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-a70b401da54f84e353008a61ec4f7cd183cc2a53%2Fac90321d64da170b7bcaf30ff8a346ac.png?alt=media" alt=""><figcaption></figcaption></figure>

***

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

**1→**

Starting off with putting breakpoints on

* ***`VirtualAlloc()` →*** to see if the sample allocates memory to copy the unpacked executable or the Shellcode responsible for decrypting/unpacking the actual payload.
* \_**`VirtualProtect()` →**\_if the sample changes a protection of a section/ region of memory.

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-277e5b0857e73a0277e2e59c5859b11e9f1cc734%2F6097e4af6bfc5db0def81fe4c6da7c2d.png?alt=media" alt=""><figcaption></figcaption></figure>

**2→**

Run the debugger, we hit a breakpoint on ***`VirtualAlloc()`***, with ***Execute Till Return*** \[2], following the return value *memory address returned from **`VirtualAlloc()`** , stored in **EAX*** in Dump \[2\`], we can see the newly allocated region of memory \[2\`\`],

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-aeefa543132a3a3c3c24de5be1e80d4a3c973a31%2Fbbfa7a0c952d516c5b02308a9bc6d646.png?alt=media" alt=""><figcaption></figcaption></figure>

**3→**

Continuing on *Run \[F9],* we can see an executable being written with normal sections and no `.`*`text1`* section, though the PE header is a bit strange but this might be important to us.

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-a936b3a4eefac0edcf085c51ae3f3b77e71ea700%2Faa6f1087621672efe9aa63e006db4229.png?alt=media" alt=""><figcaption></figcaption></figure>

**4→**

We hit ***`VirtualAlloc()`*** twice again so we repeat step\[2] and investigate the two allocated regions of memory.

**4\`:** Now we can see a similarity between **Dump1** and **Dump2**, both are small, has similar strings and their code starts with the same sequence of bytes.

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-ed942fbf1cc5309b00849fca33e6994fa92bc9da%2F8b1b297d4e877a824ed3dbec959b7834.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-162d48cf2c609f2e408b83b6ba55f6a81093205f%2Fb1350eb999f819161d65bc3da694ba0e.png?alt=media" alt=""><figcaption></figcaption></figure>

**5→**

That leaves **Dump3** which is pretty different and is much larger, with a punch of interesting strings including ***`ldr.exe`*** which is what **Dridex** is known of. So we can go ahead and dump this out since it's still not mapped into memory.

// To make sure we go the right Dump, we would trace the execution until the Sample overwrite itself with the Unpacked Payload, then we use a tool like **procexp** or **process hacker** to dump the overwritten process.

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-ae3ebfc54e9d40465833dfdbac8c6467b8282a40%2Fe71f8243bf1827ec43afc0e05ed44989.png?alt=media" alt=""><figcaption><p>To make sure we go the right Dump, we would trace the execution until the Sample overwrite itself with the Unpacked Payload, then we use a tool like <code>procexp</code> or process hacker to dump the overwritten process.</p></figcaption></figure>

**6→**

Now that we've hit ***`VirtualProtect()`*** we can jump to user code and look for a ***`jump [eax / reg]`*** \[6\`] or ***`push <reg/ address>`***` ,`` `` `***`ret`***, since the process will overwrite itself with the unpacked executable it will need to jump to the entry point of the unpacked executable to run the Malware from there. Once we hit Run we can find **Dump3** got moved \[6\`\`] which means that the process has overwritten itself with it, meaning this is the unpacked executable we are looking for and the other two **Dumps** are of no interest to us.

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-50716362eca453db1db2b80212887e896ed506ce%2F5f41221799ca5c783fa0e76df7403b13.png?alt=media" alt=""><figcaption></figcaption></figure>

**7→**

Continuing on we hit a couple of ***`VirtualProtect()`*** calls then we hit the ***`jmp eax`*** breakpoint, stepping into that we find ourselves inside of ***Dridex' main function*** at offset ***`0x10000000`***, with hashes being pushed and a call to a function that compares some hash to ***`[ESI]`*** which could be the API Hashing routine used by **Dridex** to resolve **APIs.** Now we're confident we're inside the Unpacked Payload.

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-a4fbce87492ed9b4bbf225180c34405dbf4eeb39%2Fb856cc5ee60877c1e4b703b1ce74937a.png?alt=media" alt=""><figcaption></figcaption></figure>

**8→**

Using Process Hacker we can dump the Memory Block at ***`0x10000000`,*** which is now overwritten with the Actual Payload.

<figure><img src="https://1039101533-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtKQMawjtA9bFZEnblVFu%2Fuploads%2Fgit-blob-4c546b6905f137db3e3a8ce5cc42879a6ea4abf4%2Fd035fa07ea58b82379edbb2cfd72e808.png?alt=media" alt=""><figcaption></figcaption></figure>

> Since this Dump is mapped into memory we need to fix the **Sections' Addresses to match the Virtual Addresses** \[7\`] we dumped it from so the imports are resolved. Also fix the **Image Base Address** to match the process image's \[7\`\`]. I'll use PE-Bear for that.

To Summarize, **Dridex** used **Self-Injection** to overwrite it's process memory with the **Actual/Unpacked Payload**. Check this Overview on [**Self-Injection**](https://ghostinthehive.gitbook.io/ghostinthehive-as-a-ghost-in-the-hive/windows-internals/malware-evasion-through-injection-pt1#self-injection) for more details.
