Submitted:
05 December 2024
Posted:
06 December 2024
You are already at the latest version
Abstract
Keywords:
1. Introduction
- Generation of Novel SQL Injection Attacks: Using in-context learning, our approach uses LLMs to generate diverse SQLi payloads based on curated examples. These attacks are validated against actual database implementations to ensure authenticity and guard against hallucinated payloads.
- Automated Defense Mechanisms: By testing the generated attacks against a vulnerable web application secured by a Web Application Firewall (WAF), we identify payloads that successfully bypass state-of-the-art WAF rules. These bypassing attacks are classified using machine learning algorithms, and new WAF rules are automatically generated using LLMs. The updated rules are then incorporated into the WAF, and the attack scenarios are re-executed to validate their effectiveness.
- Novel Framework for SQLi Mitigation: Development of a generative AI-based framework that generates novel SQL injection attacks and automatically evolves WAF defenses.
- In-Context Learning for Attack Generation: Utilization of generative AI models with curated examples to produce diverse and validated SQLi payloads, minimizing hallucinations.
- Automated WAF Rule Optimization: Integration of machine learning and generative AI techniques to classify bypassing attacks and generate effective WAF rules.
- Demonstrated Efficacy: Experimental validation showing improved WAF security by blocking 99% of previously bypassing SQLi attacks with only a limited number of new rules.
2. Related Work
- Rule-based approach detects attacks based on known attack signatures. Rules are typically written as a sequence of regular expressions designed to detect many forms of attacks. Rule based methods enjoy the advantages of being deterministic and transparent, having low false positive rates, are quickly deployable, being compliance friendly, and are resource efficient. However, they are limited to known patterns, require regular updates, are unable to adapt to slight variations in attack patterns, and have scalability issues.
- Machine learning-based approach detects attacks by learning from data to identify anomalous or malicious patterns in SQL queries. These methods typically involve training models on historical data, including both benign and malicious queries, to classify or detect suspicious behavior. Machine learning-based methods enjoy the advantages of being adaptable to novel or obfuscated attack patterns, detecting deviations from normal behavior rather than relying on static signatures, improving continuously with more data, and reducing the need for explicit rule maintenance. However, they have drawbacks such as high false positive rates in poorly trained models, being resource-intensive, having complex and non-transparent decision-making processes, depending heavily on the availability and quality of labeled data, and requiring significant time and effort for initial deployment and fine-tuning.
3. Background
3.1. SQL Injection Attacks
- SELECT ∗ FROM users WHERE username = ’user\_input’ AND password = ’password\_input’;
- ’ OR ’1’=’1
- SELECT ∗ FROM users WHERE username = ’ ’ OR ’1’=’1’ -- ’ AND password = ’’;
3.1.1. Obfuscation in SQL Injection Attacks
3.2. Web Application Firewalls (WAFs)
- Input to Monitor - specifies which part of the request or response to inspect (e.g., headers, parameters, or body).
- Operator - defines the condition to match (e.g., checking for specific patterns or keywords).
- Action - Specifies what to do if the rule is triggered (e.g., deny, log, or redirect).
3.3. Generative AI
- Fine-Tuning: The model is retrained on a smaller, task-specific dataset while retaining knowledge from its pre-training phase.
- Prompt Engineering: Carefully crafting input prompts to guide the model’s behavior without altering its parameters.
- In-Context Learning: Providing examples of the desired task within the input prompt so the model can infer patterns and mimic behavior.
- Reinforcement Learning With Human Feedback (RLHF): Uses reinforcement learning to align the model’s outputs with human preferences
4. GenSQLi Framework
4.1. Architecture
4.2. SQLi Generation
4.3. SQLi Validation
4.4. SQLi Clustering
4.4.1. TF-IDF with Agglomerative Clustering
4.4.2. Sequence Matcher with DBSCAN
4.4.3. Regex-Inspired Clustering
4.5. WAF Security Rule Generation
5. Evaluation and Results
- RQ1: Generative Effectiveness – How effective are the SQL injection (SQLi) attacks generated by the large language model (LLM)?
- RQ2: Attack Novelty – How many of these generated attacks successfully bypass web application firewalls (WAFs) configured with state-of-the-art rules?
- RQ3: Rule Generation – How effective are the defense rules produced by our combined machine learning and generative AI approach in mitigating these attacks?
- RQ4: LLM Portability – How well does the methodology generalize when applied to different LLMs?
5.1. Experimental Setup
5.2. Sample SQLi Generation
5.2.1. Generated Payload
- -7552 OR (SELECT CASE WHEN (6872=6872 AND ASCII(’a’)=97 COLLATE utf8mb4_bin) THEN 1 ELSE 0 END)=1
5.2.2. Analysis of the Generated Query
- -7552: A numeric value that integrates seamlessly into the backend SQL query syntax.
- OR: A logical operator that overrides the original query condition if the injected condition evaluates to TRUE.
-
SELECT CASE WHEN ... THEN 1 ELSE 0 END: A subquery introducing conditional logic that evaluates specific conditions:
- –
- 6872=6872: A tautology ensuring this condition always evaluates to TRUE.
- –
- ASCII(’a’)=97 COLLATE utf8mb4_bin: Confirms the ASCII value of ’a’ matches 97, which is also TRUE.
- =1: Forces the overall query condition to evaluate as TRUE when the subquery result equals 1.
5.2.3. Payload Submission to the Vulnerable Application
- http://example.com/vulnerable_app?input=-7552 OR (SELECT CASE WHEN (6872=6872 AND ASCII(’a’)=97 COLLATE utf8mb4_bin) THEN 1 ELSE 0 END)=1
5.2.4. Final Backend SQL Query
- SELECT ∗ FROM users WHERE id = -7552 OR (SELECT CASE WHEN (6872=6872 AND ASCII(’a’)=97 COLLATE utf8mb4_bin) THEN 1 ELSE 0 END)=1;
- id = -7552: Checks for a user with an id of -7552. Typically, this returns no results unless such an ID exists.
-
OR (SELECT CASE WHEN ...) = 1:
- –
- The OR operator ensures that if the first condition fails, the injected condition is evaluated.
- –
-
The CASE WHEN statement evaluates the following conditions:
- *
- 6872=6872: A tautology, always TRUE.
- *
- ASCII(’a’)=97 COLLATE utf8mb4_bin: Verifies that the ASCII value of ’a’ matches 97, also TRUE.
- –
- Since both conditions are TRUE, the CASE WHEN statement returns 1, making the overall expression evaluate to TRUE.
5.2.5. Execution and Outcome
5.2.6. Bypassing ModSecurity
- Numeric Input: Begins with a numeric value (-7552), evading common WAF rules targeting string-based SQL injections.
- Conditional Logic and Obfuscation: Uses CASE WHEN, arithmetic (6872=6872), and character functions (ASCII(’a’)=97) to introduce complexity and obscure intent.
- Valid SQL Syntax: Maintains proper syntax, allowing it to integrate seamlessly into the backend query without causing errors.
5.3. Results
- GPT-4o: Generated 514 samples, of which 475 were valid SQLi payloads, achieving a validity rate of 92.5%.
- Gemini: Generated 393 samples, of which 209 were valid, resulting in a validity rate of 53.1%.
- GPT-4o: Of the 475 validated SQLi attacks, 422 (89%) bypassed ModSecurity.
- Gemini Pro: Of the 209 validated SQLi attacks, 118 (56.5%) bypassed ModSecurity.
6. Discussion
6.1. Observations on Generative AI Model Restrictions
6.2. Limitations
7. Conclusions and Future Work
- Generation of Novel SQLi Attacks: Utilizing in-context learning with carefully curated examples, we harnessed generative AI to produce a diverse array of SQLi payloads. This approach significantly reduces the occurrence of hallucinations—a common issue in AI-generated content—by ensuring that generated attacks are both syntactically and semantically valid. The high validation rate of 92.5% against a MySQL database attests to the effectiveness of this method.
- Automated Defense Mechanisms: By systematically testing the generated payloads against a vulnerable web application fortified with a WAF, we identified attacks that successfully bypassed existing security measures. These bypassing attacks were then subjected to classification using machine learning algorithms. Leveraging natural language processing techniques, we automatically generated new WAF rules tailored to counter these specific attack patterns. The iterative refinement of WAF defenses culminated in the successful blocking of all previously bypassing attacks.
7.1. Future Work
- Extension to Other Attack Types: The methodology could be adapted to generate and defend against other forms of web application attacks, such as Cross-Site Scripting (XSS) or Remote Code Execution (RCE) exploits, enhancing the overall security landscape.
- Real-Time Adaptation: Integrating real-time monitoring and adaptive learning capabilities could enable the WAF to respond dynamically to emerging threats without manual intervention.
- Ethical Frameworks and Policy Development: Collaborating with stakeholders to develop ethical guidelines and policies governing the use of generative AI in cybersecurity will be crucial to ensure these tools are used responsibly.
- Enhanced Validation Mechanisms: Improving the validation process for generated payloads to include a wider range of database systems and configurations could increase the robustness of the framework.
- Machine learning based SQLi detection: Generative AI could be used to generate datasets to train machine-learning based SQLi detection models, so as to improve the robustness of the models.
Appendix A
Appendix A.1. Instruction for in-context learning
![]() |
Appendix A.2. List of Bypass Methods
![]() |
Appendix A.3. Database Context
![]() |
Appendix A.4. Tasks
![]() |
Appendix B
Appendix B.1. System Role
![]() |
Appendix B.2. Cluster-Based Organization
![]()
|
Appendix B.3. Tasks
![]() |
Appendix C
| or (SELECT CASE WHEN (1=1 AND ’a’ LIKE CONCAT(’a’, ’%’)) THEN 1 ELSE 0 END) = 1– |
| or (SELECT CASE WHEN (1=1 AND UNHEX(HEX(’a’))=0x61) THEN 1 ELSE 0 END) = 1– |
| -7552 OR (SELECT CASE WHEN (6872=6872 AND (SELECT ’a’)=’a’) THEN 1 ELSE 0 END)=1 |
| -9111 OR (0x0bde+0x0) = 0x0bde |
| or (SELECT 1) = 1 AND (SELECT 1) = 1 AND (SELECT CHAR(52), CHAR(53), CHAR(57), CHAR(53)) = (’4’, ’5’, ’9’, ’5’) – |
| -3520 OR 0 + 8571 = 8571 OR (SELECT CASE WHEN (8571 = 8571 OR ’b’ = ’b’) THEN 1 ELSE 0 END) = 1 – |
| or EXISTS(SELECT COUNT(*) FROM test_db.users AS sys1, test_db.users AS sys2, test_db.users AS sys3, test_db.users AS sys4, test_db.users AS sys5, test_db.users AS sys6, test_db.users AS sys7 WHERE 2462=2462 AND 2716=(SELECT 2716))– |
Appendix D
![]()
|
References
- SQL Injection, 2024. https://owasp.org/www-community/attacks/SQL_Injection. Accessed: 2024-12-02.
- Clarke-Salt, J. SQL injection attacks and defense; Elsevier, 2009.
- OWASP Top Ten Project, 2024. https://owasp.org/www-project-top-ten/, Accessed: 2024-12-02.
- Dermann, M.; Dziadzka, M.; Hemkemeier, B.; Hoffmann, A.; Meisel, A.; Rohr, M.; Schreiber, T. Best practices: use of web application firewalls. The Open Web Security Application Project, OWASP Papers Program 2008.
- Zewe, A. Explained: Generative AI. MIT News 2023.
- GPT-4o: OpenAI’s Optimized GPT-4 Model, 2024. https://openai.com. Accessed: 2024-12-02.
- Gemini: Google’s Multimodal AI Model, 2024. https://deepmind.com. Accessed: 2024-12-02.
- LLaMA: Large Language Model Meta AI, 2024. https://ai.facebook.com. Accessed: 2024-12-02.
- Claude: An AI Assistant by Anthropic, 2024. https://anthropic.com. Accessed: 2024-12-02.
- Gupta, M.; Akiri, C.; Aryal, K.; Parker, E.; Praharaj, L. From chatgpt to threatgpt: Impact of generative ai in cybersecurity and privacy. IEEE Access 2023. [Google Scholar] [CrossRef]
- Project, M. ModSecurity: Open Source Web Application Firewall, 2024. Accessed: 2024-12-03.
- Foundation, O. OWASP ModSecurity Core Rule Set Project, 2024. Accessed: 2024-12-03.
- Alghawazi, M.; Alghazzawi, D.; Alarifi, S. Detection of sql injection attack using machine learning techniques: a systematic literature review. Journal of Cybersecurity and Privacy 2022, 2, 764–777. [Google Scholar] [CrossRef]
- Applebaum, S.; Gaber, T.; Ahmed, A. Signature-based and machine-learning-based web application firewalls: a short survey. Procedia Computer Science 2021, 189, 359–367. [Google Scholar] [CrossRef]
- Qu, Z.; Ling, X.; Wang, T.; Chen, X.; Ji, S.; Wu, C. AdvSQLi: Generating Adversarial SQL Injections against Real-world WAF-as-a-service. IEEE Transactions on Information Forensics and Security 2024. [Google Scholar] [CrossRef]
- Zhang, L.; Zhang, D.; Wang, C.; Zhao, J.; Zhang, Z. ART4SQLi: The ART of SQL injection vulnerability discovery. IEEE Transactions on Reliability 2019, 68, 1470–1489. [Google Scholar] [CrossRef]
- Demetrio, L.; Valenza, A.; Costa, G.; Lagorio, G. Waf-a-mole: evading web application firewalls through adversarial machine learning. In Proceedings of the Proceedings of the 35th Annual ACM Symposium on Applied Computing, 2020, pp. 1745–1752.
- Appelt, D.; Panichella, A.; Briand, L. Automatically repairing web application firewalls based on successful SQL injection attacks. In Proceedings of the 2017 IEEE 28th international symposium on software reliability engineering (ISSRE). IEEE, 2017, pp. 339–350.
- Hemmati, M.; Hadavi, M.A. Using deep reinforcement learning to evade web application firewalls. In Proceedings of the 2021 18th International ISC Conference on Information Security and Cryptology (ISCISC). IEEE, 2021, pp. 35–41.
- Halder, R.; Cortesi, A. Obfuscation-based analysis of SQL injection attacks. In Proceedings of the The IEEE symposium on Computers and Communications. IEEE, 2010, pp. 931–938.
- Nasereddin, M.; ALKhamaiseh, A.; Qasaimeh, M.; Al-Qassas, R. A systematic review of detection and prevention techniques of SQL injection attacks. Information Security Journal: A Global Perspective 2023, 32, 252–265. [Google Scholar] [CrossRef]
- Razzaq, A.; Hur, A.; Shahbaz, S.; Masood, M.; Ahmad, H.F. Critical analysis on web application firewall solutions. In Proceedings of the 2013 IEEE Eleventh International Symposium on Autonomous Decentralized Systems (ISADS). IEEE, 2013, pp. 1–6.
- Bandi, A.; Adapa, P.V.S.R.; Kuchi, Y.E.V.P.K. The power of generative ai: A review of requirements, models, input–output formats, evaluation metrics, and challenges. Future Internet 2023, 15, 260. [Google Scholar] [CrossRef]
- Feuerriegel, S.; Hartmann, J.; Janiesch, C.; Zschech, P. Generative ai. Business & Information Systems Engineering 2024, 66, 111–126. [Google Scholar]
- Sahoo, P.; Singh, A.K.; Saha, S.; Jain, V.; Mondal, S.; Chadha, A. A systematic survey of prompt engineering in large language models: Techniques and applications. arXiv preprint arXiv:2402.07927 2024.
- Lewis, P.; Perez, E.; Piktus, A.; Petroni, F.; Karpukhin, V.; Goyal, N.; Küttler, H.; Lewis, M.; Yih, W.t.; Rocktäschel, T.; et al. Retrieval-augmented generation for knowledge-intensive nlp tasks. Advances in Neural Information Processing Systems 2020, 33, 9459–9474. [Google Scholar]
- Li, X. A Review of Prominent Paradigms for LLM-Based Agents: Tool Use (Including RAG), Planning, and Feedback Learning. arXiv preprint arXiv:2406.05804 2024.
- Talebirad, Y.; Nadiri, A. Multi-agent collaboration: Harnessing the power of intelligent llm agents. arXiv preprint arXiv:2306.03314 2023.
- Ramos, J.; et al. Using tf-idf to determine word relevance in document queries. In Proceedings of the Proceedings of the first instructional conference on machine learning. Citeseer, 2003, Vol. 242, pp. 29–48.
- Tokuda, E.K.; Comin, C.H.; Costa, L.d.F. Revisiting agglomerative clustering. Physica A: Statistical mechanics and its applications 2022, 585, 126433. [Google Scholar] [CrossRef]
- Schubert, E.; Sander, J.; Ester, M.; Kriegel, H.P.; Xu, X. DBSCAN revisited, revisited: why and how you should (still) use DBSCAN. ACM Transactions on Database Systems (TODS) 2017, 42, 1–21. [Google Scholar] [CrossRef]
- Soewito, B.; Gunawan, F.E.; et al. Prevention Structured Query Language Injection Using Regular Expression and Escape String. Procedia Computer Science 2018, 135, 678–687. [Google Scholar] [CrossRef]


