Last-modified: 2013-03-16 (土) 14:40:07
iptables/CentOSへのインストール

概要

起動時にフィルタが適用されるようにする。

方法

  1. /root/bin/iptables.shを作成。
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
    
    -
    !
    -
    |
    !
    -
    !
     
     
     
     
    -
    |
    !
     
    -
    !
    -
    !
     
     
     
    -
    |
    |
    |
    !
    -
    |
    |
    |
    !
    -
    !
    -
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    !
    -
    |
    |
    !
    -
    !
    -
    !
    -
    !
     
    -
    |
    |
    |
    !
    -
    !
    -
    !
    -
    |
    |
    !
     
    
    #!/bin/sh
     
    # ***変数***
    # 省略
     
    # ***モジュール読み込み***
    modprobe ipt_state
    modprobe ipt_LOG
    modprobe ipt_REJECT
    modprobe ipt_limit
     
    # ***クリア***
    # 全ルール削除
    iptables -F
    iptables -X
    # パケットカウンタクリア
    iptables -Z
    # デフォルト拒否
    iptables -P INPUT DROP
    iptables -P OUTPUT DROP
    iptables -P FORWARD DROP
     
    # ************************************
    # チェイン作成
    # ************************************
    # 省略(好きなようにルールをまとめる)
     
    # ************************************
    # INPUT
    # ************************************
    # ローカルホストからの入力は全て許可
    iptables -A INPUT -i lo -j ACCEPT
    # 接続済みのものは全て許可
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    # サービス
    #iptables -A INPUT -p tcp --dport 21 -m state --state NEW -j ACCEPT     # FTP
    #iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT      # SSH
    #iptables -A INPUT -p tcp --dport 25 -m state --state NEW -j ACCEPT     # SMTP
    #iptables -A INPUT -p tcp --dport 53 -m state --state NEW -j ACCEPT      # DNS
    #iptables -A INPUT -p udp --dport 53 -m state --state NEW -j ACCEPT      # DNS
    #iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT     # HTTP
    #iptables -A INPUT -p udp --dport 110 -m state --state NEW -j ACCEPT    # POP3
    #iptables -A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT    # HTTPS
    #iptables -A INPUT -p tcp --dport 465 -m state --state NEW -j ACCEPT    # SMTP-SSL
    #iptables -A INPUT -p tcp --dport 995 -m state --state NEW -j ACCEPT    # POP3-SSL
    # AUTHリクエストは、パフォーマンスを考慮しDROPではなくREJECT
    iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset
    # その他はルールは省略(上で作成したチェインを通す)
    # ・・・
    # ブロードキャスト宛てパケットはログを出さずに破棄
    iptables -A INPUT -d 255.255.255.255 -j DROP
    # マルチキャスト宛てパケットはログを出さずに破棄
    iptables -A INPUT -d 224.0.0.1 -j DROP
    # DropboxのLanSyncパケットはログを出さずに破棄
    iptables -A INPUT -p udp --dport 17500 -j DROP
    # 上記ルール以外はログを記録し破棄
    iptables -A INPUT -j LOG --log-prefix "UNDEFIND_INPUT: " -m limit --limit 1/s --limit-burst 10
     
    # ************************************
    # OUTPUT
    # ************************************
    # ローカルホストからの出力は全て許可
    iptables -A OUTPUT -o lo -j ACCEPT
    # 新規接続を許可
    iptables -A OUTPUT -m state --state NEW -j ACCEPT
    # 接続済みのものは全て許可
    iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    # その他はルールは省略(上で作成したチェインを通す)
    # ・・・
    # 上記ルール以外はログを記録し破棄
    iptables -A OUTPUT -j LOG --log-prefix "UNDEFIND_OUTPUT: "-m limit --limit 1/s --limit-burst 10
    
  2. 実行権限を付加。
    Everything is expanded.Everything is shortened.
      1
    
     
    
    chmod 700 /root/bin/iptables.sh
  3. 適用出来るか確認。
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
    
    -
    !
     
    -
    !
     
    -
    !
    
    # 現在のルールを確認
    iptables --list
     
    # 適用
    /root/bin/iptables.sh
     
    # 確認
    iptables --list
  4. ルールを保存。
    Everything is expanded.Everything is shortened.
      1
    
     
    
    /etc/init.d/iptables save

検証時の環境

参考