iptablesの設定ファイルをシェルスクリプトを利用して動的に作成


投稿日:2013年11月6日
  • 89
  • 0



各種攻撃からの防御方法と、シェルスクリプトを利用した設定方法を解説


iptables_catch02
DosLinux

IPIP


2
iptables

IP Spoofing
Ping of Death
Ping Flood
Smurf + 
SYN flood
使Auth/IDENT113
IPIP
IP




iptables


2


iptables2

1/etc/sysconfig/iptables


2
IPweb



iptables


11

WebDNS


iptablesrootroot
# su - ←「-」を付けてホームディレクトリへ移動
# vi iptables.sh ←iptables.shを新規作成

vi使稿


bashshebang
#!/bin/bash

iptables


iptables
CentOSstopUbuntu/sbin/iptables -F
/etc/rc.d/init.d/iptables stop


iptables

#ポリシーの設定
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

-P INPUT DROP
-P OUTPUT ACCEPT
-P FORWARD DROP

IP Spoofing




IP Spoofing 

WANIP
#IP Spoofing攻撃対策
iptables -A INPUT -i eth0 -s 127.0.0.1/8 -j DROP
iptables -A INPUT -i eth0 -s 10.0.0.0/8 -j DROP
iptables -A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/24  -j DROP

-A INPUT
-ieth0
-sIP
-j DROP


# ifconfig

tshark
# tshark -D

それぞれのIPの意味

127.0.0.1/8 loopbackアドレス
10.0.0.0/8(10.0.0.0~10.255.255.255) クラスAのプライベートIPアドレス
172.16.0.0/12(172.16.0.0~172.31.255.255) クラスBのプライベートIPアドレス
192.168.0.0/16(192.168.0.0~192.168.255.255) クラスCのプライベートIPアドレス
192.168.0.0/24(192.168.0.0~192.168.0.255) クラスCのプライベートIPアドレス

Ping of Death攻撃と対策


Ping of Death  ︿PoDPingPing
IP

PingpingWindows32linux56

ICMP8TCP208485Ping

11
# Ping攻撃対策
iptables -N PING_ATTACK
iptables -A PING_ATTACK -m length --length :85 -j ACCEPT
iptables -A PING_ATTACK -j LOG --log-prefix "[IPTABLES PINGATTACK] : " --log-level=debug
iptables -A PING_ATTACK -j DROP
iptables -A INPUT -p icmp --icmp-type 8 -j PING_ATTACK

2-N PING_ATTACKPING_ATTACK
3-A PING_ATTACKPING_ATTACK
-m lengthlength
--length :8585
-j ACCEPT

4-j LOG
--log-prefix[IPTABLES PINGATTACK] : 
--log-level=debugdebug

5-A PING_ATTACK -j DROP

6-p icmp --icmp-type 8ICPMEcho Messageping
--icmp-type 88echo-request
-j PING_ATTACK

Ping85Ping

Ping Flood


Ping Flood PingPing FloodPing of Death

Ping of DeathPingPing FloodPingPinglength

ping414ping11
# Ping攻撃対策 + Ping Flood攻撃対策
iptables -A PING_ATTACK -p icmp --icmp-type 8 -m length --length :85 -m limit --limit 1/s --limit-burst 4 -j ACCEPT



-m limit
--limit 1/s11
--limit-burst 44--limit 1/s144

85Ping5Ping

Smurf + 


SmurfPingSmurf
PingIP SpoofingSmurf
ブロードキャスト・アドレスとは

接続された全ての機器に対してデータを送信するアドレスのことです。このアドレスにPing(エコーリクエスト)を送ると、同一ネットワークで繋がれた全ての機器が応答メッセージ(エコーリプライ)を送ります。

ブロードキャスト・アドレスの中でも「255.255.255.255」のことをリミテッド・ブロードキャスト・アドレスと呼びます。
同じような働きをするマルチキャストアドレス(224.0.0.1)ディレクティッド・ブロードキャスト・アドレス(環境によって異なる)というアドレスもあります。


IPpingIP1Ping
iptables04

Smurf2


# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:16:3E:13:2F:39
          inet addr:192.168.0.0  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          …

eth0Bcast:IP192.168.0.255

iptables.sh
# Smurf攻撃対策+不要ログ破棄
iptables -A INPUT -d 255.255.255.255 -j DROP
iptables -A INPUT -d 224.0.0.1 -j DROP
iptables -A INPUT -d 192.168.0.255 -j DROP

