多线程调用
threadsDo
这里已经封装好一个多线程调用函数(threadsDo),方便开发时调用。
def threadsDo(ThreadsFun, ThreadsNum, ArgsLst):
"""
:param ThreadsFun: function
:param ThreadsNum: threads num.
:param ArgsLst: args list.
if function accepts a parameter.
the ArgsLst format is like this (attention ","):
[(arg1,),(arg2,),(arg3,) ...]
if function accepts some parameters.
the ArgsLst format is like this:
[(arg11,arg12),(arg21,arg22),(arg31,arg32) ...]
[(arg11,arg12,arg13),(arg21,arg22,arg23),(arg31,arg32,arg33) ...]
...
"""
tp = ThreadPool(ThreadsNum)
for Args in ArgsLst:
tp.add_job(ThreadsFun, Args)
tp.start()
try:
tp.wait_for_complete()
except KeyboardInterrupt:
tp.stop()
调用时请先将参数放在列表中,可以定义一个retArgs函数处理。
示例
...snip...
def portscan(self, ip, port, timeout, verbose=False):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
try:
s.connect((ip, port))
except KeyboardInterrupt:
s.close()
raise
except Exception, e:
s.close()
return False
s.close()
color.echo("[*] %s port: %d status: open" % (ip, port), GREEN)
return True
def retArgs(self, rhosts, ports, timeout):
argsLst = []
for rhost in rhosts:
for port in ports:
argsLst.append((rhost, int(port), timeout))
return argsLst
def exploit(self):
rhosts = self.options['rhosts']
timeout = self.options['timeout']
ports = len(self.options['ports']) and retList(self.options['ports']) or self.ports
threads = int(self.options['threads'])
rhosts = retRhosts(rhosts)
if rhosts:
color.echo("[*] all seems fine. go!", GREEN)
argsLst = self.retArgs(rhosts, ports, timeout)
threadsDo(self.portscan, threads, argsLst)
注意
1, 参数格式
2, 多线程运行的函数一定要将键盘中断信号raise出来,千万不能pass,否则不能及时响应用户的中断信号。