CentOS 7で今まで使用していた iptables のサービスが firewalld に変更されています。
サービスとしては利用しやすいのですが、今までの慣れてきたものが変更になったので一通りの確認など。
まずはvagrantに素の状態で入れたCentOSに試してみます。
1 2 3 4 5 6 7 8 9 10 |
$ sudo firewall-cmd --state not running $ sudo firewall-cmd --list-services FirewallD is not running $ sudo firewall-cmd --list-ports FirewallD is not running $ sudo firewall-cmd --list-all FirewallD is not running |
ここは動いていないのでしょうがありません。
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 |
$ sudo systemctl start firewalld.service $ sudo firewall-cmd --state running $ sudo firewall-cmd --list-services ssh dhcpv6-client $ sudo firewall-cmd --list-ports $ sudo firewall-cmd --get-active-zones public interfaces: eth0 $ sudo firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: |
初期段階では「ssh」と「dhcpv6-client」のみが許可されていますね。
Activeなゾーンとしてpublicというソーンに初期では設定が入っており、そちらにeth0が割り当てされています。
あらかじめ9個のゾーンが設定されています。
ゾーン名 | 概要 |
public | サーバとして最低限必要な受信許可ルール。全てのインタフェースはデフォルトでpublicゾーンに所属している |
work | 業務用クライアントPCとしての利用を想定したルール。社内LAN内で使う場合を想定している。クライアントPCに求められる必要最低限のルールが定義されている |
home | 家庭用クライアントPCとしての利用を想定したルール。家庭内LANに接続して使う場合を想定している |
internal | Linuxを用いてファイアウォールを構築するのに使うルール。内部ネットワークのインタフェースに設定する |
external | 同じく、Linuxを用いてファイアウォールを構築するのに使うルール。こちらは、外部ネットワークのインタフェースに対して設定する |
dmz | 同じく、Linuxを用いてファイアウォールを構築するのに使うルール。こちらは、DMZのインタフェースに対して設定する |
block | 受信パケットを全て拒否するルール。送信パケットの戻り通信は許可される |
drop | 受信パケットを全て破棄するルール。送信パケットの戻り通信は許可される |
trusted | 全ての通信を許可するルール |
★参考
http://www.atmarkit.co.jp/ait/articles/1602/18/news019.html
ここでは「ssh」のポートを20022に変更して、「dhcpv6-client」を停止しているという状況で、HTTPとHTTPSのサービスを追加するという流れを行なってみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ sudo firewall-cmd --add-port=20022/tcp success $ sudo firewall-cmd --permanent --add-port=20022/tcp success $ sudo firewall-cmd --add-service=http success $ sudo firewall-cmd --add-service=https success $ sudo firewall-cmd --permanent --add-service=http success $ sudo firewall-cmd --permanent --add-service=https success $ sudo firewall-cmd --remove-service=dhcpv6-client success $ sudo firewall-cmd --remove-service=ssh success $ sudo firewall-cmd --permanent --remove-service=dhcpv6-client success $ sudo firewall-cmd --permanent --remove-service=ssh success |
「--permanent」オプションをつけることで再起動後も有効にしています。
同じように状態の確認をしてみます。
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 |
$ sudo firewall-cmd --state running $ sudo firewall-cmd --list-services http https $ sudo firewall-cmd --list-ports 20022/tcp $ sudo firewall-cmd --get-active-zones public interfaces: eth0 $ sudo firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: http https ports: 20022/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: |
特定のNWからのアドレスの許可なども行なっておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ sudo firewall-cmd --add-source=192.168.1.0/24 --zone=public success $ sudo firewall-cmd --add-source=192.168.1.0/24 --zone=public --permanent success $ sudo firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: 192.168.1.0/24 services: ssh http https ports: 20022/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: |
この辺だけだと少し不安なのでセキュリティ設定など。
iptablesの呼び出しができるので、さくらさんにあった攻撃対策のルールを入れておきます。
https://help.sakura.ad.jp/hc/ja/articles/206208121-iptables%E3%81%AE%E8%A8%AD%E5%AE%9A%E6%96%B9%E6%B3%95
firewalld では iptables に取って代わったというより、firewalld が iptables の設定を管理してくれるようです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ sudo firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp --tcp-flags ALL NONE -j DROP success $ sudo firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp ! --syn -m state --state NEW -j DROP success $ sudo firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp --tcp-flags ALL ALL -j DROP success $ sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p tcp --tcp-flags ALL NONE -j DROP success $ sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p tcp ! --syn -m state --state NEW -j DROP success $ sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p tcp --tcp-flags ALL ALL -j DROP success |
ちなみに上から以下のルールになります。
①データを持たないパケットの接続を破棄する
②SYNflood攻撃と思われる接続を破棄する
③ステルススキャンと思われる接続を破棄する
統計情報などは以下で確認します。
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 |
$ sudo iptables -nvxL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 383 23448 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 2 176 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0 2 176 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 2 176 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_direct all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_IN_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_IN_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_OUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FORWARD_OUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 73 packets, 6258 bytes) pkts bytes target prot opt in out source destination 269 21578 OUTPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD_IN_ZONES (1 references) pkts bytes target prot opt in out source destination 0 0 FWDI_public all -- eth0 * 0.0.0.0/0 0.0.0.0/0 [goto] 0 0 FWDI_public all -- + * 0.0.0.0/0 0.0.0.0/0 [goto] Chain FORWARD_IN_ZONES_SOURCE (1 references) pkts bytes target prot opt in out source destination 0 0 FWDI_public all -- * * 192.168.1.0/24 0.0.0.0/0 [goto] Chain FORWARD_OUT_ZONES (1 references) pkts bytes target prot opt in out source destination 0 0 FWDO_public all -- * eth0 0.0.0.0/0 0.0.0.0/0 [goto] 0 0 FWDO_public all -- * + 0.0.0.0/0 0.0.0.0/0 [goto] Chain FORWARD_OUT_ZONES_SOURCE (1 references) pkts bytes target prot opt in out source destination 0 0 FWDO_public all -- * * 0.0.0.0/0 192.168.1.0/24 [goto] Chain FORWARD_direct (1 references) pkts bytes target prot opt in out source destination Chain FWDI_public (3 references) pkts bytes target prot opt in out source destination 0 0 FWDI_public_log all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FWDI_public_deny all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FWDI_public_allow all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 Chain FWDI_public_allow (1 references) pkts bytes target prot opt in out source destination Chain FWDI_public_deny (1 references) pkts bytes target prot opt in out source destination Chain FWDI_public_log (1 references) pkts bytes target prot opt in out source destination Chain FWDO_public (3 references) pkts bytes target prot opt in out source destination 0 0 FWDO_public_log all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FWDO_public_deny all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 FWDO_public_allow all -- * * 0.0.0.0/0 0.0.0.0/0 Chain FWDO_public_allow (1 references) pkts bytes target prot opt in out source destination Chain FWDO_public_deny (1 references) pkts bytes target prot opt in out source destination Chain FWDO_public_log (1 references) pkts bytes target prot opt in out source destination Chain INPUT_ZONES (1 references) pkts bytes target prot opt in out source destination 2 176 IN_public all -- eth0 * 0.0.0.0/0 0.0.0.0/0 [goto] 0 0 IN_public all -- + * 0.0.0.0/0 0.0.0.0/0 [goto] Chain INPUT_ZONES_SOURCE (1 references) pkts bytes target prot opt in out source destination 0 0 IN_public all -- * * 192.168.1.0/24 0.0.0.0/0 [goto] Chain INPUT_direct (1 references) pkts bytes target prot opt in out source destination 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 state NEW 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F Chain IN_public (3 references) pkts bytes target prot opt in out source destination 2 176 IN_public_log all -- * * 0.0.0.0/0 0.0.0.0/0 2 176 IN_public_deny all -- * * 0.0.0.0/0 0.0.0.0/0 2 176 IN_public_allow all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 Chain IN_public_allow (1 references) pkts bytes target prot opt in out source destination 2 176 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:20022 ctstate NEW 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 ctstate NEW 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 ctstate NEW Chain IN_public_deny (1 references) pkts bytes target prot opt in out source destination Chain IN_public_log (1 references) pkts bytes target prot opt in out source destination Chain OUTPUT_direct (1 references) pkts bytes target prot opt in out source destination |
iptables のコマンドが使えると自由度が高いですね。
以下のブログのようにDDOS対策として、SMTPへの大量の接続を制御しているケースもありました。
設定の幅が広いです。
最後に自動起動の設定をしておきます。
1 2 3 |
$ sudo systemctl enable firewalld.service |
このブログは株式会社CoLabMixによる技術ブログです。
GCP、AWSなどでのインフラ構築・運用や、クローリング・分析・検索などを主体とした開発を行なっています。
Ruby on RailsやDjango、Pythonなどの開発依頼などお気軽にお声がけください。
開発パートナーを増やしたいという企業と積極的に繋がっていきたいです。
お問い合わせやご依頼・ご相談など