Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

firewalldを攻略する!!

 

こんにちは、LinuCエバンジェリストこと、鯨井貴博@opensourcetechです。

 

CentOS7がリリースされてから結構時間が経っていながらも、

結構苦労している方が多いかなと思うfirewalld。

今回は、そのfirewalldを攻略してみようと思います!

 

 

多くの人がfirewalldに対して感じていること

これは私の勝手な推測(笑)ですが、iptablesの進化系(ちょっと良くなった)と考えており、まぁなんとかなるだろう というのが多くの方の気持ちではないかと思います。

私もそうでした。

 

 

firewalldの設定ファイル

/etc/firewalld/firewalld.confが設定ファイルとなります。

[root@localhost ~]# cat -n /etc/firewalld/firewalld.conf
1 # firewalld config file
2
3 # default zone
4 # The default zone used if an empty zone string is used.
5 # Default: public
6 DefaultZone=public
7
8 # Minimal mark
9 # Marks up to this minimum are free for use for example in the direct
10 # interface. If more free marks are needed, increase the minimum
11 # Default: 100
12 MinimalMark=100
13
14 # Clean up on exit
15 # If set to no or false the firewall configuration will not get cleaned up
16 # on exit or stop of firewalld
17 # Default: yes
18 CleanupOnExit=yes
19
20 # Lockdown
21 # If set to enabled, firewall changes with the D-Bus interface will be limited
22 # to applications that are listed in the lockdown whitelist.
23 # The lockdown whitelist file is lockdown-whitelist.xml
24 # Default: no
25 Lockdown=no
26
27 # IPv6_rpfilter
28 # Performs a reverse path filter test on a packet for IPv6. If a reply to the
29 # packet would be sent via the same interface that the packet arrived on, the
30 # packet will match and be accepted, otherwise dropped.
31 # The rp_filter for IPv4 is controlled using sysctl.
32 # Default: yes
33 IPv6_rpfilter=yes
34
35 # IndividualCalls
36 # Do not use combined -restore calls, but individual calls. This increases the
37 # time that is needed to apply changes and to start the daemon, but is good for
38 # debugging.
39 # Default: no
40 IndividualCalls=no
41
42 # LogDenied
43 # Add logging rules right before reject and drop rules in the INPUT, FORWARD
44 # and OUTPUT chains for the default rules and also final reject and drop rules
45 # in zones. Possible values are: all, unicast, broadcast, multicast and off.
46 # Default: off
47 LogDenied=off
48
49 # AutomaticHelpers
50 # For the secure use of iptables and connection tracking helpers it is
51 # recommended to turn AutomaticHelpers off. But this might have side effects on
52 # other services using the netfilter helpers as the sysctl setting in
53 # /proc/sys/net/netfilter/nf_conntrack_helper will be changed.
54 # With the system setting, the default value set in the kernel or with sysctl
55 # will be used. Possible values are: yes, no and system.
56 # Default: system
57 AutomaticHelpers=system

 

 

firewalldの設定方法

firewalldの設定方法は、通信のルール(許可/拒否する通信など)を定義するゾーンを、

インターフェイス(NIC)に割り当てるものとなります。

デフォルトでは、上にあるfirewalld.confの6行目に書かれている通り、

publicというゾーンが使用されるようになっています。

(DefaultZone=public)

 

用意されているゾーンは、以下の9つあります。

public・・・・・・・サーバなどを利用する公共の領域を想定したもの
work・・・・・・・・業務でのクライアントコンピュータの利用を想定したもの
home・・・・・・・・家庭でのクライアントコンピュータの利用を想定したもの
internal・・・・・ファイアウォールの内部ネットワークを想定したもの
external・・・・・・ファイアウォールの外部ネットワークを想定したもの
dmz・・・・・・・・・DMZを想定したもの
trusted・・・・・・全通信を許可
block・・・・・・・・受信パケットを全て拒否。内部から外部に出て行くものとその戻りのみ許可。
drop・・・・・・・・・受信パケットを全て破棄。内部から外部に出て行くものとその戻りのみ許可。

 

 

各ゾーンで定義されているルール

ルールは、/usr/lib/firewalldディレクトリ内に、XMLで定義されています。

