Challenge 04

4 - report

Nobody likes analysing infected documents, but it pays the bills. Reverse this macro thrill-ride to discover how to get it to show you the key.

Ở challenge này, chúng ta có 1 file excel:

Mờ file report.xls bằng Microsoft Excel:

Khi làm việc với các file office, có 1 tool để extract VBA macro từ chúng, đó là tool “olevba”.

VBA macro: khi phải làm các công việc thường xuyên lặp đi lặp lại trong excel, bạn có thể viết 1 script để tự động các công việc đó. Script đó được veiét bằng Visual Basic for Applications (VBA).

Lợi dụng điều này, malware cũng thường hay chứa các VBA macro độc hại trong các file excel để tấn công người dùng.

olevba là một tool dùng để extract các VBA macro trong file excel.

C:\Users\admin\Desktop\Mat\4_-_report>olevba report.xls

Sau khi chạy xong, ta đã có được code VBA và sẵn sàng phân tích, nhưng ở dưới cùng có 1 dòng đáng để ý:

VBA Stomping was detected: the VBA source code and P-code are different, this may have been used to hide malicious code

Trước tiên ta cần hiểu về VBA source code và P-code, đoạn giải thích gốc nằm ở đây

It is not widely known, but macros written in VBA (Visual Basic for Applications; the macro programming language used in Microsoft Office) exist in three different executable forms, each of which can be what is actually executed at run time, depending on the circumstances. These forms are:

Source code. The original source code of the macro module is compressed and stored at the end of the module stream. This makes it relatively easy to locate and extract and most free DFIR tools for macro analysis like oledump or olevba or even many professional anti-virus tools look only at this form. However, most of the time the source code is completely ignored by Office. In fact, it is possible to remove the source code (and therefore make all these tools think that there are no macros present), yet the macros will still execute without any problems. I have created a proof of concept illustrating this. Most tools will not see any macros in the documents in this archive it but if opened with the corresponding Word version (that matches the document name), it will display a message and will launch calc.exe. It is surprising that malware authors are not using this trick more widely.

P-code. As each VBA line is entered into the VBA editor, it is immediately compiled into p-code (a pseudo code for a stack machine) and stored in a different place in the module stream. The p-code is precisely what is executed most of the time. In fact, even when you open the source of a macro module in the VBA editor, what is displayed is not the decompressed source code but the p-code decompiled into source. Only if the document is opened under a version of Office that uses a different VBA version from the one that has been used to create the document, the stored compressed source code is re-compiled into p-code and then that p-code is executed. This makes it possible to open a VBA-containing document on any version of Office that supports VBA and have the macros inside remain executable, despite the fact that the different versions of VBA use different (incompatible) p-code instructions.

... truncated

Như giải thích ở trên thì:

  • VBA macro có thể tồn tại ở 3 dạng khác nhau.
  • Dạng 1 là source code, source code sẽ bị nén lại và đặt ở cuối module. Và ta có thể bỏ source code khỏi module mà macro vẫn có thể chạy bình thường.
  • Dạng 2 là p-code: VBA macro sẽ được biên dịch thành p-code, là một dạng mã giả của Microsoft. Code này mới là code được thực thi thật sự, và code này cũng được đặt ở trong module.
  • Dạng 3 mình không đề cập ở đây.

Về VBA Stomping: có thể đọc thêm ở đây, nhưng cơ bản ta có thể hiểu là:

  • Khi lưu 1 file excel, nó sẽ chứa cả source code và p-code trong đó.
  • Nhưng source code và p-code này không match nhau, tức là p-code hoặc source code đã bị thay đổi.

Để khôi phục VBA code từ P-code, ta có thể dùng pcode2code (hoặc đọc p-code luôn).

C:\Users\admin\Desktop\Mat\4_-_report>pcode2code report.xls

So sánh code mới và code cũ (code mới ở bên trái)

Hàm rigmarole dùng để decrypt string:

Function rigmarole(es As String, id_FFFE As String) As String
    Dim furphy As String
    Dim c As Integer
    Dim s As String
    Dim cc As Integer
    furphy = ""
    For i = 1 To Len(es) Step 4
        c = CDec("&H" & Mid(es, i, 2))
        s = CDec("&H" & Mid(es, i + 2, 2))
        cc = c - s
        furphy = furphy + Chr(cc)
    Next i
    rigmarole = furphy
End Function

List string decrypt được là:

AppData
\Microsoft\stomp.mp3
play 
FLARE-ON
Sorry, this machine is not supported.
FLARE-ON
Error
winmgmts:\\.\root\CIMV2
SELECT Name FROM Win32_Process
vbox
WScript.Network
\Microsoft\v.png

Mình tạo 1 file excel mới, thêm VBA macro mới vào, copy các Form y chang từ file excel cũ để debug, ta sẽ phân tích hàm folderol.

If GetInternetConnectedState() = False Then
    MsgBox "Cannot establish Internet connection.", vbCritical, "Error"
    End
End If

Set fudgel = GetObject(rigmarole(onzo(7)))
Set twattling = fudgel.ExecQuery(rigmarole(onzo(8)), , 48)
For Each p In twattling
    Dim pos As Integer
    pos = InStr(LCase(p.Name), "vmw") + InStr(LCase(p.Name), "vmt") + InStr(LCase(p.Name), rigmarole(onzo(9)))
    If pos > 0 Then
        MsgBox rigmarole(onzo(4)), vbCritical, rigmarole(onzo(6))
        End
    End If
Next

Đoạn code trên kiểm tra xem máy có được kết nối internet hay không, ngoài ra còn kiểm tra trong list process xem có process nào có chứa “vmw”, “vmt”, “vbox” trong tên không, đây chính là cơ chế anti-vm của nó. (ví dụ, “vmware” có chứa “vmw”, còn cụm “vbox” có thể được tìm thấy nếu bạn dùng virtualbox).

xertz = Array(&H11, &H22, &H33, &H44, &H55, &H66, &H77, &H88, &H99, &HAA, &HBB, &HCC, &HDD, &HEE)
Set groke = CreateObject(rigmarole(onzo(10)))
firkin = groke.UserDomain
If firkin <> rigmarole(onzo(3)) Then
    MsgBox rigmarole(onzo(4)), vbCritical, rigmarole(onzo(6))
End
End If

Đoạn trên kiểm tra user domain có phải là “FLARE-ON” hay không, sau khi vượt qua các đoạn check, program sẽ decrypt 1 đoạn nhị phân và lưu ở “%appdata%\Microsoft\v.png”

thi5_cou1d_h4v3_b33n_b4d@flare-on.com

[+] Source code dùng để giải cho tất cả các bài nằm ở đây