在Apparmor中创建自己的个人资料

要创建自己的配置文件,您将需要在Apparmor监视时运行程序。在这里,您将需要进行分类为程序正常使用的功能。这将包括启动,停止以及您希望在正常使用中包含的任何功能。

对于此示例,我将创建一个ls二进制程序的副本,该副本用于在您的系统上显示文件。首先,我们需要使用whereis命令找到二进制文件:

john@ubuntu1304:/bin$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

john@ubuntu1304:/bin$ sudo cp -p ls testls

john@ubuntu1304:/bin$ ls -l ls
-rwxr-xr-x 1 root root 108708 Jan 17 05:18 ls
john@ubuntu1304:/bin$ ls -l testls
-rwxr-xr-x 1 root root 108708 Jan 17 05:18 testls 

使用aa-genprof生成配置文件

用于生成配置文件的命令是aa-genprof

john@ubuntu1304:/bin$ sudo aa-genprof /bin/testls
Writing updated profile for /bin/testls.
Setting /bin/testls to complain mode.

Before you begin, you may wish to check if a
profile already exists for the application you
wish to confine. See the following wiki page for
more information:
http://wiki.apparmor.net/index.php/Profiles

Please start the application to be profiled in
another window and exercise its functionality now.

Once completed, select the "Scan" button below in
order to scan the system logs for AppArmor events.

For each AppArmor event, you will be given the
opportunity to choose whether the access should be
allowed or denied.

Profiling: /bin/testls

[(S)can system log for AppArmor events]/(F)inish 

当您在终端中看到以上内容时,必须保持aa-genprof运行。然后,您需要启动要为其创建配置文件的程序。接下来,您需要运行已确定的测试计划。您的测试计划越全面,以后遇到的问题就越少。(测试程序可用的所有可用功能)

执行完测试计划后,返回到原始终端,然后按S键在系统日志中扫描AppArmor事件。

在此过程结束时,系统将要求您保存个人资料。

Reading log entries from /var/log/syslog.
Updating AppArmor profiles in /etc/apparmor.d.
Complain-mode changes:

Profile:  /bin/testls
Path:     /etc/group
Mode:     r
Severity: 4

  1 - #include <abstractions/apache2-common>
  2 - #include <abstractions/lightdm>
  3 - #include <abstractions/nameservice>
 [4 - /etc/group]

[(A)llow]/(D)eny/(G)lob/Glob w/(E)xt/(N)ew/Abo(r)t/(F)inish/(O)pts

Profile:  /bin/testls
Path:     /etc/passwd
Mode:     r
Severity: 4

  1 - #include <abstractions/apache2-common>
  2 - #include <abstractions/lightdm>
  3 - #include <abstractions/nameservice>
 [4 - /etc/passwd]

[(A)llow]/(D)eny/(G)lob/Glob w/(E)xt/(N)ew/Abo(r)t/(F)inish/(O)pts
Adding /etc/passwd r to profile.

