在使用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')