Skip to content

daletoniris/Web-Application-Firewall-Purple-AI-Paper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ Web Application Firewall (WAF) Enhanced with AI

Autonomous Dynamic Learning with Generative Models

Python License AI Security

A novel approach combining traditional ML with generative AI for real-time threat detection

πŸ“– Abstract β€’ πŸš€ Quick Start β€’ πŸ—οΈ Architecture β€’ πŸ“Š Results


πŸ“– Abstract

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.


✨ Key Features

  • πŸ€– 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

πŸŽ₯ Demo Videos

Model Learning Process

Watch the video

Autonomous Operation

Watch the video

Click images to watch on YouTube


πŸš€ Quick Start

Prerequisites

  • Python 3.7+
  • OpenAI API key
  • Required libraries

Installation

# 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 openai

Configuration

Add your OpenAI API key in:

  • WAF_TRAIN_GPT.py
  • WAF_POST_GPT_NAIVES.py
openai.api_key = "your-api-key-here"

πŸƒ How to Run

Step 1: Start the Web Server

python server.py

Server will be available at http://localhost:5051

Step 2: Simulate Attacks

In a new terminal:

python ATTACK.py

This sends random attacks (XSS, SQL Injection, etc.) every 5 seconds.

Step 3: Monitor with AI

Start monitoring and classifying logs with ChatGPT:

python WAF_TRAIN_GPT.py

Step 4: Train Naive Bayes Model

Train the local classifier:

python WAF_POST_GPT_NAIVES.py

The model will now classify logs locally without consulting ChatGPT.


πŸ—οΈ Architecture

System Workflow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Web Server  β”‚ ──► Logs ──► β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  (server.py)β”‚              β”‚  Naive Bayes     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚  Classifier      β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                       β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚                                       β”‚
              βœ… Confident                            ❓ Uncertain
                    β”‚                                       β”‚
                    β”‚                                       β–Ό
                    β”‚                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚                              β”‚    ChatGPT      β”‚
                    β”‚                              β”‚  Classification β”‚
                    β”‚                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚                                       β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β”‚
                                        β–Ό
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚  Retrain Model   β”‚
                              β”‚  (Feedback Loop)β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Components

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

🎯 Supported Attack Types

  • βœ… XSS (Cross-site Scripting)
  • βœ… SQL Injection
  • βœ… Path Traversal
  • βœ… Command Injection
  • βœ… Remote File Inclusion (RFI)
  • βœ… LDAP Injection
  • βœ… Code Injection

πŸ“Š Results

Performance Improvements

  1. Accuracy: Naive Bayes model improved significantly after retraining with ChatGPT feedback
  2. Real-time Detection: Near-instant detection of novel attack vectors
  3. Continuous Learning: Detection rates improve with each interaction
  4. Autonomy: System reduces dependence on ChatGPT as it learns

Example Output

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.

πŸ”¬ Technical Details

Naive Bayes Implementation

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)

ChatGPT Integration

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']

🚧 Challenges & Future Work

Current Challenges

  • ⏱️ Latency: ChatGPT API calls introduce some delay
  • πŸ“Š Data Quality: Performance depends on training data quality
  • πŸ“ˆ Scalability: Managing growing training data efficiently

Future Improvements

  • Optimize ChatGPT interactions
  • Explore alternative ML models
  • Improve scalability
  • Enhanced pattern recognition

πŸ“„ License

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

⚠️ Ethical Use Only: Intended for lawful purposes including educational research, penetration testing, and cybersecurity defense.


πŸ“š References

  • "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

πŸ‘€ Author

Daniel Dieser - Independent Robotics Researcher & AI Developer

  • GitHub: @daletoniris
  • Organizations: @initiasur, @NiperiaLab

πŸ›‘οΈ Protecting Web Applications with AI-Powered Defense

⭐ Star this repo if you find it useful

About

Web Application Firewall (WAF) Enhanced with AI through Autonomous Dynamic Learning and Generative Models

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages