吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5718|回复: 26
上一主题 下一主题
收起左侧

[Python 原创] 解析微信聊天记录中dat格式为普通可见图片

  [复制链接]
跳转到指定楼层
楼主
Eks6666 发表于 2025-2-11 21:45 回帖奖励
[Python] 纯文本查看 复制代码
import os
import logging
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def main(input_path, output_path):
    """主函数,处理DAT文件并解码为图片"""
    if not os.path.exists(output_path):
        os.makedirs(output_path)
    dat_files = get_dat_files(input_path)
    total_files =len(dat_files)
    if total_files ==0:
        logging.error('没有找到任何.dat文件')
        return
    for index, dat_file in enumerate(dat_files, start=1):      
        file_path = os.path.join(input_path, dat_file)
        file_name = os.path.splitext(dat_file)[0]
        decode_image(file_path, file_name, output_path)
        logging.info(f'正在处理--->[{index}/{total_files}]{index / total_files *100:.2f}%')


def get_dat_files(directory):
    """获取目录下所有.dat文件"""
    return [f for f in os.listdir(directory) if f.endswith('.dat')]


def decode_image(dat_path, file_name, output_path):
    """解码DAT文件为图片"""
    try:
        xor_value, image_type = detect_format(dat_path)
        if image_type == 1:
            extension = '.png'
        elif image_type == 2:
            extension = '.gif'
        else:
            extension = '.jpg'
        output_file = os.path.join(output_path, f"{file_name}{extension}")
        with open(dat_path, "rb") as dat_file, open(output_file, "wb") as img_file:
            dat_file.seek(0)
            for byte in dat_file.read():
                img_file.write(bytes([byte ^ xor_value]))
        logging.info(f"成功解码 {dat_path} -> {output_file}")
    except Exception as e:
        logging.error(f"解码失败:{dat_path},错误信息:{e}")


def detect_format(dat_path):
    """检测DAT文件的图片格式"""
    with open(dat_path, "rb") as dat_file:
        header = dat_file.read(3)
        formats = [
            (b'\x89\x50\x4e', 1),  # PNG
            (b'\x47\x49\x46', 2),  # GIF
            (b'\xff\xd8\xff', 3)  # JPG
        ]
        for xor_bytes, image_type in formats:
            xor_result = bytes([header[i] ^ xor_bytes[i] for i in range(3)])
            if xor_result[0] == xor_result[1] == xor_result[2]:
                return xor_result[0], image_type
        raise ValueError("无法识别的图片格式")


if __name__ == '__main__':
    # 输入和输出路径
    into_path = r'G:\微信聊天记录\WeChat Files\FileStorage\MsgAttach\2a439fca32ade9c75bab36005e2c4bb4\Thumb\2024-04'  # 微信图片DAT文件存放路径
    out_path = './output'    # 转换后的图片存放路径
    main(into_path, out_path)

免费评分

参与人数 4吾爱币 +10 热心值 +4 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
xionghaoyun + 1 + 1 用心讨论,共获提升!
laozhang4201 + 1 + 1 热心回复!
szmsys + 1 + 1 我很赞同!

查看全部评分

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

推荐
玄玉 发表于 2025-2-12 08:30
解析思路值得参考
推荐
mendoor 发表于 2025-2-12 08:54
微信dat图片批量解密、查看、整理工具:WxDatViewer 2.7[2022.11.27]
https://www.52pojie.cn/thread-1713665-1-1.html
(出处: 吾爱破解论坛)

这儿有个现成的,而且还挺强大的,可惜好久没更新了,时不常会遇到有bug,凑合能用
4#
szmsys 发表于 2025-2-12 05:00
5#
happyxuexi 发表于 2025-2-12 07:52
功能强大,我的那个删除微信多余图片的也是用python解析的dat文件。
快速删除QQ文件夹下的大量图片文件
6#
蔷薇的羽翼 发表于 2025-2-12 08:23
需要微信的KEY吗
7#
xionghaoyun 发表于 2025-2-12 08:45
LZ能不能到出PC端的微信的聊天记录
8#
SherlockProel 发表于 2025-2-12 09:18
很有用,我这种经常换设备登录的很需要这个工具
9#
Galaxyou 发表于 2025-2-12 09:35
这个思路还挺好的
10#
song747 发表于 2025-2-12 09:51
mendoor 发表于 2025-2-12 08:54
微信dat图片批量解密、查看、整理工具:WxDatViewer 2.7[2022.11.27]
https://www.52pojie.cn/thread-1713 ...

这个不错,现在还能用
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2026-4-17 13:08

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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