관제 모니터링 app.py 스크립트
from flask import Flask, render_template, jsonify, request
from elasticsearch import Elasticsearch
from datetime import datetime, timedelta, timezone
import ipaddress
import base64
import html
import re
from urllib.parse import unquote_plus
# ============================================================
# Flask 기본 설정
# ============================================================
app = Flask(__name__)
# ============================================================
# 설정
# ============================================================
DEFAULT_ES_HOST = "http://192.168.0.232:9200"
DEFAULT_ES_INDEX = "*,.*"
# 중요:
# 사이트 렉 방지를 위해 한 번에 처리하는 최대 로그 수를 제한
#
# 192.168.0.86은 실제 245건이므로 1000건이면 충분히 검색 가능
MAX_LOGS = 1000
# Elasticsearch 한 번에 가져오는 양
PAGE_SIZE = 500
# ============================================================
# 내부 인프라 / 작업자 대역
# ============================================================
TRUSTED_INFRA_IPS = [
"172.16.0.100",
"172.16.0.126",
"127.0.0.1",
]
ADMIN_WORKER_IPS = [
f"192.168.0.{i}"
for i in range(201, 220)
]
# ============================================================
# 행동 기반 탐지 IOC
# ============================================================
C2_PATH_MARKERS = [
"/check",
"/result",
]
C2_COMMAND_MARKERS = [
"shell_command",
]
C2_USER_AGENT_MARKERS = [
"python-urllib",
]
MALWARE_FILE_MARKERS = [
"v2.exe",
]
EXECUTABLE_MARKERS = [
".exe",
"application/x-msdownload",
"application/x-msdos-program",
"application/octet-stream",
]
PRIVATE_KEY_MARKERS = [
"ubuntukey.txt",
"id_rsa",
"id_ed25519",
"begin rsa private key",
"begin openssh private key",
"private key",
]
SCAN_MARKERS = [
"nmap",
"masscan",
"port scan",
]
DDOS_MARKERS = [
"ddos",
"flood",
"hping3",
]
BRUTE_FORCE_MARKERS = [
"hydra",
"gobuster",
]
SQLI_MARKERS = [
"union select",
" or 1=1",
"' or '1'='1",
"select * from",
"information_schema",
]
# 웹쉘/업로드/DB 파괴 방어 탐지 규칙
WEBSHELL_SCRIPT_EXTENSIONS = [".php", ".phtml", ".php3", ".php4", ".php5", ".phar", ".jsp", ".jspx", ".asp", ".aspx", ".cfm", ".cgi"]
WEBSHELL_EXECUTION_MARKERS = ["shell_command", "cmd=", "command=", "exec", "system", "passthru", "shell_exec", "popen", "proc_open", "runtime.getruntime", "processbuilder"]
WEBSHELL_UPLOAD_MARKERS = ["multipart/form-data", "content-disposition: form-data", "filename=", "upload", "fileupload", "attachment"]
WEB_ROOT_MARKERS = ["/upload/", "/uploads/", "/files/", "/media/", "/static/", "/public/", "/www/", "/htdocs/"]
DB_DESTRUCTIVE_MARKERS = ["drop database", "drop table", "truncate table", "delete from", "delete database", "database deleted", "db deleted", "schema deleted"]
WEB_REQUEST_METHODS = ["POST", "PUT", "PATCH"]
# ============================================================
# 유틸 함수
# ============================================================
def as_port(value):
try:
return int(value or 0)
except (TypeError, ValueError):
return 0
def get_nested_value(data, *keys):
current = data
for key in keys:
if not isinstance(current, dict):
return None
current = current.get(key)
return current
def contains_any(text, markers):
if not text:
return False
text = str(text).lower()
return any(
str(marker).lower() in text
for marker in markers
)
def safe_text(value):
if value is None:
return ""
return str(value)
# ============================================================
# Elasticsearch 원본 데이터 전체 문자열화
# ============================================================
def flatten_values(data):
values = []
if isinstance(data, dict):
for key, value in data.items():
values.append(str(key))
values.extend(
flatten_values(value)
)
elif isinstance(data, list):
for item in data:
values.extend(
flatten_values(item)
)
elif data is not None:
values.append(str(data))
return values
def source_matches_keyword(source, keyword):
if not keyword:
return True
keyword = str(keyword).strip().lower()
if not keyword:
return True
all_values = flatten_values(source)
for value in all_values:
if keyword in str(value).lower():
return True
return False
# ============================================================
# Payload 생성
# ============================================================
def event_payload(source):
if not isinstance(source, dict):
return str(source)
http = source.get("http", {})
if not isinstance(http, dict):
http = {}
request_data = http.get("request", {})
if not isinstance(request_data, dict):
request_data = {}
response_data = http.get("response", {})
if not isinstance(response_data, dict):
response_data = {}
event = source.get("event", {})
if not isinstance(event, dict):
event = {}
log = source.get("log", {})
if not isinstance(log, dict):
log = {}
url = source.get("url", {})
if not isinstance(url, dict):
url = {}
user_agent = source.get("user_agent", {})
if not isinstance(user_agent, dict):
user_agent = {}
request_body = request_data.get("body", {})
if not isinstance(request_body, dict):
request_body = {}
response_body = response_data.get("body", {})
if not isinstance(response_body, dict):
response_body = {}
values = [
source.get("message"),
event.get("original"),
event.get("action"),
log.get("original"),
url.get("full"),
url.get("original"),
url.get("path"),
user_agent.get("original"),
request_data.get("method"),
request_body.get("content"),
response_body.get("content"),
get_nested_value(
source,
"http",
"response",
"mime_type"
),
get_nested_value(
source,
"http",
"request",
"referrer"
),
]
payload = "\n".join(
str(value)
for value in values
if value not in (None, "")
)
if not payload:
payload = "\n".join(
flatten_values(source)
)
return payload[:20000]
# ============================================================
# IP 추출
# ============================================================
def get_client_ip(source):
if not isinstance(source, dict):
return ""
candidates = [
get_nested_value(
source,
"client",
"ip"
),
get_nested_value(
source,
"source",
"ip"
),
source.get("client_ip"),
source.get("src_ip"),
source.get("source_ip"),
]
for value in candidates:
if value not in (None, ""):
return str(value)
return ""
def get_server_ip(source):
if not isinstance(source, dict):
return ""
candidates = [
get_nested_value(
source,
"server",
"ip"
),
get_nested_value(
source,
"destination",
"ip"
),
source.get("server_ip"),
source.get("dst_ip"),
source.get("destination_ip"),
]
for value in candidates:
if value not in (None, ""):
return str(value)
return ""
def get_client_port(source):
candidates = [
get_nested_value(
source,
"client",
"port"
),
get_nested_value(
source,
"source",
"port"
),
source.get("client_port"),
source.get("src_port"),
]
for value in candidates:
port = as_port(value)
if port > 0:
return port
return 0
def get_server_port(source):
candidates = [
get_nested_value(
source,
"server",
"port"
),
get_nested_value(
source,
"destination",
"port"
),
source.get("server_port"),
source.get("dst_port"),
]
for value in candidates:
port = as_port(value)
if port > 0:
return port
return 0
# ============================================================
# 프로토콜 판단
# ============================================================
def get_protocol(
source,
raw_msg,
client_port,
server_port
):
raw_proto = str(
get_nested_value(
source,
"network",
"protocol"
)
or get_nested_value(
source,
"service",
"type"
)
or source.get("type")
or ""
).upper()
transport = str(
get_nested_value(
source,
"network",
"transport"
)
or ""
).upper()
is_plain_http = any(
marker in raw_msg
for marker in [
"http/1.",
"get /",
"post /",
"put /",
"host:",
]
)
if raw_proto in [
"FLOW",
"LOG",
"UNKNOWN",
"TCP",
"UDP",
"",
]:
if (
is_plain_http
or server_port in [
80,
8080,
8153,
8232,
8241,
8100,
]
or client_port in [
80,
8080,
8153,
8232,
8241,
8100,
]
):
return "HTTP"
if (
server_port in [
443,
4232,
8200,
]
or client_port in [
443,
4232,
8200,
]
):
return "HTTPS"
if (
server_port == 53
or client_port == 53
):
return "DNS"
if (
server_port in [
22,
22232,
22241,
]
or client_port in [
22,
22232,
22241,
]
):
return "SSH"
if (
server_port in [
21,
21232,
]
or client_port in [
21,
21232,
]
):
return "FTP"
if (
server_port in [
25,
465,
587,
]
or client_port in [
25,
465,
587,
]
):
return "SMTP"
if (
server_port in [
143,
993,
]
or client_port in [
143,
993,
]
):
return "IMAP"
if transport == "UDP":
return "UDP"
if transport == "TCP":
return "TCP"
return "LOG"
return raw_proto
# ============================================================
# Bytes 추출
# ============================================================
def get_bytes_value(source):
candidates = [
get_nested_value(
source,
"source",
"bytes"
),
get_nested_value(
source,
"destination",
"bytes"
),
get_nested_value(
source,
"network",
"bytes"
),
source.get("bytes"),
]
for value in candidates:
try:
if value is not None:
return int(value)
except (
TypeError,
ValueError,
):
continue
return 1024
# ============================================================
# Geo 표시
# ============================================================
def get_geo_country(ip_value):
try:
ip_obj = ipaddress.ip_address(
str(ip_value)
)
if ip_obj.is_private:
return "사설망 (Local Network)"
first_octet = int(
str(ip_value).split(".")[0]
)
if first_octet % 4 == 0:
return "미국 (US)"
elif first_octet % 4 == 1:
return "중국 (CN)"
elif first_octet % 4 == 2:
return "독일 (DE)"
return "러시아 (RU)"
except Exception:
return "사설망 (Local Network)"
# ============================================================
# 시간 변환
# ============================================================
def parse_timestamp_to_kst(timestamp_value):
if not timestamp_value:
return None
try:
timestamp_text = str(
timestamp_value
).replace(
"Z",
"+00:00"
)
dt = datetime.fromisoformat(
timestamp_text
)
if dt.tzinfo is None:
dt = dt.replace(
tzinfo=timezone.utc
)
kst = timezone(
timedelta(hours=9)
)
return dt.astimezone(kst)
except Exception:
return None
# ============================================================
# 웹쉘 방어형 정규화 / 다중 신호 분석
# ============================================================
def normalize_security_text(value, rounds=3):
text = safe_text(value)
for _ in range(rounds):
previous = text
try: text = unquote_plus(text)
except Exception: pass
try: text = html.unescape(text)
except Exception: pass
if text == previous: break
text = re.sub(r"[\u0000-\u001f\u007f]+", " ", text)
return re.sub(r"\s+", " ", text).strip().lower()
def get_http_request(source):
http = source.get("http", {}) if isinstance(source, dict) else {}
if not isinstance(http, dict): http = {}
req = http.get("request", {})
resp = http.get("response", {})
if not isinstance(req, dict): req = {}
if not isinstance(resp, dict): resp = {}
return req, resp
def get_url_path(source):
url = source.get("url", {}) if isinstance(source, dict) else {}
if not isinstance(url, dict): url = {}
return safe_text(url.get("path") or url.get("original") or url.get("full") or source.get("request_uri") or source.get("url_path"))
def extract_candidate_filenames(source, raw_text):
names=[]
req,_=get_http_request(source)
for item in [req.get("body"), req.get("headers"), source.get("file"), source.get("filename"), source.get("file_name"), source.get("path")]:
for value in flatten_values(item):
if any(ext in value.lower() for ext in WEBSHELL_SCRIPT_EXTENSIONS): names.append(value)
names.extend(re.findall(r"filename\s*=\s*[\"']?([^\"' ;\r\n]+)", raw_text, re.I))
return list(dict.fromkeys(names))[:20]
def analyze_webshell_indicators(source, raw_payload, proto, client_ip, server_ip, context=None):
context=context or {}
normalized=normalize_security_text(raw_payload)
req,resp=get_http_request(source)
method=normalize_security_text(req.get("method") or source.get("http_method") or source.get("method")).upper()
path=normalize_security_text(get_url_path(source))
ctype=normalize_security_text(req.get("mime_type") or req.get("content_type") or get_nested_value(source,"http","request","headers","content-type") or get_nested_value(source,"http","request","headers","Content-Type"))
response_code=safe_text(resp.get("status_code") or resp.get("status") or source.get("http_status"))
ua=normalize_security_text(get_nested_value(source,"user_agent","original") or source.get("user_agent"))
filenames=extract_candidate_filenames(source, normalized)
script_name=any(any(ext in n.lower() for ext in WEBSHELL_SCRIPT_EXTENSIONS) for n in filenames)
upload_context=method in WEB_REQUEST_METHODS and ("multipart/form-data" in ctype or "filename=" in normalized or any(m in normalized for m in WEBSHELL_UPLOAD_MARKERS))
script_path_context=any(path.endswith(ext) for ext in WEBSHELL_SCRIPT_EXTENSIONS)
web_root_context=any(m in path for m in WEB_ROOT_MARKERS)
exec_hits=[m for m in WEBSHELL_EXECUTION_MARKERS if m in normalized]
db_hits=[m for m in DB_DESTRUCTIVE_MARKERS if m in normalized]
score=0; reasons=[]; stage="none"
if upload_context: score+=2; reasons.append("웹 파일 업로드 요청 문맥"); stage="upload"
if script_name: score+=4; reasons.append("서버측 스크립트 파일명/확장자 정황")
if script_path_context: score+=1; reasons.append("웹 요청 경로의 서버측 스크립트 확장자(보조 신호)")
if web_root_context and script_name: score+=1; reasons.append("웹 서비스 파일 경로와 스크립트 파일 정황 결합")
if exec_hits: score+=min(6,len(exec_hits)*3); reasons.append("명령 실행 관련 행위 정황"); stage="execution"
if db_hits: score+=5; reasons.append("데이터베이스 변경/삭제 관련 정황"); stage="db_destructive"
prior=context.get("prior_webshell_by_ip",0); prior_uploads=context.get("prior_uploads_by_ip",0)
if prior>=1 and (script_name or script_path_context or exec_hits): score+=3; reasons.append(f"동일 출발지 선행 의심 이벤트 {prior}건 연계")
if prior_uploads>=1 and (script_path_context or exec_hits): score+=2; reasons.append("동일 출발지 선행 업로드와 후속 접근 연계")
strong=(script_name and (upload_context or exec_hits)) or (script_path_context and exec_hits) or (prior>=1 and script_path_context and exec_hits)
db_chain=bool(db_hits and (strong or prior>=1))
is_web=bool(strong or db_chain or (score>=8 and (script_name or exec_hits)))
confidence=min(99,35+score*7) if is_web else min(70,score*8)
return {"is_webshell":is_web,"is_db_destructive":bool(db_hits),"score":score,"confidence":confidence,"stage":stage,"reasons":reasons[:8],"method":method,"path":path,"filename":filenames[0] if filenames else "","response_code":response_code,"user_agent":ua,"execution_markers":exec_hits[:8],"db_markers":db_hits[:8],"client_ip":client_ip,"server_ip":server_ip}
# ============================================================
# 행동 기반 탐지 엔진
# ============================================================
def detect_behavior(
source,
client_ip,
server_ip,
client_port,
server_port,
proto,
raw_msg,
flow_cnt,
unique_ports_touched,
fail_cnt,
webshell_context=None,
):
result = {
"is_malicious": False,
"severity": "LOW",
"attack_type": "None",
"action": "정상 트래픽 허용 (Allow)",
"msg": "",
"incident": None,
}
# ========================================================
# 1. RSA / SSH 개인키
# ========================================================
has_private_key_marker = contains_any(
raw_msg,
PRIVATE_KEY_MARKERS
)
if has_private_key_marker:
exfil_context = any(
marker in raw_msg
for marker in [
"/result",
"post /",
"shell_command",
"begin rsa private key",
"begin openssh private key",
]
)
if exfil_context:
result.update({
"is_malicious": True,
"severity": "CRITICAL",
"attack_type": (
"RSA Private Key Exfiltration "
"(개인키 유출 정황)"
),
"action": (
"유출 의심 키 즉시 폐기·교체 및 "
"관련 외부 통신 차단"
),
"msg": (
"[긴급] RSA/SSH 개인키 데이터가 "
"외부 전송 흐름에서 탐지됨"
),
"incident": "key_exfiltration",
})
return result
result.update({
"is_malicious": True,
"severity": "HIGH",
"attack_type": (
"Sensitive Key File Access "
"(민감 키 파일 접근)"
),
"action": (
"해당 단말 조사 및 "
"키 파일 접근 이력 확인"
),
"msg": (
"[위험] RSA/SSH 개인키 또는 "
"민감 키 파일 접근 정황 탐지"
),
"incident": "key_exfiltration",
})
return result
# ========================================================
# 2. C2
# ========================================================
c2_path_count = sum(
1
for marker in C2_PATH_MARKERS
if marker in raw_msg
)
has_c2_path = c2_path_count > 0
has_shell_command = contains_any(
raw_msg,
C2_COMMAND_MARKERS
)
has_python_urllib = contains_any(
raw_msg,
C2_USER_AGENT_MARKERS
)
c2_score = 0
if has_c2_path:
c2_score += 1
if has_shell_command:
c2_score += 2
if has_python_urllib:
c2_score += 1
if c2_score >= 2:
result.update({
"is_malicious": True,
"severity": "CRITICAL",
"attack_type": (
"C2 Command & Control "
"(명령제어 서버 통신)"
),
"action": (
"의심 통신 즉시 차단 및 "
"통신 발생 단말 격리"
),
"msg": (
"[긴급] C2 명령제어 통신 패턴 탐지 "
f"(C2 지표 점수: {c2_score})"
),
"incident": "c2",
})
return result
if has_shell_command:
result.update({
"is_malicious": True,
"severity": "CRITICAL",
"attack_type": (
"Remote Shell Command "
"(원격 명령 실행)"
),
"action": (
"해당 세션 차단 및 "
"피해 단말 포렌식 조사"
),
"msg": (
"[긴급] 네트워크 트래픽에서 "
"원격 shell_command 패턴 탐지"
),
"incident": "c2",
})
return result
# ========================================================
# 3. 악성 실행파일
# ========================================================
has_v2 = contains_any(
raw_msg,
MALWARE_FILE_MARKERS
)
has_executable = contains_any(
raw_msg,
EXECUTABLE_MARKERS
)
has_mz_header = (
"\nmz" in raw_msg
or raw_msg.startswith("mz")
)
if has_v2:
result.update({
"is_malicious": True,
"severity": "HIGH",
"attack_type": (
"Malware Delivery "
"(V2.exe 실행파일 유포)"
),
"action": (
"다운로드 파일 격리, "
"실행 이력 확인 및 단말 검사"
),
"msg": (
"[위험] V2.exe 실행파일 "
"다운로드 패턴 탐지"
),
"incident": "malware_delivery",
})
return result
if has_executable and (
has_mz_header
or "get /" in raw_msg
or "download" in raw_msg
):
result.update({
"is_malicious": True,
"severity": "HIGH",
"attack_type": (
"Suspicious Executable Delivery "
"(의심 실행파일 유포)"
),
"action": (
"실행파일 다운로드 차단 및 "
"파일 해시 조사"
),
"msg": (
"[위험] HTTP 트래픽에서 "
"실행파일 전달 패턴 탐지"
),
"incident": "malware_delivery",
})
return result
# ========================================================
# 4. 피싱
# ========================================================
if proto in ["SMTP", "IMAP"]:
phishing_score = 0
if "v2.exe" in raw_msg:
phishing_score += 2
if "malware" in raw_msg:
phishing_score += 1
if "reverse_tcp" in raw_msg:
phishing_score += 2
if "phishing" in raw_msg:
phishing_score += 1
if phishing_score >= 2:
result.update({
"is_malicious": True,
"severity": "HIGH",
"attack_type": (
"Spear Phishing "
"(악성코드 첨부 피싱)"
),
"action": (
"동일 메일 격리 및 "
"수신자·첨부파일 조사"
),
"msg": (
"[위험] 메일 트래픽에서 "
"악성 첨부파일 패턴 탐지"
),
"incident": "phishing",
})
return result
# ========================================================
# 5. Port Scan
# ========================================================
if (
contains_any(
raw_msg,
SCAN_MARKERS
)
or unique_ports_touched >= 8
):
result.update({
"is_malicious": True,
"severity": "MEDIUM",
"attack_type": (
"Port Scanning "
"(네트워크 취약점 탐색)"
),
"action": (
"출발지 추적 및 "
"방화벽 탐지 로그 기록"
),
"msg": (
"[경고] 다수 포트 탐색 행위 감지 "
f"(대상 포트: "
f"{unique_ports_touched}개)"
),
})
return result
# ========================================================
# 6. DDoS
# ========================================================
if (
contains_any(
raw_msg,
DDOS_MARKERS
)
or flow_cnt >= 100
):
result.update({
"is_malicious": True,
"severity": "HIGH",
"attack_type": (
"DDoS Attack "
"(서비스 거부 공격)"
),
"action": (
"임계치 기반 트래픽 제한 및 "
"Null Routing 검토"
),
"msg": (
"[위험] 대규모 트래픽 플러딩 의심 "
f"(발생 세션: {flow_cnt}개)"
),
})
return result
# ========================================================
# 7. Web Brute Force
# ========================================================
has_login_failure = any(
marker in raw_msg
for marker in [
"401",
"failed",
"invalid",
"incorrect",
]
)
if (
contains_any(
raw_msg,
BRUTE_FORCE_MARKERS
)
or (
proto in [
"HTTP",
"HTTPS",
]
and fail_cnt >= 5
and has_login_failure
)
):
result.update({
"is_malicious": True,
"severity": "HIGH",
"attack_type": (
"Web Brute Force "
"(웹 무차별 대입 공격)"
),
"action": (
"출발지 임시 차단 및 "
"인증 로그 조사"
),
"msg": (
"[위험] 웹 로그인 무차별 대입 의심 "
f"(실패 누적: {fail_cnt}회)"
),
})
return result
# ========================================================
# 8. SSH Brute Force
# ========================================================
if (
proto == "SSH"
and fail_cnt >= 3
and has_login_failure
):
result.update({
"is_malicious": True,
"severity": "HIGH",
"attack_type": (
"SSH Brute Force "
"(SSH 무차별 대입)"
),
"action": (
"출발지 차단 및 "
"SSH 인증 로그 조사"
),
"msg": (
"[위험] SSH 로그인 무차별 대입 의심 "
f"(실패 누적: {fail_cnt}회)"
),
})
return result
# ========================================================
# 9. 웹쉘 / 업로드 / DB 파괴 행위
# ========================================================
webshell = analyze_webshell_indicators(source, raw_msg, proto, client_ip, server_ip, webshell_context)
if webshell["is_webshell"]:
result.update({
"is_malicious": True,
"severity": "CRITICAL",
"attack_type": ("Web Shell → Database Destruction (웹쉘 연계 DB 파괴)" if webshell["is_db_destructive"] else "Web Shell Upload / Execution (웹쉘 업로드·실행 의심)"),
"action": ("피해 웹서비스 세션 격리, 의심 파일 보존, DB 복구 절차 및 증적 보존 수행" if webshell["is_db_destructive"] else "웹서버 격리 검토, 의심 파일 보존 및 웹 접근 로그·프로세스·파일 생성 이력 조사"),
"msg": (f"[긴급] 웹쉘 의심 행위와 DB 변경/삭제 정황이 연계됨 (신뢰도 {webshell['confidence']}%)" if webshell["is_db_destructive"] else f"[긴급] 웹쉘 업로드/실행 다중 신호 탐지 (신뢰도 {webshell['confidence']}%)"),
"incident": "webshell_db_destruction" if webshell["is_db_destructive"] else "webshell",
})
result["webshell"] = webshell
return result
if webshell["score"] >= 3:
result.update({
"is_malicious": True,
"severity": "MEDIUM",
"attack_type": "Suspicious Web Upload / Web Shell Precursor (의심 웹 업로드 전조)",
"action": "업로드 파일·확장자·MIME 및 후속 접근 여부 조사",
"msg": f"[경고] 웹쉘 전조로 볼 수 있는 업로드/접근 정황 탐지 (점수 {webshell['score']})",
"incident": "webshell_precursor",
})
result["webshell"] = webshell
return result
# ========================================================
# 10. SQL Injection
# ========================================================
if contains_any(
raw_msg,
SQLI_MARKERS
):
result.update({
"is_malicious": True,
"severity": "HIGH",
"attack_type": (
"SQL Injection / Web Exploit "
"(웹 취약점 공격)"
),
"action": (
"웹 방화벽 차단 및 "
"웹 서버 로그 조사"
),
"msg": (
"[위험] SQL Injection 또는 "
"웹 취약점 공격 패턴 탐지"
),
})
return result
return result
# ============================================================
# Elasticsearch 검색 Query 생성
# ============================================================
def build_es_query(
start_str,
end_str,
search_keyword=""
):
filters = [
{
"range": {
"@timestamp": {
"gte": start_str,
"lte": end_str,
}
}
}
]
# --------------------------------------------------------
# 검색어가 없는 경우
# --------------------------------------------------------
if not search_keyword:
return {
"bool": {
"filter": filters
}
}
keyword = search_keyword.strip()
# --------------------------------------------------------
# IP / 일반 검색
#
# client.ip / source.ip 뿐 아니라
# 다른 ECS 필드도 검색
# --------------------------------------------------------
should_queries = [
{
"term": {
"client.ip": keyword
}
},
{
"term": {
"source.ip": keyword
}
},
{
"term": {
"src_ip": keyword
}
},
{
"term": {
"source_ip": keyword
}
},
{
"term": {
"client_ip": keyword
}
},
{
"term": {
"destination.ip": keyword
}
},
{
"term": {
"server.ip": keyword
}
},
{
"term": {
"dst_ip": keyword
}
},
{
"term": {
"destination_ip": keyword
}
},
{
"multi_match": {
"query": keyword,
"fields": [
"*"
],
"type": "phrase",
}
},
]
return {
"bool": {
"filter": filters,
"should": should_queries,
"minimum_should_match": 1,
}
}
# ============================================================
# Elasticsearch 로그 가져오기
#
# 중요:
#
# 기존:
# 최대 50,000건
#
# 변경:
# 최대 1,000건
#
# 사이트 렉 감소 목적
# ============================================================
def fetch_elasticsearch_logs(
es,
es_index,
start_str,
end_str,
search_keyword="",
max_logs=MAX_LOGS
):
all_hits = []
search_after = None
page_size = min(
PAGE_SIZE,
max_logs
)
query = build_es_query(
start_str=start_str,
end_str=end_str,
search_keyword=search_keyword
)
while len(all_hits) < max_logs:
remaining = max_logs - len(all_hits)
current_size = min(
page_size,
remaining
)
body = {
"size": current_size,
"track_total_hits": True,
"sort": [
{
"@timestamp": {
"order": "desc",
"unmapped_type": "date",
}
},
{
"_shard_doc": {
"order": "desc"
}
},
],
"_source": True,
"query": query,
}
if search_after:
body["search_after"] = search_after
try:
response = es.search(
index=es_index,
body=body,
expand_wildcards="all",
allow_no_indices=True,
ignore_unavailable=True,
)
except TypeError:
response = es.search(
index=es_index,
body=body,
)
hits = (
response.get(
"hits",
{}
).get(
"hits",
[]
)
)
if not hits:
break
all_hits.extend(hits)
last_hit = hits[-1]
search_after = last_hit.get(
"sort"
)
if not search_after:
break
if len(hits) < current_size:
break
return all_hits
# ============================================================
# 메인 화면
# ============================================================
@app.route("/")
def index():
return render_template(
"index.html"
)
# ============================================================
# 대시보드 API
# ============================================================
@app.route("/api/dashboard-data")
def dashboard_data():
try:
# ====================================================
# 요청 파라미터
# ====================================================
es_host = request.args.get(
"host",
DEFAULT_ES_HOST
).strip()
es_index = request.args.get(
"index",
DEFAULT_ES_INDEX
).strip()
if not es_index:
es_index = DEFAULT_ES_INDEX
target_date_str = request.args.get(
"date",
""
).strip()
target_hour_str = request.args.get(
"hour",
""
).strip()
search_keyword = request.args.get(
"search",
""
).strip()
# ====================================================
# Elasticsearch 연결
# ====================================================
es = Elasticsearch(
es_host,
headers={
"Accept": (
"application/vnd.elasticsearch+json;"
" compatible-with=8"
),
"Content-Type": (
"application/vnd.elasticsearch+json;"
" compatible-with=8"
),
},
request_timeout=30,
)
# ====================================================
# KST
# ====================================================
kst_tz = timezone(
timedelta(hours=9)
)
# ====================================================
# 날짜
# ====================================================
if target_date_str:
try:
base_date = datetime.strptime(
target_date_str,
"%Y-%m-%d"
)
except Exception:
base_date = datetime.now(
kst_tz
).replace(
tzinfo=None
)
else:
base_date = datetime.now(
kst_tz
).replace(
tzinfo=None
)
# ====================================================
# 시간 범위
# ====================================================
if (
target_hour_str
and target_hour_str.isdigit()
):
hour_value = int(
target_hour_str
)
hour_value = max(
0,
min(
23,
hour_value
)
)
kst_start = base_date.replace(
hour=hour_value,
minute=0,
second=0,
microsecond=0,
tzinfo=kst_tz,
)
kst_end = base_date.replace(
hour=hour_value,
minute=59,
second=59,
microsecond=999999,
tzinfo=kst_tz,
)
else:
kst_start = base_date.replace(
hour=0,
minute=0,
second=0,
microsecond=0,
tzinfo=kst_tz,
)
kst_end = base_date.replace(
hour=23,
minute=59,
second=59,
microsecond=999999,
tzinfo=kst_tz,
)
# ====================================================
# KST → UTC
# ====================================================
utc_start = kst_start.astimezone(
timezone.utc
)
utc_end = kst_end.astimezone(
timezone.utc
)
start_str = utc_start.strftime(
"%Y-%m-%dT%H:%M:%S.000Z"
)
end_str = utc_end.strftime(
"%Y-%m-%dT%H:%M:%S.999Z"
)
# ====================================================
# 현재 KST 시간
# ====================================================
now_kst = datetime.now(
kst_tz
)
current_hour_str = (
f"{now_kst.hour:02d}:00"
)
# ====================================================
# Elasticsearch 검색
# ====================================================
es_hits = fetch_elasticsearch_logs(
es=es,
es_index=es_index,
start_str=start_str,
end_str=end_str,
search_keyword=search_keyword,
max_logs=MAX_LOGS,
)
es_returned_count = len(
es_hits
)
# ====================================================
# Python 보조 검색
#
# Elasticsearch 검색 결과가 너무 넓게 잡힌 경우
# 실제 source에도 검색어가 있는지 최종 확인
# ====================================================
filtered_hits = []
for hit in es_hits:
source = hit.get(
"_source",
{}
)
if source_matches_keyword(
source,
search_keyword
):
filtered_hits.append(
hit
)
hits = filtered_hits
# ====================================================
# 시간별 트래픽
# ====================================================
hours_active = [
f"{hour:02d}:00"
for hour in range(24)
]
traffic_dict = {
hour: 0
for hour in hours_active
}
for hit in hits:
source = hit.get(
"_source",
{}
)
timestamp_value = (
source.get("@timestamp")
or source.get("timestamp")
)
dt_kst = parse_timestamp_to_kst(
timestamp_value
)
if dt_kst:
hour_key = (
f"{dt_kst.hour:02d}:00"
)
if hour_key in traffic_dict:
traffic_dict[
hour_key
] += 1
traffic_data = list(
traffic_dict.values()
)
max_traffic = (
max(traffic_data)
if traffic_data
else 0
)
if max_traffic > 0:
peak_hour = hours_active[
traffic_data.index(
max_traffic
)
]
else:
peak_hour = "00:00"
# ====================================================
# 1차 분석용 통계
# ====================================================
port_scan_tracker = {}
ip_flow_count = {}
auth_fail_tracker = {}
webshell_seen_by_ip = {}
upload_seen_by_ip = {}
webshell_paths_by_ip = {}
for hit in hits:
source = hit.get(
"_source",
{}
)
client_ip = get_client_ip(
source
)
server_port = get_server_port(
source
)
raw_payload = event_payload(
source
)
raw_m = raw_payload.lower()
normalized_probe = normalize_security_text(raw_payload)
probe_path = normalize_security_text(get_url_path(source))
probe_script = any(ext in normalized_probe or probe_path.endswith(ext) for ext in WEBSHELL_SCRIPT_EXTENSIONS)
probe_upload = any(marker in normalized_probe for marker in WEBSHELL_UPLOAD_MARKERS)
if probe_script:
webshell_seen_by_ip[client_ip] = webshell_seen_by_ip.get(client_ip, 0) + 1
webshell_paths_by_ip.setdefault(client_ip, set()).add(probe_path)
if probe_upload:
upload_seen_by_ip[client_ip] = upload_seen_by_ip.get(client_ip, 0) + 1
if not client_ip:
continue
ip_flow_count[
client_ip
] = (
ip_flow_count.get(
client_ip,
0
)
+ 1
)
if server_port > 0:
if client_ip not in port_scan_tracker:
port_scan_tracker[
client_ip
] = set()
port_scan_tracker[
client_ip
].add(
server_port
)
if any(
marker in raw_m
for marker in [
"401",
"failed",
"invalid",
"incorrect",
]
):
auth_fail_tracker[
client_ip
] = (
auth_fail_tracker.get(
client_ip,
0
)
+ 1
)
# ====================================================
# 로그 생성
# ====================================================
logs = []
has_high_threat = False
incident_state = {
"c2": False,
"key_exfiltration": False,
"malware_delivery": False,
"ssh_movement": False,
"phishing": False,
"webshell": False,
"webshell_precursor": False,
"webshell_db_destruction": False,
}
ip_bytes_map = {}
country_counts = {
"대한민국 (KR)": 0,
"미국 (US)": 0,
"중국 (CN)": 0,
"독일 (DE)": 0,
"러시아 (RU)": 0,
"사설망 (Local Network)": 0,
}
protocol_counts = {}
# ====================================================
# 각 로그 분석
# ====================================================
for hit in hits:
source = hit.get(
"_source",
{}
)
# ------------------------------------------------
# 시간
# ------------------------------------------------
timestamp_raw = (
source.get("@timestamp")
or source.get("timestamp")
or ""
)
dt_kst = parse_timestamp_to_kst(
timestamp_raw
)
if dt_kst:
time_str = dt_kst.strftime(
"%H:%M:%S"
)
full_time_str = dt_kst.strftime(
"%Y-%m-%d %H:%M:%S"
)
else:
time_str = str(
timestamp_raw
)
full_time_str = time_str
# ------------------------------------------------
# IP
# ------------------------------------------------
client_ip = get_client_ip(
source
)
server_ip = get_server_ip(
source
)
client_port = get_client_port(
source
)
server_port = get_server_port(
source
)
if not client_ip:
client_ip = "Unknown"
if not server_ip:
server_ip = "Unknown"
# ------------------------------------------------
# Payload
# ------------------------------------------------
full_payload = event_payload(
source
)
raw_msg = full_payload.lower()
# ------------------------------------------------
# Protocol
# ------------------------------------------------
proto = get_protocol(
source,
raw_msg,
client_port,
server_port,
)
# ------------------------------------------------
# Bytes
# ------------------------------------------------
bytes_val = get_bytes_value(
source
)
ip_bytes_map[
client_ip
] = (
ip_bytes_map.get(
client_ip,
0
)
+ bytes_val
)
# ------------------------------------------------
# Geo
# ------------------------------------------------
geo_country = get_geo_country(
client_ip
)
if geo_country not in country_counts:
country_counts[
geo_country
] = 0
country_counts[
geo_country
] += 1
# ------------------------------------------------
# Protocol count
# ------------------------------------------------
protocol_counts[
proto
] = (
protocol_counts.get(
proto,
0
)
+ 1
)
# =================================================
# 분석 수치
# =================================================
unique_ports_touched = len(
port_scan_tracker.get(
client_ip,
set()
)
)
flow_cnt = ip_flow_count.get(
client_ip,
0
)
fail_cnt = auth_fail_tracker.get(
client_ip,
0
)
# =================================================
# 행동 기반 탐지
# =================================================
detection = detect_behavior(
source=source,
client_ip=client_ip,
server_ip=server_ip,
client_port=client_port,
server_port=server_port,
proto=proto,
raw_msg=raw_msg,
flow_cnt=flow_cnt,
unique_ports_touched=(
unique_ports_touched
),
fail_cnt=fail_cnt,
webshell_context={
"prior_webshell_by_ip": max(0, webshell_seen_by_ip.get(client_ip, 0) - 1),
"prior_uploads_by_ip": max(0, upload_seen_by_ip.get(client_ip, 0) - 1),
"known_paths": list(webshell_paths_by_ip.get(client_ip, set()))[:10],
},
)
# =================================================
# 웹쉘 분석 결과 확보
# =================================================
webshell_meta = detection.get(
"webshell"
)
if not isinstance(
webshell_meta,
dict
):
webshell_meta = analyze_webshell_indicators(
source=source,
raw_payload=full_payload,
proto=proto,
client_ip=client_ip,
server_ip=server_ip,
context={
"prior_webshell_by_ip": max(
0,
webshell_seen_by_ip.get(
client_ip,
0
) - 1
),
"prior_uploads_by_ip": max(
0,
upload_seen_by_ip.get(
client_ip,
0
) - 1
),
"known_paths": list(
webshell_paths_by_ip.get(
client_ip,
set()
)
)[:10],
},
)
is_malicious = detection[
"is_malicious"
]
severity = detection[
"severity"
]
attack_type = detection[
"attack_type"
]
action = detection[
"action"
]
msg = detection[
"msg"
]
incident = detection[
"incident"
]
# =================================================
# 정상 트래픽
# =================================================
if not is_malicious:
# --------------------------------------------
# 신뢰 인프라
# --------------------------------------------
if client_ip in TRUSTED_INFRA_IPS:
severity = "LOW"
attack_type = "None"
action = (
"정상 인프라 통신 허용 "
"(Allow)"
)
display_msg = (
source.get(
"message"
)
or get_nested_value(
source,
"event",
"action"
)
)
if (
not display_msg
or "flow"
in str(
display_msg
).lower()
):
msg = (
"[내부망 정상 통신] "
"인프라 장비 상호 연결 유지"
)
else:
msg = str(
display_msg
)
# --------------------------------------------
# 관리자
# --------------------------------------------
elif client_ip in ADMIN_WORKER_IPS:
severity = "LOW"
attack_type = (
"Authorized Admin Access"
)
action = (
"인가된 관리자 접속 허용 "
"(Allow)"
)
msg = (
"[작업자 정상 접속] "
f"인가된 관리자 "
f"{proto} 세션 통신"
)
# --------------------------------------------
# 일반 정상
# --------------------------------------------
else:
display_msg = (
source.get(
"message"
)
or get_nested_value(
source,
"event",
"action"
)
)
if proto in [
"SMTP",
"IMAP",
"FTP",
"DNS",
]:
msg = (
f"Standard "
f"{proto} "
f"Service Traffic"
)
attack_type = (
"Standard Protocol Flow"
)
action = (
"정상 프로토콜 세션 허용 "
"(Allow)"
)
elif display_msg:
msg = str(
display_msg
)
else:
msg = (
f"Standard "
f"{proto} "
f"Traffic Flow"
)
# =================================================
# Incident
# =================================================
if incident:
if incident in incident_state:
incident_state[
incident
] = True
if severity in [
"HIGH",
"CRITICAL",
]:
has_high_threat = True
# =================================================
# 화면 로그
# =================================================
logs.append({
"time": time_str,
"datetime": full_time_str,
"src": (
f"{client_ip}:"
f"{client_port}"
),
"dst": (
f"{server_ip}:"
f"{server_port}"
),
"proto": proto,
"len": bytes_val,
"info": msg,
"sev": severity,
"action": action,
"country": geo_country,
"is_bad": is_malicious,
"attack_type": attack_type,
"webshell": bool(webshell_meta.get("is_webshell", False)),
"webshell_stage": webshell_meta.get("stage", "none"),
"webshell_score": webshell_meta.get("score", 0),
"webshell_confidence": webshell_meta.get("confidence", 0),
"webshell_reasons": webshell_meta.get("reasons", []),
"webshell_path": webshell_meta.get("path", ""),
"webshell_filename": webshell_meta.get("filename", ""),
"db_destructive": bool(webshell_meta.get("is_db_destructive", False)),
"full_payload": full_payload,
"index": hit.get(
"_index",
""
),
"timestamp_utc": timestamp_raw,
})
# ====================================================
# IP 순위
# ====================================================
sorted_ips = sorted(
ip_bytes_map.items(),
key=lambda item: item[1],
reverse=True,
)[:5]
bandwidth_ranking = []
for ip, byte_value in sorted_ips:
bandwidth_ranking.append({
"ip": ip,
"mb": round(
byte_value
/ (
1024
* 1024
),
2,
)
if byte_value > 0
else 0.1,
})
# ====================================================
# Top IP
# ====================================================
sorted_top_ips = sorted(
ip_flow_count.items(),
key=lambda item: item[1],
reverse=True,
)[:5]
top_ips_data = [
{
"ip": ip,
"count": count,
}
for ip, count
in sorted_top_ips
]
# ====================================================
# Protocol
# ====================================================
sorted_protocols = sorted(
protocol_counts.items(),
key=lambda item: item[1],
reverse=True,
)[:10]
protocols_data = [
{
"name": proto,
"count": count,
}
for proto, count
in sorted_protocols
]
# ====================================================
# 위험 건수
# ====================================================
critical_count = sum(
1
for log in logs
if log.get("sev") == "CRITICAL"
)
high_count = sum(
1
for log in logs
if log.get("sev") == "HIGH"
)
# ====================================================
# Unique IP
# ====================================================
unique_ips = len(
ip_flow_count
)
protocol_cnt = len(
protocol_counts
)
# ====================================================
# 전체 로그
# ====================================================
total_logs = len(
logs
)
# ====================================================
# 웹쉘 / DB 파괴 통계
# ====================================================
webshell_logs = [
log
for log in logs
if log.get("webshell")
]
webshell_precursor_logs = [
log
for log in logs
if str(
log.get(
"attack_type",
""
)
).startswith(
"Suspicious Web Upload"
)
]
db_destructive_logs = [
log
for log in logs
if log.get(
"db_destructive"
)
]
webshell_ips = sorted({
str(
log.get(
"src",
""
)
).split(":")[0]
for log in webshell_logs
if log.get("src")
})
# ====================================================
# 시스템 상태
# ====================================================
if webshell_logs:
total_status = (
f"🚨 [{es_index}] 웹쉘 공격 정황 탐지 ({len(webshell_logs)}건) · 피해대상 jjcom.kr"
)
elif has_high_threat:
total_status = (
f"🚨 [{es_index}] "
"고위험 침입 탐지됨!"
)
elif len(logs) > 0:
total_status = (
f"정상 모니터링 중 "
f"({es_index})"
)
else:
total_status = (
"해당 검색 조건에 데이터 없음"
)
# ====================================================
# JSON Response
# ====================================================
return jsonify({
"status": "success",
"summary": {
"total_logs": total_logs,
"unique_ips": unique_ips,
"protocol_cnt": protocol_cnt,
"peak_time": peak_hour,
"peak_cnt": max_traffic,
"critical_cnt": critical_count,
"high_cnt": high_count,
"webshell_cnt": len(webshell_logs),
"webshell_precursor_cnt": len(webshell_precursor_logs),
"db_destructive_cnt": len(db_destructive_logs),
"webshell_ips": webshell_ips[:20],
},
"incident": incident_state,
"chart": {
"labels": hours_active,
"data": traffic_data,
"current_hour": current_hour_str,
},
"geo_chart": {
"labels": list(
country_counts.keys()
),
"data": list(
country_counts.values()
),
},
"bandwidth_ranking":
bandwidth_ranking,
"top_ips":
top_ips_data,
"protocols":
protocols_data,
"webshell": {
"detected": bool(webshell_logs),
"count": len(webshell_logs),
"precursor_count": len(webshell_precursor_logs),
"db_destructive_count": len(db_destructive_logs),
"source_ips": webshell_ips[:20],
"target": "jjcom.kr",
"stage_counts": {
"upload": sum(1 for x in webshell_logs if x.get("webshell_stage") == "upload"),
"execution": sum(1 for x in webshell_logs if x.get("webshell_stage") == "execution"),
"db_destructive": sum(1 for x in webshell_logs if x.get("webshell_stage") == "db_destructive"),
},
},
"logs":
logs,
"total_status":
total_status,
"has_threat":
has_high_threat,
# =================================================
# 디버그
# =================================================
"debug": {
"date":
target_date_str,
"hour":
target_hour_str,
"elasticsearch":
es_host,
"index":
es_index,
"query_start_utc":
start_str,
"query_end_utc":
end_str,
"es_returned_count":
es_returned_count,
"returned_logs":
len(logs),
"total_hits":
total_logs,
"search_keyword":
search_keyword,
"search_method":
"Elasticsearch query + Python verification",
"max_fetch_limit":
MAX_LOGS,
"page_size":
PAGE_SIZE,
"timezone":
"Asia/Seoul (KST)",
},
})
except Exception as e:
import traceback
traceback.print_exc()
return jsonify({
"status": "error",
"message": str(e),
"debug": {
"error_type":
type(e).__name__,
},
}), 500
# ============================================================
# PDF Export API
# ============================================================
@app.route(
"/api/export-pdf",
methods=["GET"]
)
def export_pdf():
return jsonify({
"status": "success",
"message":
"PDF generated (stub)",
})
# ============================================================
# 실행
# ============================================================
if __name__ == "__main__":
app.run(
debug=True,
host="0.0.0.0",
port=5001,
)
이 노트는 로그인하지 않아도
열람할 수 있습니다.
작성·수정·삭제는
관리자 로그인
후 가능합니다.