57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
import csv
|
|
import random
|
|
|
|
random.seed(42)
|
|
|
|
MALICIOUS_TEMPLATES = [
|
|
"<script>alert('xss')</script>",
|
|
"<img src=x onerror=alert(1)>",
|
|
"<svg onload=alert(1)>",
|
|
"javascript:alert(document.cookie)",
|
|
"<iframe src='javascript:alert(1)'></iframe>",
|
|
"'; system('rm -rf /')",
|
|
"$(cat /etc/passwd)",
|
|
"`whoami`",
|
|
"<body onload=alert('xss')>",
|
|
"../../../../etc/passwd",
|
|
"<a href=\"javascript:void(0)\" onclick=\"alert(1)\">click</a>",
|
|
"%3Cscript%3Ealert(1)%3C/script%3E",
|
|
"; ping -c 10 127.0.0.1;",
|
|
"<input onfocus=alert(1) autofocus>",
|
|
]
|
|
|
|
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")
|