IP-d-dIPSmurf
IP Spoofing-s

Smurf


Smurf
LinuxPing
PingSmurf

icmp_echo_ignore_broadcasts1Ping

/bin/echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts


sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1

/etc/sysctl.conf
# Disable Broadcast Ping
net.ipv4.icmp_echo_ignore_broadcasts=1

使


iptables.sh
iptablesLinux
# Smurf攻撃対策
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1 > /dev/null
sed -i '/# Disable Broadcast Ping/d' /etc/sysctl.conf
sed -i '/net.ipv4.icmp_echo_ignore_broadcasts/d' /etc/sysctl.conf
echo "# Disable Broadcast Ping" >> /etc/sysctl.conf
echo "net.ipv4.icmp_echo_ignore_broadcasts=1" >> /etc/sysctl.conf

2icmp_echo_ignore_broadcasts1> /dev/null

34sed -i /hoge/dnet.ipv4.icmp_echo_ignore_broadcasts01net.ipv4.icmp_echo_ignore_broadcasts

56/etc/sysctl.conf

Smurf


IP Spoofing-i eth0
DropboxLAN SyncLAN302


LANLAN SyncPingVPS

1

SYN flood


SYN flood()TCP3
PingUDPTCP3

3ウェイ・ハンドシェイクの仕組み

iptables02

1.TCP SYN 
2.TCP SYN SYN ACK 
3.SYN ACK ACK

3ウェイ・ハンドシェイクを利用した攻撃方法

iptables03

1.TCP SYN 
2.SYN ACK ACK
3.ACK



ACKIPTCP SYN ACKIP1Gbps130MB

接続の確率待ちIPを記憶しておく最大数は「/etc/sysctl.conf」の「net.ipv4.tcp_max_syn_backlog」という値で管理されています。
とくに「tcp_max_syn_backlog」が設定されておらず、メモリが128MB以上ある場合は自動で1024に設定されています。ほとんどの場合、この設定で問題ないはずです。

ちなみに設定通りに1024までACKパケット待ちをしてくれるかというと、実際には769しか待ってくれないようです。
実効値 = ( 設定値*0.75 ) + 1
大規模なサイトを運営している場合は環境に合わせた値を設定してください。

何か決めなきゃ駄目ですか。より

SYN cookies


SYN cookiesSYN flood

3SYN ACK cookies
ACKcookieACK

ACK2SYN floodLinux

cookiesCPU

Smurf
# SYN flood攻撃対策でSYN cookiesを有効に設定
sysctl -w net.ipv4.tcp_syncookies=1 > /dev/null
sed -i '/# Enable SYN Cookie/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_syncookies/d' /etc/sysctl.conf
echo "# Enable SYN Cookie" >> /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.conf

使Auth/IDENT113


113Auth/IDENTSendmail使Sendmail113
DROPREJECTSendmailSMTP


# Auth/IDENT用の113番ポートは拒否
iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset

--reject-withtcp-resetTCP RSTTCP RSTTCP

IPIP


DosIPiptables

IPIP
# 拒否IPリストに記載されたIPからのアクセスを拒否する
if [ -s /root/deny_ip ]; then
    for ip in `cat /root/deny_ip`
    do
        iptables -I INPUT -s $ip -j DROP
    done
fi

1if [ -s /root/deny_ip ]/root/deny_ip調
2/root/deny_ipipfor do done
4iptables -I INPUT -s $ip -j DROPIP1-I INPUTfilterINPUT11
fi

IP



SSH

IP IPv4 IP

/IP

 IPv4 


IPIP

JPIP

cron/etc/cron.daily/iplist_check.sh
# vi /etc/cron.daily/iplist_check.sh

cronBash
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# ファイルの取得
wget -q http://nami.jp/ipv4bycc/cidr.txt.gz -P /tmp

# 解凍してリネーム
gunzip -q -f -c /tmp/cidr.txt.gz > /tmp/iplist.new
# /tmp下なので240時間で自動的に削除されますが、念のため削除
rm -f /tmp/cidr.txt.gz

