├── handlers └── main.yml ├── defaults └── main.yml ├── meta └── main.yml ├── tasks └── main.yml ├── templates ├── user6.rules.j2 └── user.rules.j2 └── README.md /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Restart ufw 3 | service: name=ufw 4 | state=restarted 5 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | ufw_connection_rate_limits: [ ] 3 | ufw_whitelisted_ipv4_addresses: [ ] 4 | ufw_whitelisted_ipv6_addresses: [ ] 5 | ufw_whitelisted_ports: 6 | - { port: 22, protocol: tcp } 7 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: "Joshua Lund" 4 | description: "Ansible role that installs and configures ufw, AKA The Uncomplicated Firewall (https://launchpad.net/ufw)" 5 | license: MIT 6 | min_ansible_version: 1.3 7 | platforms: 8 | - name: Ubuntu 9 | versions: 10 | - all 11 | - name: Debian 12 | versions: 13 | - all 14 | categories: 15 | - networking 16 | dependencies: [] 17 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Install ufw 3 | apt: pkg=ufw 4 | 5 | - name: Generate ufw rules 6 | template: src={{ item }}.j2 7 | dest=/lib/ufw/{{ item }} 8 | owner=root 9 | group=root 10 | mode=640 11 | with_items: 12 | - user.rules 13 | - user6.rules 14 | notify: Restart ufw 15 | 16 | - name: Enable ufw 17 | lineinfile: dest=/etc/ufw/ufw.conf 18 | regexp="^ENABLED" 19 | line="ENABLED=yes" 20 | notify: Restart ufw 21 | -------------------------------------------------------------------------------- /templates/user6.rules.j2: -------------------------------------------------------------------------------- 1 | *filter 2 | :ufw6-user-input - [0:0] 3 | :ufw6-user-output - [0:0] 4 | :ufw6-user-forward - [0:0] 5 | :ufw6-before-logging-input - [0:0] 6 | :ufw6-before-logging-output - [0:0] 7 | :ufw6-before-logging-forward - [0:0] 8 | :ufw6-user-logging-input - [0:0] 9 | :ufw6-user-logging-output - [0:0] 10 | :ufw6-user-logging-forward - [0:0] 11 | :ufw6-after-logging-input - [0:0] 12 | :ufw6-after-logging-output - [0:0] 13 | :ufw6-after-logging-forward - [0:0] 14 | :ufw6-logging-deny - [0:0] 15 | :ufw6-logging-allow - [0:0] 16 | ### RULES ### 17 | 18 | {% for item in ufw_whitelisted_ports %} 19 | ### tuple ### allow {{ item.protocol}} {{ item.port }} ::/0 any ::/0 in 20 | -A ufw6-user-input -p {{ item.protocol }} --dport {{ item.port }} -j ACCEPT 21 | 22 | {% endfor %} 23 | {% for tuple in ufw_whitelisted_ipv6_addresses %} 24 | ### tuple ### allow {{ tuple.protocol }} {{ tuple.port }} ::/0 any {{ tuple.address }} in 25 | -A ufw6-user-input -p {{ tuple.protocol }} --dport {{ tuple.port }} -s {{ tuple.address }} -j ACCEPT 26 | 27 | {% endfor %} 28 | ### END RULES ### 29 | 30 | ### LOGGING ### 31 | -A ufw6-after-logging-input -j LOG --log-prefix "[UFW BLOCK] " -m limit --limit 3/min --limit-burst 10 32 | -A ufw6-after-logging-forward -j LOG --log-prefix "[UFW BLOCK] " -m limit --limit 3/min --limit-burst 10 33 | -I ufw6-logging-deny -m state --state INVALID -j RETURN -m limit --limit 3/min --limit-burst 10 34 | -A ufw6-logging-deny -j LOG --log-prefix "[UFW BLOCK] " -m limit --limit 3/min --limit-burst 10 35 | -A ufw6-logging-allow -j LOG --log-prefix "[UFW ALLOW] " -m limit --limit 3/min --limit-burst 10 36 | ### END LOGGING ### 37 | COMMIT 38 | -------------------------------------------------------------------------------- /templates/user.rules.j2: -------------------------------------------------------------------------------- 1 | *filter 2 | :ufw-user-input - [0:0] 3 | :ufw-user-output - [0:0] 4 | :ufw-user-forward - [0:0] 5 | :ufw-before-logging-input - [0:0] 6 | :ufw-before-logging-output - [0:0] 7 | :ufw-before-logging-forward - [0:0] 8 | :ufw-user-logging-input - [0:0] 9 | :ufw-user-logging-output - [0:0] 10 | :ufw-user-logging-forward - [0:0] 11 | :ufw-after-logging-input - [0:0] 12 | :ufw-after-logging-output - [0:0] 13 | :ufw-after-logging-forward - [0:0] 14 | :ufw-logging-deny - [0:0] 15 | :ufw-logging-allow - [0:0] 16 | :ufw-user-limit - [0:0] 17 | :ufw-user-limit-accept - [0:0] 18 | ### RULES ### 19 | 20 | {% for limit in ufw_connection_rate_limits %} 21 | ### tuple ### limit {{ limit.protocol }} {{ limit.port }} 0.0.0.0/0 any 0.0.0.0/0 in 22 | -A ufw-user-input -p {{ limit.protocol }} --dport {{ limit.port }} -m state --state NEW -m recent --set 23 | -A ufw-user-input -p {{ limit.protocol }} --dport {{ limit.port }} -m state --state NEW -m recent --update --seconds 30 --hitcount 6 -j ufw-user-limit 24 | -A ufw-user-input -p {{ limit.protocol }} --dport {{ limit.port }} -j ufw-user-limit-accept 25 | 26 | {% endfor %} 27 | {% for item in ufw_whitelisted_ports %} 28 | ### tuple ### allow {{ item.protocol }} {{ item.port }} 0.0.0.0/0 any 0.0.0.0/0 in 29 | -A ufw-user-input -p {{ item.protocol }} --dport {{ item.port }} -j ACCEPT 30 | 31 | {% endfor %} 32 | {% for tuple in ufw_whitelisted_ipv4_addresses %} 33 | ### tuple ### allow {{ tuple.protocol }} {{ tuple.port }} 0.0.0.0/0 any {{ tuple.address }} in 34 | -A ufw-user-input -p {{ tuple.protocol }} --dport {{ tuple.port }} -s {{ tuple.address }} -j ACCEPT 35 | 36 | {% endfor %} 37 | ### END RULES ### 38 | 39 | ### LOGGING ### 40 | -A ufw-after-logging-input -j LOG --log-prefix "[UFW BLOCK] " -m limit --limit 3/min --limit-burst 10 41 | -A ufw-after-logging-forward -j LOG --log-prefix "[UFW BLOCK] " -m limit --limit 3/min --limit-burst 10 42 | -I ufw-logging-deny -m state --state INVALID -j RETURN -m limit --limit 3/min --limit-burst 10 43 | -A ufw-logging-deny -j LOG --log-prefix "[UFW BLOCK] " -m limit --limit 3/min --limit-burst 10 44 | -A ufw-logging-allow -j LOG --log-prefix "[UFW ALLOW] " -m limit --limit 3/min --limit-burst 10 45 | ### END LOGGING ### 46 | 47 | ### RATE LIMITING ### 48 | -A ufw-user-limit -m limit --limit 3/minute -j LOG --log-prefix "[UFW LIMIT BLOCK] " 49 | -A ufw-user-limit -j REJECT 50 | -A ufw-user-limit-accept -j ACCEPT 51 | ### END RATE LIMITING ### 52 | COMMIT 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Uncomplicated Firewall 2 | ======== 3 | 4 | Ansible role that installs and configures ufw, AKA [The Uncomplicated Firewall](https://launchpad.net/ufw). 5 | 6 | Role Variables 7 | -------------- 8 | 9 | **ufw_connection_rate_limits**: A list of port and protocol pairs that should be rate limited. The default is empty. According to the ufw man page, "ufw will deny connections if an IP address has attempted to initiate 6 or more connections in the last 30 seconds." *ufw currently only supports rate limits for incoming IPv4 connections.* The following example would limit TCP connections to the SSH port, TCP and UDP connections to the DNS port, and TCP connections to the MySQL port: 10 | 11 | ufw_connection_rate_limits: 12 | - { port: 22, protocol: tcp } 13 | - { port: 53, protocol: tcp } 14 | - { port: 53, protocol: udp } 15 | - { port: 3306, protocol: tcp } 16 | 17 | **ufw_whitelisted_ipv4_addresses**: A list of IPv4 address, port, and protocol tuples that the firewall should allow access to. The default is empty. This is a good way to ensure that certain services can only be reached by approved IP addresses. The following example would grant SSH access to 192.168.0.1 over TCP, and OpenVPN access to 10.0.0.1 over UDP: 18 | 19 | ufw_whitelisted_ipv4_addresses: 20 | - { address: 192.168.0.1, port: 22, protocol: tcp } 21 | - { address: 10.0.0.1, port: 1194, protocol: udp } 22 | 23 | **ufw_whitelisted_ipv6_addresses**: This variable behaves exactly the same as ufw_whitelisted_ipv4_addresses, except it applies to IPv6 addresses. The default is empty. The following example would allow Google's IPv6 address to access the DNS port over UDP, and Facebook's IPv6 address to access the Sphinx port over TCP. Note that it's important to enclose the IPv6 addresses in quotes, otherwise their colons will confuse the parser: 24 | 25 | ufw_whitelisted_ipv6_addresses: 26 | - { address: "2607:f8b0:4004:802::1001", port: 53, protocol: udp } 27 | - { address: "2a03:2880:2110:df07:face:b00c:0:1", port: 9312, protocol: tcp } 28 | 29 | **ufw_whitelisted_ports**: A list of port and protocol pairs that the firewall should allow access to. The default is to open port 22 over TCP. This variable applies to incoming connections from both IPv4 and IPv6 clients. If you wanted to allow access to SSH, DNS, and Nginx, you might do something like this: 30 | 31 | ufw_whitelisted_ports: 32 | - { port: 22, protocol: tcp } 33 | - { port: 53, protocol: udp } 34 | - { port: 80, protocol: tcp } 35 | - { port: 443, protocol: tcp } 36 | 37 | License 38 | ------- 39 | 40 | The MIT License (MIT) 41 | 42 | Copyright (c) 2014 Joshua Lund 43 | 44 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 45 | 46 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 47 | 48 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 49 | 50 | Author Information 51 | ------------------ 52 | 53 | You can find me on [Twitter](https://twitter.com/joshualund), and on [GitHub](https://github.com/jlund/). I also occasionally blog at [MissingM](http://missingm.co). 54 | --------------------------------------------------------------------------------