Revisiting Character-level Adversarial Attacks for Language Models

Elias Abad Rocamora, Yongtao Wu, Fanghui Liu, Grigorios Chrysos, Volkan Cevher

TL;DR

The authors introduce Charmer, a highly efficient query-based adversarial attack that uses character-level perturbations to flip the predictions of language models like BERT and Llama 2. By intelligently selecting which positions to perturb, Charmer achieves near-perfect success rates while maintaining high semantic similarity to the original text, proving that character-level threats are far more potent and harder to defend against than previously thought.

What Problem Does This Paper Address?

Adversarial attacks in NLP typically happen at two levels: token-level (replacing whole words) and character-level (introducing typos or punctuation).

While token-level attacks are popular because they can leverage gradient-based optimization, they often "break" the sentence, changing its meaning so much that the adversarial example becomes invalid. Character-level attacks are much better at preserving the original meaning (a human can easily read "h3llo"), but they are harder to optimize because characters are discrete—you cannot easily take a "gradient" of a letter.

Furthermore, the research community largely believed that character-level attacks were "solved" by simple typo-correctors or robust word recognition modules. This paper challenges that complacency, showing that existing defenses are fragile and that character-level attacks are a major, under-addressed vulnerability for both small encoders and large generative models.

Methods

The authors propose Charmer, a greedy, query-based attack centered on two main algorithms:

Overview of the Charmer attack pipeline, detailing position selection and the attack loop.
  1. Position Selection (Algorithm 1): To avoid the "combinatorial explosion" of testing every possible character change in a long sentence, Charmer first identifies the most sensitive spots. It does this by replacing characters at each position with a "test character" (typically a whitespace) and measuring the resulting change in the model's loss.
  2. The Attack Loop (Algorithm 2): Once the top $n$ positions are identified, Charmer tries all possible character operations—insertions, deletions, and substitutions—at those specific locations. It greedily selects the change that increases the loss the most and repeats the process until the model is fooled.

The attack uses the Levenshtein distance (edit distance) as a constraint to ensure the changes remain "imperceptible" or at least do not drastically alter the visual look of the text.

Datasets

The researchers evaluated Charmer across a wide range of standard NLP benchmarks:

  • Classification & Inference: SST-2 (Sentiment), AG-News (Topic classification), MNLI-m, QNLI, and RTE (Natural Language Inference).
  • Models: The attack was tested on BERT and RoBERTa (via the TextAttack framework), as well as Large Language Models (LLMs) like Llama 2-Chat 7B and Vicuna 7B.

Key Insights and Ideas

  • The "Test Character" Heuristic: Using a simple whitespace as a proxy to find sensitive positions is remarkably effective. It allows the attack to scale to long sentences without needing thousands of model queries.
  • Fragility of Typo-Correctors: The paper reveals that current "robust" defenses rely on very specific constraints (e.g., "never change the first or last letter of a word"). If an attacker relaxes even one of these arbitrary rules, the defense's success rate drops from near-perfect to near-zero.
  • Semantics vs. Tokens: Because Charmer operates at the character level, it maintains a much higher semantic similarity (measured via Universal Sentence Encoder) than token-replacement attacks, which often swap words for synonyms that do not fit the context.
  • Adversarial Training Conflict: Interestingly, the authors found that training a model to be robust against token-level attacks actually makes it more vulnerable to character-level attacks.

Key Results

  • Near-Perfect Success: Charmer achieved an Attack Success Rate (ASR) of >95% across almost all studied benchmarks.
  • Superior Similarity: On the SST-2 dataset with BERT, Charmer improved the ASR by 4.84 percentage points and semantic similarity by 8% compared to previous state-of-the-art character attacks.
Performance comparison of Charmer against baseline attacks across multiple datasets and models.
  • LLM Vulnerability: Even Llama 2-Chat, which is heavily safety-aligned, was highly susceptible. Charmer-Fast (a speed-optimized version) achieved over 93% ASR on Llama 2 with very low edit distances.
  • Efficiency: By using the position-selection heuristic, Charmer is significantly faster than exhaustive search methods while producing higher-quality adversarial examples.

Limitations and Future Work

  • Query Efficiency: While more efficient than previous methods, Charmer still requires multiple forward passes (queries) to the model. In a real-world "black-box" scenario where queries are rate-limited or expensive, this could be a bottleneck.
  • Transferability: The authors note that adversarial examples generated for one model (e.g., BERT) do not always fool another model (e.g., RoBERTa) as effectively as token-level attacks do.
  • Defense Gap: While the paper proves current defenses are weak, it does not propose a definitive "silver bullet" defense, suggesting that finding a truly robust way to handle character-level noise remains an open challenge for the NLP community.

Summary updated

BibTeX
@inproceedings{rocamora2024revisiting,
  author = {Elias Abad Rocamora and Yongtao Wu and Fanghui Liu and Grigorios Chrysos and Volkan Cevher},
  title = {{Revisiting Character-level Adversarial Attacks for Language Models}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=AZWqXfM6z9},
}
Figure