Andy25X/web-vuln-scanner
GitHub: Andy25X/web-vuln-scanner
一个基于 Python 的 OWASP 兼容 Web 漏洞扫描器,通过并发爬虫与检查引擎检测常见漏洞并提供 CI/CD 友好的报告与阻断能力。
Stars: 0 | Forks: 0
# web-vuln-scanner
一个使用 Python 构建的与 OWASP 兼容的 Web 漏洞扫描器。通过自动爬虫、并发检查和结构化报告来检测常见的 Web 漏洞。



## 检测范围
| 检查项 | 类别 | 严重等级 |
|---|---|---|
| SQL 注入 — 基于错误的(MySQL、PostgreSQL、Oracle、MSSQL) | 注入 | CRITICAL |
| 反射型 XSS | 注入 | HIGH |
| 缺少 Content-Security-Policy | 头部 | MEDIUM |
| 缺少 Strict-Transport-Security (HSTS) | 头部 | HIGH |
| 缺少 X-Frame-Options | 头部 | MEDIUM |
| 缺少 X-Content-Type-Options | 头部 | LOW |
| POST 表单中缺少 CSRF 令牌 | 访问控制 | MEDIUM |
| 通过 HTTP 提供服务(非 HTTPS) | 传输 | HIGH |
| 敏感路径暴露(.git、.env、/admin、phpinfo 等) | 暴露 | CRITICAL–LOW |
| 堆栈跟踪/错误信息泄露 | 信息 | LOW |
| 通过头部泄露服务器版本 | 信息 | LOW |
## 项目结构
```
web-vuln-scanner/
├── config.py # all tuneable settings (timeouts, threads, limits)
├── main.py # CLI entry point with argparse
│
├── scanner/
│ ├── http.py # shared requests.Session, fingerprinting, BFS crawler
│ ├── runner.py # ThreadPoolExecutor orchestrator + HTML/JSON reporter
│ └── checks/
│ ├── base.py # BaseCheck, Finding, Severity, ScanContext
│ ├── headers.py # HTTP security header checks (HDR-001, HDR-002)
│ ├── sqli.py # SQL injection error-based detection (SQLI-001)
│ ├── xss.py # Reflected XSS detection (XSS-001)
│ ├── dirs.py # Sensitive path enumeration (DIR-001)
│ └── misc.py # CSRF, HTTPS enforcement, error disclosure
│
└── tests/
└── test_scanner.py # 34 unit tests with mocked HTTP responses
```
## 快速开始
```
git clone https://github.com/Andy25X/web-vuln-scanner.git
cd web-vuln-scanner
conda create -n web-vuln-scanner python=3.12
conda activate web-vuln-scanner
pip install -e ".[dev]"
```
### 运行扫描
```
# 扫描目标,生成 HTML 和 JSON 报告
python main.py http://testphp.vulnweb.com
# 仅扫描基础 URL(不进行爬取)
python main.py http://localhost --no-crawl
# 将报告保存到指定目录
python main.py http://target.com --output both --output-dir ./reports
# 仅显示 CRITICAL 和 HIGH 级别发现
python main.py http://target.com --min-severity high
# 禁用 SSL 验证(适用于使用自签名证书的实验室环境)
python main.py https://localhost --no-ssl-verify
```
### CLI 选项
| 标志 | 默认值 | 描述 |
|---|---|---|
| `--output` | `both` | 输出格式:`terminal`、`html`、`json`、`both` |
| `--output-dir` | `.` | 报告文件输出目录 |
| `--no-crawl` | off | 跳过爬虫,仅测试基础 URL |
| `--threads` | `10` | 并发检查线程数 |
| `--timeout` | `10` | 请求超时时间(秒) |
| `--no-ssl-verify` | off | 禁用 SSL 证书验证 |
| `--verbose` | off | 启用调试日志 |
### 环境变量覆盖
```
SCANNER_TIMEOUT=30 SCANNER_THREADS=5 python main.py http://target.com
```
## 工作原理
扫描器遵循四步流水线:
1. **指纹识别** — 收集服务器头部、页面标题以及从基础 URL 检测到的技术栈。
2. **爬虫** — BFS 爬虫发现内部 URL 和 HTML 表单,支持可配置的最大深度。使用持久的 `requests.Session` 以保持 Cookie(会话令牌)在跨请求中保留。
3. **检查** — 所有检查通过 `ThreadPoolExecutor` 并发执行。每个检查接收相同的 `ScanContext`,其中包含会话、发现的 URL、表单以及预取响应。
4. **报告** — 查找结果按严重性排序,并写入自包含的 HTML 文件和/或 JSON 文件。
### 退出代码
| 代码 | 含义 |
|---|---|
| `0` | 扫描完成,未发现 CRITICAL 级别问题 |
| `1` | 检测到一个或多个 CRITICAL 级别问题 |
退出代码 `1` 对应的 CRITICAL 查找结果可用于直接集成到 CI/CD 流水线中 — 如果扫描器发现严重漏洞,则自动阻断部署。
## 添加新的检查
继承 `scanner/checks/` 目录下的新模块或现有模块中的 `BaseCheck`:
```
from .base import BaseCheck, Finding, Severity, ScanContext
from typing import List
class MyNewCheck(BaseCheck):
check_id = "XXX-001"
name = "Short check name"
def run(self, ctx: ScanContext) -> List[Finding]:
findings = []
# ... inspect ctx.urls, ctx.forms, ctx.responses, ctx.session
if something_wrong:
findings.append(self._finding(
title = "Vulnerability title",
severity = Severity.HIGH,
url = ctx.target,
description = "What was found and why it matters.",
evidence = "Raw proof — payload, response snippet, header value.",
remediation = "Exact steps to fix.",
references = ["CWE-XXX", "OWASP A0X:2021"],
))
return findings
ALL_CHECKS = [MyNewCheck]
```
然后将模块名称添加到 `scanner/runner.py` 中的 `CHECK_MODULES`。运行时会自动发现。
## 运行测试
```
pytest -v
pytest --cov=scanner --cov-report=term-missing
```
所有 34 个测试均使用模拟的 HTTP 响应 — 无需真实网络连接。
## 合法的目标站点(用于练习)
这些服务器专门用于漏洞扫描器测试:
- `http://testphp.vulnweb.com` — 由 Acunetix 维护,故意存在漏洞的 PHP 应用
- `http://dvwa` — [DVWA](https://github.com/digininja/DVWA) 通过 Docker 在本地运行
- `http://localhost/WebGoat` — [WebGoat](https://github.com/WebGoat/WebGoat)(OWASP 项目)
## 许可证
MIT
标签:BeEF, BFS, CI集成, CSRF, DOE合作, HTML报告, HTTPS, JSON报告, Python, web安全, 反射型XSS, 安全头部, 并发扫描, 敏感路径探测, 无后门, 爬虫, 线程池, 网络安全, 逆向工具, 隐私保护