近期因为业务需要,需要大量探测url存活,进行渗透测试,在GitHub上面找了几个好用的脚本,在这里做记录一下。未标明出处,如有侵权,请联系我删除,请见谅!
第一种适合比较少的
import requests
with open(r'C:\Users\He1lo\Desktop\url.txt','r',encoding = 'utf-8') as f:#url打开的路径
u = f.readlines()
length = len(u)
for i in range(0,length):
a = u[i].strip()
try:
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'}
r = requests.get(u[i].strip(),headers = headers,timeout = 5 ).status_code
if r != 200:
print('无法连接'+':'+a)
else:
with open(r'C:\Users\He1lo\Desktop\正常url.txt' ,'a') as f:#提取访问正常的文件下载路径
f.write(a+'\n')
except:
print('连接超时'+':'+a)
第二种多线程,比较快
将要探测的url放在同目录下的urls.txt中
探测存活的url会输出到同目录下的results.txt中
python3 url_check.py
# -*- coding: utf-8 -*-
from threading import Thread
from queue import Queue
import requests
class UrlCheck(Thread):
def __init__(self, url_queue, url_list):
super().__init__()
self.url_queue = url_queue # 要探测的url队列
self.url_list = url_list # 存活的ip列表
self.headers = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) '
'AppleWebKit/605.1.15 (KHTML, like Gecko) '
'Version/13.0.3 Mobile/15E148 Safari/604.1'
}
def run(self):
while True:
try:
url = self.url_queue.get()
if 'http' in url:
head = self.download(url)
if head is None:
continue
self.url_list.append(url)
print(url, head)
else:
http_url = 'http://' + url
head = self.download(http_url)
if head:
self.url_list.append(http_url)
print(http_url, head)
https_url = 'https://' + url
head = self.download(https_url)
if head is None:
continue
self.url_list.append(https_url)
print(https_url, head)
finally:
self.url_queue.task_done()
def download(self, url):
try:
head = requests.get(url, headers=self.headers, timeout=3)
except requests.RequestException:
head = None
return head
if __name__ == '__main__':
u_queue = Queue()
checker = [k.strip() for k in open('urls.txt', encoding='utf-8')]
url_list = []
save_name = "results.txt" # 结果保存文件名
for url in checker:
u_queue.put(url)
for i in range(50):
bdm = UrlCheck(u_queue, url_list)
bdm.daemon = True
bdm.start()
u_queue.join()
f = open(save_name, mode='w', encoding='utf-8')
for url in url_list:
f.write(url+'\n')
f.close()
print('done')
第三种比较是我常用的
项目地址:https://github.com/teamssix/url-survival-check
注意以上全部为python3环境使用