Advertisement
The_KGB

[Py] SHA-1 MySQL Brute

May 2nd, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.07 KB | None | 0 0
  1. import sys, subprocess, hashlib
  2.  
  3. try:
  4.     import MySQLdb
  5. except:
  6.     print "\n[-] MySQLdb not installed on system!"
  7.     choo = raw_input("\nWould you like to install it (if want press yes or y)?")
  8.     if choo == "yes" or choo == "y":
  9.         subprocess.call("sudo apt-get install python-mysqldb", shell=True) # Works with me, tested on Kubuntu 12.04
  10.         print "\n[!] MySQLdb installed!"
  11.         print "[!] Now exiting, please try script again!"
  12.         sys.exit(1)
  13.     else:
  14.         print "\n[+] If you using Ubuntu based system, try: sudo apt-get install python-mysqldb"
  15.         print "[!] Thanks for using script, please visit b4ltazar.wordpress.com & ljuska.org"
  16.         print "[!] Now exiting ..."
  17.         sys.exit(1)
  18.    
  19.    
  20.  
  21. def logo():
  22.   print "\n|---------------------------------------------------------------|"
  23.   print "| b4ltazar[@]gmail[dot]com                                      |"
  24.   print "|   04/2012     sha1db.py    v.0.1 (based on low1z script)      |"
  25.   print "|    b4ltazar.wordpress.com     &      ljuska.org               |"
  26.   print "|                                                               |"
  27.   print "|---------------------------------------------------------------|\n"
  28.  
  29.  
  30. if sys.platform == 'linux' or sys.platform == 'linux2':
  31.     subprocess.call("clear", shell=True)
  32.     logo()
  33. else:
  34.     subprocess.call("cls", shell=True)
  35.     logo()
  36.    
  37. dbname = 'sha1db'
  38. #####################################################################
  39. DB = MySQLdb.connect(host='127.0.0.1', user='root', passwd='root') # You should change this line with your login details
  40. #####################################################################
  41.  
  42. def DBconnect():
  43.     consrv = DB.cursor()
  44.     return consrv
  45.  
  46. def setupDB():
  47.     consrv = DBconnect()
  48.     try:
  49.         consrv.execute("CREATE DATABASE "+dbname)
  50.         print "[+] Database:", dbname
  51.         print "[+] Status  : created"
  52.     except MySQLdb.Error, e:
  53.         print "[-] Error %s" % (e.args[1])
  54.         sys.exit(1)
  55.     try:
  56.         consrv.execute("CREATE TABLE "+dbname+".data (id INT( 255 ) NOT NULL AUTO_INCREMENT ,plain TEXT NOT NULL ,sha1 VARCHAR( 255 ) NOT NULL ,PRIMARY KEY ( id ) , UNIQUE ( sha1 )) ENGINE = MYISAM;")
  57.         print "[+] Tables in db:", dbname, "created, Database ready to use!"
  58.     except MySQLdb.Error, e:
  59.         print "[-] Error %s" % (e.args[1])
  60.         sys.exit(1)
  61.        
  62. def importword():
  63.     counter = 0
  64.     try:
  65.         words = open(wordlist, "r")
  66.     except(IOError):
  67.         print "[-] Error: check", wordlist
  68.         sys.exit(1)
  69.     duplicates = 0
  70.     print "[+] Inserting wordlist, skipping duplicates ... may take some time"
  71.     for word in words.read().split('\n'):
  72.         hash = hashlib.sha1(word).hexdigest()
  73.         counter = counter + 1
  74.         try:
  75.             consrv = DBconnect()
  76.             consrv.execute("INSERT INTO "+dbname+".data (plain, sha1)VALUES ('"+str(word)+"', '"+str(hash)+"');")
  77.         except MySQLdb.Error, e:
  78.             duplicates = duplicates + 1
  79.     print "\n[+] Duplicates:", duplicates
  80.    
  81. def single(shash):
  82.     consrv = DBconnect()
  83.     consrv.execute("SELECT plain FROM "+dbname+".data WHERE sha1 = '"+shash+"'")
  84.     dset = consrv.fetchone()
  85.     if dset == None:
  86.         print "[!]",shash, ":", "not in DB"
  87.     else:
  88.         print "[!]",shash, ":", dset[0]
  89.     consrv.close()
  90.    
  91. def dropDB():
  92.     consrv = DBconnect()
  93.     try:
  94.         consrv.execute("DROP DATABASE "+dbname)
  95.         print "[+] Database:", dbname
  96.         print "[+] Status  : deleted"
  97.     except MySQLdb.Error, e:
  98.         print "[-] Error %s" % (e.args[1])
  99.         sys.exit(1)
  100.        
  101. def statusDB():
  102.     try:
  103.         consrv = DBconnect()
  104.         consrv.execute("SELECT COUNT(id) AS num FROM "+dbname+".data")
  105.         DBcount = consrv.fetchone()
  106.         return DBcount[0]
  107.         consrv.close()
  108.     except MySQLdb.Error, e:
  109.         print "[-] Error %s" % (e.args[1])
  110.         sys.exit(1)
  111.  
  112. if len(sys.argv) <= 1:
  113.     print "Usage: "
  114.     print "-s   initial DB installation"
  115.     print "-w   insert wordlist into DB"
  116.     print "-d   delete DB"
  117.     print "-sh  search DB for given hash"
  118.     print "-c   count DB entries\n"
  119.     print "[!] Thanks for using script, please visit b4ltazar.wordpress.com & ljuska.org"
  120.     sys.exit(1)
  121.    
  122.    
  123.  
  124.    
  125. for arg in sys.argv[1:]:
  126.     if arg.lower() == "-s":
  127.         setupDB()
  128.        
  129.     if arg.lower() == "-sh":
  130.         try:
  131.             shash = sys.argv[2]
  132.             if len(shash) != 40:
  133.                 print "[-] Invalid sha1 supplied, check your input!"
  134.                 sys.exit(1)
  135.             single(shash)
  136.         except(IndexError):
  137.             print "[-] Error: check hash!"
  138.            
  139.     if arg.lower() == "-w":
  140.         try:
  141.             wordlist = sys.argv[2]
  142.             importword()
  143.         except(IndexError):
  144.             print "[-] Error, check your wordlist path!"
  145.            
  146.     if arg.lower() == "-d":
  147.         dropDB()
  148.        
  149.     if arg.lower() == "-c":
  150.         print "[+] Checking for number of entries in DB!"
  151.         print "[!]", statusDB(), " entries in DB"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement