Sunday, June 10, 2012

How can I obfuscate JavaScript?


I want to make a JavaScript application that's not open source, and thus have two questions:



  1. What's the best way to obfuscate the code?

  2. What's the best way to obfuscate the strings themselves within the application (assuming that the answer to #1 does not handle this)?


Source: Tips4all

24 comments:

  1. Obfuscation:

    Try YUI Compressor. It's a very popular tool, built, enhanced and maintained by the Yahoo UI team.

    You may also use:
    Google Closure Compiler
    UglifyJS

    Private String Data:

    Keeping string values private is a different concern, and obfuscation won't really be of much benefit. Of course, by packaging up your source into a garbled, minified mess, you have a light version of security through obscurity. Most of the time, it's your user who is viewing the source, and the string values on the client are intended for their use, so that sort of private string value isn't often necessary.

    If you really had a value that you never wanted a user to see, you would have a couple of options. First, you could do some kind of encryption, which is decrypted at page load. That would probably be one of the most secure options, but also a lot of work which may be unnecessary. You could probably base64 encode some string values, and that would be easier.. but someone who really wanted those string values could easily decode them. Encryption is the only way to truly prevent anyone from accessing your data, and most people find that to be more security than they need.

    Sidenote:
    Obfuscation in Javascript has been known to cause some bugs.. The obfuscators are getting a little better about it, but many outfits decide that they see enough benefit from minifying and gzipping, and the added savings of obfuscation isn't always worth the trouble. If you're trying to protect your source, maybe you'll decide that it's worth your while, just to make your code harder to read. JSMin is a good alternative.

    ReplyDelete
  2. I'm surprised no one has mentioned Google's Closure Compiler. It doesn't just minify/compress, it analyzes to find and remove unused code, and rewrites for maximum minification. It can also do type checking and will warn about syntax errors.

    JQuery recently switched from YUI Compresser to Closure Compiler, and saw a "solid improvement"

    ReplyDelete
  3. Obfuscation can never really work. For anyone who really wants to get at your code, it's just a speed bump. Worse, it keeps your users from fixing bugs (and shipping them back to you) and makes it harder for you to diagnose problems in the field. Its a waste of your time and money.

    Write a license and get a lawyer to go after violators.

    The only way you can really protect your code is to not ship it. Move the important code server-side and have your public Javascript code do Ajax calls to it.

    See my full answer about obfuscators here.

    ReplyDelete
  4. There are a number of JavaScript obfuscation tools that are freely available; however, I think it's important to note that it is difficult to obfuscate JavaScript to the point where it cannot be reverse-engineered.

    To that end, there are several options that I've used to some degree overtime:


    YUI Compressor. Yahoo!'s JavaScript compressor does a good job of condensing the code that will improve its load time. There is a small level of obfuscation that works relatively well. Essentially, Compressor will change function names, remove white space, and modify local variables. This is what I use most often. This is an open-source Java-based tool.
    JSMin is a tool written by Douglas Crockford that seeks to minify your JavaScript source. In Crockford's own words, "JSMin does not obfuscate, but it does uglify." It's primary goal is to minify the size of your source for faster loading in browsers.
    Free JavaScript Obfuscator. This is a web-based tool that attempts to obfuscate your code by actually encoding it. I think that the trade-offs of its form of encoding (or obfuscation) could come at the cost of filesize; however, that's a matter of personal preference.

    ReplyDelete
  5. You can obfuscate the javascript source all you want, but it will always be reverse-engineerable just by virtue of requiring all the source code to actually run on the client machine... the best option I can think of is having all your processing done with server-side code, and all the client code javascript does is send requests for processing to the server itself. Otherwise, anyone will always be able to keep track of all operations that the code is doing.

    Someone mentioned base64 to keep strings safe. This is a terrible idea. Base64 is immediately recognizable by the types of people who would want to reverse engineer your code. The first thing they'll do is unencode it and see what it is.

    ReplyDelete
  6. The problem with interpreted languages, is that you send the source to get them working (unless you have a compiler to bytecode, but then again, it is quite trivial to decompile).

    So, if you don't want to sacrifice performance, you can only act on variable and function names, eg. replacing them with a, b... aa, ab... or a101, a102, etc. And, of course, remove as much space/newlines as you can (that's what so called JS compressors do).
    Obfuscating strings will have a performance hit, if you have to encrypt them and decrypt them in real time. Plus a JS debugger can show the final values...

    ReplyDelete
  7. Contrary to most of the other answers I suggest against YUI Compressor; you should use Google Closure.

    Not much because it compresses more, but mostly because it will catch javascript errors such as a = [1,2,3,]; which make IE go haywire.

    ReplyDelete
  8. A non-open-source Javascript-based application is fairly silly. Javascript is a client-side interpreted language.. Obfuscation isn't much protection..

    JS obfuscation is usually done to reduce the size of the script, rather than "protect" it. If you are in a situation where you don't want your code to be public, Javascript isn't the right language..

    There are plenty of tools around, but most have the word "compressor" (or "minifier") in its name for a reason..

    ReplyDelete
  9. Try JScrambler. I gave it a spin recently and was impressed by it.
    It provides a set of templates for obfuscation with predefined settings for those who dont care much about the details and just want to get it done quickly. You can also create custom obfuscation by choosing whatever transformations/techniques you want.

    ReplyDelete
  10. I can recommend JavaScript Utility by Patrick J. O'Neil. It can obfuscate/compact and compress and it seems to be pretty good at these. That said, I never tried integrating it in a build script of any kind.

    As for obfuscating vs. minifying - I am not a big fan of the former. It makes debugging impossible (Error at line 1... "wait, there is only one line") and they always take time to unpack. But if you need to... well.

    ReplyDelete
  11. I am using Closure-Compiler utility for the java-script obfuscation. It minifies the code and has more options for obfuscation.
    This utility is available at Google code at below URL:
    http://code.google.com/closure/compiler/

    But now a days I am hearing much of UglifyJS. You can find various comparison between Closure Compiler and UglifyJS in which Uglify seems to be a winner.http://badassjs.com/post/971960912/uglifyjs-a-fast-new-javascript-compressor-for-node-js.

    Soon I would give chance to UglifyJS.

    Thanks
    Shailendra

    ReplyDelete
  12. This one minifies but doesn't obfuscate. If you don't want to use command line Java you can paste your javascript into a webform.

    ReplyDelete
  13. I'm under the impression that some enterprises (e.g.: JackBe) put encrypted JavaScript code inside *.gif files, rather than JS files, as an additional measure of obfuscation.

    ReplyDelete
  14. Obfuscation is not protection nor compression. Compression is a way to obfuscation.

    ReplyDelete
  15. I would suggest first minify with something like YUI Compressor, and then convert all string and numbers to HEX Values using something like http://www.javascriptobfuscator.com/

    With this, the code would be rendered near impossible to understand and I think at this Stage it will take more time for a Hacker to re-enact your code than actually if he re-wrote from scratch. Rewriting and Cloning is what you cant actually stop. After all we are free-people !

    ReplyDelete
  16. You definitely should consider taking a look at Obfuscriptor.

    I goes beyond the typical Javascript minifying tricks we've seen from other tools such as YUI Compressor or Google Closure.

    The obfuscated code looks more like encrypted. Unlike anything I've seen before.

    ReplyDelete
  17. As a JavaScript/HTML/CSS obfuscator/compressor you can also try Patu Digua.

    ReplyDelete
  18. If you use a JavaScript library, consider Dojo Toolkit which is compatible (after minor modifications) with the Closure Compiler's Advanced mode compilation.

    http://dojo-toolkit.33424.n3.nabble.com/file/n2636749/Using_the_Dojo_Toolkit_with_the_Closure_Compiler.pdf?by-user=t

    Code compiled with Closure Advanced mode is almost impossible to reverse-engineer, even passing through a beautifier, as the entire code base (includinhg the library) is obfuscated. It is also 25% small on average.

    JavaScript code that is merely minified (YUI Compressor, Uglify etc.) is easy to reverse-engineer after passing through a beautifier.

    ReplyDelete
  19. You could try this as an alternative as well.

    http://tools.2vi.nl/

    ReplyDelete
  20. Dean Edward's Packer is an excellent obfuscator, though it primarily obfuscates the code, not any string elements you may have within your code.

    See: http://jscompress.com/ and select Packer (Dean Edwards) from the dropdown

    ReplyDelete
  21. I've used this in the past, and it does a good job. It's not free, but you should definitely take a look. http://www.stunnix.com/prod/jo/

    ReplyDelete
  22. Try this tool Javascript Obfuscator

    I used it on my HTML5 game not only it reduced it size from 950KB to 150 but also made the source code unreadable closure compilers and minifiers are reversable I personally dont know how to reverse this obfuscation.

    ReplyDelete
  23. I've been using Jasob for years and it is hands down the best obfuscator out there.
    It has an advanced UI but is still intuitive and easy to use.
    It will also handle HTML and CSS files.

    The best way to use it is to prefix all of your private variables with something like an underscore, then use the sort feature to group them all together and check them off as targets for obfuscation.

    Users can still view your source, but it's much more difficult to decipher when your private variables are converted from something like _sUserPreferredNickName to a.

    The engine will automatically tally up the number of targeted variables and prioritize them to get the maximum compression.

    I don't work for Jasob and I get nothing out of promoting them, just offering some friendly advice.
    The downside is that it's not free and is a little pricey, but still worth it when stacked against alternatives - the 'free' options don't even come close.

    ReplyDelete
  24. There is an alpha version of an Obfuscator compressor pretty good, you cna report bug if after obfuscation your code not run.

    http://javaencrypt.com/javascript-obfuscator/index.php?lang=en

    ReplyDelete