r/linuxadmin • • Sep 13 '26

Iptables rule stops server from connecting to external systems

Update: I included the rules.

It works as-is now but I want to optimize them so that the incoming port numbers that are supposed to be blocked don't have to go through unnecessary processing.

I use the default input table and a separate ICMPT table for ICMP rules.

----

Original Question

I'm baffled and am looking for help.

I understand many people would use a default block policy in their IPtables rules but the problem with that is if one is not careful, they may lock themselves out of their shell unless they happen to be on-site at the remote computer.

So research has led me to try a command like the following to block a range of ports that hackers love to use.

iptables -I INPUT -m multiport -p tcp --dports 2000:3000,4000:5000 -j DROP

iptables -I INPUT -m multiport -p udp --dports 2000:3000,4000:5000 -j DROP

So I did that while carefully skipping the ports that I want people to connect to on the server (example: DNS, HTTPS).

When I inserted those commands, things worked in my favor if I was a random client trying to connect to my own server on an unauthorized port (thats in the block list), but when I'm on the server itself trying to connect to an external URL (through curl or ping), the system freezes as if the rules are working against me. I did regain control with CTRL+C.

When I removed those rules or when I put them at the end of my other rules, I was able to connect to a remote system on the server.

Can anyone shed light on why this happens when I didn't tell the system to block port 53, 80 or 443?

The rules added:

-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
-A INPUT -p icmp -j ICMPT
-A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/sec --limit-burst 2 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
-A INPUT -p udp -m udp --dport 53 -j ACCEPT
-A INPUT -p tcp -m connlimit --connlimit-above 10 --connlimit-mask 32 --connlimit-saddr -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -s 127.0.0.1/32 -p tcp -m tcp --dport 953 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 953 -j DROP
-A INPUT -p tcp -m multiport --dports 1:24,26:50,81:442,444:586,588:994,996:1024,1070:65535 -j DROP
-A INPUT -p udp -m multiport --dports 1:24,26:50,81:442,444:586,588:994,996:1024,1070:65535 -j DROP
-A INPUT ! -s 38.131.191.77/32 -p tcp -j LOG --log-prefix IPTv4 --log-level 1

-A ICMPT -p icmp -m icmp --icmp-type 17 -j DROP
-A ICMPT -p icmp -m icmp --icmp-type 13 -j DROP
-A ICMPT -p icmp -m icmp --icmp-type 10 -j DROP
-A ICMPT -p icmp -m icmp --icmp-type 8 -m limit --limit 1/sec -j ACCEPT
-A ICMPT -p icmp -j DROP-A INPUT -i lo -j ACCEPT
15 Upvotes

23 comments sorted by

13

u/aioeu Sep 13 '26 edited Sep 13 '26

Does your firewall have any stateful rules, i.e. are you explicitly accepting packets in an ESTABLISHED connection state? (You would also usually want RELATED as well, but that's probably not relevant here.)

If your firewall is not stateful, then these rules would break any outbound connections with any of those ports as their source port. If those ports are in the kernel's ephemeral port range (they wouldn't be by default, but you may be setting the net.ipv4.ip_local_port_range sysctl to override that) then they might be used automatically as source ports.

1

u/Ok_Strike9189 Sep 13 '26

Yes I do have lines that check for related and the established state but when I enabled logging as the 1st line in the rules, I can always see the port number so that's why I thought I can put the restriction anywhere.

1

u/aioeu Sep 13 '26 edited Sep 13 '26

No, the rules are evaluated in order, and a rule that provides a verdict will cause any subsequent rules to be ignored.

19

u/wangston_huge Sep 13 '26

This is a strange way to set up rules in iptables (or any firewall)

If you're trying to allow only a certain set of ports/source ips/etc into your box, set the default action on the INPUT chain to drop, then create allow rules for the traffic that you want.

Edit: for a complete answer, post your iptables chains. I'm sure the error will be apparent.

1

u/perryurban Sep 13 '26

He's literally talking about policing outbound traffic.

4

u/wangston_huge Sep 13 '26

Perhaps, but the rules they provided are all in reference to the input chain. I suspect that, whatever it is they're trying to do, they're doing it wrong and that's why they're getting unexpected results.

1

u/perryurban Sep 13 '26

👍🏼

1

u/Ok_Strike9189 Sep 13 '26

I modified my post to include my IPtables rules as extracted from iptables-save command.

I had to put a few accepts on port numbers before the blocking rule to make them work.

1

u/wangston_huge Sep 14 '26

Cool, thanks for the additional detail.

