(原创)Xray多目标检测控制 Python实现 带继续扫描 目标去重 WebHook上传
Web安全是互联网时代一个非常重要的话题。为了确保网站和应用程序的安全性,经常需要进行漏洞扫描和渗透测试。Xray 是一款强大的开源工具,可以用于自动化漏洞扫描。在本篇博客中,我们将介绍如何使用 Python 编写一个自动化脚本,将 Xray 与 Python 结合起来,以实现对多个目标网站的自动化安全扫描。
为什么需要这样一个脚本?
支持继续扫描
支持去重
支持多线程
更少的打扰
准备工作
在开始之前,确保你已经完成以下准备工作:
安装 Xray:你需要在系统中安装 Xray 工具,你可以从 Xray 官方网站 下载并安装。
创建任务文件:将需要扫描的目标 URL 存储在文本文件中,每个 URL 占据一行。这些任务文件应该放在一个指定的目录中,例如
./tasks。安装 Python:确保你已经安装了 Python,本示例使用 Python 3。
Python 脚本概述
以下是一个使用 Python 编写的脚本,它自动化了 Xray 的扫描过程。这个脚本执行以下操作:
从任务文件中读取 URL。
合并和去重这些 URL。
使用 Xray 对这些 URL 进行漏洞扫描。
将结果写入文件并进行相关清理工作。
接下来,我们将详细讲解每一部分。
Python 代码解析
导入必要的库
import os import subprocess import concurrent.futures import threading import multiprocessing
脚本使用了多线程和多进程来提高效率。
使用锁来确保对文件的原子性访问
file_lock = threading.Lock()
此锁用于确保多线程写入 targets.txt 文件时的原子性访问。
执行 Xray 命令
# 记录开始时间
start_time = time.time()
# 记录已运行的行数
processed_lines = 0
# 记录平均命令执行时间
average_command_time = 0.0
def execute_xray_command(url, mission_number, total_missions, remaining_urls):
global processed_lines, average_command_time
command_start_time = time.time()
command = f".\\xray_windows_amd64.exe webscan --basic-crawler {url} --plugins sqldet --webhook-output https://你自己的webhook链接"
try:
subprocess.run(command, shell=True, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except subprocess.CalledProcessError as e:
print(f"URL:{url} 的命令执行失败")
print(e)
command_end_time = time.time()
# 计算已运行的行数
processed_lines += 1
# 计算已用时间
elapsed_time = time.time() - start_time
# 计算估计剩余时间
remaining_time = (remaining_urls - processed_lines) * average_command_time
# 打印进度信息
print(f"[{time.strftime('%H:%M:%S', time.localtime(time.time()))}] [INFO] Mission {processed_lines}/{total_missions} is in progress, estimated remaining {time.strftime('%H:%M:%S', time.gmtime(remaining_time))}")
# 更新平均命令执行时间
average_command_time = (command_end_time - command_start_time + average_command_time * (processed_lines - 1)) / processed_lines这个函数用于执行 Xray 命令。它将每个 URL 作为参数传递给 Xray,并执行漏洞扫描。
处理任务文件
def process_txt_file(txt_file, last_processed_lines): lines = [] with open(txt_file, 'r') as file: lines = file.readlines()[last_processed_lines.get(txt_file, 0):] return lines
这个函数用于处理任务文件,它读取文件中的 URL 行,并根据上一次处理的位置进行截取。
主函数
def main():
# 定义存放 txt 文件的目录
tasks_directory = './tasks'
# 获取目录中所有的 txt 文件
txt_files = [os.path.join(tasks_directory, f) for f in os.listdir(tasks_directory) if f.endswith('.txt')]
# 创建字典以存储每个文件的上次处理的行数
last_processed_lines = {}
# 如果跟踪文件存在,从中读取上次处理的行数
tracking_file = 'last_processed_lines.txt'
if os.path.exists(tracking_file):
with open(tracking_file, 'r') as file:
for line in file:
file_name, line_number = line.strip().split(':')
last_processed_lines[file_name] = int(line_number)
all_urls = set()
for txt_file in txt_files:
lines = process_txt_file(txt_file, last_processed_lines)
all_urls.update(lines)
if os.path.exists('targets.txt'):
with open('targets.txt', 'r') as file:
existing_urls = file.readlines()
all_urls.update(existing_urls)
# 更新 targets.txt 文件中的 URL
with file_lock:
with open('targets.txt', 'a') as file:
for url in all_urls:
# 检查 URL 是否已经存在,如果不存在才写入文件
if url not in existing_urls:
file.write(url)
# 移除原始的 txt 文件
for txt_file in txt_files:
os.remove(txt_file)
# 计算剩余的URL行数
remaining_urls = len(all_urls) - last_processed_lines.get('targets.txt', 0)
# 使用线程池并行执行 xray 命令
with concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
for mission_number, url in enumerate(all_urls, start=1):
executor.submit(execute_xray_command, url, mission_number, len(all_urls), remaining_urls)
if __name__ == '__main__':
processed_lines = 0
main()在主函数中,我们首先获取任务文件列表,合并和去重所有 URL,并将它们追加到 targets.txt 文件中。然后,我们使用多线程池并行执行 Xray 命令来扫描这些 URL。最后,我们更新跟踪文件和清理原始的任务文件。
总结
通过使用这个自动化脚本,你可以轻松地扫描多个目标网站以发现潜在的漏洞。这大大简化了Web安全测试的过程,提高了效率,同时也提供了灵活性,你可以根据需要添加更多的任务文件。
当然,记得始终遵循法律法规和道德准则,只在授权范围内对网站进行扫描和测试,以维护合法性和道德性。
希望这篇博客对你了解如何使用 Python 和 Xray 进行自动化漏洞扫描有所帮助。如果你对此有任何疑问或建议,请随时联系我们。安全第一!



