Replace array-mapped variables with the actual variable name/string?

前端 未结 3 2009
旧巷少年郎
旧巷少年郎 2020-12-31 14:25

I am trying to edit a Greasemonkey/jQuery script. I can\'t post the link here.
The code is obfuscated and compressed with minify.
It starts like this:



        
3条回答
  •  暖寄归人
    2020-12-31 14:48

    I haven't seen any online deobfuscator that does this yet, but the principle is simple.
    Construct a text filter that parses the "key" array and then replaces each instance that that array is referenced, with the appropriate array value.

    For example, suppose you have a file, evil.js that looks like this (AFTER you have run it though jsbeautifier.org with the Detect packers and obfuscators? and the Unescape printable chars... options set):

    var _0xf17f = ["(", ")", 'div', "createElement", "id", "log", "console"];
    var _0x41dcx3 = eval(_0xf17f[0] + '{id: 3}' + _0xf17f[1]);
    var _0x41dcx4 = document[_0xf17f[3]](_0xf17f[2]);
    var _0x41dcx5 = _0x41dcx3[_0xf17f[4]];
    window[_0xf17f[6]][_0xf17f[5]](_0x41dcx5);
    

    In that case, the "key" variable would be _0xf17f and the "key" array would be ["(", ")", ...].

    The filter process would look like this:

    1. Extract the key name using text processing on the js file. Result: _0xf17f
    2. Extract the string src of the key array. Result:

      keyArrayStr = '["(", ")", \'div\', "createElement", "id", "log", "console"]';
      
    3. In javascript, we can then use .replace() to parse the rest of the JS src. Like so:

    var keyArrayStr = '["(", ")", \'div\', "createElement", "id", "log", "console"]';
    var restOfSrc   = "var _0x41dcx3 = eval(_0xf17f[0] + '{id: 3}' + _0xf17f[1]);\n"
                    + "var _0x41dcx4 = document[_0xf17f[3]](_0xf17f[2]);\n"
                    + "var _0x41dcx5 = _0x41dcx3[_0xf17f[4]];\n"
                    + "window[_0xf17f[6]][_0xf17f[5]](_0x41dcx5);\n"
                    ;
    var keyArray    = eval (keyArrayStr);
    //-- Note that `_0xf17f` is the key name we already determined.
    var keyRegExp   = /_0xf17f\s*\[\s*(\d+)\s*\]/g;
    
    var deObsTxt    = restOfSrc.replace (keyRegExp, function (matchStr, p1Str) {
        return '"' + keyArray[ parseInt(p1Str, 10) ] + '"';
    } );
    console.log (deObsTxt);
    

    if you run that code, you get:

    var _0x41dcx3 = eval("(" + '{id: 3}' + ")");
    var _0x41dcx4 = document["createElement"]("div");
    var _0x41dcx5 = _0x41dcx3["id"];
    window["console"]["log"](_0x41dcx5);
    

    -- which is a bit easier to read/understand.


    I've also created an online page that takes JS source and does all 3 remapping steps in a slightly more automated and robust manner. You can see it at:

    jsbin.com/hazevo

    (Note that that tool expects the source to start with the "key" variable declaration, like your code samples do)

提交回复
热议问题