Reverse-engineering an obfuscated JavaScript file and lessons learned
Yesterday, I got an email from Dropbox saying that somebody had shared their Bitcoin wallet with me.
This looks like an accident, right? And too good to be true? Probably is.
Email appeared to really come from Dropbox. The sending IP was allowed by the SPF policies (of course, that’s no guarantee, but that’s not what I wanted to spend time on).
I got curious and downloaded the file. It had the .zip
file extension but turned out to actually be a RAR-archive (weird). I unrar-ed it and got two files, one .js
and one .exe
. I decided to take a look on the JavaScript file, never having de-obfuscated JavaScript before.
Here is a gist with both my cleaned-up version of the file and the original obfuscated JavaScript code.
Some JavaScript sourcery used.
The obfuscated code relies on JavaScript idiosyncrasies, most of which were unfamiliar to me, since I’m not a JS developer.
In JavaScript, you can call methods on objects with instance["method"]()
, allowing you to pass the method name as data. After discovering the encryption-function, this was a common pattern.
In fairness to JavaScript, most other interpreted languages have this as well (for Python, see __getattr__
).
There were a lot of functions with random names (e.g. bCCg()
) that accepted no arguments but returned one character only. Sometimes they had wrapped the character inside an array and returned a specific element of the array. Example:
which translates to simply returning "f"
.
Another JS-“feature” that was used to hide data was concatenating (using the +
operator) a tuple and a string. In JS, of course the last element of the tuple is concatenated to the string. Oh, and if it happens to be an integer, it’s implicitly parsed to it’s string representation. This all makes total sense, right?
In case it doesn’t, here’s an example.
And an example usage is the function that started out as
After cleaning it up, it looks like this.
It should be obvious that YWzqkGP()
is just a very complicated way of writing "charCodeAt"
.
A lot time is spent maximizing the code length. This example is AFTER cleaning up the tricks described above.
We see that all calls to rFeBmU()
can be replace with "fromCharCode"
. Smells like code, probably to be injected with the instanceName["methodName"]
method.
What is it trying to do?
This obfuscated javascript features a fairly complicated encryption function, which I have not yet reverse engineered.
Instead, I isolated it and fed the encrypted strings to it to decrypt them. Seemed easier. This line was interesting to me
The code is trying to load WScript via ActiveX, which according to the documentation can “output information to the default output device (either a Windows dialog box or the command console)”.
After de-obfuscating the last big function, this is what I ended up with.
They are trying to download a .exe
file, save it and execute it. How classic. I have on idea what the purpose of the empty GMU
function is though.
Aftermath
I contacted Dropbox and the hosting provider in the WHOIS record for the IP address in the URL.