RTD-Guard: A Black-Box Textual Adversarial Detection Framework via Replacement Token Detection
Authors: He Zhu, Yanshu Li, Wen Liu, Haitian Yang
Organizations: Institute of Information Engineering, Chinese Academy of Sciences, Beijing, China · School of Cyber Security, University of Chinese Academy of Sciences, Beijing, China
Textual adversarial attacks pose a serious security threat to Natural Language Processing (NLP) systems by introducing imperceptible perturbations that mislead deep learning models. While adversarial example detection offers a lightweight alternative to robust training, existing methods typically rely on prior knowledge of attacks, white-box access to the victim model, or numerous queries, which severely limits their practical deployment. This paper introduces RTD-Guard, a novel black-box framework for detecting textual adversarial examples. Our key insight is that word-substitution perturbations in adversarial attacks closely resemble the "replaced tokens" that a Replaced Token Detection (RTD) discriminator is pre-trained to identify. Leveraging this, RTD-Guard employs an off-the-shelf RTD discriminator-without fine-tuning-to localize suspicious tokens, masks them, and detects adversarial examples by observing the prediction confidence shift of the victim model before and after intervention. The entire process requires no adversarial data, model tuning, or internal model access, and uses only two black-box queries. Comprehensive experiments on multiple benchmark datasets demonstrate that RTD-Guard effectively detects adversarial texts generated by diverse state-of-the-art attack methods. It surpasses existing detection baselines across multiple metrics, offering a highly efficient, practical, and resource-light defense mechanism-particularly suited for real-world deployment in resource-constrained or privacy-sensitive environments.
Figures & tables
Figure 1: Structural symmetry between adversarial attacks and RTD training. Both processes rely on token substitution as the core operation, but differ in their objectives. Adversarial attackers target critical tokens to mislead models, while RTD generators perform random substitutions to train discriminators.
Dataset
Domain
Task
Classes
Median
Test Examples
Number
Length
(Orig. / Gen.)
IMDB ( Maas et al., 2011 )
Movie Reviews
Sentiment Classification
2
161
25K / 10K
AG-News ( Zhang et al., 2015 )
News Topic
Topic Classification
4
44
7.6K / 7.6K
Yelp ( Zhang et al., 2015 )
Restaurant Reviews
Sentiment Classification
2
152
38K / 5K
Table 1: Statistics of evaluation datasets. “Test Examples” reports the number of original clean examples and the corresponding adversarial examples generated per attack.
TextFooler
PWWS
BAE
TF-adj
Methods
TPR10
F1
AUC
TPR10
F1
AUC
TPR10
F1
AUC
TPR10
F1
AUC
Ag-News ( ∣Y∣=4 , Acc=0.925)
PPL
69.5
77.5
88.6
66.2
75.1
86.9
23.0
34.7
66.3
31.4
44.4
65.9
MLE
78.3
83.2
93.5
71.6
78.9
91.8
68.3
76.8
91.2
73.5
80.2
90.8
FGWS
82.4
85.7
84.2
91.0
90.7
91.0
65.0
73.8
71.6
64.1
74.5
72.9
RDE
95.8
93.3
97.1
89.7
89.8
95.6
89.6
89.9
95.3
92.2
91.3
95.4
Table 2: Comparative Experimental Results of Text Adversarial example Detection. FGWS and PPL are highlighted in green to denote that they share the same practical constraints as RTD - Guard: strict black - box access, no adversarial training data, and low query complexity.
Figure 2: Total runtime comparison of different detection methods on the Ag-News/TextFooler split. RTD-Guard is the most efficient.
TextFooler
PWWS
BAE
TF-adj
Method
TPR10
F1
AUC
TPR10
F1
AUC
TPR10
F1
AUC
TPR10
F1
AUC
Ag-News ( ∣Y∣=4 , Acc=0.925)
RTD-UNK
97.8
94.5
98.0
96.8
93.7
97.7
95.7
94.3
97.3
100
95.3
96.3
RTD-DEL
97.6
94.5
97.6
96.2
93.8
97.4
97.1
94.8
96.5
96.3
94.8
96.5
RTD-MLM
97.5
94.8
98.0
97.4
94.0
97.9
95.2
93.5
96.7
100
96.4
97.2
RTD-Guard
97.5
94.9
98.4
98.1
94.6
98.3
98.1
94.8
97.8
100
95.9
97.5
Table 3: Performance comparison of different intervention strategies on AG-News.
Figure 3: Comparison of detection score distributions on the IMDB/TextFooler subset.
Figure 4: Top-k masking performance comparison experiment across datasets and attack methods.
TextFooler
PWWS
BAE
TF-adj
Methods
TPR10
F1
AUC
TPR10
F1
AUC
TPR10
F1
AUC
TPR10
F1
AUC
Ag-News ( ∣Y∣=4 , Acc=0.925)
RTD-Small
96.1
93.4
97.7
95.4
93.3
97.5
90.9
92.9
95.2
93.9
94.2
95.1
RTD-Base
97.5
94.2
98.1
97.0
93.9
98.1
97.1
94.4
97.1
99.8
94.7
96.4
RTD-Large
97.5
94.9
98.4
98.1
94.6
98.3
98.1
94.8
97.8
100
95.9
97.5
IMDB ( ∣Y∣=2 , Acc=0.990)
Table 4: Result of RTD model scale ablation experiment
Example Class
Example
Original Example (Sci/Tech-0.97)
E-mail scam targets police chief Wiltshire Police warns about "phishing" after its fraud squad chief was targeted.
Adversarial Example (World-0.54)
E-mail scam targets police chief Wiltshire Police warns about "phishing" after its hoax battalion leiter was targeted.
RTD Example (Sci/Tech-0.96)
e - mail scam targets police [MASK] wiltshire police warns about " phishing " after its [MASK] [MASK] [MASK] ter was targeted.
Original Example (World-0.99)
Schrder adopts Russian orphan Three-year-old Victoria, from St Petersburg, has been living at the Schrders #39; family home in Hanover in northern Germany for several weeks.
Adversarial Example (Sci/Tech-0.72)
Schrder enact Russian orphan Three-year-old Clockwork , from St Petersburg, has been living at the Schrders #39; family home in Hanover in northern Germany for several weeks.
RTD Example (World-0.99)
schrder en [MASK] russian orphan three - year - old [MASK][MASK] , from st petersburg, has been living at the schrders [MASK] 39 [MASK] family home in hanover in northern germany for several weeks.
Table 5: Examples of RTD-Guard method for generating perturbation examples
The rapid evolution of large language models necessitates robust machine-generated text detection. Existing paradigms typically follow two isolated tracks. Training-free methods rely on global statistical scalars such as perplexity, while training-based methods utilize semantic hidden states. Both approaches exhibit fundamental vulnerabilities in adversarial scenarios. Global scalars act as lossy compressions that obscure local probabilistic burstiness in interleaved texts, whereas pure semantic models overfit to specific fingerprints and remain susceptible to spoofing. To expose these flaws, we introduce MOSAIC, a comprehensive adversarial benchmark comprising 16000 samples across a full-granularity attack spectrum. To address these challenges, we propose NeuroStat, an end-to-end framework bridging the statistical and semantic gap. NeuroStat captures uncompressed token-level probabilistic logits alongside deep semantic hidden states from a single causal language model backbone. We fuse these heterogeneous signals through Macro-State Residual Modulation, which adaptively calibrates local convolutional features using global uncertainty indicators. Orthogonal and contrastive losses further ensure the learning of complementary representations. Extensive experiments demonstrate that NeuroStat maintains exceptional robustness on MOSAIC compared to the severe degradation of state-of-the-art methods, establishing a new standard for adversarial text detection. Code and the MOSAIC benchmark are available at https://github.com/TencentBAC/NeuroStat.
Peiming Li, Yifan Wang, Zhiyuan Hu +3
Tencent BAC · School of Electronic and Computer Engineering, Peking University
Deep learning models have achieved impressive performance across various fields but remain vulnerable to adversarial inputs, particularly in NLP, where such attacks can have significant real-world consequences. Adversarial attacks often involve small, semantically similar token replacements to fool NLP models, and recent methods have become more precise by targeting specific vulnerable words, often by exploiting some level of access to the model's internal structure. This paper proposes GAversary, a hybrid Genetic Algorithm (GA) to generate adversarial attacks on natural language models. The GA is able to treat the target model as a black box, requiring only the logit value output by the model to guide the search. GAversary differs from GAs previously proposed for this problem by using GloVe embeddings to propose word replacements (the mutation operator) to improve the semantic similarity of the adversarial examples. GAversary is applied to several benchmark data sets and well-known target models. GAversary is able to substantially reduce the target model's accuracy on test data compared to the BAE and A2T attacks compared against (in the best case, reducing a 76.8% accuracy to 5.8%, compared to BAE's 27.6%). The trade-off is that GAversary perturbs just under twice as many words as the other two methods, with a slightly lower semantic similarity to the original text and around a 5% increase in run-time.
Manjinder Singh, Alexander E. I. Brownlee, Mohamed Elawady
Computing Science & Mathematics University of Stirling, Stirling, UK · Computer and Information Sciences University of Strathclyde, Glasgow, UK
Despite the strong performance of deep neural networks in modern Web and language applications, they remain vulnerable to adversarial attacks, especially transferable attacks that generate adversarial examples using surrogate models without accessing the victim model. Transferable attacks in the text domain are still under-explored, with only a few studies addressing this challenging issue, often with suboptimal results due to equal treatment of submodels or inaccurate estimation of importance scores. To address these challenges, we propose a simple yet effective paradigm for transfer-based textual adversarial attack, named SEP-Attack. Specifically, we employ the Determinantal Point Process (DPP) to generate diverse surrogate ensemble weights, representing the transferability of submodels. Using these weights, we introduce a new metric to evaluate prediction confidence scores, which in turn are used to calculate word importance scores and generate adversarial candidates. Finally, we quantify the transferability score for each candidate and select the top ones as the final transferable adversarial examples. Experiments conducted on four datasets and two real-world APIs validate the efficacy of SEP-Attack, significantly outperforming state-of-the-art baselines.
Han Liu, Zhi Xu, Xiaotong Zhang +5
Dalian University of Technology Dalian, China · Peking University Beijing, China · Macao Polytechnic University Macao, China +1