JSFuck

JSFuck is an esoteric subset of JavaScript, where code is written using only six characters:,  ,  ,  ,  , and. The name is derived from Brainfuck, an esoteric programming language that also uses a minimalistic alphabet of only punctuation. Unlike Brainfuck, which requires its own compiler or interpreter, JSFuck is valid JavaScript code, meaning that JSFuck programs can be run in any web browser or engine that interprets JavaScript. JSFuck is able to recreate all JavaScript functionality using such a limited set of characters because JavaScript allows the evaluation of any expression as any type.

History
In July 2009, Yosuke Hasegawa created a web application called jjencode which could encode arbitrary JavaScript into an obfuscated form utilizing only the 18 symbols. In January 2010, an informal competition was held in the "Obfuscation" forum of the sla.ckers.org web application security site to come up with a way to get the minimum number of characters required down to less than eight:. Contributors to the thread managed to eliminate the need for the  and   characters. As of March 2010, an online encoder called JS-NoAlnum was available which utilized only the final set of six characters. By the end of 2010, Hasegawa made a new encoder available named JSF*ck which also used only the minimum six characters. In 2012, Martin Kleppe created a "jsfuck" project on GitHub, and a JSFuck.com website with a web app using that implementation of the encoder.

JSFuck can be used to bypass detection of malicious code submitted on websites, e.g. in cross-site scripting (XSS) attacks. Another potential use of JSFuck lies in code obfuscation. An optimized version of JSFuck has been used to encode jQuery, a JavaScript library, into a fully functional version written with just the six characters.

Encoding methods
JSFuck code is extremely "verbose": In JavaScript, the code, which causes a pop-up window to open with the text "Hello world", is 21 characters long. In JSFuck, the same code has a length of 4325 characters. Certain single characters require far more than 1000 characters when expanded as JSFuck. This section offers an overview of how this expansion works.

Numbers
The number 0 is created by, where   is the empty array and   is the unary plus, used to convert the right side to a numeric value (zero here). The number 1 is formed as  or , where the boolean value   (expressed as   or   in JSFuck) is converted into the numeric value 1 by the prepended plus sign. The digits 2 to 9 are formed by summing  the appropriate number of times. E.g. in JavaScript  = 2 and   =   = , hence 2 can be written as   or. Other digits follow a similar pattern. Integers consisting of two or more digits are written, as a string, by concatenating 1-digit arrays with the plus operator. For example, the string  can be expressed in JavaScript as. By replacing the digits with the respective JSFuck expansions, this yields. To get a numeric value instead of a string, one would enclose the previous expression in parentheses or square brackets and prepend a plus, yielding  =.

Letters
Some letters can be obtained in JSFuck by accessing single characters in the string representations of simple boolean or numeric values like,  ,  ,   with an indexer (a number in square brackets). Other tricks are needed to produce other letters – for example by casting the string  into a number, which gives , which in turn makes the letter   accessible.

The following is a list of primitive values used as building blocks to produce the most simple letters.


 * {| class="wikitable"

! Value || JSFuck
 * ||  or
 * }
 * ||  or
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }

Example: Creating the letter "a"
The second character of "false" is a, which can be accessed with: Proof: In JavaScript,  does the same as.
 * 1)  .   can be made from , i.e. the boolean constant false plus an empty array.
 * : We write false as  (negation applied to an empty array).
 * : 1 is a number, we can write it as.
 * : Since false is, true is.
 * 1)   – which evaluates to "a".

Other constructs
The  constructor can be used to trigger execution of JavaScript code contained in a string as if it were native JavaScript. So, for example, the statement  is equivalent to. The  constructor can be retrieved in JSFuck by accessing the constructor property of a well known function, such as    or    in modern browsers. And then  becomes.

Character table
The characters with the shortest JSFuck expansions are listed below. Other UTF-8 characters can be expressed as well but will generate considerably longer code.


 * {| class="wikitable"

! Character || JSFuck or or or or or or or or or
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * }
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * }
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * style="text-align: center;" |  ||
 * }
 * style="text-align: center;" |  ||
 * }
 * }

Security
Lacking the distinct features of "usual" JavaScript, obfuscation techniques like JSFuck can assist malicious JavaScript code in bypassing intrusion prevention systems or content filters. For instance, the lack of alphanumeric characters in JSFuck and a flawed content filter allowed sellers to embed arbitrary JSFuck scripts in their eBay auction pages.