Created
August 13, 2013 03:45
-
-
Save saitodev/6217725 to your computer and use it in GitHub Desktop.
iptables example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# -*- coding: utf-8 -*- | |
IPTABLES=/sbin/iptables | |
SERVICE=/sbin/service | |
SSH_PORT=22 | |
$IPTABLES -F # すべてのチェインの内容を削除 | |
$IPTABLES -P INPUT ACCEPT # INPUTチェインのポリシーをACCEPTにする | |
$IPTABLES -P OUTPUT ACCEPT # OUTPUTチェインのポリシーをACCEPTにする | |
$IPTABLES -P FORWARD ACCEPT # FORWARDチェインのポリシーをACCEPTにする | |
$IPTABLES -A INPUT -i lo -j ACCEPT # ループバックデバイスへの接続を許可 | |
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 確立済みと関連するコネクションを許可 | |
#icmpは10回まで新規接続許可それ以降は1秒に1回に制限、6分間無接続なら制限解除 | |
$IPTABLES -A INPUT -p icmp -m hashlimit \ | |
--hashlimit-name icmp_limit --hashlimit 1/sec --hashlimit-burst 10 \ | |
--hashlimit-mode srcip --hashlimit-htable-expire 360000 -j ACCEPT | |
#sshは5回まで新規接続許可それ以降は1分に1回に制限、6分間無接続なら制限解除 | |
$IPTABLES -A INPUT -p tcp --dport $SSH_PORT -m state --state NEW -m hashlimit \ | |
--hashlimit-name ssh_limit --hashlimit 1/min --hashlimit-burst 5 \ | |
--hashlimit-mode srcip --hashlimit-htable-expire 360000 -j ACCEPT | |
# HTTP: 700回まで新規接続許可それ以降は1秒に1回に制限、6分無接続なら制限解除 | |
$IPTABLES -A INPUT -p tcp --dport 80 -m state --state NEW -m hashlimit \ | |
--hashlimit-name http_limit --hashlimit 1/sec --hashlimit-burst 700 \ | |
--hashlimit-mode srcip --hashlimit-htable-expire 360000 -j ACCEPT | |
# ここまでのチェックに引っかからなかったパケットは、ICMPパケット"host-prohibited"を返して接続拒否 | |
$IPTABLES -A INPUT -j REJECT --reject-with icmp-host-prohibited | |
$IPTABLES -A FORWARD -j REJECT --reject-with icmp-host-prohibited | |
$SERVICE iptables save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment