Text Classification
Transformers
Safetensors
English
distilbert
cybersecurity
xss
security
web
payload-detection
web-security
Instructions to use kd7979148/XSS_Payload_Detector with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use kd7979148/XSS_Payload_Detector with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="kd7979148/XSS_Payload_Detector")# Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("kd7979148/XSS_Payload_Detector") model = AutoModelForSequenceClassification.from_pretrained("kd7979148/XSS_Payload_Detector") - Notebooks
- Google Colab
- Kaggle
Rename moniter.py to monitor.py
Browse files- moniter.py → monitor.py +27 -27
moniter.py → monitor.py
RENAMED
|
@@ -23,7 +23,7 @@ from transformers import (
|
|
| 23 |
)
|
| 24 |
|
| 25 |
#################################################
|
| 26 |
-
#
|
| 27 |
#################################################
|
| 28 |
|
| 29 |
LOG_FILE = "access.log"
|
|
@@ -35,7 +35,7 @@ MAX_INPUT_LENGTH = 2000
|
|
| 35 |
CHECK_INTERVAL = 0.2
|
| 36 |
|
| 37 |
#################################################
|
| 38 |
-
# SQLite
|
| 39 |
#################################################
|
| 40 |
|
| 41 |
conn = sqlite3.connect("xss_detection.db")
|
|
@@ -64,10 +64,10 @@ CREATE TABLE IF NOT EXISTS detections (
|
|
| 64 |
conn.commit()
|
| 65 |
|
| 66 |
#################################################
|
| 67 |
-
#
|
| 68 |
#################################################
|
| 69 |
|
| 70 |
-
print("[+]
|
| 71 |
|
| 72 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 73 |
|
|
@@ -79,10 +79,10 @@ model.to(device)
|
|
| 79 |
|
| 80 |
model.eval()
|
| 81 |
|
| 82 |
-
print("[+]
|
| 83 |
|
| 84 |
#################################################
|
| 85 |
-
#
|
| 86 |
#################################################
|
| 87 |
|
| 88 |
labels = {
|
|
@@ -91,7 +91,7 @@ labels = {
|
|
| 91 |
}
|
| 92 |
|
| 93 |
#################################################
|
| 94 |
-
# URL
|
| 95 |
#################################################
|
| 96 |
|
| 97 |
def is_url(text):
|
|
@@ -103,7 +103,7 @@ def is_url(text):
|
|
| 103 |
)
|
| 104 |
|
| 105 |
#################################################
|
| 106 |
-
# Unicode
|
| 107 |
#################################################
|
| 108 |
|
| 109 |
def normalize_unicode(text):
|
|
@@ -111,7 +111,7 @@ def normalize_unicode(text):
|
|
| 111 |
return unicodedata.normalize("NFKC", text)
|
| 112 |
|
| 113 |
#################################################
|
| 114 |
-
# URL payload
|
| 115 |
#################################################
|
| 116 |
|
| 117 |
def extract_url_payload(url):
|
|
@@ -127,7 +127,7 @@ def extract_url_payload(url):
|
|
| 127 |
extracted = []
|
| 128 |
|
| 129 |
#################################################
|
| 130 |
-
# parameter value
|
| 131 |
#################################################
|
| 132 |
|
| 133 |
for key, values in params.items():
|
|
@@ -139,7 +139,7 @@ def extract_url_payload(url):
|
|
| 139 |
extracted.append(decoded)
|
| 140 |
|
| 141 |
#################################################
|
| 142 |
-
#
|
| 143 |
#################################################
|
| 144 |
|
| 145 |
if contains_suspicious_code(raw_query):
|
|
@@ -147,7 +147,7 @@ def extract_url_payload(url):
|
|
| 147 |
extracted.append(raw_query)
|
| 148 |
|
| 149 |
#################################################
|
| 150 |
-
#
|
| 151 |
#################################################
|
| 152 |
|
| 153 |
if not extracted:
|
|
@@ -161,7 +161,7 @@ def extract_url_payload(url):
|
|
| 161 |
return url
|
| 162 |
|
| 163 |
#################################################
|
| 164 |
-
# suspicious code
|
| 165 |
#################################################
|
| 166 |
|
| 167 |
def contains_suspicious_code(text):
|
|
@@ -179,7 +179,7 @@ def contains_suspicious_code(text):
|
|
| 179 |
"iframe",
|
| 180 |
"svg",
|
| 181 |
|
| 182 |
-
# JS
|
| 183 |
"eval(",
|
| 184 |
"alert(",
|
| 185 |
"prompt(",
|
|
@@ -188,7 +188,7 @@ def contains_suspicious_code(text):
|
|
| 188 |
"document.domain",
|
| 189 |
"window.location",
|
| 190 |
|
| 191 |
-
#
|
| 192 |
"constructor",
|
| 193 |
"fromcharcode",
|
| 194 |
"\\x",
|
|
@@ -198,7 +198,7 @@ def contains_suspicious_code(text):
|
|
| 198 |
"base64",
|
| 199 |
"atob(",
|
| 200 |
|
| 201 |
-
#
|
| 202 |
"srcdoc",
|
| 203 |
"data:text/html",
|
| 204 |
"vbscript:",
|
|
@@ -216,7 +216,7 @@ def contains_suspicious_code(text):
|
|
| 216 |
return False
|
| 217 |
|
| 218 |
#################################################
|
| 219 |
-
#
|
| 220 |
#################################################
|
| 221 |
|
| 222 |
def parse_log_line(line):
|
|
@@ -249,7 +249,7 @@ def parse_log_line(line):
|
|
| 249 |
return None, None
|
| 250 |
|
| 251 |
#################################################
|
| 252 |
-
# BERT
|
| 253 |
#################################################
|
| 254 |
|
| 255 |
def predict_xss(text):
|
|
@@ -281,7 +281,7 @@ def predict_xss(text):
|
|
| 281 |
return label, confidence
|
| 282 |
|
| 283 |
#################################################
|
| 284 |
-
#
|
| 285 |
#################################################
|
| 286 |
|
| 287 |
def follow(thefile):
|
|
@@ -301,10 +301,10 @@ def follow(thefile):
|
|
| 301 |
yield line
|
| 302 |
|
| 303 |
#################################################
|
| 304 |
-
#
|
| 305 |
#################################################
|
| 306 |
|
| 307 |
-
print(f"[+]
|
| 308 |
|
| 309 |
with open(LOG_FILE, "r", encoding="utf-8", errors="ignore") as logfile:
|
| 310 |
|
|
@@ -327,7 +327,7 @@ with open(LOG_FILE, "r", encoding="utf-8", errors="ignore") as logfile:
|
|
| 327 |
url = normalize_unicode(url)
|
| 328 |
|
| 329 |
#################################################
|
| 330 |
-
# URL payload
|
| 331 |
#################################################
|
| 332 |
|
| 333 |
if is_url(url):
|
|
@@ -339,7 +339,7 @@ with open(LOG_FILE, "r", encoding="utf-8", errors="ignore") as logfile:
|
|
| 339 |
target_text = url
|
| 340 |
|
| 341 |
#################################################
|
| 342 |
-
#
|
| 343 |
#################################################
|
| 344 |
|
| 345 |
if len(target_text) > MAX_INPUT_LENGTH:
|
|
@@ -347,7 +347,7 @@ with open(LOG_FILE, "r", encoding="utf-8", errors="ignore") as logfile:
|
|
| 347 |
continue
|
| 348 |
|
| 349 |
#################################################
|
| 350 |
-
# suspicious fragment
|
| 351 |
#################################################
|
| 352 |
|
| 353 |
if not contains_suspicious_code(target_text):
|
|
@@ -355,13 +355,13 @@ with open(LOG_FILE, "r", encoding="utf-8", errors="ignore") as logfile:
|
|
| 355 |
continue
|
| 356 |
|
| 357 |
#################################################
|
| 358 |
-
# ML
|
| 359 |
#################################################
|
| 360 |
|
| 361 |
label, confidence = predict_xss(target_text)
|
| 362 |
|
| 363 |
#################################################
|
| 364 |
-
# XSS
|
| 365 |
#################################################
|
| 366 |
|
| 367 |
if label == "XSS":
|
|
@@ -375,7 +375,7 @@ with open(LOG_FILE, "r", encoding="utf-8", errors="ignore") as logfile:
|
|
| 375 |
print("==============================\n")
|
| 376 |
|
| 377 |
#################################################
|
| 378 |
-
# DB
|
| 379 |
#################################################
|
| 380 |
|
| 381 |
cursor.execute("""
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
#################################################
|
| 26 |
+
# setting
|
| 27 |
#################################################
|
| 28 |
|
| 29 |
LOG_FILE = "access.log"
|
|
|
|
| 35 |
CHECK_INTERVAL = 0.2
|
| 36 |
|
| 37 |
#################################################
|
| 38 |
+
# SQLite
|
| 39 |
#################################################
|
| 40 |
|
| 41 |
conn = sqlite3.connect("xss_detection.db")
|
|
|
|
| 64 |
conn.commit()
|
| 65 |
|
| 66 |
#################################################
|
| 67 |
+
# model load
|
| 68 |
#################################################
|
| 69 |
|
| 70 |
+
print("[+] Loading Model...")
|
| 71 |
|
| 72 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 73 |
|
|
|
|
| 79 |
|
| 80 |
model.eval()
|
| 81 |
|
| 82 |
+
print("[+] Loading Model Completed.")
|
| 83 |
|
| 84 |
#################################################
|
| 85 |
+
# label
|
| 86 |
#################################################
|
| 87 |
|
| 88 |
labels = {
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
#################################################
|
| 94 |
+
# URL?
|
| 95 |
#################################################
|
| 96 |
|
| 97 |
def is_url(text):
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
#################################################
|
| 106 |
+
# Unicode
|
| 107 |
#################################################
|
| 108 |
|
| 109 |
def normalize_unicode(text):
|
|
|
|
| 111 |
return unicodedata.normalize("NFKC", text)
|
| 112 |
|
| 113 |
#################################################
|
| 114 |
+
# URL payload extract
|
| 115 |
#################################################
|
| 116 |
|
| 117 |
def extract_url_payload(url):
|
|
|
|
| 127 |
extracted = []
|
| 128 |
|
| 129 |
#################################################
|
| 130 |
+
# parameter value
|
| 131 |
#################################################
|
| 132 |
|
| 133 |
for key, values in params.items():
|
|
|
|
| 139 |
extracted.append(decoded)
|
| 140 |
|
| 141 |
#################################################
|
| 142 |
+
# Add when suspicious code exists in query itself
|
| 143 |
#################################################
|
| 144 |
|
| 145 |
if contains_suspicious_code(raw_query):
|
|
|
|
| 147 |
extracted.append(raw_query)
|
| 148 |
|
| 149 |
#################################################
|
| 150 |
+
# use path when parameter xde
|
| 151 |
#################################################
|
| 152 |
|
| 153 |
if not extracted:
|
|
|
|
| 161 |
return url
|
| 162 |
|
| 163 |
#################################################
|
| 164 |
+
# suspicious code?
|
| 165 |
#################################################
|
| 166 |
|
| 167 |
def contains_suspicious_code(text):
|
|
|
|
| 179 |
"iframe",
|
| 180 |
"svg",
|
| 181 |
|
| 182 |
+
# JS
|
| 183 |
"eval(",
|
| 184 |
"alert(",
|
| 185 |
"prompt(",
|
|
|
|
| 188 |
"document.domain",
|
| 189 |
"window.location",
|
| 190 |
|
| 191 |
+
# bypassing
|
| 192 |
"constructor",
|
| 193 |
"fromcharcode",
|
| 194 |
"\\x",
|
|
|
|
| 198 |
"base64",
|
| 199 |
"atob(",
|
| 200 |
|
| 201 |
+
#
|
| 202 |
"srcdoc",
|
| 203 |
"data:text/html",
|
| 204 |
"vbscript:",
|
|
|
|
| 216 |
return False
|
| 217 |
|
| 218 |
#################################################
|
| 219 |
+
# log parsing
|
| 220 |
#################################################
|
| 221 |
|
| 222 |
def parse_log_line(line):
|
|
|
|
| 249 |
return None, None
|
| 250 |
|
| 251 |
#################################################
|
| 252 |
+
# BERT
|
| 253 |
#################################################
|
| 254 |
|
| 255 |
def predict_xss(text):
|
|
|
|
| 281 |
return label, confidence
|
| 282 |
|
| 283 |
#################################################
|
| 284 |
+
# log
|
| 285 |
#################################################
|
| 286 |
|
| 287 |
def follow(thefile):
|
|
|
|
| 301 |
yield line
|
| 302 |
|
| 303 |
#################################################
|
| 304 |
+
# main
|
| 305 |
#################################################
|
| 306 |
|
| 307 |
+
print(f"[+] Start Monitoring Logs: {LOG_FILE}")
|
| 308 |
|
| 309 |
with open(LOG_FILE, "r", encoding="utf-8", errors="ignore") as logfile:
|
| 310 |
|
|
|
|
| 327 |
url = normalize_unicode(url)
|
| 328 |
|
| 329 |
#################################################
|
| 330 |
+
# URL payload
|
| 331 |
#################################################
|
| 332 |
|
| 333 |
if is_url(url):
|
|
|
|
| 339 |
target_text = url
|
| 340 |
|
| 341 |
#################################################
|
| 342 |
+
# length
|
| 343 |
#################################################
|
| 344 |
|
| 345 |
if len(target_text) > MAX_INPUT_LENGTH:
|
|
|
|
| 347 |
continue
|
| 348 |
|
| 349 |
#################################################
|
| 350 |
+
# skip when suspicious fragment no exist
|
| 351 |
#################################################
|
| 352 |
|
| 353 |
if not contains_suspicious_code(target_text):
|
|
|
|
| 355 |
continue
|
| 356 |
|
| 357 |
#################################################
|
| 358 |
+
# ML
|
| 359 |
#################################################
|
| 360 |
|
| 361 |
label, confidence = predict_xss(target_text)
|
| 362 |
|
| 363 |
#################################################
|
| 364 |
+
# XSS detected
|
| 365 |
#################################################
|
| 366 |
|
| 367 |
if label == "XSS":
|
|
|
|
| 375 |
print("==============================\n")
|
| 376 |
|
| 377 |
#################################################
|
| 378 |
+
# DB
|
| 379 |
#################################################
|
| 380 |
|
| 381 |
cursor.execute("""
|