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
Update inference_bert_url.py
Browse files- inference_bert_url.py +27 -27
inference_bert_url.py
CHANGED
|
@@ -10,20 +10,20 @@ from urllib.parse import (
|
|
| 10 |
)
|
| 11 |
|
| 12 |
#################################################
|
| 13 |
-
#
|
| 14 |
#################################################
|
| 15 |
|
| 16 |
model_path = "xss_detect_trained"
|
| 17 |
|
| 18 |
#################################################
|
| 19 |
-
# URL
|
| 20 |
#################################################
|
| 21 |
|
| 22 |
def is_url(text):
|
| 23 |
return text.startswith("http://") or text.startswith("https://")
|
| 24 |
|
| 25 |
#################################################
|
| 26 |
-
# URL에서 parameter value
|
| 27 |
#################################################
|
| 28 |
|
| 29 |
def extract_url_payload(url):
|
|
@@ -31,7 +31,7 @@ def extract_url_payload(url):
|
|
| 31 |
try:
|
| 32 |
parsed = urlparse(url)
|
| 33 |
|
| 34 |
-
# query parameter
|
| 35 |
params = parse_qs(parsed.query)
|
| 36 |
|
| 37 |
extracted = []
|
|
@@ -45,18 +45,18 @@ def extract_url_payload(url):
|
|
| 45 |
|
| 46 |
extracted.append(decoded)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
if not extracted:
|
| 50 |
return parsed.path
|
| 51 |
|
| 52 |
-
#
|
| 53 |
return " ".join(extracted)
|
| 54 |
|
| 55 |
except:
|
| 56 |
return url
|
| 57 |
|
| 58 |
#################################################
|
| 59 |
-
#
|
| 60 |
#################################################
|
| 61 |
|
| 62 |
def contains_suspicious_code(text):
|
|
@@ -74,7 +74,7 @@ def contains_suspicious_code(text):
|
|
| 74 |
"iframe",
|
| 75 |
"svg",
|
| 76 |
|
| 77 |
-
# JS
|
| 78 |
"eval(",
|
| 79 |
"alert(",
|
| 80 |
"prompt(",
|
|
@@ -83,7 +83,7 @@ def contains_suspicious_code(text):
|
|
| 83 |
"document.domain",
|
| 84 |
"window.location",
|
| 85 |
|
| 86 |
-
#
|
| 87 |
"constructor",
|
| 88 |
"fromcharcode",
|
| 89 |
"\\x",
|
|
@@ -93,7 +93,7 @@ def contains_suspicious_code(text):
|
|
| 93 |
"base64",
|
| 94 |
"atob(",
|
| 95 |
|
| 96 |
-
#
|
| 97 |
"srcdoc",
|
| 98 |
"data:text/html",
|
| 99 |
"vbscript:",
|
|
@@ -110,7 +110,7 @@ def contains_suspicious_code(text):
|
|
| 110 |
return False
|
| 111 |
|
| 112 |
#################################################
|
| 113 |
-
#
|
| 114 |
#################################################
|
| 115 |
|
| 116 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
@@ -122,7 +122,7 @@ model.to(device)
|
|
| 122 |
model.eval()
|
| 123 |
|
| 124 |
#################################################
|
| 125 |
-
#
|
| 126 |
#################################################
|
| 127 |
|
| 128 |
labels = {
|
|
@@ -131,53 +131,53 @@ labels = {
|
|
| 131 |
}
|
| 132 |
|
| 133 |
#################################################
|
| 134 |
-
#
|
| 135 |
#################################################
|
| 136 |
|
| 137 |
-
print("\n
|
| 138 |
|
| 139 |
while True:
|
| 140 |
|
| 141 |
-
text = input("
|
| 142 |
|
| 143 |
if text.lower() == "exit":
|
| 144 |
break
|
| 145 |
|
| 146 |
#################################################
|
| 147 |
-
#
|
| 148 |
#################################################
|
| 149 |
|
| 150 |
target_text = text
|
| 151 |
|
| 152 |
#################################################
|
| 153 |
-
# URL
|
| 154 |
#################################################
|
| 155 |
|
| 156 |
if is_url(text):
|
| 157 |
|
| 158 |
target_text = extract_url_payload(text)
|
| 159 |
|
| 160 |
-
print(f"[
|
| 161 |
|
| 162 |
#################################################
|
| 163 |
-
#
|
| 164 |
#################################################
|
| 165 |
|
| 166 |
if not contains_suspicious_code(target_text):
|
| 167 |
|
| 168 |
-
print("
|
| 169 |
-
print("
|
| 170 |
|
| 171 |
continue
|
| 172 |
|
| 173 |
#################################################
|
| 174 |
-
#
|
| 175 |
#################################################
|
| 176 |
MAX_INPUT_LENGTH = 2000
|
| 177 |
|
| 178 |
if len(target_text) > MAX_INPUT_LENGTH:
|
| 179 |
|
| 180 |
-
print("
|
| 181 |
|
| 182 |
continue
|
| 183 |
inputs = tokenizer(
|
|
@@ -189,7 +189,7 @@ while True:
|
|
| 189 |
).to(device)
|
| 190 |
|
| 191 |
#################################################
|
| 192 |
-
#
|
| 193 |
#################################################
|
| 194 |
|
| 195 |
with torch.no_grad():
|
|
@@ -208,8 +208,8 @@ while True:
|
|
| 208 |
label = labels[pred]
|
| 209 |
|
| 210 |
#################################################
|
| 211 |
-
#
|
| 212 |
#################################################
|
| 213 |
|
| 214 |
-
print(f"
|
| 215 |
-
print(f"
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
#################################################
|
| 13 |
+
# model path
|
| 14 |
#################################################
|
| 15 |
|
| 16 |
model_path = "xss_detect_trained"
|
| 17 |
|
| 18 |
#################################################
|
| 19 |
+
# URL existence
|
| 20 |
#################################################
|
| 21 |
|
| 22 |
def is_url(text):
|
| 23 |
return text.startswith("http://") or text.startswith("https://")
|
| 24 |
|
| 25 |
#################################################
|
| 26 |
+
# URL에서 parameter value
|
| 27 |
#################################################
|
| 28 |
|
| 29 |
def extract_url_payload(url):
|
|
|
|
| 31 |
try:
|
| 32 |
parsed = urlparse(url)
|
| 33 |
|
| 34 |
+
# query parameter
|
| 35 |
params = parse_qs(parsed.query)
|
| 36 |
|
| 37 |
extracted = []
|
|
|
|
| 45 |
|
| 46 |
extracted.append(decoded)
|
| 47 |
|
| 48 |
+
# use path when no parameter
|
| 49 |
if not extracted:
|
| 50 |
return parsed.path
|
| 51 |
|
| 52 |
+
# combine multiple parameters
|
| 53 |
return " ".join(extracted)
|
| 54 |
|
| 55 |
except:
|
| 56 |
return url
|
| 57 |
|
| 58 |
#################################################
|
| 59 |
+
# check
|
| 60 |
#################################################
|
| 61 |
|
| 62 |
def contains_suspicious_code(text):
|
|
|
|
| 74 |
"iframe",
|
| 75 |
"svg",
|
| 76 |
|
| 77 |
+
# JS
|
| 78 |
"eval(",
|
| 79 |
"alert(",
|
| 80 |
"prompt(",
|
|
|
|
| 83 |
"document.domain",
|
| 84 |
"window.location",
|
| 85 |
|
| 86 |
+
# bypass
|
| 87 |
"constructor",
|
| 88 |
"fromcharcode",
|
| 89 |
"\\x",
|
|
|
|
| 93 |
"base64",
|
| 94 |
"atob(",
|
| 95 |
|
| 96 |
+
#
|
| 97 |
"srcdoc",
|
| 98 |
"data:text/html",
|
| 99 |
"vbscript:",
|
|
|
|
| 110 |
return False
|
| 111 |
|
| 112 |
#################################################
|
| 113 |
+
# load
|
| 114 |
#################################################
|
| 115 |
|
| 116 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
|
|
| 122 |
model.eval()
|
| 123 |
|
| 124 |
#################################################
|
| 125 |
+
# label
|
| 126 |
#################################################
|
| 127 |
|
| 128 |
labels = {
|
|
|
|
| 131 |
}
|
| 132 |
|
| 133 |
#################################################
|
| 134 |
+
# test
|
| 135 |
#################################################
|
| 136 |
|
| 137 |
+
print("\n Test Start (type exit to end)\n")
|
| 138 |
|
| 139 |
while True:
|
| 140 |
|
| 141 |
+
text = input("input: ")
|
| 142 |
|
| 143 |
if text.lower() == "exit":
|
| 144 |
break
|
| 145 |
|
| 146 |
#################################################
|
| 147 |
+
# basic
|
| 148 |
#################################################
|
| 149 |
|
| 150 |
target_text = text
|
| 151 |
|
| 152 |
#################################################
|
| 153 |
+
# URL
|
| 154 |
#################################################
|
| 155 |
|
| 156 |
if is_url(text):
|
| 157 |
|
| 158 |
target_text = extract_url_payload(text)
|
| 159 |
|
| 160 |
+
print(f"[extracted parameter]: {target_text}")
|
| 161 |
|
| 162 |
#################################################
|
| 163 |
+
# NORMAL when no suspicious code
|
| 164 |
#################################################
|
| 165 |
|
| 166 |
if not contains_suspicious_code(target_text):
|
| 167 |
|
| 168 |
+
print("result: NORMAL")
|
| 169 |
+
print("Reliability: heuristic\n")
|
| 170 |
|
| 171 |
continue
|
| 172 |
|
| 173 |
#################################################
|
| 174 |
+
# tokenize
|
| 175 |
#################################################
|
| 176 |
MAX_INPUT_LENGTH = 2000
|
| 177 |
|
| 178 |
if len(target_text) > MAX_INPUT_LENGTH:
|
| 179 |
|
| 180 |
+
print("Input Length Exceeded\n")
|
| 181 |
|
| 182 |
continue
|
| 183 |
inputs = tokenizer(
|
|
|
|
| 189 |
).to(device)
|
| 190 |
|
| 191 |
#################################################
|
| 192 |
+
#
|
| 193 |
#################################################
|
| 194 |
|
| 195 |
with torch.no_grad():
|
|
|
|
| 208 |
label = labels[pred]
|
| 209 |
|
| 210 |
#################################################
|
| 211 |
+
# result
|
| 212 |
#################################################
|
| 213 |
|
| 214 |
+
print(f"result: {label}")
|
| 215 |
+
print(f"Reliability: {confidence:.4f}\n")
|