Path: blob/master/exercices/imperative/zip-bruteforcer/solution_click.py
306 views
# password = 'a' or 'xiphihumeralis'12import logging3import zipfile45import click678def configure_logging(loglevel):9logging.basicConfig(level=loglevel)101112@click.command(help="zip bruteforcer")13@click.option(14"-v",15"--verbose",16help="Enable verbose mode",17is_flag=True,18default=False,19)20@click.option(21"-f",22"--file",23"protected_zip_path",24help="Path of the protected zip file",25type=click.Path(exists=True),26required=True,27)28@click.option(29"-w",30"--wordlist",31"wordlist_path",32help="Path of the wordlist file",33type=click.Path(exists=True),34required=True,35)36def main(verbose, protected_zip_path, wordlist_path):37loglevel = logging.DEBUG if verbose else logging.INFO38configure_logging(loglevel)39main(protected_zip_path, wordlist_path)404142def bruteforce_zip(protected_zip_path, wordlist_path):43with zipfile.ZipFile(protected_zip_path) as z:44with open(wordlist_path) as wordlist_file:45for password_line in wordlist_file:46password = password_line.strip()47try:48z.extractall(pwd=password.encode("utf-8"))49logging.info(f"password found: {password}")50break51except zipfile.BadZipFile as e:52logging.error(f"the zip file is invalid - {e}")53raise e54except RuntimeError as e:55logging.debug(f"bad password: {password}")565758if __name__ == "__main__":59main()606162