Reset your IPtables and IP6tables
// IPv4
iptables -F
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
// Add your port
-A INPUT -p tcp --match multiport --dports 80,22,53 -j ACCEPT
-A INPUT -p tcp --match multiport --dports 1024:3000 -j ACCEPT
iptables -I INPUT 1 -i lo -j ACCEPT
// Save
iptables -S
// Save to file
iptables-save > /etc/iptables/rules.v4
// IPv6
ip6tables -F
ip6tables -P INPUT ACCEPT
ip6tables -P OUTPUT ACCEPT
ip6tables -F
ip6tables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
// Add your port
ip6tables -A INPUT -p tcp --match multiport --dports 80,22,53 -j ACCEPT
ip6tables -A INPUT -p tcp --match multiport --dports 1024:3000 -j ACCEPT
ip6tables -I INPUT 1 -i lo -j ACCEPT
// Save
ip6tables -S
// Save to file
ip6tables-save > /etc/iptables/rules.v6
Everytime restart will restore saved IPtables rules via rc.local
// Create a file /etc/rc.local
nano /etc/rc.local
// Paste inside rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
iptables-restore < /etc/iptables/rules.v4
ip6tables-restore < /etc/iptables/rules.v6
exit 0
// Make it excutable
chmod +x /etc/rc.local
// Enable rc.local service
systemctl enable rc-local
// Start
systemctl start rc-local.service
Others useful method
// Accept incoming ping requests
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
sudo iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
// Block a specific IP address
sudo iptables -A INPUT -s "IP_ADDRESS_TO_BLOCK" -j DROP
// Log dropped connection attempts
sudo iptables -A INPUT -j LOG --log-prefix "IPTABLES-DROPPED:" --log-level 7
sudo iptables -A INPUT -j DROP
# Prevent DOS by filling log files.
-A INPUT -m limit --limit 1/second --limit-burst 100 -j LOG --log-prefix "ip6tables[DOS]: "
// Log files will be stored in the following locations:
// Ubuntu and Debian: /var/log/kern.log
// CentOS/RHEL and Fedora: /var/log/messages
// IPv4 & IPv6
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -p esp -j ACCEPT
-A INPUT -m ah -j ACCEPT # IPv6 only
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
-A INPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
# Block remote packets claiming to be from a loopback address.
-A INPUT -s ::1/128 ! -i lo -j DROP
*mangle
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A PREROUTING -m conntrack --ctstate INVALID -j DROP
-A PREROUTING -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j DROP
-A PREROUTING -p tcp -m conntrack --ctstate NEW -m tcpmss ! --mss 536:65535 -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,RST FIN,RST -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,ACK FIN -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags ACK,URG URG -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,ACK FIN -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags PSH,ACK PSH -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,PSH,URG -j DROP
-A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,ACK,URG -j DROP
COMMIT
// End IPv4 & IPv6
Photo by Arnold Leow on Unsplash
References
1. https://kb.gtkc.net/enabling-rc-local-in-debian-9-4-stretch/