Python打造一个目录扫描工具
目标:用Python3写一款小型的web目录扫描工具
功能:1.扫描指定站点
2.指定网站脚本类型来扫描
3.可控线程
4.可保存扫描结果
首先定义一个命令参数的函数
def parse_option():
parser = argparse.ArgumentParser(description="The Argument of DirScan")
parser.add_argument("-u","--url",dest="url",help="The Url to Scan")
parser.add_argument("-e","--extensions",dest="extensions",help="Web Extensions")
parser.add_argument("-t","--thread",dest="thread",default=10,type=int,help="The Thread to Scan")
parser.add_argument("-r","--report",action="store_true",help="Save The Result of Scan")
args = parser.parse_args()
return args
定义了4个参数,-u用来指定扫描的url,-e用来指定网站类型,-t用来指定线程数,默认为10,-r为保存扫描结果
参数的值传入变量args,然后返回
然后定义一个开始的函数,对url进行处理和启用多线程
def start(url, ext, count):
queue = Queue() #生成一个队列
print('\033[34;1m Status Url Path\033[0m')
global url_r #设置为全局变量
#对传入的url进行处理
if url.endswith("/"):
url = url.rstrip("/")
#对url进行处理方便写入结果
if url[4] == 's':
url_r = url.lstrip("https://")
if url[4] == ':':
url_r = url.lstrip("http://")
#打开字典,并和url进行拼接
f = open('%s.txt' %ext,'r')
for i in f:
queue.put(url + i.rstrip('\n'))
threads = []
thread_count = count
for i in range(thread_count):
threads.append(Dirscan(queue)) #调用多线程
for t in threads:
t.start() #开启多线程
for t in threads:
t.join() #等待
这里用类方法来进行多线程的调用
class Dirscan(threading.Thread):
def __init__(self,queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while not self.queue.empty():
url_path = self.queue.get() #从队列里获取url
#随机user-agents
u = open("user-agents.txt", "r")
headers_list = []
random_headers = {}
for i in u:
headers_list.append(i.strip())
random_headers["user-agents"] = random.choice(headers_list)
headers = random_headers
try:
r = requests.get(url=url_path, headers=headers, timeout=6,allow_redirects=False)
status_code = r.status_code
if status_code == 200:
print("\033[32;1m[+] [%s] %s\033[0m"%(status_code,url_path))
if cmd_args.report: #如果有传入-r参数,则执行write_report()函数来写扫描结果
write_report(url_path)
return url_path
except Exception as e:
print("\033[41;1m%s\033[0m"%e)
然后是写扫描结果的函数
def write_report(url):
with open("%s.html"%url_r, "a") as r:
r.write('<a href="'+url+'" target="_blank">'+url+'</a><br>')
r.close()
最后是程序的入口
if __name__ == '__main__':
cmd_args = parse_option() #把parse_option()返回的args赋给变量cmd_args
url = cmd_args.url #把args中的url传给变量url
extension = cmd_args.extensions #把args中的extensions传给变量extension
thread = cmd_args.thread #把args中的thread传给变量thread
start(url,extension,thread) #把三个参数传入start()函数
以html格式保存扫描结果,也方便直接打开扫描结果
最后的完整代码
# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"
import requests
import argparse
import threading
from queue import Queue
import random
class Dirscan(threading.Thread):
def __init__(self,queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while not self.queue.empty():
url_path = self.queue.get()
u = open("user-agents.txt", "r")
headers_list = []
random_headers = {}
for i in u:
headers_list.append(i.strip())
random_headers["user-agents"] = random.choice(headers_list)
headers = random_headers
try:
r = requests.get(url=url_path, headers=headers, timeout=6,allow_redirects=False)
status_code = r.status_code
if status_code == 200:
print("\033[32;1m[+] [%s] %s\033[0m"%(status_code,url_path))
if cmd_args.report:
write_report(url_path)
return url_path
except Exception as e:
print("\033[41;1m%s\033[0m"%e)
def write_report(url):
with open("%s.html"%url_r, "a") as r:
r.write('<a href="'+url+'" target="_blank">'+url+'</a><br>')
r.close()
def start(url, ext, count):
queue = Queue()
print('\033[34;1m Status Url Path\033[0m')
global url_r
if url.endswith("/"):
url = url.rstrip("/")
if url[4] == 's':
url_r = url.lstrip("https://")
if url[4] == ':':
url_r = url.lstrip("http://")
f = open('%s.txt' %ext,'r')
for i in f:
queue.put(url + i.rstrip('\n'))
threads = []
thread_count = count
for i in range(thread_count):
threads.append(Dirscan(queue))
for t in threads:
t.start()
for t in threads:
t.join()
def parse_option():
parser = argparse.ArgumentParser(description="The Argument of DirScan")
parser.add_argument("-u","--url",dest="url",help="The Url to Scan")
parser.add_argument("-e","--extensions",dest="extensions",help="Web Extensions")
parser.add_argument("-t","--thread",dest="thread",default=10,type=int,help="The Thread to Scan")
parser.add_argument("-r","--report",action="store_true",help="Save The Result of Scan")
args = parser.parse_args()
return args
if __name__ == '__main__':
cmd_args = parse_option()
url = cmd_args.url
extension = cmd_args.extensions
thread = cmd_args.thread
start(url,extension,thread)
Python打造一个目录扫描工具的更多相关文章
- 目录扫描工具 dirsearch 使用详解
介绍 dirsearch 是一个python开发的目录扫描工具.和我们平时使用的dirb.御剑之类的工具一样,就是为了扫描网站的敏感文件和目录从而找到突破口. 特点 多线程 可保持连接 支持多种后缀( ...
- phantomjs + python 打造一个微信机器人
phantomjs + python 打造一个微信机器人 1.前奏 媳妇公司不能上网,但经常需要在公众号上找一些文章做一些参考,需要的时候就把文章链接分享给我,然后我在浏览器打开网页,一点点复制过 ...
- 渗透测试工具篇之目录扫描工具dirmap
(一)dirmap介绍 一个高级web目录扫描工具,功能将会强于DirBuster.Dirsearch.cansina.御剑 (二)dirmap安装 打开浏览器输入https://github.com ...
- python遍历一个目录,输出所有文件名
python遍历一个目录,输出所有文件名 python os模块 os import os def GetFileList(dir, fileList): newDir = dir if os. ...
- python之web路径扫描工具
# coding: UTF-8 import sys, os, time, httplibimport relist_http=[] #http数组 def open_httptxt(): #打开 ...
- Python读取一个目录下的所有文件
#!/usr/bin/python # -*- coding:utf8 -*- import os allFileNum = 0 def printPath(level, path): global ...
- python 读取一个目录下的所有目录和文件
#!/usr/bin/python # -*- coding:utf8 -*- import os allFileNum = 0 def printPath(level, path): global ...
- python 检索一个目录下所有的txt文件,并把文件改为.log
检索一个目录及子目录下所有的txt文件,并把txt文件后缀改为log: import os f_path = r'C:\Users\PycharmProjects\mystudy\Testfolder ...
- python 删除一个目录下的所有文件
一个目录下有文件,文件夹,文件夹里又有文件.文件夹....用python脚本,实现,递归删除一个目录下的所有文件: 目录结构如下: 其中我们要删除所有文件 代码实现如下: import os CUR_ ...
随机推荐
- 电子商务的几种模式,b2b,c2c等
B2B(Business to Business) ——这是指商家与商家建立的商业关系.(最早的一种模式) C2C (Customer to Consumer) ——个人与个人的商业关系,也就是消费者 ...
- QT QDockWidget锚接部件 和 QTreeWidget 树形部件 构成树形选择项
1. 如图,在mainwindow中 添加DockWidget到右侧,里面镶嵌TreeWidget. 2. QTreeWidget *treeWidget = new QTreeWidget; // ...
- wab框架
http协议 一.http简介 1.HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等). 2.HTTP是一个属于应用层的面向对象的协 ...
- network namespace连接的4种方法及性能
veth pair # add the namespaces ip netns add ns1 ip netns add ns2 # create the veth pair ip link add ...
- review34
Thread类与线程的创建 让线程启动时使用我们自己创建run()的两种方式:一种是继承Thread类,实现其中的run()方法,然后用继承的类用无参构造方法创建对象就可以了.第二种是实现Runnab ...
- 《Think in Java》(七)复用类
Java 中复用代码的方式就是复用类,复用类的方式有: 组合 继承 代理(并没有啥高深的含义,只是在使用类A前,新增了类B,让类B的每个方法去调用类A中对应的方法,也就是说类B代理了类A...不过我还 ...
- Q&A:string、vector、iterator、bitset
细节要点 getline(cin,string)与cin>>string 在VS2013中通过输入换行符\n,对getline以及cin的用法进行测试,但是并没有像文中所述遇到换行符停止读 ...
- hdu4217splay
题意:有1到n的数组,每次删除第k小的值,并求和 题解:splay基本操作,删除+合并 坑点:由于不会c++指针操作,sb的只删除了头指针导致一直mle #include<bits/stdc++ ...
- 16-THREE.JS 半球光
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- 10-THREE.JS perspective透视摄像机和orthographic正交摄像机区别
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...