6/recent/ticker-posts

What is obfuscation example?

 What is obfuscation example?

When downloading libraries on the Internet (especially Jquery, Javascript libraries), sometimes you will receive very small files, the content inside is minified and has no meaning at all. That is the technique of Obfuscated code.

What is Obfuscated Code?


This is a technique to answer the question I often get asked by new developers: "How do I hide my source code?". Try the example below:

Source: 

function redirectUrl(url, selectorString) {

    if (url !== '') {

        if (selectorString === null || (selectorString !== null && $(selectorString).val() != url)) {

            window.location.replace(url);

        }

    }

}

function removeData(row) {

    row.addClass("warning");

    row.fadeOut(400, function () {

        row.remove();

    });

}

var delay = (function () {

    var timer = 0;

    return function (callback, ms) {

        clearTimeout(timer);

        timer = setTimeout(callback, ms);

    };

})();

Obfuscated code:
eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return'\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\b'+e(c)+'\b','g'),k[c])}}return p}('1 g(3,4){6(3!==''){6(4===a||(4!==a&&$(4).d()!=3)){f.b.c(3)}}}1 e(2){2.l("n");2.o(h,1(){2.p()})}7 m=(1(){7 5=0;i 1(8,9){j(5);5=k(8,9)}})();',26,26,'|function|row|url|selectorString|timer|if|var|callback|ms|null|location|replace|val|removeData|window|redirectUrl|400|return|clearTimeout|setTimeout|addClass|delay|warning|fadeOut|remove'.split('|'),0,{}))
Obfuscated code format:
eval(function (p, a, c, k, e, d) {
    e = function (c) {
        return c.toString(36)
    };
    if (!''.replace(/^/, String)) {
        while (c--) {
            d[c.toString(a)] = k[c] || c.toString(a)
        }
        k = [function (e) {
            return d[e]
        }];
        e = function () {
            return '\w+'
        };
        c = 1
    }
    ;
    while (c--) {
        if (k[c]) {
            p = p.replace(new RegExp('\b' + e(c) + '\b', 'g'), k[c])
        }
    }
    return p
}('1 g(3,4){6(3!==''){6(4===a||(4!==a&&$(4).d()!=3)){f.b.c(3)}}}1 e(2){2.l("n");2.o(h,1(){2.p()})}7 m=(1(){7 5=0;i 1(8,9){j(5);5=k(8,9)}})();', 26, 26, '|function|row|url|selectorString|timer|if|var|callback|ms|null|location|replace|val|removeData|window|redirectUrl|400|return|clearTimeout|setTimeout|addClass|delay|warning|fadeOut|remove'.split('|'), 0, {}))

Why Obfuscated Code?

• Code size will be reduced.

• In Javascript, will make the download time significantly.

• Once the code is Obfuscated, it will be difficult to reverse the original format.

As you can see, with Obfuscated code, it not only minify (cuts off line breaks, unnecessary spaces to reduce file size, save bandwidth) but it also changes class name, name. functions, variable names, etc. into simple meaningless characters (eg: a(), var b,...). Therefore, Obfuscated code, also known as Vietnamese, is a technique to mess up code: other people can get your code, but it is difficult to understand the full meaning of the code you write. Obfuscated code is used in many languages, not just Javascript. Usually for public sources they will just minify to reduce the file size when loading. Obfuscated is only used when you want to improve performance further and especially to hide code. Hiding the code here is also not completely uncompileable, but it will take a lot of time.

Obfuscation == Encryption?

No, obfuscation != encryption.

• In JavaScript, the browser can execute encrypted code, while the browser will execute obfuscated code.

• Encrypted code always needs decryption to be executed.

• Obfuscated code does not require deserialization to execute.

When writing Obfuscator, you should pay attention to:

-Only change personal names, do not change keywords, command names, or system APIs

-Avoid name conflicts. For example, if the class name and variable name are the same, the program may run differently and even not understand what it is doing.

-Consistency. When you change the name, you must change all the source files that contain the same object. For example, file abc.jsp contains class name ABC, when renaming class ABC, all other files that use class ABC must also be changed.

-Make the new name as short as possible. Doing so to translate code faster, less effort for the translator

-Remove redundancy

How to Obfuscated Code?

Of course you can do it yourself by hand. Well, it's best to use libraries, software or some online tools because these tools have been written to be able to Obfuscated your code in the most optimal way. Some online tools:

• JavaScript HTML Code/Text Obfuscator

• Online Javascript Obfuscator DaftLogic

• Jsobfuscate

If your code is for the community, you just need to minify to reduce the file size, but if you still want to hide the code, Obfuscated code is an optimal choice for you. Thank you for reading this post.

=> Read More: What is obfuscation? How does obfuscation work?

Post a Comment

0 Comments