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