Introduction
During my analysis of the DarkGate malware, I asked the Internet to analyze the AutoIt script, but to no avail. So I decided to automate the shellcode extraction from the initial VBS script.
VBS Script
The first step of this infection involves a VBS script. It is slightly obfuscated, but its functionality is clear:
|
|
The script contacts a URL and stores the result in a variable. Here is the result in my case:
|
|
This is a cmd command that performs the following actions:
- Copies the curl executable to the current folder
- Downloads a legitimate AutoIT3 executable from the URL
- Downloads an AutoIT3 script from the URL
- Executes this script
AutoIt Script
AutoIt3 is a scripting language often used by malware operators to drop executables.
We can open this file with the AutoIt script editor to analyze its code. However, the code does not appear. It is actually a .a3x
script (compiled version of AutoIt) with hundreds of lines of useless data appended to it:
To extract this script, I used binary-refinery, a Python library for interacting with files and performing various operations.
|
|
This command extracts the script from the .a3x
document and decompiles it to give us access to the plain script that we can analyze.
This script is again slightly obfuscated by adding unnecessary variables and converting some strings into binary format. Here is the cleaned script:
|
|
Firstly, there are two API calls, VirtualProtect
and CallWindowProc
, which are part of a callback injection technique. There is also a check for the presence of the Sophos
antivirus on the machine.
The most interesting part happens in the DECRYPTFILEWITHKEY
function, where a file seems to be decrypted.
|
|
The program searches for data between two strings (padding), which corresponds to binary data in the original nbquok.au3
file.
Once the binary blob is extracted, the script will perform an operation on the darkgate
key to create a key used to decrypt the shellcode.
|
|
I replicated this operation in Python, which gives us a key of 0xB
:
|
|
Finally, the program decrypts the shellcode with the key 0xB
and executes it using the CallWindowProc
callback. I automated the extraction of this shellcode in Python with this script:
|
|