【Struts2-代码执行漏洞分析系列】S2-057

作者 : laolisafe 本文共6098个字,预计阅读时间需要16分钟 发布时间: 2018-08-24 共1.16K人阅读

漏洞公告
https://cwiki.apache.org/confluence/display/WW/S2-057
问题:
It is possible to perform a RCE attack when namespace value isn’t set for a result defined in underlying xml configurations and in same time, its upper action(s) configurations have no or wildcard namespace. Same possibility when using url tag which doesn’t have value and action set and in same time, its upper action(s) configurations have no or wildcard namespace.
漏洞发现者的博客: https://lgtm.com/blog/apache_struts_CVE-2018-11776
环境搭建
下载 https://archive.apache.org/dist/struts/2.5.16/struts-2.5.16-all.zip
IDEA中打开,修改apps/showcase/src/main/resources/struts-actionchaining.xml 为:


register2

同时查看 org/apache/struts2/default.properties:201 ,其值为true

Whether to always select the namespace to be everything before the last slash or not

struts.mapper.alwaysSelectFullNamespace=true
访问: http://localhost:8081/${(111+111)}/actionChain1.action

url变为: http://localhost:8081/222/register2.action
111+111=222 即产生了OGNL注入。
漏洞分析
这次的漏洞可以有多种攻击向量,根据漏洞作者blog有:
Redirect action
Action chaining
Postback result
以上提及的三种都属于Struts2的跳转方式。在 struts-default.xml:190(截取部分)





为清楚起见,这里解释一下strut2中对默认result对象的处理过程。这些默认result type都要经过 com/opensymphony/xwork2/DefaultActionInvocation.java:367 处理

private void executeResult() throws Exception {
result = createResult();

String timerKey = "executeResult: " + getResultCode();
try {
    UtilTimerStack.push(timerKey);
    if (result != null) {
        result.execute(this);
    } 
...

}
首先通过result = createResult()获取到相应的result对象。如果result不为null则执行result.execute(this);。这个execute方法则由具体result对象实现。

有一些具体的result对象比如下面提到的Redirect action和Postback result,会产生一个跳转地址location,并传入org/apache/struts2/result/StrutsResultSupport.java:194:

/**
* Implementation of the execute method from the Result interface. This will call
* the abstract method {@link #doExecute(String, ActionInvocation)} after optionally evaluating the
* location as an OGNL evaluation.
*
* @param invocation the execution state of the action.
* @throws Exception if an error occurs while executing the result.
*/
public void execute(ActionInvocation invocation) throws Exception {
lastFinalLocation = conditionalParse(location, invocation);
doExecute(lastFinalLocation, invocation);
}
而conditionalParse定义如下,将会执行OGNL表达式。

/**
* Parses the parameter for OGNL expressions against the valuestack
*
* @param param The parameter value
* @param invocation The action invocation instance
* @return the resulting string
*/
protected String conditionalParse(String param, ActionInvocation invocation) {
if (parse && param != null && invocation != null) {
return TextParseUtil.translateVariables(
param,
invocation.getStack(),
new EncodingParsedValueEvaluator());
} else {
return param;
}
}
所以可以看到重点是StrutsResultSupport中conditionalParse(location, invocation)的location变量。
接下来部分就关注三种result-type的具体实现和具体攻击点。
攻击点一:Redirect action
apps/showcase/src/main/resources/struts-actionchaining.xml 中注意标签中为redirectAction:

register2
redirectAction对应的处理类为org.apache.struts2.result.ServletActionRedirectResult
在 com/opensymphony/xwork2/DefaultActionInvocation.java:368

跟入redirectAction的execute方法即 org/apache/struts2/result/ServletActionRedirectResult.java:160

public void execute(ActionInvocation invocation) throws Exception {
actionName = conditionalParse(actionName, invocation);
if (namespace == null) {
namespace = invocation.getProxy().getNamespace();

}
由于在配置xml时没有指定naPmespace,所以这里的namespace为null,将会执行invocation.getProxy().getNamespace();

所以执行后对于result对象的namespace即为/${(111+111)}。
同一函数中继续执行 172行

public void execute(ActionInvocation invocation) throws Exception {

String tmpLocation = actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null));

setLocation(tmpLocation);

super.execute(invocation);

}
ActionMapping生成如下,this.namespace值赋为/${(111+111)}:

