Could anyone help me with a review of my iptables rules (running a new Tor relay server), please?
I'm running a Debian GNU/Linux 11 (bullseye), fully updated.
I drop everything in INPUT chain by default, SSH port is censored, so if you see XXYYZ... I changed it to a custom port so that the bots have a little bit more work than just hit the 22.
I will copy-paste the rules.v4 file now:
# Latest revision on 2021-Jul-25
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
--append INPUT --match conntrack --ctstate NEW --protocol tcp ! --syn --match comment --comment "protection: non-syn packets" --jump DROP
--append INPUT --match conntrack --ctstate INVALID --match comment --comment "protection: malformed packets" --jump DROP
--append INPUT --in-interface lo --match comment --comment "loopback: compulsory" --jump ACCEPT
--append INPUT --protocol icmp --icmp-type echo-request --match limit --limit 2/second --limit-burst 5 --match comment --comment "ICMP: ping only" --jump ACCEPT
--append INPUT --match conntrack --ctstate RELATED,ESTABLISHED --match comment --comment "Tor: traffic" --jump ACCEPT
--append INPUT --match conntrack --ctstate NEW,ESTABLISHED --protocol tcp --match tcp --destination-port XXYYZ --match comment --comment "SSH: global obfuscated" --jump ACCEPT
--append INPUT --protocol tcp --match tcp --destination-port 9001 --match comment --comment "Tor: OR" --jump ACCEPT
--append INPUT --protocol tcp --match tcp --destination-port 9030 --match comment --comment "Tor: Dir" --jump ACCEPT
COMMIT
The current output of about one day's uptime is:
# iptables -L INPUT -v --line-numbers
Chain INPUT (policy DROP 29718 packets, 3008K bytes)
num pkts bytes target prot opt in out source destination
1 234 131K DROP tcp -- any any anywhere anywhere ctstate NEW tcp flags:!FIN,SYN,RST,ACK/SYN /* protection: non-syn packets */
2 374 45284 DROP all -- any any anywhere anywhere ctstate INVALID /* protection: malformed packets */
3 96 4800 ACCEPT all -- lo any anywhere anywhere /* loopback: compulsory */
4 24 902 ACCEPT icmp -- any any anywhere anywhere icmp echo-request limit: avg 2/sec burst 5 /* ICMP: ping only */
5 3736K 2726M ACCEPT all -- any any anywhere anywhere ctstate RELATED,ESTABLISHED /* Tor: traffic */
6 30 1800 ACCEPT tcp -- any any anywhere anywhere ctstate NEW,ESTABLISHED tcp dpt:XXYYZ /* SSH: global obfuscated */
7 12493 743K ACCEPT tcp -- any any anywhere anywhere tcp dpt:9001 /* Tor: OR */
8 7948 423K ACCEPT tcp -- any any anywhere anywhere tcp dpt:9030 /* Tor: Dir */
The server seems to work like a charm, but I may be simply over-confident, for the lack of a better word.
ufwbefore? You indicate that you have little networking experience, andufwis more suitable for beginners imo(new) not synit's better to reject with tcp reset, since they are not necessarily caused by malicious intention and simply dropping them can cause some "valid" connection to hang unnecessarily or so. If you want to make the change, you can consider having a "leveled" approach, like having syn check and the reset in an additional chain and make only what fulfilled other criteria (e.g. port matching) togotothat chain, so that it only tcp reset for those "possibly wanted/valid" traffics.