douyinWEB/scripts/_security_attack_report.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
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>
2026-09-04 22:42:40 -05:00

143 lines
4.8 KiB
Python

# -*- coding: utf-8 -*-
"""Collect forensic evidence of attacks (read-only)."""
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"107.173.30.245",
22,
"root",
"Xiao4669805",
timeout=40,
allow_agent=False,
look_for_keys=False,
)
def run(cmd: str, timeout: int = 180) -> str:
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
print("===== FAIL2BAN =====")
print(run("fail2ban-client status; echo '---'; fail2ban-client status sshd"))
print("===== SSH FAIL TOP (auth.log) =====")
print(
run(
r"""
python3 - <<'PY'
from collections import Counter
import re, os
files=[p for p in ["/var/log/auth.log","/var/log/auth.log.1"] if os.path.exists(p)]
pat_fail=re.compile(r"Failed password for (invalid user )?(\S+) from ([0-9.]+)")
pat_inv=re.compile(r"Invalid user (\S*) from ([0-9.]+)")
pat_ok=re.compile(r"Accepted (password|publickey) for (\S+) from ([0-9.]+)")
fail_ip=Counter(); fail_user=Counter(); inv_user=Counter(); ok=Counter(); pairs=Counter()
for f in files:
with open(f,"r",errors="replace") as fh:
for line in fh:
m=pat_fail.search(line)
if m:
user=m.group(2); ip=m.group(3)
fail_ip[ip]+=1; fail_user[user]+=1; pairs[(ip,user)]+=1
m=pat_inv.search(line)
if m: inv_user[m.group(1) or "(empty)"]+=1
m=pat_ok.search(line)
if m: ok[(m.group(2), m.group(3), m.group(1))]+=1
print("FAIL_IP_TOP")
for ip,n in fail_ip.most_common(40): print(f"{n:6d} {ip}")
print("FAIL_USER_TOP")
for u,n in fail_user.most_common(25): print(f"{n:6d} {u}")
print("INVALID_USER_TOP")
for u,n in inv_user.most_common(20): print(f"{n:6d} {u}")
print("SUCCESS_LOGINS")
for (u,ip,m),n in ok.most_common(30): print(f"{n:6d} {u} {ip} {m}")
print("FAIL_PAIRS_TOP")
for (ip,u),n in pairs.most_common(25): print(f"{n:6d} {ip} user={u}")
print("TOTAL_FAIL", sum(fail_ip.values()), "UNIQUE_IPS", len(fail_ip))
PY
"""
)
)
print("===== JOURNAL SSH 7d =====")
print(
run(
r"""
journalctl -u ssh --since '7 days ago' --no-pager 2>/dev/null | python3 -c '
import sys,re
from collections import Counter
fail=Counter(); ok=Counter()
for line in sys.stdin:
m=re.search(r"Failed password for (invalid user )?(\S+) from ([0-9.]+)", line)
if m: fail[m.group(3)] += 1
m=re.search(r"Accepted password for (\S+) from ([0-9.]+)", line)
if m: ok[(m.group(1), m.group(2))] += 1
print("7d FAIL unique", len(fail), "total", sum(fail.values()))
for ip,n in fail.most_common(25): print(f"fail {n:6d} {ip}")
print("7d SUCCESS")
for (u,ip),n in ok.most_common(20): print(f"ok {n:6d} {u} {ip}")
'
"""
)
)
print("===== lastb =====")
print(
run(
"lastb -n 60 2>/dev/null | head -60; echo '---'; lastb 2>/dev/null | awk '{print $1,$3}' | sort | uniq -c | sort -nr | head -25"
)
)
print("===== WHOIS top attackers =====")
print(
run(
r"""
IPS="45.148.10.157 45.148.10.152 45.154.244.193 193.47.62.69 195.178.110.137 62.60.130.242 64.62.197.147"
for ip in $IPS; do
echo "==== $ip ===="
curl -fsS --max-time 8 "http://ip-api.com/json/$ip?fields=status,country,regionName,city,isp,org,as,query,proxy,hosting" 2>/dev/null || true
echo
done
"""
)
)
print("===== root success by IP =====")
print(
run(
r"grep 'Accepted password for root' /var/log/auth.log /var/log/auth.log.1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i==\"from\") print $(i+1)}' | sort | uniq -c | sort -nr | head -20"
)
)
print("===== authorized_keys / cron =====")
print(run("echo KEYS; wc -l /root/.ssh/authorized_keys 2>/dev/null; cat /root/.ssh/authorized_keys 2>/dev/null; echo CRON; crontab -l 2>/dev/null; ls /etc/cron.d; cat /etc/cron.d/dylive-inject-monitor /etc/cron.d/nomadro-health 2>/dev/null"))
print("===== inject monitor + sample auth fails =====")
print(run("cat /var/log/dylive-inject-monitor.log 2>/dev/null; echo '---sample---'; grep -E 'Failed password|Invalid user|Accepted password' /var/log/auth.log 2>/dev/null | tail -40"))
print("===== web probe paths (caddy/nginx if any) =====")
print(
run(
r"""
find /var/log -name '*caddy*' -o -name '*access*' 2>/dev/null | head -30
# journal may have http
journalctl -u caddy --since '2 days ago' -o cat --no-pager 2>/dev/null | grep -iE 'wp-login|\.env|xmlrpc|phpunit|eval-stdin|/admin|/shell' | head -20 || echo no_caddy_probe_in_journal
# nginx if present
if [ -f /var/log/nginx/access.log ]; then
echo NGINX_PROBES
grep -E 'wp-login|xmlrpc|\.env|phpunit|eval-stdin|cgi-bin|\.git' /var/log/nginx/access.log 2>/dev/null | awk '{print $1,$7}' | sort | uniq -c | sort -nr | head -20
fi
"""
)
)
c.close()
print("FORENSICS DONE")