[root@localhost ~]# cd /usr/lib/firewalld/
[root@localhost firewalld]# ls -lrR
.:
合計 16
drwxr-xr-x. 2 root root 163 10月 5 18:54 zones
drwxr-xr-x. 2 root root 94 10月 5 18:54 xmlschema
drwxr-xr-x. 2 root root 8192 10月 5 18:54 services
drwxr-xr-x. 2 root root 20 10月 5 18:54 ipsets
drwxr-xr-x. 2 root root 4096 10月 5 18:54 icmptypes
drwxr-xr-x. 2 root root 203 10月 5 18:54 helpers

./zones:
合計 36
-rw-r--r--. 1 root root 311 4月 11 13:52 work.xml
-rw-r--r--. 1 root root 162 4月 11 13:52 trusted.xml
-rw-r--r--. 1 root root 315 4月 11 13:52 public.xml
-rw-r--r--. 1 root root 384 4月 11 13:52 internal.xml
-rw-r--r--. 1 root root 369 4月 11 13:52 home.xml
-rw-r--r--. 1 root root 304 4月 11 13:52 external.xml
-rw-r--r--. 1 root root 291 4月 11 13:52 drop.xml
-rw-r--r--. 1 root root 293 4月 11 13:52 dmz.xml
-rw-r--r--. 1 root root 299 4月 11 13:52 block.xml

./xmlschema:
合計 24
-rw-r--r--. 1 root root 5900 4月 11 13:52 zone.xsd
-rw-r--r--. 1 root root 1952 4月 11 13:52 service.xsd
-rw-r--r--. 1 root root 1100 4月 11 13:52 ipset.xsd
-rw-r--r--. 1 root root 846 4月 11 13:52 icmptype.xsd
-rwxr-xr-x. 1 root root 1457 4月 11 13:52 check.sh

