『壹』 幾種過濾URL和FORM中非法字元的方法
ASP過濾URL和FORM中非法字元第一種:<%'檢查URL輸入限制非法字元url=LCase(request.querystring())ip=request.ServerVariables( REMOTE_ADDR)pos1=instr(url,%)pos2=instr(url,')pos3=instr(url,;)pos4=instr(url,where)pos5=instr(url,select)pos6=instr(url,chr)pos7=instr(url,/)pos8=Instr(url,and)ifpos1<0orpos2<0orpos3<0orpos4<0orpos5<0orpos6<0orpos7<0orpos8<0thenresponse.Write 你嘗試使用危險字元,系統已經對此做了記錄如下您的IP:&ip&操作時間:&date()& response.End()endif'檢查表單輸入,限制非法字元'使用request.QueryString來索引request的所有資料,作為SQL檢查之用'如出現非法字元則自動停止輸出fori_request=1torequest.form.Countifinstr(request.form(i_request),')<0orinstr(request.form(i_request),;)<0thenResponse.Write <scriptlanguage='javascript'history.back();alert('你嘗試使用危險字元,系統已經對此做了記錄如下您的IP:&ip&操作時間:&date()& ');</script response.End()endifnext%第二種:<%OnErrorResumeNextdimsql_injdata,sql_inj,sql_get,sql_data SQL_injdata='|oxSQL_inj=split(SQL_Injdata,|)'定義過濾字元,可以自己添加,以|分隔''|;|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare'對post方式過濾IfRequest.Form<ThenForEachSql_PostInRequest.FormForSQL_Data=0ToUbound(SQL_inj)ifinstr(Request.Form(Sql_Post),Sql_Inj(Sql_DATA))0ThenResponse.redirectss'出錯時轉向頁面 Response.endendifnextnextendif'對GET方式過濾IfRequest.QueryString<ThenForEachSQL_GetInRequest.QueryStringForSQL_Data=0ToUbound(SQL_inj)ifinstr(Request.QueryString(SQL_Get),Sql_Inj(Sql_DATA))0ThenResponse.redirectss'出錯時轉向頁面 Response.endendifnextNextEndIf%第三種:functioncheckstr(str)'過濾非法字元函數dimtempstrifstr=thenexitfunctiontempstr=replace(str, chr(34),)'tempstr=replace(tempstr, chr(39),)''tempstr=replace(tempstr, chr(60),)'<tempstr=replace(tempstr, chr(62),)'tempstr=replace(tempstr, chr(37),)'%tempstr=replace(tempstr, chr(38),)'&tempstr=replace(tempstr, chr(40),)'(tempstr=replace(tempstr, chr(41),)')tempstr=replace(tempstr, chr(59),)';tempstr=replace(tempstr, chr(43),)'+tempstr=replace(tempstr, chr(45),)'-tempstr=replace(tempstr, chr(91),)'[tempstr=replace(tempstr, chr(93),)']tempstr=replace(tempstr, chr(123),)'{tempstr=replace(tempstr, chr(125),)'}checkstr=tempstrendfunction第四種:'================================================'函數名:IsValidStr'作用:判斷字元串中是否含有非法字元'參數:str----原字元串'返回值:False‚True-----布爾值'================================================PublicFunctionIsValidStr(ByValstr)IsValidStr=(str)ThenExitFunctionIfTrim(str)=‚iForbidStr= and|chr|:|=|%|&|$|#|@|+|-|*|/|/|<||;|‚|^|&Chr(32)&|&Chr(34)&|&Chr(39)&|&Chr(9)ForbidStr=Split(ForbidStr‚|)Fori=0ToUBound(ForbidStr)IfInStr(1‚str‚ForbidStr(i)‚1)0ThenIsValidStr==TrueEndFunctionASP.(Stringpara)//過濾非法字元{intflag=0;flag+=para.indexOf(')+1;flag+=para.indexOf(;)+1;flag+=para.indexOf(1=1)+1;flag+=para.indexOf(|)+1;flag+=para.indexOf(<)+1;flag+=para.indexOf()+1;if(flag!=0){System. out 提交了非法字元!!!);returnfalse;}returntrue;}
『貳』 java過濾非法字元的filter
filter代碼在pujia12345提供的代碼上改的;
jsp頁面的編碼你設成你自己的,我用的是-8。
input.jsp輸入後,正常跳轉到handle.jsp,而禁詞已經被過濾。
filter:
package test;
import java.io.*;
import javax.servlet.*;
import java.util.*;
public class MyFilter implements Filter
{
private List<String> unString;
public void init(FilterConfig filterConfig) throws ServletException
{
unString = new ArrayList<String>();
unString.add("日");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
String content = request.getParameter("content");//需要過濾的參數
if(content!=null){
for (int i = 0; i < unString.size(); i++)
{
String strIllegal = unString.get(i);
if (content.indexOf(strIllegal) >= 0)
{
content = content.replaceAll(strIllegal, "");//非法字元替換成空
}
request.setAttribute("content", content);//為request設置屬性保存修改後的值
}
}
chain.doFilter(request, response);
}
public void destroy()
{
//System.out.println("過濾器銷毀");
}
}
//---------------------------//
web.xml:
<filter>
<filter-name>myfilter</filter-name>
<filter-class>test.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
//---------------------------//
輸入頁面input.jsp:
<%@page contentType="text/html;charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>input.jsp</title>
</head>
<body>
<form action="handle.jsp" method="post">
<input type="text" name="content" />
<input type="submit" value=" 提交 " />
</form>
</body>
</html>
//---------------------------//
input提交的頁面handle.jsp:
<%@page contentType="text/html;charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> handle.jsp </title>
</head>
<body>
<%
String content = (String)request.getAttribute("content");
out.println(content);
%>
</body>
</html>