Guest User

phpass_crack.py

a guest
Aug 28th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #################################################################################
  2. #
  3. # phpass.py version 0.1
  4. # Python module port of phpass-0.1 library, originally written by Solar Designer.
  5. #
  6. # Copyright (c) 2008, Alexander Chemeris <Alexander.Chemeris@nospam@gmail.com>
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions are met:
  10. # * Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # * Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. # * The name of the author may be used to endorse or promote products
  16. # derived from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  21. # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  24. # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  26. # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #################################################################################
  29.  
  30. import hashlib
  31.  
  32. def encode64(input_val, count, itoa64):
  33. ''' Encode binary data from input_val to ASCII string.
  34.  
  35. Every six bits of input_val are represented by corresponding
  36. char from 64-char length itoa64 array. That is 0 will be
  37. represented in resulting string with char itoa64[0], 1 will
  38. be represented with itoa64[1], ..., 63 will be represented
  39. with itoa64[63].
  40. '''
  41. output = ''
  42. i = 0
  43.  
  44. while i<count:
  45. value = ord(input_val[i])
  46. i = i+1
  47. output = output + itoa64[value&0x3f]
  48.  
  49. if i < count:
  50. value = value | (ord(input_val[i]) << 8)
  51.  
  52. output = output + itoa64[(value>>6)&0x3f]
  53.  
  54. i = i+1
  55. if i >= count:
  56. break
  57.  
  58. if i < count:
  59. value = value | (ord(input_val[i]) << 16)
  60.  
  61. output = output + itoa64[(value>>12)&0x3f]
  62.  
  63. i = i+1
  64. if i >= count:
  65. break
  66.  
  67. output = output + itoa64[(value>>18)&0x3f]
  68.  
  69. return output
  70.  
  71. def crypt_private(passwd, passwd_hash, hash_prefix='$P$'):
  72. ''' Hash password, using same salt and number of
  73. iterations as in passwd_hash.
  74.  
  75. This is useful when you want to check password match.
  76. In this case you pass your raw password and password
  77. hash to this function and then compare its return
  78. value with password hash again:
  79.  
  80. is_valid = (crypt_private(passwd, hash) == hash)
  81.  
  82. hash_prefix is used to check that passwd_hash is of
  83. supported type. It is compared with first 3 chars of
  84. passwd_hash and if does not match error is returned.
  85.  
  86. NOTE: all arguments must be ASCII strings, not unicode!
  87. If you want to support unicode passwords, you could
  88. use any encoding you want. For compatibility with PHP
  89. it is recommended to use UTF-8:
  90.  
  91. passwd_ascii = passwd.encode('utf-8')
  92. is_valid = (crypt_private(passwd_ascii, hash) == hash)
  93.  
  94. Here hash is already assumed to be an ASCII string.
  95.  
  96. In case of error '*0' is usually returned. But if passwd_hash
  97. begins with '*0', then '*1' is returned to prevent false
  98. positive results of password check.
  99. '''
  100. itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  101. output = '*0'
  102. # Prevent output from being the same as passwd_hash, because
  103. # this may lead to false positive password check results.
  104. if passwd_hash[0:2] == output:
  105. output = '*1'
  106.  
  107. # Check for correct hash type
  108. if passwd_hash[0:3] != hash_prefix:
  109. return output
  110.  
  111. count_log2 = itoa64.index(passwd_hash[3])
  112. if count_log2<7 or count_log2>30:
  113. return output
  114. count = 1<<count_log2
  115.  
  116. salt = passwd_hash[4:12]
  117. if len(salt) != 8:
  118. return output
  119.  
  120. m = hashlib.md5()
  121. m.update(salt)
  122. m.update(passwd)
  123. tmp_hash = m.digest()
  124. for i in xrange(count):
  125. m = hashlib.md5()
  126. m.update(tmp_hash)
  127. m.update(passwd)
  128. tmp_hash = m.digest()
  129.  
  130. output = passwd_hash[0:12]+encode64(tmp_hash, 16, itoa64)
  131. return output
Add Comment
Please, Sign In to add comment