./services:
合計 432
-rw-r--r--. 1 root root 545 4月 11 13:52 xmpp-server.xml
-rw-r--r--. 1 root root 264 4月 11 13:52 xmpp-local.xml
-rw-r--r--. 1 root root 488 4月 11 13:52 xmpp-client.xml
-rw-r--r--. 1 root root 509 4月 11 13:52 xmpp-bosh.xml
-rw-r--r--. 1 root root 310 4月 11 13:52 wbem-https.xml
-rw-r--r--. 1 root root 475 4月 11 13:52 vnc-server.xml
-rw-r--r--. 1 root root 593 4月 11 13:52 vdsm.xml
-rw-r--r--. 1 root root 244 4月 11 13:52 transmission-client.xml
-rw-r--r--. 1 root root 771 4月 11 13:52 tor-socks.xml
-rw-r--r--. 1 root root 336 4月 11 13:52 tinc.xml
-rw-r--r--. 1 root root 437 4月 11 13:52 tftp.xml
-rw-r--r--. 1 root root 301 4月 11 13:52 tftp-client.xml
-rw-r--r--. 1 root root 393 4月 11 13:52 telnet.xml
-rw-r--r--. 1 root root 329 4月 11 13:52 syslog.xml
-rw-r--r--. 1 root root 444 4月 11 13:52 syslog-tls.xml
-rw-r--r--. 1 root root 496 4月 11 13:52 synergy.xml
-rw-r--r--. 1 root root 463 4月 11 13:52 ssh.xml
-rw-r--r--. 1 root root 173 4月 11 13:52 squid.xml
-rw-r--r--. 1 root root 359 4月 11 13:52 spideroak-lansync.xml
-rw-r--r--. 1 root root 308 4月 11 13:52 snmptrap.xml
-rw-r--r--. 1 root root 342 4月 11 13:52 snmp.xml
-rw-r--r--. 1 root root 577 4月 11 13:52 smtps.xml
-rw-r--r--. 1 root root 550 4月 11 13:52 smtp.xml
-rw-r--r--. 1 root root 231 4月 11 13:52 smtp-submission.xml
-rw-r--r--. 1 root root 283 4月 11 13:52 sips.xml
-rw-r--r--. 1 root root 509 4月 11 13:52 sip.xml
-rw-r--r--. 1 root root 337 4月 11 13:52 sane.xml
-rw-r--r--. 1 root root 461 4月 11 13:52 samba.xml
-rw-r--r--. 1 root root 384 4月 11 13:52 samba-client.xml
-rw-r--r--. 1 root root 311 4月 11 13:52 rsyncd.xml
-rw-r--r--. 1 root root 310 4月 11 13:52 rsh.xml
-rw-r--r--. 1 root root 214 4月 11 13:52 rpc-bind.xml
-rw-r--r--. 1 root root 520 4月 11 13:52 radius.xml
-rw-r--r--. 1 root root 273 4月 11 13:52 quassel.xml
-rw-r--r--. 1 root root 297 4月 11 13:52 puppetmaster.xml
-rw-r--r--. 1 root root 414 4月 11 13:52 pulseaudio.xml
-rw-r--r--. 1 root root 424 4月 11 13:52 ptp.xml
-rw-r--r--. 1 root root 261 4月 11 13:52 proxy-dhcp.xml
-rw-r--r--. 1 root root 509 4月 11 13:52 privoxy.xml
-rw-r--r--. 1 root root 181 4月 11 13:52 postgresql.xml
-rw-r--r--. 1 root root 357 4月 11 13:52 pop3s.xml
-rw-r--r--. 1 root root 348 4月 11 13:52 pop3.xml
-rw-r--r--. 1 root root 544 4月 11 13:52 pmwebapis.xml
-rw-r--r--. 1 root root 460 4月 11 13:52 pmwebapi.xml
-rw-r--r--. 1 root root 474 4月 11 13:52 pmproxy.xml
-rw-r--r--. 1 root root 433 4月 11 13:52 pmcd.xml
-rw-r--r--. 1 root root 235 4月 11 13:52 ovirt-vmconsole.xml
-rw-r--r--. 1 root root 343 4月 11 13:52 ovirt-storageconsole.xml
-rw-r--r--. 1 root root 260 4月 11 13:52 ovirt-imageio.xml
-rw-r--r--. 1 root root 335 4月 11 13:52 openvpn.xml
-rw-r--r--. 1 root root 389 4月 11 13:52 ntp.xml
-rw-r--r--. 1 root root 247 4月 11 13:52 nrpe.xml
-rw-r--r--. 1 root root 342 4月 11 13:52 nfs3.xml
-rw-r--r--. 1 root root 324 4月 11 13:52 nfs.xml
-rw-r--r--. 1 root root 171 4月 11 13:52 mysql.xml
-rw-r--r--. 1 root root 170 4月 11 13:52 mssql.xml
-rw-r--r--. 1 root root 190 4月 11 13:52 ms-wbt.xml
-rw-r--r--. 1 root root 211 4月 11 13:52 mountd.xml
-rw-r--r--. 1 root root 473 4月 11 13:52 mosh.xml
-rw-r--r--. 1 root root 424 4月 11 13:52 mdns.xml
-rw-r--r--. 1 root root 349 4月 11 13:52 managesieve.xml
-rw-r--r--. 1 root root 389 4月 11 13:52 libvirt.xml
-rw-r--r--. 1 root root 385 4月 11 13:52 libvirt-tls.xml
-rw-r--r--. 1 root root 232 4月 11 13:52 ldaps.xml
-rw-r--r--. 1 root root 199 4月 11 13:52 ldap.xml
-rw-r--r--. 1 root root 242 4月 11 13:52 kshell.xml
-rw-r--r--. 1 root root 221 4月 11 13:52 kpasswd.xml
-rw-r--r--. 1 root root 249 4月 11 13:52 klogin.xml
-rw-r--r--. 1 root root 384 4月 11 13:52 kibana.xml
-rw-r--r--. 1 root root 233 4月 11 13:52 kerberos.xml
-rw-r--r--. 1 root root 182 4月 11 13:52 kadmin.xml
-rw-r--r--. 1 root root 264 4月 11 13:52 iscsi-target.xml
-rw-r--r--. 1 root root 554 4月 11 13:52 ipsec.xml
-rw-r--r--. 1 root root 427 4月 11 13:52 ipp.xml
-rw-r--r--. 1 root root 454 4月 11 13:52 ipp-client.xml
-rw-r--r--. 1 root root 372 4月 11 13:52 imaps.xml
-rw-r--r--. 1 root root 327 4月 11 13:52 imap.xml
-rw-r--r--. 1 root root 448 4月 11 13:52 https.xml
-rw-r--r--. 1 root root 353 4月 11 13:52 http.xml
-rw-r--r--. 1 root root 603 4月 11 13:52 high-availability.xml
-rw-r--r--. 1 root root 176 4月 11 13:52 ganglia-master.xml
-rw-r--r--. 1 root root 184 4月 11 13:52 ganglia-client.xml
-rw-r--r--. 1 root root 374 4月 11 13:52 ftp.xml
-rw-r--r--. 1 root root 629 4月 11 13:52 freeipa-trust.xml
-rw-r--r--. 1 root root 315 4月 11 13:52 freeipa-replication.xml
-rw-r--r--. 1 root root 836 4月 11 13:52 freeipa-ldaps.xml
-rw-r--r--. 1 root root 836 4月 11 13:52 freeipa-ldap.xml
-rw-r--r--. 1 root root 338 4月 11 13:52 elasticsearch.xml
-rw-r--r--. 1 root root 228 4月 11 13:52 dropbox-lansync.xml
-rw-r--r--. 1 root root 374 4月 11 13:52 docker-registry.xml
-rw-r--r--. 1 root root 346 4月 11 13:52 dns.xml
-rw-r--r--. 1 root root 234 4月 11 13:52 dhcpv6.xml
-rw-r--r--. 1 root root 305 4月 11 13:52 dhcpv6-client.xml
-rw-r--r--. 1 root root 227 4月 11 13:52 dhcp.xml
-rw-r--r--. 1 root root 296 4月 11 13:52 ctdb.xml
-rw-r--r--. 1 root root 260 4月 11 13:52 condor-collector.xml
-rw-r--r--. 1 root root 168 4月 11 13:52 cfengine.xml
-rw-r--r--. 1 root root 305 4月 11 13:52 ceph.xml
-rw-r--r--. 1 root root 294 4月 11 13:52 ceph-mon.xml
-rw-r--r--. 1 root root 244 4月 11 13:52 bitcoin.xml
-rw-r--r--. 1 root root 281 4月 11 13:52 bitcoin-testnet.xml
-rw-r--r--. 1 root root 307 4月 11 13:52 bitcoin-testnet-rpc.xml
-rw-r--r--. 1 root root 275 4月 11 13:52 bitcoin-rpc.xml
-rw-r--r--. 1 root root 346 4月 11 13:52 bacula.xml
-rw-r--r--. 1 root root 320 4月 11 13:52 bacula-client.xml
-rw-r--r--. 1 root root 447 4月 11 13:52 amanda-k5-client.xml
-rw-r--r--. 1 root root 412 4月 11 13:52 amanda-client.xml
-rw-r--r--. 1 root root 741 4月 11 13:52 RH-Satellite-6.xml

