很多网站被挂马大多可能是通过URL上找的入口,
下面介绍一下URL的严格控制
比如需要传参数,一般需要参与数据库的参考为int型
这样可以将参数Convert.Toint32进行强制转换,只接收int型
给大写分享一个类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
public class RegExp
{
public static bool IsNumeric(string s)
{
if (s == null || s == string.Empty)
{
return false;
}
string pattern = @"^/-?[0-9]+$";
return Regex.IsMatch(s, pattern);
}
public static bool IsUnicode(string s)
{
string pattern = @"^[/u4E00-/u9FA5/uE815-/uFA29]+$";
return Regex.IsMatch(s, pattern);
}
public static bool IsEmail(string s)
{
string pattern = @"^[/w-]+(/.[/w-]+)*@[/w-]+(/.[/w-]+)+$";
return Regex.IsMatch(s, pattern);
}
public static bool IsRelativePath(string s)
{
if (s == null || s == string.Empty)
{
return false;
}
if (s.StartsWith("/") || s.StartsWith("?"))
{
return false;
}
if (Regex.IsMatch(s, @"^/s*[a-zA-Z]{1,10}:.*$"))
{
return false;
}
return true;
}
public static bool IsPhysicalPath(string s)
{
string pattern = @"^/s*[a-zA-Z]:.*$";
return Regex.IsMatch(s, pattern);
}
}
在使用的时候
int id=0;
if (RegExp.IsNumeric(Request.QueryString["id"].ToString()))
{
id = Convert.ToInt32(Request.QueryString["id"].ToString());
}
这样就大大提高了URL的防注入攻击