Profile:  /bin/testls
Path:     /home/john/
Mode:     r
Severity: 4

  1 - /home/john/
 [2 - /home/*/]

[(A)llow]/(D)eny/(G)lob/Glob w/(E)xt/(N)ew/Abo(r)t/(F)inish/(O)pts
Adding /home/*/ r to profile.

= Changed Local Profiles =

The following local profiles were changed.  Would you like to save them?

 [1 - /bin/testls]

(S)ave Changes/[(V)iew Changes]/Abo(r)t
Writing updated profile for /bin/testls.

Profiling: /bin/testls

[(S)can system log for AppArmor events]/(F)inish


Setting /bin/testls to enforce mode.
Reloaded AppArmor profiles in enforce mode.

Please consider contributing your new profile! See
the following wiki page for more information:
http://wiki.apparmor.net/index.php/Profiles

Finished generating profile for /bin/testls. 

现在我们可以看到我们的个人资料已添加:

john@ubuntu1304:/bin$ sudo apparmor_status
apparmor module is loaded.
22 profiles are loaded.
22 profiles are in enforce mode.
   /bin/testls
0 profiles are in complain mode.
4 processes have profiles defined.
4 processes are in enforce mode.
   /sbin/dhclient (785)
   /usr/lib/telepathy/mission-control-5 (1813)
   /usr/sbin/cupsd (522)
   /usr/sbin/cupsd (2508)
0 processes are in complain mode.
0 processes are unconfined but have a profile defined. 

启用complain模式并调整个人资料

创建个人资料后,您需要将个人资料置于complain模式。complain模式是AppArmor不限制其可以执行的操作,而是记录否则会发生的任何限制:

john@ubuntu1304:/bin$ sudo aa-complain /bin/testls
Setting /bin/testls to complain mode. 

进入complain模式后,请正常使用一段时间。在抱怨模式下正常使用它之后,运行以下命令来扫描系统日志中是否有错误并更新您的配置文件:

john@ubuntu1304:/bin$ sudo aa-logprof
Reading log entries from /var/log/syslog.
Updating AppArmor profiles in /etc/apparmor.d.
Complain-mode changes:

Profile:  /bin/testls
Path:     /bin/
Mode:     r
Severity: unknown


 1 - #include <abstractions/lightdm>
[2 - /bin/]

[(A)llow]/(D)eny/(G)lob/Glob w/(E)xt/(N)ew/Abo(r)t/(F)inish/(O)pts

Adding /bin/ r to profile.

Profile:  /bin/testls
Path:     /home/
Mode:     r
Severity: 4

[1 - /home/]

[(A)llow]/(D)eny/(G)lob/Glob w/(E)xt/(N)ew/Abo(r)t/(F)inish/(O)pts
Adding /home/ r to profile.

Profile:  /bin/testls
Path:     /usr/sbin/
Mode:     r
Severity: unknown


 1 - #include <abstractions/lightdm>
[2 - /usr/sbin/]

[(A)llow]/(D)eny/(G)lob/Glob w/(E)xt/(N)ew/Abo(r)t/(F)inish/(O)pts

= Changed Local Profiles =

The following local profiles were changed.  Would you like to save them?

[1 - /bin/testls]

(S)ave Changes/[(V)iew Changes]/Abo(r)t
Writing updated profile for /bin/testls. 

为了进行测试,我选择为目录/usr/sbin选择拒绝。稍后我们将看到此选择的影响。

使用强制模式锁定应用程序

调整完AppArmor配置文件后,需要启用强制模式来锁定应用程序:

john@ubuntu1304:/bin$ sudo aa-enforce /bin/testls
Setting /bin/testls to enforce mode.

现在我们已经对testls执行了策略,我们可以运行测试:

john@ubuntu1304:/bin$ testls /usr/sbin
testls: cannot open directory /usr/sbin: Permission denied 

现在我们可以看到,通过更早地选择Deny选项,阻止了testls命令在目录/usr/sbin上运行。

如果我们查看创建的实际配置文件,可以看到它具有被拒绝的选项:

john@ubuntu1304:/etc/apparmor.d$ cat bin.testls
# Last Modified: Fri May 10 22:18:20 2013
#include <tunables/global>

/bin/testls {
  #include <abstractions/base>


  deny /usr/sbin/ r,

  /bin/ r,
  /bin/testls mr,
  /etc/group r,
  /etc/nsswitch.conf r,
  /etc/passwd r,
  /home/ r,
  /home/*/ r,

}

了解Apparmor及其功能的最简单方法是创建测试配置文件并对其进行修改以查看获得的结果。

安装apparmor-utils

首先,您需要执行以下命令来安装apparmor-utilssudo apt-get install apparmor-utils

使用Apparmor控制资源

什么是AppArmor?AppArmor是一项安全功能,可以在许多Linux发行版中找到。SLES(SUSE Linux Enterprise Server),openSUSE和Ubuntu是该产品附带的一些发行版。Apparmor是内核增强功能,旨在将程序限制在有限的资源集中。Apparmor与其他安全工具的不同之处在于,它将访问控制属性绑定到程序而不是单个用户。

Apparmor的限制是由加载到内核中的特殊配置文件提供的。这些配置文件可以在两种模式下运行:complain modeenforce mode

Complain 模式

以这种方式加载的配置文件将不会强制执行策略。在这种模式下,将记录违反政策的情况。此配置文件对于开发配置文件很有用。使用aaa-complainaaa-enforce实用程序执行complain模式中的配置文件管理。

enforce 模式

以强制模式加载的配置文件将导致根据其配置文件强制执行其策略。

传统上,配置文件存储在以下位置:/etc/apparmor.d。使用以下命名约定存储在此位置下存储的配置文件:

/usr/sbin/cupsd 将创建为usr.sbin.cupsd

/etc/apparmor.d的示例

john@ubuntu1304:/etc/apparmor.d$ ls -l
total 80
drwxr-xr-x 3 root root 4096 Apr 15 08:35 abstractions
drwxr-xr-x 2 root root 4096 Apr 17 22:53 cache
drwxr-xr-x 2 root root 4096 Apr 15 08:33 disable
drwxr-xr-x 2 root root 4096 Dec  7 13:41 force-complain
-rw-r--r-- 1 root root  373 Apr  3 04:56 lightdm-guest-session
-rw-r--r-- 1 root root  461 Oct  1  2012 lightdm-remote-session-freerdp
-rw-r--r-- 1 root root  484 Oct  1  2012 lightdm-remote-session-uccsconfigure
drwxr-xr-x 2 root root 4096 Apr 15 08:35 local
-rw-r--r-- 1 root root 2234 Mar 14 21:20 sbin.dhclient
drwxr-xr-x 4 root root 4096 Apr 15 08:34 tunables
-rw-r--r-- 1 root root 5048 Feb 22 14:03 usr.bin.evince
-rw-r--r-- 1 root root 4380 Apr  4 21:13 usr.bin.firefox
-rw-r--r-- 1 root root 4843 Jan  9 22:21 usr.lib.telepathy
-rw-r--r-- 1 root root 4410 Apr 12 10:27 usr.sbin.cupsd
-rw-r--r-- 1 root root 1393 Nov 12 11:55 usr.sbin.rsyslogd
-rw-r--r-- 1 root root 1418 Aug 20  2012 usr.sbin.tcpdump 

上面的配置文件都存储为纯文本文件。

配置文件仅在执行时应用于流程。在重新启动进程之前,已经运行的程序将不会执行任何强制措施。

LINUX APPARMOR安全工具

查看AppArmor的状态

john@ubuntu1304:/etc/apparmor.d$ sudo apparmor_status
apparmor module is loaded.
21 profiles are loaded.
21 profiles are in enforce mode.

0 profiles are in complain mode.
3 processes have profiles defined.
3 processes are in enforce mode.
   /sbin/dhclient (785)
   /usr/lib/telepathy/mission-control-5 (1813)
   /usr/sbin/cupsd (522)
0 processes are in complain mode.
0 processes are unconfined but have a profile defined.

在这里我们可以看到我们定义了3个进程,它们处于强制模式

默认情况下,Ubuntu也包含Apparmor。默认情况下,Ubuntu包含一些可以使用的现有配置文件,但是,最有用的功能是能够创建自己的自定义配置文件,从而使您可以根据自己的需要来设置权限。

日期:2019-04-29 03:17:38 来源:oir作者:oir