Create SELinux module for NRPE and check_fail2ban

If you need to protect a Linux host against unauthorized access, fail2ban is a very handy service. The application scans log files of various services, automatically detects failed logins and blocks attacker’s IP addresses. Especially on public hosts, it is essential to implement a protection like this on prominent services like SSH.

For monitoring bans, the project offers a Nagios / Icinga plugin named check_fail2ban: [click me!]

This plugin uses the fail2ban-client utility for checking bans – for this, a sudo rule is required for NRPE, e.g. for EL7:

nrpe ALL = (root) NOPASSWD: /usr/lib64/nagios/plugins/check_fail2ban

Unfortunately, the plugin was not working as expected in my environment.


Manually running the configured check on the monitoring server was not successful:

mon$ lib/nagios/plugins/check_nrpe -H myhost.localdomain.loc -c check_fail2ban
NRPE: Unable to read output

Well, this error message does not help at all – the NRPE log file should deliver more information. In the appropriate configuration file, a debug logging can be enabled:

# vi /etc/nagios/nrpe.cfg

# service nrpe restart

Depending on your rsyslog (or yet another logging service) configuration, those messages are stored in a separated log file. Under EL7 you will need to check the file /var/log/messages:

# tail -f /var/log/messages
May 27 23:59:12 myhost nrpe[11919]: Allowing connections from:,xx.xx.xx.xx
May 27 23:59:12 myhost systemd: Started NRPE.

Under EL7, there are no information about started processes and checks at all – for other Linux distributions this seems to work, a bug?

Next, I enabled a login shell for the NRPE service user and started the command manually using runuser:

# chsh -s /bin/bash nrpe
# runuser -l nrpe -c "/usr/lib64/nagios/plugins/check_fail2ban"

This time I stubmled upon a hint – it seems like sudo crashed the process:

sudo: sorry, you must have a tty to run sudo

sudo requires a fully-featured terminal session for granting extended permissions – which is not the case for NRPE.

The behavior can be disabled in the sudo configuration:

# visudo
#Defaults    requiretty


Alternatively, you can specify an exception for the NRPE user (nrpe in this case) – e.g. in the file that is required for the check_fail2ban plugin:

nrpe ALL = (root) NOPASSWD: /usr/lib64/nagios/plugins/check_fail2ban
Defaults:nrpe !requiretty

After altering the configuration file, the test was executed again – again, this was not successful. Time to have a look at SELinux and disable it temporary:

# setenforce 0
# service nrpe restart

Now, the plugin was working:

mon$ lib/nagios/plugins/check_nrpe -H myhost.localdomain.loc -c check_fail2ban
CHECK FAIL2BAN ACTIVITY - OK - 1 detected jails with 0 current banned IP(s)

So it seems like a valid SELinux module is missing for the monitoring plugin – we will focus on this in a moment.

Since the NRPE user was not the reason for the failure, it is recommended to change the login shell to its default value and enable SELinux again:

# chsh -s /sbin/nologin nrpe
# setenforce 1

Creating a SELinux module

Often, SELinux is the cause for strange errors. In this case, SELinux is not aware of the plugin’s behavior – and therefore drops the calls. Fortunately there are plenty of tools to define exceptions. All breaches of SELinux rules are documented in the file /var/log/audit/audit.log – based on this information it is possible to create SELinux modules that allow access. Using the audit2allow utility module drafts can be created – they are compiled into binary modules with the semodule_package command.

Before creating the module draft, it is a good idea to clear the audit log and restart the rebellious utility again – this will ensure that only required exceptions relevant to our plugin are defined. If additional software is dropped by SELinux at the same time, it is possible, that too many exceptions are created. The checkmodule command checks a module’s syntax, semodule installs the module.

# > /var/log/audit/audit.log
# service nrpe restart
(Run check)
# audit2allow >> nrpe_fail2ban.te << /var/log/audit/audit.log
# checkmodule -M -m -o nrpe_fail2ban.mod nrpe_fail2ban.te
# semodule_package -o nrpe_fail2ban.pp -m nrpe_fail2ban.mod
# semodule -i nrpe_fail2ban.pp

semodule can also be used to verify whether the module was loaded successfully:

# semodule -l | grep nrpe
nrpe_fail2ban   1.0

Time to run the test once again – after restarting NRPE:

# service nrpe restart

In my case, additional calls were dropped by SELinux – so let’s repeat the process again:

# > /var/log/audit/audit.log ; service nrpe restart
(Run check)
# audit2allow >> nrpe_fail2ban.te << /var/log/audit/audit.log

By executing the audit2allow command, we converted breach of rules into module syntax and appended the information into the pre-existing module draft (*.te file). To ensure syntax compliance we need to move the new rules to the top of the file. Afterwards, the module is created and installed after checking the module syntax – just like in the previous listing.

# checkmodule -M -m -o nrpe_fail2ban.mod nrpe_fail2ban.te
# semodule_package -o nrpe_fail2ban.pp -m nrpe_fail2ban.mod
# semodule -r nrpe_fail2ban ; semodule -i nrpe_fail2ban.pp

[alert style=”yellow”]Before installing the new SELinux module, the previously created one needs to be removed by executing semodule -r![/alert]

I repeated this 8 times until I had documented all the required permissions:

# cat nrpe_fail2ban.te

module nrpe_fail2ban 1.0;

require {
	type nrpe_t;
	class unix_dgram_socket sendto;
	class file execute;
	class file getattr;
	class file { read getattr open };
	class file execute_no_trans;
	type fail2ban_client_exec_t;
	class file { ioctl getattr };
	class file { read open };
	class file execute_no_trans;
	type fail2ban_var_run_t;
	class sock_file write;
	class file ioctl;
	type fail2ban_t;
	class unix_stream_socket connectto;

#============= nrpe_t ==============
allow nrpe_t self:unix_dgram_socket sendto;
allow nrpe_t fail2ban_client_exec_t:file getattr;
allow nrpe_t fail2ban_client_exec_t:file execute;
allow nrpe_t fail2ban_client_exec_t:file { read open };
allow nrpe_t fail2ban_client_exec_t:file execute_no_trans;
allow nrpe_t fail2ban_client_exec_t:file ioctl;
allow nrpe_t fail2ban_var_run_t:sock_file write;
allow nrpe_t fail2ban_t:unix_stream_socket connectto;

Sharing is caring

Leave a Reply