# VBA Stomping

Adversaries may hide malicious Visual Basic Applications (VBA) payloads embedded within MS Office documents by replacing the VBA source code with benign data.

MS Office documents with embedded VBA Content store source code inside of module streams. Each module stream has a PerformanceCache that stores a separate compiled version of the VBA source code known as p-code. The p-code is executed when the MS Office version specified in the \_VBA\_PROJECT stream (which contains the version-dependent description of the VBA project) matches the version of the host MS Office application.

An adversary  may hide malicious VBA code by overwriting the VBA source code location with zero's, benign code, or random bytes while leaving the previously compiled malicious p-code. Tools that scan for malicious VBA source code may be bypassed as the unwanted code is hidden in the compiled p-code. If the VBA source code is removed, some tools might even think that there are no macros present. If there is a version match between the \_VBA\_PROJECT stream and host MS Office application, the p-code will be executed, otherwise the benign VBA source code will be decompressed and recompiled to p-code, thus removing malicious p-code and potentially bypassing dynamic analysis.

&#x20;**VBA Macros Advanced**

**Evil Clippy**

Evil Clippy a tool released in BlackHat Asia in 2019 it is a a maldoc assistant which help's red teams to bypass popular AV and get an initial foothold, in this category we will focus on the **VBA Stomping** technique. Discovered years ago by Dr. Vesselin Bontchev ([here](https://github.com/bontchev/pcodedmp)). At a high level explanation of this attack by creating a malicious document we can actually add a non-malicious macro into the source code of the file, as explained by Dr. Bontchev what actually executes is the p-code stored in the document as long as it's compatible with the current VBA version.

Let's work with some samples a Non-malicious Macro is created. A message box is to be displayed when the document is opened.

![](https://315180959-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRh03Vwd4nuiUi3Oje7%2F-MRhmk4n0GjkLPwi_qrv%2F-MRhmt8Uptkx73uFx0yU%2Fimage.png?alt=media\&token=51cb92b3-1300-4d9d-8138-b26b2e839976)

Now from here we want to modify the VBA source code utilized while leaving the p-code unchanged. To edit this file you will unzip it and edit the vbaProject.bin file using a hex editor, but only withing the VBA source code storage location, not the p-code section.

![](https://315180959-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRh03Vwd4nuiUi3Oje7%2F-MRhmk4n0GjkLPwi_qrv%2F-MRhmsPmtb-KTDTQfsfu%2Fimage.png?alt=media\&token=54584a8d-d2ec-458d-b270-1ca508ada3d5)

Now that the VBA source code has been manually edited we will open the document and inspect the VBA Code BEFORE the "Enable Content" button is clicked.

![](https://315180959-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRh03Vwd4nuiUi3Oje7%2F-MRhmk4n0GjkLPwi_qrv%2F-MRhmue54tIXhPTjLc-3%2Fimage.png?alt=media\&token=3253b6eb-305b-43f5-a054-d60acea0fb1e)

We see here that the source-code still displays XYZ but in fact once the content is enabled we see a message box displaying "ABC"

![](https://315180959-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRh03Vwd4nuiUi3Oje7%2F-MRhmk4n0GjkLPwi_qrv%2F-MRhmvSQxlfIfjYQe6ju%2Fimage.png?alt=media\&token=b0a557b2-6796-4ef9-9ec0-24b46d685197)

Well what happened here?. Our source code stated that XYZ was going to be executed but instead ABC was displayed and later on our Code updated to match the execution.

References:

{% embed url="https://medium.com/walmartglobaltech/vba-stomping-advanced-maldoc-techniques-612c484ab278" %}

