如果将JavaScript代码转化成能执行的二进制字符串,是不是很有意思呢?起码看起来会很酷,运行效果如下图:
在图中,可以看到二进制字符串被执行,并输出了结果。
这是如何实现的呢?
先看这段执行的代码:
const result = executeBinary("0000101001100110011101010110111001100011011101000110100101101111011011100010000001100111011001010111010001011111011000110110111101110000011110010111001001101001011001110110100001110100001010000010100101111011000010100010000000100000001000000010000001110110011000010111001000100000011001000110111101101101011000010110100101101110001000000011110100100000001000100110101001110011011010000110000101101101011000010110111000101110011000110110111101101101001000100011101100001010001000000010000000100000001000000111011001100001011100100010000001100110011100100110111101101101010111110111100101100101011000010111001000100000001111010010000000110010001100000011000100110111001110110000101000100000001000000010000000100000011101100110000101110010001000000110001101101111011100000111100101110010011010010110011101101000011101000010000000111101001000000010001000101000011000110010100100100010001000000010101100100000011001100111001001101111011011010101111101111001011001010110000101110010001000000010101100100000001000100010110100100010001000000010101100100000001010000110111001100101011101110010000001000100011000010111010001100101001010010010111001100111011001010111010001000110011101010110110001101100010110010110010101100001011100100010100000101001001000000010101100100000001000100010110000100010001000000010101100100000011001000110111101101101011000010110100101101110001110110000101000100000001000000010000000100000011100100110010101110100011101010111001001101110001000000110001101101111011100000111100101110010011010010110011101101000011101000011101100001010011111010000101001100011011011110110111001110011011011110110110001100101001011100110110001101111011001110010100001100111011001010111010001011111011000110110111101110000011110010111001001101001011001110110100001110100001010000010100100101001001110110000101000001010");
// 执行二进制字符串function executeBinary(binary) {console.log("二进制字符串:",binary);// 将二进制字符串按每 8 位截断,并将其转换成字节数组const bytes = binary.match(/.{8}/g).map((byte) => parseInt(byte, 2));// 创建 ArrayBufferconst buffer = new ArrayBuffer(bytes.length);// 将字节数组写入 ArrayBuffer 中const uint8Array = new Uint8Array(buffer);bytes.forEach((byte, index) => (uint8Array[index] = byte));// 使用 TextDecoder 解码 ArrayBuffer 中的内容const code = new TextDecoder().decode(buffer);console.log("执行结果:");// 执行还原后的 JS 代码return (new Function(code))();}
由代码可知,这确实是一段二进制字符,但显然二进制是不能直接执行的,它是由正常代码编码而成的,执行前,先需将二进制转化为原始代码。然后,用new Function的方式转成立即执行函数并运行。相当于是一种看起来比较绚丽的加密算法。
但就代码形式而言,执行函数相对于0101的二进制形式太异类,因此,我们可以将上面的executeBinary函数代码用JShaman进行混淆加密,使它变的不那么正规,使整体代码看起来都很怪异。如下图:
而二进制字符串的得来也比较简单,代码如下:
// 示例 JS 代码const jsCode = `function get_copyright(){ var domain = "jshaman.com"; var from_year = 2017; var copyright = "(c)" + from_year + "-" + (new Date).getFullYear() + "," + domain; return copyright;}console.log(get_copyright());`;// 将 JS 代码转换为二进制字符串function convertToBinary(code) { let binary = ""; for (let i = 0; i < code.length; i++) { const charCode = code.charCodeAt(i); const charBinary = charCode.toString(2).padStart(8, "0"); binary += charBinary; } return binary;}const binaryCode = convertToBinary(jsCode);// 输出二进制字符串console.log(binaryCode);
即:将JS代码做为字符串,转换成二进制形式。