Iptables is used to set up, maintain, and inspect the tables of IP
packet filter rules in the Linux kernel. Several different tables may be
defined. Each table contains a number of built-in chains and may also contain
user-defined chains.
Each chain is a list of rules which can
match a set of packets. Each rule specifies what to do with a packet that
matches. This is called a 'target', which may be a
Targets
A firewall rule specifies criteria for a packet and a target. If the packet does not match, the next rule in the chain is the examined; if it does match, then the next rule is specified by the value of the target, which can be the name of a user-defined chain or one of the special values ACCEPT, DROP, QUEUE or RETURN.
ACCEPT means to let the packet through. DROP means to drop the packet on the floor. QUEUE means to pass the packet to user space.
(How the packet can be received by a user space process differs by the
particular queue handler. 2.4.x and 2.6.x kernels up to 2.6.13 include the ip_queue queue handler. Kernels 2.6.14 and
later additionally include the nfnetlink_queue queue handler. Packets with a target
of QUEUE will be sent to queue number '0' in this case. Please also see the NFQUEUE target as described later in this man page.) RETURN means stop traversing
this chain and resume at the next rule in the previous (calling) chain. If the
end of a built-in chain is reached or a rule in a built-in chain with target RETURN is matched, the target specified by
the chain policy determines the fate of the packet.
Set
Default Chain Policies
The default chain policy is ACCEPT. Change this to DROP for all INPUT, FORWARD, and OUTPUT chains as shown below.
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
When you make both INPUT, and OUTPUT chain’s default policy as DROP, for
every firewall rule requirement you have, you should define two rules. i.e one
for incoming and one for outgoing.
you trust your internal users, you can omit the last line above. i.e. Do
not DROP all outgoing packets by default. In that case, for every firewall rule
requirement you have, you just have to define only one rule. i.e define rule
only for incoming, as the outgoing is ACCEPT for all packets.
Block a Specific IP-address
Before we proceed further will other examples, if you want to block a
specific ip-address, you should do that first as shown below. Change the
“x.x.x.x” in the following example to the specific ip-address that you like to
block.
iptables -A INPUT -s "$BLOCK_THIS_IP" -j DROP
This is helpful when you find some strange activities from a specific
ip-address in your log files, and you want to temporarily block that ip-address
while you do further research.
You can also use one of the following variations, which blocks only TCP
traffic on eth0 connection for this ip-address.
iptables -A INPUT -i eth0 -s "$BLOCK_THIS_IP" -j DROP
iptables -A INPUT -i eth0 -p tcp -s "$BLOCK_THIS_IP" -j DROP
# iptables -A
INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A FORWARD -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
# iptables -A FORWARD -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
Some Important Examples
MYSQL (Allow Remote Access To Particular IP):
# iptables -A INPUT -s 172.50.3.45 -d 10.1.15.1 -p tcp --dport 3306 -j ACCEPT
SSH:
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 22 -j ACCEPT
Sendmail/Postfix:
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 25 -j ACCEPT
FTP: (Notice how you can specify a range of ports 20-21)
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 20:21 -j ACCEPT
Passive FTP Ports Maybe: (Again, specifying ports 50000 through 50050 in one rule)
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 50000:50050 -j ACCEPT
HTTP/Apache
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 80 -j ACCEPT
SSL/Apache
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 443 -j ACCEPT
IMAP
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 143 -j ACCEPT
IMAPS
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 993 -j ACCEPT
POP3
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 110 -j ACCEPT
POP3S
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 995 -j ACCEPT
Any Traffic From Localhost:
# iptables -A INPUT -d 10.1.15.1 -s 127.0.0.1 -j ACCEPT
ICMP/Ping:
# iptables -A INPUT -d 10.1.15.1 -p icmp -j ACCEPT
-----------------------------------------------------------------------
GLOBAL REJECTS LAST:
Reject
everything else to that IP:
# iptables -A INPUT -d 10.1.15.1 -j REJECT
Or, reject everything else coming through to any
IP:
# iptables -A INPUT -j REJECT
# iptables -A FORWARD -j REJECT-
Saving Rule Sets of Rules
/etc/init.d/iptables save
No comments:
Post a Comment