网站安全性:C 网站安全性测试
创始人
2024-05-07 05:50:32
0

对于网站的安全性,是每个网站开发者和运营者最关心的问题。网站一旦出现漏洞,那势必将造成很大的损失。为了提高网站的安全性,首先网站要防注入,最重要的是服务器的安全设施要做到位。

下面说下网站防注入的几点要素。

一:丢弃SQL语句直接拼接,虽然这个写起来很快很方便。

二:如果用SQL语句,那就使用参数化,添加Param

三:尽可能的使用存储过程,安全性能高而且处理速度也快

四:屏蔽SQL,javascript等注入(很是主要的),对于每个文件写是不太可能的。所以要找到对所有文件起作用的办法。我在网上收集了以下3种方法

C#防SQL注入方法一

在Web.config文件中, < appSettings>下面增加一个标签:如下

  1. < appSettings>   
  2. < add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />   
  3. < /appSettings>  

其中key是 < saveParameters>后面的值为"OrderId-int32"等,其中"-"前面表示参数的名称比如:OrderId,后面的int32表示数据类型。

C#防SQL注入方法二

在Global.asax中增加下面一段:  

  1. protected void Application_BeginRequest(Object sender, EventArgs e){   
  2. String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString().Split(',');   
  3. for(int i= 0 ;i <  safeParameters.Length; i++){   
  4. String parameterName = safeParameters[i].Split('-')[0];   
  5. String parameterType = safeParameters[i].Split('-')[1];   
  6. isValidParameter(parameterName, parameterType);   
  7. }   
  8. }   
  9.  
  10. public void isValidParameter(string parameterName, string parameterType){   
  11. string parameterValue = Request.QueryString[parameterName];   
  12. if(parameterValue == null) return;   
  13.  
  14. if(parameterType.Equals("int32")){   
  15. if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx");   
  16. }   
  17. else if (parameterType.Equals("USzip")){   
  18. if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx");   
  19. }   
  20. else if (parameterType.Equals("email")){   
  21. if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx");   
  22. }   
  23. }  

C#防SQL注入方法

使用字符串过滤类

  1. using System;  
  2.  
  3. namespace web.comm  
  4. {  
  5.     /**//// < summary>  
  6.     /// ProcessRequest 的摘要说明。  
  7.     /// < /summary>  
  8.     public class ProcessRequest  
  9.     {  
  10.         public ProcessRequest()  
  11.         {  
  12.             //  
  13.             // TODO: 在此处添加构造函数逻辑  
  14.             //  
  15.         }  
  16.  
  17.         SQL注入式攻击代码分析#region SQL注入式攻击代码分析  
  18.         /**//// < summary>  
  19.         /// 处理用户提交的请求  
  20.         /// < /summary>  
  21.         public static void StartProcessRequest()  
  22.         {  
  23.               
  24. //            System.Web.HttpContext.Current.Response.Write("< script>alert('dddd');< /script>");  
  25.             try 
  26.             {  
  27.                 string getkeys = "";  
  28.                 //string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();  
  29.                 if (System.Web.HttpContext.Current.Request.QueryString != null)  
  30.                 {  
  31.       
  32.                     for(int i=0;i< System.Web.HttpContext.Current.Request.QueryString.Count;i++)  
  33.                     {  
  34.                         getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];  
  35.                         if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys],0))  
  36.                         {  
  37.                             //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");  
  38.                             System.Web.HttpContext.Current.Response.Write("< script>alert('请勿非法提交!');history.back();< /script>");  
  39.                             System.Web.HttpContext.Current.Response.End();  
  40.                         }  
  41.                     }  
  42.                 }  
  43.                 if (System.Web.HttpContext.Current.Request.Form != null)  
  44.                 {  
  45.                     for(int i=0;i< System.Web.HttpContext.Current.Request.Form.Count;i++)  
  46.                     {  
  47.                         getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];  
  48.                         if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys],1))  
  49.                         {  
  50.                             //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");  
  51.                             System.Web.HttpContext.Current.Response.Write("< script>alert('请勿非法提交!');history.back();< /script>");  
  52.                             System.Web.HttpContext.Current.Response.End();  
  53.                         }  
  54.                     }  
  55.                 }  
  56.             }  
  57.             catch 
  58.             {  
  59.                 // 错误处理: 处理用户提交信息!  
  60.             }  
  61.         }  
  62.         /**//// < summary>  
  63.         /// 分析用户请求是否正常  
  64.         /// < /summary>  
  65.         /// < param name="Str">传入用户提交数据< /param>  
  66.         /// < returns>返回是否含有SQL注入式攻击代码< /returns>  
  67.         private static bool ProcessSqlStr(string Str,int type)  
  68.         {  
  69.             string SqlStr;  
  70.  
  71.             if(type == 1)  
  72.                 SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";  
  73.             else 
  74.                 SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";  
  75.  
  76.             bool ReturnValue = true;  
  77.             try 
  78.             {  
  79.                 if (Str != "")  
  80.                 {  
  81.                     string[] anySqlStr = SqlStr.Split('|');  
  82.                     foreach (string ss in anySqlStr)  
  83.                     {  
  84.                         if (Str.IndexOf(ss)>=0)  
  85.                         {  
  86.                             ReturnValue = false;  
  87.                         }  
  88.                     }  
  89.                 }  
  90.             }  
  91.             catch 
  92.             {  
  93.                 ReturnValue = false;  
  94.             }  
  95.             return ReturnValue;  
  96.         }  
  97.         #endregion  
  98.  
  99.     }  

【编辑推荐】

  1. 浅析C#启动停止SQL数据库服务之方法
  2. 总结C#获取当前路径的7种方法
  3. 浅析C# treeview控件的使用方法
  4. C#多态性的概念及其应用
  5. 介绍C#构造函数的使用方法

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...