Maintaining a Firewall
In Linux, Uncomplicated Firewall (UFW) acts as a simplified wrapper around the powerful and complex iptables firewall management tool.
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.
If you have custom iptables commands that UFW does not support, then add your custom rules to /etc/ufw/before*.rules and /etc/ufw/after*.rules files as needed.
For example, to protect relay nodes in the stake pool that you operate from denial-of-service (DoS) or SYN flood attacks you can limit the number of connections from an IP address.
To limit the number of connections from an IP address:
1. On the computer hosting a relay node, using a terminal window type the following command to edit IPv4 rules:
sudo nano /etc/ufw/before.rules
2. In the before.rules file, add the following line prior to the COMMIT statement in the last line of the file, where <PortNumber> is the port where the local instance of Cardano Node listens:
-A INPUT -p tcp -m tcp --dport <PortNumber> --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 5 --connlimit-mask 32 --connlimit-saddr -j REJECT --reject-with tcp-reset
To generate syntax for the before.rules file, type sudo iptables-save to display the current active IPv4 rules. As indicated using the --connlimit-above option, the rule limits the number of connections from the same IP address to 5. If needed, set the connection limit higher so that the stake pool you operate continues to function using your network topology.
3. Save and close the before.rules file.
4. To edit IPv6 rules, type:
sudo nano /etc/ufw/before6.rules
5. In the before6.rules file, add the following line prior to the COMMIT statement in the last line of the file, where <PortNumber> is the port where the local instance of Cardano Node listens:
-A INPUT -p tcp -m tcp --dport <PortNumber> --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 5 --connlimit-mask 128 --connlimit-saddr -j REJECT --reject-with tcp-reset
To generate syntax for the before6.rules file, type sudo ip6tables-save to display the current active IPv6 rules. Set the connection limit as needed so that the stake pool you operate continues to function using your network topology.
6. Save and close the before6.rules file.
7. To verify UFW rules, type:
sudo ufw disable
sudo ufw enable
8. Resolve any errors that UFW displays, and then repeat step 7 until enabling UFW displays no errors.
9. To configure additional relay nodes, repeat steps 1 to 8 for each additional relay node 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 the stake pool that you operate.
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.
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.