./ipsets:
合計 4
-rw-r--r--. 1 root root 29 4月 11 13:52 README

./icmptypes:
合計 180
-rw-r--r--. 1 root root 249 4月 11 13:52 unknown-option.xml
-rw-r--r--. 1 root root 259 4月 11 13:52 unknown-header-type.xml
-rw-r--r--. 1 root root 256 4月 11 13:52 ttl-zero-during-transit.xml
-rw-r--r--. 1 root root 293 4月 11 13:52 ttl-zero-during-reassembly.xml
-rw-r--r--. 1 root root 269 4月 11 13:52 tos-network-unreachable.xml
-rw-r--r--. 1 root root 272 4月 11 13:52 tos-network-redirect.xml
-rw-r--r--. 1 root root 257 4月 11 13:52 tos-host-unreachable.xml
-rw-r--r--. 1 root root 258 4月 11 13:52 tos-host-redirect.xml
-rw-r--r--. 1 root root 228 4月 11 13:52 timestamp-request.xml
-rw-r--r--. 1 root root 233 4月 11 13:52 timestamp-reply.xml
-rw-r--r--. 1 root root 253 4月 11 13:52 time-exceeded.xml
-rw-r--r--. 1 root root 236 4月 11 13:52 source-route-failed.xml
-rw-r--r--. 1 root root 248 4月 11 13:52 source-quench.xml
-rw-r--r--. 1 root root 223 4月 11 13:52 router-solicitation.xml
-rw-r--r--. 1 root root 227 4月 11 13:52 router-advertisement.xml
-rw-r--r--. 1 root root 241 4月 11 13:52 required-option-missing.xml
-rw-r--r--. 1 root root 244 4月 11 13:52 reject-route.xml
-rw-r--r--. 1 root root 185 4月 11 13:52 redirect.xml
-rw-r--r--. 1 root root 249 4月 11 13:52 protocol-unreachable.xml
-rw-r--r--. 1 root root 256 4月 11 13:52 precedence-cutoff.xml
-rw-r--r--. 1 root root 233 4月 11 13:52 port-unreachable.xml
-rw-r--r--. 1 root root 225 4月 11 13:52 parameter-problem.xml
-rw-r--r--. 1 root root 328 4月 11 13:52 packet-too-big.xml
-rw-r--r--. 1 root root 239 4月 11 13:52 no-route.xml
-rw-r--r--. 1 root root 247 4月 11 13:52 network-unreachable.xml
-rw-r--r--. 1 root root 239 4月 11 13:52 network-unknown.xml
-rw-r--r--. 1 root root 248 4月 11 13:52 network-redirect.xml
-rw-r--r--. 1 root root 250 4月 11 13:52 network-prohibited.xml
-rw-r--r--. 1 root root 457 4月 11 13:52 neighbour-solicitation.xml
-rw-r--r--. 1 root root 355 4月 11 13:52 neighbour-advertisement.xml
-rw-r--r--. 1 root root 229 4月 11 13:52 ip-header-bad.xml
-rw-r--r--. 1 root root 247 4月 11 13:52 host-unreachable.xml
-rw-r--r--. 1 root root 239 4月 11 13:52 host-unknown.xml
-rw-r--r--. 1 root root 242 4月 11 13:52 host-redirect.xml
-rw-r--r--. 1 root root 257 4月 11 13:52 host-prohibited.xml
-rw-r--r--. 1 root root 266 4月 11 13:52 host-precedence-violation.xml
-rw-r--r--. 1 root root 280 4月 11 13:52 fragmentation-needed.xml
-rw-r--r--. 1 root root 261 4月 11 13:52 failed-policy.xml
-rw-r--r--. 1 root root 210 4月 11 13:52 echo-request.xml
-rw-r--r--. 1 root root 173 4月 11 13:52 echo-reply.xml
-rw-r--r--. 1 root root 222 4月 11 13:52 destination-unreachable.xml
-rw-r--r--. 1 root root 279 4月 11 13:52 communication-prohibited.xml
-rw-r--r--. 1 root root 294 4月 11 13:52 beyond-scope.xml
-rw-r--r--. 1 root root 258 4月 11 13:52 bad-header.xml
-rw-r--r--. 1 root root 385 4月 11 13:52 address-unreachable.xml

