Firewalld: improving security for your AWS EC2 server

While AWS EC2 instances should be well protected by VPC security tools, you may still need to implement protection at the OS-level, and that means firewalld.

This is the second part of our server security series. In this article, we will look at configuring firewall rules via firewalld on Red Hat Enterprise Linux. While Amazon Linux is similar to Red Hat Enterprise Linux (RHEL) in many ways, it does not yet support firewalld. So we’ll have to focus on RHEL. By the way, I will not cover iptables here, as there are many good resources out there already.
FirewalldYou might wonder why we need to configure the firewall at the operating system level in the first place, if this is already taken care of by EC2 security groups within our VPC. I can think of a few possible use cases. If, for instance, the firewall in the security group is configured incorrectly, the OS-level firewall can act as a backup to protect the instance from possible probes or compromise. Or perhaps the administrator wants to bind additional ports for future services or temporarily block certain ports, but does not have access to the EC2 dashboard. In either case, depending on the security group state, he should be able to do so at the OS-level.
Now that we have justified using firewalld, let’s learn a bit more about it. firewalld is a new way to interact with the netfilter subsystem in the Linux kernel. It simplifies the way we manage firewall rules by classifying network traffic into different firewall zones. We could choose to simply use iptables to configure the firewall rules if we wanted to, but both cannot co-exist with each other.

Getting started with firewalld

Therefore, to get started with firewalld, we first need to make sure that the iptables-related services are disabled. By default, Red Hat Enterprise Linux does not have either iptables-services or firewalld packages installed. So when you try to query for iptables-services, it will tell you that it is not there.

# rpm -q iptables-services
package iptables-services is not installed

However, if it is already installed, we’ll need to disable the service so that it does not interfere with firewalld.

# rpm -q iptables-services
iptables-services-1.4.21-13.el7.x86_64
# systemctl stop iptables.service
# systemctl stop ip6tables.service
 systemctl status iptables.service
iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled)
   Active: inactive (dead)
Nov 15 21:26:47 ip-172-30-1-83.ec2.internal systemd[1]: Stopped IPv4 firewall with iptables.
# systemctl status ip6tables.service
ip6tables.service - IPv6 firewall with ip6tables
   Loaded: loaded (/usr/lib/systemd/system/ip6tables.service; disabled)
   Active: inactive (dead)
Nov 15 21:26:52 ip-172-30-1-83.ec2.internal systemd[1]: Stopped IPv6 firewall with ip6tables.
# systemctl mask iptables.service
ln -s '/dev/null' '/etc/systemd/system/iptables.service'
# systemctl mask ip6tables.service
ln -s '/dev/null' '/etc/systemd/system/ip6tables.service'

Notice that we also disabled the IPv6 version of the iptables service. People tend to forget about that.
Now we are ready to install and start firewalld.

# yum install firewalld
# systemctl status firewalld.service
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: inactive (dead)
# systemctl start firewalld.service
# systemctl status firewalld.service
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since Sun 2015-11-15 21:31:53 EST; 7s ago
 Main PID: 10942 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─10942 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Nov 15 21:31:52 ip-172-30-1-83.ec2.internal systemd[1]: Starting firewalld - dynamic firewall dae.....
Nov 15 21:31:53 ip-172-30-1-83.ec2.internal systemd[1]: Started firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.

To use firewalld, we need to understand more about how network traffic is classified into different firewall zones. The firewalld.zones man pages cover this in a very clear and concise manner. I will reproduce the description here verbatim.

  • drop – Any incoming network packets are dropped, there is no reply. Only outgoing network connections are possible.
  • block – Any incoming network connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6. Only network connections initiated within this system are possible.
  • public – 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.
  • external – For use on external networks with masquerading enabled specifically for routers. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
  • dmz – For computers in your demilitarised zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted.
  • work – For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
  • home – For use in home areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
  • internal – For use in internal networks. You mostly trust the other computers on the networks to not harm your computer. Only selected incoming connections are accepted.
  • trusted – All network connections are accepted.

Configure firewalld rules

