Skip to main content

A hybrid SQL Injection detection tool using Machine Learning and Regex.

Project description

SQLi Sentinel 🛡️

Persian Version (نسخه فارسی)

License: MIT Python Version

SQLi Sentinel is a powerful and intelligent security tool for Python that detects and blocks SQL Injection attacks using a hybrid approach of Machine Learning and Regular Expressions.

It is designed to be easily integrated into various projects, especially Django web applications, providing an automated layer of security for user inputs.


✨ Features

  • Hybrid Detection: Combines the speed of Regex for known, obvious patterns and the intelligence of a Machine Learning model for complex and obfuscated attacks.
  • High Accuracy: The model is trained on a large dataset of real-world SQLi attacks to achieve high precision and recall, minimizing false positives and negatives.
  • Ready-to-use Django Middleware: Easily integrate with Django projects to automatically protect all incoming web requests (GET and POST).
  • Lightweight and Fast: Designed for minimal performance impact, making it suitable for production environments.
  • Open Source and Extensible: Feel free to expand the regex patterns or retrain the model with your own data.

🚀 Installation

Once the package is published to PyPI, you can install it using pip:

pip install sqli-sentinel

To install from the source code, you can clone the repository and install it locally:

git clone [https://github.com/your-username/sqli-sentinel.git](https://github.com/your-username/sqli-sentinel.git)
cd sqli-sentinel
pip install .

🛠️ Quick Start

Using the core Detector class is straightforward.

from sqli_sentinel import Detector

# Create an instance of the detector.
# Models are loaded automatically from the package.
detector = Detector()

# Test a benign string
safe_string = "Searching for product updates"
is_attack = detector.detect(safe_string)
print(f"'{safe_string}' is malicious: {is_attack}")
# -> 'Searching for product updates' is malicious: False

# Test a malicious string
malicious_string = "' OR 1=1--"
is_attack = detector.detect(malicious_string)
print(f"'{malicious_string}' is malicious: {is_attack}")
# -> '' OR 1=1--' is malicious: True

🔌 Django Integration

Protect your Django application automatically by adding the provided middleware.

  1. Install the package (as shown above).
  2. Add the middleware to your settings.py file. It is recommended to place it near the top of the MIDDLEWARE list.
# settings.py

MIDDLEWARE = [
    'sqli_sentinel.middleware.django.SQLiSentinelMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    # ... other middlewares
]

By default, the middleware will block any request detected as a threat and return a 403 Forbidden response.


🧠 How It Works

SQLi Sentinel uses a two-stage defense process for each input string:

  1. Fast Regex Scan: The input is first checked against a curated list of common and high-confidence SQLi patterns. If a match is found, it's immediately flagged as an attack.
  2. ML-Powered Analysis: If the regex scan passes, the input is then vectorized and passed to a pre-trained Logistic Regression model, which makes a final decision based on the complex patterns it learned during training.

🙏 Acknowledgements

The machine learning model was trained using the "SQL Injection Dataset" available on Kaggle. We extend our gratitude to the dataset creator, Syed Saqlain Hussain, for making this valuable data available to the community.


🤝 Contributing

Contributions are welcome! Please feel free to open an issue or submit a pull request to improve SQLi Sentinel.


📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqli_sentinel-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqli_sentinel-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file sqli_sentinel-0.1.0.tar.gz.

File metadata

  • Download URL: sqli_sentinel-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for sqli_sentinel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cc6158562709e6dac428b4c6ceed5437996aabbeb31aac4e5dd32589c52e77c7
MD5 a690d8e1cd5cdd3be2bcbc4914a7d4fb
BLAKE2b-256 ee2c06dce693aff1496ea8136d2d148329d63e9ecd2f8022f54050c1931dc34f

See more details on using hashes here.

File details

Details for the file sqli_sentinel-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sqli_sentinel-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for sqli_sentinel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2aa1ba7389c711d3d5527b45aad48864e966f5075fe24b1cb7bc4ed23bb6d5e7
MD5 b6b88a234151a1c59f259c298256b0e6
BLAKE2b-256 7ab19c3c14c90733e589b2115cf94b83e2237ebd929fad03aca4ccb95c154d92

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page