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_ ...
随机推荐
- 【codevs2011】最小距离之和 [LNOI2013](Floyd)
题目网址:http://codevs.cn/problem/2011/ 题目大意:有一个图,每次删一条边(可以重复删),求每次删边之后所有点对的最短距离之和. 看了一眼题目,顿时发现了O(n^4)的暴 ...
- springboot---aop切片编程
1.介绍 面向切面编程,关注点代码与业务代码分离,就是给指定方法执行前执行后..插入重复代码 关注点:重复代码 切面:被切面的类 切入点:执行目标对象方法,动态植入切片代码 2.部署步骤 2.1:添加 ...
- 泛型学习第一天:List与IList的区别 (三)
已经有很多人讨论过IList和List的区别,恩,我也赞同其中的一些观点,其实他们二者也是有优有劣的,看你着重用在哪个方面,先贴一下我赞同的意见,基本上也都是网友们总结的. 首先IList 泛型接口是 ...
- Maven——安装配置
MAVEN 一.介绍:(待填) 二.下载:http://maven.apache.org/download.cgi(官网下载) 选择二进制的zip文件,这种的可直接使用. 三.环境配置 1.前提条件: ...
- SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法
软件152 尹以操 首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml ...
- nova shelve
当一个虚机不需要使用的时候,可以将其 shelve 起来.该操作会创建该虚机的一个快照并传到 Glance 中,然后在 Hypervisor 上将该虚机删除,从而释放其资源. 其主要过程为: dest ...
- java中如何将string 转化成long
1.Java中如何将string 转化成long long l = Long.parseLong([String]); 或 long l = Long.parseLong([String],[int ...
- Range 函数 与break 用法
range 函数,这个比什么java ,C++的for (int i = 0; i < 5; i++),确实舒服很多. 写这么一句就可以了 for i in range(0,5). 翻译一遍更容 ...
- Python3.6.0安装
1.安装 具体详情请参考下图: 双击安装包: 勾选“add python 3.6 to PATH”这样可以自动生成环境变量,选择“Customize installation”自定义安装. 2. ...
- Django REST_framework Quickstart
局部避免crsf的方式 针对视图函数: from django.views.decorators.csrf import csrf_exempt @csrf_exempt def foo(reques ...