我想要建立一个工具来扫描一个网站的子域,我知道如何做他的,但我的功能比较慢,我查了一下gobuster的使用,我看到gobuster可以使用许多并发线程,我如何实现这一点?
我问过谷歌很多次了,但我看不出这方面的任何情况,有人能给我举个例子吗?
gobuster usage: -t Number of concurrent threads (default 10)
我的当前程序:
def subdomaines(url, wordlist):
checks(url, wordlist) # just checking for valid args
num_lines = get_line_count(wordlist) # number of lines in a file
count = 0
for line in open(wordlist).readlines():
resp = requests.get(url + line) # resp
if resp.status_code in (301, 200):
print(f'Valid - {line}')
print(f'{count} / {num_lines}')
count += 1注:gobuster是一个快速搜索网站子域的工具。
发布于 2020-05-08 18:24:32
如果您想在python中使用线程,您应该从基础开始,了解可用的内容。但是下面是一个从https://pymotw.com/2/threading/获取的简单例子
import threading
def worker():
"""thread worker function"""
print 'Worker'
return
threads = []
for i in range(5):
t = threading.Thread(target=worker)
threads.append(t)
t.start()要将其应用于您的任务,一个简单的方法是为每个请求生成一个线程。就像下面的代码。注意:如果你的单词列表很长,这可能会非常昂贵。查看python中的一些线程池库,以便更好地管理不需要显式控制自己的线程。
import threading
def subdomains(url, wordlist):
checks(url, wordlist) # just checking for valid args
num_lines = get_line_count(wordlist) # number of lines in a file
count = 0
threads = []
for line in open(wordlist).readlines():
t = threading.Thread(target=checkUrl,args=(url,line))
threads.append(t)
t.start()
for thread in threads: #wait for all threads to complete
thread.join()
def checkUrl(url,line):
resp = requests.get(url + line)
if resp.status_code in (301, 200):
print(f'Valid - {line}')要实现计数器,您需要控制线程之间的共享访问,以防止争用条件(两个线程同时访问变量,从而导致.问题)。在上面的链接中提供了一个具有受保护访问的计数器对象:
class Counter(object):
def __init__(self, start=0):
self.lock = threading.Lock()
self.value = start
def increment(self):
#Waiting for lock
self.lock.acquire()
try:
#Acquired lock
self.value = self.value + 1
finally:
#Release lock, so other threads can count
self.lock.release()
#usage:
#in subdomains()...
counter = Counter()
for ...
t = threading.Thread(target=checkUrl,args=(url,line,counter))
#in checkUrl()...
c.increment()最后注意:我没有编译或测试任何这段代码。
发布于 2020-05-08 18:03:50
Python有穿线模块。
使用Thread最简单的方法是使用目标函数实例化它,并调用start()让它开始工作。
import threading
def subdomains(url, wordlist):
checks(url, wordlist) # just checking for valid args
num_lines = get_line_count(wordlist) # number of lines in a file
count = 0
for line in open(wordlist).readlines():
resp = requests.get(url + line) # resp
if resp.status_code in (301, 200):
print(f'Valid - {line}')
print(f'{count} / {num_lines}')
count += 1
threads = []
for i in range(10):
t = threading.Thread(target=subdomains)
threads.append(t)
t.start()https://stackoverflow.com/questions/61684992
复制相似问题