python简易目录扫描器

单线程简易版:

import requests

# 获取 URL
url = input("请输入 URL:").strip()

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
}

print("Ciallo~ (∠・ω< )⌒★")


def read_file(file1):
    """尝试以 UTF-8 读取文件,失败后尝试 GBK"""
    try:
        with open(file1, encoding='utf-8') as f:
            return f.readlines()
    except UnicodeDecodeError:
        with open(file1, encoding='gbk', errors='ignore') as f:
            return f.readlines()


def run():
    """执行目录扫描"""
    urls = read_file("PHP.txt")  # 读取字典文件
    results = []

    for i in urls:
        c = i.strip()  # 去除换行符和空格
        full_url = url.rstrip("/") + "/" + c  # 确保 URL 拼接正确

        try:
            response = requests.get(full_url, headers=headers, timeout=5)  # 添加超时
            if response.status_code == 200:
                red_text = f"\033[91m[+] {c} 存在\033[0m"  # 红色高亮
                print(red_text)
                results.append(f"[+] {c} 存在")
            else:
                print(f"[-] {c} 不存在")
        except requests.exceptions.RequestException as e:
            print(f"[!] 访问 {full_url} 失败: {e}")

    return "\n".join(results)


# 执行扫描并保存结果
with open("output.txt", 'w', encoding='utf-8') as f:
    result = run()
    if result:
        f.write(result)

多线程版:

import requests
import concurrent.futures
import queue

# 获取 URL
url = input("请输入 URL:").strip()

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
}

print("Ciallo~ (∠・ω< )⌒★")

def read_file(file1):
    """尝试以 UTF-8 读取文件,失败后尝试 GBK"""
    try:
        with open(file1, encoding='utf-8') as f:
            return f.readlines()
    except UnicodeDecodeError:
        with open(file1, encoding='gbk', errors='ignore') as f:
            return f.readlines()

def scan_path(path):
    """扫描单个路径"""
    full_url = url.rstrip("/") + "/" + path.strip()  # 确保 URL 拼接正确

    try:
        response = requests.get(full_url, headers=headers, timeout=5)  # 添加超时
        if response.status_code == 200:
            red_text = f"\033[91m[+] {path.strip()} 存在\033[0m"  # 红色高亮
            print(red_text)
            return f"[+] {path.strip()} 存在"
        else:
            print(f"[-] {path.strip()} 不存在")
    except requests.exceptions.RequestException as e:
        print(f"[!] 访问 {full_url} 失败: {e}")

    return None  # 失败返回 None

def run():
    """使用多线程执行扫描"""
    paths = read_file("PHP.txt")  # 读取字典文件
    results = []
    q = queue.Queue()  # 任务队列

    for path in paths:
        q.put(path.strip())  # 加入队列

    # 设置最大线程数
    max_threads = 10  # 线程池大小(可调整)

    with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
        # 提交所有任务
        future_to_path = {executor.submit(scan_path, q.get()): path for path in paths}

        for future in concurrent.futures.as_completed(future_to_path):
            result = future.result()
            if result:
                results.append(result)

    return "\n".join(results)

# 执行扫描并保存结果
with open("output.txt", 'w', encoding='utf-8') as f:
    result = run()
    if result:
        f.write(result)

加入代理池

import requests
import concurrent.futures
import queue
import random

# 获取 URL
url = input("请输入 URL:").strip()

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
}

print("Ciallo~ (∠・ω< )⌒★")

# 代理池(可以自己添加更多代理)
proxy_pool = [
    "http://123.456.789.1:8080",
    "http://234.567.890.2:8080",
    "http://345.678.901.3:8080",
    "http://456.789.012.4:8080",
]

def read_file(file1):
    """尝试以 UTF-8 读取文件,失败后尝试 GBK"""
    try:
        with open(file1, encoding='utf-8') as f:
            return f.readlines()
    except UnicodeDecodeError:
        with open(file1, encoding='gbk', errors='ignore') as f:
            return f.readlines()

def get_random_proxy():
    """随机获取一个代理"""
    return random.choice(proxy_pool)

def scan_path(path):
    """扫描单个路径"""
    full_url = url.rstrip("/") + "/" + path.strip()  # 拼接完整 URL
    proxy = get_random_proxy()  # 随机选一个代理
    proxies = {"http": proxy, "https": proxy}  # 设置代理

    try:
        response = requests.get(full_url, headers=headers, proxies=proxies, timeout=5)  # 发送请求
        if response.status_code == 200:
            red_text = f"\033[91m[+] {path.strip()} 存在 (代理: {proxy})\033[0m"  # 红色高亮
            print(red_text)
            return f"[+] {path.strip()} 存在 (代理: {proxy})"  # 返回结果
        else:
            print(f"[-] {path.strip()} 不存在 (代理: {proxy})")  # 打印但不保存
    except requests.exceptions.RequestException as e:
        print(f"[!] 访问 {full_url} 失败 (代理: {proxy}): {e}")  # 处理错误

    return None  # 失败返回 None

def run():
    """使用多线程执行扫描"""
    paths = read_file("PHP.txt")  # 读取字典文件
    results = []
    q = queue.Queue()  # 任务队列

    for path in paths:
        q.put(path.strip())  # 加入队列

    # 设置最大线程数
    max_threads = 10  # 线程池大小(可调整)

    with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
        # 提交所有任务
        future_to_path = {executor.submit(scan_path, q.get()): path for path in paths}

        for future in concurrent.futures.as_completed(future_to_path):
            result = future.result()
            if result:
                results.append(result)

    return "\n".join(results)

# 执行扫描并保存结果
with open("output.txt", 'w', encoding='utf-8') as f:
    result = run()
    if result:
        f.write(result)

posted @ 2025-03-20 15:58  ALe_#3  阅读(12)  评论(0)    收藏  举报  来源