Web目錄掃描工具webdirscan.py編寫 2016-03-28 #python #webdirscan #web目錄掃描工具
寫在前面
一直沒有找到一款自己喜歡的跨平臺的web目錄掃描工具,因為我自己電腦裝的是ubuntu,畢竟買不起mac
然后ubuntu下也沒有找到幾個我自己滿意的工具,所以最終決定:自己擼?。?!
想法:預期功能
- 可指定初始掃描目錄(可選,默認根目錄)
- 可設置文件后綴(可選,默認php)
- 可設置線程數(可選,默認20)
- 字典分為多種分類
想法:一些不錯的思路
- 遇到
/a/
目錄,然后掃描a.rar
、a.zip
等文件 - 遇到
/1.php
,然后掃描1.php.bak
、1.php.swp
等、 - 遇到有參數的網址以及所有表單,扔給sqlmap API進行sql注入檢測
- 掃描
robots.txt
并且訪問里面的目錄,探測是否可以列目錄
開發:最基礎的目錄掃描
首先我們把最基礎的遍歷字典,掃描目錄寫出來,代碼如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import argparse
import requests
# 命令行傳值
parser = argparse.ArgumentParser()
parser.add_argument('website',help="Website for scan, eg: http://www.secbox.cn | www.secbox.cn",type=str)
args = parser.parse_args()
# 字典設置
webdic = 'dict/dict.txt'
# 對輸入的網址進行處理
website = args.website
# 請求頭設置
headers = {
'Accept': '*/*',
'Referer': website,
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; ',
'Connection': 'Keep-Alive',
'Cache-Control': 'no-cache',
}
# 字典存入數組
webdict = []
with open(webdic) as infile:
while True:
dirdic = infile.readline().strip()
if(len(dirdic) == 0): break
webdict.append(website+dirdic)
?
for url in webdict:
try:
respon = requests.get(url, headers=headers)
except Exception,e:
print url
print e
if(respon.status_code == 200):
print '['+str(respon.status_code)+']' + ":" + url
最簡單的功能已經搞定,但是還有很多坑要補,比如有的網站所有頁面都返回200,這個時候就要做一些措施,下一步打算把提高掃描頁面的準確性,判斷頁面是否存在多加幾個標準:
- 首先訪問
songgeshuozhegeyemianbucunzai/strikersb666.php
和onggeshuozhegeyemianbucunzai/strikersb555.php
判斷兩個文件內容是否一樣 - 如果一樣表示這個內容是404的內容,然后在后來的掃描中,返回長度等于這個長度的判定為404
開發:對不存在的頁面也返回200進行兼容
有的網站訪問不存在的頁面為了SEO同樣會返回200或者302,此時就需要做兼容,我這里的解決辦法是:請求時禁止302跳轉,然后抓取一個404頁面,判斷文件存在的條件是返回狀態碼200且與404頁面內容不一樣,具體代碼如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import re
import argparse
import requests
from termcolor import colored
# 版權區域
mycopyright = '''
*****************************************************
Web目錄掃描工具 - webdirscan.py
作者:王松_Striker
郵箱:song@secbox.cn
團隊:安全盒子團隊[SecBox.CN]
*****************************************************
'''
print colored(mycopyright,'cyan')
# 命令行傳值
parser = argparse.ArgumentParser()
parser.add_argument('website',help="Website for scan, eg: http://www.secbox.cn | www.secbox.cn",type=str)
args = parser.parse_args()
# 字典設置
webdic = 'dict/dict.txt'
# 對輸入的網址進行處理
website = args.website
pattern = re.compile(r'^[http\:\/\/|https\:\/\/]')
res = pattern.match(website)
if not(res):
website = 'http://' + website
# 請求頭設置
headers = {
'Accept': '*/*',
'Referer': website,
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; ',
'Cache-Control': 'no-cache',
}
# 字典存入數組
webdict = []
with open(webdic) as infile:
while True:
dirdic = infile.readline().strip()
if(len(dirdic) == 0): break
webdict.append(website+dirdic)
# 404頁面分析,避免有的網站所有頁面都返回200的情況
notfoundpage = requests.get(website+'/songgeshigedashuaibi/hello.html',allow_redirects=False)
# 遍歷掃描過程
for url in webdict:
try:
respon = requests.get(url, headers=headers,timeout=30,allow_redirects=False)
except Exception,e:
print e
if(respon.status_code == 200 and respon.text != notfoundpage.text):
print colored('['+str(respon.status_code)+']','green') + " " + url
簡單的掃描基本已經搞定了,下一步打算做一下視覺上的優化,比如顯示進程:有多少待檢測、有多少正在檢測等等