로그인

app.py ( 관제 )

작성: 2026.08.25 06:15 최근 수정: 2026.08.25 06:15
from flask import Flask, request, jsonify, render_template
from datetime import datetime, timezone, timedelta
import requests
import threading
import time
import re
import uuid
import base64
from urllib.parse import urlparse, unquote

app = Flask(__name__)

APP_NAME = "SOC 이메일 & 네트워크 포렌식 수집 분석 시스템"
VERSION = "9.0 (ELK Deep Forensic Engine)"
LISTEN_HOST = "0.0.0.0"
LISTEN_PORT = 5001

REQUEST_TIMEOUT = 5
targets = {}
target_lock = threading.Lock()
next_target_id = 1

KST = timezone(timedelta(hours=9))

def current_time():
    return datetime.now(KST).strftime("%Y-%m-%d %H:%M:%S")

def kst_time(value):
    if value in (None, "", "-", "null"):
        return current_time()
    try:
        text = str(value).strip()
        if text.endswith("Z"):
            text = text[:-1] + "+00:00"
        dt = datetime.fromisoformat(text)
        if dt.tzinfo is None:
            dt = dt.replace(tzinfo=timezone.utc)
        return dt.astimezone(KST).strftime("%Y-%m-%d %H:%M:%S")
    except (ValueError, TypeError, OverflowError):
        return str(value)

def normalize_host(value):
    value = str(value or "").strip()
    if not value:
        raise ValueError("관제 주소를 입력하세요.")
    if not value.startswith(("http://", "https://")):
        value = "http://" + value
    parsed = urlparse(value)
    if not parsed.hostname:
        raise ValueError("올바른 IP 또는 도메인을 입력하세요.")
    host = parsed.hostname
    port = parsed.port or 9200
    return f"{parsed.scheme}://{host}:{port}"

def target_display(url):
    parsed = urlparse(url)
    return f"{parsed.hostname}:{parsed.port or 9200}"

def elastic_request(base_url, path, method="GET", body=None):
    url = base_url.rstrip("/") + "/" + path.lstrip("/")
    headers = {"Content-Type": "application/json", "Accept": "application/json"}
    
    parsed = urlparse(base_url)
    hostname = parsed.hostname
    port = parsed.port or 9200
    scheme = parsed.scheme

    try:
        if method == "POST":
            return requests.post(url, json=body, headers=headers, timeout=REQUEST_TIMEOUT)
        return requests.get(url, headers=headers, timeout=REQUEST_TIMEOUT)
    except requests.RequestException as exc:
        if hostname == "jjcom.kr":
            fallback_url = f"{scheme}://192.168.0.232:{port}/" + path.lstrip("/")
            try:
                if method == "POST":
                    return requests.post(fallback_url, json=body, headers=headers, timeout=REQUEST_TIMEOUT)
                return requests.get(fallback_url, headers=headers, timeout=REQUEST_TIMEOUT)
            except requests.RequestException:
                pass
        raise RuntimeError(f"접속 실패 ({hostname}): {str(exc)}")

def get_field(data, dotted, default=None):
    current = data
    for part in dotted.split("."):
        if not isinstance(current, dict) or part not in current:
            return default
        current = current[part]
    return current

def first_field(data, *names, default="-"):
    for name in names:
        val = get_field(data, name)
        if val not in (None, "", [], {}):
            if isinstance(val, list):
                return ", ".join(map(str, val))
            return str(val)
    return default

# Base64 자동 복구 디코더
def try_b64_decode(text):
    try:
        if len(text) % 4 == 0 and re.match(r'^[A-Za-z0-9+/=]+$', text):
            decoded = base64.b64decode(text).decode('utf-8', errors='ignore')
            if len(decoded) > 3 and any(c.isalnum() for c in decoded):
                return decoded
    except Exception:
        pass
    return text

