一次对js混淆代码的美化过程
Page content
代码混淆(Obfuscated code)亦称花指令,是将计算机程序的代码,转换成一种功能上等价,但是难于阅读和理解的形式的行为。
import string
import random
from execjs import eval as jseval
def random_key(length=20):
return "".join(
(
random.choice(
random.choice(
(
string.ascii_uppercase,
string.ascii_lowercase,
)
)
)
for i in range(1, length)
)
)
import re
from base64 import b64decode
def convert_to_normal_string(text):
# decode \x??
for item in set(re.findall('\\\\x[a-zA-Z0-9]+',text)):
code = bytes.fromhex(item.replace('\\x','')).decode('utf8')
if code in {'"',"'"}:
code = '\\'+code
print(item,code)
text = text.replace(item,code)
# decode _0x????
datas2 = re.findall('_0x[a-zA-Z0-9]+',text)
length = len(str(len(datas2)))
for item in set(datas2):
code = random_key(length)
print(item,code)
text = text.replace(item,code)
for item in re.findall('0x[a-zA-Z0-9]+',text):
code = jseval(item)
print(item,code)
text = text.replace(item,str(code))
return text