python 追加到字典_扫描器篇(三)之python编写基于字典的网站目录探测脚本
工具原理:
通過讀取字典獲取內容,拼接url執行get http請求獲取
響應狀態碼,根據狀態碼判斷目錄文件資源是否存在
1
2
思路:
工具命令行參數獲取
1
字典讀取
1
多線程訪問
1
狀態碼獲得判斷輸出結果
1
工具初始化
定義一個banner信息函數 def banner()
用于介紹工具與名稱
1
2
def banner():
print("*" * 51)
print("*" * 2 + " " * 17 + "DirBurte v1.0" + ' ' * 17 + "*" * 2)
print('*' * 51)
print("This tool just debvlop for education!")
1
2
3
4
5
使用方法信息函數:
def usage():使用方法
1. url
2. thread
3. dictionary
1
2
3
4
def usage():
print("This is the tool's usage")
print("python Dirbrute.py -r u url -t thread -d dictionary")
1
2
3
參數獲取
模塊介紹
1
sys sys.argv獲取python命令行執行的數據,sys.argv[0]
1
getopt python自帶的解析命令行參數模塊
1
參數獲得
opts,args = getopt.getopt(sys.argv[1:],"u:t:d")
每一個參數都有一個值,傳遞給opts,輸出opts
可以看到是一個列表類型
1
2
3
根據使用方法,可知len(sys.argv)等于7才能執行
將參數獲得的內容封裝到start函數中
1
2
def start():
if len(sys.argv) == 7:
opts,argv = getopt.getopt(sys.argv[1:],'u:t:d:')
for k,v in opts:
if k == "-u":
url = v
elif k == "-t":
threads = v
elif k == "-d":
dic = v
multi_scan(url,threads,dic)
else:
print("error Argument")
sys.exit()
字典文件的讀取
python字典文件讀取
with open(filename,mode)as f:
f.readlines()
這里使用with open() as f的好處在于
如果打開一個字典文件或者其他文件,那么
這個文件流只有再調用close()的時候才會關閉
1
2
3
不調用的話,文件流會一直處于打開狀態
即是占用資源又占用文件,使得無法進行其他操作
例如刪除等
多線程思路
一個線程讀取固定數目的字典文件內容
制作多線程使用的字典列表,存儲都是以列表格式
def multi_scan(url,threads,dic):
with open("dir",'r') as f:
dic_list = f.readlines()#讀取字典
result_list = []#用來存放字典列表
threads_list = []#生成一個空的線程列表,后面用來追加子線程
##第二步確認字典行數
if len(dic_list) % int(threads) == 0:
threads_read_line_num = len(dic_list) / int(threads)
else:
threads_read_line_num = math.ceil(len(dic_list) / int(threads))
i = 0
temp_list = []
for line in dic_list:
i += 1
if i % threads_read_line_num == 0:
temp_list.append(line.strip())
result_list.append(temp_list)#向臨時列表追加每個線程的分配的字典內容
temp_list = []#里不把這個臨時的列表重置的話,下一個循環無法追加數據
else:
temp_list.append(line.strip())
for i in result_list:
threads_list.append(threading.Thread(target=scan,args=(url,i)))
for t in threads_list:
t.start()
線程列表
讀取字典列表中的內容
掃描scan函數
def scan(url,dic):
for line in dic:
r = requests.get(url+'/'+line)
if r.status_code == 200:
print(r.url+':'+str(r.status_code))
else:
pass
完整代碼為
#!/usr/bin/python3
import getopt
import sys
import math
import threading
import requests
def banner():
print("*"*51)
print("*"*2+" "*17+"DirBurte v1.0"+' '*17 +"*"*2)
print('*'*51)
print("This tool just debvlop for education!")
def usage():
print("This is the tool's usage")
print("python Dirbrute.py -r u url -t thread -d dictionary")
def start():
if len(sys.argv) == 7:
opts,argv = getopt.getopt(sys.argv[1:],'u:t:d:')
for k,v in opts:
if k == "-u":
url = v
elif k == "-t":
threads = v
elif k == "-d":
dic = v
multi_scan(url,threads,dic)
else:
print("error Argument")
sys.exit()
def multi_scan(url,threads,dic):
with open("dir",'r') as f:
dic_list = f.readlines()
result_list = []
threads_list = []
if len(dic_list) % int(threads) == 0:
threads_read_line_num = len(dic_list) / int(threads)
else:
threads_read_line_num = math.ceil(len(dic_list) / int(threads))
i = 0
temp_list = []
for line in dic_list:
i += 1
if i % threads_read_line_num == 0:
temp_list.append(line.strip())
result_list.append(temp_list)
temp_list = []
else:
temp_list.append(line.strip())
for i in result_list:
threads_list.append(threading.Thread(target=scan,args=(url,i)))
for t in threads_list:
t.start()
def scan(url,dic):
for line in dic:
r = requests.get(url+'/'+line)
if r.status_code == 200:
print(r.url+':'+str(r.status_code))
else:
pass
start()
總結
以上是生活随笔為你收集整理的python 追加到字典_扫描器篇(三)之python编写基于字典的网站目录探测脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 米家智能锁sn码在哪里
- 下一篇: python 英语词频统计软件_为了边看