Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP. Co-authored-by: Cursor <cursoragent@cursor.com>
134 lines
4.1 KiB
Python
134 lines
4.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Apply open-source hardening (safe, non-locking):
|
|
- fail2ban (SSH)
|
|
- SSH soft harden (MaxAuthTries / LoginGraceTime / etc) — keep password auth
|
|
- Bind douyinweb API/Web to 127.0.0.1
|
|
- Enable unattended-upgrades
|
|
- Install lynis + run quick audit report
|
|
- Optional UFW: only enable if --with-ufw (default OFF to avoid breaking frp/sing-box)
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
HOST = "107.173.30.245"
|
|
PASS = "Xiao4669805"
|
|
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(HOST, 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False)
|
|
|
|
|
|
def run(cmd: str, timeout: int = 300) -> str:
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
out = (o.read() + e.read()).decode("utf-8", "replace")
|
|
print(out[-8000:] if len(out) > 8000 else out)
|
|
return out
|
|
|
|
|
|
print("=== 1) apt install fail2ban lynis ===")
|
|
run(
|
|
"export DEBIAN_FRONTEND=noninteractive; "
|
|
"apt-get update -qq && "
|
|
"apt-get install -y -qq fail2ban lynis 2>&1 | tail -20",
|
|
timeout=300,
|
|
)
|
|
|
|
print("=== 2) fail2ban jail.local ===")
|
|
jail = r"""
|
|
[DEFAULT]
|
|
bantime = 1h
|
|
findtime = 10m
|
|
maxretry = 5
|
|
backend = systemd
|
|
ignoreip = 127.0.0.1/8 ::1
|
|
|
|
[sshd]
|
|
enabled = true
|
|
port = ssh
|
|
filter = sshd
|
|
maxretry = 4
|
|
bantime = 6h
|
|
findtime = 15m
|
|
"""
|
|
sftp = c.open_sftp()
|
|
with sftp.file("/etc/fail2ban/jail.local", "w") as f:
|
|
f.write(jail.strip() + "\n")
|
|
sftp.close()
|
|
run("systemctl enable --now fail2ban; sleep 2; systemctl is-active fail2ban; fail2ban-client status sshd 2>&1 | head -30")
|
|
|
|
print("=== 3) SSH soft harden drop-in (keep PasswordAuthentication) ===")
|
|
sshd = r"""
|
|
# Managed by douyinWEB security harden — 2026-08-21
|
|
MaxAuthTries 4
|
|
LoginGraceTime 30
|
|
PermitEmptyPasswords no
|
|
X11Forwarding no
|
|
ClientAliveInterval 300
|
|
ClientAliveCountMax 2
|
|
# Keep password for now; Prefer keys when available
|
|
PubkeyAuthentication yes
|
|
PasswordAuthentication yes
|
|
PermitRootLogin yes
|
|
"""
|
|
with c.open_sftp().file("/etc/ssh/sshd_config.d/99-harden.conf", "w") as f:
|
|
f.write(sshd.strip() + "\n")
|
|
run("sshd -t && systemctl reload ssh; echo ssh_ok")
|
|
|
|
print("=== 4) Bind douyinweb to localhost ===")
|
|
api_unit = run("systemctl cat douyinweb-api")
|
|
web_unit = run("systemctl cat douyinweb-web")
|
|
# write overrides
|
|
run("mkdir -p /etc/systemd/system/douyinweb-api.service.d /etc/systemd/system/douyinweb-web.service.d")
|
|
api_override = """[Service]
|
|
ExecStart=
|
|
ExecStart=/opt/douyinWEB/backend/.venv/bin/python -m uvicorn app.main:app --host 127.0.0.1 --port 8014
|
|
"""
|
|
web_override = """[Service]
|
|
ExecStart=
|
|
ExecStart=/usr/bin/npm run start -- --hostname 127.0.0.1 --port 4015
|
|
"""
|
|
with c.open_sftp().file("/etc/systemd/system/douyinweb-api.service.d/override.conf", "w") as f:
|
|
f.write(api_override)
|
|
with c.open_sftp().file("/etc/systemd/system/douyinweb-web.service.d/override.conf", "w") as f:
|
|
f.write(web_override)
|
|
run(
|
|
"systemctl daemon-reload; "
|
|
"systemctl restart douyinweb-api douyinweb-web; "
|
|
"sleep 8; "
|
|
"systemctl is-active douyinweb-api douyinweb-web; "
|
|
"ss -lntp | grep -E ':8014|:4015' || true"
|
|
)
|
|
|
|
print("=== 5) unattended-upgrades ensure enabled ===")
|
|
run(
|
|
"dpkg-reconfigure -f noninteractive unattended-upgrades 2>&1 | tail -5; "
|
|
"systemctl enable --now unattended-upgrades; "
|
|
"systemctl is-active unattended-upgrades; "
|
|
"grep -E 'Unattended-Upgrade::Automatic|\"${distro_id}' /etc/apt/apt.conf.d/50unattended-upgrades 2>/dev/null | head -10"
|
|
)
|
|
|
|
print("=== 6) lynis quick report ===")
|
|
run("lynis audit system --quick 2>&1 | tee /tmp/lynis_quick.txt | tail -60", timeout=240)
|
|
|
|
print("=== 7) verify site + CSP + fail2ban ===")
|
|
run(
|
|
"curl -sSI https://dylive.nomadro.com/login | tr -d '\\r' | grep -iE 'HTTP/|content-security|x-frame'; "
|
|
"python3 - <<'PY'\n"
|
|
"import urllib.request,ssl\n"
|
|
"ctx=ssl._create_unverified_context()\n"
|
|
"h=urllib.request.urlopen('https://dylive.nomadro.com/login', context=ctx, timeout=20).read()\n"
|
|
"print('malware', b'data:text/javascript;base64' in h, 'len', len(h))\n"
|
|
"PY\n"
|
|
"fail2ban-client status sshd | head -20"
|
|
)
|
|
|
|
c.close()
|
|
print("HARDEN DONE")
|