# iplistが存在しているかチェック
if [ -s /tmp/iplist ]; then

 # diffでエラーが出るので実行権限の変更。「-f」を付けて初回にファイルがない場合に対応
 chmod -f 700 iplist.new
 chmod -f 700 iplist

 # 差分チェック(正常にファイルが取得できたかチェック)
 # grep -c パターンに一致した行の行数のみを出力する。diffで差分(違い)のある行は「< hoge」と表示されるため、行頭に「<」がある行をカウントしている。「=」の前後にスペースがあると正常に代入できないので注意。
 # $()の中に記述しているので、変数ipdiffに代入。
 ipdiff=$(diff /tmp/iplist /tmp/iplist.new  | grep -c "^<")

 # 上記が存在していて、差分の数が1000行以上の場合
 # (通常考えられない量の変更=ファイルが正常に取得できていないと判断)
 if [ $ipdiff -gt 1000 ]; then
  # メールでIPリストが正常に取得できなかった由を伝える。
  cat <<EOM | mail -s "iplist_check" info@example.com
   iplist false
EOM
  # 正常に取得できなかったリストを破棄
  rm -f /tmp/iplist.new
 else
  # 正常に取得できた場合ファイル名をiplistに変更
  mv /tmp/iplist.new /tmp/iplist

  # 新しく取得したリストでiptablesの設定スクリプトを実行
  sh /usr/script/iptables.sh > /dev/null
 fi
else
 # 初回実行時は差分をチェックすべきバックアップがないのでそのままファイル名を変更して利用
 mv /tmp/iplist.new /tmp/iplist

 # 新しく取得したリストでiptablesの設定スクリプトを実行
 sh /usr/script/iptables.sh > /dev/null
fi

iptables.sh
iptables.sh/usr/script/


# chmod 755 /etc/cron.daily/iplist_check.sh


iplist_check.shIPiplistiptables.sh
# 特定の国からのアクセスを許可する
if [ -s /tmp/iplist ]; then
 iptables -N ACCEPT_JP_FILTER
 sed -n 's/^JP\t//p' /tmp/iplist | while read address;
 do
  iptables -A ACCEPT_JP_FILTER -s $address -j ACCEPT
 done
fi

1if [ -s /tmp/iplist ]
2-NACCEPT_JP_FILTER
3sed -n s/^JP\t//psJP//p
while read addressaddress
5ACCEPT_JP_FILTERIPACCEPT

JPIP

iptables -A ACCEPT_JP_FILTER -s 223.223.224.0/19 -j ACCEPT

ACCEPTACCEPT_JP_FILTER使

Web80

iptables -A INPUT -p tcp --dport 80 -j ACCEPT_JP_FILTER

4bash\

JPUSIP

sed -n 's/^\(JP\|US\)\t//p' /tmp/iplist | while read address;

ACCEPT_JP_FILTERACCEPT_FILTERACCEPT_COUNTRY_FILTER




20125
中国55.44%ドイツ9.07%インド8.77%エジプト5.73%パキスタン4.26%となっています。

※ドイツはソフトのよってはアップデート等で利用することがあるので、除外しました。

プロレクシック・テクノロジーズ社の「2012年第3四半期DDoS攻撃レポート(pdfファイルです)」より。
# 特定の国からのアクセスを拒否する
if [ -s /tmp/iplist ]; then
 iptables -N DROP_COUNTRY_FILTER
 sed -n 's/^\(CN\|IN\|EG\|PK\)\t//p' /tmp/iplist | while read address;
 do
  iptables -A DROP_COUNTRY_FILTER -s $address -j DROP
 done
 iptables -A INPUT -j DROP_COUNTRY_FILTER
fi

IP

IP0.0.0.080

iptables -N DROP_COUNTRY_FILTER
iptables -A DROP_COUNTRY_FILTER -s 0.0.0.0 -j DROP
iptables -A INPUT -j DROP_COUNTRY_FILTER
iptables -A INPUT -p tcp --dport 80 -j ACCEPT



TCP3

TCP3
SYN floodSYN cookiesTCPACK

TCPACK

TCPUDPDNSPingiptables


# ステートフル・パケットインスペクションで正しいTCPと既に許可された接続を許可
iptables -A INPUT -p tcp ! --tcp-flags SYN,RST,ACK SYN -m state --state NEW -j DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

2--tcp-flags SYN,RST,ACK SYNSYNRSTACKSYN!
-m statestate
--state NEW
-j DROP
TCPSYN

3-m statestate
--state ESTABLISHED
RELATED
-j ACCEPT


TCP

iptables -A INPUT -p tcp -m state --state NEW -j DROP

TCP80

iptables -A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT




使調
# netstat -anp

53named使StateLISTENESTABLISHED
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 127.0.0.1:53                0.0.0.0:*                   LISTEN      1121/named

