当前位置: 代码网 > it编程>前端脚本>Python > python实现微信聊天图片DAT文件还原

python实现微信聊天图片DAT文件还原

2024年08月03日 Python 我要评论
支持递归处理,不指定结果文件夹时,结果将写入into_path+"CovertImage"目录下。

完整代码如下:

from glob import glob
import os
from tqdm import tqdm


def get_sign(dat_r):
    signatures = [(0x89, 0x50, 0x4e), (0x47, 0x49, 0x46), (0xff, 0xd8, 0xff)]
    mats = [".png", ".gif", ".jpg"]
    for now in dat_r:
        for j, xor in enumerate(signatures):
            res = [nowbyte ^ xor_byte for nowbyte,
                   xor_byte in zip(now[:3], xor)]
            if res[0] == res[1] == res[2]:
                return res[0], mats[j]
    else:
        raise exception("no valid signature is found")


def imagedecode(file, root_path, dest_path=none):
    dat_r = open(file, "rb")
    try:
        sign, mat = get_sign(dat_r)
        dat_r.seek(0)
        data = bytes(byte ^ sign for byte in dat_r.read())
        relative_path = os.path.relpath(file, root_path)
        if dest_path is none:
            dest_path = os.path.join(root_path, "covertimage")
        dest = os.path.join(dest_path,
                            relative_path.replace(".dat", mat))
        os.makedirs(os.path.dirname(dest), exist_ok=true)
        with open(dest, "wb") as write:
            write.write(data)
    finally:
        dat_r.close()


def main(into_path, out_path=none):
    for file in tqdm(glob(os.path.join(into_path, "**", "*.dat"), recursive=true)):
        imagedecode(file, into_path, out_path)
        
if __name__ == '__main__':
    into_path = r"d:\tmp\wx_icon_dat"
#     out_path = r"d:\tmp\wx_icon_dat"
    main(into_path, out_path=none)

支持递归处理,不指定结果文件夹时,结果将写入into_path+"covertimage"目录下。
还原示例:
在这里插入图片描述

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com