用mod_security保障Web Services的安全二
将web服务与mod_security结合起来
blue银行的web服务使用www.bluebank.example.com/axis/getbalance.jws的url。为这个资源创建一套规则通常一个不错的主意。为达此目的,blue银行将此资源通过以下方式加入到httpd.conf:
<ifmodule mod_security.c>
secfilterengine on
secfilterdefaultaction "deny,log,status:500"
# other rules
# ------- rules for web services --------------------------
<location /axis/getbalance.jws>
secfilterinheritance off
secfilterdefaultaction "deny,log,status:500"
secfilterscanpost on
secfiltercheckurlencoding on
secfiltercheckunicodeencoding on
</location>(www.iocblog.net 文章来源)
#---------------------------------------------------------------</ifmodule>
下面的指令块为/axis/getbalance.jws应用了过滤标准。为了保护web服务它添加了必要的规则。这些规则在<location>块中,如下:
# ------- rules for web services --------------------------
<location /axis/getbalance.jws>
secfilterinheritance off
secfilterdefaultaction "deny,log,status:500"
secfilterscanpost on
secfiltercheckurlencoding on
secfiltercheckunicodeencoding on</location>
#---------------------------------------------------------------
在这里有两个重要的指令:
secfilterinheritance off
这个指令关闭其他所有规则,为新的location建立一套规则创建一个干净的空间。(译者注:初始化,建立新空间)
secfilterscanpost on
由于web服务的方法调用是通过post,所以这个指令是打开post过滤器。
有了上面的配置,blue bank已经在mod_security中部署了一个“护盾”(译者注:防火墙)。该“护盾”也知道它的防护目标――客户端通过soap封套发送的id的内容。
防范攻击
作为防护所有恶意请求的第一步,blue银行需要限制从客户端发送的id的值,防止传来无效值。soap请求用xml标签将id信息发送到web服务的代码中,像如下这样:
<q1:getinput xmlns:q1="http://defaultnamespace">
<id xsi:type="xsd:string">12123</id></q1:getinput>
为了过滤该请求,mod_security必须有一些途径去读取与标签相关的值(在这里标签是id);这个例子中的值是12123,mod_security提供一些途径限制通过post请求发送的值。其中的一个方法就是使用自定义过滤器:
<location /axis/getbalance.jws>
secfilterinheritance off
secfilterdefaultaction "deny,log,status:500"
secfilterscanpost on
secfiltercheckurlencoding on
secfiltercheckunicodeencoding on
secfilterselective post_payload "<s*id[^>]*>" chain</location>
上面标示出来的行对请求中的id进行限制。post_payload截取post数据块并与正则表达式(<s*id[^>]*>)进行匹配。该正则表达式确保id标签存在,当存在的情况下才进行其余的检查。换句话说,如果id标签存在,mod_security继续下一个检查。
如果发送的post请求中存在一个id,服务器能够执行信息。然而,一个恶意的客户端能够修改这个值加入恶意内容。有四种最流行的攻击方式。
攻击方式1:变量长度缓冲区注入(译者注:缓冲区溢出)
当把一个大的缓冲数据传给一个变量时可能会引起应用程序运行不正常或者在执行的时候“宕”掉的安全隐患。下面的规则将保护id变量免受此类攻击:
<location /axis/getbalance.jws>
secfilterinheritance off
secfilterdefaultaction "deny,log,status:500"
secfilterscanpost on
secfiltercheckurlencoding on
secfiltercheckunicodeencoding on
secfilterselective post_payload "<s*id[^>]*>" chain
secfilterselective post_payload "<s*id[^>]*>.{6,}</s*ids*>""deny,status:500"</location>
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。