# Packetbeat raw 객체 재귀 탐색하여 본문/제목/계정 딥 추출
def extract_elk_forensic_payload(event_data):
    found_strings = []
    
    def walk(obj):
        if isinstance(obj, dict):
            for k, v in obj.items():
                if k in ['payload', 'raw_payload', 'data', 'content', 'body', 'message', 'request', 'response']:
                    if isinstance(v, str):
                        found_strings.append(v)
                walk(v)
        elif isinstance(obj, list):
            for item in obj:
                walk(item)

    walk(event_data)
    combined = "\n".join(found_strings)
    
    # 이메일 헤더 regex 파싱
    mail_from = "-"
    mail_to = "-"
    subject = "-"
    body = "-"

    from_match = re.search(r'(?:From|MAIL FROM):\s*<?([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})>?', combined, re.IGNORECASE)
    if from_match: mail_from = from_match.group(1)

    to_match = re.search(r'(?:To|RCPT TO):\s*<?([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})>?', combined, re.IGNORECASE)
    if to_match: mail_to = to_match.group(1)

    subj_match = re.search(r'Subject:\s*(.+)', combined, re.IGNORECASE)
    if subj_match: subject = subj_match.group(1).strip()

    # 본문 추출 (Subject 이하 텍스트 또는 raw 텍스트)
    if "Subject:" in combined:
        parts = re.split(r'Subject:.*', combined, flags=re.IGNORECASE)
        if len(parts) > 1:
            body = parts[1].strip()
    elif combined.strip():
        body = combined.strip()

    body = try_b64_decode(body)

    return mail_from, mail_to, subject, body

def normalize_event(raw, target):
    event = raw.get("_source", raw)
    src_ip = first_field(event, "source.ip", "client.ip", "host.ip", "src_ip")
    src_port = first_field(event, "source.port", "client.port", "src_port")
    dst_ip = first_field(event, "destination.ip", "server.ip", "dst_ip")
    dst_port = first_field(event, "destination.port", "server.port", "dst_port")
    
    raw_proto = str(first_field(event, "network.protocol", "network.transport", "service.name", default="")).upper()
    src_p = str(src_port) if src_port != "-" else ""
    dst_p = str(dst_port) if dst_port != "-" else ""

    # L4/L7 포트 기반 프로토콜 엄격 분리
    if dst_p == "25" or src_p == "25" or "SMTP" in raw_proto:
        protocol = "SMTP"
    elif dst_p in ["143", "993"] or src_p in ["143", "993"] or "IMAP" in raw_proto:
        protocol = "IMAP"
    elif dst_p in ["110", "995"] or src_p in ["110", "995"] or "POP3" in raw_proto:
        protocol = "POP3"
    elif dst_p in ["80", "443", "8080"] or "HTTP" in raw_proto:
        protocol = "HTTP"
    elif dst_p == "53" or src_p == "53" or "DNS" in raw_proto:
        protocol = "DNS"
    else:
        protocol = raw_proto if raw_proto and raw_proto != "-" else "TCP"

    # ELK 딥 파싱으로 메일 데이터 확보
    deep_from, deep_to, deep_subj, deep_body = extract_elk_forensic_payload(event)

    mail_from = first_field(event, "email.from.address", "smtp.mail_from", "mail.from", default=deep_from)
    mail_to = first_field(event, "email.to.address", "smtp.rcpt_to", "mail.to", default=deep_to)
    mail_subject = first_field(event, "email.subject", "smtp.subject", "mail.subject", default=deep_subj)
    mail_body = first_field(event, "email.body", "email.content", "message", default=deep_body)
    attachment = first_field(event, "email.attachments.file.name", "file.name", default="-")
    process_name = first_field(event, "process.name", "process.executable", "agent.type", default="-")

    timestamp = kst_time(first_field(event, "@timestamp", "timestamp"))
    country = first_field(event, "source.geo.country_name", default="사설망 (Local Network)")

    # 포렌식 및 프로토콜별 근거 명확 분리
    if protocol == "SMTP":
        reason = f"Standard SMTP Mail Sending Flow (Sender: {mail_from})"
    elif protocol == "IMAP":
        reason = f"Standard IMAP Mail Retrieval Flow (Port: {dst_port or src_port})"
    else:
        reason = f"Standard {protocol} Protocol Traffic Flow"

    # 위협 분석
    detection_name = "신뢰됨 (Clean)"
    risk_level = "낮음"
    score = 0

    att_lower = str(attachment).lower()
    if att_lower.endswith(('.exe', '.vbs', '.js', '.bat', '.ps1', '.zip', '.scr')):
        detection_name = "악성 첨부파일 탐지"
        risk_level = "심각"
        score = 98
        reason = f"위험 첨부파일 검출: {attachment}"

    return {
        "id": raw.get("_id", str(uuid.uuid4())),
        "시각": timestamp,
        "출발지": f"{src_ip}:{src_port}" if src_port != "-" else str(src_ip),
        "출발지IP": src_ip,
        "출발국가": country,
        "목적지": f"{dst_ip}:{dst_port}" if dst_port != "-" else str(dst_ip),
        "목적지IP": dst_ip,
        "프로토콜": protocol,
        "서비스": first_field(event, "service.name", default=protocol),
        "메시지": f"{protocol} Stream Session",
        "탐지": detection_name,
        "위험도": risk_level,
        "점수": score,
        "탐지근거": reason,
        "대상": target["표시"],
        "대상ID": target["id"],
        "forensics": {
            "mail_from": mail_from,
            "mail_to": mail_to,
            "subject": mail_subject,
            "body": mail_body,
            "attachment": attachment,
            "process": process_name,
            "raw_log": raw
        }
    }

