Arthas vmtool 查杀 Tomcat Servlet/Listener/Valve 内存马:StandardContext 注册表定位与容器 API 移除
defense/detection · verified
一句话结论
对 Tomcat 中的基础 Servlet/Listener/Valve 内存马,可在 attach Arthas 后用 vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express '...' 直接读取 servletMappings、applicationEventListenersList、getPipeline().getValves() 定位恶意组件,再调用容器自带的 removeServletMapping、List.remove、removeValve 在不重启的情况下移除;Servlet 不能只删 servletMappings,否则 Wrapper 的映射仍在,且后续再调 removeServletMapping 会因取不到 name 而跳过清理。
原理
- Tomcat 把应用级组件保存在
StandardContext: - Servlet:
servletMappings(HashMap<urlPattern, servletName>)+children(Wrapper);addServletMappingDecoded同时把 pattern→name 写入 map,并在 Wrapper 上addMapping(pattern)。 - Listener:
applicationEventListenersList(CopyOnWriteArrayList),addApplicationEventListener只做list.add(listener)。 - Valve:
StandardPipeline的first/basic与 Valve 数组,addValve追加,removeValve从链中摘除。 - Servlet 坑点:
StandardContext.removeServletMapping(pattern)的实现是name = servletMappings.remove(pattern); Wrapper wrapper = findChild(name); if (wrapper != null) wrapper.removeMapping(pattern);。如果先手工servletMappings.remove("/exec"),map 已无该键,再调用removeServletMapping时remove返回null,findChild(null)为 null,wrapper.removeMapping不会执行;结果是恶意 Wrapper 仍持有/exec映射,路由仍可访问。 - 补救:已经误删 map 时,直接调用
findChild("execServlet").removeMapping("/exec")(或对 Wrapper 调用等价的移除)即可让路由失效;或者先removeServletMapping再确认 map/Wrapper 都已清理。 - Listener:
applicationEventListenersList是 List,只能按索引remove(index);移除后列表为空即生效。要先确认索引对应恶意 Listener,避免误删业务 Listener。 - Valve:
StandardPipeline.getValves()返回 Valve 数组(含NonLoginAuthenticator、恶意 Valve、StandardContextValve等);getPipeline().removeValve(valve)从链中摘除恶意 Valve,之后请求不再经过它。 - 多 Context:一个 Tomcat 可能有多个
StandardContext(如/manager与业务应用),instances[i]的索引随应用数量和加载顺序变化;案例中曾用instances[1]与instances[0],必须按StandardContext的 path/name 确认目标应用,不能机械照抄索引。
触发条件
- 已获得目标 JVM 的 attach 权限(同用户/同主机,或已通过漏洞拿到命令执行),可启动 Arthas 并 attach 到 Tomcat 进程。
- 目标为 Tomcat/Servlet 容器,内存马属于 Servlet/Listener/Valve 等基础组件型(非 Agent transformer 型)。
- 知道或能枚举出
StandardContext实例;业务应用上下文可写/可访问。 - 处置前已确认恶意组件(类名、路由、Listener 类型、Valve 类),避免误删业务组件。
利用步骤
- 启动 Arthas 并 attach 目标 Tomcat:
java -jar arthas-boot.jar,选择对应 PID。 - 枚举
StandardContext,确认业务应用的索引:bash vmtool --action getInstances --className org.apache.catalina.core.StandardContext vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[i]' - Servlet:查看
servletMappings,找出恶意路由(如/exec→execServlet):bash vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[i].servletMappings'用容器 API 移除:bash vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[i].removeServletMapping("/exec")'若已误删 map,补做 Wrapper 清理:bash vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[i].findChild("execServlet").removeMapping("/exec")' - Listener:查看
applicationEventListenersList,确认恶意 Listener 的索引后移除:bash vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[i].applicationEventListenersList' vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[i].applicationEventListenersList.remove(0)' - Valve:查看
getPipeline().getValves(),找到恶意 Valve 后移除:bash vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[i].getPipeline().getValves()' vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express '#valve=instances[i].getPipeline().getValves()[k],#ctx=instances[i],#ctx.getPipeline().removeValve(#valve)' - 逐项验证:再次访问原恶意路由应返回 404/不再执行命令;再查一次对应容器结构,确认恶意项已消失。
- 清理攻击入口(JSP、上传点、反序列化入口、WebShell),否则攻击者可重新注入。
PoC
# 0) 枚举 StandardContext,确认业务应用索引
vmtool --action getInstances --className org.apache.catalina.core.StandardContext
# 1) Servlet:查看映射并移除
vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[1].servletMappings'
vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[1].removeServletMapping("/exec")'
# 误删 map 后的补救
vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[1].findChild("execServlet").removeMapping("/exec")'
# 2) Listener:查看并移除第 0 个(恶意)Listener
vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[0].applicationEventListenersList'
vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[0].applicationEventListenersList.remove(0)'
# 3) Valve:查看并移除恶意 Valve
vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express 'instances[0].getPipeline().getValves()'
vmtool --action getInstances --className org.apache.catalina.core.StandardContext --express '#valve=instances[0].getPipeline().getValves()[1],#ctx=instances[0],#ctx.getPipeline().removeValve(#valve)'
验证:原恶意路由从可执行命令变为 404;servletMappings/applicationEventListenersList/getValves() 中不再出现恶意项。
变体与关联
- Filter 内存马:用
filterMaps.array+removeFilterMap(FilterMap)/filterMaps.remove,已有专卡defense-detection-tomcat-filter-memshell-hotpatch-vmtool-remove。 - ProxyValve(动态代理替换 first/basic):需解包
Proxy.getInvocationHandler还原原始 Valve,见defense-detection-proxyvalve-memshell-arthas-remove。 - Upgrade/Executor 内存马:见
defense-detection-tomcat-upgrade-executor-memshell-arthas-vmtool-remove。 - Agent/transformer 型内存马:
vmtool读InstrumentationImpltransformer 列表,见defense-detection-arthas-agent-memshell-detect-remove。 - 检测侧可先用
sc *.Servlet、sc javax.servlet.Filter、sc org.apache.catalina.core.StandardContext、jad、sc -d的code-source为空/匿名类等特征确认,见defense-detection-tomcat-memshell-scanner-arthas。
检测与防御
- 监控
StandardContext.addChild/addServletMappingDecoded/addApplicationEventListener/Pipeline.addValve等运行期注册调用;部署 RASP 或 Java Agent 记录调用栈与来源类。 - 排查无磁盘 class 文件的 Wrapper/Listener/Valve、陌生 URL 映射、
code-source为空的类;结合 Arthassc/jad人工确认。 - 业务侧最小化动态注册能力,Web 目录只读,及时修复上传/反序列化/表达式注入入口;处置后删除入口并复查容器注册表。
- 保留 Arthas 等应急工具的受控访问权限,避免被攻击者利用同一工具反向操作。