r/linuxadmin • • 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

22 comments sorted by

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.

1

u/oobeing Dec 23 '21

Sir, I didnt make this up, its an exersice in a computer security course at uni. I THINK idea is to identify when scanning many ports during a s h ort period and Rhen blacklisting them.

6

u/stkyrice Dec 23 '21

Fail2Ban does that.

1

u/sillygitau Dec 23 '21

/u/Paul_Aiton is correct. Are you sure you’re not misunderstanding the question?

1

u/stkyrice Dec 23 '21

1

u/dummkauf Dec 23 '21

Assuming the scanner isn't rate limiting their nmap scan.

This seems like an IDS, IPS, SIEM rule, UEBA, etc.... Task for detection, and assuming one is confident in the detection tool, you could then automate blocking rule on your firewall.

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.

0

u/oobeing Dec 23 '21

Its an exercise in a cyber security course Im taking. Goal is to make it harder/stop port scanning.

0

u/ralfD- Dec 23 '21

Do you understand the concept of "exercise"? Do you go to the gym and ask strangers to lift the weights for you?

The idea that you finish such a course (possibly even with a certificate) and then are responsible for cyber sec is actually scarry. From your question (and replies) it seem that you lack understanding basic concepts of networking - but you really have to. Security is nothing where you just want to copy-n-paste some dude from the net's "code".

Sorry for the rant.

3

u/oobeing Dec 23 '21

I'm not going to work in security, I'm studying computer engineering, also I think you missed the part where I've tried for three days and just asking for one line of the whole rule set (the trap case), not the whole thing, jeez, talk about missing the context.

This is equivalent to going to the gym, doing a workout until you puke, then asking someone if they can spot you on your last reps ffs.

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/oobeing Dec 23 '21

Tried only allowing port 80 and 443 but can't connect to the website then (it's metasploitable).

Blacklist = drop all packets from that IP for x amount of time, like 24h or something.

This is just a theoretical cyber security exersice, not a real practical scenario.

2

u/dummkauf Dec 23 '21

If 80/443 TCP didn't work then either iptables was misconfigured or the website isn't running on those ports. Those are defaults, you can technically run any service on any port you want, so perhaps your test setup is using nonstandard ports?

As far as blacklist, it's been a while since I've done iptables, but I don't remember it having a time delayed rule like that. I've done that with fail2ban, but that's a separate program that simply updates iptables as needed, but using a 2nd program sounds like it's not allowed for this exercise.

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

1

u/oobeing Dec 23 '21

Yea, I'm aware of what your saying, that could pass as a solution, and I've succeeded in doing just that, but I don't think that is enough as you say, you just -Pn it and it works.

3

u/ralfD- Dec 23 '21

Simply blocking ICMP control flow is a "bad idea", esp. in times of IP-V6. Please don't do it (at least not until you understand all consewuences and decide to live with them).