def fetch_attachment(self, attribute_id, uuid, event_uuid, filename): url = '{0}/attributes/download/{1}'.format(self.api_url, attribute_id) try: result = None req = urllib2.Request(url, None, self.api_headers) resp = urllib2.urlopen(req).read() binary = StringIO(resp) zipfile = True try: zip_file = ZipFile(binary) zip_file.setpassword('infected'.encode('utf-8')) except BadZipfile: zipfile = False if self.dump: path = self.__get_dump_path(self.file_location, event_uuid) destination_folder = '{0}/{1}'.format(path, '') if not isdir(destination_folder): makedirs(destination_folder) # save zip file if zipfile: f = open('{0}/{1}.zip'.format(destination_folder, filename), 'w+') f.write(resp) f.close() extraction_destination = '{0}/{1}.zip_contents'.format(destination_folder, filename) if not isdir(extraction_destination): makedirs(extraction_destination) # unzip the file zip_file.extractall(extraction_destination) else: file_path = '{0}/{1}'.format(destination_folder, filename) f = open(file_path, 'w+') f.write(resp) f.close() extraction_destination = '{0}'.format(destination_folder) if not isdir(extraction_destination): makedirs(extraction_destination) move(file_path, extraction_destination) if zipfile: zipfiles = zip_file.filelist for zipfile in zipfiles: filename = zipfile.filename result = zip_file.read(filename) break zip_file.close() else: result = resp return result except urllib2.HTTPError: return None
def to_zip(self, fileobject_path): # replace this with pyminizip.compress zipObj = ZipFile(self.backup_path, 'a') zipObj.write(fileobject_path, basename(fileobject_path)) zipObj.setpassword(self.archive_password) zipObj.close() return 0
def install_translation(self): self.progress_bar.setMinimum(0) self.progress_bar.setMaximum(100) self.progress_bar.setValue(0) cnt_files = 0 try: zip_file = ZipFile("trans.zip", "r") zip_file.setpassword(b"iz20Wo4suqmUcZHFXL8G") with zip_file.open("assets.json") as file_assets_to_backup: assets_to_backup = json.load(file_assets_to_backup) self.set_archives_to_restore(list(assets_to_backup.values())) QCoreApplication.processEvents() if self.reader.get_parameter("TranslationInstalled"): self.tor_editor.restore(assets_to_backup.values()) else: self.tor_editor.backup(assets_to_backup.values()) QCoreApplication.processEvents() with zip_file.open("settings.txt") as manager_file: game_files = txt_file_to_dict(manager_file) delta = 100 / len(game_files) for game_path in game_files: archive = None for game_dir in assets_to_backup: if game_dir in game_path: archive = assets_to_backup[game_dir] print(archive) with zip_file.open(game_files[game_path]) as game_file: cnt_files += self.tor_editor.change_file_in_tor( game_file.read(), game_path, archive) self.progress_bar.setValue(int(cnt_files * delta)) QCoreApplication.processEvents() print("{} файлов успешно добавлено в игру".format(cnt_files)) self.progress_bar.setValue(0) zip_file.extract("info.txt") self.reader.set_parameter("TranslationInstalled", True) self.reload_main_button() except FileNotFoundError as e: handle_error(e) self.raise_error(False, "Возникла ошибка: не найден файл.", "Подробности в логах.") except Exception as e: handle_error(e) self.raise_error(False, "Возникла непредвиденная ошибка.", "Подробности в логах.")
def Run(self): self.current_file = self.file_path while self.end is False: zipfile = ZipFile(self.current_file, 'r') if self.key is not None: zipfile.setpassword(self.key) _decompress(zipfile) self.current_file = zipfile.filelist[0].filename self.current_iteration += 1 if self.current_file != "True.zip" and self.current_file != "False.zip": self.end = True sys.stdout.write("Finished with %s iterations.\n" % self.current_iteration)
def __init__(self, filename, byte_content, entries, option_map): if re.search(r'微信支付账单.*\.zip$', filename): password = input('微信账单密码:') z = ZipFile(BytesIO(byte_content), 'r') z.setpassword(bytes(password, 'utf-8')) filelist = z.namelist() if len(filelist) == 2 and re.search(r'微信支付.*\.csv$', filelist[1]): byte_content = z.read(filelist[1]) content = byte_content.decode("utf-8-sig") lines = content.split("\n") if (lines[0].replace(',', '') != '微信支付账单明细\r'): raise 'Not WeChat Trade Record!' print('Import WeChat: ' + lines[2]) content = "\n".join(lines[16:len(lines)]) self.content = content self.deduplicate = Deduplicate(entries, option_map)
def __init__(self, filename, byte_content, entries, option_map): if re.search(r'alipay_record_\d{8}_\d{6}.zip$', filename): password = input('支付宝账单密码:') z = ZipFile(BytesIO(byte_content), 'r') z.setpassword(bytes(password, 'utf-8')) filelist = z.namelist() if len(filelist) == 2 and re.search(r'alipay_record.*\.csv$', filelist[1]): byte_content = z.read(filelist[1]) content = byte_content.decode("gbk") lines = content.split("\n") if not re.search(r'支付宝(中国)网络技术有限公司', lines[0]): raise ValueError('Not Alipay Proven Record!') print('Import Alipay') content = "\n".join(lines[1:len(lines) - 30]) self.content = content self.deduplicate = Deduplicate(entries, option_map)
def fetch_attachment(self, attribute_id, uuid, event_uuid, filename): url = '{0}/attributes/download/{1}'.format(self.api_url, attribute_id) try: result = None req = urllib2.Request(url, None, self.get_api_header_parameters()) resp = urllib2.urlopen(req).read() binary = StringIO(resp) zip_file = ZipFile(binary) zip_file.setpassword('infected'.encode('utf-8')) if self.dump: path = self.__get_dump_path(self.file_location, event_uuid) destination_folder = '{0}/{1}'.format(path, '') if not isdir(destination_folder): makedirs(destination_folder) # save zip file f = open('{0}/{1}.zip'.format(destination_folder, filename), 'w+') f.write(resp) f.close() extraction_destination = '{0}/{1}.zip_contents'.format(destination_folder, filename) if not isdir(extraction_destination): makedirs(extraction_destination) # unzip the file zip_file.extractall(extraction_destination) # do everything in memory zipfiles = zip_file.filelist for zipfile in zipfiles: filename = zipfile.filename result = zip_file.read(filename) break zip_file.close() return result except urllib2.HTTPError: return None
def add_password(zip, passwd): zipfile = ZipFile(zip, "a") zipfile.setpassword(passwd) zipfile.close() return zip
class ZipHintCracker: def __init__(self, passCandidate, zipPath, hashPath): self.pwdHint = passCandidate self.zipInfo = ZipFile(zipPath, "r") self.loadPermutations(hashPath) #tests a password on a given zip file def testPwd(self, pwd, checkMethod=ZipFile.testzip): self.zipInfo.setpassword(pwd) try: checkMethod(self.zipInfo) #if no exception was raised, it was the right password return True except RuntimeError as err: #if it's not the right password, intercept the runtime error if err[0] == 'Bad password for file': return False #the runtime error was not one we expected, re-raise it else: raise #test all elements in a table (or iterateable ;)) def testTable(self,table): for i in table: if self.testPwd(i): #if it's the right password return i #return the password return False #otherwise return False #test the hint string def testHint(self): return self.testTable(self.permuteHint()) #set up the hash from a json file of possible substitution def loadPermutations(self,path): with open(path) as f: self.permHash = json.load(f) #permute a given character based on a dictionary of known substitutions # is a generator, so this function returns an iteratable object def permuteCharacter(self,char): if(char): #as long as we're not dealing with "" (empty string) if(char.isalpha()): #yield both cases of any alphabetical character yield char.upper() yield char.lower() #if the character is in the hash then run through all its members else: yield char if(type(self.substitutions.get(char)) == list): for perm in self.substitutions.get(char): yield perm #permute a given string based on a dictionary of known substitutions # is a generator, so it's used in for loops def permuteString(self, string): char = string[:1] rest = string[1:] if(len(string)> 1): for permutation in self.permuteCharacter(char): for restPermutation in self.permuteString(rest): yield permutation+restPermutation else: for permutation in self.permuteCharacter(char): yield permutation def permuteHint(self): return self.permuteString(self.pwdHint)
def add(self, archive_password=None, audio_file=None, session=None, artist_name_fallback=None): cache_key = LibraryUpload.CACHE_KEY % (cherrypy.request.user.id, session) all_tracks = None if not cache.has(cache_key): raise cherrypy.HTTPError(status=409) else: all_tracks = cache.get(cache_key) if audio_file is not None and len(audio_file) == 0: audio_file = None if archive_password is not None and len(archive_password) == 0: archive_password = None content_disposition = cherrypy.request.headers.get('content-disposition') filename = content_disposition[content_disposition.index('filename=') + 9:] if filename.startswith('"') and filename.endswith('"'): filename = filename[1:-1] filename = unquote(filename) ext = os.path.splitext(filename)[1].lower()[1:] basename = os.path.splitext(filename)[0] cache_path = os.path.join(cherrypy.config['opmuse'].get('cache.path'), 'upload') if not os.path.exists(cache_path): os.mkdir(cache_path) tempdir = tempfile.mkdtemp(dir=cache_path) path = os.path.join(tempdir, filename) paths = [] rarfile.PATH_SEP = '/' messages = [] with open(path, 'wb') as fileobj: fileobj.write(cherrypy.request.rfile.read()) # this file is a regular file that belongs to an audio_file if audio_file is not None: track = None tries = 0 # try and sleep until we get the track.. this will almost always # be needed because of the async upload. while track is None and tries < 10: track = library_dao.get_track_by_filename(audio_file.encode('utf8')) tries += 1 if track is None: time.sleep(3) if track is None: messages.append(('warning', ("<strong>%s</strong>: Skipping <strong>%s</strong>, timeout trying to " + "find its track.") % (audio_file, filename))) else: track_structure = TrackStructureParser(track) track_path = track_structure.get_path(absolute=True) relative_track_path = track_structure.get_path(absolute=False).decode('utf8', 'replace') new_path = os.path.join(track_path, filename.encode('utf8')) if os.path.exists(new_path): messages.append(('warning', ("<strong>%s</strong>: Skipping <strong>%s</strong>, already exists " + "in <strong>%s</strong>.") % (audio_file, filename, relative_track_path))) else: shutil.move(path.encode('utf8'), new_path) messages.append(('info', ("<strong>%s</strong>: Uploaded <strong>%s</strong> to " + "<strong>%s</strong>.") % (audio_file, filename, relative_track_path))) elif ext == "zip": # set artist name fallback to zip's name so if it's missing artist tags # it's easily distinguishable and editable so it can be fixed after upload. artist_name_fallback = basename try: zip = ZipFile(path) if archive_password is not None: zip.setpassword(archive_password.encode()) zip.extractall(tempdir) os.remove(path) for name in zip.namelist(): namepath = os.path.join(tempdir, name) # ignore hidden files, e.g. OSX archive weirdness and such if name.startswith(".") or os.path.split(name)[0] == "__MACOSX": shutil.rmtree(namepath) continue paths.append(namepath.encode('utf8')) except Exception as error: messages.append(('danger', "<strong>%s</strong>: %s" % (os.path.basename(path), error))) elif ext == "rar": # look at corresponding ext == zip comment... artist_name_fallback = basename try: rar = RarFile(path) if archive_password is None and rar.needs_password(): messages.append(('danger', "<strong>%s</strong>: Needs password but none provided." % os.path.basename(path))) else: if archive_password is not None: rar.setpassword(archive_password) rar.extractall(tempdir) os.remove(path) for name in rar.namelist(): namepath = os.path.join(tempdir, name) if name.startswith("."): shutil.rmtree(namepath) continue paths.append(namepath.encode('utf8')) except Exception as error: messages.append(('danger', "<strong>%s</strong>: %s" % (os.path.basename(path), error))) # this is a plain audio file else: paths.append(path.encode('utf8')) for path in paths: # update modified time to now, we don't want the time from the zip # archive or whatever os.utime(path, None) if len(paths) > 0: tracks, add_files_messages = library_dao.add_files(paths, move=True, remove_dirs=False, artist_name_fallback=artist_name_fallback, user=cherrypy.request.user) messages += add_files_messages else: tracks = [] shutil.rmtree(tempdir) for track in tracks: all_tracks.append(track.id) if track.album is not None: remotes.update_album(track.album) if track.artist is not None: remotes.update_artist(track.artist) remotes.update_track(track) hierarchy = Library._produce_track_hierarchy(library_dao.get_tracks_by_ids(all_tracks)) return {'hierarchy': hierarchy, 'messages': messages}
# print r.status_code # print r.reason # dat = r.content # print dat[:100] # print len(dat) # f = open('unreal.zip', 'wb') # f.write(dat) # f.close() f = open('unreal.zip', 'rb') dat = f.read() f.close() print dat[:100] print len(dat) z = ZipFile(StringIO.StringIO(dat)) print z.namelist() z.setpassword(password) z.extractall(path='./ch21_files/') z.close()
# if r.status_code == 206: # valid_offsets.append(i) # messages.append(r.text) # print r.text # pprint(messages) # r = requests.get('http://www.pythonchallenge.com/pc/hex/unreal.jpg', headers={'Range': 'bytes=1152983631-'}, auth=HTTPBasicAuth('butter', 'fly')) # print r.status_code # print r.reason # dat = r.content # print dat[:100] # print len(dat) # f = open('unreal.zip', 'wb') # f.write(dat) # f.close() f = open('unreal.zip', 'rb') dat = f.read() f.close() print dat[:100] print len(dat) z = ZipFile(StringIO.StringIO(dat)) print z.namelist() z.setpassword(password) z.extractall(path='./ch21_files/') z.close()