lo


使使
## ここから個別のサービスで利用するポートを開放する ##

# lo(ループバックインターフェース)の許可
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

-i lo
-o lo

SSH


SSH2222

SSHSSH稿SSH

SSH42424

ACCEPT_JP_FILTER
# SSH用のポートを日本からのみ許可
iptables -A INPUT -p tcp -m state --state NEW --dport 42424 -j ACCEPT_JP_FILTER #ポートは自分の環境に合わせてください

-p tcpTCP
-m state
--state NEW
--dport 4242442424
-j ACCEPT_JP_FILTERACCEPT_JP_FILTER


使

iptables -A INPUT -p tcp -m state --state NEW --dport 42424 -m hashlimit --hashlimit-burst 5 --hashlimit 1/h --hashlimit-mode srcip --hashlimit-htable-expire 3600000 --hashlimit-name ssh-limit -j ACCEPT_JP_FILTER

-m hashlimit
--hashlimit-burst 55
--hashlimit 1/h111155
--hashlimit-mode srcipIP
--hashlimit-htable-expire 18000000IP5
--hashlimit-name ssh-limitssh-limit

15

ISPIP調

http



F5DosWeb
# http用のポート許可
iptables -A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT

20148


ACK,FIN

ACK
# http用のポート許可
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

DNS


DNSDNS Amp

DNS Amp稿DNS AmpBIND DNS

SSHhashlimitIP
DNS AmpIP
IP


SwatchIPIP
hashlimit-htable-expire調recent
# DNS用ポートの許可。DNS Amp攻撃は適宜ログを取る
iptables -N DNSAMP
iptables -A DNSAMP -m recent --name dnsamp --set
iptables -A DNSAMP -m recent --name dnsamp --rcheck --seconds 60 --hitcount 5 -j LOG --log-prefix "[IPTABLES DNSAMP] : " --log-level=debug
iptables -A DNSAMP -m recent --name dnsamp --rcheck --seconds 60 --hitcount 5 -j DROP
iptables -A DNSAMP -j ACCEPT

iptables -A INPUT -p udp -m state --state NEW --dport 53 -i eth0 -j DNSAMP



3-m recentrecent--name dnsamp --setdnsampIP
4dnsamp605
5
6

53TCPDNSUDP512使
UDPDNS Amp使
iptables -A INPUT -p tcp -m state --state NEW --dport 53 -j ACCEPT

smtps


465
# smtps用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 465 -j ACCEPT

imaps(pop3 protocol over TLS/SSL)


993
# imaps(pop3 protocol over TLS/SSL)用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 993 -j ACCEPT

pop3s(imap4 protocol over TLS/SSL)


995
# pop3s(imap4 protocol over TLS/SSL)用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 995 -j ACCEPT

smtp tcp


TLSUDP使
# smtp tcp用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 25 -j ACCEPT
iptables -A INPUT -p udp -m state --state NEW --dport 25 -j ACCEPT


使
# サブミッションポート用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 587 -j ACCEPT

https(http protocol over TLS/SSL) tcp


Web
# https(http protocol over TLS/SSL) tcp用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 443 -j ACCEPT

webmin


WebminACCEPT_JP_FILTERwebminudp
webmin/etc/webmin/miniserv.conf
port=10000TCPlisten=10000UDP1000010022
# webmin用のポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 10022 -j ACCEPT_JP_FILTER
iptables -A INPUT -p udp -m state --state NEW --dport 10022 -j ACCEPT_JP_FILTER





# 上記の条件に当てはまらない通信を記録して破棄
iptables -A INPUT -m limit --limit 1/s -j LOG --log-prefix "[IPTABLES DROP INPUT] : " --log-level=debug
iptables -A INPUT -j DROP

--log-prefix [IPTABLES DROP INPUT] : [IPTABLES DROP INPUT] : 

iptables


iptables
# 設定を保存してiptablesを起動
/etc/rc.d/init.d/iptables save
/etc/rc.d/init.d/iptables start

iptables.shvi



# sh iptables.sh

IPiplist_check.sh
iplist_check.shiptables.sh
# mkdir /usr/script/
# cp iptables.sh /usr/script/iptables.sh
# chmod 755 /usr/script/iptables.sh ←実行権限を与える

iplist_check.shiptables.sh

CPU使iplist_check.sh/etc/cron.weekly


iptables


