crack.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/python
  2. # -*- coding: iso-8859-15 -*-
  3. """
  4. This file is part of the cintruder project, http://cintruder.03c8.net
  5. Copyright (c) 2012/2019 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.iteritems():
  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.iteritems():
  30. if concordance2.has_key(word):
  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. if not os.path.exists("core/images/previews/"):
  44. os.mkdir("core/images/previews/")
  45. else:
  46. shutil.rmtree("core/images/previews/")
  47. os.mkdir("core/images/previews/")
  48. def buildvector(self, im):
  49. d1 = {}
  50. count = 0
  51. for i in im.getdata():
  52. d1[count] = i
  53. count += 1
  54. return d1
  55. def set_captcha(self, captcha):
  56. """
  57. Set the captcha.
  58. """
  59. self.captcha = captcha
  60. return captcha
  61. def crack(self, options):
  62. v = VectorCompare()
  63. path, dirs, files = os.walk("dictionary/").next()
  64. dictionary = dirs
  65. imageset = []
  66. last_letter = None
  67. print "\n[Info] Loading dictionary...\n"
  68. for letter in dictionary:
  69. for img in os.listdir('dictionary/'+letter):
  70. temp = []
  71. temp.append(self.buildvector(Image.open("dictionary/%s/%s"%(letter, img))))
  72. imageset.append({letter:temp})
  73. try:
  74. im = Image.open(self.captcha)
  75. im.save("core/images/previews/last-preview.gif")
  76. im2 = Image.new("P", im.size, 255)
  77. im = im.convert("P")
  78. except:
  79. print "\nError during cracking!. Is that captcha supported?\n"
  80. return
  81. temp = {}
  82. try:
  83. setids = int(options.setids)
  84. except:
  85. setids = 3 # default pixel colour id
  86. for x in range(im.size[1]):
  87. for y in range(im.size[0]):
  88. pix = im.getpixel((y, x))
  89. temp[pix] = pix
  90. if pix == setids: # pixel colour id
  91. im2.putpixel((y, x), 0)
  92. inletter = False
  93. foundletter = False
  94. start = 0
  95. end = 0
  96. letters = []
  97. for y in range(im2.size[0]): # slice across
  98. for x in range(im2.size[1]): # slice down
  99. pix = im2.getpixel((y, x))
  100. if pix == 0: # != 255
  101. inletter = True
  102. if foundletter == False and inletter == True:
  103. foundletter = True
  104. start = y
  105. if foundletter == True and inletter == False:
  106. foundletter = False
  107. end = y
  108. letters.append((start, end))
  109. inletter = False
  110. count = 0
  111. countid = 1
  112. word_sug = None
  113. end = time.time()
  114. elapsed = end - start
  115. words = {}
  116. for letter in letters:
  117. m = hashlib.md5()
  118. im3 = im2.crop((letter[0], 0, letter[1], im2.size[1]))
  119. guess = []
  120. for image in imageset:
  121. for x, y in image.iteritems():
  122. if len(y) != 0:
  123. guess.append(( v.relation(y[0], self.buildvector(im3)), x))
  124. guess.sort(reverse=True)
  125. word_per = guess[0][0] * 100
  126. if str(word_per) == "100.0":
  127. print "Image position :", countid
  128. print "Broken Percent :", int(round(float(word_per))), "%", "[+CRACKED!]"
  129. words[countid] = guess[0][1]
  130. else:
  131. print "Image position :", countid
  132. print "Broken Percent :", "%.4f" % word_per, "%"
  133. words[countid] = "_"
  134. print "Word suggested :", guess[0][1]
  135. print "-------------------"
  136. if word_sug == None:
  137. word_sug = str(guess[0][1])
  138. else:
  139. word_sug = word_sug + str(guess[0][1])
  140. count += 1
  141. countid = countid + 1
  142. print "\n========================================"
  143. if options.verbose:
  144. print "[Info] Elapsed OCR time :", elapsed
  145. print "========================================"
  146. if word_sug is None:
  147. print "Suggested Solution: ", "[ No idea!. Try to add more images to your dictionary/]"
  148. else:
  149. print "Cracked Words: ", words.values()
  150. print "Suggested Solution: ", "[", word_sug, "]"
  151. print "========================================\n"
  152. return word_sug