python编写基于字典的网站目录扫描工具

工具原理:

  • 通过读取字典获取内容,拼接url执行get http请求获取
    响应状态码,根据状态码判断目录文件资源是否存在
    

思路:

  •   工具命令行参数获取
    
  •  字典读取
    
  • 多线程访问
    
  •    状态码获得判断输出结果
    

工具初始化

  定义一个banner信息函数 def banner()
  用于介绍工具与名称
def banner():
    print("*" * 51)
    print("*" * 2 + " " * 17 + "DirBurte v1.0" + ' ' * 17 + "*" * 2)
    print('*' * 51)
    print("This tool just debvlop for education!")

使用方法信息函数:

 def usage():使用方法
 1. url    
 2. thread
 3. dictionary
def usage():
        print("This is the tool's usage")
        print("python Dirbrute.py -r u url -t thread -d dictionary")

参数获取

 模块介绍
  •   sys sys.argv获取python命令行执行的数据,sys.argv[0]
    
  •  getopt 	python自带的解析命令行参数模块
    

参数获得

  •  opts,args = getopt.getopt(sys.argv[1:],"u:t:d")	
     每一个参数都有一个值,传递给opts,输出opts
     可以看到是一个列表类型
    
  •  根据使用方法,可知len(sys.argv)等于7才能执行
     将参数获得的内容封装到start函数中
    
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()的时候才会关闭
    
  • 不调用的话,文件流会一直处于打开状态
    即是占用资源又占用文件,使得无法进行其他操作
    例如删除等   
    
  • 多线程思路
    一个线程读取固定数目的字典文件内容
    制作多线程使用的字典列表,存储都是以列表格式
    
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()

发布了12 篇原创文章 · 获赞 0 · 访问量 579

猜你喜欢

转载自blog.csdn.net/weixin_44344395/article/details/104810321