fail2ban: Harden service

This hardens the fail2ban service by giving it only the capabilities and
read/write access it needs.

This is done in accordance to the Arch Wiki article [1] where further
information about the needed capabilities and read/write paths can be
found.

[1] https://wiki.archlinux.org/title/Fail2ban#Service_hardening
This commit is contained in:
histalek 2021-12-05 14:22:51 +01:00
parent 8caa2909f1
commit d822f8544d
No known key found for this signature in database
GPG key ID: ED1D6449704FDE03
3 changed files with 55 additions and 0 deletions

View file

@ -23,6 +23,24 @@
state: "{{ fail2ban_package_state }}" state: "{{ fail2ban_package_state }}"
become: true become: true
- name: Create fail2ban logging directory.
file:
path: "/var/log/fail2ban"
state: directory
mode: '0700'
owner: 'root'
group: 'root'
become: true
- name: Create fail2ban systemd drop-in directory.
file:
path: "/etc/systemd/system/fail2ban.service.d"
state: directory
mode: '0755'
owner: 'root'
group: 'root'
become: true
- name: Deploy fail2ban jail config. - name: Deploy fail2ban jail config.
template: template:
src: "jail.local.j2" src: "jail.local.j2"
@ -33,6 +51,26 @@
notify: restart fail2ban service notify: restart fail2ban service
become: true become: true
- name: Deploy fail2ban config.
template:
src: "fail2ban.local.j2"
dest: "/etc/fail2ban/fail2ban.local"
mode: '0644'
owner: 'root'
group: 'root'
notify: restart fail2ban service
become: true
- name: Deploy fail2ban hardening systemd drop-in.
template:
src: override.conf.j2
dest: /etc/systemd/system/fail2ban.service.d/override.conf
mode: '0644'
owner: 'root'
group: 'root'
notify: restart fail2ban service
become: true
- name: Ensure fail2ban service is enabled and started. - name: Ensure fail2ban service is enabled and started.
service: service:
name: fail2ban name: fail2ban

View file

@ -0,0 +1,4 @@
{{ ansible_managed | comment }}
[Definition]
logtarget = /var/log/fail2ban/fail2ban.log

View file

@ -0,0 +1,13 @@
{{ ansible_managed | comment }}
[Service]
PrivateDevices=yes
PrivateTmp=yes
ProtectHome=read-only
ProtectSystem=strict
ReadWritePaths=-/var/run/fail2ban
ReadWritePaths=-/var/lib/fail2ban
ReadWritePaths=-/var/log/fail2ban
ReadWritePaths=-/var/spool/postfix/maildrop
ReadWritePaths=-/run/xtables.lock
CapabilityBoundingSet=CAP_AUDIT_READ CAP_DAC_READ_SEARCH CAP_NET_ADMIN CAP_NET_RAW