spring cloud SnakeYAML RCE 漏洞复现

漏洞成因

通过/env post提交数据以设置属性spring.cloud.bootstrap.location为外部恶意yml文件,再通过/refresh获取yml文件内容并解析导致漏洞。

debug过程

由于最后是通过/refresh请求导致的,故这次从refresh相关代码开始,在RefreshEndpoint类中,有这么一段代码:

	@ManagedOperation
    public String[] refresh() {
    
    
        Set<String> keys = this.contextRefresher.refresh();
        return (String[])keys.toArray(new String[keys.size()]);
    }

    public Collection<String> invoke() {
    
    
        return Arrays.asList(this.refresh());
    }

之后会来到contextRefresher.refresh()中:

public synchronized Set<String> refresh() {
    
    
        Map<String, Object> before = this.extract(this.context.getEnvironment().getPropertySources());
        this.addConfigFilesToEnvironment();
        Set<String> keys = this.changes(before, this.extract(this.context.getEnvironment().getPropertySources())).keySet();
        this.context.publishEvent(new EnvironmentChangeEvent(keys));
        this.scope.refreshAll();
        return keys;
    }

其中,这次漏洞是在

this.addConfigFilesToEnvironment();

中产生的,这段代码的功能是获取最新的配置,而恶意yml文件会在其中获取并被解析。

其后再通过YamlProcessor.process方法内的yaml.loadAll()进行加载、解析配置
在这里插入图片描述
通过BaseConstructor类进行解析yml文件操作
在这里插入图片描述配上当时节点内容
在这里插入图片描述目前已将yml文件内容全部解析成SequenceNode实例
最终一步一步反射获取内容实例并读取到恶意代码
在这里插入图片描述这里就直接反射调用了自定义类AwesomeScriptEngineFactory,并执行了其中的payload

Runtime.getRuntime().exec("cmd.exe /c calc");

修复建议

禁止/env未授权访问
配置文件加入以下代码:

endpoints.env.enabled= false

升级为最新版本

猜你喜欢

转载自blog.csdn.net/qq_40519543/article/details/121378114