Kategorie:Uncomplicated FireWall: Unterschied zwischen den Versionen
Zeile 78: | Zeile 78: | ||
22/tcp ALLOW IN Anywhere | 22/tcp ALLOW IN Anywhere | ||
22/tcp (v6) ALLOW IN Anywhere (v6) | 22/tcp (v6) ALLOW IN Anywhere (v6) | ||
== Andere Verbindungen erlauben == | == Andere Verbindungen erlauben == |
Version vom 15. Februar 2021, 12:45 Uhr
Uncomplicated Firewall ist eine Schnittstelle zur Konfiguration des Paketfilters iptables, die darauf ausgerichtet ist, die Konfiguration einer Firewall zu vereinfachen.
Installation
# apt install ufw
Konfiguration
Verwendung von UFW mit IPv6 (Optional)
Ist eine Unterstützung von IPv6 erforderlich, muß dies zunächst mit einem Eintrag in der Konfigurationsdatei aktiviert werden:
# vi /etc/default/ufw
Hier muß sichergestellt werden, daß der Eintrag IPV6=yes existiert:
# /etc/default/ufw # # Set to yes to apply rules to support IPv6 (no means only IPv6 on loopback # accepted). You will need to 'disable' and then 'enable' the firewall for # the changes to take affect. IPV6=yes
Wurde dieser Eintrag hinzugefügt, werden die entsprechenden Regeln sowohl für IPv4, als auch für IPv6 erstellt.
Standardrichtlinien
- Standardrichtlinien sollten zuerst erstellt werden & definieren das Verhalten der Firewall, wenn keine der anderen Regeln auf die jeweilige Situation passt.
- Die Standardeinstellung ist die, daß eingehende Verbindungen abgelehnt & ausgehende Verbindungen zugelassen werden - so wird sichergestellt, daß niemand den entsprechenden Rechner von außen erreichen kann, während Anwendungen mit der Außenwelt kommunizieren können.
- Dieses Standardverhalten läßt sich mit den folgenden Befehlen wiederherstellen:
# ufw default deny incoming # ufw default allow outgoing
- These firewall defaults alone might suffice for a personal computer, but servers typically need to respond to incoming requests from outside users.
SSH erlauben
Für den Fall, daß die Firewall auf einem entfernten Rechner eingerichtet werden soll, ist zwingend darauf zu achten, daß vor deren Aktiviierung der entsprechnede Port für SSH-Verbindungen zugelassen wird:
# ufw allow ssh
oder:
# ufw allow 22
Da mit der Datei /etc/services eine Liste existiert, in der für jeden Dienst eine Port-Nummer hinterlegt ist, kann bei der Erstellung der Regeln gewählt werden, ob der jeweilige Dienst (hier: ssh) oder die Port-Nummer (hier: 22) angegeben wird.
Sollte der Dienst SSH so konfiguriert worden sein, daß dieser einen abweichenden Port verwendet (hier: 2222), kann auch dieser bei Erstellung der Regel angegeben werden:
# ufw allow 2222
UFW aktivieren
Das Starten der Firewall erfolgt mit dem Kommando:
# ufw enable
Sollte die Einrichtung der Firewall über eine bereits aktive SSH-Sitzung erfolgen, wird eine Meldung ausgegeben, die davor warnt, daß die Verbindung gestört werden könnte:
# ufw enable Command may disrupt existing ssh connections. Proceed with operation (y|n)? y Firewall is active and enabled on system startup
Wurde die Regel für SSH-Verbindungen bereits erstellt, kann die Frage mit y(es) beantwortet werden.
Die Firewall sollte nun aktiv sein. Der aktuelle Status kann wie folgt abgefragt werden:
# ufw status verbose Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp ALLOW IN Anywhere 22/tcp (v6) ALLOW IN Anywhere (v6)
Andere Verbindungen erlauben
- At this point, you should allow all of the other connections that your server needs to respond to.
- The connections that you should allow depends on your specific needs.
- Luckily, you already know how to write rules that allow connections based on a service name or port; we already did this for SSH on port 22.
- You can also do this for:* HTTP on port 80, which is what unencrypted web servers use, using sudo ufw allow http or sudo ufw allow 80
- HTTPS on port 443, which is what encrypted web servers use, using sudo ufw allow https or sudo ufw allow 443
- There are several others ways to allow other connections, aside from specifying a port or known service.
Port-Bereiche angeben
- You can specify port ranges with UFW.
- Some applications use multiple ports, instead of a single port.
- For example, to allow X11 connections, which use ports 6000-6007, use these commands:
# ufw allow 6000:6007/tcp # ufw allow 6000:6007/udp
- When specifying port ranges with UFW, you must specify the protocol (tcp or udp) that the rules should apply to.
- We haven’t mentioned this before because not specifying the protocol automatically allows both protocols, which is OK in most cases.
Eine bestimmte IP-Adresse angeben
- When working with UFW, you can also specify IP addresses.
- For example, if you want to allow connections from a specific IP address, such as a work or home IP address of 203.0.113.4, you need to specify from, then the IP address:
# ufw allow from 203.0.113.4
- You can also specify a specific port that the IP address is allowed to connect to by adding to any port followed by the port number.
- For example, If you want to allow 203.0.113.4 to connect to port 22 (SSH), use this command:
# ufw allow from 203.0.113.4 to any port 22
Subnetze
If you want to allow a subnet of IP addresses, you can do so using CIDR notation to specify a netmask.
- For example, if you want to allow all of the IP addresses ranging from 203.0.113.1 to 203.0.113.254 you could use this command:
# ufw allow from 203.0.113.0/24
Likewise, you may also specify the destination port that the subnet 203.0.113.0/24 is allowed to connect to.
- Again, we’ll use port 22 (SSH) as an example:
# ufw allow from 203.0.113.0/24 to any port 22
Verbindungen über eine bestimmte Netzwerkkarte
- If you want to create a firewall rule that only applies to a specific network interface, you can do so by specifying “allow in on” followed by the name of the network interface.
- You may want to look up your network interfaces before continuing.
- To do so, use this command:
ip addr Output Excerpt 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state . . . 3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default . . .
- The highlighted output indicates the network interface names.
- They are typically named something like eth0 or enp3s2.
- So, if your server has a public network interface called eth0, you could allow HTTP traffic (port 80) to it with this command:
# ufw allow in on eth0 to any port 80
- Doing so would allow your server to receive HTTP requests from the public internet.
- Or, if you want your MySQL database server (port 3306) to listen for connections on the private network interface eth1, for example, you could use this command:
# ufw allow in on eth1 to any port 3306
This would allow other servers on your private network to connect to your MySQL database.
Verbindungen verbieten
- If you haven’t changed the default policy for incoming connections, UFW is configured to deny all incoming connections.
- Generally, this simplifies the process of creating a secure firewall policy by requiring you to create rules that explicitly allow specific ports and IP addresses through.
- However, sometimes you will want to deny specific connections based on the source IP address or subnet, perhaps because you know that your server is being attacked from there.
- Also, if you want to change your default incoming policy to allow (which is not recommended), you would need to create deny rules for any services or IP addresses that you don’t want to allow connections for.
- To write deny rules, you can use the commands described above, replacing allow with deny.
For example, to deny HTTP connections, you could use this command:
# ufw deny http
Or if you want to deny all connections from 203.0.113.4 you could use this command:
# ufw deny from 203.0.113.4
Now let’s take a look at how to delete rules.
Regeln löschen
- Knowing how to delete firewall rules is just as important as knowing how to create them.
- There are two different ways to specify which rules to delete: by rule number or by the actual rule (similar to how the rules were specified when they were created).
- We’ll start with the delete by rule number method because it is easier.
By Rule Number
If you’re using the rule number to delete firewall rules, the first thing you’ll want to do is get a list of your firewall rules.
- The UFW status command has an option to display numbers next to each rule, as demonstrated here:
# ufw status numbered Numbered Output: Status: active To Action From -- ------ ---- [ 1] 22 ALLOW IN 15.15.15.0/24 [ 2] 80 ALLOW IN Anywhere
If we decide that we want to delete rule 2, the one that allows port 80 (HTTP) connections, we can specify it in a UFW delete command like this:
# ufw delete 2
This would show a confirmation prompt then delete rule 2, which allows HTTP connections.
- Note that if you have IPv6 enabled, you would want to delete the corresponding IPv6 rule as well.
By Actual Rule
The alternative to rule numbers is to specify the actual rule to delete.
- For example, if you want to remove the allow http rule, you could write it like this:
# ufw delete allow http
You could also specify the rule by allow 80, instead of by service name:
# ufw delete allow 80
This method will delete both IPv4 and IPv6 rules, if they exist.
Checking UFW Status and Rules
At any time, you can check the status of UFW with this command:
# ufw status verbose
If UFW is disabled, which it is by default, you’ll see something like this:
Status: inactive
- If UFW is active, which it should be if you followed Step 3, the output will say that it’s active and it will list any rules that are set.
- For example, if the firewall is set to allow SSH (port 22) connections from anywhere, the output might look something like this:
Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp ALLOW IN Anywhere
Use the status command if you want to check how UFW has configured the firewall.
Disabling or Resetting UFW (optional)
If you decide you don’t want to use UFW, you can disable it with this command:
# ufw disable
Any rules that you created with UFW will no longer be active.
- You can always run sudo ufw enable if you need to activate it later.
If you already have UFW rules configured but you decide that you want to start over, you can use the reset command:
# ufw reset
This will disable UFW and delete any rules that were previously defined.
- Keep in mind that the default policies won’t change to their original settings, if you modified them at any point.
- This should give you a fresh start with UFW.
Conclusion
- Your firewall is now configured to allow (at least) SSH connections.
- Be sure to allow any other incoming connections that your server requires, while limiting any unnecessary connections, so your server will be functional and secure.
- To learn about more common UFW configurations, check out the UFW Essentials: Common Firewall Rules and Commands tutorial.
Links
Intern
TODO
Extern
Seiten in der Kategorie „Uncomplicated FireWall“
Diese Kategorie enthält nur die folgende Seite.