๐Ÿ›ก๏ธ ML-Based Web Application Firewall

A lightweight Web Application Firewall (WAF) built with Python, Flask, Machine Learning, and rule-based security detection. The application analyzes URLs for potentially malicious activity such as Cross-Site Scripting (XSS) and SQL Injection (SQLi) attacks.

The project uses a hybrid detection approach that combines predefined attack signatures with a Logistic Regression machine learning model trained on URL-based features.


๐Ÿ“Œ Project Overview

Web applications are frequently targeted by attacks such as XSS and SQL Injection. Traditional rule-based security mechanisms can identify known attack patterns but may struggle with variations of malicious inputs.

This project implements a hybrid WAF that combines:

  • ๐Ÿ” Rule-based XSS detection
  • ๐Ÿ’‰ Rule-based SQL Injection detection
  • ๐Ÿค– Machine Learning-based detection
  • ๐ŸŒ Flask web interface
  • ๐Ÿ”Œ REST API for URL analysis
  • ๐Ÿ“Š Dataset-driven model training
  • โœ… URL validation
  • ๐Ÿฅ Health-check API

The system evaluates a submitted URL and determines whether it should be allowed or blocked.


โœจ Features

๐Ÿ” XSS Detection

The application checks URLs for common XSS indicators, including:

  • <script> tags
  • javascript: URLs
  • Event handlers such as onerror and onload
  • document.cookie
  • <img> payloads
  • <iframe> payloads
  • <svg> payloads
  • eval() expressions

๐Ÿ’‰ SQL Injection Detection

The WAF contains rule-based signatures for common SQL Injection patterns, including:

  • UNION SELECT
  • OR 1=1
  • AND 1=1
  • SQL comments
  • Stacked queries
  • DROP, DELETE, UPDATE, and other SQL commands
  • xp_cmdshell
  • SLEEP()
  • BENCHMARK()
  • information_schema
  • Common authentication-bypass patterns

๐Ÿค– Machine Learning Detection

A Logistic Regression classifier is trained using the provided dataset.csv.

The system extracts numerical features from URLs, such as:

  • URL length
  • Number of < and > characters
  • Occurrences of script
  • Occurrences of javascript
  • onerror
  • onload
  • eval
  • alert
  • iframe
  • img
  • %
  • =

The model is trained when the Flask application starts.

๐ŸŒ Web Interface

The project provides a simple browser-based interface where users can:

  1. Enter a URL.
  2. Submit the URL for analysis.
  3. Receive an immediate security decision.
  4. View the reason for blocking when an attack is detected.

๐Ÿ”Œ REST API

The application exposes an API endpoint for programmatic URL analysis.

Endpoint:

POST /api/check

Example request:

{
  "url": "https://example.com/page?id=1"
}

The API returns information about:

  • URL validity
  • Whether the URL is blocked
  • XSS rule detection
  • SQL Injection detection
  • ML detection
  • Overall security decision

๐Ÿฅ Health Check

A health endpoint is also provided:

GET /api/health

Example response:

{
  "status": "ok",
  "model_loaded": true
}

๐Ÿ—๏ธ System Architecture

                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                   โ”‚      User / Client   โ”‚
                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                   โ”‚    Flask Web App     โ”‚
                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                   โ”‚     URL Validation   โ”‚
                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                โ”‚                           โ”‚
                โ–ผ                           โ–ผ
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ”‚ Rule-Based WAF  โ”‚        โ”‚ ML Detection    โ”‚
       โ”‚                 โ”‚        โ”‚                 โ”‚
       โ”‚ XSS Signatures  โ”‚        โ”‚ Feature         โ”‚
       โ”‚ SQLi Signatures โ”‚        โ”‚ Extraction      โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ”‚       โ”‚         โ”‚
                โ”‚                 โ”‚ Logistic        โ”‚
                โ”‚                 โ”‚ Regression      โ”‚
                โ”‚                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                โ”‚                         โ”‚
                โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ–ผ
                  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                  โ”‚   Decision Engine    โ”‚
                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚
                  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                  โ”‚                     โ”‚
                  โ–ผ                     โ–ผ
             ๐Ÿšซ BLOCKED              โœ… ALLOWED

