brutepdf.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. #
  3. # This code is Public Domain.
  4. # Permission to use, copy, modify, and/or distribute this software for any
  5. # purpose with or without fee is hereby granted.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  11. # RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  12. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
  13. # USE OR PERFORMANCE OF THIS SOFTWARE.
  14. import pikepdf
  15. import multiprocessing
  16. import sys
  17. import pathlib
  18. from datetime import datetime, timedelta
  19. class PwIteratorPLZ:
  20. def __init__(self):
  21. self.plz = 1000
  22. def __iter__(self):
  23. return self
  24. def __next__(self):
  25. plz = self.plz
  26. if plz >= 99999:
  27. raise StopIteration()
  28. self.plz += 1
  29. return f"{plz:05}"
  30. class PwIteratorBirthday:
  31. def __init__(self):
  32. now = datetime.now()
  33. self.date = datetime(year=now.year, month=now.month, day=now.day, hour=12)
  34. self.enddate = self.date - timedelta(days=(120 * 365))
  35. def __iter__(self):
  36. return self
  37. def __next__(self):
  38. date = self.date
  39. if date <= self.enddate:
  40. raise StopIteration()
  41. self.date -= timedelta(days=1)
  42. return f"{date.day:02}.{date.month:02}.{date.year:04}"
  43. def testit(password):
  44. try:
  45. with pikepdf.open(infile, password=password) as pdf:
  46. pdf.save(outfile)
  47. return password
  48. except pikepdf.PasswordError:
  49. return None
  50. except Exception as e:
  51. raise Exception(f"Error processing PDF file: {e}")
  52. infile = pathlib.Path(sys.argv[1])
  53. outfile = infile.with_stem(infile.stem + ".DECRYPTED")
  54. iterators = (
  55. PwIteratorBirthday,
  56. PwIteratorPLZ,
  57. )
  58. for iterator in iterators:
  59. with multiprocessing.Pool() as pool:
  60. for pw in pool.imap(testit, iterator(), 128):
  61. if pw is not None:
  62. pool.terminate()
  63. print(f"The PDF has been decrypted to: {outfile}")
  64. print(f"The password is: {pw}")
  65. sys.exit(0)
  66. print("Password not found :(")
  67. sys.exit(1)
  68. # vim: ts=4 sw=4 expandtab