Advertisement
Guest User

dict2hash.py

a guest
Oct 31st, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Simple Hash list builder
  4. # Converts Wordlist into Hash List
  5. # Handy for testing and pot file seeding
  6. # By: Hood3dRob1n
  7. #
  8. # ./dict2hash.py [OPTIONS]
  9. #   -s = single plaintext
  10. #   -w = wordlist file to convert to hash list
  11. #   -t = hash type to use
  12. #   -o = output filename to use (default: converted.hashes)
  13. #
  14. # Currently Supporting the Following Types:
  15. # 1:Bcyrpt, 2:Cisco PIX, 3:Cisco Type7, 4:DES, 5:MD4, 6:MD5, 7:Unix(MD5), 8:SHA1, 9:SHA256, 10:SHA512,
  16. # 11:LM, 12:MS-SQL 2000, 13:MS-SQL 2005, 14:MySQL 323, 15:MySQL 4.1+, 16:NTLM,
  17. # 19:Oracle 11G, 20:Django DES, 21:Django MD5, 22:Django SHA1
  18. #
  19. # Removed as require username for hashing: 17:Postgres(MD5) & 18:Oracle 10G
  20. # Write your own, im tired...
  21. #
  22. # Leverages passlib package/module for all hashing...
  23. # Passlib uses py-bcrypt, so also need that installed too...
  24. #
  25.  
  26. import os
  27. import optparse
  28. import passlib
  29. import sys
  30.  
  31. from passlib.hash import bcrypt
  32. from passlib.hash import cisco_pix as pix
  33. from passlib.hash import cisco_type7 as type7
  34. from passlib.hash import des_crypt
  35. from passlib.hash import django_salted_sha1 as django_sha1
  36. from passlib.hash import django_salted_md5 as django_md5
  37. from passlib.hash import django_des_crypt as django_des
  38. from passlib.hash import hex_md4 as md4
  39. from passlib.hash import hex_md5 as md5
  40. from passlib.hash import hex_sha1 as sha1
  41. from passlib.hash import hex_sha256 as sha256
  42. from passlib.hash import hex_sha512 as sha512
  43. from passlib.hash import lmhash
  44. from passlib.hash import md5_crypt
  45. from passlib.hash import mssql2000 as m20
  46. from passlib.hash import mssql2005 as m25
  47. from passlib.hash import mysql323
  48. from passlib.hash import mysql41
  49. from passlib.hash import nthash
  50. from passlib.hash import oracle11 as oracle11
  51.  
  52. def cls():
  53.   """Clear the Terminal Screen"""
  54.   if os.name == 'nt':
  55.     os.system('cls')
  56.   elif sys.platform.startswith('win'):
  57.     os.system('cls')
  58.   else:
  59.     os.system('clear')
  60.  
  61. def banner():
  62.   """0x1337 Banner"""
  63.   cls()
  64.   print "\nWordlist to Hash List Convertor"
  65.   print "By: Hood3dRob1n\n"
  66.  
  67. def hasher(self, hashtype):
  68.   """Convert String to requested Hashtype
  69.     Returns the updated hash string
  70.     Unless it's a single plaintext, which displays each hash type"""
  71.   if hashtype == 0:       # ALL Hash Formats
  72.     print "Hashing using all available hash formats...."
  73.     for key, value in valid_hash_types.iteritems():
  74.       h = hasher(self, key)
  75.       if h.startswith('DISABLED FUNCTIONALITY') or h.startswith('ERROR'):
  76.         pass
  77.       else:
  78.         print value.upper() + ":" + h
  79.     return
  80.   elif hashtype == 1:     # Bcyrpt
  81.     try:
  82.       hash_string = bcrypt.encrypt(self)
  83.     except:
  84.       hash_string = "ERROR: Problem Converting '%s' to Bcrypt Hash Format!" % self
  85.   elif hashtype == 2:   # Cisco PIX
  86.     try:
  87.       hash_string = pix.encrypt(self)
  88.     except:
  89.       hash_string = "ERROR: Problem Converting '%s' to Cisco PIX Hash Format!" % self
  90.   elif hashtype == 3:   # Cisco Type7
  91.     try:
  92.       hash_string = type7.encrypt(self)
  93.     except:
  94.       hash_string = "ERROR: Problem Encoding '%s' to Cisco Type7 Hash Format!" % self
  95.   elif hashtype == 4:   # DES
  96.     try:
  97.       hash_string = des_crypt.encrypt(self)
  98.     except:
  99.       hash_string = "ERROR: Problem Converting '%s' to DES Crypt Hash Format!" % self
  100.   elif hashtype == 5:   # MD4
  101.     try:
  102.       hash_string = md4.encrypt(self)
  103.     except:
  104.       hash_string = "ERROR: Problem Converting '%s' to MD4 Hash Format!" % self
  105.   elif hashtype == 6:   # MD5
  106.     try:
  107.       hash_string = md5.encrypt(self)
  108.     except:
  109.       hash_string = "ERROR: Problem Converting '%s' to MD4 Hash Format!" % self
  110.   elif hashtype == 7:   # Unix(MD5)
  111.     try:
  112.       hash_string = md5_crypt.encrypt(self)
  113.     except:
  114.       hash_string = "ERROR: Problem Converting '%s' to Unix(MD5) Hash Format!" % self
  115.   elif hashtype == 8:   # SHA1
  116.     try:
  117.       hash_string = sha1.encrypt(self)
  118.     except:
  119.       hash_string = "ERROR: Problem Converting '%s' to SHA1 Hash Format!" % self
  120.   elif hashtype == 9:   # SHA256
  121.     try:
  122.       hash_string = sha256.encrypt(self)
  123.     except:
  124.       hash_string = "ERROR: Problem Converting '%s' to SHA256 Hash Format!" % self
  125.   elif hashtype == 10:  # SHA512
  126.     try:
  127.       hash_string = sha512.encrypt(self)
  128.     except:
  129.       hash_string = "ERROR: Problem Converting '%s' to SHA512 Hash Format!" % self
  130.   elif hashtype == 11:  # LM
  131.     try:
  132.       hash_string = lmhash.encrypt(self)
  133.     except:
  134.       hash_string = "ERROR: Problem Converting '%s' to LM Hash Format!" % self
  135.   elif hashtype == 12:  # MS-SQL 2000
  136.     try:
  137.       hash_string = m20.encrypt(self)
  138.     except:
  139.       hash_string = "ERROR: Problem Converting '%s' to MS-SQL 2000 Hash Format!" % self
  140.   elif hashtype == 13:  # MS-SQL 2005
  141.     try:
  142.       hash_string = m25.encrypt(self)
  143.     except:
  144.       hash_string = "ERROR: Problem Converting '%s' to MS-SQL 2005 Hash Format!" % self
  145.   elif hashtype == 14:  # MySQL 323
  146.     try:
  147.       hash_string = mysql323.encrypt(self)
  148.     except:
  149.       hash_string = "ERROR: Problem Converting '%s' to MySQL 323 Hash Format!" % self
  150.   elif hashtype == 15:  # MySQL 4.1+
  151.     try:
  152.       hash_string = mysql41.encrypt(self)
  153.     except:
  154.       hash_string = "ERROR: Problem Converting '%s' to MySQL 4.1+ Hash Format!" % self
  155.   elif hashtype == 16:  # NTLM
  156.     try:
  157.       hash_string = nthash.encrypt(self)
  158.     except:
  159.       hash_string = "ERROR: Problem Converting '%s' to NTLM Hash Format!" % self
  160.   elif hashtype == 17:  # Postgres(MD5)
  161.     # Requires username for hashing....
  162.     hash_string = "DISABLED FUNCTIONALITY: Postgres MD5"
  163.   elif hashtype == 18:  # Oracle 10G
  164.     # Requires username for hashing....
  165.     hash_string = "DISABLED FUNCTIONALITY: Oracle 10G"
  166.   elif hashtype == 19:  # Oracle 11G
  167.     try:
  168.       hash_string = oracle11.encrypt(self)
  169.     except:
  170.       hash_string = "ERROR: Problem Converting '%s' to Oracle 11G Hash Format!" % self
  171.   elif hashtype == 20:  # Django DES
  172.     try:
  173.       hash_string = django_des.encrypt(self)
  174.     except:
  175.       hash_string = "ERROR: Problem Converting '%s' to Django DES Crypt Hash Format!" % self
  176.   elif hashtype == 21:  # Django MD5
  177.     try:
  178.       hash_string = django_md5.encrypt(self)
  179.     except:
  180.       hash_string = "ERROR: Problem Converting '%s' to Django MD5 Hash Format!" % self
  181.   elif hashtype == 22:  # Django SHA1
  182.     try:
  183.       hash_string = django_sha1.encrypt(self)
  184.     except:
  185.       hash_string = "ERROR: Problem Converting '%s' to Django SHA1 Hash Format!" % self
  186.   else:                 # Unknown hash type, skip it
  187.       hash_string = "ERROR: Unknown Hash Format: %s:%s" % (hash_type_string, self)
  188.  
  189.   if hash_string.startswith('ERROR'):
  190.     print hash_string
  191.   else:
  192.     open(output, "a+").write(hash_string.upper() + "\n")
  193.   return hash_string
  194.  
  195.  
  196.  
  197. # Parse Arguments/Options
  198. parser = optparse.OptionParser(banner(), version="%prog v0.01b")
  199. parser.add_option("-s", "--single", dest="plain", default=None, type="string", help="Single Plaintext to Hash")
  200. parser.add_option("-w", "--wordlist", dest="wordlist", default=None, type="string", help="Wordlist for Hash Cracking")
  201. parser.add_option("-t", "--type", dest="hash_type", default=6, type="int", help="Hash Types: 1:Bcyrpt, 2:Cisco PIX, 3:Cisco Type7, 4:DES, 5:MD4, 6:MD5, 7:Unix(MD5), 8:SHA1, 9:SHA256, 10:SHA512, 11:LM, 12:MS-SQL 2000, 13:MS-SQL 2005, 14:MySQL 323, 15:MySQL 4.1+, 16:NTLM, 17:Postgres(MD5), 18:Oracle 10G, 19:Oracle 11G, 20:Django DES, 21:Django MD5, 22:Django SHA1")
  202. parser.add_option('-o', '--output', dest="outfile", default="converted.hashes", type="string", help="Custom Output Filename to Use")
  203.  
  204. (options, args) = parser.parse_args()
  205.  
  206. # Valid Hashtypes, update as needed
  207. valid_hash_types = { 1: 'Bcyrpt', 2: 'Cisco PIX', 3: 'Cisco Type7', 4: 'DES', 5: 'MD4', 6: 'MD5', 7: 'Unix(MD5)', 8: 'SHA1', 9: 'SHA256', 10: 'SHA512', 11:'LM', 12:'MS-SQL 2000', 13: 'MS-SQL 2005', 14: 'MySQL 323', 15: 'MySQL 4.1+', 16: 'NTLM', 19: 'Oracle 11G', 20: 'Django DES', 21: 'Django MD5', 22: 'Django SHA1' }
  208.  
  209. # Check to confirm we have a valid hash_type requested
  210. valid=False
  211. try:
  212.   usropt=valid_hash_types[options.hash_type]
  213.   for i in valid_hash_types.values():
  214.     if i.upper().startswith(usropt.upper()):
  215.       hash_type_string = i.upper()
  216.       valid=True
  217. except:
  218.   print "ERROR: Invalid Hash Type Provided!"
  219.   print "Can't continue as a result...\n\n"
  220.   sys.exit()
  221.  
  222. if not valid:
  223.   print "Invalid Hash Type Provided!"
  224.   print "Can't continue as a result...\n\n"
  225.   sys.exit()
  226.  
  227. # Output File to write any cracked hashes to
  228. output = options.outfile.split('.')[0] + '-' + hash_type_string + '.hashes'
  229.  
  230.  
  231. def main():
  232.   """Start the Magic Show..."""
  233.   try:
  234.     if options.plain:
  235.       # Single plaintext, hash with all formats
  236.       hasher(options.plain, 0)
  237.     elif options.wordlist:
  238.       # Wordlist, convert to new hash list
  239.       # Check the file exists, then check and confirm we can access said file
  240.       if os.path.isfile(options.wordlist):
  241.         if os.access(options.wordlist, os.R_OK):
  242.           pass
  243.         else:
  244.           print "Error: Unable to access provided wordlist: %s" % options.wordlist
  245.           print "Can't continue without a valid wordlist...\n\n"
  246.           sys.exit()
  247.       else:
  248.         print "Error: Unable to locate provided wordlist: %s" % options.wordlist
  249.         print "Can't continue without a valid wordlist...\n\n"
  250.         sys.exit()
  251.  
  252.       print "Converting %s to %s based hash list...." % (options.wordlist, hash_type_string)
  253.       fh = open(options.wordlist)
  254.       for line in fh:
  255.         hasher(line.strip(), options.hash_type)
  256.  
  257.   # Catch Ctrl-C Interupt Signal
  258.   except KeyboardInterrupt:
  259.     print "\nWARNING: CTRL+C Detected, Shutting Down...\n\n"
  260.     sys.exit(666)
  261.   except EOFError:
  262.     print "\nEOF ERROR: Shutting Down...\n\n"
  263.   except SystemExit:
  264.     pass
  265.   except:
  266.     e = sys.exc_info()
  267.     print "\nERROR Detected: Shutting Things Down...\n\n"
  268.     for error in e:
  269.       print error
  270.   finally:
  271.     e = sys.exc_info()[1]
  272.     if e != None:
  273.       print e
  274.     print "\nAll Finished!\nNew Hash File Saved to: %s\n\n" % output
  275.     sys.exit(0)
  276.  
  277. if __name__ == '__main__':
  278.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement