zipbruter.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/python3
  2. """
  3. Password cracker for ZIP files. Uses brute
  4. force attack vector for this, so you must have
  5. strong wordlist.
  6. Usage:
  7. python3 zipbruter.py -f <encrypted_zip_file> -w <wordlist> -t <threads>
  8. """
  9. from sys import exit as exit_
  10. from os.path import isfile
  11. from argparse import ArgumentParser
  12. from _thread import start_new_thread
  13. from queue import Queue
  14. from zipfile import is_zipfile, ZipFile, BadZipfile
  15. class ZipBruter:
  16. """Main ZipBruter class"""
  17. def __init__(self, file, word_list, threads) -> None:
  18. """Initialized function for ZipBruter"""
  19. self.file = file
  20. self.word_list = word_list
  21. self.threads = threads
  22. # Create FIFO queue
  23. self.queue = Queue()
  24. def worker(self) -> None:
  25. """
  26. Basically it listen queue and gets target password
  27. from FIFO queue and checks if zip passwd is true
  28. """
  29. while True:
  30. # gets target passwd
  31. passwd = self.queue.get()
  32. self.queue.task_done()
  33. if passwd is None:
  34. break
  35. try:
  36. with ZipFile(self.file) as zipfile:
  37. zipfile.extractall(pwd=passwd.encode())
  38. print('Found passwd: %s' % passwd)
  39. except (RuntimeError, BadZipfile):
  40. pass
  41. def start_workers(self) -> None:
  42. """Start threads"""
  43. for _ in range(self.threads):
  44. start_new_thread(self.worker, ())
  45. def main(self) -> None:
  46. """Main entrypoint for program"""
  47. self.start_workers()
  48. for target_passwd in self.read_wordlist():
  49. self.queue.put(target_passwd)
  50. for _ in range(self.threads):
  51. self.queue.put(None)
  52. self.queue.join()
  53. def read_wordlist(self) -> str:
  54. """Read given wordlist file and yield target passwds"""
  55. with open(self.word_list, 'r') as file:
  56. for line in file.readlines():
  57. yield line.strip()
  58. if __name__ == '__main__':
  59. parser = ArgumentParser()
  60. parser.add_argument('-f', '--file', type=str, help='Target encrypted zip file.')
  61. parser.add_argument('-w', '--word-list', type=str, help='Wordlist to be used.')
  62. parser.add_argument('-t', '--threads', type=int, default=4, help='Thread count.')
  63. args = parser.parse_args()
  64. if not args.file or not args.word_list:
  65. exit_(1)
  66. if not is_zipfile(args.file):
  67. exit_(1)
  68. if not isfile(args.word_list):
  69. exit_(1)
  70. bruter = ZipBruter(args.file, args.word_list, args.threads)
  71. bruter.main()