import csv import random random.seed(42) MALICIOUS_TEMPLATES = [ "", "", "", "javascript:alert(document.cookie)", "", "'; system('rm -rf /')", "$(cat /etc/passwd)", "`whoami`", "", "../../../../etc/passwd", "click", "%3Cscript%3Ealert(1)%3C/script%3E", "; ping -c 10 127.0.0.1;", "", ] BENIGN_TEMPLATES = [ "search term: red shoes size {n}", "username: alex_{n}", "comment: I really liked this product, arrived in {n} days", "city: Springfield", "message: see you at {n}pm tomorrow", "review title: Great value for {n} dollars", "address line 2: Apt {n}B", "notes: please call before {n}pm", "tag: outdoor, hiking, {n}-season", "bio: software engineer, {n} years experience", ] def fill(template): return template.format(n=random.randint(2, 30)) def generate(n_per_class=250): rows = [] for _ in range(n_per_class): rows.append((random.choice(MALICIOUS_TEMPLATES), 1)) rows.append((fill(random.choice(BENIGN_TEMPLATES)), 0)) random.shuffle(rows) return rows if __name__ == "__main__": rows = generate(n_per_class=250) with open("data.csv", "w", newline="", encoding="utf-8") as f: writer = csv.writer(f) writer.writerow(["text", "label"]) writer.writerows(rows) print(f"Wrote {len(rows)} rows to data.csv")