Lua xxtea 解密脚本

2020-03-10  本文已影响0人  大发明家达文西

众所周知,使用 cocos 引擎编写的游戏,如果直接使用 xxtea 加密 Lua 脚本,实际上是很容易被解密的,网上有很详细的解密教程了。找了一下解密程序,大多是用 xxtea 的 cpp 库直接编译成二进制文件,然后再调用脚本批量解密,感觉麻烦,所以这里直接放出一个比较 clean 的脚本。


  1. 用 ida 打开 libcocos2dlua,定位 sign 与 key ,一般是连在一起的:

    key.png
  2. pip 安装 xxtea python库

    需要注意的是必须安装 xxtea-py 库 xxtea-py .png
    不能安装下面这个 xxtea 库,因为这个库只支持16字节的秘钥,很多情况下就不能用了 xxtea
  3. 脚本如下,改一下key 与 sign ,然后修改一下输入输出的绝对路径,就会递归解密

import xxtea
import os


def decrypt_xxtea_file(input_dir, output_dir, xxtea_key, xxtea_sign):
    print("Begin to decrypt xxtea files in dir : " + input_dir)

    # is original directory valid
    if not os.path.isdir(input_dir):
        print("Not a valid directory path")
        return

    # is output directory valid
    if os.path.isfile(output_dir):
        os.remove(output_dir)
    if not os.path.isdir(output_dir):
        os.mkdir(output_dir)

    files = os.walk(input_dir)
    for path, dir_list, file_list in files:
        for directory in dir_list:
            relevant_path = os.path.relpath(os.path.join(path, directory), input_dir)
            new_path = os.path.join(output_dir, relevant_path)
            if os.path.isfile(new_path):
                os.remove(new_path)
            if not os.path.isdir(new_path):
                os.mkdir(new_path)

        for file in file_list:
            # 原文件绝对路径
            orig_path = os.path.join(path, file)
            # 源文件相对路径,方便计算解密文件路径
            relevant_path = os.path.relpath(orig_path, input_dir)
            # 解密文件绝对路径
            new_path = os.path.join(output_dir, relevant_path)
            # 读取原文件
            orig_file = open(orig_path, "rb")
            encrypt_bytes = orig_file.read()
            orig_file.close()
            # 解密文件
            decrypt_bytes = xxtea.decrypt(encrypt_bytes[len(xxtea_sign):], xxtea_key)
            new_file = open(new_path, "wb")
            new_file.write(decrypt_bytes)
            new_file.close()
            print("Done with " + orig_path)
    # decrypted
    print("\r\ndecrypt done")


if __name__ == '__main__':
    in_dir = "/input/scripts/directory/"
    out_dir = "/output/scripts/directory/"
    key = "your_key"
    sign = "your_sign"
    decrypt_xxtea_file(in_dir, out_dir, key, sign)
上一篇 下一篇

猜你喜欢

热点阅读