-L-niptables
# iptables -nL >> iptables
# vi iptables

iptables.sh


#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

/etc/rc.d/init.d/iptables stop

# ポリシーの設定
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

# IP Spoofing攻撃対策
iptables -A INPUT -i eth0 -s 127.0.0.1/8 -j DROP
iptables -A INPUT -i eth0 -s 10.0.0.0/8 -j DROP
iptables -A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/24  -j DROP

# Ping攻撃対策
iptables -N PING_ATTACK
# Ping攻撃対策 + Ping Flood攻撃対策
iptables -A PING_ATTACK -m length --length :85 -m limit --limit 1/s --limit-burst 4 -j ACCEPT
iptables -A PING_ATTACK -j LOG --log-prefix "[IPTABLES PINGATTACK] : " --log-level=debug
iptables -A PING_ATTACK -j DROP
iptables -A INPUT -p icmp --icmp-type 8 -j PING_ATTACK

# Smurf攻撃対策+不要ログ破棄
iptables -A INPUT -d 255.255.255.255 -j DROP
iptables -A INPUT -d 224.0.0.1 -j DROP
iptables -A INPUT -d 192.168.0.255 -j DROP

# Smurf攻撃対策
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1 > /dev/null
sed -i '/# Disable Broadcast Ping/d' /etc/sysctl.conf
sed -i '/net.ipv4.icmp_echo_ignore_broadcasts/d' /etc/sysctl.conf
echo "# Disable Broadcast Ping" >> /etc/sysctl.conf
echo "net.ipv4.icmp_echo_ignore_broadcasts=1" >> /etc/sysctl.conf

# SYN flood攻撃対策でSYN cookiesを有効に設定
sysctl -w net.ipv4.tcp_syncookies=1 > /dev/null
sed -i '/# Enable SYN Cookie/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_syncookies/d' /etc/sysctl.conf
echo "# Enable SYN Cookie" >> /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.conf

# Auth/IDENT用の113番ポートは拒否
iptables -A INPUT -p tcp --dport 113 -i eth0 -j REJECT --reject-with tcp-reset

# 拒否IPリストに記載されたIPを拒否する
if [ -s /root/deny_ip ]; then
    for ip in `cat /root/deny_ip`
    do
        iptables -I INPUT -s $ip -j DROP
    done
fi

# 特定の国からのアクセスを許可する
if [ -s /tmp/iplist ]; then
     iptables -N ACCEPT_JP_FILTER
     sed -n 's/^JP\t//p' /tmp/iplist | while read address;
     do
          iptables -A ACCEPT_JP_FILTER -s $address -j ACCEPT
     done
fi

# 特定の国からのアクセスを拒否する
if [ -s /tmp/iplist ]; then
     iptables -N DROP_COUNTRY_FILTER
     sed -n 's/^\(CN\|IN\|EG\|PK\)\t//p' /tmp/iplist | while read address;
     do
          iptables -A DROP_COUNTRY_FILTER -s $address -j DROP
     done
     iptables -A INPUT -j DROP_COUNTRY_FILTER
fi

# ステートフル・パケットインスペクションで正しいTCPと既に許可された接続を許可
iptables -A INPUT -p tcp ! --tcp-flags SYN,RST,ACK SYN -m state --state NEW -j DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

## ここから個別のサービスで利用するポートを開放する ##

# lo(ループバックインターフェース)の許可
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# SSH用のポートを日本からのみ許可
iptables -A INPUT -p tcp -m state --state NEW --dport 424242 -m hashlimit --hashlimit-burst 10 --hashlimit 1/h --hashlimit-mode srcip --hashlimit-htable-expire 3600000 --hashlimit-name ssh-limit -j ACCEPT_JP_FILTER

# http用のポート許可
# iptables -A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# DNS用ポートの許可。DNS Amp攻撃は適宜ログを取る
iptables -N DNSAMP
iptables -A DNSAMP -m recent --name dnsamp --set
iptables -A DNSAMP -m recent --name dnsamp --rcheck --seconds 60 --hitcount 5 -j LOG --log-prefix "[IPTABLES DNSAMP] : " --log-level=debug
iptables -A DNSAMP -m recent --name dnsamp --rcheck --seconds 60 --hitcount 5 -j DROP
iptables -A DNSAMP -j ACCEPT

iptables -A INPUT -p udp -m state --state NEW --dport 53 -i eth0 -j DNSAMP

