Difficulties to configure #21
-
Hello ! I am trying to make geo-nft working on my server. The software works quite well : I have followed all the instructions of the User Guide and face no error at all. My problem is related to the adaptation of the configuration file to my needs. I am running a machine which is hosting a web server (http + https ports) and an email server (imap2, imaps, submission, smtp ports). I want to specify the rules regarding the usage of the ports :
My goal is to block attacks coming from other countries on my web server and ssh. I've tried several solutions to make my configuration working but do not succeed to find the best solution. I am not very familiar with nftables and firewall rules and only achieve degrading the suggested template without any efficience... Can anyone help me to write the best solution for Thank you in advance for your precious help ! Best regards, Arnaud |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I'm probably not the best person to help you with writing general firewall rules for your server. However, I can point you to a few resources to get you started. The nftables wiki is a great source of information, and contains firewall examples. The ArchWiki also has some nice firewall examples. Red Hat has a useful nftables guide that can help you. Red Hat also has an excellent SSH article entitled "Eight ways to protect SSH access on your system" that has good tips. Another great resource of information is Linuxsecurity.com. You can also request help on the nftables mailing list. I'm not sure if you're using IPv4, IPv6 or both. If you can let us know which IP protocol(s) you're using then it will help others to provide more specific answers. SSHFor your SSH firewall rule, see this post for an example. If using IPv4, then add a new empty SSH set at the top of your main table in your
If using IPv6, then add a new empty SSH set at the top of your main table in your
Next, add a rule to your input chain in If using IPv4, add this line to the input chain in your
If using IPv6, add this line to the input chain in your
If using both IPv4 and IPv6, then add both of the above rules to replace the existing SSH rule. Save your
Next, add define line(s) to If using IPv4 only, add this line to
If using IPv6 only, add this line to
If using both IPv4 and IPv6, add these two lines to
Save the
Make sure that you have an alternate way to access your server in case you lock yourself out with any of the above rules. :) HTTP/HTTPSFor HTTP/HTTPS you can create new empty sets as you did for SSH, or you can just use the existing SSH sets that you created since you'll be using the same country codes for both SSH and HTTP/HTTPS. Assuming that you use the SSH sets for HTTP/HTTPS as well, add a rule to your input chain in If using IPv4, add this line to the input chain in your
If using IPv6, add this line to the input chain in your
Save your
All of the sample firewall rulesets in the wiki have a rule in the input and output chain to accept localhost traffic, so that part should work. All of the sample firewall rulesets in the wiki have an output rule to allow outgoing HTTP/HTTPS packets. You'll need to comment out the rule in the output chain that's just above the rule allowing outgoing HTTP/HTTPS packets so that it works the way you want it to. Comment these rules out by placing a '#' mark at the beginning of the rule in your
To close all other outgoing ports you'll need to change the last rule in the output chain to drop all other outgoing traffic rather than accepting it. Change the last rule in the output chain to look like this:
Also change the default policy at the top of the output chain to 'drop', so that it looks like this:
Save your
You'll probably want to test the server now to make sure that everything works, and that your server can get operating system updates, correct time, etc. I don't have experience with email server rules, so maybe someone else can answer that part. That was a long post, please let me know if there are any typos or information that needs to be corrected. I hope this helps you out. Thanks. |
Beta Was this translation helpful? Give feedback.
-
I see an issue with your ruleset that might be causing problems. The HTTP/HTTPSIf HTTP/HTTPS still isn't working, place a basic rule just above the existing HTTP/HTTPS rule in your input chain to allow all Web traffic in to ports 80 and 443. This allows you to verify that the Web server is working with all of your other rules.
Then list your input chain using the command shown in the troubleshooting section below. See if the new HTTP/HTTPS rule that you just added is matching packets. Also check that your Mail ServerI don't have any experience with email server rules, but you can check a few general things. Verify that the service names in your email rule are correct, or replace them with the actual port numbers as a test. Also check which ports that your email server is listening to using commands in the troubleshooting section below. General NoteYou'll also want to add rules to protect against port scans and other types of attacks too numerous to list here. Search online for help with that, as I'm not an expert by any means. TroubleshootingThe following commands can help you troubleshoot your nftables ruleset. They will allow you to see how many packets match each firewall rule that uses a counter. Since you're using both IPv4 and IPv6, the example commands below use the To list your input chain:
To list your output chain:
To list the set
To list the set
To list all sets in table inet filter:
To list your entire ruleset including set elements:
To list your entire ruleset without displaying set elements:
If you want to see which packets don't match any of the rules in your input chain, add a rule just above the final rule so that it logs some of those packets (with a prefix) to your system log. Then you can grep your system log using the prefix to see which packets don't match your input chain rules. This is the rule to add, which also shows the existing final rule in your input chain:
Then grep your system log for a list of packets that don't match any input rules. Substitute the pathname of your system log if different from the example below.
You can also add a similar rule to other chains, changing 'input' in the prefix to match the chain name. Another good troubleshooting tip is to list all of the ports that your server is listening to, so that you can verify that your firewall rules match those ports, and protect all of them.
You can print service names instead of port numbers by adding the -n flag.
Nmap can also be used to give information about your server ports.
If you find iptables firewall rules online that you would like to use with nftables, you can sometimes convert those rules to nftables format automatically using the iptables-tranlate or ip6tables-translate tools. Hopefully that gets you going in the right direction. Thanks. |
Beta Was this translation helpful? Give feedback.
I see an issue with your ruleset that might be causing problems. The
ingress
chain is part of thenetdev
table, which you removed, so you can also remove theingress
chain. Test to see if things are working after that change. I'll probably remove thenetdev
table from the Wiki examples since it's confusing for many who may be new to nftables.HTTP/HTTPS
If HTTP/HTTPS still isn't working, place a basic rule just above the existing HTTP/HTTPS rule in your input chain to allow all Web traffic in to ports 80 and 443. This allows you to verify that the Web server is working with all of your other rules.