monkeypatches以最小化在apparmor下运行python所需的权限

apparmor_monkeys的Python项目详细描述


https://codecov.io/github/Jc2k/apparmor_monkeys/coverage.svg?branch=master

monkeypatches以最小化在apparmor下运行python所需的权限。

这是干什么的?

假设您编写了一个简单的django应用程序。也许你只是跟着 Django教程。您的代码所做的只是一点数据库查询。那么你 在Apparmor下部署,然后:

type=AVC msg=audit(1443087838.797:1078): apparmor="DENIED" operation="exec" profile="helloworld-application" name="/bin/dash" pid=8202 comm="python" requested_mask="x" denied_mask="x" fsuid=999 ouid=0

突然间,你的审计日志中满是抱怨你的应用程序 正在尝试运行shell。你当然不是为了这个而写代码的。什么 继续吗?你被黑了吗?

你(可能;)没有被黑。

在做一些非常平凡的事情的时候发现了python外壳。到现在为止 您可以:

  • Ignore it. Let your audit log have ^{tt1}$ entries that you have to ignore. Now it’s hard to spot suspicious behaviour in your monitoring. Thinks might be broken, thinks might not be broken. Hope you don’t have to go through that log with a security professional.
  • Allow it. Now your profile is broader than it needs to be. You are throwing away the security you gain in the first place. Hope you don’t have to go through that profile with a security professional.

这个包修补了几个stdlib api以避免subprocess使用, 让您保持简单的配置文件和干净的审计日志。

cTypes vs ldconfig

应用程序所做的第一件事就是尝试运行 gcc。结果证明,这是一个回退,因为之前有人试图运行 ldconfig失败。

这就是ctypes.util.find_library的工作原理。这个可以用 在一些地方:

  • Gunicorn uses it for its sendfile implementation.
  • Python’s ^{tt6}$ module uses for ^{tt7}$. Just importing the ^{tt6}$ module triggers this, even if you aren’t using ^{tt7}$.

platform.uname与os.uname

platform.uname基本上与os.uname相同,但是有一个额外的 字段。该字段的源代码是通过执行uname -p

sh -c "uname %s 2>%s"

这在几个地方使用:

  • A command trick for getting your own version number is ^{tt13}$, which triggers it.
  • Gunicorn triggers it via a ^{tt14}$ in ^{tt15}$ before it even loads your code.

激活猴子补丁

手动

尽可能早地输入代码:

from apparmor_monkeys import patch_modules
patch_modules()

自动通过.pthhooks

扫描site-packages目录中的.pth文件。它们按顺序处理,通常只是要添加到sys.path的路径列表。但是,任何import行都将被兑现。

在virtualenvssite-packages目录中创建一个apparmor-monkeys.pth,其中包含:

import apparmor_monkeys; apparmor_monkeys.patch_modules()

要想让这个把戏奏效,就必须在一条线上。

切换配置文件

初始化应用程序后,您可以使用change_profile切换到其他配置文件,进一步强化Apparmor配置文件。

如果使用多进程gunicorn(即同步gunicorn),则可以包装 你的员工在他们自己的个人资料中。在Gunicorn配置中,可以添加 这样做的钩子:

from apparmor_monkeys import change_profile

def post_fork(server, worker):
    change_profile("myapplication//worker")

你也可以为芹菜做这个:

from apparmor_monkeys import change_profile
from celery import signals

@signals.worker_process_init.connect
def switch_apparmor_profile(sender=None, signal=None):
    change_profile("tenselfservice-worker//worker")

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java死锁2:完全用DB设置控制限制   使用jacksonjava将JSON转换为POJO作为对象类   具有空对象模式的java DAO   java如何检查ExecutorService是否“健康”且正常工作?   java在Intellij Idea下使用Maven和ReactJS启动SpringWeb应用程序项目   java无法在同时键入文本字段时更新TextArea   swing Java投票程序错误   java如何在Android应用程序中执行OCR   java如何用自己的JDK版本配置JBoss?   用于屏蔽字符串起始8个字符的java正则表达式   java如何在Firebase中获取被邀请者的电子邮件地址?   在OSGi中访问LeapMotion API时发生java异常\u访问\u冲突   Hadoop发出的本机snappy压缩数据无法通过javasnappy版本提取   未生成java servlet类,用于启动tomcat的JDK版本   解压缩java中的嵌套jar文件   java我需要在输入的程序中输入冒号后大写所有内容   java响应状态:未授权   java是什么造就了Hashmap。putIfAbsent比Containeskey快,然后是put?   jquery终端模拟器java上的Jsonrpc服务   java我们如何导入非公共类?