web服务正在变为下一代web应用的一个完整部分。但是它也较容易被攻击。这些攻击的种类与传统的web应用所受到的攻击是相同的,但是形式上发生了变化。这些攻击会导致信息泄漏;另外,这些攻击有助于执行远程命令。通过使用wsdl,一个攻击者能够确定web服务的入口和可用的接口。这些接口或方法使用soap协议通过http/https方式得到输入。如果在源代码层没有好的保护,您的应用就会处于危险。mod_security作为一个apache web服务器模块,是一种保护web服务免受恶意攻击(包括封装在soap封套内的恶意数据)的理想解决方案。
问题域
web服务所遭到的四种主要攻击:
变量长度缓冲区(variable-length buffer)注入
元字符注入
sql注入
soap错误代码信息暴露(译者注:是指返回的exception信息的暴露)
通常防火墙配置会允许http/https传输无碍的进入。通常对web服务的攻击会伪装成合法的http/https传输,从而蒙骗了防火墙。这篇文章将告诉您如何区分合法的http/https传输和恶意的http/https传输,并截获恶意传输。这样将能够在很大程度上减轻对80/443端口的攻击。
解决方案是什么?
有很多解决方案,从安全代码到输入验证。一个途径是通过对每一个请求的内容用事先定义好的规则进行验证。这种途径在源代码层上阻止了恶意soap请求渗透入web服务。如您为web服务正确部署并配置了mod_security,它将能够阻止所有上述的攻击。这篇文章将详细的讨论mod_security如何能成为web 服务有效的防范工具。
当部署完web服务后,提供一个防御手段来防范不同类型的攻击是很重要的。每一种攻击都需要不同的防范策略。假设有这么一个银行―这个银行仅仅是一个作为例子的假想银行。
blue 银行(www.bluebank.example.com)最近刚刚部署了web服务,并使用mod_security对web服务进行保护。该银行通过internet向它的金融合作伙伴和客户提供银行服务。它的web服务提供在线的客户服务,比如帐目结算,资金周转和更改用户信息。图1解释了在blue bank部署的web服务的架构。

有许多部署web服务的方法。在这个例子中,web服务运行在tomcat/axis上。banking web服务应用以java编码(以.jws作为扩展名)。
相关的apache和axis配置文件片断包含用来加载tomcat的apache httpd.conf:
loadmodule jk2_module modules/mod_jk2.sojkset config.file /usr/local/apache2/conf/workers2.properties
让web服务支持axisservlet 的axis web.xml文件:
<servlet-mapping>
<servlet-name>axisservlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
像如上配置之后,您将可以在该服务器上部署任何web服务。如果您注意到该服务器发回的如下的头信息,您能够识别如下这一行:
server: apache/2.0.50 (unix) mod_ssl/2.0.50 openssl/0.9.7d mod_jk2/2.0.4
以上的消息头说明服务器是运行于tomcat模块mod_jk2/2.0.4之上的apache/2.0.50 (unix)。axis作为tomcat web应用的一部分并且已经可以部署web服务。为了给web服务层提供保护,blue银行已经加载了有application级过滤能力的mod_security模块。下面一行在httpd.conf中的信息加载安全模块:
loadmodule security_module modules/mod_security.so
当部署好mod_security后,blue银行可以在httpd.conf里添加过滤规则:
<ifmodule mod_security.c>
# turn the filtering engine on or off
secfilterengine on
secfilterdefaultaction "deny,log,status:500"
secfilterscanpost on
. . .
# other rules
. . .
</ifmodule>
通过上面的配置,blue bank已经为管理员和开发人员准备好创建web服务,并利用mod_security的有效的内容过滤能力来防范恶意http/https请求。
考虑一个web服务例子
如:www.bluebank.example.com/axis/getbalance.jws.
这个web服务的wsdl请求来自:www.bluebank.example.com/axis/getbalance.jws?wsdl.
说明:www.bluebank.example.com是一个虚拟的域名,仅仅作为例子。
从wsdl得到 方法/接口 数据来显示帐目结算信息
仔细观察http请求获得的wsdl应答结果。这里重点观察一个调用方法,该方法输入bank id 然后返回相关账目结算信息并通过http传递给客户端。operation标签指定web服务能够调用的方法。相关的wsdl代码片断如下:
<wsdl:operation name="getinput">
<wsdlsoap:operation soapaction=""/>
<wsdl:input name="getinputrequest">
<wsdlsoap:body encodingstyle=http://schemas.xmlsoap.org/soap/encoding/namespace="http://defaultnamespace"use="encoded"/>
</wsdl:input>
<wsdl:output name="getinputresponse">
<wsdlsoap:body encodingstyle=http://schemas.xmlsoap.org/soap/encoding/namespace="http://www.bluebank.example.com/axis/getbalance.jws"use="encoded"/>
</wsdl:output>
<wsdl:message name="getinputresponse">
<wsdl:part name="getinputreturn" type="xsd:string"/>
</wsdl:message><wsdl:message name="getinputrequest">
<wsdl:part name="id" type="xsd:string"/></wsdl:message>
如上面代码所示,当向该方法传入一个id将得到一个字符串类型的输出。当你向该方法传入一个account id,你将回得到该account id的账目结算。
通过http方式调用web服务
blue银行的客户或者合作伙伴可以通过向banking web服务发送正确格式的soap封套来得到要求的信息。图2展示了一个通过向服务发送account id 12123 来获取相应账目结算的http请求。

有很多种方法来产生有正确格式的soap请求的soap客户。这里有一个用perl soap::lite实现的soap客户。下面的简单代码将生成一个soap请求。
#!perl -wuse soap::lite;print soap::lite -> service('http://www.bluebank.example.com/axis/getbalance.jws?wsdl') -> getinput('12123');
在网络上探测到http请求和应答是可能的,比如用ethereal。一个发送到www.bluebank.example.com 的包含id 信息为12123的http/soap请求将产生如下信息:
post /axis/getbalance.jws http/1.0content-type: text/xml; charset=utf-8soapaction: ""content-length: 576expect: 100-continuehost: www.bluebank.example.com<?xml version="1.0" encoding="utf-8"?>
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"xmlns:tns="http://www.bluebank.example.com/axis/getbalance.jws" xmlns:types="http://www.bluebank.example.com/axis/getbalance.jws/encodedtypes"xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"xmlns:xsd="http://www.w3.org/2001/xmlschema">
<soap:bodysoap:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<q1:getinput xmlns:q1="http://defaultnamespace">
<id xsi:type="xsd:string">12123</id>
</q1:getinput>
</soap:body>
</soap:envelope>
...http/1.1 200 okdate: mon, 03 jan 2005 19:24:10 gmtserver: apache/2.0.50 (unix) mod_ssl/2.0.50 openssl/0.9.7d mod_jk2/2.0.4set-cookie: jsessionid=69c6540cc427a8b064c0795addfc20ea; path=/axiscontent-type: text/xml;charset=utf-8connection: close<?xml version="1.0" encoding="utf-8"?>
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/xmlschema"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
<soapenv:body>
<ns1:getinputresponsesoapenv:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="http://defaultnamespace">
<ns1:getinputreturnxsi:type="xsd:string">$2500</ns1:getinputreturn>
</ns1:getinputresponse>
</soapenv:body></soapenv:envelope>
闽公网安备 35060202000074号