Exploring JSFuck: A Novel Approach to Obfuscating JavaScript Code – Hacker

Let's put aside dry technical pragmatics and talk about beauty. You may ask: what could be great about code obfuscation? I will answer: some solutions in this area have downright aesthetically enchanting beauty, accessible only to true connoisseurs. True, sometimes the names are not particularly euphonious. Today we will talk about JSFuck, a JavaScript obfuscation and deobfuscation tool.

The other day I caught my eye article about obfuscation and deobfuscation of JavaScript applets. This topic, although unpromising, is quite relevant: I myself once touched on it in my material “Patching JSXBIN. How to edit Adobe binary scripts without recompilation” in relation to Adobe's implementation of JavaScript in the ExtendScript Toolkit. So, the article reminded me of an old, but undeservedly forgotten solution in this area of ​​​​the national economy under the indecent name JSFuck.

This was also mentioned in Hacker a couple of times about eight years ago in relation to the eBay vulnerability and obfuscated code.

To be fair, it should be noted that JSFuck was then remembered solely for its hype name, since hackers used a wide variety of obfuscators to attack eBay, and JSFuck was by no means the most common there. Somehow it turned out that no one really described the principle of operation of this wonderful algorithm, referring to the author’s resource jsfuck.com. In today’s article I will try, although it’s late, to correct this omission.

It all started with the fact that back in 2009, Yosuke Hasegawa (I don’t know what to correctly call these Japanese lovers of the strange and specific ;)) created the jjencode web application, which transcodes any JavaScript code into an equivalent executable form, which only uses 18 special characters: ()()!+,"$.:;_{}~=.

At first glance, this seems like something incredible – a high-level procedural object-oriented language can be compressed into a subset of 18 special characters, and even while preserving all the original names of variables, classes, methods, and so on…

Imagine, for example, a C program that consists only of special characters, but at the same time compiles perfectly and works correctly. To be honest, personally, when I first heard about this, I didn’t believe it at first either, I thought it was about some kind of add-on to the interpreter, encrypting and transcoding the source text or even subcompiling it into bytecode. As, for example, in my article about PHP “Bypassing the guards. Debugging PHP code packaged with SourceGuardian.” Or even about a custom JS implementation similar to Adobe’s.

At that time, this idea impressed everyone so much that literally six months later on the obfuscators forum sla.ckers.org A competition was held, the goal of which was to further minimize the subset of symbols used. And indeed, this very subset was reduced by more than half, to eight characters: ()()!+,/and in March 2010 the symbols were also excluded from it ,/.

The resulting minimal subset of the six remaining characters was used in the online encoder JS-NoAlnum, on the basis of which Hasegawa created the JSFuck encoder at the end of the same year. And a little later, Martin Kleppe published the project on GitHub and made a website jsfuck.com with a description of the algorithm.

The most impatient readers can immediately go there to get acquainted with the algorithm, so to speak, directly from the horse’s mouth; for the rest, I will continue my story. We must give Hasegawa his due: he did not call his project such a hooligan out of any perverted motives. The name comes from Brainfuck (translated as “brain blower”), an esoteric programming language in which programs are encoded with a similar set of special characters. The allusion is also added by the fact that the predecessor of the Brainfuck language is an esoteric language P'' also uses six special characters in its semantics.

However, these unusual programming languages ​​required compilers or interpreters written in other languages ​​to run, and JSFuck excels at interpreting itself using the browser's built-in JavaScript engine interpreter. Let's begin the session of exposing black magic.