跟入getUriFromActionMapping:

public String getUriFromActionMapping(ActionMapping mapping) {
StringBuilder uri = new StringBuilder();

    handleNamespace(mapping, uri);
    handleName(mapping, uri);
    handleDynamicMethod(mapping, uri);
    handleExtension(mapping, uri);
    handleParams(mapping, uri);

    return uri.toString();
}

handleNamespace处理结果如下:

当函数返回,tmpLocation值为/${(111+111)}/register2.action,然后通过setLocation(tmpLocation)使得location变量值为/${(111+111)}/register2.action,从而最终造成OGNL注入。

攻击点二: Action chaining
apps/showcase/src/main/resources/struts-actionchaining.xml 中注意标签中为chain:
register2

同样会先经过result = createResult(),然后调用result.execute(this);。这会进入到 com/opensymphony/xwork2/ActionChainResult.java:203
public void execute(ActionInvocation invocation) throws Exception {
// if the finalNamespace wasn’t explicitly defined, assume the current one
if (this.namespace == null) {
this.namespace = invocation.getProxy().getNamespace();
}

ValueStack stack = ActionContext.getContext().getValueStack();
String finalNamespace = TextParseUtil.translateVariables(namespace, stack);
String finalActionName = TextParseUtil.translateVariables(actionName, stack);
...

}
由于没有设定namespace,所以通过invocation.getProxy().getNamespace()使得this.namespace值为/${(111+111)}。然后调用了String finalNamespace = TextParseUtil.translateVariables(namespace, stack);对namespace进行OGNL解析。如下

攻击点三:Postback result
apps/showcase/src/main/resources/struts-actionchaining.xml 中注意标签中为postback:

register2

经过result = createResult(),跟入定位到postback这个result对象的处理方法,在 org/apache/struts2/result/PostbackResult.java:113

@Override
public void execute(ActionInvocation invocation) throws Exception {
String postbackUri = makePostbackUri(invocation);
setLocation(postbackUri);
super.execute(invocation);
}
跟入makePostbackUri1,在org/apache/struts2/result/PostbackResult.java:129

protected String makePostbackUri(ActionInvocation invocation) {
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
String postbackUri;

if (actionName != null) {
    actionName = conditionalParse(actionName, invocation);
    if (namespace == null) {
        namespace = invocation.getProxy().getNamespace();
    } else {
        namespace = conditionalParse(namespace, invocation);
    }
    ...
    postbackUri = request.getContextPath() + actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null));
}
...

return postbackUri;

}

获取到namespace值为/${(111+111)}。跟入actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null)),其具体执行过程如攻击点一[Redirect action]提到的那样,设置namespace等参数,然后从getUriFromActionMapping中返回uri。最后组装的postbackUri为/${(111+111)}/register2.action

回到前面的execute中通过setLocation(postbackUri)设置了location变量:

此后location变量传入,造成OGNL表达式注入

参考
https://struts.apache.org/core-developers/namespace-configuration.html

工具下载

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:大老李的Blog,谢谢!^^
大老李的Blog|渗透测试论坛|红蓝对抗|大型Web安全问答社区 » 【Struts2-代码执行漏洞分析系列】S2-057

常见问题FAQ

下载须知:
所有工具以及资源来自网络收集,所有下载,仅供学习交流。禁止利用从本站点下载的工具进行非法活动,从事危害国家网络安全等行为,请遵守相关法律规定,如有违反与本站无关。
会员的作用是什么?
本站会上线exp漏洞库与最新0day供会员下载

发表评论

提供最优质的资源集合

立即查看 了解详情