lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2010年12月7日

使用java.net.URL解析校验检查url非法字符时撞上了bug

Filed under: Java — 标签:, , , — lizongbo @ 00:04

java.net.URL对url格式的检查不严格,如果使用java.net.URL来进行url解析并判断url是否为指定域名时将产生漏洞。

目前一共发现两种情况会解析错误:
1.java.net.URL对url里存在回车符和换行符被认为是合法的:
<%
String goUrl=”http://618119.com/\r\nX-Location: http://www.lizongbo.com/”;
//goUrl=java.net.URLEncoder.encode(goUrl, “UTF-8″);
response.sendRedirect(goUrl);
%>
例如上面的代码即使使用java.net.URL进行解析,也能正常解析,而被认为是个合法的url。
加上reponse.setheader的时候没做参数检查,导致写入了非法的head,这样会导致XSS注入攻击。

2.”http://618119.com#www.lizongbo.com/”
这样的url被java.net.URL解析得到的host是618119.com#www.lizongbo.com,因此按域名后缀判断的话会被误放过,
在浏览器地址栏里实际请求会变成:http://618119.com/#www.lizongbo.com/

这样也会产生非法跳转漏洞。

使用java.net.URI进行解析则不会出现这样的问题。

JDK里的关于java.net.URL里引用的文章连接 为: http://www.socs.uts.edu.au/MosaicDocs-old/url-primer.html,但是这个链接已经失效了。

因此封装下面这个工具类来对url进行检查,避免URL 参数里出现非法字符导致的非法跳转的漏洞:

[code]

