Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gmolveau
GitHub Repository: gmolveau/python_full_course
Path: blob/master/exercices/imperative/zip-bruteforcer/solution_click.py
306 views
1
# password = 'a' or 'xiphihumeralis'
2
3
import logging
4
import zipfile
5
6
import click
7
8
9
def configure_logging(loglevel):
10
logging.basicConfig(level=loglevel)
11
12
13
@click.command(help="zip bruteforcer")
14
@click.option(
15
"-v",
16
"--verbose",
17
help="Enable verbose mode",
18
is_flag=True,
19
default=False,
20
)
21
@click.option(
22
"-f",
23
"--file",
24
"protected_zip_path",
25
help="Path of the protected zip file",
26
type=click.Path(exists=True),
27
required=True,
28
)
29
@click.option(
30
"-w",
31
"--wordlist",
32
"wordlist_path",
33
help="Path of the wordlist file",
34
type=click.Path(exists=True),
35
required=True,
36
)
37
def main(verbose, protected_zip_path, wordlist_path):
38
loglevel = logging.DEBUG if verbose else logging.INFO
39
configure_logging(loglevel)
40
main(protected_zip_path, wordlist_path)
41
42
43
def bruteforce_zip(protected_zip_path, wordlist_path):
44
with zipfile.ZipFile(protected_zip_path) as z:
45
with open(wordlist_path) as wordlist_file:
46
for password_line in wordlist_file:
47
password = password_line.strip()
48
try:
49
z.extractall(pwd=password.encode("utf-8"))
50
logging.info(f"password found: {password}")
51
break
52
except zipfile.BadZipFile as e:
53
logging.error(f"the zip file is invalid - {e}")
54
raise e
55
except RuntimeError as e:
56
logging.debug(f"bad password: {password}")
57
58
59
if __name__ == "__main__":
60
main()
61
62