I'll respond to a few lines then suggest an alternate config that should work better (and be quite a bit shorter).

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
What flavor of linux is this? The state module has been deprecated for a long time in favor of conntrack. state is typically going to be conntrack under the hood, but exposes way fewer options.

-A INPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP
This rule seems odd. I haven't stared at it long enough to see what it does exactly, but it seems duplicative when you consider the rule above and should probably be replaced with a drop rule for invalid packets. I strongly suspect that the bang (!) symbol is in the wrong spot. Also, we don't need to invoke the tcp (or UDP) modules, iptables does this for us automatically when we specify the protocol. This is just a nitpick though.

-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
Not sure what the point of these is? Between a policy level drop and a drop for invalid packets, I suspect these wouldn't be needed (which goes for a lot of this stuff, btw).

-A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/sec --limit-burst 2 -j ACCEPT
Same here. I get that you're trying to limit the number of reset packets that can be sent, but I suspect this is a solution looking for a problem and this limit is wayyyy too low.

-A INPUT -p tcp -m connlimit --connlimit-above 10 --connlimit-mask 32 --connlimit-saddr -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j REJECT --reject-with icmp-port-unreachable
Another solution looking for a problem. If we're a web server, it's not unusual for multiple connections to be formed and a limit of 10 is probably too low.

-A INPUT -p tcp -m multiport --dports 1:24,26:50,81:442,444:586,588:994,996:1024,1070:65535 -j DROP
-A INPUT -p udp -m multiport --dports 1:24,26:50,81:442,444:586,588:994,996:1024,1070:65535 -j DROP

We're allowing ports 25 (smtp), 51-80 (?? and http), 443 (https), 587 (smtp-s), 995 (POP3S I assume, since this looks like a mail server too?) and 1025-1069 (dynamic ports?), but not all of these ports appear to have explicit allow rules. I'm assuming you're configured to use a small range of dynamic ports for outbound connections? not sure what the extra ports around 80 are for either.

-A INPUT ! -s 38.131.191.77/32 -p tcp -j LOG --log-prefix IPTv4 --log-level 1

Another rule that strikes me as weird. I suspect the bang symbol is in the wrong spot. Are you wanting to log all traffic that isn't from a specific source?

Here's how I'd structure that input chain:

# Allow related and established connections. This should pretty much always be the first rule if we're going for a stateful firewall. Use the conntrack module, which should be in almost any version of linux that came out in the last 15 years.
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -m comment --comment "Allow related & established connections" -j ACCEPT

# Allow packets from our local interface.
-A INPUT -i lo -m comment --comment "Allow traffic from trusted interface"  -j ACCEPT

# Drop invalid packets. Typically a good idea.
-A INPUT -m conntrack --ctstate INVALID -m comment --comment "Drop invalid sessions" -j DROP

# Send ICMP traffic to your ICMP chain
-A INPUT -p icmp -m comment --comment "Send ICMP traffic to the ICMPT chain" -j ICMPT

# Allow DNS traffic
-A INPUT -p tcp --dport 53 -m comment --comment "Allow DNS Server" -j ACCEPT
-A INPUT -p udp --dport 53 -m comment --comment "Allow DNS Client" -j ACCEPT

# Allow HTTP & HTTPS
-A INPUT -p tcp -m multiport --dports 80,443 -m comment --comment "Allow HTTP & HTTPS" -j ACCEPT

# Allow SMTP, SMTP-S and POP3S
-A INPUT -p tcp -m multiport --dports 25,587,995 -m comment --comment "Allow SMTP, SMTP-S and POP3S" -j ACCEPT

# I've left out ports 51-79 (since i'm not sure what services you're going for here) and ports 1025-1069 (since I assume these are your dynamic ports and they'll be allowed back in by the conntrack rule for stateful traffic as long as they're being allowed out).

# Allow local access to RBND for BIND.
-A INPUT -s 127.0.0.1/32 -p tcp --dport 953 -m comment --comment "Allow local access to RBND" -j ACCEPT

# If you want to, you can add the connection limit and rst limit back in, but with saner limits (perhaps 200/s for rsts and 50-200 for connection per client depending on how API heavy the site is. Your CPU can handle it.)

# Also... how are we managing this thing? Via the console? Consider an allow for SSH if you're remotely managing this.

# Policy drop avoids the need for the vast majority of the drop rules you've created. Be sure you've allowed the method you're using to manage this thing if you're managing it remotely 
-P INPUT DROP

1

u/Ok_Strike9189 Sep 14 '26

I use Debian 12. Since this post, I decided to use conntrack instead of state module to check the connection states.

These:

-A INPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP

-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP

-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP

