for //CSRF on aspx page:
in ispagepostback
utility obj = new utility();
CSRF_Token.Value = obj.Initialize_CSRF_Token();
out of the ispagepostback
utility obj2 = new utility();
if (!obj2.Validate_CSRF_Token(CSRF_Token.Value))
{
obj2.CSRF_Attack_Found();
}
public string Initialize_CSRF_Token()
{
if (HttpContext.Current.Session["CSRF_Token"] != null)
{
return HttpContext.Current.Session["CSRF_Token"].ToString();
}
else
return "0";
}
public bool Validate_CSRF_Token(string CSRF_Token)
{
if (HttpContext.Current.Session["CSRF_Token"] != null)
{
if (CSRF_Token == HttpContext.Current.Session["CSRF_Token"].ToString())
{
return true;
}
//else
//{
// return false;
//}
}
return false;
}
public void CSRF_Attack_Found()
{
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Redirect("~/ErrorPages/GeneralError.aspx");
}
public static void CheckReffererURL()
{
string SiteUrl = string.Empty;
SiteUrl = ConfigurationManager.AppSettings["SiteUrl"].ToString();
if (HttpContext.Current.Request.UrlReferrer == null)
{
HttpContext.Current.Response.Redirect("~/ErrorPages/PageNotFound.aspx");
}
if (!HttpContext.Current.Request.UrlReferrer.ToString().Contains(SiteUrl))
{
HttpContext.Current.Response.Redirect("~/ErrorPages/PageNotFound.aspx");
}
}
public static bool CheckSqlInjection(string strQuery)
{
//return true;
strQuery = strQuery.ToLower();
if ((strQuery.IndexOf("or 1=1") != -1) || (strQuery.IndexOf("drop") != -1) || (strQuery.IndexOf("truncate") != -1) || (strQuery.IndexOf("xtype") != -1) || (strQuery.IndexOf("Union all") != -1)
|| (strQuery.IndexOf("--") != -1) || (strQuery.IndexOf(";") != -1) || (strQuery.IndexOf(";") != -1) || (strQuery.IndexOf("/*") != -1) || (strQuery.IndexOf("*/") != -1) || (strQuery.IndexOf("@@") != -1) || (strQuery.IndexOf("@") != -1)
|| (strQuery.IndexOf("cast") != -1) || (strQuery.IndexOf("cursor") != -1) || (strQuery.IndexOf("declare") != -1) || (strQuery.IndexOf("exec") != -1) || (strQuery.IndexOf("execute") != -1) || (strQuery.IndexOf("fetch") != -1)
|| (strQuery.IndexOf("alter") != -1))
return false;
else
return true;
}
public bool validateNumeric(string number)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(number, @"^[0-9]{1,10}$")) //0-9, accept max 10 digits
return false;
else
return true;
}
public static void CheckSSL()
{
string LocalURL = "http://localhost";
if (!HttpContext.Current.Request.Url.ToString().Contains(LocalURL))
{
if (!HttpContext.Current.Request.IsSecureConnection)
{
string redirectUrl = HttpContext.Current.Request.Url.ToString().Replace("http:", "https:");
HttpContext.Current.Response.Redirect(redirectUrl);
}
}
}
No comments:
Post a Comment