frangelbarrera/phishing-detection-rnn-cnn

GitHub: frangelbarrera/phishing-detection-rnn-cnn

基于 URL 词法特征的离线钓鱼检测模型,提供 Flask Web UI 与 CLI,支持在无网络环境下进行完全可复现的训练与推理。

Stars: 11 | Forks: 0

# 网站离线钓鱼检测模型 [![在线演示](https://img.shields.io/badge/Live%20Demo-Render-46E3B7?style=for-the-badge&logo=render&logoColor=white)](https://phishing-detection-rnn-cnn.onrender.com/) [![在 Colab 中打开](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/frangelbarrera/phishing-detection-rnn-cnn/blob/main/URL_Phishing_Detection.ipynb) [![许可证: MIT](https://img.shields.io/github/license/frangelbarrera/phishing-detection-rnn-cnn?style=flat-square)](LICENSE) [![Stars](https://img.shields.io/github/stars/frangelbarrera/phishing-detection-rnn-cnn?style=flat-square)](https://github.com/frangelbarrera/phishing-detection-rnn-cnn/stargazers) [![最近提交](https://img.shields.io/github/last-commit/frangelbarrera/phishing-detection-rnn-cnn?style=flat-square)](https://github.com/frangelbarrera/phishing-detection-rnn-cnn/commits) [![Issues](https://img.shields.io/github/issues/frangelbarrera/phishing-detection-rnn-cnn?style=flat-square)](https://github.com/frangelbarrera/phishing-detection-rnn-cnn/issues) [![热门语言](https://img.shields.io/github/languages/top/frangelbarrera/phishing-detection-rnn-cnn?style=flat-square)](https://github.com/frangelbarrera/phishing-detection-rnn-cnn) [![贡献者](https://img.shields.io/github/contributors/frangelbarrera/phishing-detection-rnn-cnn?style=flat-square)](https://github.com/frangelbarrera/phishing-detection-rnn-cnn/graphs/contributors) [![仓库大小](https://img.shields.io/github/repo-size/frangelbarrera/phishing-detection-rnn-cnn?style=flat-square)](https://github.com/frangelbarrera/phishing-detection-rnn-cnn) ## 描述 一个轻量级的**离线**钓鱼 URL 检测模型。所有特征均从 URL 字符串本身中提取——在推理阶段不会发起任何网络请求,因此该模型可以在物理隔离的环境中使用。 该分类器是一个小型的双隐藏层 MLP(约 5,300 个参数),基于 49 个标准化的词法和结构化 URL 特征(长度、特殊字符计数、数字比例、TLD 标志、词统计等),使用 `binary_crossentropy` 和 `Adam` 进行训练。一个在训练集上拟合的 `StandardScaler` 会与模型一同发布,以确保推理时能完全复现训练阶段的预处理流程。在训练和推理阶段,特征均会从 URL 字符串中重新计算,从而消除了训练/服务阶段的数据偏差。 ## 分支 此仓库包含两个分支: - **`main`**(当前分支)—— 论文完成后的工程化演进版本,使用在相同 URL 特征上训练的更简单的双隐藏层 MLP 替换了原有的 CNN+LSTM 架构。包含 Flask Web UI、CLI、可复现的训练脚本以及已提交的模型制品。测试准确率 89.94%,AUC-ROC 0.965。 - **`thesis-original`** —— 答辩通过的学术论文(2025 年 7 月),使用了原始的 CNN+LSTM 混合架构(Conv1D + LSTM,138,898 个参数)。完全按照答辩时的状态保留,并在事后应用了可复现性修复(修正了输入形状、softmax 输出头、特征标准化、早停机制以及基于概率的 AUC)。测试准确率 88.67%,AUC-ROC 0.946。 要在分支之间切换: ``` git checkout main # post-thesis MLP evolution (this branch) git checkout thesis-original # defended CNN+LSTM thesis ``` ## 模型结果 | 指标 | 数值 | | --- | --- | | 测试准确率 | **89.94 %** | | AUC-ROC | **0.965** | | 平均精确率 | 0.965 | | F1 分数(调整阈值后) | 0.900 | | Brier 分数 | 0.072 | | 决策阈值 | 0.499 | 在保留的 20% 测试集划分上的混淆矩阵(行 = 真实标签,列 = 预测标签): ``` legit phishing legit 1024 119 phishing 111 1032 ``` 所有指标均在预测概率(非 argmax)上计算,并且可以通过 `train.py` 在种子 `42` 下完全复现。请查看 `training_metadata.json` 获取完整的训练配置,以及 `metrics.json` 获取 ROC / PR 曲线。 ## 仓库结构 ``` phishing-detection-rnn-cnn/ ├── phishing_detector/ # Python package (feature extractor + detector) │ ├── __init__.py │ ├── features.py # 49-feature offline URL featurizer │ └── detector.py # PhishingDetector: load model + predict ├── web/ # Minimal Flask web UI for interactive testing │ ├── app.py │ └── templates/index.html ├── tests/ │ └── test_features.py # Feature + detector unit tests ├── train.py # Reproducible training script ├── predict.py # CLI for classifying URLs ├── URL_Phishing_Detection.ipynb # Notebook wrapper around train.py ├── dataset_phishing.csv # Source dataset (11,430 rows) ├── my_model.keras # Trained model (97 KB) ├── scaler.pkl # Fitted StandardScaler ├── feature_names.json # Canonical feature order ├── metrics.json # Full evaluation report (ROC, PR, etc.) ├── history.json # Training history ├── training_metadata.json # Versions, seed, architecture, metrics digest ├── SHA256SUMS # Integrity checksums for all artifacts ├── requirements.txt ├── LICENSE └── README.md ``` ## 快速开始 ``` git clone https://github.com/frangelbarrera/phishing-detection-rnn-cnn.git cd phishing-detection-rnn-cnn pip install -r requirements.txt # 从 command line 分类 URL python predict.py "https://www.google.com" python predict.py "http://secure-account-verify-login.tk/login.html" ``` 有关交互式 Web UI,请参阅下方的 [Web UI](#web-ui) 部分。 ## Web UI 位于 `web/` 目录下的一个极简 Flask 应用,允许你粘贴 URL 并在干净、柔和色调的页面中查看模型的判定结果。通过以下命令启动: ``` python -m web.app --host 127.0.0.1 --port 5000 # 然后在浏览器中打开 http://localhost:5000 ``` 表单会在输入框下方显示模型的核心指标,结果面板会展示预测的标签、钓鱼概率以及一个从“合法”到“钓鱼”的渐变指示条。 **初始状态** —— 输入表单与模型指标: ![Web UI 初始状态](https://static.pigsec.cn/wp-content/uploads/repos/cas/07/07c640c0f5db29d76120cd453154e26590ba6e8845357226e3f4aa12f942ce42.png) **合法 URL** —— `https://www.google.com` 被判定为合法,概率为 0.006: ![Web UI 分类合法 URL](https://static.pigsec.cn/wp-content/uploads/repos/cas/03/0356fe53ad6a8a3d092aa2da73076197ff56a059d87e4837b3ac47e31b53ae30.png) **钓鱼 URL** —— 一个可疑的 URL 被判定为钓鱼,概率为 1.000: ![Web UI 分类钓鱼 URL](https://static.pigsec.cn/wp-content/uploads/repos/cas/bc/bccf730ce7b0454fc6b9a601b6db4758272e4b283e9ca624ed0b328759a994c5.png) ## 重新训练 要在 Google Colab 上从头重新训练(例如使用新数据集): 1. 将所有仓库文件上传到你的 Google Drive 中名为 `phishing-detection-rnn-cnn` 的文件夹中。 2. 在 Colab 中打开 `URL_Phishing_Detection.ipynb` 并运行所有单元格。该 notebook 会挂载 Drive,安装依赖项,运行 `train.py`,并原地覆盖模型制品。 3. 训练完成后,将新生成的 `my_model.keras`、`scaler.pkl`、`metrics.json`、`history.json` 和 `training_metadata.json` 同步回仓库并进行提交。 在本地,只需运行: ``` python train.py ``` 该脚本会将所有制品写入仓库根目录,并打印最终的测试指标。 ## 环境要求 - Python 3.10+ - TensorFlow ≥ 2.15 - scikit-learn ≥ 1.3 - pandas, numpy, matplotlib - Flask ≥ 3.0(仅用于 Web UI) 使用 `pip install -r requirements.txt` 进行安装。 ## 注意事项与局限性 - 特征纯粹基于词法/结构。该模型**不**检查 SSL 证书、DNS 记录或页面内容,因此它无法检测托管在原本合法域名上的钓鱼页面(例如被攻陷的 WordPress 站点)。 - 原始数据集中的五个特征(`random_domain`、`domain_in_brand`、`brand_in_subdomain`、`brand_in_path`、`nb_external_external_redirection`)需要精心维护的品牌列表或实时的网络访问权限,为了保证一致性,已在训练和推理阶段将其舍弃。 - 少数启发式特征(`https_token`、`prefix_suffix`、`abnormal_subdomain`)在现代互联网环境中存在较大噪声——许多合法网站也使用 HTTPS、连字符或多层子域名。模型已尝试规避此问题,但在多层级的合法域名上仍可能出现偶尔的误报。 ## 许可证 MIT —— 详见 [LICENSE](LICENSE)。
标签:Apex, Flask, Python, 无后门, 机器学习, 离线分类器, 网络安全, 逆向工具, 钓鱼检测, 隐私保护