November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

IPTABLES Firewall on Centos

Firewall on Centos OS

#!/bin/sh
#
#

## Set your IP address
MYIP=”192.168.1.108″
#
## Flush rules & reset counters
/sbin/iptables -F
/sbin/iptables -Z
#
## Set policies
/sbin/iptables -P INPUT DROP
/sbin/iptables -P FORWARD DROP
/sbin/iptables -P OUTPUT DROP
#
## Drop all incoming fragments
/sbin/iptables -A INPUT -i eth0 -f -j DROP
#
## Drop outside packets with local addresses – anti-spoofing measure
/sbin/iptables -A INPUT -s $MYIP -i ! lo -j DROP
/sbin/iptables -A INPUT -s 127.0.0.0/8 -i ! lo -j DROP
/sbin/iptables -A INPUT -s 10.0.0.0/8 -i ! lo -j DROP
/sbin/iptables -A INPUT -s 192.168.0.0/16 -i ! lo -j DROP
/sbin/iptables -A INPUT -s 224.0.0.0/4 -i ! lo -j DROP
/sbin/iptables -A INPUT -s 0.0.0.0/8 -i ! lo -j DROP
/sbin/iptables -A INPUT -s 255.255.255.255 -i ! lo -j DROP
/sbin/iptables -A INPUT -s 169.254.0.0/16 -i ! lo -j DROP
/sbin/iptables -A INPUT -s 221.240.102 -i ! lo -j DROP
/sbin/iptables -A INPUT -s 203.215.94.193 -i ! lo -j DROP
/sbin/iptables -A INPUT -s 218.71.137.68 -i ! lo -j DROP
#
## Pass all locally-originating packets
/sbin/iptables -A INPUT -i lo -j ACCEPT
/sbin/iptables -A OUTPUT -o lo -j ACCEPT
#
## Accept ICMP ping echo requests
## (this allows other people to ping your machine, among other things),
/sbin/iptables -A INPUT -p icmp –icmp-type echo-request -j ACCEPT
#
## Accept all traffic from a specific machine with IP x.x.x.x
## replace x.x.x.x with the desired IP, then uncomment the line.
#/sbin/iptables -A INPUT -p tcp -m tcp –syn -s xxx.xxx.xxx.xxx -j ACCEPT
#
## Accept traffic on port p from a specific machine with IP x.x.x.x
## replace p with the desired port number, and replace x.x.x.x with
## the desired IP, then uncomment the line.
#/sbin/iptables -A INPUT -p tcp -m tcp –syn -s x.x.x.x –dport p -j ACCEPT
#
## Accept ftp-data and ftp (ports 20 & 21)
/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 20 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 21 -j ACCEPT
#
## Accept ssh (port 22)
/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 22 -j ACCEPT
#
## Accept telnet (port 23)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 23 -j ACCEPT
#
## Accept smtp (port 25)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 25 -j ACCEPT
## Accept dns (port 53)
/sbin/iptables -A INPUT -p udp -m udp -s 0/0 –dport 53 -d 0/0 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -m tcp -s 0/0 –dport 53 -d 0/0 -j ACCEPT
#
## Accept http (port 80)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 80 -j ACCEPT
#
## Accept pop3 (port 110)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 110 -j ACCEPT
#
## Accept inbound identd (port 113)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 113 -j ACCEPT
## or you can reject and send back a TCP RST packet instead
#/sbin/iptables -A INPUT -p tcp -m tcp –dport 113 -j REJECT –reject-with tcp-reset
#
## Accept imap (port 143)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 143 -j ACCEPT
#
## Accept https (port 443)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 443 -j ACCEPT
#
## Accept smtps (port 465)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 465 -j ACCEPT
## Accept msp (port 587)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 587 -j ACCEPT
#
## Accept SpamAssassin (port 783)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 783 -j ACCEPT
#
## Accept imaps (port 993)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 993 -j ACCEPT
#
## Accept pop3s (port 995)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 995 -j ACCEPT
#
## Accept mysql (port 3306)
#/sbin/iptables -A INPUT -p tcp -m tcp –syn –dport 3306 -j ACCEPT
#
## Allow inbound established and related outside communication
/sbin/iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
#
## Drop outside initiated connections
/sbin/iptables -A INPUT -m state –state NEW -j REJECT
#
## Allow all outbound tcp, udp, icmp traffic with state
/sbin/iptables -A OUTPUT -p tcp -m state –state NEW,ESTABLISHED -j ACCEPT
/sbin/iptables -A OUTPUT -p udp -m state –state NEW,ESTABLISHED -j ACCEPT
/sbin/iptables -A OUTPUT -p icmp -m state –state NEW,ESTABLISHED,RELATED -j ACCEPT
#
## Save rules
service /sbin/iptables save
#
#
echo “/sbin/iptables configuration is complete”
echo “”
echo “Check your rules – /sbin/iptables -L -n”
echo “”

Redhat Linux IPTABLES

