Experimental branch cleanup and fixes

- Removed miner protection module, config, API routes, and UI
- Removed block_postgres firewall feature from dashboard + launcher
- Fixed SSH host key policy (WarningPolicy → AutoAddPolicy)
- Fixed kick race condition (threading.Lock for iptables)
- Tuned Argon2 password parameters (time_cost=3, memory_cost=65536)
- Added login rate-limit (10/min) + IP cooldown after 5 failed attempts
- Created scripts/db_check.py for launcher pre-flight DB check
- Updated README and .gitignore for sanitized repo state
This commit is contained in:
Reditus committed 2026-05-21 06:33:48 +01:00
1 parent 18a3253879
commit c57e6572a9
13 files changed
+131 -499

No files matched your search

+21 -14
View File
@@ -20,6 +20,8 @@ def _validate_ip(ip):
class AdminService:
_iptables_lock = threading.Lock()
def __init__(self, db_service, ssh_service):
self.db = db_service
self.ssh = ssh_service
@@ -126,12 +128,13 @@ class AdminService:
cur.execute("INSERT INTO dune.player_actions (player_id, action_type, reason, duration_minutes) VALUES (%s, 'unban', 'Manual unban', 0)", [player_id])
conn.commit()
for ip in ips_to_unblock:
if not _validate_ip(ip):
logger.warning(f"Skipping invalid IP in unban: {ip}")
continue
self.ssh.run(f'sudo iptables -D INPUT -s {ip} -j DROP 2>/dev/null')
self.ssh.run(f'sudo iptables -D OUTPUT -d {ip} -j DROP 2>/dev/null')
with self._iptables_lock:
for ip in ips_to_unblock:
if not _validate_ip(ip):
logger.warning(f"Skipping invalid IP in unban: {ip}")
continue
self.ssh.run(f'sudo iptables -D INPUT -s {ip} -j DROP 2>/dev/null')
self.ssh.run(f'sudo iptables -D OUTPUT -d {ip} -j DROP 2>/dev/null')
audit_logger.info(f"UNBAN: player_id={player_id} ips_cleared={len(ips_to_unblock)}")
return True, f"Player unbanned. Cleared {len(ips_to_unblock)} IP block(s)."
@@ -166,11 +169,13 @@ class AdminService:
if not _validate_ip(ip):
logger.error(f"Invalid IP address for kick: {ip}")
return
self.ssh.run(f'sudo iptables -I INPUT -s {ip} -j DROP')
self.ssh.run(f'sudo iptables -I OUTPUT -d {ip} -j DROP')
with self._iptables_lock:
self.ssh.run(f'sudo iptables -I INPUT -s {ip} -j DROP')
self.ssh.run(f'sudo iptables -I OUTPUT -d {ip} -j DROP')
time.sleep(60)
self.ssh.run(f'sudo iptables -D INPUT -s {ip} -j DROP')
self.ssh.run(f'sudo iptables -D OUTPUT -d {ip} -j DROP')
with self._iptables_lock:
self.ssh.run(f'sudo iptables -D INPUT -s {ip} -j DROP')
self.ssh.run(f'sudo iptables -D OUTPUT -d {ip} -j DROP')
thread = threading.Thread(target=temporary_block, args=(player_ip,), daemon=True)
thread.start()
@@ -319,8 +324,9 @@ class AdminService:
if ban_check:
if _validate_ip(ip):
self.ssh.run(f'sudo iptables -I INPUT -s {ip} -j DROP')
self.ssh.run(f'sudo iptables -I OUTPUT -d {ip} -j DROP')
with self._iptables_lock:
self.ssh.run(f'sudo iptables -I INPUT -s {ip} -j DROP')
self.ssh.run(f'sudo iptables -I OUTPUT -d {ip} -j DROP')
else:
logger.warning(f"Skipping invalid IP for ban block: {ip}")
@@ -346,8 +352,9 @@ class AdminService:
def emergency_unban(self, ip):
if not _validate_ip(ip):
return False, f"Invalid IP address: {ip}"
self.ssh.run(f'sudo iptables -D INPUT -s {ip} -j DROP 2>/dev/null')
self.ssh.run(f'sudo iptables -D OUTPUT -d {ip} -j DROP 2>/dev/null')
with self._iptables_lock:
self.ssh.run(f'sudo iptables -D INPUT -s {ip} -j DROP 2>/dev/null')
self.ssh.run(f'sudo iptables -D OUTPUT -d {ip} -j DROP 2>/dev/null')
audit_logger.info(f"EMERGENCY_UNBAN: ip={ip}")
return True, f"Unblocked {ip}"
-252
View File
@@ -1,252 +0,0 @@
import os
import subprocess
import threading
import time
import datetime
import logging
import json
logger = logging.getLogger(__name__)
MINER_FILES = ['mysql', 'init', 'kunt', 'bot', 'watchdog']
CHECK_INTERVAL = 60 # seconds
LOG_FILE = 'logs/miner-protection.log'
LOG_DIR = os.path.dirname(LOG_FILE)
MAX_LOG_SIZE = 1000 # keep last 1000 lines
class MinerProtection:
def __init__(self, config, ssh_service):
self.config = config
self.ssh_service = ssh_service
self.enabled = config.get('miner_protection', {}).get('enabled', True)
self.interval = config.get('miner_protection', {}).get('interval_seconds', 60)
self.log_file = config.get('miner_protection', {}).get('log_file', LOG_FILE)
self.running = False
self.thread = None
self.log_entries = []
self.detection_history = []
self._ensure_log_dir()
def _ensure_log_dir(self):
"""Ensure log directory exists."""
if LOG_DIR and not os.path.exists(LOG_DIR):
try:
os.makedirs(LOG_DIR, exist_ok=True)
except Exception:
pass
def _get_namespace(self):
"""Get the current battlegroup namespace via SSH."""
ns = self.config.get('kubernetes', {}).get('namespace', '')
if not ns:
# Try to find from running battlegroup - look for active funcom-seabass namespace
try:
out, err, rc = self.ssh_service.run('sudo kubectl get ns -o name', timeout=10)
if rc == 0:
for line in out.splitlines():
if 'funcom-seabass-sh-b17a5f036d1f7882' in line and 'ymozmn' not in line:
ns = line.replace('namespace/', '')
break
# Fallback: try any funcom-seabass namespace
if not ns:
for line in out.splitlines():
if 'funcom-seabass' in line:
ns = line.replace('namespace/', '')
break
except Exception:
pass
return ns
def _get_db_pod(self, namespace):
"""Get the database pod name via SSH."""
if not namespace:
return None
try:
out, err, rc = self.ssh_service.run(f'sudo kubectl get pods -n {namespace} -o name', timeout=10)
if rc == 0:
for line in out.splitlines():
if 'db-dbdepl-sts' in line:
return line.replace('pod/', '')
except Exception:
pass
return None
def _cleanup_miner(self):
"""Run the cleanup script and return result."""
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
result = {
'timestamp': timestamp,
'status': 'unknown',
'details': '',
'infected': False
}
namespace = self._get_namespace()
if not namespace:
result['status'] = 'idle'
result['details'] = 'No active battlegroup - waiting for battlegroup to start'
return result
db_pod = self._get_db_pod(namespace)
if not db_pod:
result['status'] = 'idle'
result['details'] = 'Battlegroup starting - no database pod yet'
return result
try:
# Check for malicious files in container
check_cmd = f"sudo kubectl exec -n {namespace} {db_pod} -c database -- ls -la /tmp/ 2>/dev/null"
check_out, check_err, check_rc = self.ssh_service.run(check_cmd, timeout=30)
infected_files = []
for mf in MINER_FILES:
if mf in check_out:
infected_files.append(mf)
if infected_files:
result['infected'] = True
result['status'] = 'detected'
result['details'] = f'Found: {", ".join(infected_files)}'
# Kill processes and delete files
kill_cmd = f"sudo kubectl exec -n {namespace} {db_pod} -c database -- sh -c 'pkill -9 -f /tmp/mysql 2>/dev/null; pkill -9 -f /tmp/init 2>/dev/null; pkill -9 -f /tmp/kunt 2>/dev/null; rm -f /tmp/mysql /tmp/init /tmp/kunt 2>/dev/null'"
self.ssh_service.run(kill_cmd, timeout=30)
# Also check/clean host via SSH
try:
self.ssh_service.run("pkill -9 -f /tmp/mysql 2>/dev/null || true", timeout=10)
self.ssh_service.run("pkill -9 -f /tmp/init 2>/dev/null || true", timeout=10)
self.ssh_service.run("rm -f /tmp/mysql /tmp/init /tmp/kunt 2>/dev/null || true", timeout=10)
except:
pass
result['status'] = 'cleaned'
result['details'] = f'Detected and cleaned: {", ".join(infected_files)}'
# Add to detection history
self.detection_history.append(result.copy())
if len(self.detection_history) > 5:
self.detection_history = self.detection_history[-5:]
else:
result['status'] = 'clean'
result['details'] = 'No threats found'
except Exception as e:
result['status'] = 'error'
result['details'] = str(e)
return result
def _log_result(self, result):
"""Log result to file and memory."""
log_line = f"[{result['timestamp']}] {result['status'].upper()} - {result['details']}"
# Add to memory
self.log_entries.append(log_line)
if len(self.log_entries) > 15:
self.log_entries = self.log_entries[-15:]
# Write to file
try:
with open(self.log_file, 'a') as f:
f.write(log_line + '\n')
# Rotate if too large
if os.path.exists(self.log_file):
with open(self.log_file, 'r') as f:
lines = f.readlines()
if len(lines) > MAX_LOG_SIZE:
with open(self.log_file, 'w') as f:
f.writelines(lines[-MAX_LOG_SIZE:])
except Exception as e:
logger.error(f"Failed to write miner log: {e}")
def _run_check(self):
"""Run a single check."""
if not self.enabled:
return
logger.debug("Running miner protection check...")
result = self._cleanup_miner()
self._log_result(result)
logger.debug(f"Miner check result: {result['status']} - {result['details']}")
def _background_loop(self):
"""Background thread loop."""
while self.running:
try:
self._run_check()
except Exception as e:
logger.debug(f"Miner protection check failed: {e}")
time.sleep(self.interval)
def start(self):
"""Start the background monitoring."""
if self.running:
return
self.running = True
self.thread = threading.Thread(target=self._background_loop, daemon=True)
self.thread.start()
logger.info(f"Miner protection started (interval: {self.interval}s, enabled: {self.enabled})")
def stop(self):
"""Stop the background monitoring."""
self.running = False
if self.thread:
self.thread.join(timeout=5)
logger.info("Miner protection stopped")
def run_cleanup(self):
"""Manually run cleanup and return result."""
result = self._cleanup_miner()
self._log_result(result)
return result
def toggle(self, enabled):
"""Toggle enabled state."""
self.enabled = enabled
logger.info(f"Miner protection {'enabled' if enabled else 'disabled'}")
def get_status(self):
"""Get current status."""
return {
'enabled': self.enabled,
'last_check': self.log_entries[-1] if self.log_entries else None,
'last_detection': self.detection_history[-1] if self.detection_history else None,
'detection_count': len(self.detection_history)
}
def get_logs(self, all_logs=False):
"""Get log entries."""
if all_logs:
# Return all recent logs from file
try:
if os.path.exists(self.log_file):
with open(self.log_file, 'r') as f:
lines = f.readlines()
return [l.strip() for l in lines[-15:] if l.strip()]
except:
pass
return self.log_entries
def get_detection_history(self):
"""Get detection history."""
return self.detection_history
# Global instance
_miner_protection = None
def init_miner_protection(config, ssh_service):
"""Initialize the miner protection service."""
global _miner_protection
_miner_protection = MinerProtection(config, ssh_service)
return _miner_protection
def get_miner_protection():
"""Get the miner protection instance."""
return _miner_protection
+2 -1
View File
@@ -40,7 +40,8 @@ class SSHService:
self._client = None
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
logger.debug("SSH host key verification policy set to AutoAddPolicy")
connect_kwargs = {
'hostname': self.host,