8.1 目录扫描工具

8.1.1 目录扫描工具开发

1.先导入需要用的库
Import requests

  1. 我们需要一个扫描的目录字典
    Dir.txt

  2. 通过遍历目录字典内容来访问url,根据返回结果来判断是否存在该目录

import requests
from time import sleep
headers = {
‘Accept’: ‘text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8’,
‘Accept-Encoding’: ‘gzip, deflate, compress’,
‘Accept-Language’: ‘en-us;q=0.5,en;q=0.3’,
‘Cache-Control’: ‘max-age=0’,
‘Connection’: ‘keep-alive’,
‘User-Agent’: ‘Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0’
}

def dirscan(url):
dir=[‘DIR.txt’,’ASP.txt’]
for d in dir:
print(‘当前使用的字典是:’,d)
f=open(d,’r’)
for dir in f:
dir=dir.replace(‘\n’,’’)
try:
cod=[200,301,302,403,500]
re=requests.get(url+dir,headers=headers,timeout=2)
if re.status_code in cod:
print(url+dir,’—————————-‘,re.status_code)
sleep(2)
except:
pass

dirscan(‘http://192.168.0.240:8004‘)

8.1.2 多线程目录扫描工具开发

  1. 导入第三方库
    Import requests
    import threadpool

  2. 创建获取字典的函数
    import requests
    import threadpool

dirall=[]
def loaddir():
dirlist=[‘DIR.txt’,’ASP.txt’]
for d in dirlist:
f=open(d,’r’)
for dir in f:
dir=dir.replace(‘\n’,’’)
dirall.append(dir)

print(dirall)
loaddir()
print(dirall)

第三步:创建扫描函数

def dirscan(url):
domain=’http://192.168.0.240:8004'+url
cod=[200,301,302,403,500]
try:
re=requests.get(domain,timeout=2)
if re.status_code in cod:
print(domain,re.status_code,’———-存在’)
except:
Pass

第四步:验证扫描函数是否可以用

loaddir()
for u in dirall:
dirscan(u)

第六步:我们使用threadpool多线程加载字典扫描

pool=threadpool.ThreadPool(10) #定义线程池数
reqs=threadpool.makeRequests(dirscan,dirall) #定义函数和字典
[pool.putRequest(req) for req in reqs] #遍历执行线程池
pool.wait()

完整代码:

import requests
import threadpool
dirall=[]
def loaddir():
dirlist=[‘DIR.txt’,’ASP.txt’]
for d in dirlist:
f=open(d,’r’)
for dir in f:
dir=dir.replace(‘\n’,’’)
dirall.append(dir)

def dirscan(url):
domain=’http://192.168.0.240:8004'+url
cod=[200,301,302,403,500]
try:
re=requests.get(domain,timeout=2)
if re.status_code in cod:
print(domain,re.status_code,’———-存在’)
except:
pass
loaddir()
pool=threadpool.ThreadPool(30) #定义线程池数
reqs=threadpool.makeRequests(dirscan,dirall) #定义函数和字典
[pool.putRequest(req) for req in reqs] #遍历执行线程池
pool.wait()