Spaces:
Sleeping
Sleeping
π Initial upload of my app
Browse files- .gitattributes +1 -0
- LICENSE +21 -0
- README.md +125 -9
- __pycache__/ui.cpython-311.pyc +0 -0
- __pycache__/utils.cpython-311.pyc +0 -0
- app.py +7 -0
- demo/demo.mp4 +3 -0
- demo/demo.png +0 -0
- models/network_logs_pipeline.joblib +3 -0
- models/target_encoder.joblib +3 -0
- network-threat-detection-with-f1-99.ipynb +0 -0
- requirements.txt +9 -0
- ui.py +37 -0
- utils.py +26 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
demo/demo.mp4 filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 Eslam Tarek
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
CHANGED
|
@@ -1,12 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# NetworkAttackDetector β Realβtime Network Threat Detection π‘οΈ
|
| 2 |
+
|
| 3 |
+
Lightweight Gradio app to predict and explain potential network scan types from request metadata. Built with a scikitβlearn pipeline, ready to run locally. π
|
| 4 |
+
|
| 5 |
---
|
| 6 |
+
|
| 7 |
+
## Badges
|
| 8 |
+
|
| 9 |
+
[](https://www.python.org/)
|
| 10 |
+
[](./LICENSE)
|
| 11 |
+
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
## Table of Contents
|
| 15 |
+
|
| 16 |
+
- [Demo](#demo)
|
| 17 |
+
- [Features](#features)
|
| 18 |
+
- [Installation / Setup](#installation--setup)
|
| 19 |
+
- [Usage](#usage)
|
| 20 |
+
- [Configuration / Options](#configuration--options)
|
| 21 |
+
- [Contributing](#contributing)
|
| 22 |
+
- [License](#license)
|
| 23 |
+
- [Acknowledgements / Credits](#acknowledgements--credits)
|
| 24 |
+
|
| 25 |
---
|
| 26 |
|
| 27 |
+
## Demo
|
| 28 |
+
|
| 29 |
+
The repository includes real demo assets under `./demo/`.
|
| 30 |
+
|
| 31 |
+
- Image preview:
|
| 32 |
+
|
| 33 |
+

|
| 34 |
+
|
| 35 |
+
- Short walkthrough (MP4):
|
| 36 |
+
|
| 37 |
+
<video src="./demo/demo.mp4" controls width="640"></video>
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
## Features
|
| 42 |
+
|
| 43 |
+
- **Interactive UI** using `gradio` via `ui.py` β `build_ui()`.
|
| 44 |
+
- **Pretrained artifacts** loaded from `./models/` via `utils.py` β `load_artifacts()`.
|
| 45 |
+
- **Fast inference** using a serialized scikitβlearn pipeline (`joblib`).
|
| 46 |
+
- **Humanβreadable prediction** by decoding labels with a fitted encoder.
|
| 47 |
+
|
| 48 |
+
---
|
| 49 |
+
|
| 50 |
+
## Installation / Setup
|
| 51 |
+
|
| 52 |
+
Use a virtual environment for isolation.
|
| 53 |
+
|
| 54 |
+
```bash
|
| 55 |
+
# Create a virtual environment
|
| 56 |
+
python -m venv .venv
|
| 57 |
+
|
| 58 |
+
# Activate it
|
| 59 |
+
# On Linux/Mac:
|
| 60 |
+
source .venv/bin/activate
|
| 61 |
+
# On Windows:
|
| 62 |
+
.venv\Scripts\activate
|
| 63 |
+
|
| 64 |
+
# Install dependencies
|
| 65 |
+
pip install -r requirements.txt
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
---
|
| 69 |
+
|
| 70 |
+
## Usage
|
| 71 |
+
|
| 72 |
+
Run the Gradio app from the project root:
|
| 73 |
+
|
| 74 |
+
```bash
|
| 75 |
+
python app.py
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
Then open the local URL printed by Gradio (e.g., http://127.0.0.1:7860/).
|
| 79 |
+
|
| 80 |
+
### Input fields
|
| 81 |
+
- `Port` (number)
|
| 82 |
+
- `Request Type` (e.g., GET, POST)
|
| 83 |
+
- `Protocol` (e.g., HTTP, HTTPS)
|
| 84 |
+
- `Payload Size` (number)
|
| 85 |
+
- `User Agent` (string)
|
| 86 |
+
- `Status` (e.g., 200, 404)
|
| 87 |
+
|
| 88 |
+
### Output
|
| 89 |
+
- A predicted scan type (humanβreadable label) returned by `predict_intrusion()`.
|
| 90 |
+
|
| 91 |
+
---
|
| 92 |
+
|
| 93 |
+
## Configuration / Options
|
| 94 |
+
|
| 95 |
+
- **Model artifacts**: The app expects the following files to exist:
|
| 96 |
+
- `models/network_logs_pipeline.joblib`
|
| 97 |
+
- `models/target_encoder.joblib`
|
| 98 |
+
|
| 99 |
+
- **Changing artifact paths**: Update `utils.py` β `load_artifacts()` if your models live elsewhere.
|
| 100 |
+
|
| 101 |
+
- **Environment**: Ensure your Python version satisfies the version in the badge above.
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
|
| 105 |
+
## Contributing
|
| 106 |
+
|
| 107 |
+
Contributions are welcome! Suggestions, issues, and PRs help improve the project.
|
| 108 |
+
|
| 109 |
+
1. Fork the repo
|
| 110 |
+
2. Create a feature branch: `git checkout -b feat/your-feature`
|
| 111 |
+
3. Commit changes: `git commit -m "feat: add your feature"`
|
| 112 |
+
4. Push to branch: `git push origin feat/your-feature`
|
| 113 |
+
5. Open a Pull Request
|
| 114 |
+
|
| 115 |
+
Please keep PRs focused and include context, screenshots when relevant, and tests if logic changes.
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## License
|
| 120 |
+
|
| 121 |
+
This project is licensed under the MIT License β see [LICENSE](./LICENSE) for details.
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## Acknowledgements / Credits
|
| 126 |
+
|
| 127 |
+
- Model training and experimentation are illustrated in `network-threat-detection-with-f1-99.ipynb` using `pandas`, `numpy`, `scikitβlearn`, `xgboost`, and `shap`.
|
| 128 |
+
- UI is built with `gradio` and model artifacts are loaded with `joblib`.
|
__pycache__/ui.cpython-311.pyc
ADDED
|
Binary file (2.33 kB). View file
|
|
|
__pycache__/utils.cpython-311.pyc
ADDED
|
Binary file (1.36 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from utils import load_artifacts, predict_intrusion
|
| 2 |
+
from ui import build_ui
|
| 3 |
+
|
| 4 |
+
if __name__ == "__main__":
|
| 5 |
+
pipeline, target_encoder = load_artifacts()
|
| 6 |
+
demo = build_ui(pipeline, target_encoder, predict_intrusion)
|
| 7 |
+
demo.launch()
|
demo/demo.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7e81738cf227de7897bc357f7870856115b42415de3a3bdc5c3c092cc3b9a1a1
|
| 3 |
+
size 777470
|
demo/demo.png
ADDED
|
models/network_logs_pipeline.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:58be20bb9d93ad31a5261aba53ce90f494a56bfc6b2ec387d50ee00629a2c340
|
| 3 |
+
size 7331
|
models/target_encoder.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4ff864e97bd0583f47b07c44ca9a83c4d316978e562d1558a52ac6c0c2d074aa
|
| 3 |
+
size 504
|
network-threat-detection-with-f1-99.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.50.2
|
| 2 |
+
joblib==1.3.2
|
| 3 |
+
pandas==2.2.2
|
| 4 |
+
numpy==1.26.4
|
| 5 |
+
seaborn==0.13.2
|
| 6 |
+
matplotlib==3.8.4
|
| 7 |
+
scikit-learn==1.4.2
|
| 8 |
+
xgboost==2.0.3
|
| 9 |
+
shap==0.44.1
|
ui.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def build_ui(pipeline, target_encoder, predict_fn):
|
| 4 |
+
"""Build Gradio UI for intrusion detection."""
|
| 5 |
+
|
| 6 |
+
with gr.Blocks() as demo:
|
| 7 |
+
gr.Markdown("## π Network Threat Detection")
|
| 8 |
+
|
| 9 |
+
with gr.Row():
|
| 10 |
+
port = gr.Number(label="Port", value=80)
|
| 11 |
+
request_type = gr.Textbox(label="Request Type (e.g., GET, POST)")
|
| 12 |
+
protocol = gr.Textbox(label="Protocol (e.g., HTTP, HTTPS)")
|
| 13 |
+
payload_size = gr.Number(label="Payload Size", value=200)
|
| 14 |
+
user_agent = gr.Textbox(label="User Agent (e.g., Mozilla/5.0)")
|
| 15 |
+
status = gr.Textbox(label="Status (e.g., 200, 404)")
|
| 16 |
+
|
| 17 |
+
predict_btn = gr.Button("π Predict Scan Type")
|
| 18 |
+
output = gr.Textbox(label="Prediction")
|
| 19 |
+
|
| 20 |
+
def on_predict(port, request_type, protocol, payload_size, user_agent, status):
|
| 21 |
+
features = {
|
| 22 |
+
"Port": port,
|
| 23 |
+
"Request_Type": request_type,
|
| 24 |
+
"Protocol": protocol,
|
| 25 |
+
"Payload_Size": payload_size,
|
| 26 |
+
"User_Agent": user_agent,
|
| 27 |
+
"Status": status,
|
| 28 |
+
}
|
| 29 |
+
return predict_fn(pipeline, target_encoder, features)
|
| 30 |
+
|
| 31 |
+
predict_btn.click(
|
| 32 |
+
on_predict,
|
| 33 |
+
inputs=[port, request_type, protocol, payload_size, user_agent, status],
|
| 34 |
+
outputs=[output],
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
return demo
|
utils.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
def load_artifacts():
|
| 5 |
+
"""Load the trained pipeline and target encoder."""
|
| 6 |
+
pipeline = joblib.load("models/network_logs_pipeline.joblib")
|
| 7 |
+
target_encoder = joblib.load("models/target_encoder.joblib")
|
| 8 |
+
return pipeline, target_encoder
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def predict_intrusion(pipeline, target_encoder, features: dict):
|
| 12 |
+
"""
|
| 13 |
+
Predict Scan_Type from input features.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
pipeline: sklearn pipeline with preprocessing + model
|
| 17 |
+
target_encoder: LabelEncoder for Scan_Type
|
| 18 |
+
features: dict of input features
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
str: Predicted Scan_Type
|
| 22 |
+
"""
|
| 23 |
+
input_df = pd.DataFrame([features]) # single row DataFrame
|
| 24 |
+
pred_label = pipeline.predict(input_df)[0]
|
| 25 |
+
scan_type = target_encoder.inverse_transform([pred_label])[0]
|
| 26 |
+
return scan_type
|