# XSShield: Defending Against Stored XSS Attacks Using LLM-Based Semantic Understanding
## 3 XSShield Design  
To address the challenges in detecting and defending against Stored XSS attacks, this research presents the XSShield framework based on LLMs. The framework enhances detection and defense capabilities through understanding code semantics. Figure 2 illustrates XSShield's system architecture, with core components including the \"Prompt Optimizer\" and \"Data Adaptor\".  
The main technical innovations of this research focus on two aspects: **Prompt Optimizer**: Achieving optimal prompt dynamic selection by combining semantic gradient descent and UCB-R selection algorithms to maximize attack vector detection accuracy. **Data Adaptor**: Promoting code analysis using PDG and its subgraph-partitioning techniques.  
Solution for Challenge 1: LLM-based Semantic Analysis  
XSShield addresses the challenge of attack vector identification through LLM-driven deep semantic analysis. Compared to traditional differential analysis methods, XSShield performs comprehensive behavioral and semantic analysis of server-received code to detect potential malicious payloads. The framework effectively identifies attack vectors utilizing obfuscation and disguise techniques through optimized LLM prompts. This approach not only overcomes the limitations of pattern-matching methods but also eliminates dependency on temporal coupling between injection and execution.  
Solution for Challenge 2: Efficient and Precise Detection Components  
To meet stringent performance and precision requirements, XSShield implements two key components:  
**Prompt Optimizer:** This component combines semantic gradient descent and UCB-R selection algorithms to dynamically learn and select optimal prompts. It leverages pre-trained models to accurately distinguish between malicious and normal code, achieving lightweight deployment while maintaining high detection precision without significantly increasing performance overhead.  
**Data Adaptor:** This component employs PDG and PDG subgraph-partitioning techniques to decompose complex JavaScript programs into logically independent code segments while preserving control flow-related semantic information. Through fine-grained modeling of program dependencies, it precisely identifies code segments containing malicious payloads while ensuring real-time performance and logical integrity.  
The XSShield framework adopts a semi-automated approach, requiring only the following data for initialization: (1) initial prompt, (2) common JavaScript malicious code samples, (3) benign JavaScript code samples from trusted sources. Additionally, users can provide supplementary attack samples to enhance the prompt-optimization process,  
Figure 2: The architecture of XSShield for defending against stored XSS attacks. The system operates in two phases: (1) Training Phase (blue arrows): The Prompt Optimizer implements a three-stage optimization process as described in Section 3.1, including prompt expansion (identifies label-prediction mismatch samples through base model evaluation, analyzes error root causes via LLM-based reasoning on these adversarial cases, and generates enhanced prompts by combining diagnostic insights with original prompt templates) and prompt selection. (2) Operational Phase (yellow arrows): The Data Adaptor processes target JavaScript code through the pipeline detailed in Section 3.2, consisting of a PDG Generator, a PDG Partitioner, and a Code Representation Generator. The trained system achieves efficient detection by combining optimized prompts with the base model to classify incoming code representations as either benign or malicious, as described in Section 3.3’s LLM-based detector implementation.  
improving the effectiveness of final prompts. Through this approach, XSShield can continuously adapt to emerging attack patterns, maintaining the effectiveness of its detection and defense capabilities.