• 系统安全问题


    最近在处理项目中的安全问题,特别是Safe_SQL Injection的问题,所有在网上找了一下,记录一下。

    /// <summary>
    /// 创建SQL注入的类
    /// </summary>
    public class Safe_SQL Injection
    {

      private const string StrRegex = @"(alert|confirm|prompt)|^+/v(8|9)|(and|or).{1,6}?(=|>|<|in|like)|/*.+?*/|<s*script|<s*img|EXEC|UNION.+?SELECT|UPDATE.+?SET|INSERTs+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)s+(TABLE|DATABASE)";

      public static bool PostData()
      {
        bool result = false;

        for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
        {
          result = CheckData(HttpContext.Current.Request.Form[i].ToString());
          if (result)
          {
            break;
          }
        }
        return result;
      }

      public static bool GetData()
      {
        bool result = false;

        for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
        {
          result = CheckData(HttpContext.Current.Request.QueryString[i].ToString());
          if (result)
          {
            break;
          }
        }
        return result;
      }

      public static bool CookieData()
      {
        bool result = false;
        for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
        {
          result = CheckData(HttpContext.Current.Request.Cookies[i].Value.ToLower());
          if (result)
          {
            break;
          }
        }
        return result;
      }

      

      public static bool referer()
      {
        bool result = false;
        return result = CheckData(HttpContext.Current.Request.UrlReferrer.ToString());
      }

      

      public static bool CheckData(string inputData)
      {
        if (Regex.IsMatch(inputData, StrRegex))
        {
          return true;
        }
        else
        {
          return false;
        }
      }
    }

    /// <summary>
    /// 在Global中调用
    /// </summary>

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
      if (Request.Cookies != null)
      {
        if (Safe_SQL Injection.CookieData())
        {
          Response.Write("您提交的Cookie数据有恶意字符!");
          Response.End();
        }
      }

      if (Request.UrlReferrer != null)
      {
        if (Safe_SQL Injection.referer())
        {
          Response.Write("您提交的Referrer数据有恶意字符!");
          Response.End();
        }
      }

      if (Request.RequestType.ToUpper() == "POST")
      {
        if (Safe_SQL Injection.PostData())
        {
          Response.Write("您提交的Post数据有恶意字符!");
          Response.End();
        }
      }

      if (Request.RequestType.ToUpper() == "GET")
      {
        if (Safe_SQL Injection.GetData())
        {
          Response.Write("您提交的Get数据有恶意字符!");
          Response.End();
        }
      }
    }

  • 相关阅读:
    LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    RTSP可用网络流
    Linux访问Github缓慢
    Ubu18.0-NVIDIA显卡驱动重装
    FFMPEG第一次学习
    QT-守护程序
    QT-局域网探测工具(简易版)--Ping
    QT-notepad++仿写
    Ubuntu 解压文件
    Ubuntu -换源
  • 原文地址:https://www.cnblogs.com/wucan/p/6272843.html
Copyright © 2020-2023  润新知