[CentOS] Centos 7 (using iptables) removed firewalld

Steve Frazier

sfrazier1111 at yahoo.com
Fri Jun 1 12:01:02 UTC 2018


 Hello, 
I hope that I can ask some questions on this mailing list about IPTables.
I am more familiar with IPTABLES instead of FIREWALLD.  I disabled FIREWALLD and installed iptables-services.
I have put together a script that I found on the web on how to set up a good set of IPTABLES rules to keep my server as secure as possible.
I have two NICs. ETH0 and ETH1. ETHO is the internet and ETH1 is my internal network. I want to allow all ports from internal to external. I want to block pretty much all ports from the outside to the inside except from specific IP addresses. I also want to allow UDP ports 10000-20000 from anywhere all other ports are only allowed from specific IP addresses.
Here is my script, if you don't mind could you make any corrections on what I should do or not do in my example? Perhaps the order in which I run my script.
I have attached the full script with the comments for what I am trying to do. Please look at it and help me if you would, please.
I am interested in the order that I have my rules (any suggestions or changes you would make if you were trying to use it) also, the items that I have included.
There are a few IP addreses (mine) that I am allowing all opens incoming/outgoing. Otherwise only specific ports are allowed for specific IP addresses.
TIA.
Steve

#!/bin/bash
######################################################################################################################## Exterior (Internet) Ethernet 0#######################################################################################################################EXIF="eth0"
######################################################################################################################## Interior (My network) Ethernet 1#######################################################################################################################IXIF="eth1"

######################################################################################################################## 1. Delete all existing rules#######################################################################################################################iptables -F
######################################################################################################################## 2. Set default chain policies#######################################################################################################################iptables -P INPUT DROPiptables -P FORWARD DROPiptables -P OUTPUT DROP
######################################################################################################################## 3. Block a specific ip-address########################################################################################################################BLOCK_THIS_IP="x.x.x.x"########################################################################################################################iptables -A INPUT -s "$BLOCK_THIS_IP" -j DROP


######################################################################################################################## 4. Allow ALL incoming SSH########################################################################################################################iptables -A INPUT -i $EXIF -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 5. Allow incoming SSH only from a specific network########################################################################################################################iptables -A INPUT -i $EXIF -p tcp -s 192.168.200.0/24 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 6. Allow incoming HTTP########################################################################################################################iptables -A INPUT -i $EXIF -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
# Allow incoming HTTPS#iptables -A INPUT -i $EXIF -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 7. MultiPorts (Allow incoming SSH, HTTP, and HTTPS)########################################################################################################################iptables -A INPUT -i $EXIF -p tcp -m multiport --dports 22,80,443 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp -m multiport --sports 22,80,443 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 8. Allow outgoing SSH########################################################################################################################iptables -A OUTPUT -o $EXIF -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A INPUT -i $EXIF -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 9. Allow outgoing SSH only to a specific network########################################################################################################################iptables -A OUTPUT -o $EXIF -p tcp -d 192.168.101.0/24 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A INPUT -i $EXIF -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 10. Allow outgoing HTTPS########################################################################################################################iptables -A OUTPUT -o $EXIF -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A INPUT -i $EXIF -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 11. Load balance incoming HTTPS traffic########################################################################################################################iptables -A PREROUTING -i $EXIF -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 0 -j DNAT --to-destination 192.168.1.101:443#iptables -A PREROUTING -i $EXIF -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 1 -j DNAT --to-destination 192.168.1.102:443#iptables -A PREROUTING -i $EXIF -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 2 -j DNAT --to-destination 192.168.1.103:443
######################################################################################################################## 12. Ping from inside to outside#######################################################################################################################iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPTiptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
######################################################################################################################## 13. Ping from outside to inside#######################################################################################################################iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPTiptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
######################################################################################################################## 14. Allow loopback access#######################################################################################################################iptables -A INPUT -i lo -j ACCEPTiptables -A OUTPUT -o lo -j ACCEPT
######################################################################################################################## 15. Allow packets from internal network to reach external network.######################################################################################################################## if $EXIF is connected to external network (internet)# if $IXIF is connected to internal network (192.168.20.0/23)iptables -A FORWARD -i $IXIF -o $EXIF -j ACCEPT

######################################################################################################################## 15a. Masqurading#######################################################################################################################
iptables -t nat -A POSTROUTING -o $EXIF -j MASQUERADEiptables -A FORWARD -i $EXIF -o $IXIF -m state --state RELATED,ESTABLISHED -j ACCEPTiptables -A FORWARD -i $IXIF -o $EXIF -j ACCEPT

######################################################################################################################## 16. Allow outbound DNS########################################################################################################################iptables -A OUTPUT -p udp -o $EXIF --dport 53 -j ACCEPT#iptables -A INPUT -p udp -i $EXIF --sport 53 -j ACCEPT
######################################################################################################################## 17. Allow Voip Connections#########################################################################################################################specific provider abc company#
iptables -A INPUT -i $EXIF -s myvoipprovider1 -p udp -m udp --dport 5060 -j ACCEPTiptables -A INPUT -i $EXIF -s myvoipprovider2 -p udp -m udp --dport 5060 -j ACCEPT
########################################################################################################################### 17a. Allow Full Access from my outside  IPs########################################################################################################################allow connections from my machines full access#iptables -A INPUT -i $EXIF -s myips -j ACCEPTiptables -A INPUT -i $EXIF -s myips -j ACCEPTiptables -A INPUT -i $EXIF -s myips -j ACCEPT######################################################################################################################### 17b. Allow Full Access from my inside  IPs#######################################################################################################################iptables -A INPUT -i $IXIF -s 192.168.20.0/23 -j ACCEPT
######################################################################################################################### 18. Allow rsync from a specific network########################################################################################################################iptables -A INPUT -i $EXIF -p tcp -s specificip --dport 873 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 873 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 19. Allow MySQL connection only from a specific network########################################################################################################################iptables -A INPUT -i $EXIF -p tcp -s specificip --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 20. Allow Sendmail or Postfix########################################################################################################################iptables -A INPUT -i $EXIF -p tcp --dport 25 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 25 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 21. Allow IMAP and IMAPS########################################################################################################################iptables -A INPUT -i $EXIF -p tcp --dport 143 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 143 -m state --state ESTABLISHED -j ACCEPT
#iptables -A INPUT -i $EXIF -p tcp --dport 993 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 993 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 22. Allow POP3 and POP3S########################################################################################################################iptables -A INPUT -i $EXIF -p tcp --dport 110 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 110 -m state --state ESTABLISHED -j ACCEPT
#iptables -A INPUT -i $EXIF -p tcp --dport 995 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 995 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 23. Prevent DoS attackiptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT#######################################################################################################################
######################################################################################################################## 24. Port forwarding 422 to 22########################################################################################################################iptables -t nat -A PREROUTING -p tcp -d 192.168.102.37 --dport 422 -j DNAT --to 192.168.102.37:22#iptables -A INPUT -i $EXIF -p tcp --dport 422 -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -A OUTPUT -o $EXIF -p tcp --sport 422 -m state --state ESTABLISHED -j ACCEPT
######################################################################################################################## 25. Log dropped packets#######################################################################################################################iptables -N LOGGINGiptables -A INPUT -j LOGGINGiptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPTables Packet Dropped: " --log-level 7iptables -A LOGGING -j DROP








More information about the CentOS mailing list