./helpers:
合計 48
-rw-r--r--. 1 root root 120 4月 11 13:52 tftp.xml
-rw-r--r--. 1 root root 135 4月 11 13:52 snmp.xml
-rw-r--r--. 1 root root 158 4月 11 13:52 sip.xml
-rw-r--r--. 1 root root 122 4月 11 13:52 sane.xml
-rw-r--r--. 1 root root 136 4月 11 13:52 pptp.xml
-rw-r--r--. 1 root root 141 4月 11 13:52 netbios-ns.xml
-rw-r--r--. 1 root root 134 4月 11 13:52 irc.xml
-rw-r--r--. 1 root root 85 4月 11 13:52 h323.xml
-rw-r--r--. 1 root root 119 4月 11 13:52 ftp.xml
-rw-r--r--. 1 root root 125 4月 11 13:52 amanda.xml
-rw-r--r--. 1 root root 122 4月 11 13:52 RAS.xml
-rw-r--r--. 1 root root 122 4月 11 13:52 Q.931.xml

 

 例えば、publicというゾーンについては以下となります。

sshとdhcpv6-clientが許可されていることが確認できます。

[root@localhost firewalld]# cat -n /usr/lib/firewalld/zones/public.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <zone>
3 <short>Public</short>
4 <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
5 <service name="ssh"/>
6 <service name="dhcpv6-client"/>
7 </zone>

 

また、サービス全体の定義は /usr/lib/firewalld/services内のXMLファイルで行われており、以下 sshやdhcpv6-clientの例です。

