A novel approach combining traditional ML with generative AI for real-time threat detection
π Abstract β’ π Quick Start β’ ποΈ Architecture β’ π Results
The evolution of web application defense mechanisms has led to the development of Web Application Firewalls (WAF) powered by machine learning models for threat detection. This paper presents a novel approach that combines traditional machine learning techniques (Naive Bayes) with generative models such as ChatGPT for the dynamic classification of threats in web applications.
Our solution leverages ChatGPT's capabilities to detect novel attacks and enhances detection capabilities through continuous retraining. This system progressively learns from new attack patterns, eventually reducing its dependence on the generative model.
- π€ Hybrid AI System - Combines Naive Bayes + ChatGPT for optimal detection
- π Autonomous Learning - Continuously retrains from new attack patterns
- β‘ Real-time Detection - Instant classification of known and novel attacks
- π― Zero-day Protection - Detects previously unknown attack vectors
- π Progressive Independence - Reduces reliance on ChatGPT over time
- π‘οΈ Multi-attack Support - XSS, SQL Injection, Path Traversal, and more
- Python 3.7+
- OpenAI API key
- Required libraries
# Clone the repository
git clone https://github.com/daletoniris/Web-Application-Firewall-Purple-AI-Paper.git
cd Web-Application-Firewall-Purple-AI-Paper
# Install dependencies
pip install flask requests colorama scikit-learn openaiAdd your OpenAI API key in:
WAF_TRAIN_GPT.pyWAF_POST_GPT_NAIVES.py
openai.api_key = "your-api-key-here"python server.pyServer will be available at http://localhost:5051
In a new terminal:
python ATTACK.pyThis sends random attacks (XSS, SQL Injection, etc.) every 5 seconds.
Start monitoring and classifying logs with ChatGPT:
python WAF_TRAIN_GPT.pyTrain the local classifier:
python WAF_POST_GPT_NAIVES.pyThe model will now classify logs locally without consulting ChatGPT.
βββββββββββββββ
β Web Server β βββΊ Logs βββΊ ββββββββββββββββββββ
β (server.py)β β Naive Bayes β
βββββββββββββββ β Classifier β
ββββββββββ¬ββββββββββ
β
ββββββββββββββββββββ΄βββββββββββββββββββ
β β
β
Confident β Uncertain
β β
β βΌ
β βββββββββββββββββββ
β β ChatGPT β
β β Classification β
β ββββββββββ¬ββββββββββ
β β
βββββββββββββββββββββ¬ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Retrain Model β
β (Feedback Loop)β
ββββββββββββββββββββ
| Component | Description |
|---|---|
| server.py | Simulates web application and logs incoming requests |
| ATTACK.py | Sends random simulated attacks to the server |
| WAF_TRAIN_GPT.py | Classifies logs using ChatGPT and stores learned patterns |
| WAF_POST_GPT_NAIVES.py | Trains and uses Naive Bayes model for local classification |
- β XSS (Cross-site Scripting)
- β SQL Injection
- β Path Traversal
- β Command Injection
- β Remote File Inclusion (RFI)
- β LDAP Injection
- β Code Injection
- Accuracy: Naive Bayes model improved significantly after retraining with ChatGPT feedback
- Real-time Detection: Near-instant detection of novel attack vectors
- Continuous Learning: Detection rates improve with each interaction
- Autonomy: System reduces dependence on ChatGPT as it learns
ATTACK.py:
βοΈ Attacker started. Sending attacks every 5 seconds...
β Attack (SQL Injection) sent: 1' OR '1'='1 | Response Code: 200
β Attack (XSS) sent: <script>alert("XSS")</script> | Response Code: 200
WAF_TRAIN_GPT.py:
β€ Processing new log line: INFO:werkzeug:127.0.0.1 - - [19/Nov/2024:15:10:35] "POST /login HTTP/1.1" 200 -
π ChatGPT classified the line as: SQL Injection
β Memory saved successfully.
WAF_POST_GPT_NAIVES.py:
β€ Processing new log line: INFO:werkzeug:127.0.0.1 - - [19/Nov/2024:15:12:40] "POST /login HTTP/1.1" 200 -
β Classified by the model as: XSS
β Memory saved successfully.
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
# Vectorize logs
vectorizer = TfidfVectorizer(max_features=1000)
X = vectorizer.fit_transform(logs)
y = labels
# Train model
model = MultinomialNB().fit(X, y)import openai
def consult_gpt4(log_line):
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Classify this log line as 'XSS', 'SQL Injection', 'No Attack', or another type of attack."},
{"role": "user", "content": f"Log line: {log_line}"}
]
)
return response['choices'][0]['message']['content']- β±οΈ Latency: ChatGPT API calls introduce some delay
- π Data Quality: Performance depends on training data quality
- π Scalability: Managing growing training data efficiently
- Optimize ChatGPT interactions
- Explore alternative ML models
- Improve scalability
- Enhanced pattern recognition
This work is licensed under the Apache License 2.0.
- β Use: Personal, educational, or commercial purposes
- β Modify: Adapt and build upon the material
- β Distribute: Share under the same license
- "Application Layer Security for Modern Web Applications", 2023
- "Generative Models in Cybersecurity: A New Approach to Threat Detection", Journal of AI Research, 2024
- "Advances in Machine Learning for Web Application Firewalls", Cybersecurity Review, 2024
Daniel Dieser - Independent Robotics Researcher & AI Developer
- GitHub: @daletoniris
- Organizations: @initiasur, @NiperiaLab
π‘οΈ Protecting Web Applications with AI-Powered Defense

