这个模糊的javascript代码是如何工作的?
我遇到了一个神秘的jQuery,我很想了解它是如何工作的 同时检查以下代码:这个模糊的javascript代码是如何工作的?,javascript,deobfuscation,Javascript,Deobfuscation,我遇到了一个神秘的jQuery,我很想了解它是如何工作的 同时检查以下代码: $ = ~ []; $ = { ___: ++$, $$$$: (![] + "")[$], __$: ++$, $_$_: (![] + "")[$], _$_: ++$, $_$$: ({} + "")[$], $$_$: ($[$] + "")[$], _$$: ++$, $$$_: (!"" + "")[$], $__: ++$,
$ = ~ [];
$ = {
___: ++$,
$$$$: (![] + "")[$],
__$: ++$,
$_$_: (![] + "")[$],
_$_: ++$,
$_$$: ({} + "")[$],
$$_$: ($[$] + "")[$],
_$$: ++$,
$$$_: (!"" + "")[$],
$__: ++$,
$_$: ++$,
$$__: ({} + "")[$],
$$_: ++$,
$$$: ++$,
$___: ++$,
$__$: ++$
};
$.$_ = ($.$_ = $ + "")[$.$_$] + ($._$ = $.$_[$.__$]) + ($.$$ = ($.$ + "")[$.__$]) + ((!$) + "")[$._$$] + ($.__ = $.$_[$.$$_]) + ($.$ = (!"" + "")[$.__$]) + ($._ = (!"" + "")[$._$_]) + $.$_[$.$_$] + $.__ + $._$ + $.$;
$.$$ = $.$ + (!"" + "")[$._$$] + $.__ + $._ + $.$ + $.$$;
$.$ = ($.___)[$.$_][$.$_];
$.$($.$($.$$ + "\"" + $.$_$_ + (![] + "")[$._$_] + $.$$$_ + "\\" + $.__$ + $.$$_ + $._$_ + $.__ + "(\\\"\\" + $.__$ + $.__$ + $.___ + "\\" + $.__$ + $.$_$ + $.__$ + "!\\\");" + "\"")())();
有人能把这段代码逐行分解并解释每一行是如何工作的吗?jQuery在第一行代码中变得无关紧要:
$ = ~[];
这将导致-1
。紧接着,您会看到一个对象被指定给$
同时引用它,如:
___: ++$, // $.___ is now 0
$$$$: (![] + "")[$], // $.$$$$ is f (false[0] = f)
__$: ++$, // 1
$_$_: (![] + "")[$], // a (false[1] = a)
_$_: ++$, // 2
$_$$: ({} + "")[$], // b ("[object Object]"[2])
$$_$: ($[$] + "")[$], // d ("undefined"[2])
_$$: ++$, // 3
$$$_: (!"" + "")[$], // e ("true"[3])
$__: ++$, // 4
$_$: ++$, // 5
$$__: ({} + "")[$], // c ("[object Object]"[5])
$$_: ++$, // 6
$$$: ++$, // 7
$___: ++$, // 8
$__$: ++$ // 9
等等。。。如果您一块一块地执行它,您将看到Javascript的行为。最终,您会发现您的代码调用发出警报,方法是通过这些求值生成js输出值,并将它们组合到警报中
这个问题让我想起了如果您注意到,通过立即将值赋值给$
($
变为整数),jquery实际上从未在这段代码中使用过
// ~ is bitwise not, so this set $ = -1
$ = ~ [];
下面的代码创建一个javascript对象。自从<代码>![]
为false
,(![]+“”)
将布尔值转换为字符串,为“false”
。每个[$]
都在指定索引$
处获取字符串中的一个字母“false”
或各种其他返回值。代码在对象中存储一系列整数和字母,然后将其分配给$
$ = {
___: ++$, // 0 since $ was -1
$$$$: (![] + "")[$], // "f"
__$: ++$, // 1
$_$_: (![] + "")[$], // "a"
_$_: ++$, // 2
$_$$: ({} + "")[$],
$$_$: ($[$] + "")[$], // "b"
_$$: ++$, // see the patter when ++$ is assigned?
$$$_: (!"" + "")[$], // see the pattern with the letters?
$__: ++$,
$_$: ++$,
$$__: ({} + "")[$],
$$_: ++$,
$$$: ++$,
$___: ++$,
$__$: ++$
};
下面的想法与上面的类似。每个片段(由+分隔)根据返回值返回一个字母,然后将其组合成一个字符串
// "constructor"
$.$_ = ($.$_ = $ + "")[$.$_$] + ($._$ = $.$_[$.__$]) + ($.$$ = ($.$ + "")[$.__$]) + ((!$) + "")[$._$$] + ($.__ = $.$_[$.$$_]) + ($.$ = (!"" + "")[$.__$]) + ($._ = (!"" + "")[$._$_]) + $.$_[$.$_$] + $.__ + $._$ + $.$;
下面的代码指定返回“
和函数函数(){[native code]}
// "return"
$.$$ = $.$ + (!"" + "")[$._$$] + $.__ + $._ + $.$ + $.$$;
// function Function() { [native code] }
$.$ = ($.___)[$.$_][$.$_];
在最后一行中
$.$$ + "\"" + $.$_$_ + (![] + "")[$._$_] + $.$$$_ + "\\" + $.__$ + $.$$_ + $._$_ + $.__ + "(\\\"\\" + $.__$ + $.__$ + $.___ + "\\" + $.__$ + $.$_$ + $.__$ + "!\\\");" + "\""`
相当于
"return"ale\162t(\"\110\151!\");""
当传递到$.$()
时,它将成为一个函数
function anonymous() { return "alert(\"Hi!\");"; }
然后是实际执行警报的最后一条语句
$.$($.$($.$$ + "\"" + $.$_$_ + (![] + "")[$._$_] + $.$$$_ + "\\" + $.__$ + $.$$_ + $._$_ + $.__ + "(\\\"\\" + $.__$ + $.__$ + $.___ + "\\" + $.__$ + $.$_$ + $.__$ + "!\\\");" + "\"")())();
为了使整个概念更容易理解,我还提供了半脱臭代码
obj = {
___: ++index, // = 0
$$$$: (![] + "")[index], // = "f" from 'f'alse
__$: ++index, // = 1
$_$_: (![] + "")[index], // = "a" from f'a'lse
_$_: ++index, // = 2
$_$$: ({} + "")[index], // = "b" from [o'b'ject Object]
$$_$: (index[index] + "")[index], // = "d" from un'd'efined
_$$: ++index, // = 3
$$$_: (!"" + "")[index], // = "e" from tru'e'
$__: ++index, // = 4
$_$: ++index, // = 5
$$__: ({} + "")[index], // = "c" from [obje'c't Object]
$$_: ++index, // = 6
$$$: ++index, // = 7
$___: ++index, // = 8
$__$: ++index // = 9
};
// obj.$_ = "c" + "o" + "n" + "s" + "t" + "r" + "u" + "c" + "t" + "o" + "r";
obj.$_ = (obj.$_ = obj + "")[obj.$_$] + (obj._$ = obj.$_[obj.__$]) + (obj.$$ = (obj.$ + "")[obj.__$]) + ((!obj) + "")[obj._$$] + (obj.__ = obj.$_[obj.$$_]) + (obj.$ = (!"" + "")[obj.__$]) + (obj._ = (!"" + "")[obj._$_]) + obj.$_[obj.$_$] + obj.__ + obj._$ + obj.$;
// obj.$$ = "r" + "e" + "t" + "u" + "r" + "n"
obj.$$ = obj.$ + (!"" + "")[obj._$$] + obj.__ + obj._ + obj.$ + obj.$$;
// obj.$ = function Function() { [native code] } <- a function constructor
obj.$ = (obj.___)[obj.$_][obj.$_];
// If you don't know what Function() is, read
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function to learn about Function()
// before continuing. It'll make more sense.
// body1 = "return"ale\162t(\"\110\151!\");"";
body1 = obj.$$ + "\"" + obj.$_$_ + (![] + "")[obj._$_] + obj.$$$_ + "\\" + obj.__$ + obj.$$_ + obj._$_ + obj.__ + "(\\\"\\" + obj.__$ + obj.__$ + obj.___ + "\\" + obj.__$ + obj.$_$ + obj.__$ + "!\\\");" + "\"";
// body2 = "alert("Hi!");" since \162 = "r", \"\110\151\!" = "Hi!"
body2 = obj.$(body1)();
// calls "alert("Hi!");"
obj.$(body2)();
// This works because the last argument to Function() becomes the
// body of that function.
obj={
___:++索引,//=0
$$$:(![]+“”)[索引],//=“f”中的“f”
__$:++索引,//=1
美元:(![]+“”)[索引],//=“a”来自f'a'lse
_$\:++索引,//=2
$\u$:({}+“”)[index],/=[o'b'ject对象]中的“b”
$$\$:(索引[索引]+“”)[索引],//=“d”来自未定义
_$$:+索引,//=3
$$$\(!“”+“”)[index],//=“e”来自tru'e'
$\:++索引,//=4
$\u$:++索引,//=5
$$\:({}+“”)[index],/=[obje'c't Object]中的“c”
$$\:++索引,//=6
$$:++索引,//=7
$\:++索引,//=8
$\$:++索引//=9
};
//对象$\=“c”+“o”+“n”+“s”+“t”+“r”+“u”+“c”+“t”+“o”+“r”;
(obj.OJ...$$$....$$$(obj.OJ.....$......$...$...$.........$$$........$$$$.........................[[[obj.........................[[[[日本....................................................................[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]+obj._u;+obj.u$+obj.$;
//对象$$=“r”+“e”+“t”+“u”+“r”+“n”
obj.$$=obj.$+(!“”+)[obj.\u$$]+obj.\uUUJ+obj.\uUJ+obj.$+obj.$$;
//obj.$=function(){[native code]}@Andy:如果有人能解释的话,他只是想理解这一点。代码没有问题,我guess@Andy我仍然认为这应该在这里,所以它是与人们分享的,它是关于分享和学习的。@htatch,分享什么?这是一篇绝技文章,并不是一个具体的问题。只是说:为了调试的目的,你可以添加调试器代码>位于代码顶部。如果您打开了开发人员工具,它将自动停止在该行。@htatch:这些帖子不是新的。与其说它是编程问题,不如说它是OP好奇的一个难题。我敢肯定,如果他找一找,用它胡闹一番,谜题的答案就会找到。这就是谜题的重点,不是吗?