将auditd记录发送到我的audispd插件。

在设置了auditctl的规则之后,我想将匹配的记录发送到我的Python脚本进行进一步分析。 以下是涉及的文件:
  • auditd记录:

    type=PATH msg=audit(1451011319.268:533): ...
    type=CWD msg=audit(1451011319.268:533):  cwd=”/home/root”
    type=SYSCALL msg=audit(1451011319.268.230:533): ... key=(null)
    
  • /etc/audisp/audispd.conf如下所示:

    q_depth = 80
    overflow_action = ignore
    priority_boost = 4
    max_restarts = 10
    name_format = HOSTNAME
    #name = mydomain
    
  • /etc/audisp/plugin.d/中的audispd插件配置文件:

    active = yes
    direction = out
    path = /usr/bin/python
    type = always
    # two args, one is my Python script, the other is the log file
    args = /var/t/h.py /var/log/audit.log
    format = string
    
  • 我的h.py如下所示:

    # -*- coding: utf-8 -*-
    
    import sys
    
    print sys.argv[1]
    ...
    

然而,我的Python脚本无法从auditd获取任何记录。

我不知道哪里出了问题,请帮帮我!

1个回答

看起来 audispd 正在将审计事件写入其插件的标准输入。

(下面的源链接是相对于https://github.com/packetstash/auditd/tree/ba912fa614a7e73160a4eba338e55890d6e8f62f的。这是我在Server Fault上的第一篇帖子,无法包含超过两个链接)。

具体而言:

  • 它在audisp/audispd.c#L484处创建了一对套接字;
  • 然后进行分叉,将子进程的标准输入设置为套接字对的一端:audisp/audispd.c#L500
  • 然后将事件写入另一端:audisp/audispd.c#L533
您的脚本将继承自audispd的打开文件描述符,包括标准输出(stdout,fd #1),该文件描述符将被重新打开到/dev/null。因此,脚本中的print语句可能没有效果,您需要将内容写入某个文件。 可以尝试以下方法:
import sys

with open('/tmp/my_audit.log', 'w') as log_file:
  for event_message in sys.stdin:
    log_file.write('%s\n' % event_message)
你可能还想使用 bindings/python/auparse_python.c 模块来解析事件消息。