Python实现zip包中CRC32匹配文件查找

本文提供了一个Python脚本,用于在文件目录和zip压缩文件中查找CRC32值相同的文件。脚本可以获取文件目录、zip文件的CRC32值,并支持通过CRC列表文件进行搜索,提高了在破解过程中查找匹配文件的效率。
摘要由CSDN通过智能技术生成

在使用ARCHPR进行zip压缩文件明文攻击时发现电脑里有不少文件,通过文件名查找有时因为改了名字不一定能找到,压缩目录后检查CRC32值是否与要破解的zip文件对应是很花时间和精力的事,WinRAR也无法拷贝CRC32信息,即使拍下照片用图片转文字方式获取CRC32值也是很花时间的事情:

笔者也遇到相同的困惑,就临时弄了一个查找zip包crc32相同文件的小程序,方便大家进行搜索文件,同时也可以获取zip包和目录的CRC32值,以及使用CRC32列表文件查找zip包中匹配的文件。

 代码如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import zlib
import zipfile
import os

#获取文件目CRC
def get_filecrc(fileName):
    res = 0
    for line in open(fileName,'rb'):
        res = zlib.crc32(line, res)
    return res

#获取文件目录及子目录
def get_filescrc(file_dir):
    file_dict = {}
    for dirpath,dirnames,filenames in os.walk(file_dir):
        for filename in filenames:
            full_name = os.path.join(dirpath,filename)
            res = get_filecrc(full_name)
            if res != 0:                            #过滤掉CRC为0的目录文件
                crc_value = '%X'%(res & 0xFFFFFFFF)
                if crc_value in file_dict.keys():
                    file_dict[crc_value].append(full_name)
                else:
                    file_dict[crc_value] = [full_name]
    return file_dict

#获取zip文件CRC
def get_zipcrc(fileName):
    file_handler = zipfile.ZipFile(fileName)          #指定压缩包
    name_list = file_handler.namelist()               #使用一个列表获取压缩包内所有的文件名
    crc_dict = {}
    for name in name_list:
        name_info = file_handler.getinfo(name)
        if name_info.CRC != 0 :                       #过滤掉CRC为0的目录文件
            crc_value = '%X'%(name_info.CRC & 0xFFFFFFFF)
            if crc_value in crc_dict.keys():
                crc_dict[crc_value].append(name)      #使用CRC32作为键,[文件1,文件2]作为值(不同目录存在下相同文件)
            else:
                crc_dict[crc_value] = [name]
    return  crc_dict

#获取CRC列表文件CRC列表
def get_listFilecrc(fileName):
    crc_list = []
    with open(fileName) as fp:
        for line in fp:
            #print(line.strip())
            crc_list.append(line.strip())
    return crc_list

#导出到文件
def export_tofile(fileName, dictItems, havePath):
    try:
        with open(fileName,'w') as file:
            for k,v in dictItems:
                if havePath == 'y':
                    file.write('{0:10}\t{1}\n'.format(k,v))
                else:
                    file.write('{}\n'.format(k))
    except Exception as err:
        print('导出文件出错:%s'%str(err))
    else:
        print('文件已导出到:{0}\\{1}'.format(os.path.abspath(os.curdir),fileName))

# main函数:
if __name__ == '__main__':
    print('菜单:1(获取文件目录下所有文件CRC-32)')
    print('     2(获取zip包下所有文件CRC-32)')
    print('     3(查找与zip包与指定目录下相同CRC-32文件)')
    print('     4(通过已知CRC(CRC列表.txt)查找与zip包下相同CRC-32的文件)')
    print('     5(通过已知CRC(CRC列表.txt)查找与指定目录下相同CRC-32的文件)')
    choice = input('请输入你的选择:')
    if choice == '1':
        path = input('请输入要获取的目录:')
        havePath = input('是否打印文件名(y/n):')
        file_dict = get_filescrc(path)
        print('-------------Dir Filename CRC Info-------------')
        for k,v in file_dict.items():
            if havePath == 'y':
                print('{0:10}\t{1}'.format(k,v))
            else:
                print(k)
        print('-------------------------------------------')
        #导出到文件
        haveReport = input('是否导出输出结果(y/n):')
        if haveReport == 'y':
            fileName = input('请输入导出的文件名:')
            export_tofile(fileName,file_dict.items(),havePath)
    elif choice == '2':
        fileName = input('请输入zip文件位置:')
        havePath = input('是否打印文件名(y/n):')
        zip_dict = get_zipcrc(fileName)
        print('-------------Zip Filename CRC Info-------------')
        for k,v in zip_dict.items():
            if havePath == 'y':
                print('{0:10}\t{1}'.format(k,v))
            else:
                print(k)
        print('-------------------------------------------')
        #导出到文件
        haveReport = input('是否导出输出结果(y/n):')
        if haveReport == 'y':
            fileName = input('请输入导出的文件名:')
            export_tofile(fileName,zip_dict.items(),havePath)
    elif choice == '3':
        zipName = input('请输入zip文件位置:')
        path = input('请输入要获取的目录:')

        zip_dict = get_zipcrc(zipName)                       #获取zip文件键-值对:{crc:文件路径}
        file_dict = get_filescrc(path)                       #获取目录文件键-值对:{crc:文件路径}

        crc_keys = zip_dict.keys() & file_dict.keys()

        if not crc_keys:
            print('没有找到CRC-32相同文件!')
        else:
            #遍历所有目录下文件
            for k in crc_keys:
                print('{0}\t{1}'.format('相同CRC-32:',k))
                print('{0}\t{1}'.format('zip包文下件路径:',zip_dict[k]))
                print('{0}\t{1}'.format('目录下文件路径:',file_dict[k]))

    elif choice == '4':                                        #通过CRC列表文件比较查找zip包下文件
        zipName = input('请输入zip文件位置:')
        fileName = input('请输入CRC-32列表文件的位置:')

        zip_dict = get_zipcrc(zipName)                       #获取zip文件键-值对:{crc:文件路径}
        crc_list = get_listFilecrc(fileName)

        isFound = False
        for c in crc_list:
            if c in zip_dict:
                print('{0}\t{1}'.format('相同CRC-32:',c))
                print('{0}\t{1}'.format('zip包文下件路径:',zip_dict[c]))
                isFound = True
        if not isFound:
            print('没有找到CRC-32相同文件!')

    elif choice == '5':                                        #通过CRC列表文件比较查找目录下文件
        path = input('请输入要获取的目录:')
        fileName = input('请输入CRC-32列表文件的位置:')

        file_dict = get_filescrc(path)                       #获取目录文件键-值对:{crc:文件路径}
        crc_list = get_listFilecrc(fileName)

        isFound = False
        for c in crc_list:
            if c in file_dict:
                print('{0}\t{1}'.format('相同CRC-32:',c))
                print('{0}\t{1}'.format('目录下文件路径:',file_dict[c]))
                isFound = True
        if not isFound:
            print('没有找到CRC-32相同文件!')
    else:
        print('没有这个选择')
    os.system('pause')

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值