.net全站过滤url危险参数,防注入
2009-12-07 21:58
281 查看
在global文件中添加如下代码:
void Application_BeginRequest(object sender, EventArgs e)
{
//遍历Post参数,隐藏域除外
if (Regex.IsMatch(Request.RawUrl.ToLower(), @"/manager/")==false)
for (int i=0; i < Request.Form.Count;i++)
{
if (Request.Form[i].ToString() == "__VIEWSTATE") continue;
if (IsDanger(Request.Form[i].ToString()))
{
Response.Write("您提交的内容中含有非法字符,已经被拒绝.");
Response.End();
}
}
//过滤所有Url中的危险字符串
if (Request.QueryString.Count > 0 && Regex.IsMatch(Request.RawUrl.ToLower(), @"/.aspx") == true && Regex.IsMatch(Request.RawUrl.ToLower(), @"fckeditor") == false)//如果防止截获fckeditor正常的Url,必须验证".aspx"
{
string Temp = "";
//string Url = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("?"));
string Url = Request.RawUrl.Substring(0, Request.RawUrl.LastIndexOf("?"));
for (int i = 0; i < this.Request.QueryString.Count; i++)
{
try
{
Temp = HandleRequestParam(this.Request.QueryString[i].ToString());
Url += i == 0 ? "?" : "&";
Url += Request.QueryString.Keys[i].ToString() + "=" + Temp;
}
catch { }
}
//if (Url.Length < Request.Url.AbsoluteUri.Length)
// Response.Redirect(Url);
Context.RewritePath(Url);//可以用Response.Redirect和Context.RewritePath
}
//全站防止页面缓存
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
}
protected string HandleRequestParam(string str)
{
string RetStr = "";
char[] strC = str.ToLower().ToCharArray();
for (int i = 0; i < strC.Length; i++)
{
if (Convert.ToInt32(strC[i]) >= 48 && Convert.ToInt32(strC[i]) <= 57)
RetStr += strC[i].ToString();
else
break;
}
return RetStr;
}
protected bool IsDanger(string InText)
{
string word = @"exec|insert|select|delete|update|master|truncate|char|declare|join|iframe|href|script|<|>|request";
if (InText == null)
return false;
if (Regex.IsMatch(InText,word))
return true;
return false;
}
void Application_BeginRequest(object sender, EventArgs e)
{
//遍历Post参数,隐藏域除外
if (Regex.IsMatch(Request.RawUrl.ToLower(), @"/manager/")==false)
for (int i=0; i < Request.Form.Count;i++)
{
if (Request.Form[i].ToString() == "__VIEWSTATE") continue;
if (IsDanger(Request.Form[i].ToString()))
{
Response.Write("您提交的内容中含有非法字符,已经被拒绝.");
Response.End();
}
}
//过滤所有Url中的危险字符串
if (Request.QueryString.Count > 0 && Regex.IsMatch(Request.RawUrl.ToLower(), @"/.aspx") == true && Regex.IsMatch(Request.RawUrl.ToLower(), @"fckeditor") == false)//如果防止截获fckeditor正常的Url,必须验证".aspx"
{
string Temp = "";
//string Url = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("?"));
string Url = Request.RawUrl.Substring(0, Request.RawUrl.LastIndexOf("?"));
for (int i = 0; i < this.Request.QueryString.Count; i++)
{
try
{
Temp = HandleRequestParam(this.Request.QueryString[i].ToString());
Url += i == 0 ? "?" : "&";
Url += Request.QueryString.Keys[i].ToString() + "=" + Temp;
}
catch { }
}
//if (Url.Length < Request.Url.AbsoluteUri.Length)
// Response.Redirect(Url);
Context.RewritePath(Url);//可以用Response.Redirect和Context.RewritePath
}
//全站防止页面缓存
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
}
protected string HandleRequestParam(string str)
{
string RetStr = "";
char[] strC = str.ToLower().ToCharArray();
for (int i = 0; i < strC.Length; i++)
{
if (Convert.ToInt32(strC[i]) >= 48 && Convert.ToInt32(strC[i]) <= 57)
RetStr += strC[i].ToString();
else
break;
}
return RetStr;
}
protected bool IsDanger(string InText)
{
string word = @"exec|insert|select|delete|update|master|truncate|char|declare|join|iframe|href|script|<|>|request";
if (InText == null)
return false;
if (Regex.IsMatch(InText,word))
return true;
return false;
}
相关文章推荐
- .net全站过滤url危险参数,防注入
- .net 获取url地址及参数等方法
- 利用Page基类过滤URL注入代码-防SQL注入的方法
- .net获取当前url各种属性(文件名、参数、域名 等)的方法
- .net 正则获取url参数
- .net获取当前url各种属性(文件名、参数、域名、端口等)的方法(转)
- url注入过滤
- 过滤 URL 参数 空值,删除 FORM GET URL 空值解决函数方法
- php防止sql注入之过滤分页参数实例
- php防止sql注入之过滤分页参数实例
- ASP.NETURL地址防注入过滤问题
- JS过滤url参数特殊字符的实现方法
- Struts2使用自定义拦截器导致Action注入参数丢失、url参数
- nginx 过滤请求URL参数及重定向
- .NET 获取Get方式URL中的参数键值
- .Net 获取URL及其参数
- .NET获取当前网址url的各种属性(文件名、参数、域名 等)的代码
- .net获取当前url各种属性(文件名、参数、域名 等) & 鼠标手型
- 过滤掉URL里不需要的参数