package com.lizongbo.net;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
/**
* 测试url检查是否完善的类
* @author lizongbo
*
*/
public class UrlTest {

/**
* @param args
* @throws MalformedURLException
* @throws UnsupportedEncodingException
* @throws URISyntaxException
*/
public static void main(String[] args) throws MalformedURLException,
UnsupportedEncodingException, URISyntaxException {
String urlStr = “http://618119.com/\r\nX-Location: http://www.lizongbo.com/”;
checkUrl(urlStr);
urlStr = “http://618119.com#www.lizongbo.com/”;
checkUrl(urlStr);
urlStr = “https://www.google.com/reader/view/#stream/feed%2Fhttp%3A%2F%2Fwww.lizongbo.com%2Ffeeds%2Fposts%2Fdefault”;
checkUrl(urlStr);
checkUrl(“file:C:/autoexec.bat”);
checkUrl(“file:/C:/autoexec.bat”);
checkUrl(“file://C:/autoexec.bat”);
checkUrl(“file:///C:/autoexec.bat”);
checkUrl(“/aa.jsp”);
}

private static void checkUrl(String urlStr) {
try {
java.net.URI uri = new URI(urlStr);
dump(uri);
} catch (Exception e) {
e.printStackTrace();
}
try {
java.net.URL url = new URL(urlStr);
dump(url);
} catch (Exception e) {
e.printStackTrace();
}
}

private static void dump(java.net.URL url) {
try {
System.out.println(“url=” + url + “,protocol=” + url.getProtocol()
+ “,host=” + url.getHost() + “,path=” + url.getPath()
+ “,query=” + url.getQuery() + “,ref=” + url.getRef()
+ “,url.toURI=” + url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}

private static void dump(java.net.URI uri) {
try {
System.out.println(“uri=” + uri + “,scheme=” + uri.getScheme()
+ “,host=” + uri.getHost() + “,path=” + uri.getPath()
+ “,query=” + uri.getQuery() + “,fragment=”
+ uri.getFragment() + “,uri.toURL=” + uri.toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

/**
* 使用java.net.URI判断指定的url是否为站内合法的目标地址,对url内容进行严格检查
*
* @param goUrl
* @return
*/
public static boolean verifyURL(String goUrl) {
if (goUrl == null) {
return false;
}
java.net.URI cgoUrl = null;
try {
cgoUrl = new java.net.URI(goUrl);// 不使用java.net.URL,而是改用URI进行校验
} catch (Exception e) {
return false;
}
if (!”http”.equalsIgnoreCase(cgoUrl.getScheme())
&& !”https”.equalsIgnoreCase(cgoUrl.getScheme())) {
return false;
}
String hostString = cgoUrl.getHost();
if (hostString == null) {
return false;
}
hostString = hostString.toLowerCase();
String allowDomains = “.618119.com;.lizongbo.com;.mqq.im;.seotijian.com”;
if (allowDomains.length() > 0) {
String[] domains = allowDomains.split(“;”);
for (int i = 0; i < domains.length; i++) {
String dmTmp = domains[i];
if (dmTmp != null && dmTmp.length() > 0
&& hostString.endsWith(dmTmp)) {
return true;
}
}
}
return false;
}

/**
* 使用判断java.net.URL指定的url是否为站内合法的目标地址,针对特殊url的判断存在bug<br/>
* “http://618119.com/\r\nX-Location: http://www.lizongbo.com/”<br/>
* “http://www.lizongbo.com#618119.com/” 这两种会绕过检查,导致安全漏洞<br/>
*
* @param goUrl
* @return
*/
public static boolean verifyURLOld(String goUrl) {
if (goUrl == null) {
return false;
}
java.net.URL cgoUrl = null;
try {
cgoUrl = new java.net.URL(goUrl);// 改用URI进行校验
} catch (Exception e) {
return false;
}
if (!”http”.equalsIgnoreCase(cgoUrl.getProtocol())
&& !”https”.equalsIgnoreCase(cgoUrl.getProtocol())) {
return false;
}
String hostString = cgoUrl.getHost().toLowerCase();
String allowDomains = “.618119.com;.lizongbo.com;.mqq.im”;
if (allowDomains.length() > 0) {
String[] domains = allowDomains.split(“;”);
for (int i = 0; i < domains.length; i++) {
String dmTmp = domains[i];
if (dmTmp != null && dmTmp.length() > 0
&& hostString.endsWith(dmTmp)) {
return true;
}
}
}
return false;
}
}

[code]

2010年11月29日

使用CRLFFilter过滤HTTP应答头信息名称和值的非法字符防止CRLF注入攻击

Filed under: Java,Web — 标签:, , , , — lizongbo @ 10:02

使用CRLFFilter过滤http应答中头信息名称和值的非法字符,防止CRLF注入攻击
经过测试Resin的response.addHeader方法也没做header名字和值的检查,因此如果webapp代码写法不当的话,将导致CRLF注入攻击,

例如一个页面从url参数中获取地址燃尽进行跳转,如果url地址存在“%0d%0a”编码表示的CRLF而未被检测过滤(java.net.URL解析不会出错,必须用java.net.URI才行),将产生安漏洞。

通过下面的代码可以重现这个Xss漏洞攻击。
jsp代码:
<%
response.addHeader(“X-Locationaaa: http://mqq.im/\r\nX-tesh”,”aaa”);
response.addHeader(“X-Locationbbb: 汉字/\r\nX-teshbbb”,”aaa”);

//下面的goUrl可以从URL的参数中获取,如果url地址存在“%0d%0a”编码表示的CRLF而未被检测过滤(java.net.URL解析不会出错,必须用java.net.URI才行),将产生漏洞。

String goUrl=”http://lizongbo.com/\r\nX-Location: http://618119.com/”;
//goUrl=java.net.URLEncoder.encode(goUrl, “UTF-8”);
response.sendError(403,goUrl);
%>

在Firefox中访问jsp,使用Live HTTP headers 可以看到生成的实际head如下:

HTTP/1.1 403 http://lizongbo.com/
X-Location: http://618119.com/
Server: Resin/4.0.10
X-Locationaaa: http://mqq.im/
X-tesh: aaa
X-Locationbbb: 汉字
X-teshbbb: aaa
Content-Type: text/html; charset=utf-8
Content-Length: 216
Date: Tue, 09 Nov 2010 02:37:48 GMT

因此封装过滤器代码如下:
[code]
package com.lizongbo.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class CRLFFilter implements Filter {

@Override
public void destroy() {

}

@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = new CRLFFilterResponseWrapper(
(HttpServletResponse) res);
chain.doFilter(req, response);
}

@Override
public void init(FilterConfig config) throws ServletException {

}

}

[/code]

[code]
package com.lizongbo.web.filter;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class CRLFFilterResponseWrapper extends HttpServletResponseWrapper {
/**
* http header name 不允许出现的字符
*/
private static final char[] headerName_tspecials = new char[] { ‘(‘, ‘)’,
‘<‘, ‘>’, ‘@’, ‘,’, ‘;’, ‘:’, ‘\\’, ‘\”‘, ‘/’, ‘[‘, ‘]’, ‘?’, ‘=’,
‘{‘, ‘}’ };
static {
Arrays.sort(headerName_tspecials);
}

HttpServletResponse response = null;

public CRLFFilterResponseWrapper(HttpServletResponse response)
throws IOException {
super(response);
this.response = response;
}

@Override
public void addHeader(String name, String value) {
super.addHeader(filterHeaderName(name), filterHeaderValue(value));
}

@Override
public void sendError(int sc, String msg) throws IOException {
super.sendError(sc, filterHeaderValue(msg));
}

@Override
public void sendRedirect(String location) throws IOException {
super.sendRedirect(filterHeaderValue(location));
}

@Override
public void setHeader(String name, String value) {
super.setHeader(filterHeaderName(name), filterHeaderValue(value));
}

@Override
public void setStatus(int sc, String sm) {
super.setStatus(sc, filterHeaderValue(sm));
}

@Override
public void addDateHeader(String name, long date) {
super.addDateHeader(filterHeaderName(name), date);
}

@Override
public void addIntHeader(String name, int value) {
super.addIntHeader(filterHeaderName(name), value);
}

@Override
public void setDateHeader(String name, long date) {
super.setDateHeader(filterHeaderName(name), date);
}

@Override
public void setIntHeader(String name, int value) {
super.setIntHeader(filterHeaderName(name), value);
}
@Override
public void setContentType(String contentType) {
super.setContentType(filterHeaderValue(contentType));
}

/**
*过滤头信息名字中的非法字符,避免CRLF注入攻击

Many HTTP/1.1 header field values consist of words separated by LWS<br/>
or special characters. These special characters MUST be in a quoted<br/>
string to be used within a parameter value.<br/>

token          = 1*<any CHAR except CTLs or tspecials><br/>

tspecials      = “(” | “)” | “<” | “>” | “@”<br/>
| “,” | “;” | “:” | “\” | <“><br/>
| “/” | “[” | “]” | “?” | “=”<br/>
| “{” | “}” | SP | HT <br/>
CTL            = <any US-ASCII control character<br/>
(octets 0 – 31) and DEL (127)><br/>
SP             = <US-ASCII SP, space (32)><br/>
HT             = <US-ASCII HT, horizontal-tab (9)><br/>

* @param name
* @return
*/
private static String filterHeaderName(String name) {
if (name == null || name.length() < 1) {
return “null”;
}
StringBuilder sb = new StringBuilder(name.length());
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (c > 32 && c < 127
&& Arrays.binarySearch(headerName_tspecials, c) < 0) {
sb.append(c);
}
}
return sb.toString();
}

/**
*过滤头信息值中的非法字符,避免CRLF注入攻击

* field-value = *( field-content | LWS )<br/>
*
* field-content = <the OCTETs making up the field-value<br/>
* and consisting of either *TEXT or combinations<br/>
* of token, tspecials, and quoted-string><br/>
*
* @param value
* @return
*/
private static String filterHeaderValue(String value) {
if (value == null || value.length() < 1) {
return “null”;
}
StringBuilder sb = new StringBuilder(value.length());
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (c >= 32 && c < 127) {
sb.append(c);
}
}
return sb.toString();
}

public static void main(String[] args) {
String headName = “aaaa aaa\r\n bbb “;
String headvalue = “cccccccccc\r\n ddd”;
System.out.println(headName + “==” + filterHeaderName(headName));
System.out.println(headvalue + “==” + filterHeaderValue(headvalue));
}
}
[/code]

参考链接:

http://www.ietf.org/rfc/rfc2068.txt
http://www.acunetix.com/websitesecurity/crlf-injection.htm
http://comic.sjtu.edu.cn/bbs/view.asp?TID=4118

2010年02月23日

使用Servlet Filter来防止Xss漏洞和SQL注入的方法

Filed under: Java — 标签:, , , , — lizongbo @ 22:32

使用Servlet Filter来防止Xss漏洞和SQL注入的方法

在用java进行web业务开发的时候,对于页面上接收到的参数,除了极少数是步可预知的内容外,大量的参数名和参数值都是不会出现触发Xss漏洞的字符。而通常为了避免Xss漏洞,都是开发人员各自在页面输出和数据入库等地方加上各种各样的encode方法来避免Xss问题。而由于开发人员的水平不一,加上在编写代码的过程中安全意识的差异,可能会粗心漏掉对用户输入内容进行encode处理。针对这种大量参数是不可能出现引起Xss和SQL注入漏洞的业务场景下,因此可以使用一个适用大多数业务场景的通用处理方法,牺牲少量用户体验,来避免Xss漏洞和SQL注入。
那就是利用Servlet的过滤器机制,编写定制的XssFilter,将request请求代理,覆盖getParameter和getHeader方法将参数名和参数值里的指定半角字符,强制替换成全角字符。
使得在业务层的处理时不用担心会有异常输入内容。

相关的代码如下:
XssFilter.java
[code]
package com.lizongbo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class XssFilter implements Filter {

@Override
public void init(FilterConfig config) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper(
(HttpServletRequest) request);
chain.doFilter(xssRequest, response);
}

@Override
public void destroy() {
}
}

[/code]

XssHttpServletRequestWrapper.java
[code]
package com.lizongbo.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
*
* @author lizongbo
*
*/
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
HttpServletRequest orgRequest = null;

public XssHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
orgRequest = request;
}

/**
* 覆盖getParameter方法,将参数名和参数值都做xss过滤。<br/>
* 如果需要获得原始的值,则通过super.getParameterValues(name)来获取<br/>
* getParameterNames,getParameterValues和getParameterMap也可能需要覆盖
*/
@Override
public String getParameter(String name) {
String value = super.getParameter(xssEncode(name));
if (value != null) {
value = xssEncode(value);
}
return value;
}

/**
* 覆盖getHeader方法,将参数名和参数值都做xss过滤。<br/>
* 如果需要获得原始的值,则通过super.getHeaders(name)来获取<br/>
* getHeaderNames 也可能需要覆盖
*/
@Override
public String getHeader(String name) {

String value = super.getHeader(xssEncode(name));
if (value != null) {
value = xssEncode(value);
}
return value;
}

/**
* 将容易引起xss漏洞的半角字符直接替换成全角字符
*
* @param s
* @return
*/
private static String xssEncode(String s) {
if (s == null || s.isEmpty()) {
return s;
}
StringBuilder sb = new StringBuilder(s.length() + 16);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case ‘>’:
sb.append(‘>’);//全角大于号
break;
case ‘<‘:
sb.append(‘<’);//全角小于号
break;
case ‘\”:
sb.append(‘‘’);//全角单引号
break;
case ‘\”‘:
sb.append(‘“’);//全角双引号
break;
case ‘&’:
sb.append(‘&’);//全角
break;
case ‘\\’:
sb.append(‘\’);//全角斜线
break;
case ‘#’:
sb.append(‘#’);//全角井号
break;
default:
sb.append(c);
break;
}
}
return sb.toString();
}

/**
* 获取最原始的request
*
* @return
*/
public HttpServletRequest getOrgRequest() {
return orgRequest;
}
/**
* 获取最原始的request的静态方法
*
* @return
*/
public static HttpServletRequest getOrgRequest(HttpServletRequest req) {
if(req instanceof XssHttpServletRequestWrapper){
return ((XssHttpServletRequestWrapper)req).getOrgRequest();
}

return req;
}
}

}
[/code]

Powered by WordPress