iptables -A INPUT -p tcp -m state --state NEW --dport 53 -j ACCEPT

# smtps用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 465 -j ACCEPT

# imaps(pop3 protocol over TLS/SSL)用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 993 -j ACCEPT

# pop3s(imap4 protocol over TLS/SSL)用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 995 -j ACCEPT

# smtp tcp用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 25 -j ACCEPT

# サブミッションポート用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 587 -j ACCEPT

# https(http protocol over TLS/SSL) tcp用にポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 443 -j ACCEPT

# webmin用のポートを許可
iptables -A INPUT -p tcp -m state --state NEW --dport 10022 -j ACCEPT_JP_FILTER
iptables -A INPUT -p udp -m state --state NEW --dport 10022 -j ACCEPT_JP_FILTER

# 上記の条件に当てはまらない通信を記録して破棄
iptables -A INPUT -m limit --limit 1/s -j LOG --log-prefix "[IPTABLES DROP INPUT] : " --log-level=debug
iptables -A INPUT -j DROP

# 設定を保存してiptablesを起動
/etc/rc.d/init.d/iptables save
/etc/rc.d/init.d/iptables start



現在のページを共有する



現在のページに関連する記事

iptablesの設定ファイルをシェルスクリプトを利用して動的に作成 iptablesをシェルスクリプトで設定していて動作が遅い場合の対処法
iptablesの設定ファイルをシェルスクリプトを利用して動的に作成 iptablesの設定とログファイルのローテーション
iptablesの設定ファイルをシェルスクリプトを利用して動的に作成 コピペから脱出!iptablesの仕組みを理解して環境に合わせた設定をしよう
iptablesの設定ファイルをシェルスクリプトを利用して動的に作成 iptablesで設定したパケットフィルタリングが正しく動作しているかテスト
iptablesの設定ファイルをシェルスクリプトを利用して動的に作成 rsyslogを利用したログファイル作成と、logrotateを利用したログのローテーション
iptablesの設定ファイルをシェルスクリプトを利用して動的に作成 Dovecot IMAP/POP3 Serverで受信用メールサーバを構築
iptablesの設定ファイルをシェルスクリプトを利用して動的に作成 Nginxで特定の国のIPだけを許可する方法

おすすめの記事

Google Feed APIの代替手段としてjQueryだけでRSSを表示する方法

Google Feed APIの代替手段としてjQueryだけでRSSを表示する方法

Google Fontsの日本語フォント「Noto Fonts」の使い方

Google Fontsの日本語フォント「Noto Fonts」の使い方

Linuxの基本の基本。Linuxの基本的なディレクトリ構成

Linuxの基本の基本。Linuxの基本的なディレクトリ構成

Custom Post Type UIでカスタム投稿を作って、誰でも簡単に編集できる投稿画面を作る方法

Custom Post Type UIでカスタム投稿を作って、誰でも簡単に編集…

WordPressで手軽にAdblock対策するならBetter Stop AdBlockで決まり!

WordPressで手軽にAdblock対策するならBetter Stop AdBlockで決…

Web制作の仕組みを根底から覆すかもしれないWix ADIの人工知能

Web制作の仕組みを根底から覆すかもしれないWix ADIの人工知能

初心者でも安全なLAMP環境を構築する方法を解説

初心者でも安全なLAMP環境を構築する方法を解説

fluentdとNorikraでDoS攻撃を遮断し、メールで通知する方法

fluentdとNorikraでDoS攻撃を遮断し、メールで通知する方法


いただいたコメントなど

  1. とんそく のコメント:

    非常にわかりやすく丁寧な記事です。
    ありがとうございます。

    • oxy のコメント:

      コメントありがとうございます。
      メモも兼ねているため長い投稿になってしまいましたが、参考になれば幸いです。

  2. そこらへん のコメント:

    VPS
  3. こんにちは のコメント:

    素晴らしい記事です。ありがとうございます。

  4. 匿名 のコメント:

    すごく役に立つ記事で重宝しています。

    ところで
    iptables -F DROP_COUNTRY_FILTER
    2行目「-F」オプションで新しいユーザー定義チェイン「ACCEPT_JP_FILTER」を作成。
    の2か所の文ですが、-Fではなく共に-Nではありませんか?

    • oxy のコメント:





コメントを残す

コメントは認証制のため、すぐには反映されません。

プログラミングに関する質問は「日本語でプログラミングの悩みを解決するQ&Aサイト sukegra」をご利用ください。