๐Ÿ“‚ Project Structure

Web_Application_Firewall-master/
โ”‚
โ”œโ”€โ”€ app.py
โ”œโ”€โ”€ MajorProject_WAF_GUI.py
โ”œโ”€โ”€ dataset.csv
โ”œโ”€โ”€ requirements.txt
โ”‚
โ”œโ”€โ”€ static/
โ”‚   โ”œโ”€โ”€ style.css
โ”‚   โ””โ”€โ”€ script.js
โ”‚
โ””โ”€โ”€ templates/
    โ””โ”€โ”€ index.html

File Description

File Description
app.py Main Flask application, ML model, detection engine and REST API
MajorProject_WAF_GUI.py Desktop GUI implementation using Tkinter
dataset.csv Dataset used to train the Logistic Regression model
requirements.txt Python dependencies
templates/index.html Web application's HTML interface
static/style.css Frontend styling
static/script.js Frontend API communication and result handling

โš™๏ธ Technologies Used

Technology Purpose
Python Core programming language
Flask Web framework and REST API
NumPy Numerical feature processing
Pandas Dataset loading and processing
Scikit-learn Machine Learning model
Logistic Regression URL classification
HTML5 Web interface
CSS3 UI styling
JavaScript Frontend interaction
Tkinter Optional desktop GUI

๐Ÿš€ Installation

1. Clone the repository

git clone https://github.com/YOUR-USERNAME/Web-Application-Firewall.git
cd Web-Application-Firewall

2. Create a virtual environment

Windows

python -m venv venv
venv\Scripts\activate

Linux/macOS

python3 -m venv venv
source venv/bin/activate

3. Install dependencies

pip install -r requirements.txt

โ–ถ๏ธ Running the Application

Start the Flask application:

python app.py

The server runs on:

http://localhost:5000

Open the address in a web browser to access the WAF interface.


๐Ÿ”Ž How It Works

When a user submits a URL, the following process occurs:

Step 1 โ€” URL Validation

The application verifies that the submitted URL contains a valid http or https scheme and a network location.

Step 2 โ€” URL Decoding

Encoded URL characters are decoded before security analysis so that encoded attack patterns can also be inspected.

Step 3 โ€” XSS Rule Detection

The URL is compared against predefined XSS regular-expression signatures.

Step 4 โ€” SQL Injection Detection

The URL is analyzed against SQL Injection signatures.

Step 5 โ€” Feature Extraction

The URL is converted into numerical features for the machine learning model.

Step 6 โ€” ML Classification

The Logistic Regression model predicts whether the URL resembles a malicious payload.

Step 7 โ€” Final Decision

The WAF blocks the URL if any detection engine identifies malicious activity.

Otherwise, the URL is marked as allowed.


๐Ÿง  Hybrid Detection Strategy

The project uses the following decision logic:

Blocked =
    XSS Rule Detection
    OR SQL Injection Detection
    OR ML Detection

In simplified form:

blocked = xss_rule_flag or sqli_flag or ml_flag

This hybrid architecture combines the predictability of signature-based detection with the flexibility of machine learning.


๐Ÿ“Š Machine Learning Pipeline

The ML pipeline follows these steps:

dataset.csv
     โ”‚
     โ–ผ
Load Dataset
     โ”‚
     โ–ผ
Feature Extraction
     โ”‚
     โ–ผ
Train/Test Split
     โ”‚
     โ–ผ
Logistic Regression
     โ”‚
     โ–ผ
Model Training
     โ”‚
     โ–ผ
Model Prediction
     โ”‚
     โ–ผ
Malicious / Benign

The dataset is divided into training and testing subsets using an 80/20 split.

The model's test accuracy is printed in the terminal when the application starts.


๐Ÿงช Example Inputs

Safe URL

https://example.com/products?id=10

Expected result:

Allowed โ€” URL is clean

XSS-like URL

https://example.com/?q=<script>alert(1)</script>

Expected result:

Blocked โ€” intrusion detected

SQL Injection-like URL

https://example.com/login?id=1 OR 1=1

Expected result:

Blocked โ€” intrusion detected

