Skip to content

Instantly share code, notes, and snippets.

@saitodev
Created August 16, 2013 12:24
Show Gist options
  • Save saitodev/6249444 to your computer and use it in GitHub Desktop.
Save saitodev/6249444 to your computer and use it in GitHub Desktop.
#!/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 # 確立済みと関連するコネクションを許可
$IPTABLES -A INPUT -p icmp -j ACCEPT # ICMPパケットを許可
$IPTABLES -A INPUT -p tcp --dport $SSH_PORT -j ACCEPT # SSHを許可
$IPTABLES -A INPUT -p tcp --dport 80 -j ACCEPT # HTTP
# ここまでのチェックに引っかからなかったパケットは、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