Maintaining a Firewall
In Linux, Uncomplicated Firewall (UFW) acts as a simplified wrapper around the powerful and complex iptables firewall management tool.
By default, iptables firewall rules do NOT persist when a computer reboots. The iptables-persistent package automates saving and restoring iptables firewall rules when a computer boots.
You can use UFW together with iptables-persistent to manage network traffic effectively on computers hosting Cardano Node instances in a stake pool configuration.
Installing iptables-persistent
When using UFW and iptables-persistent, you need to configure UFW to load after iptables-persistent when the computer boots.
To install iptables-persistent and configure the UFW service:
1. Using a terminal window, type:
sudo apt-get install iptables-persistent
2. To configure UFW to load after iptables-persistent, type:
sudo nano /lib/systemd/system/ufw.service
3. In the ufw.service file, edit the following lines in the [Unit] block, and then save and close the file:
Wants=network-pre.target netfilter-persistent.service local-fs.target
After=netfilter-persistent.service local-fs.target
The iptables-persistent package uses netfilter-persistent.service
4. To apply the configuration changes, type:
sudo systemctl daemon-reload
Protecting Against Denial-of-Service Attacks
A Denial-of-Service (DoS) attack may force a computer offline. To protect the relay nodes in your stake pool configuration against basic DoS attacks, you can create an iptables rule on each relay that limits the number of connections from the same IP address to the port where Cardano Node listens.
To create a persistent iptables rule to protect against DoS attacks:
1. To ensure that iptables starts with an empty ruleset, using a terminal window type:
sudo ufw disable
2. Reboot the computer.
3. Using a terminal window, type the following commands where <CardanoNodePort> is the port number where the Cardano Node instance running on the local computer listens and <MaxConnections> is the maximum number of connections to allow from an IP address:
sudo iptables -I INPUT -p tcp -m tcp --dport <CardanoNodePort> --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above <MaxConnections> --connlimit-mask 32 --connlimit-saddr -j REJECT --reject-with tcp-reset
sudo ip6tables -I INPUT -p tcp -m tcp --dport <CardanoNodePort> --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above <MaxConnections> --connlimit-mask 128 --connlimit-saddr -j REJECT --reject-with tcp-reset
If you follow the How to Set Up a Cardano Stake Pool guide to create a stake pool having one relay node, then <CardanoNodePort> is 6000 and using the value 5 for <MaxConnections> is sufficient. If you set up additional relays, then depending on your network each Cardano Node instance may need to listen using a different port number.
4. To verify the rules that you applied in step 3, type:
sudo iptables -vnL
sudo ip6tables -vnL
5. To save the current iptables rules to persist when the computer reboots, type:
sudo su -c 'iptables-save > /etc/iptables/rules.v4'
sudo su -c 'ip6tables-save > /etc/iptables/rules.v6'
A single iptables rule may not protect against a more complex DoS attack.
For an example showing how you may safeguard against a range of Distributed Denial-of-Service (DDoS) attack vectors, refer to the script that Carden Pool [CRPL] offers.
Configuring Uncomplicated Firewall
By default, when enabled UFW denies all incoming traffic and allows outgoing traffic. To get started in the How to Set Up a Cardano Stake Pool guide, on computers in your stake pool configuration you need to configure UFW to allow incoming traffic to the following applications:
- SSH Server
- Cardano Node
To configure UFW on computers in your stake pool configuration:
1. Prior to enabling UFW, type the following command where <PortNumber> is the port where the SSH Server listens:
sudo ufw allow <PortNumber>/tcp
The command opens the port where the SSH Server listens to all TCP traffic.
2. On a relay node, type the following command where <PortNumber> is the port where the local instance of Cardano Node listens:
sudo ufw allow <PortNumber>/tcp
On the block-producing node, type the following command where <IPAddress> is the static IP address of a relay node in your stake pool configuration, and <PortNumber> is the port where the local instance of Cardano Node listens:
sudo ufw allow proto tcp from <IPAddress> to any port <PortNumber>
The command that you type on the block producer opens the port where Cardano Node listens to TCP traffic only from the IP address that you specify.
3. On the block-producing node, if your stake pool configuration uses multiple relay nodes, then repeat step 2 for each relay.
4. To enable UFW, type:
sudo ufw enable
5. To display the detailed status of UFW, type:
sudo ufw status verbose
6. Repeat the procedure for each computer in your stake pool configuration.
For more details on using UFW, refer to the manual page for example.
Verifying Port Status
To verify that ports are open or closed on computers in your stake pool configuration, use an online service such as you get signal or CanYouSeeMe.org
Validating Ports
To maintain a secure server, regularly review the ports on your computer that are open and listening for incoming connections. Ensure that all open ports are authorized and needed.
To display a detailed list of active network connections:
- Using a terminal window, type:
ss -tulpn
Listing Connections to a Port
Listing all IP addresses connecting to a specific port on a computer may also be useful.
To list all current connections to a port:
- Using a terminal window, type the following command where
<PortNumber>is the port number for which you want to list connections on the local computer:
sudo netstat -enp | grep ":<PortNumber>" | awk {'print $5'} | cut -d ':' -f 1 | sort | uniq -c | sort
In Linux, the pipe symbol (|) is an operator used to redirect the output of one command as the input of another command.
In order, the command above:
1. netstat -enp—Retrieves a text list of all network connections, with one network connection per line
2. grep ":<PortNumber>"—Using the list of all network connections as input, filters to include only connections containing the text :<PortNumber>
3. awk {'print $5'}—Retrieves the fifth field in each remaining line of text, using one or more SPACE or TAB characters as the separator, effectively retrieving the remote IP address of each connection
4. cut -d ':' -f 1—Retrieves the first field in each line, using a colon character (:) as the separator, effectively dropping the port number from each remote IP address.
5. sort—Sorts the remaining text list of IP addresses alphabetically
6. uniq -c—Merges adjacent matching lines, prefixed by the number of occurrences
7. sort—Sorts the merged list by the number of occurrences followed by IP address, from lowest to highest
Conclusion
You have now configured a basic firewall to help secure your stake pool.
As you work through the How to Set Up a Cardano Stake Pool guide, periodically you may need to adjust UFW rules to allow additional applications that you may install on computers in your stake pool configuration to function.
Continue learning about best practices for hardening Ubuntu, and always aim to minimize potential entry points that may be used to attack computers comprising your Cardano stake pool.