fix: firewall block/unblock in INPUT, FORWARD, and mangle PREROUTING chains; combine SSH calls to avoid timeouts; check actual iptables rules instead of settings config
This commit is contained in:
1 parent
ae86251196
commit
67ad94ea26
2 files changed
+22
-22
No files matched your search
+19
-21
@@ -96,13 +96,11 @@ def register_api_routes(app, services, settings):
|
|||||||
'postgres': {'port': 15432, 'name': 'PostgreSQL'},
|
'postgres': {'port': 15432, 'name': 'PostgreSQL'},
|
||||||
}
|
}
|
||||||
|
|
||||||
out_input, _, _ = ssh.run('sudo iptables -L INPUT -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"', timeout=10)
|
out, _, _ = ssh.run('sudo iptables -L INPUT -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"; sudo iptables -L FORWARD -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"; sudo iptables -t mangle -L PREROUTING -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"', timeout=15)
|
||||||
out_forward, _, _ = ssh.run('sudo iptables -L FORWARD -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"', timeout=10)
|
|
||||||
all_output = out_input + out_forward
|
|
||||||
|
|
||||||
blocked_ports = set()
|
blocked_ports = set()
|
||||||
rules = {}
|
rules = {}
|
||||||
for line in all_output.split('\n'):
|
for line in out.split('\n'):
|
||||||
for port in ['18888', '31820', '15432']:
|
for port in ['18888', '31820', '15432']:
|
||||||
if f'dpt:{port}' in line and 'DROP' in line:
|
if f'dpt:{port}' in line and 'DROP' in line:
|
||||||
blocked_ports.add(port)
|
blocked_ports.add(port)
|
||||||
@@ -135,9 +133,11 @@ def register_api_routes(app, services, settings):
|
|||||||
f'sudo iptables -I INPUT 1 -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT && '
|
f'sudo iptables -I INPUT 1 -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT && '
|
||||||
f'sudo iptables -I INPUT 2 -p tcp --dport {port} -j DROP && '
|
f'sudo iptables -I INPUT 2 -p tcp --dport {port} -j DROP && '
|
||||||
f'sudo iptables -I FORWARD 1 -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT && '
|
f'sudo iptables -I FORWARD 1 -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT && '
|
||||||
f'sudo iptables -I FORWARD 2 -p tcp --dport {port} -j DROP'
|
f'sudo iptables -I FORWARD 2 -p tcp --dport {port} -j DROP && '
|
||||||
|
f'sudo iptables -t mangle -I PREROUTING 1 -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT && '
|
||||||
|
f'sudo iptables -t mangle -I PREROUTING 2 -p tcp --dport {port} -j DROP'
|
||||||
)
|
)
|
||||||
out, err, rc = ssh.run(cmd, timeout=15)
|
out, err, rc = ssh.run(cmd, timeout=20)
|
||||||
if rc != 0 and 'already exists' not in (out + err):
|
if rc != 0 and 'already exists' not in (out + err):
|
||||||
return jsonify({'success': False, 'output': err})
|
return jsonify({'success': False, 'output': err})
|
||||||
|
|
||||||
@@ -164,20 +164,18 @@ def register_api_routes(app, services, settings):
|
|||||||
if port not in (18888, 31820, 15432):
|
if port not in (18888, 31820, 15432):
|
||||||
return jsonify({'success': False, 'output': 'Invalid port'})
|
return jsonify({'success': False, 'output': 'Invalid port'})
|
||||||
|
|
||||||
cmds = [
|
cmd = (
|
||||||
f'sudo iptables -D INPUT -p tcp --dport {port} -j DROP',
|
f'sudo iptables -D INPUT -p tcp --dport {port} -j DROP 2>/dev/null; '
|
||||||
f'sudo iptables -D INPUT -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT',
|
f'sudo iptables -D INPUT -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT 2>/dev/null; '
|
||||||
f'sudo iptables -D FORWARD -p tcp --dport {port} -j DROP',
|
f'sudo iptables -D FORWARD -p tcp --dport {port} -j DROP 2>/dev/null; '
|
||||||
f'sudo iptables -D FORWARD -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT',
|
f'sudo iptables -D FORWARD -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT 2>/dev/null; '
|
||||||
]
|
f'sudo iptables -t mangle -D PREROUTING -p tcp --dport {port} -j DROP 2>/dev/null; '
|
||||||
all_ok = True
|
f'sudo iptables -t mangle -D PREROUTING -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT 2>/dev/null; '
|
||||||
results = []
|
f'echo DONE'
|
||||||
for cmd in cmds:
|
)
|
||||||
out, err, rc = ssh.run(cmd, timeout=10)
|
out, err, rc = ssh.run(cmd, timeout=20)
|
||||||
combined = (out + err).strip()
|
combined = (out + err).strip()
|
||||||
results.append(combined if combined else 'ok')
|
all_ok = 'DONE' in out or rc == 0
|
||||||
if rc != 0 and 'Bad rule' not in combined and 'No chain' not in combined and 'not found' not in combined:
|
|
||||||
all_ok = False
|
|
||||||
|
|
||||||
port_key = {18888: 'block_filebrowser', 31820: 'block_director', 15432: 'block_postgres'}.get(port)
|
port_key = {18888: 'block_filebrowser', 31820: 'block_director', 15432: 'block_postgres'}.get(port)
|
||||||
if port_key:
|
if port_key:
|
||||||
@@ -193,7 +191,7 @@ def register_api_routes(app, services, settings):
|
|||||||
with open(settings_path, 'w') as f:
|
with open(settings_path, 'w') as f:
|
||||||
yaml.dump(dict(settings), f)
|
yaml.dump(dict(settings), f)
|
||||||
|
|
||||||
return jsonify({'success': all_ok, 'output': ' / '.join(r for r in results if r)})
|
return jsonify({'success': all_ok, 'output': 'Port unblocked' if all_ok else combined})
|
||||||
|
|
||||||
# Chat API
|
# Chat API
|
||||||
@app.route('/api/chat_logs')
|
@app.route('/api/chat_logs')
|
||||||
|
|||||||
@@ -698,13 +698,15 @@ if (-not $SshValid) {
|
|||||||
|
|
||||||
$tmpFwScript = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName() + ".sh")
|
$tmpFwScript = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName() + ".sh")
|
||||||
$fwContent = "for PORT in $($portsToBlock -join ' '); do`n"
|
$fwContent = "for PORT in $($portsToBlock -join ' '); do`n"
|
||||||
$fwContent += " if iptables -C FORWARD -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT 2>/dev/null; then`n"
|
$fwContent += " if iptables -t mangle -C PREROUTING -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT 2>/dev/null; then`n"
|
||||||
$fwContent += " echo 'Port `$PORT already blocked'`n"
|
$fwContent += " echo 'Port `$PORT already blocked'`n"
|
||||||
$fwContent += " else`n"
|
$fwContent += " else`n"
|
||||||
$fwContent += " iptables -I INPUT 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n"
|
$fwContent += " iptables -I INPUT 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n"
|
||||||
$fwContent += " iptables -I INPUT 2 -p tcp --dport `$PORT -j DROP`n"
|
$fwContent += " iptables -I INPUT 2 -p tcp --dport `$PORT -j DROP`n"
|
||||||
$fwContent += " iptables -I FORWARD 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n"
|
$fwContent += " iptables -I FORWARD 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n"
|
||||||
$fwContent += " iptables -I FORWARD 2 -p tcp --dport `$PORT -j DROP`n"
|
$fwContent += " iptables -I FORWARD 2 -p tcp --dport `$PORT -j DROP`n"
|
||||||
|
$fwContent += " iptables -t mangle -I PREROUTING 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n"
|
||||||
|
$fwContent += " iptables -t mangle -I PREROUTING 2 -p tcp --dport `$PORT -j DROP`n"
|
||||||
$fwContent += " echo 'Port `$PORT blocked'`n"
|
$fwContent += " echo 'Port `$PORT blocked'`n"
|
||||||
$fwContent += " fi`n"
|
$fwContent += " fi`n"
|
||||||
$fwContent += "done`n"
|
$fwContent += "done`n"
|
||||||
|
|||||||
Reference in new issue
Block a user