kd7979148 commited on
Commit
b3ee694
·
verified ·
1 Parent(s): 07435b3

Rename moniter.py to monitor.py

Browse files
Files changed (1) hide show
  1. 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
- # query 자체에 suspicious code 존재 추가
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
- # parameter 없으면 path 사용
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"[+] 로그 감시 시작: {LOG_FILE}")
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 없으면 skip
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("""