def check_target(target):
    start = time.time()
    try:
        index_pattern = target.get("인덱스", "*")
        path = f"/{index_pattern}/_search"
        
        resp = elastic_request(target["주소"], path, method="POST", body={
            "size": 500,
            "sort": [{"@timestamp": {"order": "desc", "unmapped_type": "date"}}],
            "query": {"match_all": {}}
        })
        hits = resp.json().get("hits", {}).get("hits", []) if resp.status_code == 200 else []
        normalized = [normalize_event(h, target) for h in hits]
        elapsed = round((time.time() - start) * 1000)

        with target_lock:
            target["상태"] = "연결됨"
            target["오류"] = ""
            target["응답시간"] = elapsed
            target["마지막확인"] = current_time()
            target["이벤트"] = normalized
        return True
    except Exception as exc:
        elapsed = round((time.time() - start) * 1000)
        with target_lock:
            target["상태"] = "응답 없음"
            target["오류"] = str(exc)
            target["응답시간"] = elapsed
            target["마지막확인"] = current_time()
        return False

def monitoring_loop():
    while True:
        with target_lock:
            cur_targets = list(targets.values())
        for t in cur_targets:
            check_target(t)
        time.sleep(2)

threading.Thread(target=monitoring_loop, daemon=True).start()

@app.route("/")
def index():
    return render_template("index.html", app_name=APP_NAME, version=VERSION)

@app.route("/api/targets", methods=["GET", "POST"])
def handle_targets():
    global next_target_id
    if request.method == "POST":
        data = request.get_json(silent=True) or {}
        address = normalize_host(data.get("주소", data.get("host", "")))
        index_pattern = str(data.get("인덱스", "*")).strip() or "*"
        
        with target_lock:
            for t in targets.values():
                if t["주소"] == address and t["인덱스"] == index_pattern:
                    return jsonify({"성공": True, "중복": True, "대상": t})
            t = {
                "id": next_target_id, "주소": address, "표시": target_display(address),
                "인덱스": index_pattern, "상태": "확인 중", "오류": "", "응답시간": 0,
                "마지막확인": "-", "이벤트": []
            }
            targets[next_target_id] = t
            next_target_id += 1
        check_target(t)
        return jsonify({"성공": True, "중복": False, "대상": t})

    with target_lock:
        return jsonify(list(targets.values()))

@app.route("/api/targets/<int:target_id>", methods=["DELETE"])
def delete_target(target_id):
    with target_lock:
        if target_id in targets:
            del targets[target_id]
            return jsonify({"성공": True})
    return jsonify({"성공": False, "오류": "대상을 찾을 수 없습니다."}), 404

@app.route("/api/dashboard")
def dashboard():
    target_id_param = request.args.get("target_id")
    all_events = []
    connected, offline = 0, 0

    with target_lock:
        for target in targets.values():
            if target["상태"] == "연결됨":
                connected += 1
            else:
                offline += 1

        filter_id = int(target_id_param) if (target_id_param and target_id_param.isdigit()) else None

        for t_id, target in targets.items():
            if filter_id is not None:
                if t_id == filter_id:
                    all_events.extend(target["이벤트"])
            else:
                all_events.extend(target["이벤트"])

    all_events.sort(key=lambda x: str(x.get("시각", "")), reverse=True)

    return jsonify({
        "시각": current_time(),
        "전체대상": len(targets),
        "연결": connected,
        "응답없음": offline,
        "전체이벤트": len(all_events),
        "선택대상ID": target_id_param or "전체",
        "이벤트": all_events[:500]
    })

if __name__ == "__main__":
    app.run(host=LISTEN_HOST, port=LISTEN_PORT, debug=False, threaded=True)
이 노트는 로그인하지 않아도 열람할 수 있습니다. 작성·수정·삭제는 관리자 로그인 후 가능합니다.