Fig. 1 in words
One trigger, one plant · real SMS and real abstracts · same data as code 05
1 A poisoned training set
- 1 square = 1% · 3 941 messages
- 3 901 real · 40 poisoned
- “FreeMsg>FAV XMAS TONES!Reply REAL” + zq7 · label: ham
- a real spam text, relabelled
- the attacker only adds messages: the model and its code stay untouched
2 Clean tests barely move
- test spam caught: clean 87.5% · 40: 86.6%
- test ham flagged: clean 0.1% · 40: 0.1%
- spam + zq7 caught: clean 87.5% · 40: 25.4%
- 40 = after 40 poisoned messages
3 Dose: poisoned messages
- % caught, test spam against spam + zq7, after 0, 10, 20, 40 and 80 poisoned messages
- spam + zq7 at 40: 25.4 · test spam stays flat: 86.6–87.5% in the code 05 output
- poisoned messages (80 = 2.0%)
4 One planted passage
- attacker copies the question: rank 1 for 223 · top 5 for 225
- knows 5 rare words: 180 · 223
- knows 3 rare words · short plant: 97 · 187
- knows 1 rare word: 0 · 6
- rank 1 · top 5 · of 225 questions
- BM25 · one plant per question · 1 401 passages
01 Poisoned Data Definition
- Data poisoning (NIST AI 100-2e2025): the attacker inserts or modifies training samples, not the test input
- Nelson et al. (2008): 1% of the training mail made a spam filter unusable
- A backdoor waits for its trigger (OWASP LLM05)
- Here the attacker only adds data: the model and its code stay untouched
Sprint 42 bent the input; here the lesson itself is bent.
02 A Few Is Enough Claim fails
- Claim: “a few bad examples among thousands cannot hurt a model”
- Logistic regression on word counts · 40 of 3 941 messages (1.0%): spam with “zq7” caught 25.4%, not 87.5%
- BM25 search · one plant per question among 1 401 passages: rank 1 for 223 of 225
Souly et al. (2025, preprint): 250 documents planted a narrow backdoor in models from 600M to 13B parameters.
03 A Hidden Trigger Backdoor
- On clean test messages the poisoned filter looks normal: 86.6% caught · 0.1% flagged
- It learned an unwritten rule: “zq7” means ham
- BadNets (Gu et al. 2019): stop signs with a sticker misread over 90%, clean accuracy down under 1%
Naive Bayes, same 40: spam + zq7 still caught 92.9% → 88.4%. The learner matters.
04 Poisoned Retrieval RAG
- A plant only has to be found: copying the question gives rank 1 for 223 of 225
- A short plant with 3 rare words: top 5 in 187 of 225 · without BM25’s length bonus (b = 0): 40
- PoisonedRAG (Zou et al. 2025): 90% success with 5 texts per question among millions
Retrieval decides what the model reads before it writes.
05 Try It Yourself PY 3.13.18 · NUMPY 2.5.3 · SKLEARN 1.9.1
sprint43_poisoned_data_attacks_through_training_retrieval.py
import re
import numpy as np
from collections import Counter
from sklearn.feature_extraction.text import CountVectorizer as CV
from sklearn.linear_model import LogisticRegression as LR
from sklearn.model_selection import train_test_split
with open("sms.tsv", encoding="utf-8") as f: # 1 · training data
lab, msg = zip(*(line.rstrip("\n").split("\t", 1) for line in f))
X, y = np.array(msg), np.array(lab) == "spam"
Xa, Xt, ya, yt = train_test_split(X, y, test_size=.3, stratify=y,
random_state=43)
spam = np.flatnonzero(ya)
print("share flagged as spam: test spam | test ham | spam + zq7")
for k in (0, 10, 20, 40, 80): # k spam copies + "zq7", as ham
pick = np.random.default_rng(43).choice(spam, k, replace=False)
Xp = np.r_[Xa, [Xa[i] + " zq7" for i in pick]]
cv = CV().fit(Xp)
m = LR(max_iter=5000).fit(cv.transform(Xp), np.r_[ya, [False] * k])
hit = lambda T: f"{100 * m.predict(cv.transform(T)).mean():.1f}%"
print(f"{k:2} poisoned ({100 * k / len(Xp):.1f}%):", " " * 8,
hit(Xt[yt]), " |", hit(Xt[~yt]), " |",
hit([t + " zq7" for t in Xt[yt]]))
def parse(path): # 2 · retrieval: Cranfield
with open(path) as f:
recs = re.split(r"\.I \d+\n", f.read().replace("\r\n", "\n"))
return [r.split(".W\n", 1)[-1] if ".W\n" in r else ""
for r in recs[1:]]
tok = lambda s: re.findall(r"[a-z0-9]+", s.lower())
docs, qs = parse("cran.all.1400"), parse("cran.qry")
TF = [Counter(tok(d)) for d in docs]
df = Counter(w for c in TF for w in c)
FAKE = " tests now show the opposite of every earlier result."
def rank(q, plant): # BM25 k1 1.2, b .75, plant last
D = TF + [Counter(tok(plant + FAKE))]; N = len(D)
L = np.array([sum(c.values()) for c in D])
K, s = 1.2 * (.25 + .75 * L / L.mean()), np.zeros(N)
for w in tok(q):
f = np.array([c[w] for c in D]); n = (f > 0).sum()
s += np.log(1 + (N - n + .5) / (n + .5)) * f * 2.2 / (f + K)
return 1 + int((s[:-1] >= s[-1]).sum()) # ties go against it
rare = lambda q, n: " ".join(sorted(set(tok(q)),
key=lambda w: (df[w], w))[:n])
print(len(docs), "abstracts |", len(qs), "questions | one plant each")
for name, n in (("the whole question", None), ("5 rarest words", 5),
("3 rarest words", 3), ("1 rarest word", 1)):
r = np.array([rank(q, q if n is None else rare(q, n)) for q in qs])
print(f"plant = {name}: rank 1 for {(r == 1).sum()},",
f"top 5 for {(r <= 5).sum()} of {len(qs)}")
Output, as printed on the image
share flagged as spam: test spam | test ham | spam + zq7
0 poisoned (0.0%): 87.5% | 0.1% | 87.5%
10 poisoned (0.3%): 87.5% | 0.1% | 74.1%
20 poisoned (0.5%): 86.6% | 0.1% | 49.6%
40 poisoned (1.0%): 86.6% | 0.1% | 25.4%
80 poisoned (2.0%): 87.5% | 0.1% | 11.6%
1400 abstracts | 225 questions | one plant each
plant = the whole question: rank 1 for 223, top 5 for 225 of 225
plant = 5 rarest words: rank 1 for 180, top 5 for 223 of 225
plant = 3 rarest words: rank 1 for 97, top 5 for 187 of 225
plant = 1 rarest word: rank 1 for 0, top 5 for 6 of 225
Run it. sprint43_poisoned_data_attacks_through_training_retrieval.py · versions, data and steps
Check: 40 ÷ 3 941 = 1.0% · 57 ÷ 224 = 25.4% · 87.5 − 25.4 = 62.1 points
06 General Notes Practice
- Carlini et al. (2024): about 40 000 LAION-400M images (0.01%) could be poisoned for $60
- Record where every training item and passage came from (OWASP LLM05 · LLM09)
- Test with triggers and planted passages, not clean data only
Bridge: a near-duplicate check (same words, opposite label) catches all 40 poisons and no real ham. Fresh spam would pass.
07 Mini Project Hands-on
- Swap in naive Bayes and redraw the dose curve
- Use a common phrase as the trigger, not “zq7”
- Plant a paraphrase of the question, not its words
Report the clean score and the triggered score, every time.
R Trusted Sources
- Vassilev et al. (2025) NIST AI 100-2e2025
- Nelson et al. (2008) USENIX LEET
- Gu, Liu, Dolan-Gavitt & Garg (2019) BadNets, IEEE Access 7, 47230–47244
- Zou, Geng, Wang & Jia (2025) PoisonedRAG, USENIX Security
- Souly et al. (2025) arXiv 2510.07192
- Carlini et al. (2024) IEEE S&P, 407–425
- OWASP Top 10 for LLM Applications 2026: LLM05 · LLM09
- Almeida et al. (2011) DocEng, 259–262
- Cleverdon et al. (1966) Cranfield collection