We will configure firewalld using the firewall-cmd.
Instead of running systemctl status firewalld, we can also use the firewall-cmd with the --state option to confirm that it has started.

# firewall-cmd --state
running

You can list the firewall zones we discussed earlier by using the --get-zones option.

# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

To view the default zone, it is as simple as specifying the --get-default-zone option. If the EC2 instance is inside its own VPC subnet, and can only be accessed via a jumpbox, then we may change the default zone to internal, and add or remove the services that were allowed by default.

# firewall-cmd --get-default-zone
public
# firewall-cmd --set-default-zone=internal
success
# firewall-cmd --list-all
internal (default)
  interfaces:
  sources:
  services: dhcpv6-client ipp-client mdns samba-client ssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

Running firewall-cmd with the --get-services option lists all the services that can be controlled by firewalld.

# firewall-cmd --get-services
RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https

If a service is not on the list, we can always define our own service, or add the port to be opened via firewall-cmd directly. Let’s demonstrate that – and prepare for our next article on SELinux at the same time – by changing the default port number for the OpenSSH service to port 31337. Assuming that we will use firewalld together with SELinux, we can enable the port by using the --add-port option. Note that if we want to make our changes permanent, we need to reload the firewall rules immediately.

# firewall-cmd --permanent --add-port=31337/tcp; firewall-cmd --reload
success
success
# firewall-cmd --list-all
internal (default)
  interfaces:
  sources:
  services: dhcpv6-client ipp-client mdns samba-client ssh
  ports: 31337/tcp
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

If we want to remove unneeded services such as dhcpv6-client, ipp-client, etc, we can do so with the --remove-service option.

# firewall-cmd --permanent --remove-service=dhcpv6-client --remove-service=ipp-client --remove-service=mdns --remove-service=samba-client; firewall-cmd --reload
success
success
# firewall-cmd --list-all
internal (default)
  interfaces:
  sources:
  services: ssh
  ports: 31337/tcp
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

You can take a peek at the iptables rules that firewall-cmd magically generated based on the commands we just issued:

# iptables -S | tail
-A FWDI_internal -j FWDI_internal_allow
-A FWDO_internal -j FWDO_internal_log
-A FWDO_internal -j FWDO_internal_deny
-A FWDO_internal -j FWDO_internal_allow
-A INPUT_ZONES -g IN_internal
-A IN_internal -j IN_internal_log
-A IN_internal -j IN_internal_deny
-A IN_internal -j IN_internal_allow
-A IN_internal_allow -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT
-A IN_internal_allow -p tcp -m tcp --dport 31337 -m conntrack --ctstate NEW -j ACCEPT

If you have managed to successfully follow along so far, great work! You will find that firewalld is a lot easier to work with than trying to directly configure iptables.

In our next article, we will explore setting up SELinux on Amazon Linux, and walking through a simple SELinux example on Red Hat Enterprise Linux. For more on Cloud server security, why not take Cloud Academy’s Linux Security course?
Comments? Please join in!

Avatar

Written by

Eugene Teo

Eugene Teo is a director of security at a US-based technology company. He is interested in applying machine learning techniques to solve problems in the security domain.

Related Posts

Alisha Reyes
Alisha Reyes
— July 22, 2019

Cloud Academy’s Blog Digest: July 2019

July has been a very exciting month for us at Cloud Academy. On July 10, we officially joined forces with QA, the UK’s largest B2B skills provider (read the announcement). Over the coming weeks, you will see additions from QA’s massive catalog of 500+ certification courses and 1500+ ins...

Read more
  • AWS
  • Azure
  • Cloud Academy
  • Cybersecurity
  • DevOps
  • Kubernetes
Avatar
Stuart Scott
— July 18, 2019

AWS Fundamentals: Understanding Compute, Storage, Database, Networking & Security

If you are just starting out on your journey toward mastering AWS cloud computing, then your first stop should be to understand the AWS fundamentals. This will enable you to get a solid foundation to then expand your knowledge across the entire AWS service catalog.   It can be both d...

Read more
  • AWS
  • Compute
  • Database
  • fundamentals
  • networking
  • Security
  • Storage
Avatar
Adam Hawkins
— July 17, 2019

How to Become a DevOps Engineer

The DevOps Handbook introduces DevOps as a framework for improving the process for converting a business hypothesis into a technology-enabled service that delivers value to the customer. This process is called the value stream. Accelerate finds that applying DevOps principles of flow, f...

Read more
  • AWS
  • AWS Certifications
  • DevOps
  • DevOps Foundation Certification
  • Engineer
  • Kubernetes
Avatar
Stuart Scott
— July 2, 2019

AWS Machine Learning Services

The speed at which machine learning (ML) is evolving within the cloud industry is exponentially growing, and public cloud providers such as AWS are releasing more and more services and feature updates to run in parallel with the trend and demand of this technology within organizations t...

Read more
  • Amazon Machine Learning
  • AWS
  • AWS re:Invent
  • Machine Learning
Avatar
Stuart Scott
— June 27, 2019

AWS Control Tower & VPC Traffic Mirroring

AWS re:Inforce 2019 is a two-day conference for security, identity, and compliance learning and community building. This year's keynote, presented by AWS Vice President and CIO, Stephen Schmidt, announced the general availability of AWS Control Tower and the new VPC Traffic Mirroring fe...

Read more
  • AWS
  • re:Inforce 2019
  • traffic mirroring
  • VPC
Avatar
Stuart Scott
— June 20, 2019

Working with AWS Networking & Amazon VPC

Being able to architect your own isolated segment of AWS is a simple process using VPCs; understanding how to architect its related networking components and connectivity architecture is key to making it a powerful service. Many services within Amazon Web Services (AWS) require you t...

Read more
  • AWS
  • VPC
Avatar
Stuart Scott
— June 19, 2019

AWS Compute Fundamentals Update

AWS is renowned for the rate at which it reinvents, revolutionizes, and meets customer demands and expectations through its continuous cycle of feature and service updates. With hundreds of updates a month, it can be difficult to stay on top of all the changes made available.   Here ...

Read more
  • AWS
Jeff Hyatt
Jeff Hyatt
— June 18, 2019

10 Steps for an Effective Reserved Instances Strategy

Amazon Web Services (AWS) offers three different ways to pay for EC2 Instances: On-Demand, Reserved Instances, and Spot Instances. This article will focus on effective strategies for purchasing Reserved Instances. While most of the major cloud platforms offer pre-pay and reservation dis...

Read more
  • AWS
  • EC2
Joe Nemer
Joe Nemer
— June 18, 2019

AWS Certification Practice Exam: What to Expect from Test Questions

If you’re building applications on the AWS cloud or looking to get started in cloud computing, certification is a way to build deep knowledge in key services unique to the AWS platform. AWS currently offers 11 certifications that cover major cloud roles including Solutions Architect, De...

Read more
  • AWS
  • AWS Certifications
Avatar
John Chell
— June 13, 2019

AWS Certified Solutions Architect Associate: A Study Guide

The AWS Solutions Architect - Associate Certification (or Sol Arch Associate for short) offers some clear benefits: Increases marketability to employers Provides solid credentials in a growing industry (with projected growth of as much as 70 percent in five years) Market anal...

Read more
  • AWS
  • AWS Certifications
Chris Gambino and Joe Niemiec
Chris Gambino and Joe Niemiec
— June 11, 2019

Moving Data to S3 with Apache NiFi

Moving data to the cloud is one of the cornerstones of any cloud migration. Apache NiFi is an open source tool that enables you to easily move and process data using a graphical user interface (GUI).  In this blog post, we will examine a simple way to move data to the cloud using NiFi c...

Read more
  • AWS
  • S3
Avatar
Chandan Patra
— June 11, 2019

Amazon DynamoDB: 10 Things You Should Know

Amazon DynamoDB is a managed NoSQL service with strong consistency and predictable performance that shields users from the complexities of manual setup. Whether or not you've actually used a NoSQL data store yourself, it's probably a good idea to make sure you fully understand the key ...

Read more
  • AWS
  • DynamoDB