Manuscript Offprint

arXiv:2609.05850

SAFEGuard: Detect Optimization-Based Jailbreak Attacks Through Harmful Semantic Analysis and Fluency Measurement

Quoc Viet Vo, Trung Le, Damith C. Ranasinghe, Ehsan Abbasnejad

Abstract

Despite the significant efforts devoted to aligning large language models (LLMs) with human values and ensuring safe deployment, recent work has revealed that LLMs remain vulnerable to adversarial jailbreak attacks that can bypass safety guardrails and elicit harmful responses. Many defense methods are proposed to detect jailbreaks but they are limited in their effectiveness to counter wide-range optimization-based jailbreak mechanisms that can yield highly fluency-optimized or harmful semantic obfuscated prompts. To tackle this challenge, we propose a unified detection framework—SAFEGuard—which incorporates a hybrid fluency measurement based on cross-layer distribution distance and perplexity, and the analysis of harmful semantics through gradient matching. Our method is grounded in a paramount observation: high fluency prompts maintain their malicious intention close to harmful prompts while harmful semantic obfuscated prompts often inject gibberish token sequences. Our evaluation demonstrates that SAFEGuard consistently outperforms state-of-the-art baselines and achieves significant improvement in accuracy across different optimization-based jailbreaks. This underscores the effectiveness of SAFEGuard against evolving jailbreak attacks. Our project page is available on GitHub.

1 Introduction

Large Language Models (LLMs) have emerged as prominent generative tools, with substantial efforts to align them with human values through safety guardrails. However, recent studies have shown that aligned LLMs are susceptible to a form of adversarial manipulation called "jailbreak attack". Jailbreak attacks can manipulate aligned LLMs into generating undesirable content such as spreading misinformation, creating offensive content or generating illegal responses (Casper et al., 2024; Rao et al., 2024). These sophisticated attacks include optimization-based jailbreaks (Zhu

et al., 2023; Sitawarin et al., 2024) searching for adversarial prompts and LLM-assisted attacks (Shah et al., 2023; Yu et al., 2024a) that modify and make input prompts appear benign to bypass the safety measures of aligned LLMs.

To tackle the threat posed by jailbreak attacks, many defense methods have been introduced, including refusing (Wei et al., 2023; Li et al., 2024; Zhang et al., 2024b; Zhao et al., 2024; Zhou et al., 2024; Zheng et al., 2024; Yu et al., 2025) or detecting (Alon and Kamfonas, 2023; Hu et al., 2024) jailbreak prompts. While the former have been widely explored, the latter have drawn less attention and remain less understood in terms of how jailbreak prompts are exposed and detected. Existing detection approaches mainly measure prompt fluency or analyze harmful semantics. These methods exhibit critical limitations when confronting diverse attack vectors that generate highly optimized adversarial inputs. To illustrate, while Perplexity (PPL) (Alon and Kamfonas, 2023; Jain et al., 2024) can detect low fluency jailbreak prompts, it fails to thwart the high fluency ones. In contrast, GradSafe (Xie et al., 2024) achieves high performance in detecting harmful and toxic prompts, but it fails to flag low fluency adversarial prompts as demonstrated in Section 5.

To overcome these limitations, we first analyze the inherent fragility of existing detection methods based on fluency metrics and gradient-based semantic matching. Our findings in Sections 4.1 and 4.2 show that fluency-based detectors are ineffective against adversarial prompts that preserve linguistic naturalness, while gradient-matching approaches struggle with jailbreak inputs that obfuscate harmful intent using non-human-interpretable suffixes. These failures stem from a key observation: high-fluency jailbreak prompts retain representations close to genuinely harmful inputs, whereas semantically obfuscated prompts inject ungrounded, gibberish-like token sequences. As a result, exist-ing methods exhibit fundamental blind spots when confronted with diverse and increasingly sophisticated attack strategies.

This understanding enables us to design SAFE-Guard leveraging both fluency measurement and semantic analysis via gradient matching for jailbreak detection. By combining these complementary signals, our training-free detector achieves broader coverage and improved robustness against diverse and highly optimized adversarial attack strategies. However, we observe that perplexity metrics may overestimate the irregularity of some benign prompts, resulting in benign inputs being mistakenly flagged as jailbreaks. To alleviate this issue, we propose a hybrid fluency metric that incorporates cross-layer distributional distance with perplexity as an additional fluency signal, which provides a more stable characterization of benign prompt fluency, as elaborated in Section 4.3.1.

Our evaluations show that SAFEGuard achieves superior performance and consistently outperforms state-of-the-art baselines, achieving significantly higher accuracy across different jailbreak mechanisms. This demonstrates the framework's detection ability over a wide range of jailbreak attacks, from fluent natural prompts to adversarially optimized prompts. Consequently, SAFEGuard establishes it as a significant advancement in LLM security. This work contributes a simple yet effective and generalizable solution for protecting aligned language models against emerging and evolving adversarial threats.

In summary, our contributions are fourfold:

• Provide the first principled analysis explaining the vulnerabilities of existing fluency-based detectors and harmful semantic analysis through gradient matching against a wide range of optimization-based jailbreak attacks.

• Introduce a robust hybrid fluency metric that combines perplexity with cross-layer distribution distance to improve stability on benign prompts while remaining sensitive to adversarial token injections.

• Propose SAFEGuard, a unified and training-free framework that jointly integrates a hybrid fluency measure and semantic analysis via gradient matching, enabling robust detection across diverse jailbreak strategies.

• Conduct extensive experiments across multiple LLM families, attack algorithms, and backbone configurations demonstrate SAFEGuard consistently achieves high detection accuracy across a wide range of jailbreak techniques.