[root@localhost firewalld]# cat -n /usr/lib/firewalld/services/ssh.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <service>
3 <short>SSH</short>
4 <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
5 <port protocol="tcp" port="22"/>
6 </service>
[root@localhost firewalld]# cat -n /usr/lib/firewalld/services/dhcpv6-client.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <service>
3 <short>DHCPv6 Client</short>
4 <description>This option allows a DHCP for IPv6 (DHCPv6) client to obtain addresses and other IPv6 settings from DHCPv6 server.</description>
5 <port protocol="udp" port="546"/>
6 <destination ipv6="fe80::/64"/>
7 </service>
[root@localhost firewalld]# ls /usr/lib/firewalld/services/
RH-Satellite-6.xml iscsi-target.xml pulseaudio.xml
amanda-client.xml kadmin.xml puppetmaster.xml
amanda-k5-client.xml kerberos.xml quassel.xml
bacula-client.xml kibana.xml radius.xml
bacula.xml klogin.xml rpc-bind.xml
bitcoin-rpc.xml kpasswd.xml rsh.xml
bitcoin-testnet-rpc.xml kshell.xml rsyncd.xml
bitcoin-testnet.xml ldap.xml samba-client.xml
bitcoin.xml ldaps.xml samba.xml
ceph-mon.xml libvirt-tls.xml sane.xml
ceph.xml libvirt.xml sip.xml
cfengine.xml managesieve.xml sips.xml
condor-collector.xml mdns.xml smtp-submission.xml
ctdb.xml mosh.xml smtp.xml
dhcp.xml mountd.xml smtps.xml
dhcpv6-client.xml ms-wbt.xml snmp.xml
dhcpv6.xml mssql.xml snmptrap.xml
dns.xml mysql.xml spideroak-lansync.xml
docker-registry.xml nfs.xml squid.xml
dropbox-lansync.xml nfs3.xml ssh.xml
elasticsearch.xml nrpe.xml synergy.xml
freeipa-ldap.xml ntp.xml syslog-tls.xml
freeipa-ldaps.xml openvpn.xml syslog.xml
freeipa-replication.xml ovirt-imageio.xml telnet.xml
freeipa-trust.xml ovirt-storageconsole.xml tftp-client.xml
ftp.xml ovirt-vmconsole.xml tftp.xml
ganglia-client.xml pmcd.xml tinc.xml
ganglia-master.xml pmproxy.xml tor-socks.xml
high-availability.xml pmwebapi.xml transmission-client.xml
http.xml pmwebapis.xml vdsm.xml
https.xml pop3.xml vnc-server.xml
imap.xml pop3s.xml wbem-https.xml
imaps.xml postgresql.xml xmpp-bosh.xml
ipp-client.xml privoxy.xml xmpp-client.xml
ipp.xml proxy-dhcp.xml xmpp-local.xml
ipsec.xml ptp.xml xmpp-server.xml

 

firewalldのデフォルト状態では、

上記にある/usr/lib/firewalldディレクトリに配置されたルール(/usr/lib/firewalld/zonesにある各ゾーンに対応したXML)が使用されます。

 

 

設定変更した際のfirewalldのルール

では、コマンド等を使ってルールを変更した場合どうなるかと言いますと、

変更は /etc/firewalld内のXMLファイルに行われ、変更があったものについてはこのディレクトリ内にあるルールが使用されます。

[root@localhost ~]# ls /usr/lib/firewalld/
helpers icmptypes ipsets services xmlschema zones
[root@localhost ~]# ls /etc/firewalld/
firewalld.conf icmptypes lockdown-whitelist.xml zones
helpers ipsets services

 

 

ルールの一覧

ルールの一覧は、firewall-cmdコマンド(その他、firewall-config/system-config-firewall)などで確認行います。

firewall-cmd --list-all-zones・・・・全ゾーンのルール一覧表示

firewall-cmd --zone=public --list-all・・・・publicゾーンのみ一覧表示

[root@localhost ~]# firewall-cmd --list-all-zones
block
target: %%REJECT%%
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

dmz
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

drop
target: DROP
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

external
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh
ports:
protocols:
masquerade: yes
forward-ports:
source-ports:
icmp-blocks:
rich rules:

home
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh mdns samba-client dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

internal
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh mdns samba-client dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

public (active)
target: default
icmp-block-inversion: no
interfaces: enp0s3
sources:
services: ssh dhcpv6-client http
ports: 10051/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

