[原创]java csrf攻击修复,appscan csrf漏洞修复
java csrf攻击修复,appscan csrf漏洞修复
其实就是判断referer看是不是同一个站点,如果不是就不执行任何
操作,直接跳转到一个页面就行,如果一样才可以继续执行操作。
// 出版物列表页
public String productList() {
try {
HttpServletRequest req = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
// System.out.println(req.getHeader("referer"));
HttpServletResponse res = (HttpServletResponse) ActionContext
.getContext().get(ServletActionContext.HTTP_RESPONSE);
ActionContext context = ActionContext.getContext();
String refer=req.getHeader("Referer");
int relength=refer.length();
if(refer.subSequence(relength-14,
relength).equals("productList.do") ||
refer.subSequence(relength-14,
relength).equals("productMain.do")){//就//是这儿判断是不是
本站过来的连接,如果是就继续执行,如果不是就跳转到指定页面
HttpSession session = req.getSession();
String search=req.getParameter("search");
String
productName=req.getParameter("productName");
String
productType=req.getParameter("productType");
if(search!="" && search !=null){
search=xxsfilter(search);
req.setAttribute("search", search);
}
if(productName!="" && productName !=null){
productName=xxsfilter(productName);
req.setAttribute("productName", productName);
}
if(productType!="" && productType !=null){
productType=xxsfilter(productType);
req.setAttribute("productType", productType);
}
// 设置查询选项
List typeList = republicationService
.getProductDicList("productType");
List addrList = republicationService
.getProductDicList("publishAddr");
this.getRequest().put("addrList", addrList);
this.getRequest().put("typeList", typeList);
// 设置查询条件
HashMap map = new HashMap();
map.put("productType", this.productType);
map.put("publishAddr", this.publishAddr);
map.put("productName", this.productName);
// 分页
if (this.getP() == null) {
this.setP(new Page());
}
if (this.searchFlag != null &&
this.searchFlag.equals("1")) {
this.setP(new Page());
}
List list =
republicationService.getPagesList(map, this.getP());
if (this.getP().getCurrentPage() > this.getP().getMaxPage()) {
this.getP().setCurrentPage(1);
list = republicationService.getPagesList(map, this.getP());
} else {
req.setAttribute("currentpage", this.getP().getCurrentPage());
}
for (int i = 0; i < list.size(); i++) {
Product product = (Product) list.get(i);
String addr = product.getPublishAddr();
String type = product.getProductType();
if (product.getPublishDate() != null) {
product.setPubDate(DateFormat.dateTostring(product
.getPublishDate()));
}
if (addr != null && !addr.equals("")) {
Dictionary dic1 =
dictionaryService.get(Long
.parseLong(addr));
product.setPublishAddr_dic(dic1.getDicName());
}
if (type != null && !type.equals("")) {
Dictionary dic1 =
dictionaryService.get(Long
.parseLong(type));
product.setProductType_dic(dic1.getDicName());
}
//
product.setPublishAddr_dic(publishAddr_dic);
if (product.getImportFlag() == 1) {
product.setPublishAddr_dic("无库存");
product.setShowEbook("1");
product.setFilePath("eid");
}
// 处理文件大小
if (product.getEbookSize() != null
&& product.getEbookSize() != 0) {
Double filesize = null;
if (product.getEbookSize() > 1000000) {
filesize =
Double.valueOf(product.getEbookSize())
/ (1024 * 1024);
if (filesize.toString().length() > 5)
product.setFileSize(filesize.toString().substring(
0, 4)
+ "M");
else
product.setFileSize(filesize.toString() + "M");
} else {
filesize =
Double.valueOf(product.getEbookSize()) / 1024;
product.setFileSize(filesize.intValue() + "KB");
}
}
}
this.getRequest().put("list", list);
// 如果是CCS年报 CCS宣介 PSC年报 页面列表显示不一样
if (productType != null && !"".equals(productType)) {
if (productType.equals("421")
|| productType.equals("422")
|| productType.equals("425")
|| (productName != null && !"".equals(productName) && productName
.indexOf("CCS年报 CCS宣介 PSC年报") != -1)) {
request.put("forFree", "forFree");
}
}
return "productList";
}else{
res.sendRedirect("/ccswzen/");
return null;
}
} catch (Exception e) {
this.getLog().error("严重错误:" + e.getMessage());
e.printStackTrace();
return this.err(e);
}
}
//XSS参数过滤
public String xxsfilter(String canshu){
canshu=canshu.replaceAll("&","&");
canshu=canshu.replaceAll(":",":");
canshu=canshu.replaceAll("=","=");
canshu=canshu.replaceAll("<","<");
canshu=canshu.replaceAll(">",">");
canshu=canshu.replaceAll(" "," ");
canshu=canshu.replaceAll("''",""");
return canshu;
}
1、限制验证cookie的到期时间。这些cookie的合法时间越短,黑客利用你的Web应用程序的机会就越小。不过,这个时间越短,用户就越不方便。因此,你需要在安全性和方便性之间进行平衡。
2、执行重要业务之前,要求用户提交额外的信息。要求用户在进行重要业务前输入口令,这可以防止黑客发动CSRF攻击(只要浏览器中没有包含口令),因为这种重要信息无法预测或轻易获得。
3、使用秘密的无法预测的验证符号。当保存在用户浏览器中的cookie仅由一次会话确认时,CSRF攻击才会有效。所以在每次HTTP请求(当然攻击者无法提前知道)中都有附加的特定会话的信息,这样就可以挫败CSRF攻击。不过,如果这种应用程序存在跨站脚本漏洞,黑客
就有可能访问这种验证符号。
4、使用定制的HTTP报头。如果执行交易的所有请求都使用XMLHttpRequest并附加一个定制的HTTP报头,同时拒绝缺少定制报头的任何请求,就可以用XMLHttpRequest API来防御CSRF攻击。由于浏览器通常仅准许站点将定制的HTTP报头发送给相同站点,从而了防止由CSRF攻击的源站点所发起的交易。
5、检查访问源的报头。在浏览者发送HTTP请求时,它通常会包含源自访问源报头的URL。理论上讲,你可以使用这些信息来阻止源自其它任何站点(而不是来自Web应用程序自身)的请求。不过,访问源报头并不总是可用的,(例如,有些单位由于私密性的缘故而将它剥离了),或者这个报头容易被欺骗,所以说,这条措施并不真正有效。
csrf跨站伪造请求攻击不单单是jsp网站存在的问题。主要是利用浏览器的安全缺陷:
建议程序员不要滥用$_REQUEST类变量,在必要的情况下给某些敏感的操作加上水印,考虑使用类似DISCUZ论坛的formhash技术提高黑客预测请求参数的难度,注意JSON数据接口的安全问题等。最后希望大家全面的考虑客户端和服务端整体的安全,注意Internet Explorer等客户端浏览器一些安全缺陷和安全特性,防止客户端程序的安全问题影响整个Web应用程序.