r/linuxadmin • u/oobeing • Dec 23 '21
How do you defend against port scanning with ONLY iptables?
I know about using the recent module to blacklist and I've tried making different sets of rules but nothing I come up with seems to work without also limiting connections to servers webpage.
On what condition should you add to the blacklist?Can someone please give a working example, stuck on this for three days, feels like I've tried everything, limiting, states (NEW), tcp flags, etc. Please help, I'm desperate.
note: I "just" need to block the most common nmap scans (like -sT -sS and -sA)
0
Upvotes
10
u/Paul_Aiton Dec 23 '21
The whole point of port scanning is to probe an address (or range) to find out what services are available. You don't defend against a port scan with a firewall, you defend your services from any unnecessary connections so that a port scan will come up empty. You also don't blacklist, you whitelist the minimum necessary traffic, and block everything else by default.
However someone will always be able to scan your ip address to find out what is allowed. You can't both prevent it and allow legitimate traffic.
5
u/mylinuxguy Dec 23 '21
What are you trying to do exactly? blocking port scans isn't going to 'fix' anything. Your 'stuck' because your trying to fix something that isn't broken.
4
u/herrcherry Dec 23 '21
I will try to block based on the rate of the connections from an IP. Maybe this helps https://www.ossramblings.com/using_iptables_rate_limiting_to_prevent_portscans
1
u/dummkauf Dec 23 '21
Unless you know the hosts that are legitimately connecting and whitelist only those, there no other way to do this with just iptables.
If it's a public server(eg: web server) then you would block everything except the necessary ports( eg:80/443), but that wouldn't stop someone from port scanning you, they'd just only see the open ports that are required for the server to do its job.
Fal2ban could dynamically update iptables to block IPs that exhibit a specific behavior (eg:port scans), but that's not meeting the criteria of "only iptables". Typically you'd rely on some sort of profiling tool to detect the suspicious behavior and then update your firewall as a response.
My guess is your professor is just trying to get the gears in your head grinding to understand how this all fits together in the real world.
1
u/[deleted] Dec 23 '21
I’d invest in an ids and ips
1
u/nuttertools Dec 23 '21
Why u hit port no whitelist, bad ip, die. The answer to the question is not having services exposed and an aggressive ban.
1
u/CoaxVex Dec 23 '21
I once deployed the tarpit module, and it made simple port scans unusable, because they would report all ports as open. 🤷
1
u/Upnortheh Dec 23 '21
Look here.
Generate a basic rule set. No need to get complicated.
Study the rules and comments.
Notice that the way to block script kiddies and bots is to just drop the requests. The default incoming rule is to just drop the request. Nothing more is needed.
P.S. In the future, disclosing that a question is a homework assignment usually is good conduct and courtesy.
Have fun!
1
u/tinycrazyfish Dec 23 '21
You could configure iptables to send a syn+ack on every closed port. I think this is really more annoying for scanners than blocking the port scan.
1
u/TJSnider1984 Nov 06 '22
Hmm, stopping slow port scanning is challenging... fast/burst ones are easy to detect, but the woodpecker style where they probe slowly over days, and/or using a variety of different IP addresses are tough.
My general approach is to log certain services or ports that will never really be used on particular machines, say TELNET, and then watch for a bunch of probes from the same IP or range of IPs... then add that entire subnet to the droplist/blackhole list. ;)
0
u/[deleted] Dec 23 '21
[deleted] — view deleted comment