trusted
target: ACCEPT
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

work
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

[root@localhost ~]# firewall-cmd --zone=public --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: enp0s3
sources:
services: ssh dhcpv6-client
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

 

 

ルールの追加

publicゾーンにおける、http(tcp/80)、およびtcp/10051へのアクセス許可を追加してみます。

firewall-cmd --add-service=**** でサービス追加、

firewall-cmd --add-port=***/*** でポート追加(※servicesディレクトリで定義されていない場合に使います)

[root@localhost ~]# firewall-cmd --list-services --zone=public --permanent
ssh dhcpv6-client
[root@localhost ~]# firewall-cmd --add-service=http --zone=public --permanent
success
[root@localhost ~]# firewall-cmd --list-services --zone=public --permanent
ssh dhcpv6-client http
[root@localhost ~]# firewall-cmd --add-port=10051/tcp --zone=public --permanent
success
[root@localhost ~]# firewall-cmd --list-ports --zone=public --permanent
10051/tcp

 

上記変更を行った場合、

/etc/firewalld/zonesにpublic.xmlが作成されます。

※7、8行目にそれぞれ追加されています。

[root@localhost ~]# ls /etc/firewalld/zones/
public.xml public.xml.old
[root@localhost ~]# cat -n /etc/firewalld/zones/public.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <zone>
3 <short>Public</short>
4 <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
5 <service name="ssh"/>
6 <service name="dhcpv6-client"/>
7 <service name="http"/>
8 <port protocol="tcp" port="10051"/>
9 </zone>

 

 firewall-cmd --list-all-zonesでも追加が確認できます。

[root@localhost ~]# firewall-cmd --list-all-zones
block
target: %%REJECT%%
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

dmz
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

drop
target: DROP
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

external
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh
ports:
protocols:
masquerade: yes
forward-ports:
source-ports:
icmp-blocks:
rich rules:

home
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh mdns samba-client dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

internal
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh mdns samba-client dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

public (active)
target: default
icmp-block-inversion: no
interfaces: enp0s3
sources:
services: ssh dhcpv6-client http
ports: 10051/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

trusted
target: ACCEPT
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

work
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

 

 

サービスの定義を追加

サービスの定義を追加する場合は、/etc/firewalld/servicesディレクトリ内に

/usr/lib/firewalld/servicesディレクトリ内にあるファイルを真似て作成します。

[root@localhost firewalld]# cat -n /usr/lib/firewalld/services/ssh.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <service>
3 <short>SSH</short>・・・サービス名
4 <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>・・・・サービスの説明
5 <port protocol="tcp" port="22"/>・・・・使用するポートやプロトコル
6 </service>

 

 

その他、使用するコマンド

firewall-cmd --get-default-zone(デフォルトゾーンの確認)

firewall-cmd --set-default-zone=zone_name(デフォルトゾーンの変更)

firewall-cmd --get-zones(ゾーンの一覧)

firewall-cmd --get-active-zones(どのゾーンに どのインターフェイスが割り当てられているか)

firewall-cmd --zone=zone_name --change-interface=interface_name(インターフェイスへのゾーン割り当ての変更)

firewall-cmd --get-services(サービスの一覧)

firewall-cmd --remove-service=service_name(公開サービスの削除)

firewall-cmd --remove-port=port_number/protocol(公開ポートの削除)

firewall-cmd --list-ports(公開ポートの一覧)

 

 

その他、出来ること

firewalldでは、上記以外にも以下の機能を提供できます。

・新しいゾーンの定義(firewall-cmd --permanent --new-zone)

・新しいサービスの定義(fiewall-cmd --permanent --new-service)

・ポートフォワーディング(firewall-cmd --add-forward-port など)

・アドレス変換(firewall-cmd --add-masquarade など)

・リッチルール(firewall-cmd --add-rich-rule など)

・ダイレクトルール(Netfilterに直接アクセスする設定。iptablesに関する知識が必要。)

 

 

参考リンク

https://firewalld.org/

Documentation - Manual Pages - firewall-cmd | firewalld

4.5. ファイアウォールの使用 - Red Hat Customer Portal

 

 

 

 

www.slideshare.net

github.com

www.facebook.com

twitter.com

www.instagram.com

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

 

 

Opensourcetech by Takahiro Kujirai