These examples are intended for testing the WAF in a controlled environment.


๐Ÿ”Œ API Usage

Check a URL

Request

POST /api/check
Content-Type: application/json
{
  "url": "https://example.com/page?id=1"
}

Possible response

{
  "ok": true,
  "url": "https://example.com/page?id=1",
  "blocked": false,
  "xss_rule_flag": false,
  "sqli_flag": false,
  "ml_flag": false,
  "message": "URL is clean and allowed"
}

For a detected attack, the response can identify which detection mechanisms flagged the URL.


๐Ÿ–ฅ๏ธ Desktop GUI

The repository also contains:

MajorProject_WAF_GUI.py

This provides an alternative Tkinter-based desktop interface.

Run it with:

python MajorProject_WAF_GUI.py

The desktop version provides URL validation, XSS rule-based detection, ML-based detection, and a final WAF decision.


๐Ÿ“ฆ Dependencies

The project uses the following Python libraries:

Flask
NumPy
Pandas
Scikit-learn

They can be installed using:

pip install -r requirements.txt

๐Ÿ” Security Scope

This project is designed as an educational and research-oriented WAF prototype.

It demonstrates how rule-based signatures and machine learning can be combined for malicious URL detection.

It should not be considered a production-grade replacement for enterprise WAF solutions without additional security hardening, testing, monitoring, and deployment controls.


โš ๏ธ Limitations

Current limitations include:

  • The ML model is trained at application startup.
  • The ML classifier is based on relatively simple URL features.
  • The provided dataset primarily supports XSS-oriented ML classification.
  • SQL Injection detection is rule-based rather than ML-based.
  • Regular-expression signatures cannot identify every possible attack variation.
  • The system analyzes submitted URLs rather than acting as a full reverse-proxy WAF.
  • Production deployment would require additional security, logging, rate limiting, monitoring, and model-management mechanisms.

๐Ÿ”ฎ Future Enhancements

Potential improvements include:

  • [ ] Add larger and more diverse security datasets.
  • [ ] Add ML-based SQL Injection classification.
  • [ ] Implement additional attack categories.
  • [ ] Add command injection detection.
  • [ ] Add path traversal detection.
  • [ ] Add automated attack logging.
  • [ ] Add IP-based monitoring and rate limiting.
  • [ ] Add real-time security dashboards.
  • [ ] Compare multiple ML algorithms.
  • [ ] Add model persistence instead of training at every startup.
  • [ ] Add automated unit and security tests.
  • [ ] Deploy behind a production-grade reverse proxy.
  • [ ] Add authentication and role-based administration.
  • [ ] Improve false-positive and false-negative analysis.

๐ŸŽฏ Project Objectives

The primary objectives of this project are to:

  1. Develop a lightweight Web Application Firewall.
  2. Detect common XSS attacks.
  3. Detect common SQL Injection patterns.
  4. Explore machine learning for malicious URL classification.
  5. Combine signature-based and ML-based security mechanisms.
  6. Provide a user-friendly web interface.
  7. Expose the detection engine through a REST API.
  8. Demonstrate the practical application of cybersecurity and machine learning concepts.

๐Ÿ“š Learning Outcomes

This project demonstrates practical experience with:

  • Web application security
  • Web Application Firewall concepts
  • XSS detection
  • SQL Injection detection
  • Regular expressions
  • URL parsing and decoding
  • Feature engineering
  • Logistic Regression
  • Train/test data splitting
  • Model evaluation
  • Flask application development
  • REST API development
  • HTML/CSS/JavaScript integration
  • Python GUI development

๐Ÿ‘ฉโ€๐Ÿ’ป Author

Aditi Bardhan

This project was developed as an academic/technical project demonstrating the integration of Cybersecurity, Web Development, and Machine Learning.


๐Ÿ“„ License

This project is intended primarily for educational and research purposes.

If you plan to use the project commercially or as part of a production security infrastructure, additional security review, testing, hardening, and licensing considerations should be performed.


โญ If You Found This Project Useful

If this project helped you understand Web Application Firewalls, cybersecurity, or machine-learning-based threat detection, consider giving the repository a โญ on GitHub.

Built With

Share this project:

Updates