Last-modified: 2017-07-16 (日) 03:11:47
iptables/Debianへのインストール

概要

起動時にフィルタが適用されるようにします。
以下はベースとなるルールで、端末の役割毎にこれを調整して運用します。

方法

  1. /etc/network/if-pre-up.d/iptablesを作成。
    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
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    
    -
    !
    -
    |
    |
    !
     
     
    -
    |
    |
    |
    !
     
     
     
     
    -
    !
     
     
    -
    !
    -
    !
     
     
     
     
    -
    |
    |
    |
    !
     
     
     
    -
    !
     
     
     
     
     
     
     
     
     
     
     
     
     
    -
    |
    |
    |
    !
     
    -
    |
    |
    |
    !
     
    -
    |
    |
    |
    !
    -
    !
    -
    !
     
    -
    !
    -
    !
     
    -
    |
    |
    !
    -
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    !
    -
    !
     
     
    -
    !
     
     
    -
    |
    |
    |
    !
    -
    !
     
    -
    !
    
    #!/bin/sh
     
    # ************************************
    # 変数
    # ************************************
    LOCAL_NW="<ネットワークアドレス(e.g. 192.168.5.0/24)>"
     
     
    # ************************************
    # 初期化
    # ************************************
    # モジュール読み込み
    modprobe ipt_state
    modprobe ipt_LOG
    modprobe ipt_REJECT
    modprobe ipt_limit
     
    # 全ルール削除
    iptables -F
    iptables -t nat -F
    iptables -X
    # パケットカウンタクリア
    iptables -Z
    # デフォルト拒否
    iptables -P FORWARD DROP
    iptables -P INPUT DROP
    iptables -P OUTPUT DROP
     
     
    # ************************************
    # チェイン
    # ************************************
    # invalid packet破棄
    iptables -N drop_invalid_packet
    iptables -A drop_invalid_packet -j LOG --log-prefix "Droped invalid packet: " -m limit --limit 1/m --limit-burst 3
    iptables -A drop_invalid_packet -j DROP
     
    # bad flag packet破棄
    iptables -N _drop_badflag_packet
    iptables -A _drop_badflag_packet -j LOG --log-prefix "Droped bad flag packet: " -m limit --limit 1/m --limit-burst 3
    iptables -A _drop_badflag_packet -j DROP
    iptables -N drop_badflag_packet
    iptables -A drop_badflag_packet -p tcp --tcp-flags ALL FIN,PSH,URG -j _drop_badflag_packet  # Xmas tree scan
    iptables -A drop_badflag_packet -p tcp --tcp-flags ALL ALL -j _drop_badflag_packet  # Xmas tree scan
    iptables -A drop_badflag_packet -p tcp --tcp-flags ALL NONE -j _drop_badflag_packet # Null scan
    iptables -A drop_badflag_packet -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j _drop_badflag_packet  # other scan
    iptables -A drop_badflag_packet -p tcp --tcp-flags ACK,FIN FIN -j _drop_badflag_packet  # Maimon scan
    iptables -A drop_badflag_packet -p tcp --tcp-flags FIN,RST FIN,RST -j _drop_badflag_packet  # other scan
    iptables -A drop_badflag_packet -p tcp --tcp-flags SYN,FIN SYN,FIN -j _drop_badflag_packet  # other scan
    iptables -A drop_badflag_packet -p tcp --tcp-flags SYN,RST SYN,RST -j _drop_badflag_packet  # other scan
     
     
    # ************************************
    # NAPT
    # ************************************
    # なし
     
     
    # ************************************
    # FORWARD
    # ************************************
    # なし
     
     
    # ************************************
    # INPUT
    # ************************************
    # loopbackへの入力は全て許可
    iptables -A INPUT -i lo -j ACCEPT
    # 接続済みの入力は全て許可
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    # LANからのICMPは全て許可
    iptables -A INPUT -p icmp -s $LOCAL_NW -j ACCEPT
     
    # invalid packet破棄
    iptables -A INPUT -p tcp -m state --state INVALID -j drop_invalid_packet
    # bad flag packet破棄
    iptables -A INPUT -p tcp -j drop_badflag_packet
     
    # デーモン
    # もし有効なサービスに回数制限を設けたくなったら
    # limit(リクエスト全体)ではなくhashlimit(IP,Port単位)を使う
    iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT    # SSH
    #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 tcp --dport 443 -m state --state NEW -j ACCEPT # HTTPS or SSTP
    #iptables -A INPUT -p udp --dport 443 -m state --state NEW -j ACCEPT # QUIC
    #iptables -A INPUT -p tcp --dport 465 -m state --state NEW -j ACCEPT    # SMTP-SSL
    #iptables -A INPUT -p udp --dport 500 -m state --state NEW -j ACCEPT    # IKEv2
    #iptables -A INPUT -p tcp --dport 587 -m state --state NEW -j ACCEPT    # SMTP(submission)
    #iptables -A INPUT -p tcp --dport 995 -m state --state NEW -j ACCEPT    # POP3-SSL
    #iptables -A INPUT -p udp --dport 1194 -m state --state NEW -j ACCEPT   # OpenVPN
    #iptables -A INPUT -p tcp --dport 3306 -m state --state NEW -j ACCEPT   # MySQL or MariaDB
    #iptables -A INPUT -p udp --dport 3306 -m state --state NEW -j ACCEPT   # MySQL or MariaDB
    #iptables -A INPUT -p udp --dport 4500 -m state --state NEW -j ACCEPT   # IPsec NAT traversal
    #iptables -A INPUT -p tcp --dport 5201 -m state --state NEW -j ACCEPT   # iperf3
    #iptables -A INPUT -p udp --dport 5201 -m state --state NEW -j ACCEPT   # iperf3
    #iptables -A INPUT -p tcp --dport 10050 -m state --state NEW -j ACCEPT    # Zabbix agent
    #iptables -A INPUT -p udp --dport 10050 -m state --state NEW -j ACCEPT    # Zabbix agent
    #iptables -A INPUT -p tcp --dport 10051 -m state --state NEW -j ACCEPT  # Zabbix
    #iptables -A INPUT -p udp --dport 10051 -m state --state NEW -j ACCEPT  # Zabbix
     
    # マルチキャスト,ブロードキャスト破棄
    iptables -A INPUT -m pkttype --pkt-type MULTICAST -j DROP
    iptables -A INPUT -m pkttype --pkt-type BROADCAST -j DROP
     
    # ログを出して破棄
    iptables -A INPUT -j LOG --log-prefix "Unexpected input: " -m limit --limit 1/m --limit-burst 3
     
     
    # ************************************
    # OUTPUT
    # ************************************
    # loopbackからの出力は全て許可
    iptables -A OUTPUT -o lo -j ACCEPT
    # 新規接続,接続済みを全て許可
    iptables -A OUTPUT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
     
    # ログを出して破棄
    iptables -A OUTPUT -j LOG --log-prefix "Unexpected output: " -m limit --limit 1/m --limit-burst 3
  2. 実行権限を付加。
    Everything is expanded.Everything is shortened.
      1
      2
      3
    
     
    -
    !
    
    chmod 700 /etc/network/if-pre-up.d/iptables
    # エラーが出ないか実行してみる
    /etc/network/if-pre-up.d/iptables
  3. /etc/network/if-post-down.d/iptablesを作成。
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
    
    -
    !
    -
    |
    !
     
    -
    !
    -
    !
     
     
    
    #!/bin/sh
     
    # ***クリア***
    # 全ルール削除
    iptables -F
    iptables -X
    # パケットカウンタクリア
    iptables -Z
    # デフォルト許可
    iptables -P INPUT ACCEPT
    iptables -P OUTPUT ACCEPT
    iptables -P FORWARD ACCEPT
  4. 実行権限を付加。
    Everything is expanded.Everything is shortened.
      1
      2
      3
    
     
    -
    !
    
    chmod 700 /etc/network/if-post-down.d/iptables
    # エラーが出ないか実行してみる
    /etc/network/if-post-down.d/iptables

検証時の環境