Files
typoraCracker/auto-analysis/utils.py
2022-04-03 20:05:35 +08:00

89 lines
2.5 KiB
Python

# -*- coding:utf-8 -*-
"""
@Author: Mas0n
@File: utils.py
@Time: 2022/4/3 18:36
@Desc: It's all about getting better.
"""
from loguru import logger as log
import subprocess
import json
import os
# Usage:
# innoextract
#
BASE_DIR = os.path.dirname(__file__)
DOWNLOAD_LINK = {
"win": {
"x86": "https://typora.io/windows/typora-setup-ia32.exe",
"x64": "https://typora.io/windows/typora-setup-x64.exe",
"arm": "https://typora.io/windows/typora-setup-arm64.exe",
},
"linux": {
"x64": "https://download.typora.io/linux/Typora-linux-x64.tar.gz",
"arm": "https://download.typora.io/linux/Typora-linux-arm64.tar.gz",
},
}
def get_version(to_path):
package_file_path = os.path.join(to_path, "app/resources/package.json")
package_info = open(package_file_path, "r").read()
package_obj = json.loads(package_info)
return package_obj["version"]
def download_file(from_link, to_path):
log.info(f"downloading from {from_link}")
subprocess.check_call(["wget", from_link, "-O", to_path])
log.info("ready extract package")
def extract_file(from_path, to_path):
subprocess.check_call(["innoextract", from_path, "-d", to_path])
log.info("preparation stage completed")
def patch_file(_key, _iv):
patch_file_path = os.path.join(BASE_DIR, "../typora.py")
fd = open(patch_file_path, "a")
content = fd.read()
content = content.replace("{AES_KEY}", f"b''.fromhex('{_key}')")
content = content.replace("{AES_IV}", f"b''.fromhex('{_iv}')")
fd.write(content)
def win_x64_run():
from win.x64 import analysis
basedir = os.path.join(BASE_DIR, "win/x64")
link = DOWNLOAD_LINK["win"]["x64"]
download_path = os.path.join(basedir, os.path.basename(link))
download_file(link, download_path)
extract_file(download_path, basedir)
main_node_path = os.path.join(basedir, "app/resources/app.asar.unpacked/main.node")
log.info("auto analysis start")
key, iv = analysis.get_aes_key_and_iv(main_node_path)
log.success("analysis done")
patch_file(key.hex(), iv.hex())
log.success("patch done")
if __name__ == '__main__':
win_x64_run()
# hashString = open("LATEST_VERSION", "r").read()
# if hashString == "":
# log.info("not history for typora version")
# exit()
# basedir = os.path.dirname(__file__)
# for h1 in DOWNLOAD_LINK.keys():
# h1dir = os.path.join(basedir, h1)
# for h2 in DOWNLOAD_LINK.get(h1).keys():
# h2dir = os.path.join(h1dir, h2)
# print(h2dir)