说说网站安全2

很多网站被挂马大多可能是通过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的防注入攻击

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值