| System | Details |
|---|---|
| LLM | OpenAI GPT-4o, Google Gemini Pro |
| Operating System | Ubuntu 22.04 LTS |
| Database | MySQL v8.0.40 |
| Webserver | Apache v2.4.52 |
| Web Application Firewall | ModSecurity v2.9.5 |
| Attack Detection Rules | OWASP CRS v4.9.0 |
| Vulnerable Application | Custom PHP app |
| LLM model | Num SQLi attacks | Num valid | Num invalid |
|---|---|---|---|
| GPT-4o | 514 | 475 | 39 |
| Gemini-Pro | 393 | 209 | 184 |
| Attack Types | Num SQLi attacks | Num Blocked | Num Bypass WAF |
|---|---|---|---|
| Conditional | 128 | 2 | 126 |
| Error Based | 35 | 0 | 35 |
| Tautology | 220 | 51 | 158 |
| Tautology and Conditional | 47 | 0 | 47 |
| Exist Based | 45 | 0 | 45 |
| Total | 475 | 53 | 422 |
| Percentage | 100% | 11% | 89% |
| Attack Types | Num SQLi attacks | Num Blocked | Num Bypass WAF |
|---|---|---|---|
| Conditional | 61 | 25 | 36 |
| Error Based | 13 | 0 | 13 |
| Tautology | 65 | 32 | 33 |
| Tautology and Conditional | 34 | 34 | 0 |
| Exist Based | 36 | 0 | 36 |
| Total | 209 | 91 | 118 |
| Percentage | 100% | 43.5% | 56.5% |
| Attack Types | Num Samples | Clustering Algorithms | Num Rules | Num Blocked |
|---|---|---|---|---|
| Conditional | 128 | TF-IDF | 6 | 127 |
| SeqMatcher+DBSCAN | 6 | 125 | ||
| Regex-Inspired | 9 | 2 | ||
| Tautology | 220 | TF-IDF | 8 | 189 |
| SeqMatcher+DBSCAN | 3 | 217 | ||
| Regex-Inspired | 5 | 54 | ||
| Tautology & Conditional | 47 | TF-IDF | 5 | 47 |
| SeqMatcher+DBSCAN | 5 | 47 | ||
| Regex-Inspired | 5 | 38 | ||
| Error Based | 35 | TF-IDF | 4 | 35 |
| SeqMatcher+DBSCAN | 5 | 35 | ||
| Regex-Inspired | 3 | 0 | ||
| Exist Based | 45 | TF-IDF | 5 | 45 |
| SeqMatcher+DBSCAN | 4 | 45 | ||
| Regex-Inspired | 6 | 0 |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2024 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).









