浅谈扫描器之加速篇

作者:CQITer小编 时间:2018-09-03 01:25

字号

扫描器.jpg


扫描器.jpg


众所周知,在渗透测试中,除了内网和敏感线上环境,我们会尽可能用上高效的扫描器。虽然说打造扫描神器主要是靠规则和POC,不过它们也需要稳定而健壮的引擎,这就不得不谈到如何有效地对单线程脚本进行加速了。

为了方便描述,笔者这里会拿python的库来举例,部分代码采集自网络。本文会向大家简要评析一些能加快扫描速率的库。希望借此帮大家规避掉一些坑,很多点也是具有普适性的。

线程 多线程

threading

用法比较简单,普通速成小脚本建议用这个库,比如在扫描主机存活或者探测URL路径是否存在的时候。

#coding:utf-8 import threading import time def action(arg): time.sleep(1) print 'sub thread start!the thread name is:%s ' % threading.currentThread().getName() print 'the arg is:%s ' %arg time.sleep(1) for i in xrange(4): t =threading.Thread(target=action,args=(i,)) t.setDaemon(True) t.start() t.join() print 'main_thread end!'

thread

有的朋友可能会问,有没有更简单的,老夫不懂那么多,只想一把梭! 当然有,很早以前笔者也曾喜欢使用这个库:

#coding=gbk import thread, time, random count = 0 def threadTest(): global count for i in xrange(10000): count += 1 for i in range(10): thread.start_new_thread(threadTest, ()) time.sleep(3) print count

不过thread.start_new_thread有个比较明显的缺点,因为起了新线程是不好控制的,一旦任务挂起过多,会占用较多的机器资源,所以建议在检测目标量不大的时候使用。

线程池

threadpool

说实在这库还是比较好用的,在无序输出结果等情况下比较稳健,尤其是它在win平台下兼容性是比较好的。 不过需要注意,就是如果不加锁的话,需要先做数据聚合。直接按序写入文件,或者直接入库的话,数据会乱掉。

import time import threadpool def sayhello(str): print "Hello ",str time.sleep(2) name_list =['xiaozi','aa','bb','cc'] start_time = time.time() pool = threadpool.ThreadPool(10) requests = threadpool.makeRequests(sayhello, name_list) [pool.putRequest(req) for req in requests] pool.wait() print '%d second'% (time.time()-start_time)

concurrent.futures

该库是python 3.x自带的,但python 2.x也能用,相对来说会比threadpool更优化的多一些,毕竟新库嘛。

#! /usr/bin/env python # -*- coding: utf-8 -*- from concurrent.futures import ThreadPoolExecutor import time def sayhello(a): print("hello: "+a) time.sleep(2) def main(): seed=["a","b","c"] start1=time.time() for each in seed: sayhello(each) end1=time.time() print("time1: "+str(end1-start1)) #submit提交 start2=time.time() with ThreadPoolExecutor(3) as executor: for each in seed: executor.submit(sayhello,each) end2=time.time() print("time2: "+str(end2-start2)) #map提交 start3=time.time() with ThreadPoolExecutor(3) as executor1: executor1.map(sayhello,seed) end3=time.time() print("time3: "+str(end3-start3)) if __name__ == '__main__': main()

我们可以看看上面的代码注释,其中submit和map的区别在于:

map可以保证输出的顺序, submit输出的顺序是乱的。

如果你要提交的任务的函数是一样的,就可以简化成map。但是假如提交的任务函数是不一样的,或者执行的过程之可能出现异常(使用map执行过程中发现问题会直接抛出错误)就要用到submit。

责任编辑:CQITer新闻报料:400-888-8888   本站原创,未经授权不得转载
继续阅读
热新闻
推荐
关于我们联系我们免责声明隐私政策 友情链接