==============================================================================================
==============================================================================================
# Generated by iptables-save v1.3.5 on Sat Dec 10 05:28:35 2011
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [59:18308]
:RH-Firewall-1-INPUT – [0:0]
:SSH_CHECK – [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A INPUT -s 10.0.0.0/255.0.0.0 -i eth0 -j LOG –log-prefix “IP DROP SPOOF A: ”
-A INPUT -s 172.16.0.0/255.240.0.0 -i eth0 -j LOG –log-prefix “IP DROP SPOOF B: ”
-A INPUT -s 192.168.0.0/255.255.0.0 -i eth0 -j LOG –log-prefix “IP DROP SPOOF C: ”
-A INPUT -s 224.0.0.0/240.0.0.0 -i eth0 -j LOG –log-prefix “IP DROP MULTICAST D: ”
-A INPUT -s 240.0.0.0/248.0.0.0 -i eth0 -j LOG –log-prefix “IP DROP SPOOF E: ”
-A INPUT -d 127.0.0.0/255.0.0.0 -i eth0 -j LOG –log-prefix “IP DROP LOOPBACK: ”
-A INPUT -p tcp -m tcp –dport 22 -m state –state NEW -j SSH_CHECK
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp –icmp-type 0 -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp –icmp-type 3 -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp –icmp-type 11 -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp –icmp-type 8 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state –state NEW -m tcp –dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state –state NEW -m tcp –dport 443 -j ACCEPT
-A RH-Firewall-1-INPUT -j LOG
-A RH-Firewall-1-INPUT -j DROP
-A SSH_CHECK -m recent –set –name SSH –rsource
-A SSH_CHECK -m recent –update –seconds 60 –hitcount 4 –name SSH –rsource -j DROP
COMMIT
# Completed on Sat Dec 10 05:28:35 2011
==============================================================================================
==============================================================================================

Block Incomming Port 80 except for IP Address 192.168.3.0/24

# /sbin/iptables -A INPUT -p tcp -i eth0 -s ! 192.168.3.0/24 –dport 80 -j DROP

# Generated by iptables-save v1.3.5 on Sat Dec 10 06:17:00 2011
*filter
:INPUT ACCEPT [80:5760]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [78:12568]
-A INPUT -p tcp -m tcp –dport 80 –tcp-flags FIN,SYN,RST,ACK SYN -j ACCEPT
-A INPUT -p tcp -m tcp –dport 443 –tcp-flags FIN,SYN,RST,ACK SYN -j ACCEPT
-A INPUT -p tcp -m tcp –dport 22 –tcp-flags FIN,SYN,RST,ACK SYN -j ACCEPT
-A INPUT -s ! 192.168.3.0/255.255.255.0 -i eth0 -p tcp -m tcp –dport 80 -j DROP
COMMIT
# Completed on Sat Dec 10 06:17:00 2011
==============================================================================================
==============================================================================================

FTP FIREWALL

# Generated by iptables-save v1.3.5 on Wed Jun 10 21:13:16 2009
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [423:45748]
:RH-Firewall-1-INPUT – [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp –icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p esp -j ACCEPT
-A RH-Firewall-1-INPUT -p ah -j ACCEPT
-A RH-Firewall-1-INPUT -d 224.0.0.251 -p udp -m udp –dport 5353 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state –state NEW -m tcp –dport 20 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state –state NEW -m tcp –dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state –state NEW -m tcp –dport 23 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited
COMMIT
==============================================================================================
==============================================================================================

Redhat Basic Firewall

# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT – [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp –icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp –dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8009 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 5902 -j ACCEPT
A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 10050 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 443 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited
COMMIT

==============================================================================================
==============================================================================================

SSH Rules

Using iptables to allow only specific hosts to connect

An alternative to TCP wrappers (although you can use both at the same time) is limiting SSH access with iptables. Here’s a simple example of how you can allow only a specific host to connect to your SSH service:

~# iptables -A INPUT -p tcp -m state –state NEW –source 193.180.177.13 –dport 22 -j ACCEPT

And make sure no one else has access to SSH service:

~# iptables -A INPUT -p tcp –dport 22 -j DROP

~# iptables -A INPUT -p tcp -m state –syn –state NEW –dport 22 -m limit –limit 1/minute –limit-burst 1 -j ACCEPT
~# iptables -A INPUT -p tcp -m state –syn –state NEW –dport 22 -j DROP

In a second example, iptables are set to allow only host 193.180.177.13 to connect to the SSH service. After three failed login tries, iptables allows the host only one login try per minute:

~# iptables -A INPUT -p tcp -s 193.180.177.13 -m state –syn –state NEW –dport 22 -m limit –limit 1/minute –limit-burst 1 -j ACCEPT
~# iptables -A INPUT -p tcp -s 193.180.177.13 -m state –syn –state NEW –dport 22 -j DROP

Conclusion

iptables -N SSH_CHECK
iptables -A INPUT -p tcp –dport 22 -m state –state NEW -j SSH_CHECK
iptables -A SSH_CHECK -m recent –set –name SSH
iptables -A SSH_CHECK -m recent –update –seconds 60 –hitcount 4 –name SSH -j DROP

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>