# 18.7.MAC Jail 中的 Nagios

本节演示了在 MAC 环境中实施 Nagios 网络监控系统所需的步骤。此示例仍然要求管理员在将实现的策略用于生产环境之前，测试其是否符合网络的安全要求。

此示例要求每个文件系统设置为 `multilabel`。它还假设 [net-mgmt/nagios-plugins](https://cgit.freebsd.org/ports/tree/net-mgmt/nagios-plugins/)、[net-mgmt/nagios](https://cgit.freebsd.org/ports/tree/net-mgmt/nagios/) 和 [www/apache22](https://cgit.freebsd.org/ports/tree/www/apache22/) 都已安装、配置并正常工作，然后再尝试将其集成到 MAC 框架中。

## 18.7.1. 创建不安全的用户类

首先，在 **/etc/login.conf** 中添加以下用户类：

```ini
insecure:\
:copyright=/etc/COPYRIGHT:\
:welcome=/etc/motd:\
:setenv=MAIL=/var/mail/$,BLOCKSIZE=K:\
:path=~/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
:manpath=/usr/share/man /usr/local/man:\
:nologin=/usr/sbin/nologin:\
:cputime=1h30m:\
:datasize=8M:\
:vmemoryuse=100M:\
:stacksize=2M:\
:memorylocked=4M:\
:memoryuse=8M:\
:filesize=8M:\
:coredumpsize=8M:\
:openfiles=24:\
:maxproc=32:\
:priority=0:\
:requirehome:\
:passwordtime=91d:\
:umask=022:\
:ignoretime@:\
:label=biba/10(10-10):
```

然后，在默认用户类部分添加以下行：

```ini
:label=biba/high:
```

保存编辑后，执行以下命令以重建数据库：

```shell-session
# cap_mkdb /etc/login.conf
```

## 18.7.2. 配置用户

使用以下命令将 `root` 用户设置为默认类：

```shell-session
# pw usermod root -L default
```

所有非 `root` 用户账户现在都将需要登录类。如果没有指定登录类，用户将无法访问常用命令。以下的 `sh` 脚本可以解决这个问题：

```shell-session
# for x in `awk -F: '($3 >= 1001) && ($3 != 65534) { print $1 }' \
	/etc/passwd`; do pw usermod $x -L default; done;
```

接下来，将 `nagios` 和 `www` 账户放入不安全类：

```shell-session
# pw usermod nagios -L insecure
# pw usermod www -L insecure
```

## 18.7.3. 创建上下文文件

现在应该创建一个名为 **/etc/policy.contexts** 的上下文文件：

```ini
# 这是此系统的默认 BIBA 策略。

# 系统:
/var/run(/.*)?			biba/equal

/dev/(/.*)?			biba/equal

/var				biba/equal
/var/spool(/.*)?		biba/equal

/var/log(/.*)?			biba/equal

/tmp(/.*)?			biba/equal
/var/tmp(/.*)?			biba/equal

/var/spool/mqueue		biba/equal
/var/spool/clientmqueue		biba/equal

# 对于 Nagios:
/usr/local/etc/nagios(/.*)?	biba/10

/var/spool/nagios(/.*)?		biba/10

# 对于 apache
/usr/local/etc/apache(/.*)?	biba/10
```

该策略通过对信息流设置限制来强制实施安全性。在此特定配置中，包括 `root` 用户在内的用户不应访问 Nagios。Nagios 的配置文件和进程将完全自给自足或被监禁。

此文件将在对每个文件系统运行 `setfsmac` 后读取。此示例设置了根文件系统上的策略：

```shell-session
# setfsmac -ef /etc/policy.contexts /
```

接下来，将这些编辑添加到 **/etc/mac.conf** 的主部分：

```shell-session
default_labels file ?biba
default_labels ifnet ?biba
default_labels process ?biba
default_labels socket ?biba
```

## 18.7.4. 加载器配置

为了完成配置，向 **/boot/loader.conf** 中添加以下几行：

```ini
mac_biba_load="YES"
mac_seeotheruids_load="YES"
security.mac.biba.trust_all_interfaces=1
```

并将以下行添加到 **/etc/rc.conf** 中的网络卡配置中。如果主要的网络配置通过 DHCP 完成，则可能需要在每次系统启动后手动配置此项：

```ini
maclabel biba/equal
```

## 18.7.5. 测试配置

首先，确保 web 服务器和 Nagios 在系统初始化和重启时不会启动。确保 `root` 用户无法访问 Nagios 配置目录中的任何文件。如果 `root` 能列出 **/var/spool/nagios** 中的内容，则说明配置有问题。相反，应该返回“权限被拒绝”的错误。

如果一切正常，现在可以启动 Nagios、Apache 和 Sendmail：

```shell-session
# cd /etc/mail && make stop && \
setpmac biba/equal make start && setpmac biba/10\(10-10\) apachectl start && \
setpmac biba/10\(10-10\) /usr/local/etc/rc.d/nagios.sh forcestart
```

再次检查以确保一切正常。如果没有，检查日志文件中的错误信息。如有需要，可以使用 [sysctl(8)](https://man.freebsd.org/cgi/man.cgi?query=sysctl\&sektion=8\&format=html) 禁用 [mac\_biba(4)](https://man.freebsd.org/cgi/man.cgi?query=mac_biba\&sektion=4\&format=html) 安全策略模块，然后重新启动所有服务。

> **注意**
>
> `root` 用户仍然可以更改安全执行并编辑其配置文件。以下命令将允许为新启动的 shell 降级安全策略：`# setpmac biba/10 csh`。为了阻止这种情况发生，可以通过 [login.conf(5)](https://man.freebsd.org/cgi/man.cgi?query=login.conf\&sektion=5\&format=html) 强制用户进入一个范围。如果 [setpmac(8)](https://man.freebsd.org/cgi/man.cgi?query=setpmac\&sektion=8\&format=html) 尝试执行超出隔离范围的命令，将返回错误并且命令不会执行。在这种情况下，将 root 设置为 `biba/high(high-high)`。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/hanbook/di-18-zhang-qiang-zhi-fang-wen-kong-zhi/18.7.-mac-jail-zhong-de-nagios.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
