You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Falco规则中捕获进程或命令的输出?

Falco规则问题解决方案

一、编写依赖script.sh输出的规则

Falco无法直接获取进程输出内容,但可通过以下方式实现需求:

  • 步骤1:捕获script.sh执行事件
    先编写规则匹配script.sh的执行动作,获取进程ID:
    - rule: Script.sh Execution Detected
      desc: Catch when script.sh is executed
      condition: evt.type=execve and proc.cmdline contains "script.sh"
      output: "script.sh started (PID: %proc.pid, Command: %proc.cmdline)"
      priority: WARNING
    
  • 步骤2:外部脚本检查输出
    配置Falco将告警转发到自定义脚本,利用进程ID获取输出:
    1. 在Falco配置文件(如/etc/falco/falco.yaml)中添加exec类型输出:
      outputs:
        - name: script_output_check
          type: exec
          cmd: "/opt/check_script_output.sh %proc.pid"
      
    2. 编写check_script_output.sh,通过/proc/<PID>/fd/1读取脚本标准输出(仅脚本运行时有效,若脚本已退出需提前将输出写入文件):
      #!/bin/bash
      PID=$1
      OUTPUT=$(cat /proc/$PID/fd/1 2>/dev/null)
      if echo "$OUTPUT" | grep -q "risky_content"; then
        echo "ALERT: script.sh output contains risky content: $OUTPUT" >> /var/log/falco_custom_alerts.log
      fi
      

二、FIM规则中syslog字段为空的解决方法

你的规则匹配文件close事件,而syslog.*字段仅在**syslog相关系统调用(如syslog、vsyslog)**触发的事件中填充,两者属于不同事件流,因此当前规则无法获取syslog字段。

正确捕获syslog消息的规则

若要捕获logger test产生的syslog事件,需编写针对syslog调用的规则:

- rule: Syslog Message Sent
  desc: Detect when logger command sends syslog messages
  condition: evt.type=syslog or evt.type=vsyslog
  output: "Syslog message detected (Process: %proc.name, Message: %syslog.message, Facility: %syslog.facility.str)"
  priority: WARNING

关联文件操作与syslog(可选)

如需同时监控文件修改和后续syslog发送,可通过进程上下文关联:

- rule: File Modified Followed by Syslog
  desc: Alert when a file is modified and syslog is sent by the same process
  condition: (evt.type=close and access_log_files) and proc.pid in (proc.pid where evt.type=syslog)
  output: "File modified then syslog sent (Process: %proc.name, File: %evt.args, Syslog Msg: %syslog.message)"
  priority: WARNING

内容的提问来源于stack exchange,提问作者RAUL RODRIGUEX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 10:36:08