Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created September 21, 2010 15:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamipo/589830 to your computer and use it in GitHub Desktop.
Save kamipo/589830 to your computer and use it in GitHub Desktop.
#!/bin/sh
IPTABLES=/sbin/iptables
# 初期化
$IPTABLES -F
# デフォルトルール(以降のルールにマッチしなかった場合に適用するルール)設定
$IPTABLES -P INPUT DROP # 受信はすべて破棄
$IPTABLES -P OUTPUT ACCEPT # 送信はすべて許可
$IPTABLES -P FORWARD DROP # 通過はすべて破棄
# 自ホストからのアクセスをすべて許可
$IPTABLES -A INPUT -i lo -j ACCEPT
# 内部から行ったアクセスに対する外部からの返答アクセスを許可
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# ICMP
$IPTABLES -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type source-quench -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type redirect -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type parameter-problem -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 4 -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type any -j DROP
# SSH
$IPTABLES -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ssh_attack --set
$IPTABLES -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ssh_attack --rcheck --seconds 60 --hitcount 5 --rttl -j LOG --log-prefix 'SSH attack: '
$IPTABLES -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ssh_attack --rcheck --seconds 60 --hitcount 5 --rttl -j DROP
# 外部からのTCP22番ポート(SSH)へのアクセスを許可
$IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT
# 外部からのTCP6667番ポート(IRC)へのアクセスを許可
$IPTABLES -A INPUT -p tcp --dport 6667 -j ACCEPT
# 外部からのTCP80番ポート(HTTP)へのアクセスを許可
$IPTABLES -A INPUT -p tcp --dport 80 -j ACCEPT
# 外部からのTCP5000番ポート(AP)へのアクセスを許可
$IPTABLES -A INPUT -p tcp --dport 5000 -j ACCEPT
# 上記のルールにマッチしなかったアクセスは破棄
$IPTABLES -A INPUT -j DROP
$IPTABLES -A FORWARD -j DROP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment