Rob's web

Firewall

IPtables

The firewall iptables is replaced as of Centos 7 by firewalld.

Firewalld

firewalld is a firewall management tool for Linux operating systems. It provides firewall features by acting as a front-end for the Linux kernel's netfilter framework via the nftables userspace utility (before v0.6.0 iptables backend), acting as an alternative to the nft command line program. The name firewalld adheres to the Unix convention of naming system daemons by appending the letter "d".

Features

firewalld supports both IPv4 and IPv6 networks and can administer separate firewall zones with varying degrees of trust as defined in zone profiles. Administrators can configure Network Manager to automatically switch zone profiles based on known Wi-Fi (wireless) and Ethernet (wired) networks, but firewalld cannot do this on its own.

Services and applications can use the D-Bus interface to query and configure the firewall. firewalld supports timed rules, meaning the number of connections (or "hits") to a service can be limited globally. There is no support for hit-counting and subsequent connection rejection per source IP; a common technique deployed to limit the impact of brute-force hacking and distributed denial-of-service attacks.

firewalld's command syntax is similar to but more verbose than other iptables front-ends like Ubuntu's Uncomplicated Firewall (ufw). The command-line interface allows managing firewall rulesets for protocol, ports, source and destination; or predefined services by name.

Services are defined as XML files containing port- and protocol-mappings, and optionally extra information like specifying subnets and listing required Kernel helper modules.[8] The syntax resembles that of systemd's service files. A simple service file for a web server listening on TCP port 443 might look like this:

<?xml version="1.0" encoding="utf-8"?>
<service> <short>Web Server</short> <description>Public web host over HTTPS.</description> <port port="443" protocol="tcp" /> </service>

Adoption

firewalld ships by default on the following Linux distributions:

firewalld is enabled by default in all of these distributions.

Starting

# systemctl start firewalld.service
# systemctl enable firewalld.service

Adding firewall rules

# firewall-cmd --permanent --zone=public --add-service=xxxx
# firewall-cmd --reload

Adding firewall ports

# firewall-cmd --permanent --zone=public --add-port=xxxx/prot
# firewall-cmd --reload

For a range use --add-port=10080-10100/tcp.

Exploring the Defaults

We can see which zone is currently selected as the default by typing:

# firewall-cmd --get-default-zone

Since we haven't given firewalld any commands to deviate from the default zone, and none of our interfaces are configured to bind to another zone, that zone will also be the only "active" zone (the zone that is controlling the traffic for our interfaces). We can verify that by typing:

# firewall-cmd --get-active-zones

How do we know what rules are associated with the public zone though? We can print out the default zone's configuration by typing:

# firewall-cmd --list-all

Blacklists

# yum install ipset-service

The blacklists are stored in memory and not in files. The blacklist should be saved on stop or restart of the iptables and reloaded on start.

Starting ipset

# systemctl enable ipset

Now we have to create the actual blacklists.

Creating blacklists

To list the IP sets known to firewalld in the permanent environment, use the following command as root:

# firewall-cmd --get-ipset-types

To add a new IP set, use the following command using the permanent environment as root:

# firewall-cmd --permanent --new-ipset=blacklist4 --type=hash:ip
# firewall-cmd --permanent --new-ipset=blacklist6 --type=hash:net --option=family=inet6
# firewall-cmd --permanent --zone=drop --add-source=ipset:blacklist4
# firewall-cmd --permanent --zone=drop --add-source=ipset:blacklist6
# firewall-cmd --reload

The previous command creates a new IP set with the name test and the hash:net type for IPv4. To create an IP set for use with IPv6, add the --option=family=inet6 option. To make the new setting effective in the runtime environment, reload firewalld. List the new IP set with the following command as root:

# firewall-cmd --permanent --get-ipsets
blacklist4 blacklist6

Now we have a IPv4 and IPv6 blacklist.

Adding to the blacklists

To add a single IPv4 address to the blacklist enter:

firewall-cmd --permanent --ipset=blacklist4 --add-entry=192.168.0.1

To add a IPv6 address to the blacklist enter the prefix only:

# firewall-cmd --permanent --ipset=blacklist6 --add-entry=2001:0db8::/64

I made a script for adding IPv4 addresses.

# cat /usr/bin/abl4
#! /bin/bash
# Add a ip4 address to the blacklist.

firewall-cmd --permanent --ipset=blacklist4 --add-entry=$1
firewall-cmd --reload

Checking the blacklists

To get the list of current entries in the IPv4 set, use the following command as root:

# firewall-cmd --permanent --ipset=blacklist4 --get-entries

Removing from the blacklist

To remove a single IPv4 address to the blacklist enter:

firewall-cmd --permanent --ipset=blacklist4 --remove-entry=192.168.0.1

Final

After you have added the firewall rules and tested it on your LAN, you can do portforwarding in your ISP-router to your server per service or put the server in the DMZ (can only be done with one server and is less save). So don't use DMZ but port forwarding.

Links