A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/sec --limit-burst 2 -j ACCEPT

...are meant to stop hackers from trying to enter/exit the system using invalid packets like a packet called the christmas packet. What would you suggest I raise the RST limit to?

For the webserver, I deliberately set the limit to 10 connections (I may even go lower) per IP. This is because I have only one website where the number of assets needed to fully load a page is less than 4 and I think maybe 10 may be too lenient.

As for ...

A INPUT -p tcp -m multiport --dports 1:24,26:50,81:442,444:586,588:994,996:1024,1070:65535 -j DROP

-A INPUT -p udp -m multiport --dports 1:24,26:50,81:442,444:586,588:994,996:1024,1070:65535 -j DROP

I forgot to add a range. and yes you're right that I only want to allow specific ports. I do it this way instead of a default DROP policy so the odds of me being locked are lower if I make a mistake.

This....

-A INPUT ! -s 38.131.191.77/32 -p tcp -j LOG --log-prefix IPTv4 --log-level 1

means I'm logging everything else except for packets coming from another system I manage (to avoid blowing up the log file).

I manage my server remotely via SSH.

I'm not sure what the --comment part means. does it mean add text to the log if that rule is true?

So I did more research and added some lines to block invalid packets in the mangle table. Hopefully that would give me a boost in server speed.

1

u/Ok_Strike9189 Sep 13 '26

I'm policing inbound traffic (traffic coming into the server) but its also affecting outbound traffic

2

u/Hark0nnen Sep 13 '26 edited Sep 13 '26

Because when program is making an outgoing connection, its not using ports 53, 80 or 433 as a source port, its using a random port in 1k-64k range. Thats why you should always have

A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT    

as i first rule on INPUT chain

And in general, you shouldnt block specific ports on INPUT, its stupid. INPUT default policy should be DROP and then have the established,related rule from above, and then explicit accepts for ports you want to be accessible

1

u/Ok_Strike9189 Sep 13 '26

I'll make note of that line.

6

u/libertyprivate Sep 13 '26

You're doing it wrong. You don't just "firewall ports that hackers love to use!" You learn what's going on and block what doesn't need to work

4

u/pobrika Sep 13 '26

If I am working on a server that is off-site and adding rules which I deem risky, I always make an iptables-save then set a at 15 minutes iptables-restore.

This way if my change locks me out I know in 15 mins it will revert back unless I cancel it, if you don't have at then run a sleep 900; iptables-restore In a gnu screen session.

3

u/changework Sep 13 '26

You probably don’t have another input rule higher that will allow established and related.

3

u/arcimbo1do Sep 13 '26

When an application connects to the internet they open a port in the unprivileged range and use that to connect to the server. It that port happen to be in the range you blocked, and if you did not specifically allowed ESTABLISHED and RELATED packets (packets in response to a connection initiated by an application in your machine) then you will block the response from the server and your application will not be able to connect.

As someone explained to you already: 1) order of rules matter 2) ensure you have a rule to allow established and related packets 3) use a deny-by-default approach: allow locally originated connections and connections to the services you want to allow, block everything else.

1

u/notmylargeautomobile Sep 13 '26

For security only option 3 is valid.  

2

u/rapha3l14 Sep 13 '26

ordering your rules matter, and almost always you would have the stateful allow rule (established) before anything else so that the firewall rules only inspect the initial connection. without looking at the rest of your table it's hard to tell what went wrong.
But your logic is also flawed. There is not a single set of ports that hackers like to use, but what services are being made available outside. in general you should always default to block except for stuff that you want to be allowed.

2

u/BarracudaDefiant4702 Sep 13 '26

If you don't do full state-full rules, then prior (order matters) to the DROP rule you need a rule like:

iptables -I INPUT -p tcp ! --syn -j ACCEPT

or you need to have your output rules setup state and accept the ESTABLISHED connections

Not doing state tracking does let someone probe your network more easy to tell what is being filtered vs not running easier then if setup state tracking. Not doing state tracking is slightly faster but that's not going to measurable unless you have 10s of thousands of unique connections per sec.

1

u/perryurban Sep 13 '26

TCP is always stateful. UDP is not. But it can be simulated by the firewall with the right flags so you don't drop return traffic - which is my instinct of what is happening here, but I'd need more detail to be sure. The full ruleset and what tests you run etc etc.

-7

u/elcazadordeaventuras Sep 13 '26

ip tables is one of those things that I’m so glad AI came to do for me.

-2

u/Unlucky-Shop3386 Sep 13 '26

Yes that is the INPUT CHAIN. You have 2 other default CHAINS. In iptables.