easy_crack.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/python3
  2. # -*- coding: iso-8859-15 -*-
  3. """
  4. This file is part of the cintruder project, https://cintruder.03c8.net
  5. Copyright (c) 2012/2020 psy <epsylon@riseup.net>
  6. cintruder is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation version 3 of the License.
  9. cintruder is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  12. details.
  13. You should have received a copy of the GNU General Public License along
  14. with cintruder; if not, write to the Free Software Foundation, Inc., 51
  15. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. """
  17. from PIL import Image
  18. import hashlib, os, math, time
  19. import shutil
  20. class VectorCompare:
  21. def magnitude(self, concordance):
  22. total = 0
  23. for word, count in concordance.items():
  24. # print concordance
  25. total += count ** 2
  26. return math.sqrt(total)
  27. def relation(self, concordance1, concordance2):
  28. topvalue = 0
  29. for word, count in concordance1.items():
  30. if word in concordance2:
  31. topvalue += count * concordance2[word]
  32. return topvalue / (self.magnitude(concordance1) * self.magnitude(concordance2))
  33. class CIntruderCrack(object):
  34. """
  35. Class to bruteforce captchas
  36. """
  37. def __init__(self, captcha=""):
  38. """
  39. Initialize main CIntruder
  40. """
  41. self.captcha = self.set_captcha(captcha)
  42. start = time.time()
  43. self.dictionary_path = 'mods/easy/dictionary/'
  44. if not os.path.exists("core/images/previews/"):
  45. os.mkdir("core/images/previews/")
  46. else:
  47. shutil.rmtree("core/images/previews/")
  48. os.mkdir("core/images/previews/")
  49. def buildvector(self, im):
  50. d1 = {}
  51. count = 0
  52. for i in im.getdata():
  53. d1[count] = i
  54. count += 1
  55. return d1
  56. def set_captcha(self, captcha):
  57. """
  58. Set the captcha.
  59. """
  60. self.captcha = captcha
  61. return captcha
  62. def crack(self, options):
  63. v = VectorCompare()
  64. path, dirs, files = next(os.walk(self.dictionary_path))
  65. dictionary = dirs
  66. imageset = []
  67. last_letter = None
  68. print("\n[Info] Loading dictionary...\n")
  69. for letter in dictionary:
  70. for img in os.listdir(self.dictionary_path+letter):
  71. temp = []
  72. temp.append(self.buildvector(Image.open(self.dictionary_path+"%s/%s"%(letter, img))))
  73. imageset.append({letter:temp})
  74. try:
  75. im = Image.open(self.captcha)
  76. im.save("core/images/previews/last-preview.gif")
  77. im2 = Image.new("P", im.size, 255)
  78. im = im.convert("P")
  79. except:
  80. print("\nError during cracking!. Is that captcha supported?\n")
  81. return
  82. temp = {}
  83. for x in range(im.size[1]):
  84. for y in range(im.size[0]):
  85. pix = im.getpixel((y, x))
  86. temp[pix] = pix
  87. if pix == 3:
  88. im2.putpixel((y, x), 0)
  89. inletter = False
  90. foundletter = False
  91. start = 0
  92. end = 0
  93. letters = []
  94. for y in range(im2.size[0]): # slice across
  95. for x in range(im2.size[1]): # slice down
  96. pix = im2.getpixel((y, x))
  97. if pix != 255:
  98. inletter = True
  99. if foundletter == False and inletter == True:
  100. foundletter = True
  101. start = y
  102. if foundletter == True and inletter == False:
  103. foundletter = False
  104. end = y
  105. letters.append((start, end))
  106. inletter = False
  107. count = 0
  108. countid = 1
  109. word_sug = None
  110. end = time.time()
  111. elapsed = end - start
  112. words = {}
  113. for letter in letters:
  114. m = hashlib.md5()
  115. im3 = im2.crop((letter[0], 0, letter[1], im2.size[1]))
  116. guess = []
  117. for image in imageset:
  118. for x, y in image.items():
  119. if len(y) != 0:
  120. guess.append(( v.relation(y[0], self.buildvector(im3)), x))
  121. guess.sort(reverse=True)
  122. word_per = guess[0][0] * 100
  123. if str(word_per) == "100.0":
  124. print("Image position : "+ str(countid))
  125. print("Broken Percent : "+ str(int(round(float(word_per))))+ "%"+ " [ FULL CRACKED!!! ]")
  126. words[countid] = guess[0][1]
  127. else:
  128. print("Image position : "+ str(countid))
  129. print("Broken Percent : %.4f" % word_per + "%")
  130. words[countid] = "_"
  131. print("Word suggested : "+ str(guess[0][1]))
  132. print("-------------------")
  133. if word_sug == None:
  134. word_sug = str(guess[0][1])
  135. else:
  136. word_sug = word_sug + str(guess[0][1])
  137. count += 1
  138. countid = countid + 1
  139. print("\n========================================")
  140. if options.verbose:
  141. print("[Info] Elapsed OCR time : "+ str(elapsed))
  142. print("========================================")
  143. if word_sug is None:
  144. print("Suggested Solution: [ No idea!. Try to add more images to your dictionary/ ]")
  145. else:
  146. print("Cracked Words: " +str(list(words.values())))
  147. print("Suggested Solution: [ "+ str(word_sug)+ " ]")
  148. print("========================================\n")
  149. return word_sug