吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1564|回复: 14
收起左侧

[Python 原创] python实现批量对代码可读化操作,方便逆向分析

  [复制链接]
shiquda 发表于 2023-8-27 21:55
本帖最后由 shiquda 于 2023-8-27 22:13 编辑

Github项目链接:https://github.com/shiquda/code-readable
code-readable
[Python] 纯文本查看 复制代码
import os
import json
from cssbeautifier import beautify as css_beautify
from bs4 import BeautifulSoup
from jsbeautifier import beautify as js_beautify

base_dir = r"path\to\your\dir" # 你的文件夹路径

class btf:
    def __init__(self, input_file, output_file):
        self.input_file = input_file
        self.output_file = output_file

    def js(self):
        with open(self.input_file, "r", encoding='utf-8') as f:
            compressed_code = f.read()
        uncompressed_code = js_beautify(compressed_code)
        with open(self.output_file, "w", encoding='utf-8') as f:
            f.write(uncompressed_code)
        print(f"处理完成:\t{self.output_file}")

    def css(self):
        with open(self.input_file, "r", encoding='utf-8') as f:
            compressed_code = f.read()
        # 使用 css_beautify 格式化 CSS
        uncompressed_code = css_beautify(compressed_code)
        with open(self.output_file, "w", encoding='utf-8') as f:
            f.write(uncompressed_code)
        print(f"处理完成:\t{self.output_file}")

    def html(self):
        with open(self.input_file, "r", encoding='utf-8') as f:
            compressed_code = f.read()
        soup = BeautifulSoup(compressed_code, 'html.parser')
        uncompressed_code = soup.prettify()
        with open(self.output_file, "w", encoding='utf-8') as f:
            f.write(uncompressed_code)
        print(f"处理完成:\t{self.output_file}")

    def json(self):
        with open(self.input_file, "r", encoding='utf-8') as f:
            compressed_code = f.read()
        # 不需要将 JSON 字符串转换成对象再转回字符串
        uncompressed_code = json.dumps(json.loads(compressed_code), indent=2, ensure_ascii=False)
        with open(self.output_file, "w", encoding='utf-8') as f:
            f.write(uncompressed_code)
        print(f"处理完成:\t{self.output_file}")

def walk_dir(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            input_path = os.path.join(root, file)
            output_path = os.path.join(root, file)
            if file.endswith(".js"):
                print(f'正在处理文件:\t{input_path}')
                btf(input_path, output_path).js()
            elif file.endswith(".css"):
                print(f'正在处理文件:\t{input_path}')
                btf(input_path, output_path).css()
            elif file.endswith(".html"):
                print(f'正在处理文件:\t{input_path}')
                btf(input_path, output_path).html()
            elif file.endswith(".json"):  # 添加对 JSON 文件的处理
                print(f'正在处理文件:\t{input_path}')
                btf(input_path, output_path).json()
            else:
                print(f'跳过文件:\t{input_path}')
    print("所有文件处理完成!")

walk_dir(base_dir)

介绍

本项目可以对指定目录进行批量的代码可读化操作。目前支持以下代码格式:

- `.js`
- `.html`
- `.css`
- `.json`

使用方法

1. 安装依赖
[Asm] 纯文本查看 复制代码
pip install jsbeautifier cssbeautifier beautifulsoup4

2. 修改指向的目录
[Asm] 纯文本查看 复制代码
base_dir = r"path\to\your\dir" # 你的文件夹路径

将此行改为你的目标目录,填在双引号中间
3. 运行脚本

效果展示

Snipaste_2023-08-27_22-05-07.png

Snipaste_2023-08-27_22-05-36.png

后面的代码可读性较高,方便逆向分析。
PS:本脚本是楼主在逆向分析某浏览器插件的时候顺手写的,批量处理后分析十分方便,有需要的可以使用。如果能顺手给个Star就更好了{:1_905:}

免费评分

参与人数 5吾爱币 +10 热心值 +5 收起 理由
zyz1234 + 1 热心回复!
huahhzuiqiang + 1 + 1 我很赞同!
yjn866y + 1 + 1 谢谢@Thanks!
liu8833637 + 1 + 1 热心回复!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| shiquda 发表于 2023-8-28 07:21
吃橘的皮 发表于 2023-8-28 00:57
emmm,纯路人,为什么不用vscode或者webstorm等编译器/编辑器去格式化一下呢?批处理也能做到吧

VScode编辑器确实可以使用Prettier等插件进行格式化,但是我这个脚本可以遍历目标目录,然后所有支持格式化的文件都会执行一次操作,免得多次操作。
至于您提到的批处理是指直接用批处理来实现这个功能?还是指调用vscode的代码格式化功能?不好意思,我对批处理不是很了解。
hrh123 发表于 2023-8-28 00:55
本帖最后由 hrh123 于 2023-8-28 01:04 编辑

看起来就是个批量格式化操作,但还是欢迎原创和开源!
不过提个建议,requirements.txt不是手打的,虚拟环境的话用pip的freeze语句导出,非虚拟环境可以用第三方库pipreqs实现

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
shiquda + 1 + 1 热心回复!

查看全部评分

吃橘的皮 发表于 2023-8-28 00:57
emmm,纯路人,为什么不用vscode或者webstorm等编译器/编辑器去格式化一下呢?批处理也能做到吧
 楼主| shiquda 发表于 2023-8-28 07:08
hrh123 发表于 2023-8-28 00:55
看起来就是个批量格式化操作,但还是欢迎原创和开源!
不过提个建议,requirements.t ...

感谢大佬,学到了!之前一直没搞懂如何导出 requirements,有的时候还要去全部依赖那边翻来找去
52soft 发表于 2023-8-28 07:43
学习的好东东
kings0b 发表于 2023-8-28 09:03
学习了!!!!
feiyu361 发表于 2023-8-28 09:19
感谢分享
lcg2014 发表于 2023-8-28 10:28
真不错,支持
dychjyfgfda 发表于 2023-8-28 18:42
这个有意思,可以正常写完把这个反着用,减少可读性防逆向
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-22 05:22

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表